Path: blob/master/editor/debugger/editor_profiler.cpp
20816 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 "core/string/translation_server.h"34#include "editor/editor_string_names.h"35#include "editor/run/editor_run_bar.h"36#include "editor/settings/editor_settings.h"37#include "editor/themes/editor_scale.h"38#include "scene/gui/check_box.h"39#include "scene/gui/flow_container.h"40#include "scene/resources/image_texture.h"4142void EditorProfiler::_make_metric_ptrs(Metric &m) {43for (int i = 0; i < m.categories.size(); i++) {44m.category_ptrs[m.categories[i].signature] = &m.categories.write[i];45for (int j = 0; j < m.categories[i].items.size(); j++) {46m.item_ptrs[m.categories[i].items[j].signature] = &m.categories.write[i].items.write[j];47}48}49}5051EditorProfiler::Metric EditorProfiler::_get_frame_metric(int index) {52return frame_metrics[(frame_metrics.size() + last_metric - (total_metrics - 1) + index) % frame_metrics.size()];53}5455void EditorProfiler::add_frame_metric(const Metric &p_metric, bool p_final) {56++last_metric;57if (last_metric >= frame_metrics.size()) {58last_metric = 0;59}6061total_metrics++;62if (total_metrics > frame_metrics.size()) {63total_metrics = frame_metrics.size();64}6566frame_metrics.write[last_metric] = p_metric;67_make_metric_ptrs(frame_metrics.write[last_metric]);6869updating_frame = true;70clear_button->set_disabled(false);71cursor_metric_edit->set_editable(true);72cursor_metric_edit->set_max(p_metric.frame_number);73cursor_metric_edit->set_min(_get_frame_metric(0).frame_number);7475if (!seeking) {76cursor_metric_edit->set_value(p_metric.frame_number);77}7879updating_frame = false;8081if (frame_delay->is_stopped()) {82frame_delay->set_wait_time(p_final ? 0.1 : 1);83frame_delay->start();84}8586if (plot_delay->is_stopped()) {87plot_delay->set_wait_time(0.1);88plot_delay->start();89}90}9192void EditorProfiler::clear() {93int metric_size = EDITOR_GET("debugger/profiler_frame_history_size");94metric_size = CLAMP(metric_size, 60, 10000);95frame_metrics.clear();96frame_metrics.resize(metric_size);97total_metrics = 0;98last_metric = -1;99variables->clear();100plot_sigs.clear();101plot_sigs.insert("physics_frame_time");102plot_sigs.insert("category_frame_time");103display_internal_profiles->set_visible(EDITOR_GET("debugger/profile_native_calls"));104105updating_frame = true;106cursor_metric_edit->set_min(0);107cursor_metric_edit->set_max(100); // Doesn't make much sense, but we can't have min == max. Doesn't hurt.108cursor_metric_edit->set_value(0);109cursor_metric_edit->set_editable(false);110updating_frame = false;111hover_metric = -1;112seeking = false;113114// Ensure button text (start, stop) is correct115_update_button_text();116emit_signal(SNAME("enable_profiling"), activate->is_pressed());117}118119String EditorProfiler::_get_time_as_text(const Metric &m, float p_time, int p_calls) {120const String &lang = _get_locale();121const TranslationServer *ts = TranslationServer::get_singleton();122123switch (display_mode->get_selected()) {124case DISPLAY_FRAME_TIME: {125return ts->format_number(rtos(p_time * 1000).pad_decimals(2), lang) + " " + TTR("ms");126} break;127128case DISPLAY_AVERAGE_TIME: {129if (p_calls == 0) {130return ts->format_number("0.00", lang) + " " + TTR("ms");131}132return ts->format_number(rtos((p_time / p_calls) * 1000).pad_decimals(2), lang) + " " + TTR("ms");133} break;134135case DISPLAY_FRAME_PERCENT: {136float total = m.frame_time == 0 ? 0.00001 : m.frame_time;137return ts->format_number(String::num((p_time / total) * 100, 1), lang) + ts->get_percent_sign(lang);138} break;139140case DISPLAY_PHYSICS_FRAME_PERCENT: {141float total = m.physics_frame_time == 0 ? 0.00001 : m.physics_frame_time;142return ts->format_number(String::num((p_time / total) * 100, 1), lang) + ts->get_percent_sign(lang);143} break;144}145146return "err";147}148149Color EditorProfiler::_get_color_from_signature(const StringName &p_signature) const {150Color bc = get_theme_color(SNAME("error_color"), EditorStringName(Editor));151double rot = Math::abs(double(p_signature.hash()) / double(0x7FFFFFFF));152Color c;153c.set_hsv(rot, bc.get_s(), bc.get_v());154return c.lerp(get_theme_color(SNAME("base_color"), EditorStringName(Editor)), 0.07);155}156157int EditorProfiler::_get_zoom_left_border() const {158const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);159return CLAMP(zoom_center - max_profiles_shown / 2, 0, frame_metrics.size() - max_profiles_shown);160}161162void EditorProfiler::_item_edited() {163if (updating_frame) {164return;165}166167TreeItem *item = variables->get_edited();168if (!item) {169return;170}171StringName signature = item->get_metadata(0);172bool checked = item->is_checked(0);173174if (checked) {175plot_sigs.insert(signature);176} else {177plot_sigs.erase(signature);178}179180if (!frame_delay->is_processing()) {181frame_delay->set_wait_time(0.1);182frame_delay->start();183}184185_update_plot();186}187188void EditorProfiler::_update_plot() {189const int w = MAX(1, graph->get_size().width); // Clamp to 1 to prevent from crashing when profiler is autostarted.190const int h = MAX(1, graph->get_size().height);191bool reset_texture = false;192const int desired_len = w * h * 4;193194if (graph_image.size() != desired_len) {195reset_texture = true;196graph_image.resize(desired_len);197}198199uint8_t *wr = graph_image.ptrw();200const Color background_color = get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor));201202// Clear the previous frame and set the background color.203for (int i = 0; i < desired_len; i += 4) {204wr[i + 0] = Math::fast_ftoi(background_color.r * 255);205wr[i + 1] = Math::fast_ftoi(background_color.g * 255);206wr[i + 2] = Math::fast_ftoi(background_color.b * 255);207wr[i + 3] = 255;208}209210//find highest value211212const bool use_self = display_time->get_selected() == DISPLAY_SELF_TIME;213float highest = 0;214215for (int i = 0; i < total_metrics; i++) {216const Metric &m = _get_frame_metric(i);217218for (const StringName &E : plot_sigs) {219HashMap<StringName, Metric::Category *>::ConstIterator F = m.category_ptrs.find(E);220if (F) {221highest = MAX(F->value->total_time, highest);222}223224HashMap<StringName, Metric::Category::Item *>::ConstIterator G = m.item_ptrs.find(E);225if (G) {226if (use_self) {227highest = MAX(G->value->self, highest);228} else {229highest = MAX(G->value->total, highest);230}231}232}233}234235if (highest > 0) {236//means some data exists..237highest *= 1.2; //leave some upper room238graph_height = highest;239240Vector<int> columnv;241columnv.resize(h * 4);242243int *column = columnv.ptrw();244245HashMap<StringName, int> prev_plots;246247const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);248const int left_border = _get_zoom_left_border();249const int profiles_drawn = CLAMP(total_metrics - left_border, 0, max_profiles_shown);250const int pixel_cols = (profiles_drawn * w) / max_profiles_shown - 1;251252for (int i = 0; i < pixel_cols; i++) {253for (int j = 0; j < h * 4; j++) {254column[j] = 0;255}256257int current = (i * max_profiles_shown / w) + left_border;258259for (const StringName &E : plot_sigs) {260const Metric &m = _get_frame_metric(current);261262float value = 0;263264HashMap<StringName, Metric::Category *>::ConstIterator F = m.category_ptrs.find(E);265if (F) {266value = F->value->total_time;267}268269HashMap<StringName, Metric::Category::Item *>::ConstIterator G = m.item_ptrs.find(E);270if (G) {271if (use_self) {272value = G->value->self;273} else {274value = G->value->total;275}276}277278int plot_pos = CLAMP(int(value * h / highest), 0, h - 1);279280int prev_plot = plot_pos;281HashMap<StringName, int>::Iterator H = prev_plots.find(E);282if (H) {283prev_plot = H->value;284H->value = plot_pos;285} else {286prev_plots[E] = plot_pos;287}288289plot_pos = h - plot_pos - 1;290prev_plot = h - prev_plot - 1;291292if (prev_plot > plot_pos) {293SWAP(prev_plot, plot_pos);294}295296Color col = _get_color_from_signature(E);297298for (int j = prev_plot; j <= plot_pos; j++) {299column[j * 4 + 0] += Math::fast_ftoi(CLAMP(col.r * 255, 0, 255));300column[j * 4 + 1] += Math::fast_ftoi(CLAMP(col.g * 255, 0, 255));301column[j * 4 + 2] += Math::fast_ftoi(CLAMP(col.b * 255, 0, 255));302column[j * 4 + 3] += 1;303}304}305306for (int j = 0; j < h * 4; j += 4) {307const int a = column[j + 3];308if (a > 0) {309column[j + 0] /= a;310column[j + 1] /= a;311column[j + 2] /= a;312}313314const uint8_t red = uint8_t(column[j + 0]);315const uint8_t green = uint8_t(column[j + 1]);316const uint8_t blue = uint8_t(column[j + 2]);317const bool is_filled = red >= 1 || green >= 1 || blue >= 1;318const int widx = ((j >> 2) * w + i) * 4;319320// If the pixel isn't filled by any profiler line, apply the background color instead.321wr[widx + 0] = is_filled ? red : Math::fast_ftoi(background_color.r * 255);322wr[widx + 1] = is_filled ? green : Math::fast_ftoi(background_color.g * 255);323wr[widx + 2] = is_filled ? blue : Math::fast_ftoi(background_color.b * 255);324wr[widx + 3] = 255;325}326}327}328329Ref<Image> img = Image::create_from_data(w, h, false, Image::FORMAT_RGBA8, graph_image);330331if (reset_texture) {332if (graph_texture.is_null()) {333graph_texture.instantiate();334}335graph_texture->set_image(img);336}337338graph_texture->update(img);339340graph->set_texture(graph_texture);341graph->queue_redraw();342}343344void EditorProfiler::_update_frame() {345int cursor_metric = cursor_metric_edit->get_value() - _get_frame_metric(0).frame_number;346347updating_frame = true;348variables->clear();349350TreeItem *root = variables->create_item();351const Metric &m = _get_frame_metric(cursor_metric);352353int dtime = display_time->get_selected();354355for (int i = 0; i < m.categories.size(); i++) {356TreeItem *category = variables->create_item(root);357category->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);358category->set_editable(0, true);359category->set_metadata(0, m.categories[i].signature);360category->set_text(0, String(m.categories[i].name));361category->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED);362category->set_text(1, _get_time_as_text(m, m.categories[i].total_time, 1));363364if (plot_sigs.has(m.categories[i].signature)) {365category->set_checked(0, true);366category->set_custom_color(0, _get_color_from_signature(m.categories[i].signature));367}368369for (int j = 0; j < m.categories[i].items.size(); j++) {370const Metric::Category::Item &it = m.categories[i].items[j];371372if (it.internal == it.total && !display_internal_profiles->is_pressed() && m.categories[i].name == "Script Functions") {373continue;374}375TreeItem *item = variables->create_item(category);376item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);377item->set_editable(0, true);378item->set_text(0, it.name);379item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED);380item->set_metadata(0, it.signature);381item->set_metadata(1, it.script);382item->set_metadata(2, it.line);383item->set_text_alignment(2, HORIZONTAL_ALIGNMENT_RIGHT);384item->set_tooltip_text(0, it.name + "\n" + it.script + ":" + itos(it.line));385386float time = dtime == DISPLAY_SELF_TIME ? it.self : it.total;387if (dtime == DISPLAY_SELF_TIME && !display_internal_profiles->is_pressed()) {388time += it.internal;389}390391item->set_text(1, _get_time_as_text(m, time, it.calls));392393item->set_text(2, itos(it.calls));394395if (plot_sigs.has(it.signature)) {396item->set_checked(0, true);397item->set_custom_color(0, _get_color_from_signature(it.signature));398}399}400}401402updating_frame = false;403}404405void EditorProfiler::_update_button_text() {406if (activate->is_pressed()) {407activate->set_button_icon(get_editor_theme_icon(SNAME("Stop")));408activate->set_text(TTRC("Stop"));409} else {410activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));411activate->set_text(TTRC("Start"));412}413}414415void EditorProfiler::_activate_pressed() {416_update_button_text();417418if (activate->is_pressed()) {419_clear_pressed();420}421422emit_signal(SNAME("enable_profiling"), activate->is_pressed());423}424425void EditorProfiler::_clear_pressed() {426clear_button->set_disabled(true);427clear();428_update_plot();429}430431void EditorProfiler::_internal_profiles_pressed() {432_combo_changed(0);433}434435void EditorProfiler::_autostart_toggled(bool p_toggled_on) {436EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_profiler", p_toggled_on);437EditorRunBar::get_singleton()->update_profiler_autostart_indicator();438}439440void EditorProfiler::_notification(int p_what) {441switch (p_what) {442case NOTIFICATION_TRANSLATION_CHANGED: {443if (is_ready()) {444_update_frame();445}446[[fallthrough]];447}448case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:449case NOTIFICATION_THEME_CHANGED: {450activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));451clear_button->set_button_icon(get_editor_theme_icon(SNAME("Clear")));452453theme_cache.seek_line_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));454theme_cache.seek_line_color.a = 0.8;455theme_cache.seek_line_hover_color = theme_cache.seek_line_color;456theme_cache.seek_line_hover_color.a = 0.4;457458if (total_metrics > 0) {459_update_plot();460}461} break;462}463}464465void EditorProfiler::_graph_tex_draw() {466if (total_metrics == 0) {467return;468}469if (seeking) {470int frame = cursor_metric_edit->get_value() - _get_frame_metric(0).frame_number;471frame = frame - _get_zoom_left_border() + 1;472int cur_x = (frame * graph->get_size().width * Math::exp(graph_zoom)) / frame_metrics.size();473cur_x = CLAMP(cur_x, 0, graph->get_size().width);474graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), theme_cache.seek_line_color);475}476if (hover_metric > -1) {477int cur_x = (2 * hover_metric + 1) * graph->get_size().x / (2 * frame_metrics.size()) + 1;478graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), theme_cache.seek_line_hover_color);479}480}481482void EditorProfiler::_graph_tex_mouse_exit() {483hover_metric = -1;484graph->queue_redraw();485}486487void EditorProfiler::_cursor_metric_changed(double) {488if (updating_frame) {489return;490}491492graph->queue_redraw();493_update_frame();494}495496void EditorProfiler::_graph_tex_input(const Ref<InputEvent> &p_ev) {497if (last_metric < 0) {498return;499}500501Ref<InputEventMouse> me = p_ev;502Ref<InputEventMouseButton> mb = p_ev;503Ref<InputEventMouseMotion> mm = p_ev;504MouseButton button_idx = mb.is_valid() ? mb->get_button_index() : MouseButton();505506if (507(mb.is_valid() && button_idx == MouseButton::LEFT && mb->is_pressed()) ||508(mm.is_valid())) {509int x = me->get_position().x - 1;510hover_metric = x * frame_metrics.size() / graph->get_size().width;511512x = x * frame_metrics.size() / graph->get_size().width;513x = x / Math::exp(graph_zoom) + _get_zoom_left_border();514x = CLAMP(x, 0, frame_metrics.size() - 1);515516if (mb.is_valid() || (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {517updating_frame = true;518519if (x < total_metrics) {520cursor_metric_edit->set_value(_get_frame_metric(x).frame_number);521}522updating_frame = false;523524if (activate->is_pressed()) {525if (!seeking) {526emit_signal(SNAME("break_request"));527}528}529530seeking = true;531532if (!frame_delay->is_processing()) {533frame_delay->set_wait_time(0.1);534frame_delay->start();535}536}537}538539if (graph_zoom > 0 && mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::MIDDLE) || mm->get_button_mask().has_flag(MouseButtonMask::RIGHT))) {540// Panning.541const int max_profiles_shown = frame_metrics.size() / Math::exp(graph_zoom);542pan_accumulator += (float)mm->get_relative().x * max_profiles_shown / graph->get_size().width;543544if (Math::abs(pan_accumulator) > 1) {545zoom_center = CLAMP(zoom_center - (int)pan_accumulator, max_profiles_shown / 2, frame_metrics.size() - max_profiles_shown / 2);546pan_accumulator -= (int)pan_accumulator;547_update_plot();548}549}550551if (button_idx == MouseButton::WHEEL_DOWN) {552// Zooming.553graph_zoom = MAX(-0.05 + graph_zoom, 0);554_update_plot();555} else if (button_idx == MouseButton::WHEEL_UP) {556if (graph_zoom == 0) {557zoom_center = me->get_position().x;558zoom_center = zoom_center * frame_metrics.size() / graph->get_size().width;559}560graph_zoom = MIN(0.05 + graph_zoom, 2);561_update_plot();562}563564graph->queue_redraw();565}566567void EditorProfiler::disable_seeking() {568seeking = false;569graph->queue_redraw();570}571572void EditorProfiler::_combo_changed(int) {573_update_frame();574_update_plot();575}576577void EditorProfiler::_bind_methods() {578ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));579ADD_SIGNAL(MethodInfo("break_request"));580}581582void EditorProfiler::set_enabled(bool p_enable, bool p_clear) {583activate->set_disabled(!p_enable);584if (p_clear) {585clear();586}587}588589void EditorProfiler::set_profiling(bool p_pressed) {590activate->set_pressed(p_pressed);591_update_button_text();592emit_signal(SNAME("enable_profiling"), activate->is_pressed());593}594595bool EditorProfiler::is_profiling() {596return activate->is_pressed();597}598599Vector<Vector<String>> EditorProfiler::get_data_as_csv() const {600Vector<Vector<String>> res;601602if (frame_metrics.is_empty()) {603return res;604}605606// Different metrics may contain different number of categories.607HashSet<StringName> possible_signatures;608for (int i = 0; i < frame_metrics.size(); i++) {609const Metric &m = frame_metrics[i];610if (!m.valid) {611continue;612}613for (const KeyValue<StringName, Metric::Category *> &E : m.category_ptrs) {614possible_signatures.insert(E.key);615}616for (const KeyValue<StringName, Metric::Category::Item *> &E : m.item_ptrs) {617possible_signatures.insert(E.key);618}619}620621// Generate CSV header and cache indices.622HashMap<StringName, int> sig_map;623Vector<String> signatures;624signatures.resize(possible_signatures.size());625int sig_index = 0;626for (const StringName &E : possible_signatures) {627signatures.write[sig_index] = E;628sig_map[E] = sig_index;629sig_index++;630}631res.push_back(signatures);632633// values634Vector<String> values;635636int index = last_metric;637638for (int i = 0; i < frame_metrics.size(); i++) {639++index;640641if (index >= frame_metrics.size()) {642index = 0;643}644645const Metric &m = frame_metrics[index];646647if (!m.valid) {648continue;649}650651// Don't keep old values since there may be empty cells.652values.clear();653values.resize(possible_signatures.size());654655for (const KeyValue<StringName, Metric::Category *> &E : m.category_ptrs) {656values.write[sig_map[E.key]] = String::num_real(E.value->total_time);657}658for (const KeyValue<StringName, Metric::Category::Item *> &E : m.item_ptrs) {659values.write[sig_map[E.key]] = String::num_real(E.value->total);660}661662res.push_back(values);663}664665return res;666}667668EditorProfiler::EditorProfiler() {669HBoxContainer *hb = memnew(HBoxContainer);670hb->add_theme_constant_override(SNAME("separation"), 8 * EDSCALE);671add_child(hb);672673FlowContainer *container = memnew(FlowContainer);674container->set_h_size_flags(SIZE_EXPAND_FILL);675container->add_theme_constant_override(SNAME("h_separation"), 8 * EDSCALE);676container->add_theme_constant_override(SNAME("v_separation"), 2 * EDSCALE);677hb->add_child(container);678679activate = memnew(Button);680activate->set_toggle_mode(true);681activate->set_disabled(true);682activate->set_text(TTRC("Start"));683activate->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_activate_pressed));684container->add_child(activate);685686clear_button = memnew(Button);687clear_button->set_text(TTRC("Clear"));688clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_clear_pressed));689clear_button->set_disabled(true);690container->add_child(clear_button);691692CheckBox *autostart_checkbox = memnew(CheckBox);693autostart_checkbox->set_text(TTRC("Autostart"));694autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_profiler", false));695autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorProfiler::_autostart_toggled));696container->add_child(autostart_checkbox);697698HBoxContainer *hb_measure = memnew(HBoxContainer);699hb_measure->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);700container->add_child(hb_measure);701702hb_measure->add_child(memnew(Label(TTRC("Measure:"))));703704display_mode = memnew(OptionButton);705display_mode->set_accessibility_name(TTRC("Measure:"));706display_mode->add_item(TTRC("Frame Time (ms)"));707display_mode->add_item(TTRC("Average Time (ms)"));708display_mode->add_item(TTRC("Frame %"));709display_mode->add_item(TTRC("Physics Frame %"));710display_mode->connect(SceneStringName(item_selected), callable_mp(this, &EditorProfiler::_combo_changed));711712hb_measure->add_child(display_mode);713714HBoxContainer *hb_time = memnew(HBoxContainer);715hb_time->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);716container->add_child(hb_time);717718hb_time->add_child(memnew(Label(TTRC("Time:"))));719720display_time = memnew(OptionButton);721display_time->set_accessibility_name(TTRC("Time:"));722// 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.723display_time->add_item(TTRC("Inclusive"));724// 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.725display_time->add_item(TTRC("Self"));726display_time->set_tooltip_text(TTRC("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."));727display_time->connect(SceneStringName(item_selected), callable_mp(this, &EditorProfiler::_combo_changed));728hb_time->add_child(display_time);729730display_internal_profiles = memnew(CheckButton(TTRC("Display internal functions")));731display_internal_profiles->set_visible(EDITOR_GET("debugger/profile_native_calls"));732display_internal_profiles->set_pressed(false);733display_internal_profiles->connect(SceneStringName(pressed), callable_mp(this, &EditorProfiler::_internal_profiles_pressed));734container->add_child(display_internal_profiles);735736HBoxContainer *hb_frame = memnew(HBoxContainer);737hb_frame->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);738hb_frame->set_v_size_flags(SIZE_SHRINK_BEGIN);739hb->add_child(hb_frame);740741hb_frame->add_child(memnew(Label(TTRC("Frame #:"))));742743cursor_metric_edit = memnew(SpinBox);744cursor_metric_edit->set_accessibility_name(TTRC("Frame #:"));745cursor_metric_edit->set_h_size_flags(SIZE_FILL);746cursor_metric_edit->set_value(0);747cursor_metric_edit->set_editable(false);748hb_frame->add_child(cursor_metric_edit);749cursor_metric_edit->connect(SceneStringName(value_changed), callable_mp(this, &EditorProfiler::_cursor_metric_changed));750751h_split = memnew(HSplitContainer);752add_child(h_split);753h_split->set_v_size_flags(SIZE_EXPAND_FILL);754755variables = memnew(Tree);756variables->set_custom_minimum_size(Size2(320, 0) * EDSCALE);757variables->set_hide_folding(true);758h_split->add_child(variables);759variables->set_hide_root(true);760variables->set_columns(3);761variables->set_column_titles_visible(true);762variables->set_column_title(0, TTRC("Name"));763variables->set_column_expand(0, true);764variables->set_column_clip_content(0, true);765variables->set_column_custom_minimum_width(0, 60);766variables->set_column_title(1, TTRC("Time"));767variables->set_column_expand(1, false);768variables->set_column_clip_content(1, true);769variables->set_column_custom_minimum_width(1, 75 * EDSCALE);770variables->set_column_title(2, TTRC("Calls"));771variables->set_column_expand(2, false);772variables->set_column_clip_content(2, true);773variables->set_column_custom_minimum_width(2, 50 * EDSCALE);774variables->set_theme_type_variation("TreeSecondary");775variables->connect("item_edited", callable_mp(this, &EditorProfiler::_item_edited));776777graph = memnew(TextureRect);778graph->set_custom_minimum_size(Size2(250 * EDSCALE, 0));779graph->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);780graph->set_mouse_filter(MOUSE_FILTER_STOP);781graph->connect(SceneStringName(draw), callable_mp(this, &EditorProfiler::_graph_tex_draw));782graph->connect(SceneStringName(gui_input), callable_mp(this, &EditorProfiler::_graph_tex_input));783graph->connect(SceneStringName(mouse_exited), callable_mp(this, &EditorProfiler::_graph_tex_mouse_exit));784785h_split->add_child(graph);786graph->set_h_size_flags(SIZE_EXPAND_FILL);787788int metric_size = CLAMP(int(EDITOR_GET("debugger/profiler_frame_history_size")), 60, 10000);789frame_metrics.resize(metric_size);790791frame_delay = memnew(Timer);792frame_delay->set_wait_time(0.1);793frame_delay->set_one_shot(true);794add_child(frame_delay);795frame_delay->connect("timeout", callable_mp(this, &EditorProfiler::_update_frame));796797plot_delay = memnew(Timer);798plot_delay->set_wait_time(0.1);799plot_delay->set_one_shot(true);800add_child(plot_delay);801plot_delay->connect("timeout", callable_mp(this, &EditorProfiler::_update_plot));802803plot_sigs.insert("physics_frame_time");804plot_sigs.insert("category_frame_time");805}806807808