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