editor_performance_profiler.cpp 18 KB

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