editor_visual_profiler.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /**************************************************************************/
  2. /* editor_visual_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_visual_profiler.h"
  31. #include "core/os/os.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor/editor_settings.h"
  34. void EditorVisualProfiler::add_frame_metric(const Metric &p_metric) {
  35. ++last_metric;
  36. if (last_metric >= frame_metrics.size()) {
  37. last_metric = 0;
  38. }
  39. frame_metrics.write[last_metric] = p_metric;
  40. List<String> stack;
  41. for (int i = 0; i < frame_metrics[last_metric].areas.size(); i++) {
  42. String name = frame_metrics[last_metric].areas[i].name;
  43. frame_metrics.write[last_metric].areas.write[i].color_cache = _get_color_from_signature(name);
  44. String full_name;
  45. if (name[0] == '<') {
  46. stack.pop_back();
  47. }
  48. if (stack.size()) {
  49. full_name = stack.back()->get() + name;
  50. } else {
  51. full_name = name;
  52. }
  53. if (name[0] == '>') {
  54. stack.push_back(full_name + "/");
  55. }
  56. frame_metrics.write[last_metric].areas.write[i].fullpath_cache = full_name;
  57. }
  58. updating_frame = true;
  59. clear_button->set_disabled(false);
  60. cursor_metric_edit->set_max(frame_metrics[last_metric].frame_number);
  61. cursor_metric_edit->set_min(MAX(frame_metrics[last_metric].frame_number - frame_metrics.size(), 0u));
  62. if (!seeking) {
  63. cursor_metric_edit->set_value(frame_metrics[last_metric].frame_number);
  64. if (hover_metric != -1) {
  65. hover_metric++;
  66. if (hover_metric >= frame_metrics.size()) {
  67. hover_metric = 0;
  68. }
  69. }
  70. }
  71. updating_frame = false;
  72. if (frame_delay->is_stopped()) {
  73. frame_delay->set_wait_time(0.1);
  74. frame_delay->start();
  75. }
  76. if (plot_delay->is_stopped()) {
  77. plot_delay->set_wait_time(0.1);
  78. plot_delay->start();
  79. }
  80. }
  81. void EditorVisualProfiler::clear() {
  82. int metric_size = EDITOR_GET("debugger/profiler_frame_history_size");
  83. metric_size = CLAMP(metric_size, 60, 10000);
  84. frame_metrics.clear();
  85. frame_metrics.resize(metric_size);
  86. last_metric = -1;
  87. variables->clear();
  88. //activate->set_pressed(false);
  89. updating_frame = true;
  90. cursor_metric_edit->set_min(0);
  91. cursor_metric_edit->set_max(0);
  92. cursor_metric_edit->set_value(0);
  93. updating_frame = false;
  94. hover_metric = -1;
  95. seeking = false;
  96. }
  97. String EditorVisualProfiler::_get_time_as_text(float p_time) {
  98. int dmode = display_mode->get_selected();
  99. if (dmode == DISPLAY_FRAME_TIME) {
  100. return TS->format_number(String::num(p_time, 2)) + " " + RTR("ms");
  101. } else if (dmode == DISPLAY_FRAME_PERCENT) {
  102. return TS->format_number(String::num(p_time * 100 / graph_limit, 2)) + " " + TS->percent_sign();
  103. }
  104. return "err";
  105. }
  106. Color EditorVisualProfiler::_get_color_from_signature(const StringName &p_signature) const {
  107. Color bc = get_theme_color(SNAME("error_color"), SNAME("Editor"));
  108. double rot = ABS(double(p_signature.hash()) / double(0x7FFFFFFF));
  109. Color c;
  110. c.set_hsv(rot, bc.get_s(), bc.get_v());
  111. return c.lerp(get_theme_color(SNAME("base_color"), SNAME("Editor")), 0.07);
  112. }
  113. void EditorVisualProfiler::_item_selected() {
  114. if (updating_frame) {
  115. return;
  116. }
  117. TreeItem *item = variables->get_selected();
  118. if (!item) {
  119. return;
  120. }
  121. selected_area = item->get_metadata(0);
  122. _update_plot();
  123. }
  124. void EditorVisualProfiler::_update_plot() {
  125. const int w = graph->get_size().width;
  126. const int h = graph->get_size().height;
  127. bool reset_texture = false;
  128. const int desired_len = w * h * 4;
  129. if (graph_image.size() != desired_len) {
  130. reset_texture = true;
  131. graph_image.resize(desired_len);
  132. }
  133. uint8_t *wr = graph_image.ptrw();
  134. const Color background_color = get_theme_color("dark_color_2", "Editor");
  135. // Clear the previous frame and set the background color.
  136. for (int i = 0; i < desired_len; i += 4) {
  137. wr[i + 0] = Math::fast_ftoi(background_color.r * 255);
  138. wr[i + 1] = Math::fast_ftoi(background_color.g * 255);
  139. wr[i + 2] = Math::fast_ftoi(background_color.b * 255);
  140. wr[i + 3] = 255;
  141. }
  142. //find highest value
  143. float highest_cpu = 0;
  144. float highest_gpu = 0;
  145. for (int i = 0; i < frame_metrics.size(); i++) {
  146. const Metric &m = frame_metrics[i];
  147. if (!m.valid) {
  148. continue;
  149. }
  150. if (m.areas.size()) {
  151. highest_cpu = MAX(highest_cpu, m.areas[m.areas.size() - 1].cpu_time);
  152. highest_gpu = MAX(highest_gpu, m.areas[m.areas.size() - 1].gpu_time);
  153. }
  154. }
  155. if (highest_cpu > 0 || highest_gpu > 0) {
  156. if (frame_relative->is_pressed()) {
  157. highest_cpu = MAX(graph_limit, highest_cpu);
  158. highest_gpu = MAX(graph_limit, highest_gpu);
  159. }
  160. if (linked->is_pressed()) {
  161. float highest = MAX(highest_cpu, highest_gpu);
  162. highest_cpu = highest_gpu = highest;
  163. }
  164. //means some data exists..
  165. highest_cpu *= 1.2; //leave some upper room
  166. highest_gpu *= 1.2; //leave some upper room
  167. graph_height_cpu = highest_cpu;
  168. graph_height_gpu = highest_gpu;
  169. Vector<Color> columnv_cpu;
  170. columnv_cpu.resize(h);
  171. Color *column_cpu = columnv_cpu.ptrw();
  172. Vector<Color> columnv_gpu;
  173. columnv_gpu.resize(h);
  174. Color *column_gpu = columnv_gpu.ptrw();
  175. int half_w = w / 2;
  176. for (int i = 0; i < half_w; i++) {
  177. for (int j = 0; j < h; j++) {
  178. column_cpu[j] = Color(0, 0, 0, 0);
  179. column_gpu[j] = Color(0, 0, 0, 0);
  180. }
  181. int current = i * frame_metrics.size() / half_w;
  182. int next = (i + 1) * frame_metrics.size() / half_w;
  183. if (next > frame_metrics.size()) {
  184. next = frame_metrics.size();
  185. }
  186. if (next == current) {
  187. next = current + 1; //just because for loop must work
  188. }
  189. for (int j = current; j < next; j++) {
  190. //wrap
  191. int idx = last_metric + 1 + j;
  192. while (idx >= frame_metrics.size()) {
  193. idx -= frame_metrics.size();
  194. }
  195. int area_count = frame_metrics[idx].areas.size();
  196. const Metric::Area *areas = frame_metrics[idx].areas.ptr();
  197. int prev_cpu = 0;
  198. int prev_gpu = 0;
  199. for (int k = 1; k < area_count; k++) {
  200. int ofs_cpu = int(areas[k].cpu_time * h / highest_cpu);
  201. ofs_cpu = CLAMP(ofs_cpu, 0, h - 1);
  202. Color color = selected_area == areas[k - 1].fullpath_cache ? Color(1, 1, 1, 1) : areas[k - 1].color_cache;
  203. for (int l = prev_cpu; l < ofs_cpu; l++) {
  204. column_cpu[h - l - 1] += color;
  205. }
  206. prev_cpu = ofs_cpu;
  207. int ofs_gpu = int(areas[k].gpu_time * h / highest_gpu);
  208. ofs_gpu = CLAMP(ofs_gpu, 0, h - 1);
  209. for (int l = prev_gpu; l < ofs_gpu; l++) {
  210. column_gpu[h - l - 1] += color;
  211. }
  212. prev_gpu = ofs_gpu;
  213. }
  214. }
  215. //plot CPU
  216. for (int j = 0; j < h; j++) {
  217. uint8_t r, g, b;
  218. if (column_cpu[j].a == 0) {
  219. r = Math::fast_ftoi(background_color.r * 255);
  220. g = Math::fast_ftoi(background_color.g * 255);
  221. b = Math::fast_ftoi(background_color.b * 255);
  222. } else {
  223. r = CLAMP((column_cpu[j].r / column_cpu[j].a) * 255.0, 0, 255);
  224. g = CLAMP((column_cpu[j].g / column_cpu[j].a) * 255.0, 0, 255);
  225. b = CLAMP((column_cpu[j].b / column_cpu[j].a) * 255.0, 0, 255);
  226. }
  227. int widx = (j * w + i) * 4;
  228. wr[widx + 0] = r;
  229. wr[widx + 1] = g;
  230. wr[widx + 2] = b;
  231. wr[widx + 3] = 255;
  232. }
  233. //plot GPU
  234. for (int j = 0; j < h; j++) {
  235. uint8_t r, g, b;
  236. if (column_gpu[j].a == 0) {
  237. r = Math::fast_ftoi(background_color.r * 255);
  238. g = Math::fast_ftoi(background_color.g * 255);
  239. b = Math::fast_ftoi(background_color.b * 255);
  240. } else {
  241. r = CLAMP((column_gpu[j].r / column_gpu[j].a) * 255.0, 0, 255);
  242. g = CLAMP((column_gpu[j].g / column_gpu[j].a) * 255.0, 0, 255);
  243. b = CLAMP((column_gpu[j].b / column_gpu[j].a) * 255.0, 0, 255);
  244. }
  245. int widx = (j * w + w / 2 + i) * 4;
  246. wr[widx + 0] = r;
  247. wr[widx + 1] = g;
  248. wr[widx + 2] = b;
  249. wr[widx + 3] = 255;
  250. }
  251. }
  252. }
  253. Ref<Image> img = Image::create_from_data(w, h, false, Image::FORMAT_RGBA8, graph_image);
  254. if (reset_texture) {
  255. if (graph_texture.is_null()) {
  256. graph_texture.instantiate();
  257. }
  258. graph_texture->set_image(img);
  259. }
  260. graph_texture->update(img);
  261. graph->set_texture(graph_texture);
  262. graph->queue_redraw();
  263. }
  264. void EditorVisualProfiler::_update_frame(bool p_focus_selected) {
  265. int cursor_metric = _get_cursor_index();
  266. Ref<Texture> track_icon = get_theme_icon(SNAME("TrackColor"), SNAME("EditorIcons"));
  267. ERR_FAIL_INDEX(cursor_metric, frame_metrics.size());
  268. updating_frame = true;
  269. variables->clear();
  270. TreeItem *root = variables->create_item();
  271. const Metric &m = frame_metrics[cursor_metric];
  272. List<TreeItem *> stack;
  273. List<TreeItem *> categories;
  274. TreeItem *ensure_selected = nullptr;
  275. for (int i = 1; i < m.areas.size() - 1; i++) {
  276. TreeItem *parent = stack.size() ? stack.back()->get() : root;
  277. String name = m.areas[i].name;
  278. float cpu_time = m.areas[i].cpu_time;
  279. float gpu_time = m.areas[i].gpu_time;
  280. if (i < m.areas.size() - 1) {
  281. cpu_time = m.areas[i + 1].cpu_time - cpu_time;
  282. gpu_time = m.areas[i + 1].gpu_time - gpu_time;
  283. }
  284. if (name.begins_with(">")) {
  285. TreeItem *category = variables->create_item(parent);
  286. stack.push_back(category);
  287. categories.push_back(category);
  288. name = name.substr(1, name.length());
  289. category->set_text(0, name);
  290. category->set_metadata(1, cpu_time);
  291. category->set_metadata(2, gpu_time);
  292. continue;
  293. }
  294. if (name.begins_with("<")) {
  295. stack.pop_back();
  296. continue;
  297. }
  298. TreeItem *category = variables->create_item(parent);
  299. for (TreeItem *E : stack) {
  300. float total_cpu = E->get_metadata(1);
  301. float total_gpu = E->get_metadata(2);
  302. total_cpu += cpu_time;
  303. total_gpu += gpu_time;
  304. E->set_metadata(1, total_cpu);
  305. E->set_metadata(2, total_gpu);
  306. }
  307. category->set_icon(0, track_icon);
  308. category->set_icon_modulate(0, m.areas[i].color_cache);
  309. category->set_selectable(0, true);
  310. category->set_metadata(0, m.areas[i].fullpath_cache);
  311. category->set_text(0, m.areas[i].name);
  312. category->set_text(1, _get_time_as_text(cpu_time));
  313. category->set_metadata(1, m.areas[i].cpu_time);
  314. category->set_text(2, _get_time_as_text(gpu_time));
  315. category->set_metadata(2, m.areas[i].gpu_time);
  316. if (selected_area == m.areas[i].fullpath_cache) {
  317. category->select(0);
  318. if (p_focus_selected) {
  319. ensure_selected = category;
  320. }
  321. }
  322. }
  323. for (TreeItem *E : categories) {
  324. float total_cpu = E->get_metadata(1);
  325. float total_gpu = E->get_metadata(2);
  326. E->set_text(1, _get_time_as_text(total_cpu));
  327. E->set_text(2, _get_time_as_text(total_gpu));
  328. }
  329. if (ensure_selected) {
  330. variables->ensure_cursor_is_visible();
  331. }
  332. updating_frame = false;
  333. }
  334. void EditorVisualProfiler::_activate_pressed() {
  335. if (activate->is_pressed()) {
  336. activate->set_icon(get_theme_icon(SNAME("Stop"), SNAME("EditorIcons")));
  337. activate->set_text(TTR("Stop"));
  338. _clear_pressed(); //always clear on start
  339. clear_button->set_disabled(false);
  340. } else {
  341. activate->set_icon(get_theme_icon(SNAME("Play"), SNAME("EditorIcons")));
  342. activate->set_text(TTR("Start"));
  343. }
  344. emit_signal(SNAME("enable_profiling"), activate->is_pressed());
  345. }
  346. void EditorVisualProfiler::_clear_pressed() {
  347. clear_button->set_disabled(true);
  348. clear();
  349. _update_plot();
  350. }
  351. void EditorVisualProfiler::_notification(int p_what) {
  352. switch (p_what) {
  353. case NOTIFICATION_ENTER_TREE:
  354. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  355. case NOTIFICATION_THEME_CHANGED:
  356. case NOTIFICATION_TRANSLATION_CHANGED: {
  357. if (is_layout_rtl()) {
  358. activate->set_icon(get_theme_icon(SNAME("PlayBackwards"), SNAME("EditorIcons")));
  359. } else {
  360. activate->set_icon(get_theme_icon(SNAME("Play"), SNAME("EditorIcons")));
  361. }
  362. clear_button->set_icon(get_theme_icon(SNAME("Clear"), SNAME("EditorIcons")));
  363. } break;
  364. }
  365. }
  366. void EditorVisualProfiler::_graph_tex_draw() {
  367. if (last_metric < 0) {
  368. return;
  369. }
  370. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  371. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  372. const Color color = get_theme_color(SNAME("font_color"), SNAME("Editor"));
  373. if (seeking) {
  374. int max_frames = frame_metrics.size();
  375. int frame = cursor_metric_edit->get_value() - (frame_metrics[last_metric].frame_number - max_frames + 1);
  376. if (frame < 0) {
  377. frame = 0;
  378. }
  379. int half_width = graph->get_size().x / 2;
  380. int cur_x = frame * half_width / max_frames;
  381. graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), color * Color(1, 1, 1));
  382. graph->draw_line(Vector2(cur_x + half_width, 0), Vector2(cur_x + half_width, graph->get_size().y), color * Color(1, 1, 1));
  383. }
  384. if (graph_height_cpu > 0) {
  385. int frame_y = graph->get_size().y - graph_limit * graph->get_size().y / graph_height_cpu - 1;
  386. int half_width = graph->get_size().x / 2;
  387. graph->draw_line(Vector2(0, frame_y), Vector2(half_width, frame_y), color * Color(1, 1, 1, 0.5));
  388. const String limit_str = String::num(graph_limit, 2) + " ms";
  389. graph->draw_string(font, Vector2(half_width - font->get_string_size(limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x - 2, frame_y - 2), limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1, 0.75));
  390. }
  391. if (graph_height_gpu > 0) {
  392. int frame_y = graph->get_size().y - graph_limit * graph->get_size().y / graph_height_gpu - 1;
  393. int half_width = graph->get_size().x / 2;
  394. graph->draw_line(Vector2(half_width, frame_y), Vector2(graph->get_size().x, frame_y), color * Color(1, 1, 1, 0.5));
  395. const String limit_str = String::num(graph_limit, 2) + " ms";
  396. graph->draw_string(font, Vector2(half_width * 2 - font->get_string_size(limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x - 2, frame_y - 2), limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1, 0.75));
  397. }
  398. graph->draw_string(font, Vector2(font->get_string_size("X", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x, font->get_ascent(font_size) + 2), "CPU:", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1));
  399. graph->draw_string(font, Vector2(font->get_string_size("X", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x + graph->get_size().width / 2, font->get_ascent(font_size) + 2), "GPU:", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1));
  400. }
  401. void EditorVisualProfiler::_graph_tex_mouse_exit() {
  402. hover_metric = -1;
  403. graph->queue_redraw();
  404. }
  405. void EditorVisualProfiler::_cursor_metric_changed(double) {
  406. if (updating_frame) {
  407. return;
  408. }
  409. graph->queue_redraw();
  410. _update_frame();
  411. }
  412. void EditorVisualProfiler::_graph_tex_input(const Ref<InputEvent> &p_ev) {
  413. if (last_metric < 0) {
  414. return;
  415. }
  416. Ref<InputEventMouse> me = p_ev;
  417. Ref<InputEventMouseButton> mb = p_ev;
  418. Ref<InputEventMouseMotion> mm = p_ev;
  419. if (
  420. (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) ||
  421. (mm.is_valid())) {
  422. int half_w = graph->get_size().width / 2;
  423. int x = me->get_position().x;
  424. if (x > half_w) {
  425. x -= half_w;
  426. }
  427. x = x * frame_metrics.size() / half_w;
  428. bool show_hover = x >= 0 && x < frame_metrics.size();
  429. if (x < 0) {
  430. x = 0;
  431. }
  432. if (x >= frame_metrics.size()) {
  433. x = frame_metrics.size() - 1;
  434. }
  435. int metric = frame_metrics.size() - x - 1;
  436. metric = last_metric - metric;
  437. while (metric < 0) {
  438. metric += frame_metrics.size();
  439. }
  440. if (show_hover) {
  441. hover_metric = metric;
  442. } else {
  443. hover_metric = -1;
  444. }
  445. if (mb.is_valid() || mm->get_button_mask().has_flag(MouseButtonMask::LEFT)) {
  446. //cursor_metric=x;
  447. updating_frame = true;
  448. //metric may be invalid, so look for closest metric that is valid, this makes snap feel better
  449. bool valid = false;
  450. for (int i = 0; i < frame_metrics.size(); i++) {
  451. if (frame_metrics[metric].valid) {
  452. valid = true;
  453. break;
  454. }
  455. metric++;
  456. if (metric >= frame_metrics.size()) {
  457. metric = 0;
  458. }
  459. }
  460. if (!valid) {
  461. return;
  462. }
  463. cursor_metric_edit->set_value(frame_metrics[metric].frame_number);
  464. updating_frame = false;
  465. if (activate->is_pressed()) {
  466. if (!seeking) {
  467. // Break request is not required, just stop profiling
  468. }
  469. }
  470. seeking = true;
  471. if (!frame_delay->is_processing()) {
  472. frame_delay->set_wait_time(0.1);
  473. frame_delay->start();
  474. }
  475. bool touched_cpu = me->get_position().x < graph->get_size().width * 0.5;
  476. const Metric::Area *areas = frame_metrics[metric].areas.ptr();
  477. int area_count = frame_metrics[metric].areas.size();
  478. float posy = (1.0 - (me->get_position().y / graph->get_size().height)) * (touched_cpu ? graph_height_cpu : graph_height_gpu);
  479. int last_valid = -1;
  480. bool found = false;
  481. for (int i = 0; i < area_count - 1; i++) {
  482. if (areas[i].name[0] != '<' && areas[i].name[0] != '>') {
  483. last_valid = i;
  484. }
  485. float h = touched_cpu ? areas[i + 1].cpu_time : areas[i + 1].gpu_time;
  486. if (h > posy) {
  487. found = true;
  488. break;
  489. }
  490. }
  491. StringName area_found;
  492. if (found && last_valid != -1) {
  493. area_found = areas[last_valid].fullpath_cache;
  494. }
  495. if (area_found != selected_area) {
  496. selected_area = area_found;
  497. _update_frame(true);
  498. _update_plot();
  499. }
  500. }
  501. graph->queue_redraw();
  502. }
  503. }
  504. int EditorVisualProfiler::_get_cursor_index() const {
  505. if (last_metric < 0) {
  506. return 0;
  507. }
  508. if (!frame_metrics[last_metric].valid) {
  509. return 0;
  510. }
  511. int diff = (frame_metrics[last_metric].frame_number - cursor_metric_edit->get_value());
  512. int idx = last_metric - diff;
  513. while (idx < 0) {
  514. idx += frame_metrics.size();
  515. }
  516. return idx;
  517. }
  518. void EditorVisualProfiler::disable_seeking() {
  519. seeking = false;
  520. graph->queue_redraw();
  521. }
  522. void EditorVisualProfiler::_combo_changed(int) {
  523. _update_frame();
  524. _update_plot();
  525. }
  526. void EditorVisualProfiler::_bind_methods() {
  527. ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));
  528. }
  529. void EditorVisualProfiler::_update_button_text() {
  530. if (activate->is_pressed()) {
  531. activate->set_icon(get_theme_icon(SNAME("Stop"), SNAME("EditorIcons")));
  532. activate->set_text(TTR("Stop"));
  533. } else {
  534. activate->set_icon(get_theme_icon(SNAME("Play"), SNAME("EditorIcons")));
  535. activate->set_text(TTR("Start"));
  536. }
  537. }
  538. void EditorVisualProfiler::set_enabled(bool p_enable) {
  539. activate->set_disabled(!p_enable);
  540. }
  541. void EditorVisualProfiler::set_pressed(bool p_pressed) {
  542. activate->set_pressed(p_pressed);
  543. _update_button_text();
  544. }
  545. bool EditorVisualProfiler::is_profiling() {
  546. return activate->is_pressed();
  547. }
  548. Vector<Vector<String>> EditorVisualProfiler::get_data_as_csv() const {
  549. Vector<Vector<String>> res;
  550. #if 0
  551. if (frame_metrics.is_empty()) {
  552. return res;
  553. }
  554. // signatures
  555. Vector<String> signatures;
  556. const Vector<EditorFrameProfiler::Metric::Category> &categories = frame_metrics[0].categories;
  557. for (int j = 0; j < categories.size(); j++) {
  558. const EditorFrameProfiler::Metric::Category &c = categories[j];
  559. signatures.push_back(c.signature);
  560. for (int k = 0; k < c.items.size(); k++) {
  561. signatures.push_back(c.items[k].signature);
  562. }
  563. }
  564. res.push_back(signatures);
  565. // values
  566. Vector<String> values;
  567. values.resize(signatures.size());
  568. int index = last_metric;
  569. for (int i = 0; i < frame_metrics.size(); i++) {
  570. ++index;
  571. if (index >= frame_metrics.size()) {
  572. index = 0;
  573. }
  574. if (!frame_metrics[index].valid) {
  575. continue;
  576. }
  577. int it = 0;
  578. const Vector<EditorFrameProfiler::Metric::Category> &frame_cat = frame_metrics[index].categories;
  579. for (int j = 0; j < frame_cat.size(); j++) {
  580. const EditorFrameProfiler::Metric::Category &c = frame_cat[j];
  581. values.write[it++] = String::num_real(c.total_time);
  582. for (int k = 0; k < c.items.size(); k++) {
  583. values.write[it++] = String::num_real(c.items[k].total);
  584. }
  585. }
  586. res.push_back(values);
  587. }
  588. #endif
  589. return res;
  590. }
  591. EditorVisualProfiler::EditorVisualProfiler() {
  592. HBoxContainer *hb = memnew(HBoxContainer);
  593. add_child(hb);
  594. activate = memnew(Button);
  595. activate->set_toggle_mode(true);
  596. activate->set_disabled(true);
  597. activate->set_text(TTR("Start"));
  598. activate->connect("pressed", callable_mp(this, &EditorVisualProfiler::_activate_pressed));
  599. hb->add_child(activate);
  600. clear_button = memnew(Button);
  601. clear_button->set_text(TTR("Clear"));
  602. clear_button->set_disabled(true);
  603. clear_button->connect("pressed", callable_mp(this, &EditorVisualProfiler::_clear_pressed));
  604. hb->add_child(clear_button);
  605. hb->add_child(memnew(Label(TTR("Measure:"))));
  606. display_mode = memnew(OptionButton);
  607. display_mode->add_item(TTR("Frame Time (ms)"));
  608. display_mode->add_item(TTR("Frame %"));
  609. display_mode->connect("item_selected", callable_mp(this, &EditorVisualProfiler::_combo_changed));
  610. hb->add_child(display_mode);
  611. frame_relative = memnew(CheckBox(TTR("Fit to Frame")));
  612. frame_relative->set_pressed(true);
  613. hb->add_child(frame_relative);
  614. frame_relative->connect("pressed", callable_mp(this, &EditorVisualProfiler::_update_plot));
  615. linked = memnew(CheckBox(TTR("Linked")));
  616. linked->set_pressed(true);
  617. hb->add_child(linked);
  618. linked->connect("pressed", callable_mp(this, &EditorVisualProfiler::_update_plot));
  619. hb->add_spacer();
  620. hb->add_child(memnew(Label(TTR("Frame #:"))));
  621. cursor_metric_edit = memnew(SpinBox);
  622. cursor_metric_edit->set_h_size_flags(SIZE_FILL);
  623. hb->add_child(cursor_metric_edit);
  624. cursor_metric_edit->connect("value_changed", callable_mp(this, &EditorVisualProfiler::_cursor_metric_changed));
  625. hb->add_theme_constant_override("separation", 8 * EDSCALE);
  626. h_split = memnew(HSplitContainer);
  627. add_child(h_split);
  628. h_split->set_v_size_flags(SIZE_EXPAND_FILL);
  629. variables = memnew(Tree);
  630. variables->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
  631. variables->set_hide_folding(true);
  632. h_split->add_child(variables);
  633. variables->set_hide_root(true);
  634. variables->set_columns(3);
  635. variables->set_column_titles_visible(true);
  636. variables->set_column_title(0, TTR("Name"));
  637. variables->set_column_expand(0, true);
  638. variables->set_column_clip_content(0, true);
  639. variables->set_column_custom_minimum_width(0, 60);
  640. variables->set_column_title(1, TTR("CPU"));
  641. variables->set_column_expand(1, false);
  642. variables->set_column_clip_content(1, true);
  643. variables->set_column_custom_minimum_width(1, 75 * EDSCALE);
  644. variables->set_column_title(2, TTR("GPU"));
  645. variables->set_column_expand(2, false);
  646. variables->set_column_clip_content(2, true);
  647. variables->set_column_custom_minimum_width(2, 75 * EDSCALE);
  648. variables->connect("cell_selected", callable_mp(this, &EditorVisualProfiler::_item_selected));
  649. graph = memnew(TextureRect);
  650. graph->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  651. graph->set_mouse_filter(MOUSE_FILTER_STOP);
  652. graph->connect("draw", callable_mp(this, &EditorVisualProfiler::_graph_tex_draw));
  653. graph->connect("gui_input", callable_mp(this, &EditorVisualProfiler::_graph_tex_input));
  654. graph->connect("mouse_exited", callable_mp(this, &EditorVisualProfiler::_graph_tex_mouse_exit));
  655. h_split->add_child(graph);
  656. graph->set_h_size_flags(SIZE_EXPAND_FILL);
  657. int metric_size = CLAMP(int(EDITOR_GET("debugger/profiler_frame_history_size")), 60, 10000);
  658. frame_metrics.resize(metric_size);
  659. frame_delay = memnew(Timer);
  660. frame_delay->set_wait_time(0.1);
  661. frame_delay->set_one_shot(true);
  662. add_child(frame_delay);
  663. frame_delay->connect("timeout", callable_mp(this, &EditorVisualProfiler::_update_frame).bind(false));
  664. plot_delay = memnew(Timer);
  665. plot_delay->set_wait_time(0.1);
  666. plot_delay->set_one_shot(true);
  667. add_child(plot_delay);
  668. plot_delay->connect("timeout", callable_mp(this, &EditorVisualProfiler::_update_plot));
  669. }