Path: blob/master/editor/debugger/editor_performance_profiler.cpp
9896 views
/**************************************************************************/1/* editor_performance_profiler.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_performance_profiler.h"3132#include "editor/editor_string_names.h"33#include "editor/inspector/editor_property_name_processor.h"34#include "editor/settings/editor_settings.h"35#include "editor/themes/editor_scale.h"36#include "editor/themes/editor_theme_manager.h"37#include "main/performance.h"3839EditorPerformanceProfiler::Monitor::Monitor(const String &p_name, const String &p_base, int p_frame_index, Performance::MonitorType p_type, TreeItem *p_item) {40type = p_type;41item = p_item;42frame_index = p_frame_index;43name = p_name;44base = p_base;45}4647void EditorPerformanceProfiler::Monitor::update_value(float p_value) {48ERR_FAIL_NULL(item);49String label = EditorPerformanceProfiler::_create_label(p_value, type);50String tooltip = label;51switch (type) {52case Performance::MONITOR_TYPE_MEMORY: {53tooltip = label;54} break;55case Performance::MONITOR_TYPE_TIME: {56tooltip = label;57} break;58default: {59tooltip += " " + item->get_text(0);60} break;61}62item->set_text(1, label);63item->set_tooltip_text(1, tooltip);6465if (p_value > max) {66max = p_value;67}68}6970void EditorPerformanceProfiler::Monitor::reset() {71history.clear();72max = 0.0f;73if (item) {74item->set_text(1, "");75item->set_tooltip_text(1, "");76}77}7879String EditorPerformanceProfiler::_create_label(float p_value, Performance::MonitorType p_type) {80switch (p_type) {81case Performance::MONITOR_TYPE_QUANTITY: {82return TS->format_number(itos(p_value));83}84case Performance::MONITOR_TYPE_MEMORY: {85return String::humanize_size(p_value);86}87case Performance::MONITOR_TYPE_TIME: {88return TS->format_number(rtos(p_value * 1000).pad_decimals(2)) + " " + TTR("ms");89}90default: {91return TS->format_number(rtos(p_value));92}93}94}9596void EditorPerformanceProfiler::_monitor_select() {97monitor_draw->queue_redraw();98}99100void EditorPerformanceProfiler::_monitor_draw() {101Vector<StringName> active;102for (const KeyValue<StringName, Monitor> &E : monitors) {103if (E.value.item->is_checked(0)) {104active.push_back(E.key);105}106}107108if (active.is_empty()) {109info_message->show();110return;111}112113info_message->hide();114115Ref<StyleBox> graph_style_box = get_theme_stylebox(CoreStringName(normal), SNAME("TextEdit"));116Ref<Font> graph_font = get_theme_font(SceneStringName(font), SNAME("TextEdit"));117int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("TextEdit"));118119int columns = int(Math::ceil(Math::sqrt(float(active.size()))));120int rows = int(Math::ceil(float(active.size()) / float(columns)));121if (active.size() == 1) {122rows = 1;123}124Size2i cell_size = Size2i(monitor_draw->get_size()) / Size2i(columns, rows);125float spacing = float(POINT_SEPARATION) / float(columns);126float value_multiplier = EditorThemeManager::is_dark_theme() ? 1.4f : 0.55f;127float hue_shift = 1.0f / float(monitors.size());128129for (int i = 0; i < active.size(); i++) {130Monitor ¤t = monitors[active[i]];131Rect2i rect(Point2i(i % columns, i / columns) * cell_size + Point2i(MARGIN, MARGIN), cell_size - Point2i(MARGIN, MARGIN) * 2);132monitor_draw->draw_style_box(graph_style_box, rect);133134rect.position += graph_style_box->get_offset();135rect.size -= graph_style_box->get_minimum_size();136Color draw_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));137draw_color.set_hsv(Math::fmod(hue_shift * float(current.frame_index), 0.9f), draw_color.get_s() * 0.9f, draw_color.get_v() * value_multiplier, 0.6f);138monitor_draw->draw_string(graph_font, rect.position + Point2(0, graph_font->get_ascent(font_size)), current.item->get_text(0), HORIZONTAL_ALIGNMENT_LEFT, rect.size.x, font_size, draw_color);139140draw_color.a = 0.9f;141float value_position = rect.size.width - graph_font->get_string_size(current.item->get_text(1), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;142if (value_position < 0) {143value_position = 0;144}145monitor_draw->draw_string(graph_font, rect.position + Point2(value_position, graph_font->get_ascent(font_size)), current.item->get_text(1), HORIZONTAL_ALIGNMENT_LEFT, rect.size.x, font_size, draw_color);146147rect.position.y += graph_font->get_height(font_size);148rect.size.height -= graph_font->get_height(font_size);149150int line_count = rect.size.height / (graph_font->get_height(font_size) * 2);151if (line_count > 5) {152line_count = 5;153}154if (line_count > 0) {155Color horizontal_line_color;156horizontal_line_color.set_hsv(draw_color.get_h(), draw_color.get_s() * 0.5f, draw_color.get_v() * 0.5f, 0.3f);157monitor_draw->draw_line(rect.position, rect.position + Vector2(rect.size.width, 0), horizontal_line_color, Math::round(EDSCALE));158monitor_draw->draw_string(graph_font, rect.position + Vector2(0, graph_font->get_ascent(font_size)), _create_label(current.max, current.type), HORIZONTAL_ALIGNMENT_LEFT, rect.size.width, font_size, horizontal_line_color);159160for (int j = 0; j < line_count; j++) {161Vector2 y_offset = Vector2(0, rect.size.height * (1.0f - float(j) / float(line_count)));162monitor_draw->draw_line(rect.position + y_offset, rect.position + Vector2(rect.size.width, 0) + y_offset, horizontal_line_color, Math::round(EDSCALE));163monitor_draw->draw_string(graph_font, rect.position - Vector2(0, graph_font->get_descent(font_size)) + y_offset, _create_label(current.max * float(j) / float(line_count), current.type), HORIZONTAL_ALIGNMENT_LEFT, rect.size.width, font_size, horizontal_line_color);164}165}166167float from = rect.size.width;168float prev = -1.0f;169int count = 0;170List<float>::Element *e = current.history.front();171172while (from >= 0 && e) {173float m = current.max;174float h2 = 0;175if (m != 0) {176h2 = (e->get() / m);177}178h2 = (1.0f - h2) * float(rect.size.y);179if (e != current.history.front()) {180monitor_draw->draw_line(rect.position + Point2(from, h2), rect.position + Point2(from + spacing, prev), draw_color, Math::round(EDSCALE));181}182183if (marker_key == active[i] && count == marker_frame) {184Color line_color;185line_color.set_hsv(draw_color.get_h(), draw_color.get_s() * 0.8f, draw_color.get_v(), 0.5f);186monitor_draw->draw_line(rect.position + Point2(from, 0), rect.position + Point2(from, rect.size.y), line_color, Math::round(EDSCALE));187188String label = _create_label(e->get(), current.type);189Size2 size = graph_font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);190Vector2 text_top_left_position = Vector2(from, h2) - (size + Vector2(MARKER_MARGIN, MARKER_MARGIN));191if (text_top_left_position.x < 0) {192text_top_left_position.x = from + MARKER_MARGIN;193}194if (text_top_left_position.y < 0) {195text_top_left_position.y = h2 + MARKER_MARGIN;196}197monitor_draw->draw_string(graph_font, rect.position + text_top_left_position + Point2(0, graph_font->get_ascent(font_size)), label, HORIZONTAL_ALIGNMENT_LEFT, rect.size.x, font_size, line_color);198}199prev = h2;200e = e->next();201from -= spacing;202count++;203}204}205}206207void EditorPerformanceProfiler::_build_monitor_tree() {208HashSet<StringName> monitor_checked;209for (KeyValue<StringName, Monitor> &E : monitors) {210if (E.value.item && E.value.item->is_checked(0)) {211monitor_checked.insert(E.key);212}213}214215base_map.clear();216monitor_tree->get_root()->clear_children();217218for (KeyValue<StringName, Monitor> &E : monitors) {219TreeItem *base = _get_monitor_base(E.value.base);220TreeItem *item = _create_monitor_item(E.value.name, base);221item->set_checked(0, monitor_checked.has(E.key));222E.value.item = item;223if (!E.value.history.is_empty()) {224E.value.update_value(E.value.history.front()->get());225}226}227}228229TreeItem *EditorPerformanceProfiler::_get_monitor_base(const StringName &p_base_name) {230if (base_map.has(p_base_name)) {231return base_map[p_base_name];232}233234TreeItem *base = monitor_tree->create_item(monitor_tree->get_root());235base->set_text(0, EditorPropertyNameProcessor::get_singleton()->process_name(p_base_name, EditorPropertyNameProcessor::get_settings_style()));236base->set_editable(0, false);237base->set_selectable(0, false);238base->set_expand_right(0, true);239if (is_inside_tree()) {240base->set_custom_font(0, get_theme_font(SNAME("bold"), EditorStringName(EditorFonts)));241}242base_map.insert(p_base_name, base);243return base;244}245246TreeItem *EditorPerformanceProfiler::_create_monitor_item(const StringName &p_monitor_name, TreeItem *p_base) {247TreeItem *item = monitor_tree->create_item(p_base);248item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);249item->set_editable(0, true);250item->set_selectable(0, false);251item->set_selectable(1, false);252item->set_text(0, EditorPropertyNameProcessor::get_singleton()->process_name(p_monitor_name, EditorPropertyNameProcessor::get_settings_style()));253return item;254}255256void EditorPerformanceProfiler::_marker_input(const Ref<InputEvent> &p_event) {257Ref<InputEventMouseButton> mb = p_event;258if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {259Vector<StringName> active;260for (KeyValue<StringName, Monitor> &E : monitors) {261if (E.value.item->is_checked(0)) {262active.push_back(E.key);263}264}265if (active.size() > 0) {266int columns = int(Math::ceil(Math::sqrt(float(active.size()))));267int rows = int(Math::ceil(float(active.size()) / float(columns)));268if (active.size() == 1) {269rows = 1;270}271Size2i cell_size = Size2i(monitor_draw->get_size()) / Size2i(columns, rows);272Vector2i index = mb->get_position() / cell_size;273Rect2i rect(index * cell_size + Point2i(MARGIN, MARGIN), cell_size - Point2i(MARGIN, MARGIN) * 2);274if (rect.has_point(mb->get_position())) {275if (index.x + index.y * columns < active.size()) {276marker_key = active[index.x + index.y * columns];277} else {278marker_key = "";279}280Ref<StyleBox> graph_style_box = get_theme_stylebox(CoreStringName(normal), SNAME("TextEdit"));281rect.position += graph_style_box->get_offset();282rect.size -= graph_style_box->get_minimum_size();283Vector2 point = mb->get_position() - rect.position;284if (point.x >= rect.size.x) {285marker_frame = 0;286} else {287int point_sep = 5;288float spacing = float(point_sep) / float(columns);289marker_frame = (rect.size.x - point.x) / spacing;290}291monitor_draw->queue_redraw();292return;293}294}295marker_key = "";296monitor_draw->queue_redraw();297}298}299300void EditorPerformanceProfiler::reset() {301HashMap<StringName, Monitor>::Iterator E = monitors.begin();302while (E != monitors.end()) {303HashMap<StringName, Monitor>::Iterator N = E;304++N;305if (String(E->key).begins_with("custom:")) {306monitors.remove(E);307} else {308E->value.reset();309}310E = N;311}312313_build_monitor_tree();314marker_key = "";315marker_frame = 0;316monitor_draw->queue_redraw();317}318319void EditorPerformanceProfiler::update_monitors(const Vector<StringName> &p_names) {320HashMap<StringName, int> names;321for (int i = 0; i < p_names.size(); i++) {322names.insert("custom:" + p_names[i], Performance::MONITOR_MAX + i);323}324325{326HashMap<StringName, Monitor>::Iterator E = monitors.begin();327while (E != monitors.end()) {328HashMap<StringName, Monitor>::Iterator N = E;329++N;330if (String(E->key).begins_with("custom:")) {331if (!names.has(E->key)) {332monitors.remove(E);333} else {334E->value.frame_index = names[E->key];335names.erase(E->key);336}337}338E = N;339}340}341342for (const KeyValue<StringName, int> &E : names) {343String name = String(E.key).replace_first("custom:", "");344String base = "Custom";345if (name.get_slice_count("/") == 2) {346base = name.get_slicec('/', 0);347name = name.get_slicec('/', 1);348}349monitors.insert(E.key, Monitor(name, base, E.value, Performance::MONITOR_TYPE_QUANTITY, nullptr));350}351352_build_monitor_tree();353}354355void EditorPerformanceProfiler::add_profile_frame(const Vector<float> &p_values) {356for (KeyValue<StringName, Monitor> &E : monitors) {357float value = 0.0f;358if (E.value.frame_index >= 0 && E.value.frame_index < p_values.size()) {359value = p_values[E.value.frame_index];360}361E.value.history.push_front(value);362E.value.update_value(value);363}364marker_frame++;365monitor_draw->queue_redraw();366}367368List<float> *EditorPerformanceProfiler::get_monitor_data(const StringName &p_name) {369if (monitors.has(p_name)) {370return &monitors[p_name].history;371}372return nullptr;373}374375void EditorPerformanceProfiler::_notification(int p_what) {376switch (p_what) {377case NOTIFICATION_THEME_CHANGED: {378for (KeyValue<StringName, TreeItem *> &E : base_map) {379E.value->set_custom_font(0, get_theme_font(SNAME("bold"), EditorStringName(EditorFonts)));380}381} break;382383case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {384if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/localize_settings")) {385_build_monitor_tree();386}387} break;388}389}390391EditorPerformanceProfiler::EditorPerformanceProfiler() {392set_name(TTR("Monitors"));393set_split_offset(340 * EDSCALE);394395monitor_tree = memnew(Tree);396monitor_tree->set_custom_minimum_size(Size2(300, 0) * EDSCALE);397monitor_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);398monitor_tree->set_columns(2);399monitor_tree->set_column_title(0, TTR("Monitor"));400monitor_tree->set_column_expand(0, true);401monitor_tree->set_column_title(1, TTR("Value"));402monitor_tree->set_column_custom_minimum_width(1, 100 * EDSCALE);403monitor_tree->set_column_expand(1, false);404monitor_tree->set_column_titles_visible(true);405monitor_tree->connect("item_edited", callable_mp(this, &EditorPerformanceProfiler::_monitor_select));406monitor_tree->create_item();407monitor_tree->set_hide_root(true);408monitor_tree->set_theme_type_variation("TreeSecondary");409add_child(monitor_tree);410411monitor_draw = memnew(Control);412monitor_draw->set_custom_minimum_size(Size2(300, 0) * EDSCALE);413monitor_draw->set_clip_contents(true);414monitor_draw->connect(SceneStringName(draw), callable_mp(this, &EditorPerformanceProfiler::_monitor_draw));415monitor_draw->connect(SceneStringName(gui_input), callable_mp(this, &EditorPerformanceProfiler::_marker_input));416add_child(monitor_draw);417418info_message = memnew(Label);419info_message->set_focus_mode(FOCUS_ACCESSIBILITY);420info_message->set_text(TTR("Pick one or more items from the list to display the graph."));421info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);422info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);423info_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);424info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));425info_message->set_anchors_and_offsets_preset(PRESET_FULL_RECT, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);426monitor_draw->add_child(info_message);427428for (int i = 0; i < Performance::MONITOR_MAX; i++) {429const Performance::Monitor monitor = Performance::Monitor(i);430const String path = Performance::get_singleton()->get_monitor_name(monitor);431const String base = path.get_slicec('/', 0);432const String name = path.get_slicec('/', 1);433monitors.insert(path, Monitor(name, base, i, Performance::get_singleton()->get_monitor_type(monitor), nullptr));434}435436_build_monitor_tree();437}438439440