editor_profiler.cpp 22 KB

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