Debian Codegraph @

main/performance.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* performance.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 "performance.h"
32 #include "performance.compat.inc"
33
34 #include "core/os/os.h"
35 #include "core/variant/typed_array.h"
36 #include "scene/main/node.h"
37 #include "scene/main/scene_tree.h"
38 #include "servers/audio/audio_server.h"
39 #include "servers/rendering/rendering_server.h"
40
41 #ifndef NAVIGATION_2D_DISABLED
42 #include "servers/navigation_2d/navigation_server_2d.h"
43 #endif // NAVIGATION_2D_DISABLED
44
45 #ifndef NAVIGATION_3D_DISABLED
46 #include "servers/navigation_3d/navigation_server_3d.h"
47 #endif // NAVIGATION_3D_DISABLED
48
49 #ifndef PHYSICS_2D_DISABLED
50 #include "servers/physics_2d/physics_server_2d.h"
51 #endif // PHYSICS_2D_DISABLED
52
53 #ifndef PHYSICS_3D_DISABLED
54 #include "servers/physics_3d/physics_server_3d.h"
55 #endif // PHYSICS_3D_DISABLED
56
57 Performance *Performance::singleton = nullptr;
58
59 void Performance::_bind_methods() {
60 ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor);
61 ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments", "type"), &Performance::add_custom_monitor, DEFVAL(Array()), DEFVAL(MONITOR_TYPE_QUANTITY));
62 ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor);
63 ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor);
64 ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor);
65 ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time);
66 ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names);
67 ClassDB::bind_method(D_METHOD("get_custom_monitor_types"), &Performance::get_custom_monitor_types);
68
69 BIND_ENUM_CONSTANT(TIME_FPS);
70 BIND_ENUM_CONSTANT(TIME_PROCESS);
71 BIND_ENUM_CONSTANT(TIME_PHYSICS_PROCESS);
72 BIND_ENUM_CONSTANT(TIME_NAVIGATION_PROCESS);
73 BIND_ENUM_CONSTANT(MEMORY_STATIC);
74 BIND_ENUM_CONSTANT(MEMORY_STATIC_MAX);
75 BIND_ENUM_CONSTANT(MEMORY_MESSAGE_BUFFER_MAX);
76 BIND_ENUM_CONSTANT(OBJECT_COUNT);
77 BIND_ENUM_CONSTANT(OBJECT_RESOURCE_COUNT);
78 BIND_ENUM_CONSTANT(OBJECT_NODE_COUNT);
79 BIND_ENUM_CONSTANT(OBJECT_ORPHAN_NODE_COUNT);
80 BIND_ENUM_CONSTANT(RENDER_TOTAL_OBJECTS_IN_FRAME);
81 BIND_ENUM_CONSTANT(RENDER_TOTAL_PRIMITIVES_IN_FRAME);
82 BIND_ENUM_CONSTANT(RENDER_TOTAL_DRAW_CALLS_IN_FRAME);
83 BIND_ENUM_CONSTANT(RENDER_VIDEO_MEM_USED);
84 BIND_ENUM_CONSTANT(RENDER_TEXTURE_MEM_USED);
85 BIND_ENUM_CONSTANT(RENDER_BUFFER_MEM_USED);
86 BIND_ENUM_CONSTANT(PHYSICS_2D_ACTIVE_OBJECTS);
87 BIND_ENUM_CONSTANT(PHYSICS_2D_COLLISION_PAIRS);
88 BIND_ENUM_CONSTANT(PHYSICS_2D_ISLAND_COUNT);
89 #ifndef _3D_DISABLED
90 BIND_ENUM_CONSTANT(PHYSICS_3D_ACTIVE_OBJECTS);
91 BIND_ENUM_CONSTANT(PHYSICS_3D_COLLISION_PAIRS);
92 BIND_ENUM_CONSTANT(PHYSICS_3D_ISLAND_COUNT);
93 #endif // _3D_DISABLED
94 BIND_ENUM_CONSTANT(AUDIO_OUTPUT_LATENCY);
95 #if !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
96 BIND_ENUM_CONSTANT(NAVIGATION_ACTIVE_MAPS);
97 BIND_ENUM_CONSTANT(NAVIGATION_REGION_COUNT);
98 BIND_ENUM_CONSTANT(NAVIGATION_AGENT_COUNT);
99 BIND_ENUM_CONSTANT(NAVIGATION_LINK_COUNT);
100 BIND_ENUM_CONSTANT(NAVIGATION_POLYGON_COUNT);
101 BIND_ENUM_CONSTANT(NAVIGATION_EDGE_COUNT);
102 BIND_ENUM_CONSTANT(NAVIGATION_EDGE_MERGE_COUNT);
103 BIND_ENUM_CONSTANT(NAVIGATION_EDGE_CONNECTION_COUNT);
104 BIND_ENUM_CONSTANT(NAVIGATION_EDGE_FREE_COUNT);
105 BIND_ENUM_CONSTANT(NAVIGATION_OBSTACLE_COUNT);
106 #endif // !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
107 BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_CANVAS);
108 BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_MESH);
109 BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_SURFACE);
110 BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_DRAW);
111 BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_SPECIALIZATION);
112 #ifndef NAVIGATION_2D_DISABLED
113 BIND_ENUM_CONSTANT(NAVIGATION_2D_ACTIVE_MAPS);
114 BIND_ENUM_CONSTANT(NAVIGATION_2D_REGION_COUNT);
115 BIND_ENUM_CONSTANT(NAVIGATION_2D_AGENT_COUNT);
116 BIND_ENUM_CONSTANT(NAVIGATION_2D_LINK_COUNT);
117 BIND_ENUM_CONSTANT(NAVIGATION_2D_POLYGON_COUNT);
118 BIND_ENUM_CONSTANT(NAVIGATION_2D_EDGE_COUNT);
119 BIND_ENUM_CONSTANT(NAVIGATION_2D_EDGE_MERGE_COUNT);
120 BIND_ENUM_CONSTANT(NAVIGATION_2D_EDGE_CONNECTION_COUNT);
121 BIND_ENUM_CONSTANT(NAVIGATION_2D_EDGE_FREE_COUNT);
122 BIND_ENUM_CONSTANT(NAVIGATION_2D_OBSTACLE_COUNT);
123 #endif // NAVIGATION_2D_DISABLED
124 #ifndef NAVIGATION_3D_DISABLED
125 BIND_ENUM_CONSTANT(NAVIGATION_3D_ACTIVE_MAPS);
126 BIND_ENUM_CONSTANT(NAVIGATION_3D_REGION_COUNT);
127 BIND_ENUM_CONSTANT(NAVIGATION_3D_AGENT_COUNT);
128 BIND_ENUM_CONSTANT(NAVIGATION_3D_LINK_COUNT);
129 BIND_ENUM_CONSTANT(NAVIGATION_3D_POLYGON_COUNT);
130 BIND_ENUM_CONSTANT(NAVIGATION_3D_EDGE_COUNT);
131 BIND_ENUM_CONSTANT(NAVIGATION_3D_EDGE_MERGE_COUNT);
132 BIND_ENUM_CONSTANT(NAVIGATION_3D_EDGE_CONNECTION_COUNT);
133 BIND_ENUM_CONSTANT(NAVIGATION_3D_EDGE_FREE_COUNT);
134 BIND_ENUM_CONSTANT(NAVIGATION_3D_OBSTACLE_COUNT);
135 #endif // NAVIGATION_3D_DISABLED
136 BIND_ENUM_CONSTANT(MONITOR_MAX);
137
138 BIND_ENUM_CONSTANT(MONITOR_TYPE_QUANTITY);
139 BIND_ENUM_CONSTANT(MONITOR_TYPE_MEMORY);
140 BIND_ENUM_CONSTANT(MONITOR_TYPE_TIME);
141 BIND_ENUM_CONSTANT(MONITOR_TYPE_PERCENTAGE);
142 }
143
144 int Performance::_get_node_count() const {
145 MainLoop *ml = OS::get_singleton()->get_main_loop();
146 SceneTree *sml = Object::cast_to<SceneTree>(ml);
147 if (!sml) {
148 return 0;
149 }
150 return sml->get_node_count();
151 }
152
153 int Performance::_get_orphan_node_count() const {
154 #ifdef DEBUG_ENABLED
155 const int total_node_count = Node::total_node_count.get();
156 const int orphan_node_count = total_node_count - _get_node_count();
157 return orphan_node_count;
158 #else
159 return 0;
160 #endif
161 }
162
163 String Performance::get_monitor_name(Monitor p_monitor) const {
164 ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, String());
165 static const char *names[MONITOR_MAX] = {
166 PNAME("time/fps"),
167 PNAME("time/process"),
168 PNAME("time/physics_process"),
169 PNAME("time/navigation_process"),
170 PNAME("memory/static"),
171 PNAME("memory/static_max"),
172 PNAME("memory/msg_buf_max"),
173 PNAME("object/objects"),
174 PNAME("object/resources"),
175 PNAME("object/nodes"),
176 PNAME("object/orphan_nodes"),
177 PNAME("raster/total_objects_drawn"),
178 PNAME("raster/total_primitives_drawn"),
179 PNAME("raster/total_draw_calls"),
180 PNAME("video/video_mem"),
181 PNAME("video/texture_mem"),
182 PNAME("video/buffer_mem"),
183 PNAME("physics_2d/active_objects"),
184 PNAME("physics_2d/collision_pairs"),
185 PNAME("physics_2d/islands"),
186 PNAME("physics_3d/active_objects"),
187 PNAME("physics_3d/collision_pairs"),
188 PNAME("physics_3d/islands"),
189 PNAME("audio/driver/output_latency"),
190 #if !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
191 PNAME("navigation/active_maps"),
192 PNAME("navigation/regions"),
193 PNAME("navigation/agents"),
194 PNAME("navigation/links"),
195 PNAME("navigation/polygons"),
196 PNAME("navigation/edges"),
197 PNAME("navigation/edges_merged"),
198 PNAME("navigation/edges_connected"),
199 PNAME("navigation/edges_free"),
200 PNAME("navigation/obstacles"),
201 #endif // !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
202 PNAME("pipeline/compilations_canvas"),
203 PNAME("pipeline/compilations_mesh"),
204 PNAME("pipeline/compilations_surface"),
205 PNAME("pipeline/compilations_draw"),
206 PNAME("pipeline/compilations_specialization"),
207 #ifndef NAVIGATION_2D_DISABLED
208 PNAME("navigation_2d/active_maps"),
209 PNAME("navigation_2d/regions"),
210 PNAME("navigation_2d/agents"),
211 PNAME("navigation_2d/links"),
212 PNAME("navigation_2d/polygons"),
213 PNAME("navigation_2d/edges"),
214 PNAME("navigation_2d/edges_merged"),
215 PNAME("navigation_2d/edges_connected"),
216 PNAME("navigation_2d/edges_free"),
217 PNAME("navigation_2d/obstacles"),
218 #endif // NAVIGATION_2D_DISABLED
219 #ifndef NAVIGATION_3D_DISABLED
220 PNAME("navigation_3d/active_maps"),
221 PNAME("navigation_3d/regions"),
222 PNAME("navigation_3d/agents"),
223 PNAME("navigation_3d/links"),
224 PNAME("navigation_3d/polygons"),
225 PNAME("navigation_3d/edges"),
226 PNAME("navigation_3d/edges_merged"),
227 PNAME("navigation_3d/edges_connected"),
228 PNAME("navigation_3d/edges_free"),
229 PNAME("navigation_3d/obstacles"),
230 #endif // NAVIGATION_3D_DISABLED
231 };
232 static_assert(std_size(names) == MONITOR_MAX);
233
234 return names[p_monitor];
235 }
236
237 double Performance::get_monitor(Monitor p_monitor) const {
238 int info = 0;
239
240 switch (p_monitor) {
241 case TIME_FPS:
242 return Engine::get_singleton()->get_frames_per_second();
243 case TIME_PROCESS:
244 return _process_time;
245 case TIME_PHYSICS_PROCESS:
246 return _physics_process_time;
247 case TIME_NAVIGATION_PROCESS:
248 return _navigation_process_time;
249 case MEMORY_STATIC:
250 return Memory::get_mem_usage();
251 case MEMORY_STATIC_MAX:
252 return Memory::get_mem_max_usage();
253 case MEMORY_MESSAGE_BUFFER_MAX:
254 return MessageQueue::get_singleton()->get_max_buffer_usage();
255 case OBJECT_COUNT:
256 return ObjectDB::get_object_count();
257 case OBJECT_RESOURCE_COUNT:
258 return ResourceCache::get_cached_resource_count();
259 case OBJECT_NODE_COUNT:
260 return _get_node_count();
261 case OBJECT_ORPHAN_NODE_COUNT:
262 return _get_orphan_node_count();
263 case RENDER_TOTAL_OBJECTS_IN_FRAME:
264 return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_OBJECTS_IN_FRAME);
265 case RENDER_TOTAL_PRIMITIVES_IN_FRAME:
266 return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_PRIMITIVES_IN_FRAME);
267 case RENDER_TOTAL_DRAW_CALLS_IN_FRAME:
268 return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_DRAW_CALLS_IN_FRAME);
269 case RENDER_VIDEO_MEM_USED:
270 return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_VIDEO_MEM_USED);
271 case RENDER_TEXTURE_MEM_USED:
272 return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TEXTURE_MEM_USED);
273 case RENDER_BUFFER_MEM_USED:
274 return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_BUFFER_MEM_USED);
275 case PIPELINE_COMPILATIONS_CANVAS:
276 return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_CANVAS);
277 case PIPELINE_COMPILATIONS_MESH:
278 return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_MESH);
279 case PIPELINE_COMPILATIONS_SURFACE:
280 return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_SURFACE);
281 case PIPELINE_COMPILATIONS_DRAW:
282 return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_DRAW);
283 case PIPELINE_COMPILATIONS_SPECIALIZATION:
284 return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_SPECIALIZATION);
285 #ifndef PHYSICS_2D_DISABLED
286 case PHYSICS_2D_ACTIVE_OBJECTS:
287 return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ACTIVE_OBJECTS);
288 case PHYSICS_2D_COLLISION_PAIRS:
289 return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_COLLISION_PAIRS);
290 case PHYSICS_2D_ISLAND_COUNT:
291 return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ISLAND_COUNT);
292 #else
293 case PHYSICS_2D_ACTIVE_OBJECTS:
294 return 0;
295 case PHYSICS_2D_COLLISION_PAIRS:
296 return 0;
297 case PHYSICS_2D_ISLAND_COUNT:
298 return 0;
299 #endif // PHYSICS_2D_DISABLED
300 #ifndef PHYSICS_3D_DISABLED
301 case PHYSICS_3D_ACTIVE_OBJECTS:
302 return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ACTIVE_OBJECTS);
303 case PHYSICS_3D_COLLISION_PAIRS:
304 return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_COLLISION_PAIRS);
305 case PHYSICS_3D_ISLAND_COUNT:
306 return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ISLAND_COUNT);
307 #else
308 case PHYSICS_3D_ACTIVE_OBJECTS:
309 return 0;
310 case PHYSICS_3D_COLLISION_PAIRS:
311 return 0;
312 case PHYSICS_3D_ISLAND_COUNT:
313 return 0;
314 #endif // PHYSICS_3D_DISABLED
315
316 case AUDIO_OUTPUT_LATENCY:
317 return AudioServer::get_singleton()->get_output_latency();
318
319 // Deprecated, use the 2D/3D specific ones instead.
320 case NAVIGATION_ACTIVE_MAPS:
321 #ifndef NAVIGATION_2D_DISABLED
322 info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_ACTIVE_MAPS);
323 #endif // NAVIGATION_2D_DISABLED
324 #ifndef NAVIGATION_3D_DISABLED
325 info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS);
326 #endif // NAVIGATION_3D_DISABLED
327 return info;
328
329 case NAVIGATION_REGION_COUNT:
330 #ifndef NAVIGATION_2D_DISABLED
331 info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_REGION_COUNT);
332 #endif // NAVIGATION_2D_DISABLED
333 #ifndef NAVIGATION_3D_DISABLED
334 info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_REGION_COUNT);
335 #endif // NAVIGATION_3D_DISABLED
336 return info;
337
338 case NAVIGATION_AGENT_COUNT:
339 #ifndef NAVIGATION_2D_DISABLED
340 info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_AGENT_COUNT);
341 #endif // NAVIGATION_2D_DISABLED
342 #ifndef NAVIGATION_3D_DISABLED
343 info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_AGENT_COUNT);
344 #endif // NAVIGATION_3D_DISABLED
345 return info;
346
347 case NAVIGATION_LINK_COUNT:
348 #ifndef NAVIGATION_2D_DISABLED
349 info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_LINK_COUNT);
350 #endif // NAVIGATION_2D_DISABLED
351 #ifndef NAVIGATION_3D_DISABLED
352 info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_LINK_COUNT);
353 #endif // NAVIGATION_3D_DISABLED
354 return info;
355
356 case NAVIGATION_POLYGON_COUNT:
357 #ifndef NAVIGATION_2D_DISABLED
358 info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_POLYGON_COUNT);
359 #endif // NAVIGATION_2D_DISABLED
360 #ifndef NAVIGATION_3D_DISABLED
361 info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_POLYGON_COUNT);
362 #endif // NAVIGATION_3D_DISABLED
363 return info;
364
365 case NAVIGATION_EDGE_COUNT:
366 #ifndef NAVIGATION_2D_DISABLED
367 info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_COUNT);
368 #endif // NAVIGATION_2D_DISABLED
369 #ifndef NAVIGATION_3D_DISABLED
370 info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_COUNT);
371 #endif // NAVIGATION_3D_DISABLED
372 return info;
373
374 case NAVIGATION_EDGE_MERGE_COUNT:
375 #ifndef NAVIGATION_2D_DISABLED
376 info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_MERGE_COUNT);
377 #endif // NAVIGATION_2D_DISABLED
378 #ifndef NAVIGATION_3D_DISABLED
379 info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_MERGE_COUNT);
380 #endif // NAVIGATION_3D_DISABLED
381 return info;
382
383 case NAVIGATION_EDGE_CONNECTION_COUNT:
384 #ifndef NAVIGATION_2D_DISABLED
385 info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_CONNECTION_COUNT);
386 #endif // NAVIGATION_2D_DISABLED
387 #ifndef NAVIGATION_3D_DISABLED
388 info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_CONNECTION_COUNT);
389 #endif // NAVIGATION_3D_DISABLED
390 return info;
391
392 case NAVIGATION_EDGE_FREE_COUNT:
393 #ifndef NAVIGATION_2D_DISABLED
394 info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_FREE_COUNT);
395 #endif // NAVIGATION_2D_DISABLED
396 #ifndef NAVIGATION_3D_DISABLED
397 info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_FREE_COUNT);
398 #endif // NAVIGATION_3D_DISABLED
399 return info;
400
401 case NAVIGATION_OBSTACLE_COUNT:
402 #ifndef NAVIGATION_2D_DISABLED
403 info = NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_OBSTACLE_COUNT);
404 #endif // NAVIGATION_2D_DISABLED
405 #ifndef NAVIGATION_3D_DISABLED
406 info += NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_OBSTACLE_COUNT);
407 #endif // NAVIGATION_3D_DISABLED
408 return info;
409
410 #ifndef NAVIGATION_2D_DISABLED
411 case NAVIGATION_2D_ACTIVE_MAPS:
412 return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_ACTIVE_MAPS);
413 case NAVIGATION_2D_REGION_COUNT:
414 return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_REGION_COUNT);
415 case NAVIGATION_2D_AGENT_COUNT:
416 return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_AGENT_COUNT);
417 case NAVIGATION_2D_LINK_COUNT:
418 return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_LINK_COUNT);
419 case NAVIGATION_2D_POLYGON_COUNT:
420 return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_POLYGON_COUNT);
421 case NAVIGATION_2D_EDGE_COUNT:
422 return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_COUNT);
423 case NAVIGATION_2D_EDGE_MERGE_COUNT:
424 return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_MERGE_COUNT);
425 case NAVIGATION_2D_EDGE_CONNECTION_COUNT:
426 return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_CONNECTION_COUNT);
427 case NAVIGATION_2D_EDGE_FREE_COUNT:
428 return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_EDGE_FREE_COUNT);
429 case NAVIGATION_2D_OBSTACLE_COUNT:
430 return NavigationServer2D::get_singleton()->get_process_info(NavigationServer2D::INFO_OBSTACLE_COUNT);
431 #endif // NAVIGATION_2D_DISABLED
432
433 #ifndef NAVIGATION_3D_DISABLED
434 case NAVIGATION_3D_ACTIVE_MAPS:
435 return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS);
436 case NAVIGATION_3D_REGION_COUNT:
437 return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_REGION_COUNT);
438 case NAVIGATION_3D_AGENT_COUNT:
439 return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_AGENT_COUNT);
440 case NAVIGATION_3D_LINK_COUNT:
441 return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_LINK_COUNT);
442 case NAVIGATION_3D_POLYGON_COUNT:
443 return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_POLYGON_COUNT);
444 case NAVIGATION_3D_EDGE_COUNT:
445 return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_COUNT);
446 case NAVIGATION_3D_EDGE_MERGE_COUNT:
447 return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_MERGE_COUNT);
448 case NAVIGATION_3D_EDGE_CONNECTION_COUNT:
449 return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_CONNECTION_COUNT);
450 case NAVIGATION_3D_EDGE_FREE_COUNT:
451 return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_FREE_COUNT);
452 case NAVIGATION_3D_OBSTACLE_COUNT:
453 return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_OBSTACLE_COUNT);
454 #endif // NAVIGATION_3D_DISABLED
455
456 default: {
457 }
458 }
459
460 return 0;
461 }
462
463 Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const {
464 ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, MONITOR_TYPE_QUANTITY);
465 // ugly
466 static const MonitorType types[MONITOR_MAX] = {
467 MONITOR_TYPE_QUANTITY,
468 MONITOR_TYPE_TIME,
469 MONITOR_TYPE_TIME,
470 MONITOR_TYPE_TIME,
471 MONITOR_TYPE_MEMORY,
472 MONITOR_TYPE_MEMORY,
473 MONITOR_TYPE_MEMORY,
474 MONITOR_TYPE_QUANTITY,
475 MONITOR_TYPE_QUANTITY,
476 MONITOR_TYPE_QUANTITY,
477 MONITOR_TYPE_QUANTITY,
478 MONITOR_TYPE_QUANTITY,
479 MONITOR_TYPE_QUANTITY,
480 MONITOR_TYPE_QUANTITY,
481 MONITOR_TYPE_MEMORY,
482 MONITOR_TYPE_MEMORY,
483 MONITOR_TYPE_MEMORY,
484 MONITOR_TYPE_QUANTITY,
485 MONITOR_TYPE_QUANTITY,
486 MONITOR_TYPE_QUANTITY,
487 MONITOR_TYPE_QUANTITY,
488 MONITOR_TYPE_QUANTITY,
489 MONITOR_TYPE_QUANTITY,
490 MONITOR_TYPE_TIME,
491 MONITOR_TYPE_QUANTITY,
492 MONITOR_TYPE_QUANTITY,
493 MONITOR_TYPE_QUANTITY,
494 MONITOR_TYPE_QUANTITY,
495 MONITOR_TYPE_QUANTITY,
496 MONITOR_TYPE_QUANTITY,
497 MONITOR_TYPE_QUANTITY,
498 MONITOR_TYPE_QUANTITY,
499 MONITOR_TYPE_QUANTITY,
500 MONITOR_TYPE_QUANTITY,
501 MONITOR_TYPE_QUANTITY,
502 MONITOR_TYPE_QUANTITY,
503 MONITOR_TYPE_QUANTITY,
504 MONITOR_TYPE_QUANTITY,
505 MONITOR_TYPE_QUANTITY,
506 MONITOR_TYPE_QUANTITY,
507 MONITOR_TYPE_QUANTITY,
508 MONITOR_TYPE_QUANTITY,
509 MONITOR_TYPE_QUANTITY,
510 MONITOR_TYPE_QUANTITY,
511 MONITOR_TYPE_QUANTITY,
512 MONITOR_TYPE_QUANTITY,
513 MONITOR_TYPE_QUANTITY,
514 MONITOR_TYPE_QUANTITY,
515 MONITOR_TYPE_QUANTITY,
516 #ifndef _3D_DISABLED
517 MONITOR_TYPE_QUANTITY,
518 MONITOR_TYPE_QUANTITY,
519 MONITOR_TYPE_QUANTITY,
520 MONITOR_TYPE_QUANTITY,
521 MONITOR_TYPE_QUANTITY,
522 MONITOR_TYPE_QUANTITY,
523 MONITOR_TYPE_QUANTITY,
524 MONITOR_TYPE_QUANTITY,
525 MONITOR_TYPE_QUANTITY,
526 MONITOR_TYPE_QUANTITY,
527 #endif // _3D_DISABLED
528
529 };
530 static_assert((sizeof(types) / sizeof(MonitorType)) == MONITOR_MAX);
531
532 return types[p_monitor];
533 }
534
535 void Performance::set_process_time(double p_pt) {
536 _process_time = p_pt;
537 }
538
539 void Performance::set_physics_process_time(double p_pt) {
540 _physics_process_time = p_pt;
541 }
542
543 void Performance::set_navigation_process_time(double p_pt) {
544 _navigation_process_time = p_pt;
545 }
546
547 void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args, MonitorType p_type) {
548 ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists.");
549 _monitor_map.insert(p_id, MonitorCall(p_type, p_callable, p_args));
550 _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
551 }
552
553 void Performance::remove_custom_monitor(const StringName &p_id) {
554 ERR_FAIL_COND_MSG(!has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' doesn't exist.");
555 _monitor_map.erase(p_id);
556 _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
557 }
558
559 bool Performance::has_custom_monitor(const StringName &p_id) {
560 return _monitor_map.has(p_id);
561 }
562
563 Variant Performance::get_custom_monitor(const StringName &p_id) {
564 ERR_FAIL_COND_V_MSG(!has_custom_monitor(p_id), Variant(), "Custom monitor with id '" + String(p_id) + "' doesn't exist.");
565 bool error;
566 String error_message;
567 Variant return_value = _monitor_map[p_id].call(error, error_message);
568 ERR_FAIL_COND_V_MSG(error, return_value, "Error calling from custom monitor '" + String(p_id) + "' to callable: " + error_message);
569 return return_value;
570 }
571
572 TypedArray<StringName> Performance::get_custom_monitor_names() {
573 if (!_monitor_map.size()) {
574 return TypedArray<StringName>();
575 }
576 TypedArray<StringName> return_array;
577 return_array.resize(_monitor_map.size());
578 int index = 0;
579 for (KeyValue<StringName, MonitorCall> i : _monitor_map) {
580 return_array.set(index, i.key);
581 index++;
582 }
583 return return_array;
584 }
585
586 Vector<int> Performance::get_custom_monitor_types() {
587 if (_monitor_map.is_empty()) {
588 return Vector<int>();
589 }
590 Vector<int> ret;
591 ret.resize(_monitor_map.size());
592 int index = 0;
593 for (const KeyValue<StringName, MonitorCall> &i : _monitor_map) {
594 ret.set(index, (int)i.value.get_monitor_type());
595 index++;
596 }
597 return ret;
598 }
599
600 uint64_t Performance::get_monitor_modification_time() {
601 return _monitor_modification_time;
602 }
603
604 Performance::Performance() {
605 _process_time = 0;
606 _physics_process_time = 0;
607 _navigation_process_time = 0;
608 _monitor_modification_time = 0;
609 singleton = this;
610 }
611
612 Performance::MonitorCall::MonitorCall(Performance::MonitorType p_type, const Callable &p_callable, const Vector<Variant> &p_arguments) {
613 _type = p_type;
614 _callable = p_callable;
615 _arguments = p_arguments;
616 }
617
618 Performance::MonitorCall::MonitorCall() {
619 }
620
621 Variant Performance::MonitorCall::call(bool &r_error, String &r_error_message) {
622 Vector<const Variant *> arguments_mem;
623 arguments_mem.resize(_arguments.size());
624 for (int i = 0; i < _arguments.size(); i++) {
625 arguments_mem.write[i] = &_arguments[i];
626 }
627 const Variant **args = (const Variant **)arguments_mem.ptr();
628 int argc = _arguments.size();
629 Variant return_value;
630 Callable::CallError error;
631 _callable.callp(args, argc, return_value, error);
632 r_error = (error.error != Callable::CallError::CALL_OK);
633 if (r_error) {
634 r_error_message = Variant::get_callable_error_text(_callable, args, argc, error);
635 }
636 return return_value;
637 }