From c53fd9c7be09d0eac2a000816dc7439cc0f568a7 Mon Sep 17 00:00:00 2001 From: Hendrik Brucker Date: Tue, 10 Sep 2024 19:40:42 +0200 Subject: [PATCH] Implement Autostart Feature for Profiler / Visual Profiler / Network Profiler Co-authored-by: stmSi --- editor/debugger/editor_profiler.cpp | 18 +++++++-- editor/debugger/editor_profiler.h | 3 +- editor/debugger/editor_visual_profiler.cpp | 19 +++++++-- editor/debugger/editor_visual_profiler.h | 3 +- editor/debugger/script_editor_debugger.cpp | 12 +++++- .../editor/editor_network_profiler.cpp | 40 ++++++++++++++++++- .../editor/editor_network_profiler.h | 6 +++ .../editor/multiplayer_editor_plugin.cpp | 2 + 8 files changed, 91 insertions(+), 12 deletions(-) diff --git a/editor/debugger/editor_profiler.cpp b/editor/debugger/editor_profiler.cpp index 24bb6948608..d244b6b4cd9 100644 --- a/editor/debugger/editor_profiler.cpp +++ b/editor/debugger/editor_profiler.cpp @@ -35,6 +35,7 @@ #include "editor/editor_string_names.h" #include "editor/themes/editor_scale.h" #include "editor/themes/editor_theme_manager.h" +#include "scene/gui/check_box.h" #include "scene/resources/image_texture.h" void EditorProfiler::_make_metric_ptrs(Metric &m) { @@ -177,8 +178,8 @@ void EditorProfiler::_item_edited() { } void EditorProfiler::_update_plot() { - const int w = graph->get_size().width; - const int h = graph->get_size().height; + const int w = MAX(1, graph->get_size().width); // Clamp to 1 to prevent from crashing when profiler is autostarted. + const int h = MAX(1, graph->get_size().height); bool reset_texture = false; const int desired_len = w * h * 4; @@ -416,6 +417,10 @@ void EditorProfiler::_internal_profiles_pressed() { _combo_changed(0); } +void EditorProfiler::_autostart_toggled(bool p_toggled_on) { + EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_profiler", p_toggled_on); +} + void EditorProfiler::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: @@ -539,9 +544,10 @@ void EditorProfiler::set_enabled(bool p_enable, bool p_clear) { } } -void EditorProfiler::set_pressed(bool p_pressed) { +void EditorProfiler::set_profiling(bool p_pressed) { activate->set_pressed(p_pressed); _update_button_text(); + emit_signal(SNAME("enable_profiling"), activate->is_pressed()); } bool EditorProfiler::is_profiling() { @@ -633,6 +639,12 @@ EditorProfiler::EditorProfiler() { clear_button->set_disabled(true); hb->add_child(clear_button); + CheckBox *autostart_checkbox = memnew(CheckBox); + autostart_checkbox->set_text(TTR("Autostart")); + autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_profiler", false)); + autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorProfiler::_autostart_toggled)); + hb->add_child(autostart_checkbox); + hb->add_child(memnew(Label(TTR("Measure:")))); display_mode = memnew(OptionButton); diff --git a/editor/debugger/editor_profiler.h b/editor/debugger/editor_profiler.h index 64253070b14..8de276be437 100644 --- a/editor/debugger/editor_profiler.h +++ b/editor/debugger/editor_profiler.h @@ -138,6 +138,7 @@ class EditorProfiler : public VBoxContainer { void _activate_pressed(); void _clear_pressed(); + void _autostart_toggled(bool p_toggled_on); void _internal_profiles_pressed(); @@ -168,7 +169,7 @@ class EditorProfiler : public VBoxContainer { public: void add_frame_metric(const Metric &p_metric, bool p_final = false); void set_enabled(bool p_enable, bool p_clear = true); - void set_pressed(bool p_pressed); + void set_profiling(bool p_pressed); bool is_profiling(); bool is_seeking() { return seeking; } void disable_seeking(); diff --git a/editor/debugger/editor_visual_profiler.cpp b/editor/debugger/editor_visual_profiler.cpp index 56c9db44cd9..d4859fbe4d9 100644 --- a/editor/debugger/editor_visual_profiler.cpp +++ b/editor/debugger/editor_visual_profiler.cpp @@ -148,8 +148,8 @@ void EditorVisualProfiler::_item_selected() { } void EditorVisualProfiler::_update_plot() { - const int w = graph->get_size().width; - const int h = graph->get_size().height; + const int w = graph->get_size().width + 1; // `+1` is to prevent from crashing when visual profiler is auto started. + const int h = graph->get_size().height + 1; bool reset_texture = false; @@ -427,6 +427,10 @@ void EditorVisualProfiler::_clear_pressed() { _update_plot(); } +void EditorVisualProfiler::_autostart_toggled(bool p_toggled_on) { + EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_visual_profiler", p_toggled_on); +} + void EditorVisualProfiler::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: @@ -668,9 +672,10 @@ void EditorVisualProfiler::set_enabled(bool p_enable) { activate->set_disabled(!p_enable); } -void EditorVisualProfiler::set_pressed(bool p_pressed) { - activate->set_pressed(p_pressed); +void EditorVisualProfiler::set_profiling(bool p_profiling) { + activate->set_pressed(p_profiling); _update_button_text(); + emit_signal(SNAME("enable_profiling"), activate->is_pressed()); } bool EditorVisualProfiler::is_profiling() { @@ -747,6 +752,12 @@ EditorVisualProfiler::EditorVisualProfiler() { clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorVisualProfiler::_clear_pressed)); hb->add_child(clear_button); + CheckBox *autostart_checkbox = memnew(CheckBox); + autostart_checkbox->set_text(TTR("Autostart")); + autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_visual_profiler", false)); + autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorVisualProfiler::_autostart_toggled)); + hb->add_child(autostart_checkbox); + hb->add_child(memnew(Label(TTR("Measure:")))); display_mode = memnew(OptionButton); diff --git a/editor/debugger/editor_visual_profiler.h b/editor/debugger/editor_visual_profiler.h index 492985506ab..a8ed58132eb 100644 --- a/editor/debugger/editor_visual_profiler.h +++ b/editor/debugger/editor_visual_profiler.h @@ -109,6 +109,7 @@ class EditorVisualProfiler : public VBoxContainer { void _activate_pressed(); void _clear_pressed(); + void _autostart_toggled(bool p_toggled_on); String _get_time_as_text(float p_time); @@ -137,7 +138,7 @@ class EditorVisualProfiler : public VBoxContainer { public: void add_frame_metric(const Metric &p_metric); void set_enabled(bool p_enable); - void set_pressed(bool p_pressed); + void set_profiling(bool p_profiling); bool is_profiling(); bool is_seeking() { return seeking; } void disable_seeking(); diff --git a/editor/debugger/script_editor_debugger.cpp b/editor/debugger/script_editor_debugger.cpp index 5e96daf69c8..b798bdf9c19 100644 --- a/editor/debugger/script_editor_debugger.cpp +++ b/editor/debugger/script_editor_debugger.cpp @@ -1012,6 +1012,14 @@ void ScriptEditorDebugger::start(Ref p_peer) { _set_reason_text(TTR("Debug session started."), MESSAGE_SUCCESS); _update_buttons_state(); emit_signal(SNAME("started")); + + if (EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_profiler", false)) { + profiler->set_profiling(true); + } + + if (EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_visual_profiler", false)) { + visual_profiler->set_profiling(true); + } } void ScriptEditorDebugger::_update_buttons_state() { @@ -1076,10 +1084,10 @@ void ScriptEditorDebugger::stop() { profiler_signature.clear(); profiler->set_enabled(false, false); - profiler->set_pressed(false); + profiler->set_profiling(false); visual_profiler->set_enabled(false); - visual_profiler->set_pressed(false); + visual_profiler->set_profiling(false); inspector->edit(nullptr); _update_buttons_state(); diff --git a/modules/multiplayer/editor/editor_network_profiler.cpp b/modules/multiplayer/editor/editor_network_profiler.cpp index 212fd1ef6bc..3a51712c701 100644 --- a/modules/multiplayer/editor/editor_network_profiler.cpp +++ b/modules/multiplayer/editor/editor_network_profiler.cpp @@ -34,6 +34,7 @@ #include "editor/editor_settings.h" #include "editor/editor_string_names.h" #include "editor/themes/editor_scale.h" +#include "scene/gui/check_box.h" void EditorNetworkProfiler::_bind_methods() { ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable"))); @@ -170,15 +171,42 @@ void EditorNetworkProfiler::add_node_data(const NodeInfo &p_info) { } void EditorNetworkProfiler::_activate_pressed() { + _update_button_text(); + if (activate->is_pressed()) { refresh_timer->start(); + } else { + refresh_timer->stop(); + } + + emit_signal(SNAME("enable_profiling"), activate->is_pressed()); +} + +void EditorNetworkProfiler::_update_button_text() { + if (activate->is_pressed()) { activate->set_icon(theme_cache.stop_icon); activate->set_text(TTR("Stop")); } else { - refresh_timer->stop(); activate->set_icon(theme_cache.play_icon); activate->set_text(TTR("Start")); } +} + +void EditorNetworkProfiler::started() { + if (EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false)) { + set_profiling(true); + refresh_timer->start(); + } +} + +void EditorNetworkProfiler::stopped() { + set_profiling(false); + refresh_timer->stop(); +} + +void EditorNetworkProfiler::set_profiling(bool p_pressed) { + activate->set_pressed(p_pressed); + _update_button_text(); emit_signal(SNAME("enable_profiling"), activate->is_pressed()); } @@ -192,6 +220,10 @@ void EditorNetworkProfiler::_clear_pressed() { refresh_replication_data(); } +void EditorNetworkProfiler::_autostart_toggled(bool p_toggled_on) { + EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_network_profiler", p_toggled_on); +} + void EditorNetworkProfiler::_replication_button_clicked(TreeItem *p_item, int p_column, int p_idx, MouseButton p_button) { if (!p_item) { return; @@ -268,6 +300,12 @@ EditorNetworkProfiler::EditorNetworkProfiler() { clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_clear_pressed)); hb->add_child(clear_button); + CheckBox *autostart_checkbox = memnew(CheckBox); + autostart_checkbox->set_text(TTR("Autostart")); + autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false)); + autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorNetworkProfiler::_autostart_toggled)); + hb->add_child(autostart_checkbox); + hb->add_spacer(); Label *lb = memnew(Label); diff --git a/modules/multiplayer/editor/editor_network_profiler.h b/modules/multiplayer/editor/editor_network_profiler.h index b4f8ffa7242..46931c9fc97 100644 --- a/modules/multiplayer/editor/editor_network_profiler.h +++ b/modules/multiplayer/editor/editor_network_profiler.h @@ -92,7 +92,9 @@ class EditorNetworkProfiler : public VBoxContainer { void _activate_pressed(); void _clear_pressed(); + void _autostart_toggled(bool p_toggled_on); void _refresh(); + void _update_button_text(); void _replication_button_clicked(TreeItem *p_item, int p_column, int p_idx, MouseButton p_button); protected: @@ -112,6 +114,10 @@ class EditorNetworkProfiler : public VBoxContainer { void set_bandwidth(int p_incoming, int p_outgoing); bool is_profiling(); + void set_profiling(bool p_pressed); + void started(); + void stopped(); + EditorNetworkProfiler(); }; diff --git a/modules/multiplayer/editor/multiplayer_editor_plugin.cpp b/modules/multiplayer/editor/multiplayer_editor_plugin.cpp index a496f5dfa29..817d503aeca 100644 --- a/modules/multiplayer/editor/multiplayer_editor_plugin.cpp +++ b/modules/multiplayer/editor/multiplayer_editor_plugin.cpp @@ -106,6 +106,8 @@ void MultiplayerEditorDebugger::setup_session(int p_session_id) { profiler->connect("enable_profiling", callable_mp(this, &MultiplayerEditorDebugger::_profiler_activate).bind(p_session_id)); profiler->connect("open_request", callable_mp(this, &MultiplayerEditorDebugger::_open_request)); profiler->set_name(TTR("Network Profiler")); + session->connect("started", callable_mp(profiler, &EditorNetworkProfiler::started)); + session->connect("stopped", callable_mp(profiler, &EditorNetworkProfiler::stopped)); session->add_session_tab(profiler); profilers[p_session_id] = profiler; }