editor_profiler.cpp 21 KB

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