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

@@ -62,11 +62,16 @@ public:
last_perf_time = pt;
Array custom_monitor_names = performance->call("get_custom_monitor_names");
Array custom_monitor_types = performance->call("get_custom_monitor_types");
Array custom_monitor_data;
custom_monitor_data.push_back(custom_monitor_names);
custom_monitor_data.push_back(custom_monitor_types);
uint64_t monitor_modification_time = performance->call("get_monitor_modification_time");
if (monitor_modification_time > last_monitor_modification_time) {
last_monitor_modification_time = monitor_modification_time;
EngineDebugger::get_singleton()->send_message("performance:profile_names", custom_monitor_names);
EngineDebugger::get_singleton()->send_message("performance:profile_names", custom_monitor_data);
}
int max = performance->get("MONITOR_MAX");

View File

@@ -18,6 +18,7 @@
<param index="0" name="id" type="StringName" />
<param index="1" name="callable" type="Callable" />
<param index="2" name="arguments" type="Array" default="[]" />
<param index="3" name="type" type="int" enum="Performance.MonitorType" default="0" />
<description>
Adds a custom monitor with the name [param id]. You can specify the category of the monitor using slash delimiters in [param id] (for example: [code]"Game/NumberOfNPCs"[/code]). If there is more than one slash delimiter, then the default category is used. The default category is [code]"Custom"[/code]. Prints an error if given [param id] is already present.
[codeblocks]
@@ -84,6 +85,12 @@
Returns the names of active custom monitors in an [Array].
</description>
</method>
<method name="get_custom_monitor_types">
<return type="PackedInt32Array" />
<description>
Returns the [enum MonitorType] values of active custom monitors in an [Array].
</description>
</method>
<method name="get_monitor" qualifiers="const">
<return type="float" />
<param index="0" name="monitor" type="int" enum="Performance.Monitor" />
@@ -303,5 +310,17 @@
<constant name="MONITOR_MAX" value="59" enum="Monitor">
Represents the size of the [enum Monitor] enum.
</constant>
<constant name="MONITOR_TYPE_QUANTITY" value="0" enum="MonitorType">
Monitor output is formatted as an integer value.
</constant>
<constant name="MONITOR_TYPE_MEMORY" value="1" enum="MonitorType">
Monitor output is formatted as computer memory. Submitted values should represent a number of bytes.
</constant>
<constant name="MONITOR_TYPE_TIME" value="2" enum="MonitorType">
Monitor output is formatted as time in milliseconds. Submitted values should represent a time in seconds (not milliseconds).
</constant>
<constant name="MONITOR_TYPE_PERCENTAGE" value="3" enum="MonitorType">
Monitor output is formatted as a percentage. Submitted values should represent a fractional value rather than the percentage directly, e.g. [code]0.5[/code] for [code]50.00%[/code].
</constant>
</constants>
</class>

View File

@@ -88,6 +88,9 @@ String EditorPerformanceProfiler::_create_label(float p_value, Performance::Moni
case Performance::MONITOR_TYPE_TIME: {
return TS->format_number(rtos(p_value * 1000).pad_decimals(2)) + " " + TTR("ms");
}
case Performance::MONITOR_TYPE_PERCENTAGE: {
return TS->format_number(rtos(p_value * 100).pad_decimals(2)) + "%";
}
default: {
return TS->format_number(rtos(p_value));
}
@@ -317,7 +320,7 @@ void EditorPerformanceProfiler::reset() {
monitor_draw->queue_redraw();
}
void EditorPerformanceProfiler::update_monitors(const Vector<StringName> &p_names) {
void EditorPerformanceProfiler::update_monitors(const Vector<StringName> &p_names, const PackedInt32Array &p_types) {
HashMap<StringName, int> names;
for (int i = 0; i < p_names.size(); i++) {
names.insert("custom:" + p_names[i], Performance::MONITOR_MAX + i);
@@ -340,6 +343,7 @@ void EditorPerformanceProfiler::update_monitors(const Vector<StringName> &p_name
}
}
int index = 0;
for (const KeyValue<StringName, int> &E : names) {
String name = String(E.key).replace_first("custom:", "");
String base = "Custom";
@@ -347,7 +351,9 @@ void EditorPerformanceProfiler::update_monitors(const Vector<StringName> &p_name
base = name.get_slicec('/', 0);
name = name.get_slicec('/', 1);
}
monitors.insert(E.key, Monitor(name, base, E.value, Performance::MONITOR_TYPE_QUANTITY, nullptr));
Performance::MonitorType type = Performance::MonitorType(p_types[index]);
monitors.insert(E.key, Monitor(name, base, E.value, type, nullptr));
index++;
}
_build_monitor_tree();

View File

@@ -82,7 +82,7 @@ protected:
public:
void reset();
void update_monitors(const Vector<StringName> &p_names);
void update_monitors(const Vector<StringName> &p_names, const PackedInt32Array &p_types);
void add_profile_frame(const Vector<float> &p_values);
List<float> *get_monitor_data(const StringName &p_name);
EditorPerformanceProfiler();

View File

@@ -912,13 +912,25 @@ void ScriptEditorDebugger::_msg_show_selection_limit_warning(uint64_t p_thread_i
}
void ScriptEditorDebugger::_msg_performance_profile_names(uint64_t p_thread_id, const Array &p_data) {
ERR_FAIL_COND(p_data.size() != 2);
Array name_data = p_data[0];
Array type_data = p_data[1];
Vector<StringName> monitors;
monitors.resize(p_data.size());
for (int i = 0; i < p_data.size(); i++) {
ERR_FAIL_COND(p_data[i].get_type() != Variant::STRING_NAME);
monitors.set(i, p_data[i]);
monitors.resize(name_data.size());
for (int i = 0; i < name_data.size(); i++) {
ERR_FAIL_COND(name_data[i].get_type() != Variant::STRING_NAME);
monitors.set(i, name_data[i]);
}
performance_profiler->update_monitors(monitors);
PackedInt32Array types;
types.resize(type_data.size());
for (int i = 0; i < type_data.size(); i++) {
ERR_FAIL_COND(type_data[i].get_type() != Variant::INT);
types.set(i, type_data[i]);
}
performance_profiler->update_monitors(monitors, types);
}
void ScriptEditorDebugger::_msg_filesystem_update_file(uint64_t p_thread_id, const Array &p_data) {

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);

View File

@@ -61,3 +61,10 @@ GH-111439
Validate extension JSON: Error: Field 'classes/FileDialog/methods/add_filter/arguments': size changed value in new API, from 2 to 3.
Optional argument added. Compatibility method registered.
GH-110433
---------
Validate extension JSON: Error: Field 'classes/Performance/methods/add_custom_monitor/arguments': size changed value in new API, from 3 to 4.
Optional argument added. Compatibility method registered.