Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/objectdb_profiler/editor/data_viewers/refcounted_view.cpp
11325 views
1
/**************************************************************************/
2
/* refcounted_view.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 "refcounted_view.h"
32
33
#include "editor/editor_node.h"
34
#include "editor/themes/editor_scale.h"
35
#include "scene/gui/rich_text_label.h"
36
#include "scene/gui/split_container.h"
37
38
SnapshotRefCountedView::SnapshotRefCountedView() {
39
set_name(TTRC("RefCounted"));
40
}
41
42
void SnapshotRefCountedView::show_snapshot(GameStateSnapshot *p_data, GameStateSnapshot *p_diff_data) {
43
SnapshotView::show_snapshot(p_data, p_diff_data);
44
45
item_data_map.clear();
46
data_item_map.clear();
47
48
set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
49
set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
50
51
refs_view = memnew(HSplitContainer);
52
add_child(refs_view);
53
refs_view->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT);
54
55
VBoxContainer *refs_column = memnew(VBoxContainer);
56
refs_column->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT);
57
refs_view->add_child(refs_column);
58
59
// Tree of Refs.
60
refs_list = memnew(Tree);
61
62
filter_bar = memnew(TreeSortAndFilterBar(refs_list, TTRC("Filter RefCounteds")));
63
refs_column->add_child(filter_bar);
64
int offset = diff_data ? 1 : 0;
65
if (diff_data) {
66
filter_bar->add_sort_option(TTRC("Snapshot"), TreeSortAndFilterBar::SortType::ALPHA_SORT, 0);
67
}
68
filter_bar->add_sort_option(TTRC("Class"), TreeSortAndFilterBar::SortType::ALPHA_SORT, offset + 0);
69
filter_bar->add_sort_option(TTRC("Name"), TreeSortAndFilterBar::SortType::ALPHA_SORT, offset + 1);
70
TreeSortAndFilterBar::SortOptionIndexes default_sort = filter_bar->add_sort_option(
71
TTRC("Native Refs"),
72
TreeSortAndFilterBar::SortType::NUMERIC_SORT,
73
offset + 2);
74
filter_bar->add_sort_option(TTRC("ObjectDB Refs"), TreeSortAndFilterBar::SortType::NUMERIC_SORT, offset + 3);
75
filter_bar->add_sort_option(TTRC("Total Refs"), TreeSortAndFilterBar::SortType::NUMERIC_SORT, offset + 4);
76
filter_bar->add_sort_option(TTRC("ObjectDB Cycles"), TreeSortAndFilterBar::SortType::NUMERIC_SORT, offset + 5);
77
78
refs_list->set_select_mode(Tree::SelectMode::SELECT_ROW);
79
refs_list->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
80
refs_list->set_hide_folding(false);
81
refs_column->add_child(refs_list);
82
refs_list->set_hide_root(true);
83
refs_list->set_columns(diff_data ? 7 : 6);
84
refs_list->set_column_titles_visible(true);
85
86
if (diff_data) {
87
refs_list->set_column_title(0, TTRC("Snapshot"));
88
refs_list->set_column_expand(0, false);
89
refs_list->set_column_title_tooltip_text(0, "A: " + snapshot_data->name + ", B: " + diff_data->name);
90
}
91
92
refs_list->set_column_title(offset + 0, TTRC("Class"));
93
refs_list->set_column_expand(offset + 0, true);
94
refs_list->set_column_title_tooltip_text(offset + 0, TTRC("Object's class"));
95
96
refs_list->set_column_title(offset + 1, TTRC("Name"));
97
refs_list->set_column_expand(offset + 1, true);
98
refs_list->set_column_expand_ratio(offset + 1, 2);
99
refs_list->set_column_title_tooltip_text(offset + 1, TTRC("Object's name"));
100
101
refs_list->set_column_title(offset + 2, TTRC("Native Refs"));
102
refs_list->set_column_expand(offset + 2, false);
103
refs_list->set_column_title_tooltip_text(offset + 2, TTRC("References not owned by the ObjectDB"));
104
105
refs_list->set_column_title(offset + 3, TTRC("ObjectDB Refs"));
106
refs_list->set_column_expand(offset + 3, false);
107
refs_list->set_column_title_tooltip_text(offset + 3, TTRC("References owned by the ObjectDB"));
108
109
refs_list->set_column_title(offset + 4, TTRC("Total Refs"));
110
refs_list->set_column_expand(offset + 4, false);
111
refs_list->set_column_title_tooltip_text(offset + 4, TTRC("ObjectDB References + Native References"));
112
113
refs_list->set_column_title(offset + 5, TTRC("ObjectDB Cycles"));
114
refs_list->set_column_expand(offset + 5, false);
115
refs_list->set_column_title_tooltip_text(offset + 5, TTRC("Cycles detected in the ObjectDB"));
116
117
refs_list->connect(SceneStringName(item_selected), callable_mp(this, &SnapshotRefCountedView::_refcounted_selected));
118
refs_list->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
119
refs_list->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
120
121
// View of the selected refcounted.
122
ref_details = memnew(VBoxContainer);
123
ref_details->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
124
refs_view->add_child(ref_details);
125
ref_details->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
126
ref_details->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
127
128
refs_list->create_item();
129
_insert_data(snapshot_data, TTRC("A"));
130
if (diff_data) {
131
_insert_data(diff_data, TTRC("B"));
132
}
133
134
// Push the split as far right as possible.
135
filter_bar->select_sort(default_sort.descending);
136
filter_bar->apply();
137
refs_list->set_selected(refs_list->get_root()->get_first_child());
138
139
callable_mp(this, &SnapshotRefCountedView::_set_split_to_center).call_deferred();
140
}
141
142
void SnapshotRefCountedView::_set_split_to_center() {
143
refs_view->set_split_offset(refs_view->get_size().x * 0.5);
144
}
145
146
void SnapshotRefCountedView::_insert_data(GameStateSnapshot *p_snapshot, const String &p_name) {
147
for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_snapshot->objects) {
148
if (!pair.value->is_refcounted()) {
149
continue;
150
}
151
152
TreeItem *item = refs_list->create_item(refs_list->get_root());
153
item_data_map[item] = pair.value;
154
data_item_map[pair.value] = item;
155
int total_refs = pair.value->extra_debug_data.has("ref_count") ? (uint64_t)pair.value->extra_debug_data["ref_count"] : 0;
156
int objectdb_refs = pair.value->get_unique_inbound_references().size();
157
int native_refs = total_refs - objectdb_refs;
158
159
Array ref_cycles = (Array)pair.value->extra_debug_data["ref_cycles"];
160
161
int offset = 0;
162
if (diff_data) {
163
item->set_text(0, p_name);
164
item->set_tooltip_text(0, p_snapshot->name);
165
item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED);
166
offset = 1;
167
}
168
169
item->set_text(offset + 0, pair.value->type_name);
170
item->set_auto_translate_mode(offset + 0, AUTO_TRANSLATE_MODE_DISABLED);
171
item->set_text(offset + 1, pair.value->get_name());
172
item->set_auto_translate_mode(offset + 1, AUTO_TRANSLATE_MODE_DISABLED);
173
item->set_text(offset + 2, String::num_uint64(native_refs));
174
item->set_text(offset + 3, String::num_uint64(objectdb_refs));
175
item->set_text(offset + 4, String::num_uint64(total_refs));
176
item->set_text(offset + 5, String::num_uint64(ref_cycles.size())); // Compute cycles and attach it to refcounted object.
177
178
if (total_refs == ref_cycles.size()) {
179
// Often, references are held by the engine so we can't know if we're stuck in a cycle or not
180
// But if the full cycle is visible in the ObjectDB,
181
// tell the user by highlighting the cells in red.
182
item->set_custom_bg_color(offset + 5, Color(1, 0, 0, 0.1));
183
}
184
}
185
}
186
187
void SnapshotRefCountedView::_refcounted_selected() {
188
for (int i = 0; i < ref_details->get_child_count(); i++) {
189
ref_details->get_child(i)->queue_free();
190
}
191
192
SnapshotDataObject *d = item_data_map[refs_list->get_selected()];
193
EditorNode::get_singleton()->push_item(static_cast<Object *>(d));
194
195
DarkPanelContainer *refcounted_panel = memnew(DarkPanelContainer);
196
VBoxContainer *refcounted_panel_content = memnew(VBoxContainer);
197
refcounted_panel_content->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
198
refcounted_panel_content->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
199
ref_details->add_child(refcounted_panel);
200
refcounted_panel->add_child(refcounted_panel_content);
201
refcounted_panel_content->add_child(memnew(SpanningHeader(d->get_name())));
202
203
ScrollContainer *properties_scroll = memnew(ScrollContainer);
204
properties_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
205
properties_scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_AUTO);
206
properties_scroll->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
207
properties_scroll->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
208
refcounted_panel_content->add_child(properties_scroll);
209
210
VBoxContainer *properties_container = memnew(VBoxContainer);
211
properties_container->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
212
properties_scroll->add_child(properties_container);
213
properties_container->add_theme_constant_override("separation", 5);
214
properties_container->add_theme_constant_override("margin_left", 2);
215
properties_container->add_theme_constant_override("margin_right", 2);
216
properties_container->add_theme_constant_override("margin_top", 2);
217
properties_container->add_theme_constant_override("margin_bottom", 2);
218
219
int total_refs = d->extra_debug_data.has("ref_count") ? (uint64_t)d->extra_debug_data["ref_count"] : 0;
220
int objectdb_refs = d->get_unique_inbound_references().size();
221
int native_refs = total_refs - objectdb_refs;
222
Array ref_cycles = (Array)d->extra_debug_data["ref_cycles"];
223
224
String count_str = "[ul]\n";
225
count_str += vformat(TTRC("Native References: %d\n"), native_refs);
226
count_str += vformat(TTRC("ObjectDB References: %d\n"), objectdb_refs);
227
count_str += vformat(TTRC("Total References: %d\n"), total_refs);
228
count_str += vformat(TTRC("ObjectDB Cycles: %d\n"), ref_cycles.size());
229
count_str += "[/ul]\n";
230
RichTextLabel *counts = memnew(RichTextLabel(count_str));
231
counts->set_use_bbcode(true);
232
counts->set_fit_content(true);
233
counts->add_theme_constant_override("line_separation", 6);
234
properties_container->add_child(counts);
235
236
if (d->inbound_references.size() > 0) {
237
RichTextLabel *inbound_lbl = memnew(RichTextLabel(TTRC("[center]ObjectDB References[center]")));
238
inbound_lbl->set_fit_content(true);
239
inbound_lbl->set_use_bbcode(true);
240
properties_container->add_child(inbound_lbl);
241
Tree *inbound_tree = memnew(Tree);
242
inbound_tree->set_hide_folding(true);
243
properties_container->add_child(inbound_tree);
244
inbound_tree->set_select_mode(Tree::SelectMode::SELECT_ROW);
245
inbound_tree->set_hide_root(true);
246
inbound_tree->set_columns(3);
247
inbound_tree->set_column_titles_visible(true);
248
inbound_tree->set_column_title(0, TTRC("Source"));
249
inbound_tree->set_column_expand(0, true);
250
inbound_tree->set_column_clip_content(0, false);
251
inbound_tree->set_column_title_tooltip_text(0, TTRC("Other object referencing this object"));
252
inbound_tree->set_column_title(1, TTRC("Property"));
253
inbound_tree->set_column_expand(1, true);
254
inbound_tree->set_column_clip_content(1, true);
255
inbound_tree->set_column_title_tooltip_text(1, TTRC("Property of other object referencing this object"));
256
inbound_tree->set_column_title(2, TTRC("Duplicate?"));
257
inbound_tree->set_column_expand(2, false);
258
inbound_tree->set_column_title_tooltip_text(2, TTRC("Was the same reference returned by multiple getters on the source object?"));
259
inbound_tree->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
260
inbound_tree->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
261
inbound_tree->set_v_scroll_enabled(false);
262
inbound_tree->connect(SceneStringName(item_selected), callable_mp(this, &SnapshotRefCountedView::_ref_selected).bind(inbound_tree));
263
264
// The same reference can exist as multiple properties of an object (for example, gdscript `@export` properties exist twice).
265
// We flag for the user if a property is exposed multiple times so it's clearer why there are more references in the list
266
// than the ObjectDB References count would suggest.
267
HashMap<ObjectID, int> property_repeat_count;
268
for (const KeyValue<String, ObjectID> &ob : d->inbound_references) {
269
if (!property_repeat_count.has(ob.value)) {
270
property_repeat_count.insert(ob.value, 0);
271
}
272
property_repeat_count[ob.value]++;
273
}
274
275
TreeItem *root = inbound_tree->create_item();
276
for (const KeyValue<String, ObjectID> &ob : d->inbound_references) {
277
TreeItem *i = inbound_tree->create_item(root);
278
SnapshotDataObject *target = d->snapshot->objects[ob.value];
279
i->set_text(0, target->get_name());
280
i->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED);
281
i->set_text(1, ob.key);
282
i->set_auto_translate_mode(1, AUTO_TRANSLATE_MODE_DISABLED);
283
i->set_text(2, property_repeat_count[ob.value] > 1 ? TTRC("Yes") : TTRC("No"));
284
reference_item_map[i] = data_item_map[target];
285
}
286
}
287
288
if (ref_cycles.size() > 0) {
289
properties_container->add_child(memnew(SpanningHeader(TTRC("ObjectDB Cycles"))));
290
Tree *cycles_tree = memnew(Tree);
291
cycles_tree->set_hide_folding(true);
292
properties_container->add_child(cycles_tree);
293
cycles_tree->set_select_mode(Tree::SelectMode::SELECT_ROW);
294
cycles_tree->set_hide_root(true);
295
cycles_tree->set_columns(1);
296
cycles_tree->set_column_titles_visible(false);
297
cycles_tree->set_column_expand(0, true);
298
cycles_tree->set_column_clip_content(0, false);
299
cycles_tree->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
300
cycles_tree->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
301
cycles_tree->set_v_scroll_enabled(false);
302
303
TreeItem *root = cycles_tree->create_item();
304
for (const Variant &cycle : ref_cycles) {
305
TreeItem *i = cycles_tree->create_item(root);
306
i->set_text(0, cycle);
307
i->set_text_overrun_behavior(0, TextServer::OverrunBehavior::OVERRUN_NO_TRIMMING);
308
}
309
}
310
}
311
312
void SnapshotRefCountedView::_ref_selected(Tree *p_source_tree) {
313
TreeItem *target = reference_item_map[p_source_tree->get_selected()];
314
if (target) {
315
if (!target->is_visible()) {
316
// Clear the filter if we can't see the node we just chose.
317
filter_bar->clear_filter();
318
}
319
target->get_tree()->deselect_all();
320
target->get_tree()->set_selected(target);
321
target->get_tree()->ensure_cursor_is_visible();
322
}
323
}
324
325