Debian Codegraph @

editor/debugger/editor_profiler.cpp

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