Debian Codegraph @

editor/debugger/editor_performance_profiler.cpp

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