1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Allow custom monitors to select desired type

This commit is contained in:
devloglogan
2025-09-09 21:34:22 -05:00
parent 9cd297b6f2
commit 1a8306bbc1
9 changed files with 151 additions and 28 deletions

View File

@@ -0,0 +1,41 @@
/**************************************************************************/
/* performance.compat.inc */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef DISABLE_DEPRECATED
void Performance::_add_custom_monitor_bind_compat_110433(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
add_custom_monitor(p_id, p_callable, p_args, MONITOR_TYPE_QUANTITY);
}
void Performance::_bind_compatibility_methods() {
ClassDB::bind_compatibility_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::_add_custom_monitor_bind_compat_110433, DEFVAL(Array()));
}
#endif

View File

@@ -29,6 +29,7 @@
/**************************************************************************/
#include "performance.h"
#include "performance.compat.inc"
#include "core/os/os.h"
#include "core/variant/typed_array.h"
@@ -57,12 +58,13 @@ Performance *Performance::singleton = nullptr;
void Performance::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor);
ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::add_custom_monitor, DEFVAL(Array()));
ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments", "type"), &Performance::add_custom_monitor, DEFVAL(Array()), DEFVAL(MONITOR_TYPE_QUANTITY));
ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor);
ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor);
ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor);
ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time);
ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names);
ClassDB::bind_method(D_METHOD("get_custom_monitor_types"), &Performance::get_custom_monitor_types);
BIND_ENUM_CONSTANT(TIME_FPS);
BIND_ENUM_CONSTANT(TIME_PROCESS);
@@ -132,6 +134,11 @@ void Performance::_bind_methods() {
BIND_ENUM_CONSTANT(NAVIGATION_3D_OBSTACLE_COUNT);
#endif // NAVIGATION_3D_DISABLED
BIND_ENUM_CONSTANT(MONITOR_MAX);
BIND_ENUM_CONSTANT(MONITOR_TYPE_QUANTITY);
BIND_ENUM_CONSTANT(MONITOR_TYPE_MEMORY);
BIND_ENUM_CONSTANT(MONITOR_TYPE_TIME);
BIND_ENUM_CONSTANT(MONITOR_TYPE_PERCENTAGE);
}
int Performance::_get_node_count() const {
@@ -537,9 +544,9 @@ void Performance::set_navigation_process_time(double p_pt) {
_navigation_process_time = p_pt;
}
void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args, MonitorType p_type) {
ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists.");
_monitor_map.insert(p_id, MonitorCall(p_callable, p_args));
_monitor_map.insert(p_id, MonitorCall(p_type, p_callable, p_args));
_monitor_modification_time = OS::get_singleton()->get_ticks_usec();
}
@@ -576,6 +583,20 @@ TypedArray<StringName> Performance::get_custom_monitor_names() {
return return_array;
}
Vector<int> Performance::get_custom_monitor_types() {
if (_monitor_map.is_empty()) {
return Vector<int>();
}
Vector<int> ret;
ret.resize(_monitor_map.size());
int index = 0;
for (const KeyValue<StringName, MonitorCall> &i : _monitor_map) {
ret.set(index, (int)i.value.get_monitor_type());
index++;
}
return ret;
}
uint64_t Performance::get_monitor_modification_time() {
return _monitor_modification_time;
}
@@ -588,7 +609,8 @@ Performance::Performance() {
singleton = this;
}
Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) {
Performance::MonitorCall::MonitorCall(Performance::MonitorType p_type, const Callable &p_callable, const Vector<Variant> &p_arguments) {
_type = p_type;
_callable = p_callable;
_arguments = p_arguments;
}

View File

@@ -45,6 +45,11 @@ class Performance : public Object {
static Performance *singleton;
static void _bind_methods();
#ifndef DISABLE_DEPRECATED
void _add_custom_monitor_bind_compat_110433(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args);
static void _bind_compatibility_methods();
#endif
int _get_node_count() const;
int _get_orphan_node_count() const;
@@ -52,19 +57,6 @@ class Performance : public Object {
double _physics_process_time;
double _navigation_process_time;
class MonitorCall {
Callable _callable;
Vector<Variant> _arguments;
public:
MonitorCall(Callable p_callable, Vector<Variant> p_arguments);
MonitorCall();
Variant call(bool &r_error, String &r_error_message);
};
HashMap<StringName, MonitorCall> _monitor_map;
uint64_t _monitor_modification_time;
public:
enum Monitor {
TIME_FPS,
@@ -135,7 +127,8 @@ public:
enum MonitorType {
MONITOR_TYPE_QUANTITY,
MONITOR_TYPE_MEMORY,
MONITOR_TYPE_TIME
MONITOR_TYPE_TIME,
MONITOR_TYPE_PERCENTAGE,
};
double get_monitor(Monitor p_monitor) const;
@@ -147,17 +140,35 @@ public:
void set_physics_process_time(double p_pt);
void set_navigation_process_time(double p_pt);
void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args);
void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args, MonitorType p_type = MONITOR_TYPE_QUANTITY);
void remove_custom_monitor(const StringName &p_id);
bool has_custom_monitor(const StringName &p_id);
Variant get_custom_monitor(const StringName &p_id);
TypedArray<StringName> get_custom_monitor_names();
Vector<int> get_custom_monitor_types();
uint64_t get_monitor_modification_time();
static Performance *get_singleton() { return singleton; }
Performance();
private:
class MonitorCall {
MonitorType _type = MONITOR_TYPE_QUANTITY;
Callable _callable;
Vector<Variant> _arguments;
public:
MonitorCall(MonitorType p_type, const Callable &p_callable, const Vector<Variant> &p_arguments);
MonitorCall();
Variant call(bool &r_error, String &r_error_message);
inline MonitorType get_monitor_type() const { return _type; }
};
HashMap<StringName, MonitorCall> _monitor_map;
uint64_t _monitor_modification_time;
};
VARIANT_ENUM_CAST(Performance::Monitor);
VARIANT_ENUM_CAST(Performance::MonitorType);