summaryrefslogtreecommitdiff
path: root/main/performance.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main/performance.cpp')
-rw-r--r--main/performance.cpp174
1 files changed, 136 insertions, 38 deletions
diff --git a/main/performance.cpp b/main/performance.cpp
index c7e3a41aa6..7234511aeb 100644
--- a/main/performance.cpp
+++ b/main/performance.cpp
@@ -35,15 +35,20 @@
#include "scene/main/node.h"
#include "scene/main/scene_tree.h"
#include "servers/audio_server.h"
-#include "servers/physics_2d_server.h"
-#include "servers/physics_server.h"
-#include "servers/visual_server.h"
+#include "servers/physics_server_2d.h"
+#include "servers/physics_server_3d.h"
+#include "servers/rendering_server.h"
-Performance *Performance::singleton = NULL;
+Performance *Performance::singleton = nullptr;
void Performance::_bind_methods() {
-
ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor);
+ ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::add_custom_monitor, DEFVAL(Array()));
+ ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor);
+ ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor);
+ ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor);
+ ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time);
+ ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names);
BIND_ENUM_CONSTANT(TIME_FPS);
BIND_ENUM_CONSTANT(TIME_PROCESS);
@@ -79,13 +84,13 @@ void Performance::_bind_methods() {
float Performance::_get_node_count() const {
MainLoop *ml = OS::get_singleton()->get_main_loop();
SceneTree *sml = Object::cast_to<SceneTree>(ml);
- if (!sml)
+ if (!sml) {
return 0;
+ }
return sml->get_node_count();
}
String Performance::get_monitor_name(Monitor p_monitor) const {
-
ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, String());
static const char *names[MONITOR_MAX] = {
@@ -123,35 +128,61 @@ String Performance::get_monitor_name(Monitor p_monitor) const {
}
float Performance::get_monitor(Monitor p_monitor) const {
-
switch (p_monitor) {
- case TIME_FPS: return Engine::get_singleton()->get_frames_per_second();
- case TIME_PROCESS: return _process_time;
- case TIME_PHYSICS_PROCESS: return _physics_process_time;
- case MEMORY_STATIC: return Memory::get_mem_usage();
- case MEMORY_STATIC_MAX: return Memory::get_mem_max_usage();
- case MEMORY_MESSAGE_BUFFER_MAX: return MessageQueue::get_singleton()->get_max_buffer_usage();
- case OBJECT_COUNT: return ObjectDB::get_object_count();
- case OBJECT_RESOURCE_COUNT: return ResourceCache::get_cached_resource_count();
- case OBJECT_NODE_COUNT: return _get_node_count();
- case OBJECT_ORPHAN_NODE_COUNT: return Node::orphan_node_count;
- case RENDER_OBJECTS_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_OBJECTS_IN_FRAME);
- case RENDER_VERTICES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_VERTICES_IN_FRAME);
- case RENDER_MATERIAL_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_MATERIAL_CHANGES_IN_FRAME);
- case RENDER_SHADER_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_SHADER_CHANGES_IN_FRAME);
- case RENDER_SURFACE_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_SURFACE_CHANGES_IN_FRAME);
- case RENDER_DRAW_CALLS_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_DRAW_CALLS_IN_FRAME);
- case RENDER_VIDEO_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_VIDEO_MEM_USED);
- case RENDER_TEXTURE_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_TEXTURE_MEM_USED);
- case RENDER_VERTEX_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_VERTEX_MEM_USED);
- case RENDER_USAGE_VIDEO_MEM_TOTAL: return VS::get_singleton()->get_render_info(VS::INFO_USAGE_VIDEO_MEM_TOTAL);
- case PHYSICS_2D_ACTIVE_OBJECTS: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_ACTIVE_OBJECTS);
- case PHYSICS_2D_COLLISION_PAIRS: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_COLLISION_PAIRS);
- case PHYSICS_2D_ISLAND_COUNT: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_ISLAND_COUNT);
- case PHYSICS_3D_ACTIVE_OBJECTS: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_ACTIVE_OBJECTS);
- case PHYSICS_3D_COLLISION_PAIRS: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_COLLISION_PAIRS);
- case PHYSICS_3D_ISLAND_COUNT: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_ISLAND_COUNT);
- case AUDIO_OUTPUT_LATENCY: return AudioServer::get_singleton()->get_output_latency();
+ case TIME_FPS:
+ return Engine::get_singleton()->get_frames_per_second();
+ case TIME_PROCESS:
+ return _process_time;
+ case TIME_PHYSICS_PROCESS:
+ return _physics_process_time;
+ case MEMORY_STATIC:
+ return Memory::get_mem_usage();
+ case MEMORY_STATIC_MAX:
+ return Memory::get_mem_max_usage();
+ case MEMORY_MESSAGE_BUFFER_MAX:
+ return MessageQueue::get_singleton()->get_max_buffer_usage();
+ case OBJECT_COUNT:
+ return ObjectDB::get_object_count();
+ case OBJECT_RESOURCE_COUNT:
+ return ResourceCache::get_cached_resource_count();
+ case OBJECT_NODE_COUNT:
+ return _get_node_count();
+ case OBJECT_ORPHAN_NODE_COUNT:
+ return Node::orphan_node_count;
+ case RENDER_OBJECTS_IN_FRAME:
+ return RS::get_singleton()->get_render_info(RS::INFO_OBJECTS_IN_FRAME);
+ case RENDER_VERTICES_IN_FRAME:
+ return RS::get_singleton()->get_render_info(RS::INFO_VERTICES_IN_FRAME);
+ case RENDER_MATERIAL_CHANGES_IN_FRAME:
+ return RS::get_singleton()->get_render_info(RS::INFO_MATERIAL_CHANGES_IN_FRAME);
+ case RENDER_SHADER_CHANGES_IN_FRAME:
+ return RS::get_singleton()->get_render_info(RS::INFO_SHADER_CHANGES_IN_FRAME);
+ case RENDER_SURFACE_CHANGES_IN_FRAME:
+ return RS::get_singleton()->get_render_info(RS::INFO_SURFACE_CHANGES_IN_FRAME);
+ case RENDER_DRAW_CALLS_IN_FRAME:
+ return RS::get_singleton()->get_render_info(RS::INFO_DRAW_CALLS_IN_FRAME);
+ case RENDER_VIDEO_MEM_USED:
+ return RS::get_singleton()->get_render_info(RS::INFO_VIDEO_MEM_USED);
+ case RENDER_TEXTURE_MEM_USED:
+ return RS::get_singleton()->get_render_info(RS::INFO_TEXTURE_MEM_USED);
+ case RENDER_VERTEX_MEM_USED:
+ return RS::get_singleton()->get_render_info(RS::INFO_VERTEX_MEM_USED);
+ case RENDER_USAGE_VIDEO_MEM_TOTAL:
+ return RS::get_singleton()->get_render_info(RS::INFO_USAGE_VIDEO_MEM_TOTAL);
+ case PHYSICS_2D_ACTIVE_OBJECTS:
+ return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ACTIVE_OBJECTS);
+ case PHYSICS_2D_COLLISION_PAIRS:
+ return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_COLLISION_PAIRS);
+ case PHYSICS_2D_ISLAND_COUNT:
+ return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ISLAND_COUNT);
+ case PHYSICS_3D_ACTIVE_OBJECTS:
+ return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ACTIVE_OBJECTS);
+ case PHYSICS_3D_COLLISION_PAIRS:
+ return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_COLLISION_PAIRS);
+ case PHYSICS_3D_ISLAND_COUNT:
+ return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ISLAND_COUNT);
+ case AUDIO_OUTPUT_LATENCY:
+ return AudioServer::get_singleton()->get_output_latency();
default: {
}
@@ -199,18 +230,85 @@ Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const
}
void Performance::set_process_time(float p_pt) {
-
_process_time = p_pt;
}
void Performance::set_physics_process_time(float p_pt) {
-
_physics_process_time = p_pt;
}
-Performance::Performance() {
+void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
+ ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists.");
+ _monitor_map.insert(p_id, MonitorCall(p_callable, p_args));
+ _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
+}
+void Performance::remove_custom_monitor(const StringName &p_id) {
+ ERR_FAIL_COND_MSG(!has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
+ _monitor_map.erase(p_id);
+ _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
+}
+
+bool Performance::has_custom_monitor(const StringName &p_id) {
+ return _monitor_map.has(p_id);
+}
+
+Variant Performance::get_custom_monitor(const StringName &p_id) {
+ ERR_FAIL_COND_V_MSG(!has_custom_monitor(p_id), Variant(), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
+ bool error;
+ String error_message;
+ Variant return_value = _monitor_map[p_id].call(error, error_message);
+ ERR_FAIL_COND_V_MSG(error, return_value, "Error calling from custom monitor '" + String(p_id) + "' to callable: " + error_message);
+ return return_value;
+}
+
+Array Performance::get_custom_monitor_names() {
+ if (!_monitor_map.size()) {
+ return Array();
+ }
+ Array return_array;
+ return_array.resize(_monitor_map.size());
+ int index = 0;
+ for (OrderedHashMap<StringName, MonitorCall>::Element i = _monitor_map.front(); i; i = i.next()) {
+ return_array.set(index, i.key());
+ index++;
+ }
+ return return_array;
+}
+
+uint64_t Performance::get_monitor_modification_time() {
+ return _monitor_modification_time;
+}
+
+Performance::Performance() {
_process_time = 0;
_physics_process_time = 0;
+ _monitor_modification_time = 0;
singleton = this;
}
+
+Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) {
+ _callable = p_callable;
+ _arguments = p_arguments;
+}
+
+Performance::MonitorCall::MonitorCall() {
+}
+
+Variant Performance::MonitorCall::call(bool &r_error, String &r_error_message) {
+ Vector<const Variant *> arguments_mem;
+ arguments_mem.resize(_arguments.size());
+ for (int i = 0; i < _arguments.size(); i++) {
+ arguments_mem.write[i] = &_arguments[i];
+ }
+ const Variant **args = (const Variant **)arguments_mem.ptr();
+ int argc = _arguments.size();
+ Variant return_value;
+ Callable::CallError error;
+ _callable.call(args, argc, return_value, error);
+ r_error = (error.error != Callable::CallError::CALL_OK);
+ if (r_error) {
+ r_error_message = Variant::get_callable_error_text(_callable, args, argc, error);
+ }
+ return return_value;
+}