Path: blob/master/editor/debugger/editor_visual_profiler.cpp
20886 views
/**************************************************************************/1/* editor_visual_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_visual_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/flow_container.h"39#include "scene/resources/image_texture.h"4041void EditorVisualProfiler::set_hardware_info(const String &p_cpu_name, const String &p_gpu_name) {42cpu_name = p_cpu_name;43gpu_name = p_gpu_name;44queue_redraw();45}4647void EditorVisualProfiler::add_frame_metric(const Metric &p_metric) {48++last_metric;49if (last_metric >= frame_metrics.size()) {50last_metric = 0;51}5253frame_metrics.write[last_metric] = p_metric;5455List<String> stack;56for (int i = 0; i < frame_metrics[last_metric].areas.size(); i++) {57String name = frame_metrics[last_metric].areas[i].name;58frame_metrics.write[last_metric].areas.write[i].color_cache = _get_color_from_signature(name);59String full_name;6061if (name[0] == '<') {62stack.pop_back();63}6465if (stack.size()) {66full_name = stack.back()->get() + name;67} else {68full_name = name;69}7071if (name[0] == '>') {72stack.push_back(full_name + "/");73}7475frame_metrics.write[last_metric].areas.write[i].fullpath_cache = full_name;76}7778updating_frame = true;79clear_button->set_disabled(false);80cursor_metric_edit->set_max(frame_metrics[last_metric].frame_number);81cursor_metric_edit->set_min(MAX(int64_t(frame_metrics[last_metric].frame_number) - frame_metrics.size(), 0));8283if (!seeking) {84cursor_metric_edit->set_value(frame_metrics[last_metric].frame_number);85if (hover_metric != -1) {86hover_metric++;87if (hover_metric >= frame_metrics.size()) {88hover_metric = 0;89}90}91}92updating_frame = false;9394if (frame_delay->is_stopped()) {95frame_delay->set_wait_time(0.1);96frame_delay->start();97}9899if (plot_delay->is_stopped()) {100plot_delay->set_wait_time(0.1);101plot_delay->start();102}103}104105void EditorVisualProfiler::clear() {106int metric_size = EDITOR_GET("debugger/profiler_frame_history_size");107metric_size = CLAMP(metric_size, 60, 10000);108frame_metrics.clear();109frame_metrics.resize(metric_size);110last_metric = -1;111variables->clear();112//activate->set_pressed(false);113114graph_limit = 1000.0f / CLAMP(int(EDITOR_GET("debugger/profiler_target_fps")), 1, 1000);115116updating_frame = true;117cursor_metric_edit->set_min(0);118cursor_metric_edit->set_max(0);119cursor_metric_edit->set_value(0);120updating_frame = false;121hover_metric = -1;122seeking = false;123}124125String EditorVisualProfiler::_get_time_as_text(float p_time) {126const String &lang = _get_locale();127128int dmode = display_mode->get_selected();129130if (dmode == DISPLAY_FRAME_TIME) {131return TranslationServer::get_singleton()->format_number(String::num(p_time, 2), lang) + " " + TTR("ms");132} else if (dmode == DISPLAY_FRAME_PERCENT) {133return TranslationServer::get_singleton()->format_number(String::num(p_time * 100 / graph_limit, 2), lang) + " " + TranslationServer::get_singleton()->get_percent_sign(lang);134}135136return "err";137}138139Color EditorVisualProfiler::_get_color_from_signature(const StringName &p_signature) const {140Color bc = get_theme_color(SNAME("error_color"), EditorStringName(Editor));141double rot = Math::abs(double(p_signature.hash()) / double(0x7FFFFFFF));142Color c;143c.set_hsv(rot, bc.get_s(), bc.get_v());144return c.lerp(get_theme_color(SNAME("base_color"), EditorStringName(Editor)), 0.07);145}146147void EditorVisualProfiler::_item_selected() {148if (updating_frame) {149return;150}151152TreeItem *item = variables->get_selected();153if (!item) {154return;155}156selected_area = item->get_metadata(0);157_update_plot();158}159160void EditorVisualProfiler::_update_plot() {161const int w = graph->get_size().width + 1; // `+1` is to prevent from crashing when visual profiler is auto started.162const int h = graph->get_size().height + 1;163164bool reset_texture = false;165166const int desired_len = w * h * 4;167168if (graph_image.size() != desired_len) {169reset_texture = true;170graph_image.resize(desired_len);171}172173uint8_t *wr = graph_image.ptrw();174const Color background_color = get_theme_color("dark_color_2", EditorStringName(Editor));175176// Clear the previous frame and set the background color.177for (int i = 0; i < desired_len; i += 4) {178wr[i + 0] = Math::fast_ftoi(background_color.r * 255);179wr[i + 1] = Math::fast_ftoi(background_color.g * 255);180wr[i + 2] = Math::fast_ftoi(background_color.b * 255);181wr[i + 3] = 255;182}183184//find highest value185186float highest_cpu = 0;187float highest_gpu = 0;188189for (int i = 0; i < frame_metrics.size(); i++) {190const Metric &m = frame_metrics[i];191if (!m.valid) {192continue;193}194195if (m.areas.size()) {196highest_cpu = MAX(highest_cpu, m.areas[m.areas.size() - 1].cpu_time);197highest_gpu = MAX(highest_gpu, m.areas[m.areas.size() - 1].gpu_time);198}199}200201if (highest_cpu > 0 || highest_gpu > 0) {202if (frame_relative->is_pressed()) {203highest_cpu = MAX(graph_limit, highest_cpu);204highest_gpu = MAX(graph_limit, highest_gpu);205}206207if (linked->is_pressed()) {208float highest = MAX(highest_cpu, highest_gpu);209highest_cpu = highest_gpu = highest;210}211212//means some data exists..213highest_cpu *= 1.2; //leave some upper room214highest_gpu *= 1.2; //leave some upper room215graph_height_cpu = highest_cpu;216graph_height_gpu = highest_gpu;217218Vector<Color> columnv_cpu;219columnv_cpu.resize(h);220Color *column_cpu = columnv_cpu.ptrw();221222Vector<Color> columnv_gpu;223columnv_gpu.resize(h);224Color *column_gpu = columnv_gpu.ptrw();225226int half_w = w / 2;227for (int i = 0; i < half_w; i++) {228for (int j = 0; j < h; j++) {229column_cpu[j] = Color(0, 0, 0, 0);230column_gpu[j] = Color(0, 0, 0, 0);231}232233int current = i * frame_metrics.size() / half_w;234int next = (i + 1) * frame_metrics.size() / half_w;235if (next > frame_metrics.size()) {236next = frame_metrics.size();237}238if (next == current) {239next = current + 1; //just because for loop must work240}241242for (int j = current; j < next; j++) {243//wrap244int idx = last_metric + 1 + j;245while (idx >= frame_metrics.size()) {246idx -= frame_metrics.size();247}248249int area_count = frame_metrics[idx].areas.size();250const Metric::Area *areas = frame_metrics[idx].areas.ptr();251int prev_cpu = 0;252int prev_gpu = 0;253for (int k = 1; k < area_count; k++) {254int ofs_cpu = int(areas[k].cpu_time * h / highest_cpu);255ofs_cpu = CLAMP(ofs_cpu, 0, h - 1);256Color color = selected_area == areas[k - 1].fullpath_cache ? Color(1, 1, 1, 1) : areas[k - 1].color_cache;257258for (int l = prev_cpu; l < ofs_cpu; l++) {259column_cpu[h - l - 1] += color;260}261prev_cpu = ofs_cpu;262263int ofs_gpu = int(areas[k].gpu_time * h / highest_gpu);264ofs_gpu = CLAMP(ofs_gpu, 0, h - 1);265for (int l = prev_gpu; l < ofs_gpu; l++) {266column_gpu[h - l - 1] += color;267}268269prev_gpu = ofs_gpu;270}271}272273//plot CPU274for (int j = 0; j < h; j++) {275uint8_t r, g, b;276277if (column_cpu[j].a == 0) {278r = Math::fast_ftoi(background_color.r * 255);279g = Math::fast_ftoi(background_color.g * 255);280b = Math::fast_ftoi(background_color.b * 255);281} else {282r = CLAMP((column_cpu[j].r / column_cpu[j].a) * 255.0, 0, 255);283g = CLAMP((column_cpu[j].g / column_cpu[j].a) * 255.0, 0, 255);284b = CLAMP((column_cpu[j].b / column_cpu[j].a) * 255.0, 0, 255);285}286287int widx = (j * w + i) * 4;288wr[widx + 0] = r;289wr[widx + 1] = g;290wr[widx + 2] = b;291wr[widx + 3] = 255;292}293//plot GPU294for (int j = 0; j < h; j++) {295uint8_t r, g, b;296297if (column_gpu[j].a == 0) {298r = Math::fast_ftoi(background_color.r * 255);299g = Math::fast_ftoi(background_color.g * 255);300b = Math::fast_ftoi(background_color.b * 255);301} else {302r = CLAMP((column_gpu[j].r / column_gpu[j].a) * 255.0, 0, 255);303g = CLAMP((column_gpu[j].g / column_gpu[j].a) * 255.0, 0, 255);304b = CLAMP((column_gpu[j].b / column_gpu[j].a) * 255.0, 0, 255);305}306307int widx = (j * w + w / 2 + i) * 4;308wr[widx + 0] = r;309wr[widx + 1] = g;310wr[widx + 2] = b;311wr[widx + 3] = 255;312}313}314}315316Ref<Image> img = Image::create_from_data(w, h, false, Image::FORMAT_RGBA8, graph_image);317318if (reset_texture) {319if (graph_texture.is_null()) {320graph_texture.instantiate();321}322graph_texture->set_image(img);323}324325graph_texture->update(img);326327graph->set_texture(graph_texture);328graph->queue_redraw();329}330331void EditorVisualProfiler::_update_frame(bool p_focus_selected) {332int cursor_metric = _get_cursor_index();333334Ref<Texture> track_icon = get_editor_theme_icon(SNAME("TrackColor"));335336ERR_FAIL_INDEX(cursor_metric, frame_metrics.size());337338updating_frame = true;339variables->clear();340341TreeItem *root = variables->create_item();342const Metric &m = frame_metrics[cursor_metric];343344List<TreeItem *> stack;345List<TreeItem *> categories;346347TreeItem *ensure_selected = nullptr;348349for (int i = 1; i < m.areas.size() - 1; i++) {350TreeItem *parent = stack.size() ? stack.back()->get() : root;351352String name = m.areas[i].name;353354float cpu_time = m.areas[i].cpu_time;355float gpu_time = m.areas[i].gpu_time;356if (i < m.areas.size() - 1) {357cpu_time = m.areas[i + 1].cpu_time - cpu_time;358gpu_time = m.areas[i + 1].gpu_time - gpu_time;359}360361if (name.begins_with(">")) {362TreeItem *category = variables->create_item(parent);363364stack.push_back(category);365categories.push_back(category);366367name = name.substr(1);368369category->set_text(0, name);370category->set_metadata(1, cpu_time);371category->set_metadata(2, gpu_time);372continue;373}374375if (name.begins_with("<")) {376stack.pop_back();377continue;378}379TreeItem *category = variables->create_item(parent);380381for (TreeItem *E : stack) {382float total_cpu = E->get_metadata(1);383float total_gpu = E->get_metadata(2);384total_cpu += cpu_time;385total_gpu += gpu_time;386E->set_metadata(1, total_cpu);387E->set_metadata(2, total_gpu);388}389390category->set_icon(0, track_icon);391category->set_icon_modulate(0, m.areas[i].color_cache);392category->set_selectable(0, true);393category->set_metadata(0, m.areas[i].fullpath_cache);394category->set_text(0, m.areas[i].name);395category->set_text(1, _get_time_as_text(cpu_time));396category->set_metadata(1, m.areas[i].cpu_time);397category->set_text(2, _get_time_as_text(gpu_time));398category->set_metadata(2, m.areas[i].gpu_time);399400if (selected_area == m.areas[i].fullpath_cache) {401category->select(0);402if (p_focus_selected) {403ensure_selected = category;404}405}406}407408for (TreeItem *E : categories) {409float total_cpu = E->get_metadata(1);410float total_gpu = E->get_metadata(2);411E->set_text(1, _get_time_as_text(total_cpu));412E->set_text(2, _get_time_as_text(total_gpu));413}414415if (ensure_selected) {416variables->ensure_cursor_is_visible();417}418updating_frame = false;419}420421void EditorVisualProfiler::_activate_pressed() {422if (activate->is_pressed()) {423activate->set_button_icon(get_editor_theme_icon(SNAME("Stop")));424activate->set_text(TTRC("Stop"));425_clear_pressed(); //always clear on start426clear_button->set_disabled(false);427} else {428activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));429activate->set_text(TTRC("Start"));430}431emit_signal(SNAME("enable_profiling"), activate->is_pressed());432}433434void EditorVisualProfiler::_clear_pressed() {435clear_button->set_disabled(true);436clear();437_update_plot();438}439440void EditorVisualProfiler::_autostart_toggled(bool p_toggled_on) {441EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_visual_profiler", p_toggled_on);442EditorRunBar::get_singleton()->update_profiler_autostart_indicator();443}444445void EditorVisualProfiler::_notification(int p_what) {446switch (p_what) {447case NOTIFICATION_TRANSLATION_CHANGED: {448if (is_ready()) {449_update_frame();450}451[[fallthrough]];452}453case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:454case NOTIFICATION_THEME_CHANGED: {455activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));456clear_button->set_button_icon(get_editor_theme_icon(SNAME("Clear")));457} break;458}459}460461void EditorVisualProfiler::_graph_tex_draw() {462if (last_metric < 0) {463return;464}465466Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));467int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));468const Color color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));469470if (seeking) {471int max_frames = frame_metrics.size();472int frame = cursor_metric_edit->get_value() - (frame_metrics[last_metric].frame_number - max_frames + 1);473if (frame < 0) {474frame = 0;475}476477int half_width = graph->get_size().x / 2;478int cur_x = frame * half_width / max_frames;479480graph->draw_line(Vector2(cur_x, 0), Vector2(cur_x, graph->get_size().y), color * Color(1, 1, 1));481graph->draw_line(Vector2(cur_x + half_width, 0), Vector2(cur_x + half_width, graph->get_size().y), color * Color(1, 1, 1));482}483484if (graph_height_cpu > 0) {485int frame_y = graph->get_size().y - graph_limit * graph->get_size().y / graph_height_cpu - 1;486487int half_width = graph->get_size().x / 2;488489graph->draw_line(Vector2(0, frame_y), Vector2(half_width, frame_y), color * Color(1, 1, 1, 0.5));490491const String limit_str = String::num(graph_limit, 2) + " ms";492graph->draw_string(font, Vector2(half_width - font->get_string_size(limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x - 2, frame_y - 2), limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1, 0.75));493}494495if (graph_height_gpu > 0) {496int frame_y = graph->get_size().y - graph_limit * graph->get_size().y / graph_height_gpu - 1;497498int half_width = graph->get_size().x / 2;499500graph->draw_line(Vector2(half_width, frame_y), Vector2(graph->get_size().x, frame_y), color * Color(1, 1, 1, 0.5));501502const String limit_str = String::num(graph_limit, 2) + " ms";503graph->draw_string(font, Vector2(half_width * 2 - font->get_string_size(limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x - 2, frame_y - 2), limit_str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1, 0.75));504}505506graph->draw_string(font, Vector2(font->get_string_size("X", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x, font->get_ascent(font_size) + 2), "CPU: " + cpu_name, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1, 0.75));507graph->draw_string(font, Vector2(font->get_string_size("X", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x + graph->get_size().width / 2, font->get_ascent(font_size) + 2), "GPU: " + gpu_name, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color * Color(1, 1, 1, 0.75));508}509510void EditorVisualProfiler::_graph_tex_mouse_exit() {511hover_metric = -1;512graph->queue_redraw();513}514515void EditorVisualProfiler::_cursor_metric_changed(double) {516if (updating_frame) {517return;518}519520graph->queue_redraw();521_update_frame();522}523524void EditorVisualProfiler::_graph_tex_input(const Ref<InputEvent> &p_ev) {525if (last_metric < 0) {526return;527}528529Ref<InputEventMouse> me = p_ev;530Ref<InputEventMouseButton> mb = p_ev;531Ref<InputEventMouseMotion> mm = p_ev;532533if (534(mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) ||535(mm.is_valid())) {536int half_w = graph->get_size().width / 2;537int x = me->get_position().x;538if (x > half_w) {539x -= half_w;540}541x = x * frame_metrics.size() / half_w;542543bool show_hover = x >= 0 && x < frame_metrics.size();544545if (x < 0) {546x = 0;547}548549if (x >= frame_metrics.size()) {550x = frame_metrics.size() - 1;551}552553int metric = frame_metrics.size() - x - 1;554metric = last_metric - metric;555while (metric < 0) {556metric += frame_metrics.size();557}558559if (show_hover) {560hover_metric = metric;561562} else {563hover_metric = -1;564}565566if (mb.is_valid() || mm->get_button_mask().has_flag(MouseButtonMask::LEFT)) {567//cursor_metric=x;568updating_frame = true;569570//metric may be invalid, so look for closest metric that is valid, this makes snap feel better571bool valid = false;572for (int i = 0; i < frame_metrics.size(); i++) {573if (frame_metrics[metric].valid) {574valid = true;575break;576}577578metric++;579if (metric >= frame_metrics.size()) {580metric = 0;581}582}583584if (!valid) {585return;586}587588cursor_metric_edit->set_value(frame_metrics[metric].frame_number);589590updating_frame = false;591592if (activate->is_pressed()) {593if (!seeking) {594// Break request is not required, just stop profiling595}596}597598seeking = true;599600if (!frame_delay->is_processing()) {601frame_delay->set_wait_time(0.1);602frame_delay->start();603}604605bool touched_cpu = me->get_position().x < graph->get_size().width * 0.5;606607const Metric::Area *areas = frame_metrics[metric].areas.ptr();608int area_count = frame_metrics[metric].areas.size();609float posy = (1.0 - (me->get_position().y / graph->get_size().height)) * (touched_cpu ? graph_height_cpu : graph_height_gpu);610int last_valid = -1;611bool found = false;612for (int i = 0; i < area_count - 1; i++) {613if (areas[i].name[0] != '<' && areas[i].name[0] != '>') {614last_valid = i;615}616float h = touched_cpu ? areas[i + 1].cpu_time : areas[i + 1].gpu_time;617618if (h > posy) {619found = true;620break;621}622}623624StringName area_found;625if (found && last_valid != -1) {626area_found = areas[last_valid].fullpath_cache;627}628629if (area_found != selected_area) {630selected_area = area_found;631_update_frame(true);632_update_plot();633}634}635636graph->queue_redraw();637}638}639640int EditorVisualProfiler::_get_cursor_index() const {641if (last_metric < 0) {642return 0;643}644if (!frame_metrics[last_metric].valid) {645return 0;646}647648int diff = (frame_metrics[last_metric].frame_number - cursor_metric_edit->get_value());649650int idx = last_metric - diff;651while (idx < 0) {652idx += frame_metrics.size();653}654655return idx;656}657658void EditorVisualProfiler::disable_seeking() {659seeking = false;660graph->queue_redraw();661}662663void EditorVisualProfiler::_combo_changed(int) {664_update_frame();665_update_plot();666}667668void EditorVisualProfiler::_bind_methods() {669ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));670}671672void EditorVisualProfiler::_update_button_text() {673if (activate->is_pressed()) {674activate->set_button_icon(get_editor_theme_icon(SNAME("Stop")));675activate->set_text(TTRC("Stop"));676} else {677activate->set_button_icon(get_editor_theme_icon(SNAME("Play")));678activate->set_text(TTRC("Start"));679}680}681682void EditorVisualProfiler::set_enabled(bool p_enable) {683activate->set_disabled(!p_enable);684}685686void EditorVisualProfiler::set_profiling(bool p_profiling) {687activate->set_pressed(p_profiling);688_update_button_text();689emit_signal(SNAME("enable_profiling"), activate->is_pressed());690}691692bool EditorVisualProfiler::is_profiling() {693return activate->is_pressed();694}695696Vector<Vector<String>> EditorVisualProfiler::get_data_as_csv() const {697Vector<Vector<String>> res;698#if 0699if (frame_metrics.is_empty()) {700return res;701}702703// signatures704Vector<String> signatures;705const Vector<EditorFrameProfiler::Metric::Category> &categories = frame_metrics[0].categories;706707for (int j = 0; j < categories.size(); j++) {708const EditorFrameProfiler::Metric::Category &c = categories[j];709signatures.push_back(c.signature);710711for (int k = 0; k < c.items.size(); k++) {712signatures.push_back(c.items[k].signature);713}714}715res.push_back(signatures);716717// values718Vector<String> values;719values.resize(signatures.size());720721int index = last_metric;722723for (int i = 0; i < frame_metrics.size(); i++) {724++index;725726if (index >= frame_metrics.size()) {727index = 0;728}729730if (!frame_metrics[index].valid) {731continue;732}733int it = 0;734const Vector<EditorFrameProfiler::Metric::Category> &frame_cat = frame_metrics[index].categories;735736for (int j = 0; j < frame_cat.size(); j++) {737const EditorFrameProfiler::Metric::Category &c = frame_cat[j];738values.write[it++] = String::num_real(c.total_time);739740for (int k = 0; k < c.items.size(); k++) {741values.write[it++] = String::num_real(c.items[k].total);742}743}744res.push_back(values);745}746#endif747return res;748}749750EditorVisualProfiler::EditorVisualProfiler() {751HBoxContainer *hb = memnew(HBoxContainer);752hb->add_theme_constant_override(SNAME("separation"), 8 * EDSCALE);753add_child(hb);754755FlowContainer *container = memnew(FlowContainer);756container->set_h_size_flags(SIZE_EXPAND_FILL);757container->add_theme_constant_override(SNAME("h_separation"), 8 * EDSCALE);758container->add_theme_constant_override(SNAME("v_separation"), 2 * EDSCALE);759hb->add_child(container);760761activate = memnew(Button);762activate->set_toggle_mode(true);763activate->set_disabled(true);764activate->set_text(TTRC("Start"));765activate->connect(SceneStringName(pressed), callable_mp(this, &EditorVisualProfiler::_activate_pressed));766container->add_child(activate);767768clear_button = memnew(Button);769clear_button->set_text(TTRC("Clear"));770clear_button->set_disabled(true);771clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorVisualProfiler::_clear_pressed));772container->add_child(clear_button);773774CheckBox *autostart_checkbox = memnew(CheckBox);775autostart_checkbox->set_text(TTRC("Autostart"));776autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_visual_profiler", false));777autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorVisualProfiler::_autostart_toggled));778container->add_child(autostart_checkbox);779780HBoxContainer *hb_measure = memnew(HBoxContainer);781hb_measure->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);782container->add_child(hb_measure);783784hb_measure->add_child(memnew(Label(TTRC("Measure:"))));785786display_mode = memnew(OptionButton);787display_mode->set_accessibility_name(TTRC("Measure:"));788display_mode->add_item(TTRC("Frame Time (ms)"));789display_mode->add_item(TTRC("Frame %"));790display_mode->connect(SceneStringName(item_selected), callable_mp(this, &EditorVisualProfiler::_combo_changed));791792hb_measure->add_child(display_mode);793794frame_relative = memnew(CheckBox(TTRC("Fit to Frame")));795frame_relative->set_pressed(true);796container->add_child(frame_relative);797frame_relative->connect(SceneStringName(pressed), callable_mp(this, &EditorVisualProfiler::_update_plot));798linked = memnew(CheckBox(TTRC("Linked")));799linked->set_pressed(true);800container->add_child(linked);801linked->connect(SceneStringName(pressed), callable_mp(this, &EditorVisualProfiler::_update_plot));802803HBoxContainer *hb_frame = memnew(HBoxContainer);804hb_frame->add_theme_constant_override(SNAME("separation"), 2 * EDSCALE);805hb_frame->set_v_size_flags(SIZE_SHRINK_BEGIN);806hb->add_child(hb_frame);807808hb_frame->add_child(memnew(Label(TTRC("Frame #:"))));809810cursor_metric_edit = memnew(SpinBox);811cursor_metric_edit->set_accessibility_name(TTRC("Frame #:"));812cursor_metric_edit->set_h_size_flags(SIZE_FILL);813hb_frame->add_child(cursor_metric_edit);814cursor_metric_edit->connect(SceneStringName(value_changed), callable_mp(this, &EditorVisualProfiler::_cursor_metric_changed));815816h_split = memnew(HSplitContainer);817add_child(h_split);818h_split->set_v_size_flags(SIZE_EXPAND_FILL);819820variables = memnew(Tree);821variables->set_custom_minimum_size(Size2(300, 0) * EDSCALE);822variables->set_hide_folding(true);823h_split->add_child(variables);824variables->set_hide_root(true);825variables->set_columns(3);826variables->set_column_titles_visible(true);827variables->set_column_title(0, TTRC("Name"));828variables->set_column_expand(0, true);829variables->set_column_clip_content(0, true);830variables->set_column_custom_minimum_width(0, 60);831variables->set_column_title(1, TTRC("CPU"));832variables->set_column_expand(1, false);833variables->set_column_clip_content(1, true);834variables->set_column_custom_minimum_width(1, 75 * EDSCALE);835variables->set_column_title(2, TTRC("GPU"));836variables->set_column_expand(2, false);837variables->set_column_clip_content(2, true);838variables->set_column_custom_minimum_width(2, 75 * EDSCALE);839variables->set_theme_type_variation("TreeSecondary");840variables->connect("cell_selected", callable_mp(this, &EditorVisualProfiler::_item_selected));841842graph = memnew(TextureRect);843graph->set_custom_minimum_size(Size2(250 * EDSCALE, 0));844graph->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);845graph->set_mouse_filter(MOUSE_FILTER_STOP);846graph->connect(SceneStringName(draw), callable_mp(this, &EditorVisualProfiler::_graph_tex_draw));847graph->connect(SceneStringName(gui_input), callable_mp(this, &EditorVisualProfiler::_graph_tex_input));848graph->connect(SceneStringName(mouse_exited), callable_mp(this, &EditorVisualProfiler::_graph_tex_mouse_exit));849850h_split->add_child(graph);851graph->set_h_size_flags(SIZE_EXPAND_FILL);852853int metric_size = CLAMP(int(EDITOR_GET("debugger/profiler_frame_history_size")), 60, 10000);854frame_metrics.resize(metric_size);855856graph_limit = 1000.0f / CLAMP(int(EDITOR_GET("debugger/profiler_target_fps")), 1, 1000);857858frame_delay = memnew(Timer);859frame_delay->set_wait_time(0.1);860frame_delay->set_one_shot(true);861add_child(frame_delay);862frame_delay->connect("timeout", callable_mp(this, &EditorVisualProfiler::_update_frame).bind(false));863864plot_delay = memnew(Timer);865plot_delay->set_wait_time(0.1);866plot_delay->set_one_shot(true);867add_child(plot_delay);868plot_delay->connect("timeout", callable_mp(this, &EditorVisualProfiler::_update_plot));869}870871872