editor_profiler.cpp 21 KB

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