editor_visual_profiler.cpp 26 KB

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