editor_visual_profiler.cpp 27 KB

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