Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/debugger/editor_performance_profiler.cpp
9896 views
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 "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"
39
40
EditorPerformanceProfiler::Monitor::Monitor(const String &p_name, const String &p_base, int p_frame_index, Performance::MonitorType p_type, TreeItem *p_item) {
41
type = p_type;
42
item = p_item;
43
frame_index = p_frame_index;
44
name = p_name;
45
base = p_base;
46
}
47
48
void EditorPerformanceProfiler::Monitor::update_value(float p_value) {
49
ERR_FAIL_NULL(item);
50
String label = EditorPerformanceProfiler::_create_label(p_value, type);
51
String tooltip = label;
52
switch (type) {
53
case Performance::MONITOR_TYPE_MEMORY: {
54
tooltip = label;
55
} break;
56
case Performance::MONITOR_TYPE_TIME: {
57
tooltip = label;
58
} break;
59
default: {
60
tooltip += " " + item->get_text(0);
61
} break;
62
}
63
item->set_text(1, label);
64
item->set_tooltip_text(1, tooltip);
65
66
if (p_value > max) {
67
max = p_value;
68
}
69
}
70
71
void EditorPerformanceProfiler::Monitor::reset() {
72
history.clear();
73
max = 0.0f;
74
if (item) {
75
item->set_text(1, "");
76
item->set_tooltip_text(1, "");
77
}
78
}
79
80
String EditorPerformanceProfiler::_create_label(float p_value, Performance::MonitorType p_type) {
81
switch (p_type) {
82
case Performance::MONITOR_TYPE_QUANTITY: {
83
return TS->format_number(itos(p_value));
84
}
85
case Performance::MONITOR_TYPE_MEMORY: {
86
return String::humanize_size(p_value);
87
}
88
case Performance::MONITOR_TYPE_TIME: {
89
return TS->format_number(rtos(p_value * 1000).pad_decimals(2)) + " " + TTR("ms");
90
}
91
default: {
92
return TS->format_number(rtos(p_value));
93
}
94
}
95
}
96
97
void EditorPerformanceProfiler::_monitor_select() {
98
monitor_draw->queue_redraw();
99
}
100
101
void EditorPerformanceProfiler::_monitor_draw() {
102
Vector<StringName> active;
103
for (const KeyValue<StringName, Monitor> &E : monitors) {
104
if (E.value.item->is_checked(0)) {
105
active.push_back(E.key);
106
}
107
}
108
109
if (active.is_empty()) {
110
info_message->show();
111
return;
112
}
113
114
info_message->hide();
115
116
Ref<StyleBox> graph_style_box = get_theme_stylebox(CoreStringName(normal), SNAME("TextEdit"));
117
Ref<Font> graph_font = get_theme_font(SceneStringName(font), SNAME("TextEdit"));
118
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("TextEdit"));
119
120
int columns = int(Math::ceil(Math::sqrt(float(active.size()))));
121
int rows = int(Math::ceil(float(active.size()) / float(columns)));
122
if (active.size() == 1) {
123
rows = 1;
124
}
125
Size2i cell_size = Size2i(monitor_draw->get_size()) / Size2i(columns, rows);
126
float spacing = float(POINT_SEPARATION) / float(columns);
127
float value_multiplier = EditorThemeManager::is_dark_theme() ? 1.4f : 0.55f;
128
float hue_shift = 1.0f / float(monitors.size());
129
130
for (int i = 0; i < active.size(); i++) {
131
Monitor &current = monitors[active[i]];
132
Rect2i rect(Point2i(i % columns, i / columns) * cell_size + Point2i(MARGIN, MARGIN), cell_size - Point2i(MARGIN, MARGIN) * 2);
133
monitor_draw->draw_style_box(graph_style_box, rect);
134
135
rect.position += graph_style_box->get_offset();
136
rect.size -= graph_style_box->get_minimum_size();
137
Color draw_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
138
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);
139
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);
140
141
draw_color.a = 0.9f;
142
float value_position = rect.size.width - graph_font->get_string_size(current.item->get_text(1), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;
143
if (value_position < 0) {
144
value_position = 0;
145
}
146
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);
147
148
rect.position.y += graph_font->get_height(font_size);
149
rect.size.height -= graph_font->get_height(font_size);
150
151
int line_count = rect.size.height / (graph_font->get_height(font_size) * 2);
152
if (line_count > 5) {
153
line_count = 5;
154
}
155
if (line_count > 0) {
156
Color horizontal_line_color;
157
horizontal_line_color.set_hsv(draw_color.get_h(), draw_color.get_s() * 0.5f, draw_color.get_v() * 0.5f, 0.3f);
158
monitor_draw->draw_line(rect.position, rect.position + Vector2(rect.size.width, 0), horizontal_line_color, Math::round(EDSCALE));
159
monitor_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);
160
161
for (int j = 0; j < line_count; j++) {
162
Vector2 y_offset = Vector2(0, rect.size.height * (1.0f - float(j) / float(line_count)));
163
monitor_draw->draw_line(rect.position + y_offset, rect.position + Vector2(rect.size.width, 0) + y_offset, horizontal_line_color, Math::round(EDSCALE));
164
monitor_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);
165
}
166
}
167
168
float from = rect.size.width;
169
float prev = -1.0f;
170
int count = 0;
171
List<float>::Element *e = current.history.front();
172
173
while (from >= 0 && e) {
174
float m = current.max;
175
float h2 = 0;
176
if (m != 0) {
177
h2 = (e->get() / m);
178
}
179
h2 = (1.0f - h2) * float(rect.size.y);
180
if (e != current.history.front()) {
181
monitor_draw->draw_line(rect.position + Point2(from, h2), rect.position + Point2(from + spacing, prev), draw_color, Math::round(EDSCALE));
182
}
183
184
if (marker_key == active[i] && count == marker_frame) {
185
Color line_color;
186
line_color.set_hsv(draw_color.get_h(), draw_color.get_s() * 0.8f, draw_color.get_v(), 0.5f);
187
monitor_draw->draw_line(rect.position + Point2(from, 0), rect.position + Point2(from, rect.size.y), line_color, Math::round(EDSCALE));
188
189
String label = _create_label(e->get(), current.type);
190
Size2 size = graph_font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
191
Vector2 text_top_left_position = Vector2(from, h2) - (size + Vector2(MARKER_MARGIN, MARKER_MARGIN));
192
if (text_top_left_position.x < 0) {
193
text_top_left_position.x = from + MARKER_MARGIN;
194
}
195
if (text_top_left_position.y < 0) {
196
text_top_left_position.y = h2 + MARKER_MARGIN;
197
}
198
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);
199
}
200
prev = h2;
201
e = e->next();
202
from -= spacing;
203
count++;
204
}
205
}
206
}
207
208
void EditorPerformanceProfiler::_build_monitor_tree() {
209
HashSet<StringName> monitor_checked;
210
for (KeyValue<StringName, Monitor> &E : monitors) {
211
if (E.value.item && E.value.item->is_checked(0)) {
212
monitor_checked.insert(E.key);
213
}
214
}
215
216
base_map.clear();
217
monitor_tree->get_root()->clear_children();
218
219
for (KeyValue<StringName, Monitor> &E : monitors) {
220
TreeItem *base = _get_monitor_base(E.value.base);
221
TreeItem *item = _create_monitor_item(E.value.name, base);
222
item->set_checked(0, monitor_checked.has(E.key));
223
E.value.item = item;
224
if (!E.value.history.is_empty()) {
225
E.value.update_value(E.value.history.front()->get());
226
}
227
}
228
}
229
230
TreeItem *EditorPerformanceProfiler::_get_monitor_base(const StringName &p_base_name) {
231
if (base_map.has(p_base_name)) {
232
return base_map[p_base_name];
233
}
234
235
TreeItem *base = monitor_tree->create_item(monitor_tree->get_root());
236
base->set_text(0, EditorPropertyNameProcessor::get_singleton()->process_name(p_base_name, EditorPropertyNameProcessor::get_settings_style()));
237
base->set_editable(0, false);
238
base->set_selectable(0, false);
239
base->set_expand_right(0, true);
240
if (is_inside_tree()) {
241
base->set_custom_font(0, get_theme_font(SNAME("bold"), EditorStringName(EditorFonts)));
242
}
243
base_map.insert(p_base_name, base);
244
return base;
245
}
246
247
TreeItem *EditorPerformanceProfiler::_create_monitor_item(const StringName &p_monitor_name, TreeItem *p_base) {
248
TreeItem *item = monitor_tree->create_item(p_base);
249
item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
250
item->set_editable(0, true);
251
item->set_selectable(0, false);
252
item->set_selectable(1, false);
253
item->set_text(0, EditorPropertyNameProcessor::get_singleton()->process_name(p_monitor_name, EditorPropertyNameProcessor::get_settings_style()));
254
return item;
255
}
256
257
void EditorPerformanceProfiler::_marker_input(const Ref<InputEvent> &p_event) {
258
Ref<InputEventMouseButton> mb = p_event;
259
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
260
Vector<StringName> active;
261
for (KeyValue<StringName, Monitor> &E : monitors) {
262
if (E.value.item->is_checked(0)) {
263
active.push_back(E.key);
264
}
265
}
266
if (active.size() > 0) {
267
int columns = int(Math::ceil(Math::sqrt(float(active.size()))));
268
int rows = int(Math::ceil(float(active.size()) / float(columns)));
269
if (active.size() == 1) {
270
rows = 1;
271
}
272
Size2i cell_size = Size2i(monitor_draw->get_size()) / Size2i(columns, rows);
273
Vector2i index = mb->get_position() / cell_size;
274
Rect2i rect(index * cell_size + Point2i(MARGIN, MARGIN), cell_size - Point2i(MARGIN, MARGIN) * 2);
275
if (rect.has_point(mb->get_position())) {
276
if (index.x + index.y * columns < active.size()) {
277
marker_key = active[index.x + index.y * columns];
278
} else {
279
marker_key = "";
280
}
281
Ref<StyleBox> graph_style_box = get_theme_stylebox(CoreStringName(normal), SNAME("TextEdit"));
282
rect.position += graph_style_box->get_offset();
283
rect.size -= graph_style_box->get_minimum_size();
284
Vector2 point = mb->get_position() - rect.position;
285
if (point.x >= rect.size.x) {
286
marker_frame = 0;
287
} else {
288
int point_sep = 5;
289
float spacing = float(point_sep) / float(columns);
290
marker_frame = (rect.size.x - point.x) / spacing;
291
}
292
monitor_draw->queue_redraw();
293
return;
294
}
295
}
296
marker_key = "";
297
monitor_draw->queue_redraw();
298
}
299
}
300
301
void EditorPerformanceProfiler::reset() {
302
HashMap<StringName, Monitor>::Iterator E = monitors.begin();
303
while (E != monitors.end()) {
304
HashMap<StringName, Monitor>::Iterator N = E;
305
++N;
306
if (String(E->key).begins_with("custom:")) {
307
monitors.remove(E);
308
} else {
309
E->value.reset();
310
}
311
E = N;
312
}
313
314
_build_monitor_tree();
315
marker_key = "";
316
marker_frame = 0;
317
monitor_draw->queue_redraw();
318
}
319
320
void EditorPerformanceProfiler::update_monitors(const Vector<StringName> &p_names) {
321
HashMap<StringName, int> names;
322
for (int i = 0; i < p_names.size(); i++) {
323
names.insert("custom:" + p_names[i], Performance::MONITOR_MAX + i);
324
}
325
326
{
327
HashMap<StringName, Monitor>::Iterator E = monitors.begin();
328
while (E != monitors.end()) {
329
HashMap<StringName, Monitor>::Iterator N = E;
330
++N;
331
if (String(E->key).begins_with("custom:")) {
332
if (!names.has(E->key)) {
333
monitors.remove(E);
334
} else {
335
E->value.frame_index = names[E->key];
336
names.erase(E->key);
337
}
338
}
339
E = N;
340
}
341
}
342
343
for (const KeyValue<StringName, int> &E : names) {
344
String name = String(E.key).replace_first("custom:", "");
345
String base = "Custom";
346
if (name.get_slice_count("/") == 2) {
347
base = name.get_slicec('/', 0);
348
name = name.get_slicec('/', 1);
349
}
350
monitors.insert(E.key, Monitor(name, base, E.value, Performance::MONITOR_TYPE_QUANTITY, nullptr));
351
}
352
353
_build_monitor_tree();
354
}
355
356
void EditorPerformanceProfiler::add_profile_frame(const Vector<float> &p_values) {
357
for (KeyValue<StringName, Monitor> &E : monitors) {
358
float value = 0.0f;
359
if (E.value.frame_index >= 0 && E.value.frame_index < p_values.size()) {
360
value = p_values[E.value.frame_index];
361
}
362
E.value.history.push_front(value);
363
E.value.update_value(value);
364
}
365
marker_frame++;
366
monitor_draw->queue_redraw();
367
}
368
369
List<float> *EditorPerformanceProfiler::get_monitor_data(const StringName &p_name) {
370
if (monitors.has(p_name)) {
371
return &monitors[p_name].history;
372
}
373
return nullptr;
374
}
375
376
void EditorPerformanceProfiler::_notification(int p_what) {
377
switch (p_what) {
378
case NOTIFICATION_THEME_CHANGED: {
379
for (KeyValue<StringName, TreeItem *> &E : base_map) {
380
E.value->set_custom_font(0, get_theme_font(SNAME("bold"), EditorStringName(EditorFonts)));
381
}
382
} break;
383
384
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
385
if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/localize_settings")) {
386
_build_monitor_tree();
387
}
388
} break;
389
}
390
}
391
392
EditorPerformanceProfiler::EditorPerformanceProfiler() {
393
set_name(TTR("Monitors"));
394
set_split_offset(340 * EDSCALE);
395
396
monitor_tree = memnew(Tree);
397
monitor_tree->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
398
monitor_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
399
monitor_tree->set_columns(2);
400
monitor_tree->set_column_title(0, TTR("Monitor"));
401
monitor_tree->set_column_expand(0, true);
402
monitor_tree->set_column_title(1, TTR("Value"));
403
monitor_tree->set_column_custom_minimum_width(1, 100 * EDSCALE);
404
monitor_tree->set_column_expand(1, false);
405
monitor_tree->set_column_titles_visible(true);
406
monitor_tree->connect("item_edited", callable_mp(this, &EditorPerformanceProfiler::_monitor_select));
407
monitor_tree->create_item();
408
monitor_tree->set_hide_root(true);
409
monitor_tree->set_theme_type_variation("TreeSecondary");
410
add_child(monitor_tree);
411
412
monitor_draw = memnew(Control);
413
monitor_draw->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
414
monitor_draw->set_clip_contents(true);
415
monitor_draw->connect(SceneStringName(draw), callable_mp(this, &EditorPerformanceProfiler::_monitor_draw));
416
monitor_draw->connect(SceneStringName(gui_input), callable_mp(this, &EditorPerformanceProfiler::_marker_input));
417
add_child(monitor_draw);
418
419
info_message = memnew(Label);
420
info_message->set_focus_mode(FOCUS_ACCESSIBILITY);
421
info_message->set_text(TTR("Pick one or more items from the list to display the graph."));
422
info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
423
info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
424
info_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
425
info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
426
info_message->set_anchors_and_offsets_preset(PRESET_FULL_RECT, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
427
monitor_draw->add_child(info_message);
428
429
for (int i = 0; i < Performance::MONITOR_MAX; i++) {
430
const Performance::Monitor monitor = Performance::Monitor(i);
431
const String path = Performance::get_singleton()->get_monitor_name(monitor);
432
const String base = path.get_slicec('/', 0);
433
const String name = path.get_slicec('/', 1);
434
monitors.insert(path, Monitor(name, base, i, Performance::get_singleton()->get_monitor_type(monitor), nullptr));
435
}
436
437
_build_monitor_tree();
438
}
439
440