Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/debugger/editor_profiler.cpp
9906 views
1
/**************************************************************************/
2
/* editor_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_profiler.h"
32
33
#include "core/io/image.h"
34
#include "editor/editor_string_names.h"
35
#include "editor/run/editor_run_bar.h"
36
#include "editor/settings/editor_settings.h"
37
#include "editor/themes/editor_scale.h"
38
#include "scene/gui/check_box.h"
39
#include "scene/gui/flow_container.h"
40
#include "scene/resources/image_texture.h"
41
42
void EditorProfiler::_make_metric_ptrs(Metric &m) {
43
for (int i = 0; i < m.categories.size(); i++) {
44
m.category_ptrs[m.categories[i].signature] = &m.categories.write[i];
45
for (int j = 0; j < m.categories[i].items.size(); j++) {
46
m.item_ptrs[m.categories[i].items[j].signature] = &m.categories.write[i].items.write[j];
47
}
48
}
49
}
50
51
EditorProfiler::Metric EditorProfiler::_get_frame_metric(int index) {
52
return frame_metrics[(frame_metrics.size() + last_metric - (total_metrics - 1) + index) % frame_metrics.size()];
53
}
54
55
void EditorProfiler::add_frame_metric(const Metric &p_metric, bool p_final) {
56
++last_metric;
57
if (last_metric >= frame_metrics.size()) {
58
last_metric = 0;
59
}
60
61
total_metrics++;
62
if (total_metrics > frame_metrics.size()) {
63
total_metrics = frame_metrics.size();
64
}
65
66
frame_metrics.write[last_metric] = p_metric;
67
_make_metric_ptrs(frame_metrics.write[last_metric]);
68
69
updating_frame = true;
70
clear_button->set_disabled(false);
71
cursor_metric_edit->set_editable(true);
72
cursor_metric_edit->set_max(p_metric.frame_number);
73
cursor_metric_edit->set_min(_get_frame_metric(0).frame_number);
74
75
if (!seeking) {
76
cursor_metric_edit->set_value(p_metric.frame_number);
77
}
78
79
updating_frame = false;
80
81
if (frame_delay->is_stopped()) {
82
frame_delay->set_wait_time(p_final ? 0.1 : 1);
83
frame_delay->start();
84
}
85
86
if (plot_delay->is_stopped()) {
87
plot_delay->set_wait_time(0.1);
88
plot_delay->start();
89
}
90
}
91
92
void EditorProfiler::clear() {
93
int metric_size = EDITOR_GET("debugger/profiler_frame_history_size");
94
metric_size = CLAMP(metric_size, 60, 10000);
95
frame_metrics.clear();
96
frame_metrics.resize(metric_size);
97
total_metrics = 0;
98
last_metric = -1;
99
variables->clear();
100
plot_sigs.clear();
101
plot_sigs.insert("physics_frame_time");
102
plot_sigs.insert("category_frame_time");
103
display_internal_profiles->set_visible(EDITOR_GET("debugger/profile_native_calls"));
104
105
updating_frame = true;
106
cursor_metric_edit->set_min(0);
107
cursor_metric_edit->set_max(100); // Doesn't make much sense, but we can't have min == max. Doesn't hurt.
108
cursor_metric_edit->set_value(0);
109
cursor_metric_edit->set_editable(false);
110
updating_frame = false;
111
hover_metric = -1;
112
seeking = false;
113
114
// Ensure button text (start, stop) is correct
115
_update_button_text();
116
emit_signal(SNAME("enable_profiling"), activate->is_pressed());
117
}
118
119
static String _get_percent_txt(float p_value, float p_total) {
120
if (p_total == 0) {
121
p_total = 0.00001;
122
}
123
124
return TS->format_number(String::num((p_value / p_total) * 100, 1)) + TS->percent_sign();
125
}
126
127
String EditorProfiler::_get_time_as_text(const Metric &m, float p_time, int p_calls) {
128
const int dmode = display_mode->get_selected();
129
130
if (dmode == DISPLAY_FRAME_TIME) {
131
return TS->format_number(rtos(p_time * 1000).pad_decimals(2)) + " " + TTR("ms");
132
} else if (dmode == DISPLAY_AVERAGE_TIME) {
133
if (p_calls == 0) {
134
return TS->format_number("0.00") + " " + TTR("ms");
135
} else {
136
return TS->format_number(rtos((p_time / p_calls) * 1000).pad_decimals(2)) + " " + TTR("ms");
137
}
138
} else if (dmode == DISPLAY_FRAME_PERCENT) {
139
return _get_percent_txt(p_time, m.frame_time);
140
} else if (dmode == DISPLAY_PHYSICS_FRAME_PERCENT) {
141
return _get_percent_txt(p_time, m.physics_frame_time);
142
}
143
144
return "err";
145
}
146
147
Color EditorProfiler::_get_color_from_signature(const StringName &p_signature) const {
148
Color bc = get_theme_color(SNAME("error_color"), EditorStringName(Editor));
149
double rot = Math::abs(double(p_signature.hash()) / double(0x7FFFFFFF));
150
Color c;
151
c.set_hsv(rot, bc.get_s(), bc.get_v());
152
return c.lerp(get_theme_color(SNAME("base_color"), EditorStringName(Editor)), 0.07);
153
}
154
155
int EditorProfiler::_get_zoom_left_border() const {
156
const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);
157
return CLAMP(zoom_center - max_profiles_shown / 2, 0, frame_metrics.size() - max_profiles_shown);
158
}
159
160
void EditorProfiler::_item_edited() {
161
if (updating_frame) {
162
return;
163
}
164
165
TreeItem *item = variables->get_edited();
166
if (!item) {
167
return;
168
}
169
StringName signature = item->get_metadata(0);
170
bool checked = item->is_checked(0);
171
172
if (checked) {
173
plot_sigs.insert(signature);
174
} else {
175
plot_sigs.erase(signature);
176
}
177
178
if (!frame_delay->is_processing()) {
179
frame_delay->set_wait_time(0.1);
180
frame_delay->start();
181
}
182
183
_update_plot();
184
}
185
186
void EditorProfiler::_update_plot() {
187
const int w = MAX(1, graph->get_size().width); // Clamp to 1 to prevent from crashing when profiler is autostarted.
188
const int h = MAX(1, graph->get_size().height);
189
bool reset_texture = false;
190
const int desired_len = w * h * 4;
191
192
if (graph_image.size() != desired_len) {
193
reset_texture = true;
194
graph_image.resize(desired_len);
195
}
196
197
uint8_t *wr = graph_image.ptrw();
198
const Color background_color = get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor));
199
200
// Clear the previous frame and set the background color.
201
for (int i = 0; i < desired_len; i += 4) {
202
wr[i + 0] = Math::fast_ftoi(background_color.r * 255);
203
wr[i + 1] = Math::fast_ftoi(background_color.g * 255);
204
wr[i + 2] = Math::fast_ftoi(background_color.b * 255);
205
wr[i + 3] = 255;
206
}
207
208
//find highest value
209
210
const bool use_self = display_time->get_selected() == DISPLAY_SELF_TIME;
211
float highest = 0;
212
213
for (int i = 0; i < total_metrics; i++) {
214
const Metric &m = _get_frame_metric(i);
215
216
for (const StringName &E : plot_sigs) {
217
HashMap<StringName, Metric::Category *>::ConstIterator F = m.category_ptrs.find(E);
218
if (F) {
219
highest = MAX(F->value->total_time, highest);
220
}
221
222
HashMap<StringName, Metric::Category::Item *>::ConstIterator G = m.item_ptrs.find(E);
223
if (G) {
224
if (use_self) {
225
highest = MAX(G->value->self, highest);
226
} else {
227
highest = MAX(G->value->total, highest);
228
}
229
}
230
}
231
}
232
233
if (highest > 0) {
234
//means some data exists..
235
highest *= 1.2; //leave some upper room
236
graph_height = highest;
237
238
Vector<int> columnv;
239
columnv.resize(h * 4);
240
241
int *column = columnv.ptrw();
242
243
HashMap<StringName, int> prev_plots;
244
245
const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);
246
const int left_border = _get_zoom_left_border();
247
const int profiles_drawn = CLAMP(total_metrics - left_border, 0, max_profiles_shown);
248
const int pixel_cols = (profiles_drawn * w) / max_profiles_shown - 1;
249
250
for (int i = 0; i < pixel_cols; i++) {
251
for (int j = 0; j < h * 4; j++) {
252
column[j] = 0;
253
}
254
255
int current = (i * max_profiles_shown / w) + left_border;
256
257
for (const StringName &E : plot_sigs) {
258
const Metric &m = _get_frame_metric(current);
259
260
float value = 0;
261
262
HashMap<StringName, Metric::Category *>::ConstIterator F = m.category_ptrs.find(E);
263
if (F) {
264
value = F->value->total_time;
265
}
266
267
HashMap<StringName, Metric::Category::Item *>::ConstIterator G = m.item_ptrs.find(E);
268
if (G) {
269
if (use_self) {
270
value = G->value->self;
271
} else {
272
value = G->value->total;
273
}
274
}
275
276
int plot_pos = CLAMP(int(value * h / highest), 0, h - 1);
277
278
int prev_plot = plot_pos;
279
HashMap<StringName, int>::Iterator H = prev_plots.find(E);
280
if (H) {
281
prev_plot = H->value;
282
H->value = plot_pos;
283
} else {
284
prev_plots[E] = plot_pos;
285
}
286
287
plot_pos = h - plot_pos - 1;
288
prev_plot = h - prev_plot - 1;
289
290
if (prev_plot > plot_pos) {
291
SWAP(prev_plot, plot_pos);
292
}
293
294
Color col = _get_color_from_signature(E);
295
296
for (int j = prev_plot; j <= plot_pos; j++) {
297
column[j * 4 + 0] += Math::fast_ftoi(CLAMP(col.r * 255, 0, 255));
298
column[j * 4 + 1] += Math::fast_ftoi(CLAMP(col.g * 255, 0, 255));
299
column[j * 4 + 2] += Math::fast_ftoi(CLAMP(col.b * 255, 0, 255));
300
column[j * 4 + 3] += 1;
301
}
302
}
303
304
for (int j = 0; j < h * 4; j += 4) {
305
const int a = column[j + 3];
306
if (a > 0) {
307
column[j + 0] /= a;
308
column[j + 1] /= a;
309
column[j + 2] /= a;
310
}
311
312
const uint8_t red = uint8_t(column[j + 0]);
313
const uint8_t green = uint8_t(column[j + 1]);
314
const uint8_t blue = uint8_t(column[j + 2]);
315
const bool is_filled = red >= 1 || green >= 1 || blue >= 1;
316
const int widx = ((j >> 2) * w + i) * 4;
317
318
// If the pixel isn't filled by any profiler line, apply the background color instead.
319
wr[widx + 0] = is_filled ? red : Math::fast_ftoi(background_color.r * 255);
320
wr[widx + 1] = is_filled ? green : Math::fast_ftoi(background_color.g * 255);
321
wr[widx + 2] = is_filled ? blue : Math::fast_ftoi(background_color.b * 255);
322
wr[widx + 3] = 255;
323
}
324
}
325
}
326
327
Ref<Image> img = Image::create_from_data(w, h, false, Image::FORMAT_RGBA8, graph_image);
328
329
if (reset_texture) {
330
if (graph_texture.is_null()) {
331
graph_texture.instantiate();
332
}
333
graph_texture->set_image(img);
334
}
335
336
graph_texture->update(img);
337
338
graph->set_texture(graph_texture);
339
graph->queue_redraw();
340
}
341
342
void EditorProfiler::_update_frame() {
343
int cursor_metric = cursor_metric_edit->get_value() - _get_frame_metric(0).frame_number;
344
345
updating_frame = true;
346
variables->clear();
347
348
TreeItem *root = variables->create_item();
349
const Metric &m = _get_frame_metric(cursor_metric);
350
351
int dtime = display_time->get_selected();
352
353
for (int i = 0; i < m.categories.size(); i++) {
354
TreeItem *category = variables->create_item(root);
355
category->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
356
category->set_editable(0, true);
357
category->set_metadata(0, m.categories[i].signature);
358
category->set_text(0, String(m.categories[i].name));
359
category->set_text(1, _get_time_as_text(m, m.categories[i].total_time, 1));
360
361
if (plot_sigs.has(m.categories[i].signature)) {
362
category->set_checked(0, true);
363
category->set_custom_color(0, _get_color_from_signature(m.categories[i].signature));
364
}
365
366
for (int j = 0; j < m.categories[i].items.size(); j++) {
367
const Metric::Category::Item &it = m.categories[i].items[j];
368
369
if (it.internal == it.total && !display_internal_profiles->is_pressed() && m.categories[i].name == "Script Functions") {
370
continue;
371
}
372
TreeItem *item = variables->create_item(category);
373
item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
374
item->set_editable(0, true);
375
item->set_text(0, it.name);
376
item->set_metadata(0, it.signature);
377
item->set_metadata(1, it.script);
378
item->set_metadata(2, it.line);
379
item->set_text_alignment(2, HORIZONTAL_ALIGNMENT_RIGHT);
380
item->set_tooltip_text(0, it.name + "\n" + it.script + ":" + itos(it.line));
381
382
float time = dtime == DISPLAY_SELF_TIME ? it.self : it.total;
383
if (dtime == DISPLAY_SELF_TIME && !display_internal_profiles->is_pressed()) {
384
time += it.internal;
385
}
386
387
item->set_text(1, _get_time_as_text(m, time, it.calls));
388
389
item->set_text(2, itos(it.calls));
390
391
if (plot_sigs.has(it.signature)) {
392
item->set_checked(0, true);
393
item->set_custom_color(0, _get_color_from_signature(it.signature));
394
}
395
}
396
}
397
398
updating_frame = false;
399
}
400
401
void EditorProfiler::_update_button_text() {
402
if (activate->is_pressed()) {
403
activate->set_button_icon(get_editor_theme_icon(SNAME("Stop")));
404
activate->set_text(TTR("Stop"));
405
} else {
406
activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));
407
activate->set_text(TTR("Start"));
408
}
409
}
410
411
void EditorProfiler::_activate_pressed() {
412
_update_button_text();
413
414
if (activate->is_pressed()) {
415
_clear_pressed();
416
}
417
418
emit_signal(SNAME("enable_profiling"), activate->is_pressed());
419
}
420
421
void EditorProfiler::_clear_pressed() {
422
clear_button->set_disabled(true);
423
clear();
424
_update_plot();
425
}
426
427
void EditorProfiler::_internal_profiles_pressed() {
428
_combo_changed(0);
429
}
430
431
void EditorProfiler::_autostart_toggled(bool p_toggled_on) {
432
EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_profiler", p_toggled_on);
433
EditorRunBar::get_singleton()->update_profiler_autostart_indicator();
434
}
435
436
void EditorProfiler::_notification(int p_what) {
437
switch (p_what) {
438
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
439
case NOTIFICATION_THEME_CHANGED:
440
case NOTIFICATION_TRANSLATION_CHANGED: {
441
activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));
442
clear_button->set_button_icon(get_editor_theme_icon(SNAME("Clear")));
443
444
theme_cache.seek_line_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
445
theme_cache.seek_line_color.a = 0.8;
446
theme_cache.seek_line_hover_color = theme_cache.seek_line_color;
447
theme_cache.seek_line_hover_color.a = 0.4;
448
449
if (total_metrics > 0) {
450
_update_plot();
451
}
452
} break;
453
}
454
}
455
456
void EditorProfiler::_graph_tex_draw() {
457
if (total_metrics == 0) {
458
return;
459
}
460
if (seeking) {
461
int frame = cursor_metric_edit->get_value() - _get_frame_metric(0).frame_number;
462
frame = frame - _get_zoom_left_border() + 1;
463
int cur_x = (frame * graph->get_size().width * Math::exp(graph_zoom)) / frame_metrics.size();
464
cur_x = CLAMP(cur_x, 0, graph->get_size().width);
465
graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), theme_cache.seek_line_color);
466
}
467
if (hover_metric > -1) {
468
int cur_x = (2 * hover_metric + 1) * graph->get_size().x / (2 * frame_metrics.size()) + 1;
469
graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), theme_cache.seek_line_hover_color);
470
}
471
}
472
473
void EditorProfiler::_graph_tex_mouse_exit() {
474
hover_metric = -1;
475
graph->queue_redraw();
476
}
477
478
void EditorProfiler::_cursor_metric_changed(double) {
479
if (updating_frame) {
480
return;
481
}
482
483
graph->queue_redraw();
484
_update_frame();
485
}
486
487
void EditorProfiler::_graph_tex_input(const Ref<InputEvent> &p_ev) {
488
if (last_metric < 0) {
489
return;
490
}
491
492
Ref<InputEventMouse> me = p_ev;
493
Ref<InputEventMouseButton> mb = p_ev;
494
Ref<InputEventMouseMotion> mm = p_ev;
495
MouseButton button_idx = mb.is_valid() ? mb->get_button_index() : MouseButton();
496
497
if (
498
(mb.is_valid() && button_idx == MouseButton::LEFT && mb->is_pressed()) ||
499
(mm.is_valid())) {
500
int x = me->get_position().x - 1;
501
hover_metric = x * frame_metrics.size() / graph->get_size().width;
502
503
x = x * frame_metrics.size() / graph->get_size().width;
504
x = x / Math::exp(graph_zoom) + _get_zoom_left_border();
505
x = CLAMP(x, 0, frame_metrics.size() - 1);
506
507
if (mb.is_valid() || (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
508
updating_frame = true;
509
510
if (x < total_metrics) {
511
cursor_metric_edit->set_value(_get_frame_metric(x).frame_number);
512
}
513
updating_frame = false;
514
515
if (activate->is_pressed()) {
516
if (!seeking) {
517
emit_signal(SNAME("break_request"));
518
}
519
}
520
521
seeking = true;
522
523
if (!frame_delay->is_processing()) {
524
frame_delay->set_wait_time(0.1);
525
frame_delay->start();
526
}
527
}
528
}
529
530
if (graph_zoom > 0 && mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::MIDDLE) || mm->get_button_mask().has_flag(MouseButtonMask::RIGHT))) {
531
// Panning.
532
const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);
533
pan_accumulator += (float)mm->get_relative().x * max_profiles_shown / graph->get_size().width;
534
535
if (Math::abs(pan_accumulator) > 1) {
536
zoom_center = CLAMP(zoom_center - (int)pan_accumulator, max_profiles_shown / 2, frame_metrics.size() - max_profiles_shown / 2);
537
pan_accumulator -= (int)pan_accumulator;
538
_update_plot();
539
}
540
}
541
542
if (button_idx == MouseButton::WHEEL_DOWN) {
543
// Zooming.
544
graph_zoom = MAX(-0.05 + graph_zoom, 0);
545
_update_plot();
546
} else if (button_idx == MouseButton::WHEEL_UP) {
547
if (graph_zoom == 0) {
548
zoom_center = me->get_position().x;
549
zoom_center = zoom_center * frame_metrics.size() / graph->get_size().width;
550
}
551
graph_zoom = MIN(0.05 + graph_zoom, 2);
552
_update_plot();
553
}
554
555
graph->queue_redraw();
556
}
557
558
void EditorProfiler::disable_seeking() {
559
seeking = false;
560
graph->queue_redraw();
561
}
562
563
void EditorProfiler::_combo_changed(int) {
564
_update_frame();
565
_update_plot();
566
}
567
568
void EditorProfiler::_bind_methods() {
569
ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));
570
ADD_SIGNAL(MethodInfo("break_request"));
571
}
572
573
void EditorProfiler::set_enabled(bool p_enable, bool p_clear) {
574
activate->set_disabled(!p_enable);
575
if (p_clear) {
576
clear();
577
}
578
}
579
580
void EditorProfiler::set_profiling(bool p_pressed) {
581
activate->set_pressed(p_pressed);
582
_update_button_text();
583
emit_signal(SNAME("enable_profiling"), activate->is_pressed());
584
}
585
586
bool EditorProfiler::is_profiling() {
587
return activate->is_pressed();
588
}
589
590
Vector<Vector<String>> EditorProfiler::get_data_as_csv() const {
591
Vector<Vector<String>> res;
592
593
if (frame_metrics.is_empty()) {
594
return res;
595
}
596
597
// Different metrics may contain different number of categories.
598
HashSet<StringName> possible_signatures;
599
for (int i = 0; i < frame_metrics.size(); i++) {
600
const Metric &m = frame_metrics[i];
601
if (!m.valid) {
602
continue;
603
}
604
for (const KeyValue<StringName, Metric::Category *> &E : m.category_ptrs) {
605
possible_signatures.insert(E.key);
606
}
607
for (const KeyValue<StringName, Metric::Category::Item *> &E : m.item_ptrs) {
608
possible_signatures.insert(E.key);
609
}
610
}
611
612
// Generate CSV header and cache indices.
613
HashMap<StringName, int> sig_map;
614
Vector<String> signatures;
615
signatures.resize(possible_signatures.size());
616
int sig_index = 0;
617
for (const StringName &E : possible_signatures) {
618
signatures.write[sig_index] = E;
619
sig_map[E] = sig_index;
620
sig_index++;
621
}
622
res.push_back(signatures);
623
624
// values
625
Vector<String> values;
626
627
int index = last_metric;
628
629
for (int i = 0; i < frame_metrics.size(); i++) {
630
++index;
631
632
if (index >= frame_metrics.size()) {
633
index = 0;
634
}
635
636
const Metric &m = frame_metrics[index];
637
638
if (!m.valid) {
639
continue;
640
}
641
642
// Don't keep old values since there may be empty cells.
643
values.clear();
644
values.resize(possible_signatures.size());
645
646
for (const KeyValue<StringName, Metric::Category *> &E : m.category_ptrs) {
647
values.write[sig_map[E.key]] = String::num_real(E.value->total_time);
648
}
649
for (const KeyValue<StringName, Metric::Category::Item *> &E : m.item_ptrs) {
650
values.write[sig_map[E.key]] = String::num_real(E.value->total);
651
}
652
653
res.push_back(values);
654
}
655
656
return res;
657
}
658
659
EditorProfiler::EditorProfiler() {
660
HBoxContainer *hb = memnew(HBoxContainer);
661
hb->add_theme_constant_override(SNAME("separation"), 8 * EDSCALE);
662
add_child(hb);
663
664
FlowContainer *container = memnew(FlowContainer);
665
container->set_h_size_flags(SIZE_EXPAND_FILL);
666
container->add_theme_constant_override(SNAME("h_separation"), 8 * EDSCALE);
667
container->add_theme_constant_override(SNAME("v_separation"), 2 * EDSCALE);
668
hb->add_child(container);
669
670
activate = memnew(Button);
671
activate->set_toggle_mode(true);
672
activate->set_disabled(true);
673
activate->set_text(TTR("Start"));
674
activate->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_activate_pressed));
675
container->add_child(activate);
676
677
clear_button = memnew(Button);
678
clear_button->set_text(TTR("Clear"));
679
clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_clear_pressed));
680
clear_button->set_disabled(true);
681
container->add_child(clear_button);
682
683
CheckBox *autostart_checkbox = memnew(CheckBox);
684
autostart_checkbox->set_text(TTR("Autostart"));
685
autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_profiler", false));
686
autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorProfiler::_autostart_toggled));
687
container->add_child(autostart_checkbox);
688
689
HBoxContainer *hb_measure = memnew(HBoxContainer);
690
hb_measure->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);
691
container->add_child(hb_measure);
692
693
hb_measure->add_child(memnew(Label(TTR("Measure:"))));
694
695
display_mode = memnew(OptionButton);
696
display_mode->set_accessibility_name(TTRC("Measure:"));
697
display_mode->add_item(TTR("Frame Time (ms)"));
698
display_mode->add_item(TTR("Average Time (ms)"));
699
display_mode->add_item(TTR("Frame %"));
700
display_mode->add_item(TTR("Physics Frame %"));
701
display_mode->connect(SceneStringName(item_selected), callable_mp(this, &EditorProfiler::_combo_changed));
702
703
hb_measure->add_child(display_mode);
704
705
HBoxContainer *hb_time = memnew(HBoxContainer);
706
hb_time->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);
707
container->add_child(hb_time);
708
709
hb_time->add_child(memnew(Label(TTR("Time:"))));
710
711
display_time = memnew(OptionButton);
712
display_time->set_accessibility_name(TTRC("Time:"));
713
// TRANSLATORS: This is an option in the profiler to display the time spent in a function, including the time spent in other functions called by that function.
714
display_time->add_item(TTR("Inclusive"));
715
// TRANSLATORS: This is an option in the profiler to display the time spent in a function, exincluding the time spent in other functions called by that function.
716
display_time->add_item(TTR("Self"));
717
display_time->set_tooltip_text(TTR("Inclusive: Includes time from other functions called by this function.\nUse this to spot bottlenecks.\n\nSelf: Only count the time spent in the function itself, not in other functions called by that function.\nUse this to find individual functions to optimize."));
718
display_time->connect(SceneStringName(item_selected), callable_mp(this, &EditorProfiler::_combo_changed));
719
hb_time->add_child(display_time);
720
721
display_internal_profiles = memnew(CheckButton(TTR("Display internal functions")));
722
display_internal_profiles->set_visible(EDITOR_GET("debugger/profile_native_calls"));
723
display_internal_profiles->set_pressed(false);
724
display_internal_profiles->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_internal_profiles_pressed));
725
container->add_child(display_internal_profiles);
726
727
HBoxContainer *hb_frame = memnew(HBoxContainer);
728
hb_frame->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);
729
hb_frame->set_v_size_flags(SIZE_SHRINK_BEGIN);
730
hb->add_child(hb_frame);
731
732
hb_frame->add_child(memnew(Label(TTR("Frame #:"))));
733
734
cursor_metric_edit = memnew(SpinBox);
735
cursor_metric_edit->set_accessibility_name(TTRC("Frame #:"));
736
cursor_metric_edit->set_h_size_flags(SIZE_FILL);
737
cursor_metric_edit->set_value(0);
738
cursor_metric_edit->set_editable(false);
739
hb_frame->add_child(cursor_metric_edit);
740
cursor_metric_edit->connect(SceneStringName(value_changed), callable_mp(this, &EditorProfiler::_cursor_metric_changed));
741
742
h_split = memnew(HSplitContainer);
743
add_child(h_split);
744
h_split->set_v_size_flags(SIZE_EXPAND_FILL);
745
746
variables = memnew(Tree);
747
variables->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
748
variables->set_custom_minimum_size(Size2(320, 0) * EDSCALE);
749
variables->set_hide_folding(true);
750
h_split->add_child(variables);
751
variables->set_hide_root(true);
752
variables->set_columns(3);
753
variables->set_column_titles_visible(true);
754
variables->set_column_title(0, TTR("Name"));
755
variables->set_column_expand(0, true);
756
variables->set_column_clip_content(0, true);
757
variables->set_column_custom_minimum_width(0, 60);
758
variables->set_column_title(1, TTR("Time"));
759
variables->set_column_expand(1, false);
760
variables->set_column_clip_content(1, true);
761
variables->set_column_custom_minimum_width(1, 75 * EDSCALE);
762
variables->set_column_title(2, TTR("Calls"));
763
variables->set_column_expand(2, false);
764
variables->set_column_clip_content(2, true);
765
variables->set_column_custom_minimum_width(2, 50 * EDSCALE);
766
variables->set_theme_type_variation("TreeSecondary");
767
variables->connect("item_edited", callable_mp(this, &EditorProfiler::_item_edited));
768
769
graph = memnew(TextureRect);
770
graph->set_custom_minimum_size(Size2(250 * EDSCALE, 0));
771
graph->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
772
graph->set_mouse_filter(MOUSE_FILTER_STOP);
773
graph->connect(SceneStringName(draw), callable_mp(this, &EditorProfiler::_graph_tex_draw));
774
graph->connect(SceneStringName(gui_input), callable_mp(this, &EditorProfiler::_graph_tex_input));
775
graph->connect(SceneStringName(mouse_exited), callable_mp(this, &EditorProfiler::_graph_tex_mouse_exit));
776
777
h_split->add_child(graph);
778
graph->set_h_size_flags(SIZE_EXPAND_FILL);
779
780
int metric_size = CLAMP(int(EDITOR_GET("debugger/profiler_frame_history_size")), 60, 10000);
781
frame_metrics.resize(metric_size);
782
783
frame_delay = memnew(Timer);
784
frame_delay->set_wait_time(0.1);
785
frame_delay->set_one_shot(true);
786
add_child(frame_delay);
787
frame_delay->connect("timeout", callable_mp(this, &EditorProfiler::_update_frame));
788
789
plot_delay = memnew(Timer);
790
plot_delay->set_wait_time(0.1);
791
plot_delay->set_one_shot(true);
792
add_child(plot_delay);
793
plot_delay->connect("timeout", callable_mp(this, &EditorProfiler::_update_plot));
794
795
plot_sigs.insert("physics_frame_time");
796
plot_sigs.insert("category_frame_time");
797
}
798
799