editor_visual_profiler.cpp 27 KB

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