editor_performance_profiler.cpp 17 KB

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