editor_visual_profiler.cpp 26 KB

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