Path: blob/master/editor/debugger/editor_performance_profiler.cpp
20831 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 "core/string/translation_server.h"33#include "editor/editor_string_names.h"34#include "editor/inspector/editor_property_name_processor.h"35#include "editor/settings/editor_settings.h"36#include "editor/themes/editor_scale.h"37#include "editor/themes/editor_theme_manager.h"38#include "main/performance.h"3940EditorPerformanceProfiler::Monitor::Monitor(const String &p_name, const String &p_base, int p_frame_index, Performance::MonitorType p_type, TreeItem *p_item) {41type = p_type;42item = p_item;43frame_index = p_frame_index;44name = p_name;45base = p_base;46}4748void EditorPerformanceProfiler::Monitor::reset() {49history.clear();50max = 0.0f;51if (item) {52item->set_text(1, "");53item->set_tooltip_text(1, "");54}55}5657String EditorPerformanceProfiler::_format_label(float p_value, Performance::MonitorType p_type) const {58const String &lang = _get_locale();59const TranslationServer *ts = TranslationServer::get_singleton();6061switch (p_type) {62case Performance::MONITOR_TYPE_QUANTITY: {63return ts->format_number(itos(p_value), lang);64}65case Performance::MONITOR_TYPE_MEMORY: {66return String::humanize_size(p_value);67}68case Performance::MONITOR_TYPE_TIME: {69return ts->format_number(rtos(p_value * 1000).pad_decimals(2), lang) + " " + TTR("ms");70}71case Performance::MONITOR_TYPE_PERCENTAGE: {72return ts->format_number(rtos(p_value * 100).pad_decimals(2), lang) + "%";73}74default: {75return ts->format_number(rtos(p_value), lang);76}77}78}7980void EditorPerformanceProfiler::_update_monitor_value(Monitor *p_monitor, float p_value) {81TreeItem *item = p_monitor->item;82ERR_FAIL_NULL(item);8384const String label = EditorPerformanceProfiler::_format_label(p_value, p_monitor->type);85item->set_text(1, label);8687String tooltip;88switch (p_monitor->type) {89case Performance::MONITOR_TYPE_MEMORY:90case Performance::MONITOR_TYPE_TIME: {91item->set_tooltip_text(1, label);92} break;9394default: {95item->set_tooltip_text(1, label + " " + item->get_text(0));96} break;97}9899if (p_value > p_monitor->max) {100p_monitor->max = p_value;101}102}103104void EditorPerformanceProfiler::_monitor_select() {105monitor_draw->queue_redraw();106}107108void EditorPerformanceProfiler::_monitor_draw() {109Vector<StringName> active;110for (const KeyValue<StringName, Monitor> &E : monitors) {111if (E.value.item->is_checked(0)) {112active.push_back(E.key);113}114}115116if (active.is_empty()) {117info_message->show();118return;119}120121info_message->hide();122123Ref<StyleBox> graph_style_box = get_theme_stylebox(CoreStringName(normal), SNAME("TextEdit"));124Ref<Font> graph_font = get_theme_font(SceneStringName(font), SNAME("TextEdit"));125int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("TextEdit"));126127int columns = int(Math::ceil(Math::sqrt(float(active.size()))));128int rows = int(Math::ceil(float(active.size()) / float(columns)));129if (active.size() == 1) {130rows = 1;131}132Size2i cell_size = Size2i(monitor_draw->get_size()) / Size2i(columns, rows);133float spacing = float(POINT_SEPARATION) / float(columns);134float value_multiplier = EditorThemeManager::is_dark_theme() ? 1.4f : 0.55f;135float hue_shift = 1.0f / float(monitors.size());136137for (int i = 0; i < active.size(); i++) {138Monitor ¤t = monitors[active[i]];139Rect2i rect(Point2i(i % columns, i / columns) * cell_size + Point2i(MARGIN, MARGIN), cell_size - Point2i(MARGIN, MARGIN) * 2);140monitor_draw->draw_style_box(graph_style_box, rect);141142rect.position += graph_style_box->get_offset();143rect.size -= graph_style_box->get_minimum_size();144Color draw_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));145draw_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);146monitor_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);147148draw_color.a = 0.9f;149float value_position = rect.size.width - graph_font->get_string_size(current.item->get_text(1), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;150if (value_position < 0) {151value_position = 0;152}153monitor_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);154155rect.position.y += graph_font->get_height(font_size);156rect.size.height -= graph_font->get_height(font_size);157158int line_count = rect.size.height / (graph_font->get_height(font_size) * 2);159if (line_count > 5) {160line_count = 5;161}162if (line_count > 0) {163Color horizontal_line_color;164horizontal_line_color.set_hsv(draw_color.get_h(), draw_color.get_s() * 0.5f, draw_color.get_v() * 0.5f, 0.3f);165monitor_draw->draw_line(rect.position, rect.position + Vector2(rect.size.width, 0), horizontal_line_color, Math::round(EDSCALE));166monitor_draw->draw_string(graph_font, rect.position + Vector2(0, graph_font->get_ascent(font_size)), _format_label(current.max, current.type), HORIZONTAL_ALIGNMENT_LEFT, rect.size.width, font_size, horizontal_line_color);167168for (int j = 0; j < line_count; j++) {169Vector2 y_offset = Vector2(0, rect.size.height * (1.0f - float(j) / float(line_count)));170monitor_draw->draw_line(rect.position + y_offset, rect.position + Vector2(rect.size.width, 0) + y_offset, horizontal_line_color, Math::round(EDSCALE));171monitor_draw->draw_string(graph_font, rect.position - Vector2(0, graph_font->get_descent(font_size)) + y_offset, _format_label(current.max * float(j) / float(line_count), current.type), HORIZONTAL_ALIGNMENT_LEFT, rect.size.width, font_size, horizontal_line_color);172}173}174175float from = rect.size.width;176float prev = -1.0f;177int count = 0;178List<float>::Element *e = current.history.front();179180while (from >= 0 && e) {181float m = current.max;182float h2 = 0;183if (m != 0) {184h2 = (e->get() / m);185}186h2 = (1.0f - h2) * float(rect.size.y);187if (e != current.history.front()) {188monitor_draw->draw_line(rect.position + Point2(from, h2), rect.position + Point2(from + spacing, prev), draw_color, Math::round(EDSCALE));189}190191if (marker_key == active[i] && count == marker_frame) {192Color line_color;193line_color.set_hsv(draw_color.get_h(), draw_color.get_s() * 0.8f, draw_color.get_v(), 0.5f);194monitor_draw->draw_line(rect.position + Point2(from, 0), rect.position + Point2(from, rect.size.y), line_color, Math::round(EDSCALE));195196String label = _format_label(e->get(), current.type);197Size2 size = graph_font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);198Vector2 text_top_left_position = Vector2(from, h2) - (size + Vector2(MARKER_MARGIN, MARKER_MARGIN));199if (text_top_left_position.x < 0) {200text_top_left_position.x = from + MARKER_MARGIN;201}202if (text_top_left_position.y < 0) {203text_top_left_position.y = h2 + MARKER_MARGIN;204}205monitor_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);206}207prev = h2;208e = e->next();209from -= spacing;210count++;211}212}213}214215void EditorPerformanceProfiler::_build_monitor_tree() {216HashSet<StringName> monitor_checked;217for (KeyValue<StringName, Monitor> &E : monitors) {218if (E.value.item && E.value.item->is_checked(0)) {219monitor_checked.insert(E.key);220}221}222223base_map.clear();224monitor_tree->get_root()->clear_children();225226for (KeyValue<StringName, Monitor> &E : monitors) {227TreeItem *base = _get_monitor_base(E.value.base);228TreeItem *item = _create_monitor_item(E.value.name, base);229item->set_checked(0, monitor_checked.has(E.key));230E.value.item = item;231if (!E.value.history.is_empty()) {232_update_monitor_value(&E.value, E.value.history.front()->get());233}234}235}236237TreeItem *EditorPerformanceProfiler::_get_monitor_base(const StringName &p_base_name) {238if (base_map.has(p_base_name)) {239return base_map[p_base_name];240}241242TreeItem *base = monitor_tree->create_item(monitor_tree->get_root());243base->set_text(0, EditorPropertyNameProcessor::get_singleton()->process_name(p_base_name, EditorPropertyNameProcessor::get_settings_style()));244base->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED);245base->set_editable(0, false);246base->set_selectable(0, false);247base->set_expand_right(0, true);248if (is_inside_tree()) {249base->set_custom_font(0, get_theme_font(SNAME("bold"), EditorStringName(EditorFonts)));250}251base_map.insert(p_base_name, base);252return base;253}254255TreeItem *EditorPerformanceProfiler::_create_monitor_item(const StringName &p_monitor_name, TreeItem *p_base) {256TreeItem *item = monitor_tree->create_item(p_base);257item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);258item->set_editable(0, true);259item->set_selectable(0, false);260item->set_selectable(1, false);261item->set_text(0, EditorPropertyNameProcessor::get_singleton()->process_name(p_monitor_name, EditorPropertyNameProcessor::get_settings_style()));262return item;263}264265void EditorPerformanceProfiler::_marker_input(const Ref<InputEvent> &p_event) {266Ref<InputEventMouseButton> mb = p_event;267if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {268Vector<StringName> active;269for (KeyValue<StringName, Monitor> &E : monitors) {270if (E.value.item->is_checked(0)) {271active.push_back(E.key);272}273}274if (active.size() > 0) {275int columns = int(Math::ceil(Math::sqrt(float(active.size()))));276int rows = int(Math::ceil(float(active.size()) / float(columns)));277if (active.size() == 1) {278rows = 1;279}280Size2i cell_size = Size2i(monitor_draw->get_size()) / Size2i(columns, rows);281Vector2i index = mb->get_position() / cell_size;282Rect2i rect(index * cell_size + Point2i(MARGIN, MARGIN), cell_size - Point2i(MARGIN, MARGIN) * 2);283if (rect.has_point(mb->get_position())) {284if (index.x + index.y * columns < active.size()) {285marker_key = active[index.x + index.y * columns];286} else {287marker_key = "";288}289Ref<StyleBox> graph_style_box = get_theme_stylebox(CoreStringName(normal), SNAME("TextEdit"));290rect.position += graph_style_box->get_offset();291rect.size -= graph_style_box->get_minimum_size();292Vector2 point = mb->get_position() - rect.position;293if (point.x >= rect.size.x) {294marker_frame = 0;295} else {296int point_sep = 5;297float spacing = float(point_sep) / float(columns);298marker_frame = (rect.size.x - point.x) / spacing;299}300monitor_draw->queue_redraw();301return;302}303}304marker_key = "";305monitor_draw->queue_redraw();306}307}308309void EditorPerformanceProfiler::reset() {310HashMap<StringName, Monitor>::Iterator E = monitors.begin();311while (E != monitors.end()) {312HashMap<StringName, Monitor>::Iterator N = E;313++N;314if (String(E->key).begins_with("custom:")) {315monitors.remove(E);316} else {317E->value.reset();318}319E = N;320}321322_build_monitor_tree();323marker_key = "";324marker_frame = 0;325monitor_draw->queue_redraw();326}327328void EditorPerformanceProfiler::update_monitors(const Vector<StringName> &p_names, const PackedInt32Array &p_types) {329HashMap<StringName, int> names;330for (int i = 0; i < p_names.size(); i++) {331names.insert("custom:" + p_names[i], Performance::MONITOR_MAX + i);332}333334{335HashMap<StringName, Monitor>::Iterator E = monitors.begin();336while (E != monitors.end()) {337HashMap<StringName, Monitor>::Iterator N = E;338++N;339if (String(E->key).begins_with("custom:")) {340if (!names.has(E->key)) {341monitors.remove(E);342} else {343E->value.frame_index = names[E->key];344names.erase(E->key);345}346}347E = N;348}349}350351int index = 0;352for (const KeyValue<StringName, int> &E : names) {353String name = String(E.key).replace_first("custom:", "");354String base = "Custom";355if (name.get_slice_count("/") == 2) {356base = name.get_slicec('/', 0);357name = name.get_slicec('/', 1);358}359Performance::MonitorType type = Performance::MonitorType(p_types[index]);360monitors.insert(E.key, Monitor(name, base, E.value, type, nullptr));361index++;362}363364_build_monitor_tree();365}366367void EditorPerformanceProfiler::add_profile_frame(const Vector<float> &p_values) {368for (KeyValue<StringName, Monitor> &E : monitors) {369float value = 0.0f;370if (E.value.frame_index >= 0 && E.value.frame_index < p_values.size()) {371value = p_values[E.value.frame_index];372}373E.value.history.push_front(value);374_update_monitor_value(&E.value, value);375}376marker_frame++;377monitor_draw->queue_redraw();378}379380List<float> *EditorPerformanceProfiler::get_monitor_data(const StringName &p_name) {381if (monitors.has(p_name)) {382return &monitors[p_name].history;383}384return nullptr;385}386387void EditorPerformanceProfiler::_notification(int p_what) {388switch (p_what) {389case NOTIFICATION_TRANSLATION_CHANGED: {390if (is_ready()) {391_build_monitor_tree();392if (monitor_draw->is_visible_in_tree()) {393monitor_draw->queue_redraw();394}395}396} break;397398case NOTIFICATION_THEME_CHANGED: {399for (KeyValue<StringName, TreeItem *> &E : base_map) {400E.value->set_custom_font(0, get_theme_font(SNAME("bold"), EditorStringName(EditorFonts)));401}402} break;403404case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {405if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/localize_settings")) {406_build_monitor_tree();407}408} break;409}410}411412EditorPerformanceProfiler::EditorPerformanceProfiler() {413set_name(TTRC("Monitors"));414set_split_offset(340 * EDSCALE);415416monitor_tree = memnew(Tree);417monitor_tree->set_custom_minimum_size(Size2(300, 0) * EDSCALE);418monitor_tree->set_columns(2);419monitor_tree->set_column_title(0, TTRC("Monitor"));420monitor_tree->set_column_expand(0, true);421monitor_tree->set_column_title(1, TTRC("Value"));422monitor_tree->set_column_custom_minimum_width(1, 100 * EDSCALE);423monitor_tree->set_column_expand(1, false);424monitor_tree->set_column_titles_visible(true);425monitor_tree->connect("item_edited", callable_mp(this, &EditorPerformanceProfiler::_monitor_select));426monitor_tree->create_item();427monitor_tree->set_hide_root(true);428monitor_tree->set_theme_type_variation("TreeSecondary");429add_child(monitor_tree);430431monitor_draw = memnew(Control);432monitor_draw->set_custom_minimum_size(Size2(300, 0) * EDSCALE);433monitor_draw->set_clip_contents(true);434monitor_draw->connect(SceneStringName(draw), callable_mp(this, &EditorPerformanceProfiler::_monitor_draw));435monitor_draw->connect(SceneStringName(gui_input), callable_mp(this, &EditorPerformanceProfiler::_marker_input));436add_child(monitor_draw);437438info_message = memnew(Label);439info_message->set_focus_mode(FOCUS_ACCESSIBILITY);440info_message->set_text(TTRC("Pick one or more items from the list to display the graph."));441info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);442info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);443info_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);444info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));445info_message->set_anchors_and_offsets_preset(PRESET_FULL_RECT, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);446monitor_draw->add_child(info_message);447448for (int i = 0; i < Performance::MONITOR_MAX; i++) {449const Performance::Monitor monitor = Performance::Monitor(i);450const String path = Performance::get_singleton()->get_monitor_name(monitor);451const String base = path.get_slicec('/', 0);452const String name = path.get_slicec('/', 1);453monitors.insert(path, Monitor(name, base, i, Performance::get_singleton()->get_monitor_type(monitor), nullptr));454}455456_build_monitor_tree();457}458459460