editor_performance_profiler.cpp 16 KB

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