Skip to content

Commit

Permalink
Implement Autostart Feature for Profiler / Visual Profiler / Network …
Browse files Browse the repository at this point in the history
…Profiler

Co-authored-by: stmSi <stm1998sithumyo@gmail.com>
  • Loading branch information
Geometror and stmSi committed Sep 10, 2024
1 parent 27552a2 commit c53fd9c
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 12 deletions.
18 changes: 15 additions & 3 deletions editor/debugger/editor_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion editor/debugger/editor_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand Down
19 changes: 15 additions & 4 deletions editor/debugger/editor_visual_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion editor/debugger/editor_visual_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
12 changes: 10 additions & 2 deletions editor/debugger/script_editor_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,14 @@ void ScriptEditorDebugger::start(Ref<RemoteDebuggerPeer> 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() {
Expand Down Expand Up @@ -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();
Expand Down
40 changes: 39 additions & 1 deletion modules/multiplayer/editor/editor_network_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down Expand Up @@ -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());
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions modules/multiplayer/editor/editor_network_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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();
};

Expand Down
2 changes: 2 additions & 0 deletions modules/multiplayer/editor/multiplayer_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit c53fd9c

Please sign in to comment.