Path: blob/master/editor/debugger/editor_profiler.cpp
9906 views
/**************************************************************************/1/* editor_profiler.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_profiler.h"3132#include "core/io/image.h"33#include "editor/editor_string_names.h"34#include "editor/run/editor_run_bar.h"35#include "editor/settings/editor_settings.h"36#include "editor/themes/editor_scale.h"37#include "scene/gui/check_box.h"38#include "scene/gui/flow_container.h"39#include "scene/resources/image_texture.h"4041void EditorProfiler::_make_metric_ptrs(Metric &m) {42for (int i = 0; i < m.categories.size(); i++) {43m.category_ptrs[m.categories[i].signature] = &m.categories.write[i];44for (int j = 0; j < m.categories[i].items.size(); j++) {45m.item_ptrs[m.categories[i].items[j].signature] = &m.categories.write[i].items.write[j];46}47}48}4950EditorProfiler::Metric EditorProfiler::_get_frame_metric(int index) {51return frame_metrics[(frame_metrics.size() + last_metric - (total_metrics - 1) + index) % frame_metrics.size()];52}5354void EditorProfiler::add_frame_metric(const Metric &p_metric, bool p_final) {55++last_metric;56if (last_metric >= frame_metrics.size()) {57last_metric = 0;58}5960total_metrics++;61if (total_metrics > frame_metrics.size()) {62total_metrics = frame_metrics.size();63}6465frame_metrics.write[last_metric] = p_metric;66_make_metric_ptrs(frame_metrics.write[last_metric]);6768updating_frame = true;69clear_button->set_disabled(false);70cursor_metric_edit->set_editable(true);71cursor_metric_edit->set_max(p_metric.frame_number);72cursor_metric_edit->set_min(_get_frame_metric(0).frame_number);7374if (!seeking) {75cursor_metric_edit->set_value(p_metric.frame_number);76}7778updating_frame = false;7980if (frame_delay->is_stopped()) {81frame_delay->set_wait_time(p_final ? 0.1 : 1);82frame_delay->start();83}8485if (plot_delay->is_stopped()) {86plot_delay->set_wait_time(0.1);87plot_delay->start();88}89}9091void EditorProfiler::clear() {92int metric_size = EDITOR_GET("debugger/profiler_frame_history_size");93metric_size = CLAMP(metric_size, 60, 10000);94frame_metrics.clear();95frame_metrics.resize(metric_size);96total_metrics = 0;97last_metric = -1;98variables->clear();99plot_sigs.clear();100plot_sigs.insert("physics_frame_time");101plot_sigs.insert("category_frame_time");102display_internal_profiles->set_visible(EDITOR_GET("debugger/profile_native_calls"));103104updating_frame = true;105cursor_metric_edit->set_min(0);106cursor_metric_edit->set_max(100); // Doesn't make much sense, but we can't have min == max. Doesn't hurt.107cursor_metric_edit->set_value(0);108cursor_metric_edit->set_editable(false);109updating_frame = false;110hover_metric = -1;111seeking = false;112113// Ensure button text (start, stop) is correct114_update_button_text();115emit_signal(SNAME("enable_profiling"), activate->is_pressed());116}117118static String _get_percent_txt(float p_value, float p_total) {119if (p_total == 0) {120p_total = 0.00001;121}122123return TS->format_number(String::num((p_value / p_total) * 100, 1)) + TS->percent_sign();124}125126String EditorProfiler::_get_time_as_text(const Metric &m, float p_time, int p_calls) {127const int dmode = display_mode->get_selected();128129if (dmode == DISPLAY_FRAME_TIME) {130return TS->format_number(rtos(p_time * 1000).pad_decimals(2)) + " " + TTR("ms");131} else if (dmode == DISPLAY_AVERAGE_TIME) {132if (p_calls == 0) {133return TS->format_number("0.00") + " " + TTR("ms");134} else {135return TS->format_number(rtos((p_time / p_calls) * 1000).pad_decimals(2)) + " " + TTR("ms");136}137} else if (dmode == DISPLAY_FRAME_PERCENT) {138return _get_percent_txt(p_time, m.frame_time);139} else if (dmode == DISPLAY_PHYSICS_FRAME_PERCENT) {140return _get_percent_txt(p_time, m.physics_frame_time);141}142143return "err";144}145146Color EditorProfiler::_get_color_from_signature(const StringName &p_signature) const {147Color bc = get_theme_color(SNAME("error_color"), EditorStringName(Editor));148double rot = Math::abs(double(p_signature.hash()) / double(0x7FFFFFFF));149Color c;150c.set_hsv(rot, bc.get_s(), bc.get_v());151return c.lerp(get_theme_color(SNAME("base_color"), EditorStringName(Editor)), 0.07);152}153154int EditorProfiler::_get_zoom_left_border() const {155const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);156return CLAMP(zoom_center - max_profiles_shown / 2, 0, frame_metrics.size() - max_profiles_shown);157}158159void EditorProfiler::_item_edited() {160if (updating_frame) {161return;162}163164TreeItem *item = variables->get_edited();165if (!item) {166return;167}168StringName signature = item->get_metadata(0);169bool checked = item->is_checked(0);170171if (checked) {172plot_sigs.insert(signature);173} else {174plot_sigs.erase(signature);175}176177if (!frame_delay->is_processing()) {178frame_delay->set_wait_time(0.1);179frame_delay->start();180}181182_update_plot();183}184185void EditorProfiler::_update_plot() {186const int w = MAX(1, graph->get_size().width); // Clamp to 1 to prevent from crashing when profiler is autostarted.187const int h = MAX(1, graph->get_size().height);188bool reset_texture = false;189const int desired_len = w * h * 4;190191if (graph_image.size() != desired_len) {192reset_texture = true;193graph_image.resize(desired_len);194}195196uint8_t *wr = graph_image.ptrw();197const Color background_color = get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor));198199// Clear the previous frame and set the background color.200for (int i = 0; i < desired_len; i += 4) {201wr[i + 0] = Math::fast_ftoi(background_color.r * 255);202wr[i + 1] = Math::fast_ftoi(background_color.g * 255);203wr[i + 2] = Math::fast_ftoi(background_color.b * 255);204wr[i + 3] = 255;205}206207//find highest value208209const bool use_self = display_time->get_selected() == DISPLAY_SELF_TIME;210float highest = 0;211212for (int i = 0; i < total_metrics; i++) {213const Metric &m = _get_frame_metric(i);214215for (const StringName &E : plot_sigs) {216HashMap<StringName, Metric::Category *>::ConstIterator F = m.category_ptrs.find(E);217if (F) {218highest = MAX(F->value->total_time, highest);219}220221HashMap<StringName, Metric::Category::Item *>::ConstIterator G = m.item_ptrs.find(E);222if (G) {223if (use_self) {224highest = MAX(G->value->self, highest);225} else {226highest = MAX(G->value->total, highest);227}228}229}230}231232if (highest > 0) {233//means some data exists..234highest *= 1.2; //leave some upper room235graph_height = highest;236237Vector<int> columnv;238columnv.resize(h * 4);239240int *column = columnv.ptrw();241242HashMap<StringName, int> prev_plots;243244const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);245const int left_border = _get_zoom_left_border();246const int profiles_drawn = CLAMP(total_metrics - left_border, 0, max_profiles_shown);247const int pixel_cols = (profiles_drawn * w) / max_profiles_shown - 1;248249for (int i = 0; i < pixel_cols; i++) {250for (int j = 0; j < h * 4; j++) {251column[j] = 0;252}253254int current = (i * max_profiles_shown / w) + left_border;255256for (const StringName &E : plot_sigs) {257const Metric &m = _get_frame_metric(current);258259float value = 0;260261HashMap<StringName, Metric::Category *>::ConstIterator F = m.category_ptrs.find(E);262if (F) {263value = F->value->total_time;264}265266HashMap<StringName, Metric::Category::Item *>::ConstIterator G = m.item_ptrs.find(E);267if (G) {268if (use_self) {269value = G->value->self;270} else {271value = G->value->total;272}273}274275int plot_pos = CLAMP(int(value * h / highest), 0, h - 1);276277int prev_plot = plot_pos;278HashMap<StringName, int>::Iterator H = prev_plots.find(E);279if (H) {280prev_plot = H->value;281H->value = plot_pos;282} else {283prev_plots[E] = plot_pos;284}285286plot_pos = h - plot_pos - 1;287prev_plot = h - prev_plot - 1;288289if (prev_plot > plot_pos) {290SWAP(prev_plot, plot_pos);291}292293Color col = _get_color_from_signature(E);294295for (int j = prev_plot; j <= plot_pos; j++) {296column[j * 4 + 0] += Math::fast_ftoi(CLAMP(col.r * 255, 0, 255));297column[j * 4 + 1] += Math::fast_ftoi(CLAMP(col.g * 255, 0, 255));298column[j * 4 + 2] += Math::fast_ftoi(CLAMP(col.b * 255, 0, 255));299column[j * 4 + 3] += 1;300}301}302303for (int j = 0; j < h * 4; j += 4) {304const int a = column[j + 3];305if (a > 0) {306column[j + 0] /= a;307column[j + 1] /= a;308column[j + 2] /= a;309}310311const uint8_t red = uint8_t(column[j + 0]);312const uint8_t green = uint8_t(column[j + 1]);313const uint8_t blue = uint8_t(column[j + 2]);314const bool is_filled = red >= 1 || green >= 1 || blue >= 1;315const int widx = ((j >> 2) * w + i) * 4;316317// If the pixel isn't filled by any profiler line, apply the background color instead.318wr[widx + 0] = is_filled ? red : Math::fast_ftoi(background_color.r * 255);319wr[widx + 1] = is_filled ? green : Math::fast_ftoi(background_color.g * 255);320wr[widx + 2] = is_filled ? blue : Math::fast_ftoi(background_color.b * 255);321wr[widx + 3] = 255;322}323}324}325326Ref<Image> img = Image::create_from_data(w, h, false, Image::FORMAT_RGBA8, graph_image);327328if (reset_texture) {329if (graph_texture.is_null()) {330graph_texture.instantiate();331}332graph_texture->set_image(img);333}334335graph_texture->update(img);336337graph->set_texture(graph_texture);338graph->queue_redraw();339}340341void EditorProfiler::_update_frame() {342int cursor_metric = cursor_metric_edit->get_value() - _get_frame_metric(0).frame_number;343344updating_frame = true;345variables->clear();346347TreeItem *root = variables->create_item();348const Metric &m = _get_frame_metric(cursor_metric);349350int dtime = display_time->get_selected();351352for (int i = 0; i < m.categories.size(); i++) {353TreeItem *category = variables->create_item(root);354category->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);355category->set_editable(0, true);356category->set_metadata(0, m.categories[i].signature);357category->set_text(0, String(m.categories[i].name));358category->set_text(1, _get_time_as_text(m, m.categories[i].total_time, 1));359360if (plot_sigs.has(m.categories[i].signature)) {361category->set_checked(0, true);362category->set_custom_color(0, _get_color_from_signature(m.categories[i].signature));363}364365for (int j = 0; j < m.categories[i].items.size(); j++) {366const Metric::Category::Item &it = m.categories[i].items[j];367368if (it.internal == it.total && !display_internal_profiles->is_pressed() && m.categories[i].name == "Script Functions") {369continue;370}371TreeItem *item = variables->create_item(category);372item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);373item->set_editable(0, true);374item->set_text(0, it.name);375item->set_metadata(0, it.signature);376item->set_metadata(1, it.script);377item->set_metadata(2, it.line);378item->set_text_alignment(2, HORIZONTAL_ALIGNMENT_RIGHT);379item->set_tooltip_text(0, it.name + "\n" + it.script + ":" + itos(it.line));380381float time = dtime == DISPLAY_SELF_TIME ? it.self : it.total;382if (dtime == DISPLAY_SELF_TIME && !display_internal_profiles->is_pressed()) {383time += it.internal;384}385386item->set_text(1, _get_time_as_text(m, time, it.calls));387388item->set_text(2, itos(it.calls));389390if (plot_sigs.has(it.signature)) {391item->set_checked(0, true);392item->set_custom_color(0, _get_color_from_signature(it.signature));393}394}395}396397updating_frame = false;398}399400void EditorProfiler::_update_button_text() {401if (activate->is_pressed()) {402activate->set_button_icon(get_editor_theme_icon(SNAME("Stop")));403activate->set_text(TTR("Stop"));404} else {405activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));406activate->set_text(TTR("Start"));407}408}409410void EditorProfiler::_activate_pressed() {411_update_button_text();412413if (activate->is_pressed()) {414_clear_pressed();415}416417emit_signal(SNAME("enable_profiling"), activate->is_pressed());418}419420void EditorProfiler::_clear_pressed() {421clear_button->set_disabled(true);422clear();423_update_plot();424}425426void EditorProfiler::_internal_profiles_pressed() {427_combo_changed(0);428}429430void EditorProfiler::_autostart_toggled(bool p_toggled_on) {431EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_profiler", p_toggled_on);432EditorRunBar::get_singleton()->update_profiler_autostart_indicator();433}434435void EditorProfiler::_notification(int p_what) {436switch (p_what) {437case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:438case NOTIFICATION_THEME_CHANGED:439case NOTIFICATION_TRANSLATION_CHANGED: {440activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));441clear_button->set_button_icon(get_editor_theme_icon(SNAME("Clear")));442443theme_cache.seek_line_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));444theme_cache.seek_line_color.a = 0.8;445theme_cache.seek_line_hover_color = theme_cache.seek_line_color;446theme_cache.seek_line_hover_color.a = 0.4;447448if (total_metrics > 0) {449_update_plot();450}451} break;452}453}454455void EditorProfiler::_graph_tex_draw() {456if (total_metrics == 0) {457return;458}459if (seeking) {460int frame = cursor_metric_edit->get_value() - _get_frame_metric(0).frame_number;461frame = frame - _get_zoom_left_border() + 1;462int cur_x = (frame * graph->get_size().width * Math::exp(graph_zoom)) / frame_metrics.size();463cur_x = CLAMP(cur_x, 0, graph->get_size().width);464graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), theme_cache.seek_line_color);465}466if (hover_metric > -1) {467int cur_x = (2 * hover_metric + 1) * graph->get_size().x / (2 * frame_metrics.size()) + 1;468graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), theme_cache.seek_line_hover_color);469}470}471472void EditorProfiler::_graph_tex_mouse_exit() {473hover_metric = -1;474graph->queue_redraw();475}476477void EditorProfiler::_cursor_metric_changed(double) {478if (updating_frame) {479return;480}481482graph->queue_redraw();483_update_frame();484}485486void EditorProfiler::_graph_tex_input(const Ref<InputEvent> &p_ev) {487if (last_metric < 0) {488return;489}490491Ref<InputEventMouse> me = p_ev;492Ref<InputEventMouseButton> mb = p_ev;493Ref<InputEventMouseMotion> mm = p_ev;494MouseButton button_idx = mb.is_valid() ? mb->get_button_index() : MouseButton();495496if (497(mb.is_valid() && button_idx == MouseButton::LEFT && mb->is_pressed()) ||498(mm.is_valid())) {499int x = me->get_position().x - 1;500hover_metric = x * frame_metrics.size() / graph->get_size().width;501502x = x * frame_metrics.size() / graph->get_size().width;503x = x / Math::exp(graph_zoom) + _get_zoom_left_border();504x = CLAMP(x, 0, frame_metrics.size() - 1);505506if (mb.is_valid() || (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {507updating_frame = true;508509if (x < total_metrics) {510cursor_metric_edit->set_value(_get_frame_metric(x).frame_number);511}512updating_frame = false;513514if (activate->is_pressed()) {515if (!seeking) {516emit_signal(SNAME("break_request"));517}518}519520seeking = true;521522if (!frame_delay->is_processing()) {523frame_delay->set_wait_time(0.1);524frame_delay->start();525}526}527}528529if (graph_zoom > 0 && mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::MIDDLE) || mm->get_button_mask().has_flag(MouseButtonMask::RIGHT))) {530// Panning.531const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);532pan_accumulator += (float)mm->get_relative().x * max_profiles_shown / graph->get_size().width;533534if (Math::abs(pan_accumulator) > 1) {535zoom_center = CLAMP(zoom_center - (int)pan_accumulator, max_profiles_shown / 2, frame_metrics.size() - max_profiles_shown / 2);536pan_accumulator -= (int)pan_accumulator;537_update_plot();538}539}540541if (button_idx == MouseButton::WHEEL_DOWN) {542// Zooming.543graph_zoom = MAX(-0.05 + graph_zoom, 0);544_update_plot();545} else if (button_idx == MouseButton::WHEEL_UP) {546if (graph_zoom == 0) {547zoom_center = me->get_position().x;548zoom_center = zoom_center * frame_metrics.size() / graph->get_size().width;549}550graph_zoom = MIN(0.05 + graph_zoom, 2);551_update_plot();552}553554graph->queue_redraw();555}556557void EditorProfiler::disable_seeking() {558seeking = false;559graph->queue_redraw();560}561562void EditorProfiler::_combo_changed(int) {563_update_frame();564_update_plot();565}566567void EditorProfiler::_bind_methods() {568ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));569ADD_SIGNAL(MethodInfo("break_request"));570}571572void EditorProfiler::set_enabled(bool p_enable, bool p_clear) {573activate->set_disabled(!p_enable);574if (p_clear) {575clear();576}577}578579void EditorProfiler::set_profiling(bool p_pressed) {580activate->set_pressed(p_pressed);581_update_button_text();582emit_signal(SNAME("enable_profiling"), activate->is_pressed());583}584585bool EditorProfiler::is_profiling() {586return activate->is_pressed();587}588589Vector<Vector<String>> EditorProfiler::get_data_as_csv() const {590Vector<Vector<String>> res;591592if (frame_metrics.is_empty()) {593return res;594}595596// Different metrics may contain different number of categories.597HashSet<StringName> possible_signatures;598for (int i = 0; i < frame_metrics.size(); i++) {599const Metric &m = frame_metrics[i];600if (!m.valid) {601continue;602}603for (const KeyValue<StringName, Metric::Category *> &E : m.category_ptrs) {604possible_signatures.insert(E.key);605}606for (const KeyValue<StringName, Metric::Category::Item *> &E : m.item_ptrs) {607possible_signatures.insert(E.key);608}609}610611// Generate CSV header and cache indices.612HashMap<StringName, int> sig_map;613Vector<String> signatures;614signatures.resize(possible_signatures.size());615int sig_index = 0;616for (const StringName &E : possible_signatures) {617signatures.write[sig_index] = E;618sig_map[E] = sig_index;619sig_index++;620}621res.push_back(signatures);622623// values624Vector<String> values;625626int index = last_metric;627628for (int i = 0; i < frame_metrics.size(); i++) {629++index;630631if (index >= frame_metrics.size()) {632index = 0;633}634635const Metric &m = frame_metrics[index];636637if (!m.valid) {638continue;639}640641// Don't keep old values since there may be empty cells.642values.clear();643values.resize(possible_signatures.size());644645for (const KeyValue<StringName, Metric::Category *> &E : m.category_ptrs) {646values.write[sig_map[E.key]] = String::num_real(E.value->total_time);647}648for (const KeyValue<StringName, Metric::Category::Item *> &E : m.item_ptrs) {649values.write[sig_map[E.key]] = String::num_real(E.value->total);650}651652res.push_back(values);653}654655return res;656}657658EditorProfiler::EditorProfiler() {659HBoxContainer *hb = memnew(HBoxContainer);660hb->add_theme_constant_override(SNAME("separation"), 8 * EDSCALE);661add_child(hb);662663FlowContainer *container = memnew(FlowContainer);664container->set_h_size_flags(SIZE_EXPAND_FILL);665container->add_theme_constant_override(SNAME("h_separation"), 8 * EDSCALE);666container->add_theme_constant_override(SNAME("v_separation"), 2 * EDSCALE);667hb->add_child(container);668669activate = memnew(Button);670activate->set_toggle_mode(true);671activate->set_disabled(true);672activate->set_text(TTR("Start"));673activate->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_activate_pressed));674container->add_child(activate);675676clear_button = memnew(Button);677clear_button->set_text(TTR("Clear"));678clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_clear_pressed));679clear_button->set_disabled(true);680container->add_child(clear_button);681682CheckBox *autostart_checkbox = memnew(CheckBox);683autostart_checkbox->set_text(TTR("Autostart"));684autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_profiler", false));685autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorProfiler::_autostart_toggled));686container->add_child(autostart_checkbox);687688HBoxContainer *hb_measure = memnew(HBoxContainer);689hb_measure->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);690container->add_child(hb_measure);691692hb_measure->add_child(memnew(Label(TTR("Measure:"))));693694display_mode = memnew(OptionButton);695display_mode->set_accessibility_name(TTRC("Measure:"));696display_mode->add_item(TTR("Frame Time (ms)"));697display_mode->add_item(TTR("Average Time (ms)"));698display_mode->add_item(TTR("Frame %"));699display_mode->add_item(TTR("Physics Frame %"));700display_mode->connect(SceneStringName(item_selected), callable_mp(this, &EditorProfiler::_combo_changed));701702hb_measure->add_child(display_mode);703704HBoxContainer *hb_time = memnew(HBoxContainer);705hb_time->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);706container->add_child(hb_time);707708hb_time->add_child(memnew(Label(TTR("Time:"))));709710display_time = memnew(OptionButton);711display_time->set_accessibility_name(TTRC("Time:"));712// TRANSLATORS: This is an option in the profiler to display the time spent in a function, including the time spent in other functions called by that function.713display_time->add_item(TTR("Inclusive"));714// TRANSLATORS: This is an option in the profiler to display the time spent in a function, exincluding the time spent in other functions called by that function.715display_time->add_item(TTR("Self"));716display_time->set_tooltip_text(TTR("Inclusive: Includes time from other functions called by this function.\nUse this to spot bottlenecks.\n\nSelf: Only count the time spent in the function itself, not in other functions called by that function.\nUse this to find individual functions to optimize."));717display_time->connect(SceneStringName(item_selected), callable_mp(this, &EditorProfiler::_combo_changed));718hb_time->add_child(display_time);719720display_internal_profiles = memnew(CheckButton(TTR("Display internal functions")));721display_internal_profiles->set_visible(EDITOR_GET("debugger/profile_native_calls"));722display_internal_profiles->set_pressed(false);723display_internal_profiles->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_internal_profiles_pressed));724container->add_child(display_internal_profiles);725726HBoxContainer *hb_frame = memnew(HBoxContainer);727hb_frame->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);728hb_frame->set_v_size_flags(SIZE_SHRINK_BEGIN);729hb->add_child(hb_frame);730731hb_frame->add_child(memnew(Label(TTR("Frame #:"))));732733cursor_metric_edit = memnew(SpinBox);734cursor_metric_edit->set_accessibility_name(TTRC("Frame #:"));735cursor_metric_edit->set_h_size_flags(SIZE_FILL);736cursor_metric_edit->set_value(0);737cursor_metric_edit->set_editable(false);738hb_frame->add_child(cursor_metric_edit);739cursor_metric_edit->connect(SceneStringName(value_changed), callable_mp(this, &EditorProfiler::_cursor_metric_changed));740741h_split = memnew(HSplitContainer);742add_child(h_split);743h_split->set_v_size_flags(SIZE_EXPAND_FILL);744745variables = memnew(Tree);746variables->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);747variables->set_custom_minimum_size(Size2(320, 0) * EDSCALE);748variables->set_hide_folding(true);749h_split->add_child(variables);750variables->set_hide_root(true);751variables->set_columns(3);752variables->set_column_titles_visible(true);753variables->set_column_title(0, TTR("Name"));754variables->set_column_expand(0, true);755variables->set_column_clip_content(0, true);756variables->set_column_custom_minimum_width(0, 60);757variables->set_column_title(1, TTR("Time"));758variables->set_column_expand(1, false);759variables->set_column_clip_content(1, true);760variables->set_column_custom_minimum_width(1, 75 * EDSCALE);761variables->set_column_title(2, TTR("Calls"));762variables->set_column_expand(2, false);763variables->set_column_clip_content(2, true);764variables->set_column_custom_minimum_width(2, 50 * EDSCALE);765variables->set_theme_type_variation("TreeSecondary");766variables->connect("item_edited", callable_mp(this, &EditorProfiler::_item_edited));767768graph = memnew(TextureRect);769graph->set_custom_minimum_size(Size2(250 * EDSCALE, 0));770graph->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);771graph->set_mouse_filter(MOUSE_FILTER_STOP);772graph->connect(SceneStringName(draw), callable_mp(this, &EditorProfiler::_graph_tex_draw));773graph->connect(SceneStringName(gui_input), callable_mp(this, &EditorProfiler::_graph_tex_input));774graph->connect(SceneStringName(mouse_exited), callable_mp(this, &EditorProfiler::_graph_tex_mouse_exit));775776h_split->add_child(graph);777graph->set_h_size_flags(SIZE_EXPAND_FILL);778779int metric_size = CLAMP(int(EDITOR_GET("debugger/profiler_frame_history_size")), 60, 10000);780frame_metrics.resize(metric_size);781782frame_delay = memnew(Timer);783frame_delay->set_wait_time(0.1);784frame_delay->set_one_shot(true);785add_child(frame_delay);786frame_delay->connect("timeout", callable_mp(this, &EditorProfiler::_update_frame));787788plot_delay = memnew(Timer);789plot_delay->set_wait_time(0.1);790plot_delay->set_one_shot(true);791add_child(plot_delay);792plot_delay->connect("timeout", callable_mp(this, &EditorProfiler::_update_plot));793794plot_sigs.insert("physics_frame_time");795plot_sigs.insert("category_frame_time");796}797798799