editor_profiler.cpp 22 KB

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