diff options
Diffstat (limited to 'main')
-rw-r--r-- | main/main.cpp | 38 | ||||
-rw-r--r-- | main/main.h | 1 | ||||
-rw-r--r-- | main/performance.cpp | 76 | ||||
-rw-r--r-- | main/performance.h | 22 | ||||
-rw-r--r-- | main/tests/test_string.cpp | 2 |
5 files changed, 102 insertions, 37 deletions
diff --git a/main/main.cpp b/main/main.cpp index 00760b39b0..76175780a3 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -856,7 +856,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph } } else if (I->get() == "--allow_focus_steal_pid") { // not exposed to user if (I->next()) { - allow_focus_steal_pid = I->next()->get().to_int64(); + allow_focus_steal_pid = I->next()->get().to_int(); N = I->next()->next(); } else { OS::get_singleton()->print("Missing editor PID argument, aborting.\n"); @@ -1620,7 +1620,7 @@ bool Main::start() { { DirAccessRef da = DirAccess::open(doc_tool); - ERR_FAIL_COND_V_MSG(!da, false, "Argument supplied to --doctool must be a base Godot build directory."); + ERR_FAIL_COND_V_MSG(!da, false, "Argument supplied to --doctool must be a valid directory path."); } #ifndef MODULE_MONO_ENABLED @@ -2115,7 +2115,6 @@ bool Main::start() { */ uint64_t Main::last_ticks = 0; -uint64_t Main::target_ticks = 0; uint32_t Main::frames = 0; uint32_t Main::frame = 0; bool Main::force_redraw_requested = false; @@ -2266,38 +2265,7 @@ bool Main::iteration() { return exit; } - const uint32_t frame_delay = Engine::get_singleton()->get_frame_delay(); - if (frame_delay) { - // Add fixed frame delay to decrease CPU/GPU usage. This doesn't take - // the actual frame time into account. - // Due to the high fluctuation of the actual sleep duration, it's not recommended - // to use this as a FPS limiter. - OS::get_singleton()->delay_usec(frame_delay * 1000); - } - - // Add a dynamic frame delay to decrease CPU/GPU usage. This takes the - // previous frame time into account for a smoother result. - uint64_t dynamic_delay = 0; - if (OS::get_singleton()->is_in_low_processor_usage_mode() || !DisplayServer::get_singleton()->window_can_draw()) { - dynamic_delay = OS::get_singleton()->get_low_processor_usage_mode_sleep_usec(); - } - const int target_fps = Engine::get_singleton()->get_target_fps(); - if (target_fps > 0 && !Engine::get_singleton()->is_editor_hint()) { - // Override the low processor usage mode sleep delay if the target FPS is lower. - dynamic_delay = MAX(dynamic_delay, (uint64_t)(1000000 / target_fps)); - } - - if (dynamic_delay > 0) { - target_ticks += dynamic_delay; - uint64_t current_ticks = OS::get_singleton()->get_ticks_usec(); - - if (current_ticks < target_ticks) { - OS::get_singleton()->delay_usec(target_ticks - current_ticks); - } - - current_ticks = OS::get_singleton()->get_ticks_usec(); - target_ticks = MIN(MAX(target_ticks, current_ticks - dynamic_delay), current_ticks + dynamic_delay); - } + OS::get_singleton()->add_frame_delay(DisplayServer::get_singleton()->window_can_draw()); #ifdef TOOLS_ENABLED if (auto_build_solutions) { diff --git a/main/main.h b/main/main.h index ab6917a65c..308128735c 100644 --- a/main/main.h +++ b/main/main.h @@ -38,7 +38,6 @@ class Main { static void print_help(const char *p_binary); static uint64_t last_ticks; - static uint64_t target_ticks; static uint32_t frames; static uint32_t frame; static bool force_redraw_requested; diff --git a/main/performance.cpp b/main/performance.cpp index 7e6b9fca64..7234511aeb 100644 --- a/main/performance.cpp +++ b/main/performance.cpp @@ -43,6 +43,12 @@ 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); @@ -231,8 +237,78 @@ void Performance::set_physics_process_time(float p_pt) { _physics_process_time = p_pt; } +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; +} diff --git a/main/performance.h b/main/performance.h index ddbe45fa00..5f88a24c0f 100644 --- a/main/performance.h +++ b/main/performance.h @@ -32,6 +32,7 @@ #define PERFORMANCE_H #include "core/object.h" +#include "core/ordered_hash_map.h" #define PERF_WARN_OFFLINE_FUNCTION #define PERF_WARN_PROCESS_SYNC @@ -47,6 +48,19 @@ class Performance : public Object { float _process_time; float _physics_process_time; + class MonitorCall { + Callable _callable; + Vector<Variant> _arguments; + + public: + MonitorCall(Callable p_callable, Vector<Variant> p_arguments); + MonitorCall(); + Variant call(bool &r_error, String &r_error_message); + }; + + OrderedHashMap<StringName, MonitorCall> _monitor_map; + uint64_t _monitor_modification_time; + public: enum Monitor { @@ -95,6 +109,14 @@ public: void set_process_time(float p_pt); void set_physics_process_time(float p_pt); + void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args); + void remove_custom_monitor(const StringName &p_id); + bool has_custom_monitor(const StringName &p_id); + Variant get_custom_monitor(const StringName &p_id); + Array get_custom_monitor_names(); + + uint64_t get_monitor_modification_time(); + static Performance *get_singleton() { return singleton; } Performance(); diff --git a/main/tests/test_string.cpp b/main/tests/test_string.cpp index 775c039282..5a14492be5 100644 --- a/main/tests/test_string.cpp +++ b/main/tests/test_string.cpp @@ -370,7 +370,7 @@ bool test_22() { static const int num[4] = { 1237461283, -22, 0, -1123412 }; for (int i = 0; i < 4; i++) { - OS::get_singleton()->print("\tString: \"%s\" as Int is %i\n", nums[i], String(nums[i]).to_int()); + OS::get_singleton()->print("\tString: \"%s\" as Int is %lli\n", nums[i], (long long)(String(nums[i]).to_int())); if (String(nums[i]).to_int() != num[i]) { return false; |