editor_performance_profiler.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**************************************************************************/
  2. /* editor_performance_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_performance_profiler.h"
  31. #include "editor/editor_string_names.h"
  32. #include "editor/inspector/editor_property_name_processor.h"
  33. #include "editor/settings/editor_settings.h"
  34. #include "editor/themes/editor_scale.h"
  35. #include "editor/themes/editor_theme_manager.h"
  36. #include "main/performance.h"
  37. EditorPerformanceProfiler::Monitor::Monitor(const String &p_name, const String &p_base, int p_frame_index, Performance::MonitorType p_type, TreeItem *p_item) {
  38. type = p_type;
  39. item = p_item;
  40. frame_index = p_frame_index;
  41. name = p_name;
  42. base = p_base;
  43. }
  44. void EditorPerformanceProfiler::Monitor::update_value(float p_value) {
  45. ERR_FAIL_NULL(item);
  46. String label = EditorPerformanceProfiler::_create_label(p_value, type);
  47. String tooltip = label;
  48. switch (type) {
  49. case Performance::MONITOR_TYPE_MEMORY: {
  50. tooltip = label;
  51. } break;
  52. case Performance::MONITOR_TYPE_TIME: {
  53. tooltip = label;
  54. } break;
  55. default: {
  56. tooltip += " " + item->get_text(0);
  57. } break;
  58. }
  59. item->set_text(1, label);
  60. item->set_tooltip_text(1, tooltip);
  61. if (p_value > max) {
  62. max = p_value;
  63. }
  64. }
  65. void EditorPerformanceProfiler::Monitor::reset() {
  66. history.clear();
  67. max = 0.0f;
  68. if (item) {
  69. item->set_text(1, "");
  70. item->set_tooltip_text(1, "");
  71. }
  72. }
  73. String EditorPerformanceProfiler::_create_label(float p_value, Performance::MonitorType p_type) {
  74. switch (p_type) {
  75. case Performance::MONITOR_TYPE_QUANTITY: {
  76. return TS->format_number(itos(p_value));
  77. }
  78. case Performance::MONITOR_TYPE_MEMORY: {
  79. return String::humanize_size(p_value);
  80. }
  81. case Performance::MONITOR_TYPE_TIME: {
  82. return TS->format_number(rtos(p_value * 1000).pad_decimals(2)) + " " + TTR("ms");
  83. }
  84. default: {
  85. return TS->format_number(rtos(p_value));
  86. }
  87. }
  88. }
  89. void EditorPerformanceProfiler::_monitor_select() {
  90. monitor_draw->queue_redraw();
  91. }
  92. void EditorPerformanceProfiler::_monitor_draw() {
  93. Vector<StringName> active;
  94. for (const KeyValue<StringName, Monitor> &E : monitors) {
  95. if (E.value.item->is_checked(0)) {
  96. active.push_back(E.key);
  97. }
  98. }
  99. if (active.is_empty()) {
  100. info_message->show();
  101. return;
  102. }
  103. info_message->hide();
  104. Ref<StyleBox> graph_style_box = get_theme_stylebox(CoreStringName(normal), SNAME("TextEdit"));
  105. Ref<Font> graph_font = get_theme_font(SceneStringName(font), SNAME("TextEdit"));
  106. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("TextEdit"));
  107. int columns = int(Math::ceil(Math::sqrt(float(active.size()))));
  108. int rows = int(Math::ceil(float(active.size()) / float(columns)));
  109. if (active.size() == 1) {
  110. rows = 1;
  111. }
  112. Size2i cell_size = Size2i(monitor_draw->get_size()) / Size2i(columns, rows);
  113. float spacing = float(POINT_SEPARATION) / float(columns);
  114. float value_multiplier = EditorThemeManager::is_dark_theme() ? 1.4f : 0.55f;
  115. float hue_shift = 1.0f / float(monitors.size());
  116. for (int i = 0; i < active.size(); i++) {
  117. Monitor &current = monitors[active[i]];
  118. Rect2i rect(Point2i(i % columns, i / columns) * cell_size + Point2i(MARGIN, MARGIN), cell_size - Point2i(MARGIN, MARGIN) * 2);
  119. monitor_draw->draw_style_box(graph_style_box, rect);
  120. rect.position += graph_style_box->get_offset();
  121. rect.size -= graph_style_box->get_minimum_size();
  122. Color draw_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  123. draw_color.set_hsv(Math::fmod(hue_shift * float(current.frame_index), 0.9f), draw_color.get_s() * 0.9f, draw_color.get_v() * value_multiplier, 0.6f);
  124. monitor_draw->draw_string(graph_font, rect.position + Point2(0, graph_font->get_ascent(font_size)), current.item->get_text(0), HORIZONTAL_ALIGNMENT_LEFT, rect.size.x, font_size, draw_color);
  125. draw_color.a = 0.9f;
  126. float value_position = rect.size.width - graph_font->get_string_size(current.item->get_text(1), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;
  127. if (value_position < 0) {
  128. value_position = 0;
  129. }
  130. monitor_draw->draw_string(graph_font, rect.position + Point2(value_position, graph_font->get_ascent(font_size)), current.item->get_text(1), HORIZONTAL_ALIGNMENT_LEFT, rect.size.x, font_size, draw_color);
  131. rect.position.y += graph_font->get_height(font_size);
  132. rect.size.height -= graph_font->get_height(font_size);
  133. int line_count = rect.size.height / (graph_font->get_height(font_size) * 2);
  134. if (line_count > 5) {
  135. line_count = 5;
  136. }
  137. if (line_count > 0) {
  138. Color horizontal_line_color;
  139. horizontal_line_color.set_hsv(draw_color.get_h(), draw_color.get_s() * 0.5f, draw_color.get_v() * 0.5f, 0.3f);
  140. monitor_draw->draw_line(rect.position, rect.position + Vector2(rect.size.width, 0), horizontal_line_color, Math::round(EDSCALE));
  141. monitor_draw->draw_string(graph_font, rect.position + Vector2(0, graph_font->get_ascent(font_size)), _create_label(current.max, current.type), HORIZONTAL_ALIGNMENT_LEFT, rect.size.width, font_size, horizontal_line_color);
  142. for (int j = 0; j < line_count; j++) {
  143. Vector2 y_offset = Vector2(0, rect.size.height * (1.0f - float(j) / float(line_count)));
  144. monitor_draw->draw_line(rect.position + y_offset, rect.position + Vector2(rect.size.width, 0) + y_offset, horizontal_line_color, Math::round(EDSCALE));
  145. monitor_draw->draw_string(graph_font, rect.position - Vector2(0, graph_font->get_descent(font_size)) + y_offset, _create_label(current.max * float(j) / float(line_count), current.type), HORIZONTAL_ALIGNMENT_LEFT, rect.size.width, font_size, horizontal_line_color);
  146. }
  147. }
  148. float from = rect.size.width;
  149. float prev = -1.0f;
  150. int count = 0;
  151. List<float>::Element *e = current.history.front();
  152. while (from >= 0 && e) {
  153. float m = current.max;
  154. float h2 = 0;
  155. if (m != 0) {
  156. h2 = (e->get() / m);
  157. }
  158. h2 = (1.0f - h2) * float(rect.size.y);
  159. if (e != current.history.front()) {
  160. monitor_draw->draw_line(rect.position + Point2(from, h2), rect.position + Point2(from + spacing, prev), draw_color, Math::round(EDSCALE));
  161. }
  162. if (marker_key == active[i] && count == marker_frame) {
  163. Color line_color;
  164. line_color.set_hsv(draw_color.get_h(), draw_color.get_s() * 0.8f, draw_color.get_v(), 0.5f);
  165. monitor_draw->draw_line(rect.position + Point2(from, 0), rect.position + Point2(from, rect.size.y), line_color, Math::round(EDSCALE));
  166. String label = _create_label(e->get(), current.type);
  167. Size2 size = graph_font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
  168. Vector2 text_top_left_position = Vector2(from, h2) - (size + Vector2(MARKER_MARGIN, MARKER_MARGIN));
  169. if (text_top_left_position.x < 0) {
  170. text_top_left_position.x = from + MARKER_MARGIN;
  171. }
  172. if (text_top_left_position.y < 0) {
  173. text_top_left_position.y = h2 + MARKER_MARGIN;
  174. }
  175. monitor_draw->draw_string(graph_font, rect.position + text_top_left_position + Point2(0, graph_font->get_ascent(font_size)), label, HORIZONTAL_ALIGNMENT_LEFT, rect.size.x, font_size, line_color);
  176. }
  177. prev = h2;
  178. e = e->next();
  179. from -= spacing;
  180. count++;
  181. }
  182. }
  183. }
  184. void EditorPerformanceProfiler::_build_monitor_tree() {
  185. HashSet<StringName> monitor_checked;
  186. for (KeyValue<StringName, Monitor> &E : monitors) {
  187. if (E.value.item && E.value.item->is_checked(0)) {
  188. monitor_checked.insert(E.key);
  189. }
  190. }
  191. base_map.clear();
  192. monitor_tree->get_root()->clear_children();
  193. for (KeyValue<StringName, Monitor> &E : monitors) {
  194. TreeItem *base = _get_monitor_base(E.value.base);
  195. TreeItem *item = _create_monitor_item(E.value.name, base);
  196. item->set_checked(0, monitor_checked.has(E.key));
  197. E.value.item = item;
  198. if (!E.value.history.is_empty()) {
  199. E.value.update_value(E.value.history.front()->get());
  200. }
  201. }
  202. }
  203. TreeItem *EditorPerformanceProfiler::_get_monitor_base(const StringName &p_base_name) {
  204. if (base_map.has(p_base_name)) {
  205. return base_map[p_base_name];
  206. }
  207. TreeItem *base = monitor_tree->create_item(monitor_tree->get_root());
  208. base->set_text(0, EditorPropertyNameProcessor::get_singleton()->process_name(p_base_name, EditorPropertyNameProcessor::get_settings_style()));
  209. base->set_editable(0, false);
  210. base->set_selectable(0, false);
  211. base->set_expand_right(0, true);
  212. if (is_inside_tree()) {
  213. base->set_custom_font(0, get_theme_font(SNAME("bold"), EditorStringName(EditorFonts)));
  214. }
  215. base_map.insert(p_base_name, base);
  216. return base;
  217. }
  218. TreeItem *EditorPerformanceProfiler::_create_monitor_item(const StringName &p_monitor_name, TreeItem *p_base) {
  219. TreeItem *item = monitor_tree->create_item(p_base);
  220. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  221. item->set_editable(0, true);
  222. item->set_selectable(0, false);
  223. item->set_selectable(1, false);
  224. item->set_text(0, EditorPropertyNameProcessor::get_singleton()->process_name(p_monitor_name, EditorPropertyNameProcessor::get_settings_style()));
  225. return item;
  226. }
  227. void EditorPerformanceProfiler::_marker_input(const Ref<InputEvent> &p_event) {
  228. Ref<InputEventMouseButton> mb = p_event;
  229. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  230. Vector<StringName> active;
  231. for (KeyValue<StringName, Monitor> &E : monitors) {
  232. if (E.value.item->is_checked(0)) {
  233. active.push_back(E.key);
  234. }
  235. }
  236. if (active.size() > 0) {
  237. int columns = int(Math::ceil(Math::sqrt(float(active.size()))));
  238. int rows = int(Math::ceil(float(active.size()) / float(columns)));
  239. if (active.size() == 1) {
  240. rows = 1;
  241. }
  242. Size2i cell_size = Size2i(monitor_draw->get_size()) / Size2i(columns, rows);
  243. Vector2i index = mb->get_position() / cell_size;
  244. Rect2i rect(index * cell_size + Point2i(MARGIN, MARGIN), cell_size - Point2i(MARGIN, MARGIN) * 2);
  245. if (rect.has_point(mb->get_position())) {
  246. if (index.x + index.y * columns < active.size()) {
  247. marker_key = active[index.x + index.y * columns];
  248. } else {
  249. marker_key = "";
  250. }
  251. Ref<StyleBox> graph_style_box = get_theme_stylebox(CoreStringName(normal), SNAME("TextEdit"));
  252. rect.position += graph_style_box->get_offset();
  253. rect.size -= graph_style_box->get_minimum_size();
  254. Vector2 point = mb->get_position() - rect.position;
  255. if (point.x >= rect.size.x) {
  256. marker_frame = 0;
  257. } else {
  258. int point_sep = 5;
  259. float spacing = float(point_sep) / float(columns);
  260. marker_frame = (rect.size.x - point.x) / spacing;
  261. }
  262. monitor_draw->queue_redraw();
  263. return;
  264. }
  265. }
  266. marker_key = "";
  267. monitor_draw->queue_redraw();
  268. }
  269. }
  270. void EditorPerformanceProfiler::reset() {
  271. HashMap<StringName, Monitor>::Iterator E = monitors.begin();
  272. while (E != monitors.end()) {
  273. HashMap<StringName, Monitor>::Iterator N = E;
  274. ++N;
  275. if (String(E->key).begins_with("custom:")) {
  276. monitors.remove(E);
  277. } else {
  278. E->value.reset();
  279. }
  280. E = N;
  281. }
  282. _build_monitor_tree();
  283. marker_key = "";
  284. marker_frame = 0;
  285. monitor_draw->queue_redraw();
  286. }
  287. void EditorPerformanceProfiler::update_monitors(const Vector<StringName> &p_names) {
  288. HashMap<StringName, int> names;
  289. for (int i = 0; i < p_names.size(); i++) {
  290. names.insert("custom:" + p_names[i], Performance::MONITOR_MAX + i);
  291. }
  292. {
  293. HashMap<StringName, Monitor>::Iterator E = monitors.begin();
  294. while (E != monitors.end()) {
  295. HashMap<StringName, Monitor>::Iterator N = E;
  296. ++N;
  297. if (String(E->key).begins_with("custom:")) {
  298. if (!names.has(E->key)) {
  299. monitors.remove(E);
  300. } else {
  301. E->value.frame_index = names[E->key];
  302. names.erase(E->key);
  303. }
  304. }
  305. E = N;
  306. }
  307. }
  308. for (const KeyValue<StringName, int> &E : names) {
  309. String name = String(E.key).replace_first("custom:", "");
  310. String base = "Custom";
  311. if (name.get_slice_count("/") == 2) {
  312. base = name.get_slicec('/', 0);
  313. name = name.get_slicec('/', 1);
  314. }
  315. monitors.insert(E.key, Monitor(name, base, E.value, Performance::MONITOR_TYPE_QUANTITY, nullptr));
  316. }
  317. _build_monitor_tree();
  318. }
  319. void EditorPerformanceProfiler::add_profile_frame(const Vector<float> &p_values) {
  320. for (KeyValue<StringName, Monitor> &E : monitors) {
  321. float value = 0.0f;
  322. if (E.value.frame_index >= 0 && E.value.frame_index < p_values.size()) {
  323. value = p_values[E.value.frame_index];
  324. }
  325. E.value.history.push_front(value);
  326. E.value.update_value(value);
  327. }
  328. marker_frame++;
  329. monitor_draw->queue_redraw();
  330. }
  331. List<float> *EditorPerformanceProfiler::get_monitor_data(const StringName &p_name) {
  332. if (monitors.has(p_name)) {
  333. return &monitors[p_name].history;
  334. }
  335. return nullptr;
  336. }
  337. void EditorPerformanceProfiler::_notification(int p_what) {
  338. switch (p_what) {
  339. case NOTIFICATION_THEME_CHANGED: {
  340. for (KeyValue<StringName, TreeItem *> &E : base_map) {
  341. E.value->set_custom_font(0, get_theme_font(SNAME("bold"), EditorStringName(EditorFonts)));
  342. }
  343. } break;
  344. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  345. if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/localize_settings")) {
  346. _build_monitor_tree();
  347. }
  348. } break;
  349. }
  350. }
  351. EditorPerformanceProfiler::EditorPerformanceProfiler() {
  352. set_name(TTR("Monitors"));
  353. set_split_offset(340 * EDSCALE);
  354. monitor_tree = memnew(Tree);
  355. monitor_tree->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
  356. monitor_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  357. monitor_tree->set_columns(2);
  358. monitor_tree->set_column_title(0, TTR("Monitor"));
  359. monitor_tree->set_column_expand(0, true);
  360. monitor_tree->set_column_title(1, TTR("Value"));
  361. monitor_tree->set_column_custom_minimum_width(1, 100 * EDSCALE);
  362. monitor_tree->set_column_expand(1, false);
  363. monitor_tree->set_column_titles_visible(true);
  364. monitor_tree->connect("item_edited", callable_mp(this, &EditorPerformanceProfiler::_monitor_select));
  365. monitor_tree->create_item();
  366. monitor_tree->set_hide_root(true);
  367. monitor_tree->set_theme_type_variation("TreeSecondary");
  368. add_child(monitor_tree);
  369. monitor_draw = memnew(Control);
  370. monitor_draw->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
  371. monitor_draw->set_clip_contents(true);
  372. monitor_draw->connect(SceneStringName(draw), callable_mp(this, &EditorPerformanceProfiler::_monitor_draw));
  373. monitor_draw->connect(SceneStringName(gui_input), callable_mp(this, &EditorPerformanceProfiler::_marker_input));
  374. add_child(monitor_draw);
  375. info_message = memnew(Label);
  376. info_message->set_focus_mode(FOCUS_ACCESSIBILITY);
  377. info_message->set_text(TTR("Pick one or more items from the list to display the graph."));
  378. info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  379. info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  380. info_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  381. info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  382. info_message->set_anchors_and_offsets_preset(PRESET_FULL_RECT, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  383. monitor_draw->add_child(info_message);
  384. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  385. const Performance::Monitor monitor = Performance::Monitor(i);
  386. const String path = Performance::get_singleton()->get_monitor_name(monitor);
  387. const String base = path.get_slicec('/', 0);
  388. const String name = path.get_slicec('/', 1);
  389. monitors.insert(path, Monitor(name, base, i, Performance::get_singleton()->get_monitor_type(monitor), nullptr));
  390. }
  391. _build_monitor_tree();
  392. }