editor_performance_profiler.cpp 17 KB

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