editor_profiler.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. #include "editor_profiler.h"
  31. #include "core/io/image.h"
  32. #include "core/os/os.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/themes/editor_scale.h"
  36. #include "editor/themes/editor_theme_manager.h"
  37. #include "scene/gui/check_box.h"
  38. #include "scene/resources/image_texture.h"
  39. void EditorProfiler::_make_metric_ptrs(Metric &m) {
  40. for (int i = 0; i < m.categories.size(); i++) {
  41. m.category_ptrs[m.categories[i].signature] = &m.categories.write[i];
  42. for (int j = 0; j < m.categories[i].items.size(); j++) {
  43. m.item_ptrs[m.categories[i].items[j].signature] = &m.categories.write[i].items.write[j];
  44. }
  45. }
  46. }
  47. EditorProfiler::Metric EditorProfiler::_get_frame_metric(int index) {
  48. return frame_metrics[(frame_metrics.size() + last_metric - (total_metrics - 1) + index) % frame_metrics.size()];
  49. }
  50. void EditorProfiler::add_frame_metric(const Metric &p_metric, bool p_final) {
  51. ++last_metric;
  52. if (last_metric >= frame_metrics.size()) {
  53. last_metric = 0;
  54. }
  55. total_metrics++;
  56. if (total_metrics > frame_metrics.size()) {
  57. total_metrics = frame_metrics.size();
  58. }
  59. frame_metrics.write[last_metric] = p_metric;
  60. _make_metric_ptrs(frame_metrics.write[last_metric]);
  61. updating_frame = true;
  62. clear_button->set_disabled(false);
  63. cursor_metric_edit->set_editable(true);
  64. cursor_metric_edit->set_max(p_metric.frame_number);
  65. cursor_metric_edit->set_min(_get_frame_metric(0).frame_number);
  66. if (!seeking) {
  67. cursor_metric_edit->set_value(p_metric.frame_number);
  68. }
  69. updating_frame = false;
  70. if (frame_delay->is_stopped()) {
  71. frame_delay->set_wait_time(p_final ? 0.1 : 1);
  72. frame_delay->start();
  73. }
  74. if (plot_delay->is_stopped()) {
  75. plot_delay->set_wait_time(0.1);
  76. plot_delay->start();
  77. }
  78. }
  79. void EditorProfiler::clear() {
  80. int metric_size = EDITOR_GET("debugger/profiler_frame_history_size");
  81. metric_size = CLAMP(metric_size, 60, 10000);
  82. frame_metrics.clear();
  83. frame_metrics.resize(metric_size);
  84. total_metrics = 0;
  85. last_metric = -1;
  86. variables->clear();
  87. plot_sigs.clear();
  88. plot_sigs.insert("physics_frame_time");
  89. plot_sigs.insert("category_frame_time");
  90. display_internal_profiles->set_visible(EDITOR_GET("debugger/profile_native_calls"));
  91. updating_frame = true;
  92. cursor_metric_edit->set_min(0);
  93. cursor_metric_edit->set_max(100); // Doesn't make much sense, but we can't have min == max. Doesn't hurt.
  94. cursor_metric_edit->set_value(0);
  95. cursor_metric_edit->set_editable(false);
  96. updating_frame = false;
  97. hover_metric = -1;
  98. seeking = false;
  99. // Ensure button text (start, stop) is correct
  100. _update_button_text();
  101. emit_signal(SNAME("enable_profiling"), activate->is_pressed());
  102. }
  103. static String _get_percent_txt(float p_value, float p_total) {
  104. if (p_total == 0) {
  105. p_total = 0.00001;
  106. }
  107. return TS->format_number(String::num((p_value / p_total) * 100, 1)) + TS->percent_sign();
  108. }
  109. String EditorProfiler::_get_time_as_text(const Metric &m, float p_time, int p_calls) {
  110. const int dmode = display_mode->get_selected();
  111. if (dmode == DISPLAY_FRAME_TIME) {
  112. return TS->format_number(rtos(p_time * 1000).pad_decimals(2)) + " " + TTR("ms");
  113. } else if (dmode == DISPLAY_AVERAGE_TIME) {
  114. if (p_calls == 0) {
  115. return TS->format_number("0.00") + " " + TTR("ms");
  116. } else {
  117. return TS->format_number(rtos((p_time / p_calls) * 1000).pad_decimals(2)) + " " + TTR("ms");
  118. }
  119. } else if (dmode == DISPLAY_FRAME_PERCENT) {
  120. return _get_percent_txt(p_time, m.frame_time);
  121. } else if (dmode == DISPLAY_PHYSICS_FRAME_PERCENT) {
  122. return _get_percent_txt(p_time, m.physics_frame_time);
  123. }
  124. return "err";
  125. }
  126. Color EditorProfiler::_get_color_from_signature(const StringName &p_signature) const {
  127. Color bc = get_theme_color(SNAME("error_color"), EditorStringName(Editor));
  128. double rot = ABS(double(p_signature.hash()) / double(0x7FFFFFFF));
  129. Color c;
  130. c.set_hsv(rot, bc.get_s(), bc.get_v());
  131. return c.lerp(get_theme_color(SNAME("base_color"), EditorStringName(Editor)), 0.07);
  132. }
  133. void EditorProfiler::_item_edited() {
  134. if (updating_frame) {
  135. return;
  136. }
  137. TreeItem *item = variables->get_edited();
  138. if (!item) {
  139. return;
  140. }
  141. StringName signature = item->get_metadata(0);
  142. bool checked = item->is_checked(0);
  143. if (checked) {
  144. plot_sigs.insert(signature);
  145. } else {
  146. plot_sigs.erase(signature);
  147. }
  148. if (!frame_delay->is_processing()) {
  149. frame_delay->set_wait_time(0.1);
  150. frame_delay->start();
  151. }
  152. _update_plot();
  153. }
  154. void EditorProfiler::_update_plot() {
  155. const int w = MAX(1, graph->get_size().width); // Clamp to 1 to prevent from crashing when profiler is autostarted.
  156. const int h = MAX(1, graph->get_size().height);
  157. bool reset_texture = false;
  158. const int desired_len = w * h * 4;
  159. if (graph_image.size() != desired_len) {
  160. reset_texture = true;
  161. graph_image.resize(desired_len);
  162. }
  163. uint8_t *wr = graph_image.ptrw();
  164. const Color background_color = get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor));
  165. // Clear the previous frame and set the background color.
  166. for (int i = 0; i < desired_len; i += 4) {
  167. wr[i + 0] = Math::fast_ftoi(background_color.r * 255);
  168. wr[i + 1] = Math::fast_ftoi(background_color.g * 255);
  169. wr[i + 2] = Math::fast_ftoi(background_color.b * 255);
  170. wr[i + 3] = 255;
  171. }
  172. //find highest value
  173. const bool use_self = display_time->get_selected() == DISPLAY_SELF_TIME;
  174. float highest = 0;
  175. for (int i = 0; i < total_metrics; i++) {
  176. const Metric &m = _get_frame_metric(i);
  177. for (const StringName &E : plot_sigs) {
  178. HashMap<StringName, Metric::Category *>::ConstIterator F = m.category_ptrs.find(E);
  179. if (F) {
  180. highest = MAX(F->value->total_time, highest);
  181. }
  182. HashMap<StringName, Metric::Category::Item *>::ConstIterator G = m.item_ptrs.find(E);
  183. if (G) {
  184. if (use_self) {
  185. highest = MAX(G->value->self, highest);
  186. } else {
  187. highest = MAX(G->value->total, highest);
  188. }
  189. }
  190. }
  191. }
  192. if (highest > 0) {
  193. //means some data exists..
  194. highest *= 1.2; //leave some upper room
  195. graph_height = highest;
  196. Vector<int> columnv;
  197. columnv.resize(h * 4);
  198. int *column = columnv.ptrw();
  199. HashMap<StringName, int> prev_plots;
  200. for (int i = 0; i < total_metrics * w / frame_metrics.size() - 1; i++) {
  201. for (int j = 0; j < h * 4; j++) {
  202. column[j] = 0;
  203. }
  204. int current = i * frame_metrics.size() / w;
  205. for (const StringName &E : plot_sigs) {
  206. const Metric &m = _get_frame_metric(current);
  207. float value = 0;
  208. HashMap<StringName, Metric::Category *>::ConstIterator F = m.category_ptrs.find(E);
  209. if (F) {
  210. value = F->value->total_time;
  211. }
  212. HashMap<StringName, Metric::Category::Item *>::ConstIterator G = m.item_ptrs.find(E);
  213. if (G) {
  214. if (use_self) {
  215. value = G->value->self;
  216. } else {
  217. value = G->value->total;
  218. }
  219. }
  220. int plot_pos = CLAMP(int(value * h / highest), 0, h - 1);
  221. int prev_plot = plot_pos;
  222. HashMap<StringName, int>::Iterator H = prev_plots.find(E);
  223. if (H) {
  224. prev_plot = H->value;
  225. H->value = plot_pos;
  226. } else {
  227. prev_plots[E] = plot_pos;
  228. }
  229. plot_pos = h - plot_pos - 1;
  230. prev_plot = h - prev_plot - 1;
  231. if (prev_plot > plot_pos) {
  232. SWAP(prev_plot, plot_pos);
  233. }
  234. Color col = _get_color_from_signature(E);
  235. for (int j = prev_plot; j <= plot_pos; j++) {
  236. column[j * 4 + 0] += Math::fast_ftoi(CLAMP(col.r * 255, 0, 255));
  237. column[j * 4 + 1] += Math::fast_ftoi(CLAMP(col.g * 255, 0, 255));
  238. column[j * 4 + 2] += Math::fast_ftoi(CLAMP(col.b * 255, 0, 255));
  239. column[j * 4 + 3] += 1;
  240. }
  241. }
  242. for (int j = 0; j < h * 4; j += 4) {
  243. const int a = column[j + 3];
  244. if (a > 0) {
  245. column[j + 0] /= a;
  246. column[j + 1] /= a;
  247. column[j + 2] /= a;
  248. }
  249. const uint8_t red = uint8_t(column[j + 0]);
  250. const uint8_t green = uint8_t(column[j + 1]);
  251. const uint8_t blue = uint8_t(column[j + 2]);
  252. const bool is_filled = red >= 1 || green >= 1 || blue >= 1;
  253. const int widx = ((j >> 2) * w + i) * 4;
  254. // If the pixel isn't filled by any profiler line, apply the background color instead.
  255. wr[widx + 0] = is_filled ? red : Math::fast_ftoi(background_color.r * 255);
  256. wr[widx + 1] = is_filled ? green : Math::fast_ftoi(background_color.g * 255);
  257. wr[widx + 2] = is_filled ? blue : Math::fast_ftoi(background_color.b * 255);
  258. wr[widx + 3] = 255;
  259. }
  260. }
  261. }
  262. Ref<Image> img = Image::create_from_data(w, h, false, Image::FORMAT_RGBA8, graph_image);
  263. if (reset_texture) {
  264. if (graph_texture.is_null()) {
  265. graph_texture.instantiate();
  266. }
  267. graph_texture->set_image(img);
  268. }
  269. graph_texture->update(img);
  270. graph->set_texture(graph_texture);
  271. graph->queue_redraw();
  272. }
  273. void EditorProfiler::_update_frame() {
  274. int cursor_metric = cursor_metric_edit->get_value() - _get_frame_metric(0).frame_number;
  275. updating_frame = true;
  276. variables->clear();
  277. TreeItem *root = variables->create_item();
  278. const Metric &m = _get_frame_metric(cursor_metric);
  279. int dtime = display_time->get_selected();
  280. for (int i = 0; i < m.categories.size(); i++) {
  281. TreeItem *category = variables->create_item(root);
  282. category->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  283. category->set_editable(0, true);
  284. category->set_metadata(0, m.categories[i].signature);
  285. category->set_text(0, String(m.categories[i].name));
  286. category->set_text(1, _get_time_as_text(m, m.categories[i].total_time, 1));
  287. if (plot_sigs.has(m.categories[i].signature)) {
  288. category->set_checked(0, true);
  289. category->set_custom_color(0, _get_color_from_signature(m.categories[i].signature));
  290. }
  291. for (int j = 0; j < m.categories[i].items.size(); j++) {
  292. const Metric::Category::Item &it = m.categories[i].items[j];
  293. if (it.internal == it.total && !display_internal_profiles->is_pressed() && m.categories[i].name == "Script Functions") {
  294. continue;
  295. }
  296. TreeItem *item = variables->create_item(category);
  297. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  298. item->set_editable(0, true);
  299. item->set_text(0, it.name);
  300. item->set_metadata(0, it.signature);
  301. item->set_metadata(1, it.script);
  302. item->set_metadata(2, it.line);
  303. item->set_text_alignment(2, HORIZONTAL_ALIGNMENT_RIGHT);
  304. item->set_tooltip_text(0, it.name + "\n" + it.script + ":" + itos(it.line));
  305. float time = dtime == DISPLAY_SELF_TIME ? it.self : it.total;
  306. if (dtime == DISPLAY_SELF_TIME && !display_internal_profiles->is_pressed()) {
  307. time += it.internal;
  308. }
  309. item->set_text(1, _get_time_as_text(m, time, it.calls));
  310. item->set_text(2, itos(it.calls));
  311. if (plot_sigs.has(it.signature)) {
  312. item->set_checked(0, true);
  313. item->set_custom_color(0, _get_color_from_signature(it.signature));
  314. }
  315. }
  316. }
  317. updating_frame = false;
  318. }
  319. void EditorProfiler::_update_button_text() {
  320. if (activate->is_pressed()) {
  321. activate->set_button_icon(get_editor_theme_icon(SNAME("Stop")));
  322. activate->set_text(TTR("Stop"));
  323. } else {
  324. activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));
  325. activate->set_text(TTR("Start"));
  326. }
  327. }
  328. void EditorProfiler::_activate_pressed() {
  329. _update_button_text();
  330. if (activate->is_pressed()) {
  331. _clear_pressed();
  332. }
  333. emit_signal(SNAME("enable_profiling"), activate->is_pressed());
  334. }
  335. void EditorProfiler::_clear_pressed() {
  336. clear_button->set_disabled(true);
  337. clear();
  338. _update_plot();
  339. }
  340. void EditorProfiler::_internal_profiles_pressed() {
  341. _combo_changed(0);
  342. }
  343. void EditorProfiler::_autostart_toggled(bool p_toggled_on) {
  344. EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_profiler", p_toggled_on);
  345. }
  346. void EditorProfiler::_notification(int p_what) {
  347. switch (p_what) {
  348. case NOTIFICATION_ENTER_TREE:
  349. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  350. case NOTIFICATION_THEME_CHANGED:
  351. case NOTIFICATION_TRANSLATION_CHANGED: {
  352. activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));
  353. clear_button->set_button_icon(get_editor_theme_icon(SNAME("Clear")));
  354. theme_cache.seek_line_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
  355. theme_cache.seek_line_color.a = 0.8;
  356. theme_cache.seek_line_hover_color = theme_cache.seek_line_color;
  357. theme_cache.seek_line_hover_color.a = 0.4;
  358. if (total_metrics > 0) {
  359. _update_plot();
  360. }
  361. } break;
  362. }
  363. }
  364. void EditorProfiler::_graph_tex_draw() {
  365. if (total_metrics == 0) {
  366. return;
  367. }
  368. if (seeking) {
  369. int frame = cursor_metric_edit->get_value() - _get_frame_metric(0).frame_number;
  370. int cur_x = (2 * frame + 1) * graph->get_size().x / (2 * frame_metrics.size()) + 1;
  371. graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), theme_cache.seek_line_color);
  372. }
  373. if (hover_metric > -1 && hover_metric < total_metrics) {
  374. int cur_x = (2 * hover_metric + 1) * graph->get_size().x / (2 * frame_metrics.size()) + 1;
  375. graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), theme_cache.seek_line_hover_color);
  376. }
  377. }
  378. void EditorProfiler::_graph_tex_mouse_exit() {
  379. hover_metric = -1;
  380. graph->queue_redraw();
  381. }
  382. void EditorProfiler::_cursor_metric_changed(double) {
  383. if (updating_frame) {
  384. return;
  385. }
  386. graph->queue_redraw();
  387. _update_frame();
  388. }
  389. void EditorProfiler::_graph_tex_input(const Ref<InputEvent> &p_ev) {
  390. if (last_metric < 0) {
  391. return;
  392. }
  393. Ref<InputEventMouse> me = p_ev;
  394. Ref<InputEventMouseButton> mb = p_ev;
  395. Ref<InputEventMouseMotion> mm = p_ev;
  396. if (
  397. (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) ||
  398. (mm.is_valid())) {
  399. int x = me->get_position().x - 1;
  400. x = x * frame_metrics.size() / graph->get_size().width;
  401. hover_metric = x;
  402. if (x < 0) {
  403. x = 0;
  404. }
  405. if (x >= frame_metrics.size()) {
  406. x = frame_metrics.size() - 1;
  407. }
  408. if (mb.is_valid() || (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  409. updating_frame = true;
  410. if (x < total_metrics) {
  411. cursor_metric_edit->set_value(_get_frame_metric(x).frame_number);
  412. }
  413. updating_frame = false;
  414. if (activate->is_pressed()) {
  415. if (!seeking) {
  416. emit_signal(SNAME("break_request"));
  417. }
  418. }
  419. seeking = true;
  420. if (!frame_delay->is_processing()) {
  421. frame_delay->set_wait_time(0.1);
  422. frame_delay->start();
  423. }
  424. }
  425. graph->queue_redraw();
  426. }
  427. }
  428. void EditorProfiler::disable_seeking() {
  429. seeking = false;
  430. graph->queue_redraw();
  431. }
  432. void EditorProfiler::_combo_changed(int) {
  433. _update_frame();
  434. _update_plot();
  435. }
  436. void EditorProfiler::_bind_methods() {
  437. ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));
  438. ADD_SIGNAL(MethodInfo("break_request"));
  439. }
  440. void EditorProfiler::set_enabled(bool p_enable, bool p_clear) {
  441. activate->set_disabled(!p_enable);
  442. if (p_clear) {
  443. clear();
  444. }
  445. }
  446. void EditorProfiler::set_profiling(bool p_pressed) {
  447. activate->set_pressed(p_pressed);
  448. _update_button_text();
  449. emit_signal(SNAME("enable_profiling"), activate->is_pressed());
  450. }
  451. bool EditorProfiler::is_profiling() {
  452. return activate->is_pressed();
  453. }
  454. Vector<Vector<String>> EditorProfiler::get_data_as_csv() const {
  455. Vector<Vector<String>> res;
  456. if (frame_metrics.is_empty()) {
  457. return res;
  458. }
  459. // Different metrics may contain different number of categories.
  460. HashSet<StringName> possible_signatures;
  461. for (int i = 0; i < frame_metrics.size(); i++) {
  462. const Metric &m = frame_metrics[i];
  463. if (!m.valid) {
  464. continue;
  465. }
  466. for (const KeyValue<StringName, Metric::Category *> &E : m.category_ptrs) {
  467. possible_signatures.insert(E.key);
  468. }
  469. for (const KeyValue<StringName, Metric::Category::Item *> &E : m.item_ptrs) {
  470. possible_signatures.insert(E.key);
  471. }
  472. }
  473. // Generate CSV header and cache indices.
  474. HashMap<StringName, int> sig_map;
  475. Vector<String> signatures;
  476. signatures.resize(possible_signatures.size());
  477. int sig_index = 0;
  478. for (const StringName &E : possible_signatures) {
  479. signatures.write[sig_index] = E;
  480. sig_map[E] = sig_index;
  481. sig_index++;
  482. }
  483. res.push_back(signatures);
  484. // values
  485. Vector<String> values;
  486. int index = last_metric;
  487. for (int i = 0; i < frame_metrics.size(); i++) {
  488. ++index;
  489. if (index >= frame_metrics.size()) {
  490. index = 0;
  491. }
  492. const Metric &m = frame_metrics[index];
  493. if (!m.valid) {
  494. continue;
  495. }
  496. // Don't keep old values since there may be empty cells.
  497. values.clear();
  498. values.resize(possible_signatures.size());
  499. for (const KeyValue<StringName, Metric::Category *> &E : m.category_ptrs) {
  500. values.write[sig_map[E.key]] = String::num_real(E.value->total_time);
  501. }
  502. for (const KeyValue<StringName, Metric::Category::Item *> &E : m.item_ptrs) {
  503. values.write[sig_map[E.key]] = String::num_real(E.value->total);
  504. }
  505. res.push_back(values);
  506. }
  507. return res;
  508. }
  509. EditorProfiler::EditorProfiler() {
  510. HBoxContainer *hb = memnew(HBoxContainer);
  511. add_child(hb);
  512. activate = memnew(Button);
  513. activate->set_toggle_mode(true);
  514. activate->set_disabled(true);
  515. activate->set_text(TTR("Start"));
  516. activate->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_activate_pressed));
  517. hb->add_child(activate);
  518. clear_button = memnew(Button);
  519. clear_button->set_text(TTR("Clear"));
  520. clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_clear_pressed));
  521. clear_button->set_disabled(true);
  522. hb->add_child(clear_button);
  523. CheckBox *autostart_checkbox = memnew(CheckBox);
  524. autostart_checkbox->set_text(TTR("Autostart"));
  525. autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_profiler", false));
  526. autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorProfiler::_autostart_toggled));
  527. hb->add_child(autostart_checkbox);
  528. hb->add_child(memnew(Label(TTR("Measure:"))));
  529. display_mode = memnew(OptionButton);
  530. display_mode->add_item(TTR("Frame Time (ms)"));
  531. display_mode->add_item(TTR("Average Time (ms)"));
  532. display_mode->add_item(TTR("Frame %"));
  533. display_mode->add_item(TTR("Physics Frame %"));
  534. display_mode->connect(SceneStringName(item_selected), callable_mp(this, &EditorProfiler::_combo_changed));
  535. hb->add_child(display_mode);
  536. hb->add_child(memnew(Label(TTR("Time:"))));
  537. display_time = memnew(OptionButton);
  538. // 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.
  539. display_time->add_item(TTR("Inclusive"));
  540. // 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.
  541. display_time->add_item(TTR("Self"));
  542. 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."));
  543. display_time->connect(SceneStringName(item_selected), callable_mp(this, &EditorProfiler::_combo_changed));
  544. hb->add_child(display_time);
  545. display_internal_profiles = memnew(CheckButton(TTR("Display internal functions")));
  546. display_internal_profiles->set_visible(EDITOR_GET("debugger/profile_native_calls"));
  547. display_internal_profiles->set_pressed(false);
  548. display_internal_profiles->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_internal_profiles_pressed));
  549. hb->add_child(display_internal_profiles);
  550. hb->add_spacer();
  551. hb->add_child(memnew(Label(TTR("Frame #:"))));
  552. cursor_metric_edit = memnew(SpinBox);
  553. cursor_metric_edit->set_h_size_flags(SIZE_FILL);
  554. cursor_metric_edit->set_value(0);
  555. cursor_metric_edit->set_editable(false);
  556. hb->add_child(cursor_metric_edit);
  557. cursor_metric_edit->connect(SceneStringName(value_changed), callable_mp(this, &EditorProfiler::_cursor_metric_changed));
  558. hb->add_theme_constant_override("separation", 8 * EDSCALE);
  559. h_split = memnew(HSplitContainer);
  560. add_child(h_split);
  561. h_split->set_v_size_flags(SIZE_EXPAND_FILL);
  562. variables = memnew(Tree);
  563. variables->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  564. variables->set_custom_minimum_size(Size2(320, 0) * EDSCALE);
  565. variables->set_hide_folding(true);
  566. h_split->add_child(variables);
  567. variables->set_hide_root(true);
  568. variables->set_columns(3);
  569. variables->set_column_titles_visible(true);
  570. variables->set_column_title(0, TTR("Name"));
  571. variables->set_column_expand(0, true);
  572. variables->set_column_clip_content(0, true);
  573. variables->set_column_custom_minimum_width(0, 60);
  574. variables->set_column_title(1, TTR("Time"));
  575. variables->set_column_expand(1, false);
  576. variables->set_column_clip_content(1, true);
  577. variables->set_column_custom_minimum_width(1, 75 * EDSCALE);
  578. variables->set_column_title(2, TTR("Calls"));
  579. variables->set_column_expand(2, false);
  580. variables->set_column_clip_content(2, true);
  581. variables->set_column_custom_minimum_width(2, 50 * EDSCALE);
  582. variables->set_theme_type_variation("TreeSecondary");
  583. variables->connect("item_edited", callable_mp(this, &EditorProfiler::_item_edited));
  584. graph = memnew(TextureRect);
  585. graph->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  586. graph->set_mouse_filter(MOUSE_FILTER_STOP);
  587. graph->connect(SceneStringName(draw), callable_mp(this, &EditorProfiler::_graph_tex_draw));
  588. graph->connect(SceneStringName(gui_input), callable_mp(this, &EditorProfiler::_graph_tex_input));
  589. graph->connect(SceneStringName(mouse_exited), callable_mp(this, &EditorProfiler::_graph_tex_mouse_exit));
  590. h_split->add_child(graph);
  591. graph->set_h_size_flags(SIZE_EXPAND_FILL);
  592. int metric_size = CLAMP(int(EDITOR_GET("debugger/profiler_frame_history_size")), 60, 10000);
  593. frame_metrics.resize(metric_size);
  594. frame_delay = memnew(Timer);
  595. frame_delay->set_wait_time(0.1);
  596. frame_delay->set_one_shot(true);
  597. add_child(frame_delay);
  598. frame_delay->connect("timeout", callable_mp(this, &EditorProfiler::_update_frame));
  599. plot_delay = memnew(Timer);
  600. plot_delay->set_wait_time(0.1);
  601. plot_delay->set_one_shot(true);
  602. add_child(plot_delay);
  603. plot_delay->connect("timeout", callable_mp(this, &EditorProfiler::_update_plot));
  604. plot_sigs.insert("physics_frame_time");
  605. plot_sigs.insert("category_frame_time");
  606. }