2017-01-16 07:04:19 +00:00
/*************************************************************************/
/* editor_profiler.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 12:16:55 +00:00
/* https://godotengine.org */
2017-01-16 07:04:19 +00:00
/*************************************************************************/
2022-01-13 08:45:09 +00:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2017-01-16 07:04:19 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-04 23:50:27 +00:00
2016-05-22 00:18:16 +00:00
# include "editor_profiler.h"
2017-01-16 07:04:19 +00:00
2018-09-11 16:13:45 +00:00
# include "core/os/os.h"
2017-09-18 19:39:37 +00:00
# include "editor_scale.h"
2016-05-22 00:18:16 +00:00
# include "editor_settings.h"
2017-03-05 15:44:50 +00:00
void EditorProfiler : : _make_metric_ptrs ( Metric & m ) {
for ( int i = 0 ; i < m . categories . size ( ) ; i + + ) {
2018-07-25 01:11:03 +00:00
m . category_ptrs [ m . categories [ i ] . signature ] = & m . categories . write [ i ] ;
2017-03-05 15:44:50 +00:00
for ( int j = 0 ; j < m . categories [ i ] . items . size ( ) ; j + + ) {
2018-07-25 01:11:03 +00:00
m . item_ptrs [ m . categories [ i ] . items [ j ] . signature ] = & m . categories . write [ i ] . items . write [ j ] ;
2016-05-22 00:18:16 +00:00
}
}
}
2017-03-05 15:44:50 +00:00
void EditorProfiler : : add_frame_metric ( const Metric & p_metric , bool p_final ) {
2016-05-22 00:18:16 +00:00
+ + last_metric ;
2021-05-05 10:44:11 +00:00
if ( last_metric > = frame_metrics . size ( ) ) {
2017-03-05 15:44:50 +00:00
last_metric = 0 ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
2018-07-25 01:11:03 +00:00
frame_metrics . write [ last_metric ] = p_metric ;
_make_metric_ptrs ( frame_metrics . write [ last_metric ] ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
updating_frame = true ;
2016-05-22 00:18:16 +00:00
cursor_metric_edit - > set_max ( frame_metrics [ last_metric ] . frame_number ) ;
2017-03-05 15:44:50 +00:00
cursor_metric_edit - > set_min ( MAX ( frame_metrics [ last_metric ] . frame_number - frame_metrics . size ( ) , 0 ) ) ;
2016-05-22 00:18:16 +00:00
if ( ! seeking ) {
2017-01-04 04:16:14 +00:00
cursor_metric_edit - > set_value ( frame_metrics [ last_metric ] . frame_number ) ;
2017-03-05 15:44:50 +00:00
if ( hover_metric ! = - 1 ) {
2016-05-22 00:18:16 +00:00
hover_metric + + ;
2017-03-05 15:44:50 +00:00
if ( hover_metric > = frame_metrics . size ( ) ) {
hover_metric = 0 ;
2016-05-22 00:18:16 +00:00
}
}
}
2017-03-05 15:44:50 +00:00
updating_frame = false ;
2016-05-22 00:18:16 +00:00
2018-06-20 00:42:29 +00:00
if ( frame_delay - > is_stopped ( ) ) {
2017-03-05 15:44:50 +00:00
frame_delay - > set_wait_time ( p_final ? 0.1 : 1 ) ;
2016-05-22 00:18:16 +00:00
frame_delay - > start ( ) ;
}
2018-06-20 00:42:29 +00:00
if ( plot_delay - > is_stopped ( ) ) {
2016-05-22 00:18:16 +00:00
plot_delay - > set_wait_time ( 0.1 ) ;
plot_delay - > start ( ) ;
}
}
void EditorProfiler : : clear ( ) {
2017-03-05 15:44:50 +00:00
int metric_size = EditorSettings : : get_singleton ( ) - > get ( " debugger/profiler_frame_history_size " ) ;
metric_size = CLAMP ( metric_size , 60 , 1024 ) ;
2016-05-22 00:18:16 +00:00
frame_metrics . clear ( ) ;
frame_metrics . resize ( metric_size ) ;
2017-03-05 15:44:50 +00:00
last_metric = - 1 ;
2016-05-22 00:18:16 +00:00
variables - > clear ( ) ;
plot_sigs . clear ( ) ;
2017-09-30 14:19:07 +00:00
plot_sigs . insert ( " physics_frame_time " ) ;
2016-05-22 00:18:16 +00:00
plot_sigs . insert ( " category_frame_time " ) ;
2017-03-05 15:44:50 +00:00
updating_frame = true ;
2016-05-22 00:18:16 +00:00
cursor_metric_edit - > set_min ( 0 ) ;
2019-11-26 11:09:58 +00:00
cursor_metric_edit - > set_max ( 100 ) ; // Doesn't make much sense, but we can't have min == max. Doesn't hurt.
2017-01-04 04:16:14 +00:00
cursor_metric_edit - > set_value ( 0 ) ;
2017-03-05 15:44:50 +00:00
updating_frame = false ;
hover_metric = - 1 ;
seeking = false ;
2016-05-22 00:18:16 +00:00
}
2017-03-05 15:44:50 +00:00
static String _get_percent_txt ( float p_value , float p_total ) {
2020-01-24 11:07:38 +00:00
if ( p_total = = 0 ) {
2017-03-05 15:44:50 +00:00
p_total = 0.00001 ;
2020-01-24 11:07:38 +00:00
}
2017-03-05 15:44:50 +00:00
return String : : num ( ( p_value / p_total ) * 100 , 1 ) + " % " ;
2016-05-22 00:18:16 +00:00
}
2018-07-25 01:11:03 +00:00
String EditorProfiler : : _get_time_as_text ( const Metric & m , float p_time , int p_calls ) {
2020-01-24 11:07:38 +00:00
const int dmode = display_mode - > get_selected ( ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
if ( dmode = = DISPLAY_FRAME_TIME ) {
2020-01-24 11:07:38 +00:00
return rtos ( p_time * 1000 ) . pad_decimals ( 2 ) + " ms " ;
2017-03-05 15:44:50 +00:00
} else if ( dmode = = DISPLAY_AVERAGE_TIME ) {
2020-01-24 11:07:38 +00:00
if ( p_calls = = 0 ) {
return " 0.00 ms " ;
} else {
return rtos ( ( p_time / p_calls ) * 1000 ) . pad_decimals ( 2 ) + " ms " ;
}
2017-03-05 15:44:50 +00:00
} else if ( dmode = = DISPLAY_FRAME_PERCENT ) {
return _get_percent_txt ( p_time , m . frame_time ) ;
2017-09-30 14:19:07 +00:00
} else if ( dmode = = DISPLAY_PHYSICS_FRAME_PERCENT ) {
return _get_percent_txt ( p_time , m . physics_frame_time ) ;
2016-05-22 00:18:16 +00:00
}
return " err " ;
}
2017-03-05 15:44:50 +00:00
Color EditorProfiler : : _get_color_from_signature ( const StringName & p_signature ) const {
2017-08-08 02:55:24 +00:00
Color bc = get_color ( " error_color " , " Editor " ) ;
2017-03-05 15:44:50 +00:00
double rot = ABS ( double ( p_signature . hash ( ) ) / double ( 0x7FFFFFFF ) ) ;
2016-05-22 00:18:16 +00:00
Color c ;
2017-08-08 02:55:24 +00:00
c . set_hsv ( rot , bc . get_s ( ) , bc . get_v ( ) ) ;
return c . linear_interpolate ( get_color ( " base_color " , " Editor " ) , 0.07 ) ;
2016-05-22 00:18:16 +00:00
}
void EditorProfiler : : _item_edited ( ) {
2021-05-05 10:44:11 +00:00
if ( updating_frame ) {
2016-05-22 00:18:16 +00:00
return ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
TreeItem * item = variables - > get_edited ( ) ;
2021-05-05 10:44:11 +00:00
if ( ! item ) {
2016-05-22 00:18:16 +00:00
return ;
2021-05-05 10:44:11 +00:00
}
2017-03-05 15:44:50 +00:00
StringName signature = item - > get_metadata ( 0 ) ;
bool checked = item - > is_checked ( 0 ) ;
2016-05-22 00:18:16 +00:00
2021-05-05 10:44:11 +00:00
if ( checked ) {
2016-05-22 00:18:16 +00:00
plot_sigs . insert ( signature ) ;
2021-05-05 10:44:11 +00:00
} else {
2016-05-22 00:18:16 +00:00
plot_sigs . erase ( signature ) ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
if ( ! frame_delay - > is_processing ( ) ) {
frame_delay - > set_wait_time ( 0.1 ) ;
frame_delay - > start ( ) ;
}
2016-07-21 21:01:57 +00:00
_update_plot ( ) ;
2016-05-22 00:18:16 +00:00
}
void EditorProfiler : : _update_plot ( ) {
2020-01-30 02:05:48 +00:00
const int w = graph - > get_size ( ) . width ;
const int h = graph - > get_size ( ) . height ;
2018-08-28 09:49:12 +00:00
bool reset_texture = false ;
2020-01-30 02:05:48 +00:00
const int desired_len = w * h * 4 ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
if ( graph_image . size ( ) ! = desired_len ) {
reset_texture = true ;
2016-05-22 00:18:16 +00:00
graph_image . resize ( desired_len ) ;
}
2017-01-07 21:25:37 +00:00
PoolVector < uint8_t > : : Write wr = graph_image . write ( ) ;
2020-01-30 02:05:48 +00:00
const Color background_color = get_color ( " dark_color_2 " , " Editor " ) ;
2016-05-22 00:18:16 +00:00
2020-01-30 02:05:48 +00:00
// Clear the previous frame and set the background color.
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < desired_len ; i + = 4 ) {
2020-01-30 02:05:48 +00:00
wr [ i + 0 ] = Math : : fast_ftoi ( background_color . r * 255 ) ;
wr [ i + 1 ] = Math : : fast_ftoi ( background_color . g * 255 ) ;
wr [ i + 2 ] = Math : : fast_ftoi ( background_color . b * 255 ) ;
2017-03-05 15:44:50 +00:00
wr [ i + 3 ] = 255 ;
2016-05-22 00:18:16 +00:00
}
//find highest value
2020-01-30 02:05:48 +00:00
const bool use_self = display_time - > get_selected ( ) = = DISPLAY_SELF_TIME ;
2017-03-05 15:44:50 +00:00
float highest = 0 ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < frame_metrics . size ( ) ; i + + ) {
2018-07-25 01:11:03 +00:00
const Metric & m = frame_metrics [ i ] ;
2021-05-05 10:44:11 +00:00
if ( ! m . valid ) {
2016-05-22 00:18:16 +00:00
continue ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
for ( Set < StringName > : : Element * E = plot_sigs . front ( ) ; E ; E = E - > next ( ) ) {
2018-07-25 01:11:03 +00:00
const Map < StringName , Metric : : Category * > : : Element * F = m . category_ptrs . find ( E - > get ( ) ) ;
2016-05-22 00:18:16 +00:00
if ( F ) {
2017-03-05 15:44:50 +00:00
highest = MAX ( F - > get ( ) - > total_time , highest ) ;
2016-05-22 00:18:16 +00:00
}
2018-07-25 01:11:03 +00:00
const Map < StringName , Metric : : Category : : Item * > : : Element * G = m . item_ptrs . find ( E - > get ( ) ) ;
2016-05-22 00:18:16 +00:00
if ( G ) {
if ( use_self ) {
2017-03-05 15:44:50 +00:00
highest = MAX ( G - > get ( ) - > self , highest ) ;
2016-05-22 00:18:16 +00:00
} else {
2017-03-05 15:44:50 +00:00
highest = MAX ( G - > get ( ) - > total , highest ) ;
2016-05-22 00:18:16 +00:00
}
}
}
}
2017-03-05 15:44:50 +00:00
if ( highest > 0 ) {
2016-05-22 00:18:16 +00:00
//means some data exists..
2017-03-05 15:44:50 +00:00
highest * = 1.2 ; //leave some upper room
graph_height = highest ;
2016-05-22 00:18:16 +00:00
Vector < int > columnv ;
2017-03-05 15:44:50 +00:00
columnv . resize ( h * 4 ) ;
2016-05-22 00:18:16 +00:00
2017-11-25 03:07:54 +00:00
int * column = columnv . ptrw ( ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
Map < StringName , int > plot_prev ;
2016-05-22 00:18:16 +00:00
//Map<StringName,int> plot_max;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < w ; i + + ) {
for ( int j = 0 ; j < h * 4 ; j + + ) {
column [ j ] = 0 ;
2016-05-22 00:18:16 +00:00
}
2017-03-05 15:44:50 +00:00
int current = i * frame_metrics . size ( ) / w ;
int next = ( i + 1 ) * frame_metrics . size ( ) / w ;
if ( next > frame_metrics . size ( ) ) {
next = frame_metrics . size ( ) ;
2016-05-22 00:18:16 +00:00
}
2021-05-05 10:44:11 +00:00
if ( next = = current ) {
2017-03-05 15:44:50 +00:00
next = current + 1 ; //just because for loop must work
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
for ( Set < StringName > : : Element * E = plot_sigs . front ( ) ; E ; E = E - > next ( ) ) {
int plot_pos = - 1 ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
for ( int j = current ; j < next ; j + + ) {
2016-05-22 00:18:16 +00:00
//wrap
2017-03-05 15:44:50 +00:00
int idx = last_metric + 1 + j ;
while ( idx > = frame_metrics . size ( ) ) {
idx - = frame_metrics . size ( ) ;
2016-05-22 00:18:16 +00:00
}
//get
2018-07-25 01:11:03 +00:00
const Metric & m = frame_metrics [ idx ] ;
2021-05-05 10:44:11 +00:00
if ( ! m . valid ) {
2016-05-22 00:18:16 +00:00
continue ; //skip because invalid
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
float value = 0 ;
2016-05-22 00:18:16 +00:00
2018-07-25 01:11:03 +00:00
const Map < StringName , Metric : : Category * > : : Element * F = m . category_ptrs . find ( E - > get ( ) ) ;
2016-05-22 00:18:16 +00:00
if ( F ) {
2017-03-05 15:44:50 +00:00
value = F - > get ( ) - > total_time ;
2016-05-22 00:18:16 +00:00
}
2018-07-25 01:11:03 +00:00
const Map < StringName , Metric : : Category : : Item * > : : Element * G = m . item_ptrs . find ( E - > get ( ) ) ;
2016-05-22 00:18:16 +00:00
if ( G ) {
if ( use_self ) {
2017-03-05 15:44:50 +00:00
value = G - > get ( ) - > self ;
2016-05-22 00:18:16 +00:00
} else {
2017-03-05 15:44:50 +00:00
value = G - > get ( ) - > total ;
2016-05-22 00:18:16 +00:00
}
}
2017-03-05 15:44:50 +00:00
plot_pos = MAX ( CLAMP ( int ( value * h / highest ) , 0 , h - 1 ) , plot_pos ) ;
2016-05-22 00:18:16 +00:00
}
2017-03-05 15:44:50 +00:00
int prev_plot = plot_pos ;
Map < StringName , int > : : Element * H = plot_prev . find ( E - > get ( ) ) ;
2016-05-22 00:18:16 +00:00
if ( H ) {
2017-03-05 15:44:50 +00:00
prev_plot = H - > get ( ) ;
H - > get ( ) = plot_pos ;
2016-05-22 00:18:16 +00:00
} else {
2017-03-05 15:44:50 +00:00
plot_prev [ E - > get ( ) ] = plot_pos ;
2016-05-22 00:18:16 +00:00
}
2017-03-05 15:44:50 +00:00
if ( plot_pos = = - 1 & & prev_plot = = - 1 ) {
2016-05-22 00:18:16 +00:00
//don't bother drawing
continue ;
}
2017-03-05 15:44:50 +00:00
if ( prev_plot ! = - 1 & & plot_pos = = - 1 ) {
plot_pos = prev_plot ;
2016-05-22 00:18:16 +00:00
}
2017-03-05 15:44:50 +00:00
if ( prev_plot = = - 1 & & plot_pos ! = - 1 ) {
prev_plot = plot_pos ;
2016-05-22 00:18:16 +00:00
}
2017-03-05 15:44:50 +00:00
plot_pos = h - plot_pos - 1 ;
prev_plot = h - prev_plot - 1 ;
2016-05-22 00:18:16 +00:00
if ( prev_plot > plot_pos ) {
2017-03-05 15:44:50 +00:00
SWAP ( prev_plot , plot_pos ) ;
2016-05-22 00:18:16 +00:00
}
Color col = _get_color_from_signature ( E - > get ( ) ) ;
2017-03-05 15:44:50 +00:00
for ( int j = prev_plot ; j < = plot_pos ; j + + ) {
column [ j * 4 + 0 ] + = Math : : fast_ftoi ( CLAMP ( col . r * 255 , 0 , 255 ) ) ;
column [ j * 4 + 1 ] + = Math : : fast_ftoi ( CLAMP ( col . g * 255 , 0 , 255 ) ) ;
column [ j * 4 + 2 ] + = Math : : fast_ftoi ( CLAMP ( col . b * 255 , 0 , 255 ) ) ;
column [ j * 4 + 3 ] + = 1 ;
2016-05-22 00:18:16 +00:00
}
}
2017-03-05 15:44:50 +00:00
for ( int j = 0 ; j < h * 4 ; j + = 4 ) {
2020-01-30 02:05:48 +00:00
const int a = column [ j + 3 ] ;
2017-03-05 15:44:50 +00:00
if ( a > 0 ) {
column [ j + 0 ] / = a ;
column [ j + 1 ] / = a ;
column [ j + 2 ] / = a ;
2016-05-22 00:18:16 +00:00
}
2020-01-30 02:05:48 +00:00
const uint8_t red = uint8_t ( column [ j + 0 ] ) ;
const uint8_t green = uint8_t ( column [ j + 1 ] ) ;
const uint8_t blue = uint8_t ( column [ j + 2 ] ) ;
const bool is_filled = red > = 1 | | green > = 1 | | blue > = 1 ;
const int widx = ( ( j > > 2 ) * w + i ) * 4 ;
2016-05-22 00:18:16 +00:00
2020-01-30 02:05:48 +00:00
// If the pixel isn't filled by any profiler line, apply the background color instead.
wr [ widx + 0 ] = is_filled ? red : Math : : fast_ftoi ( background_color . r * 255 ) ;
wr [ widx + 1 ] = is_filled ? green : Math : : fast_ftoi ( background_color . g * 255 ) ;
wr [ widx + 2 ] = is_filled ? blue : Math : : fast_ftoi ( background_color . b * 255 ) ;
2017-03-05 15:44:50 +00:00
wr [ widx + 3 ] = 255 ;
2016-05-22 00:18:16 +00:00
}
}
}
2019-07-05 17:08:43 +00:00
wr . release ( ) ;
2016-05-22 00:18:16 +00:00
2017-05-17 10:36:47 +00:00
Ref < Image > img ;
img . instance ( ) ;
2021-05-04 14:28:24 +00:00
img - > create ( w , h , false , Image : : FORMAT_RGBA8 , graph_image ) ;
2016-05-22 00:18:16 +00:00
if ( reset_texture ) {
if ( graph_texture . is_null ( ) ) {
graph_texture . instance ( ) ;
}
2017-05-17 10:36:47 +00:00
graph_texture - > create ( img - > get_width ( ) , img - > get_height ( ) , img - > get_format ( ) , Texture : : FLAG_VIDEO_SURFACE ) ;
2016-05-22 00:18:16 +00:00
}
2017-01-14 17:03:38 +00:00
graph_texture - > set_data ( img ) ;
2016-05-22 00:18:16 +00:00
graph - > set_texture ( graph_texture ) ;
graph - > update ( ) ;
}
void EditorProfiler : : _update_frame ( ) {
int cursor_metric = _get_cursor_index ( ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_INDEX ( cursor_metric , frame_metrics . size ( ) ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
updating_frame = true ;
2016-05-22 00:18:16 +00:00
variables - > clear ( ) ;
2017-03-05 15:44:50 +00:00
TreeItem * root = variables - > create_item ( ) ;
2018-07-25 01:11:03 +00:00
const Metric & m = frame_metrics [ cursor_metric ] ;
2016-05-22 00:18:16 +00:00
int dtime = display_time - > get_selected ( ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < m . categories . size ( ) ; i + + ) {
2016-05-22 00:18:16 +00:00
TreeItem * category = variables - > create_item ( root ) ;
2017-03-05 15:44:50 +00:00
category - > set_cell_mode ( 0 , TreeItem : : CELL_MODE_CHECK ) ;
category - > set_editable ( 0 , true ) ;
category - > set_metadata ( 0 , m . categories [ i ] . signature ) ;
category - > set_text ( 0 , String ( m . categories [ i ] . name ) ) ;
category - > set_text ( 1 , _get_time_as_text ( m , m . categories [ i ] . total_time , 1 ) ) ;
2016-05-22 00:18:16 +00:00
if ( plot_sigs . has ( m . categories [ i ] . signature ) ) {
2017-03-05 15:44:50 +00:00
category - > set_checked ( 0 , true ) ;
category - > set_custom_color ( 0 , _get_color_from_signature ( m . categories [ i ] . signature ) ) ;
2016-05-22 00:18:16 +00:00
}
2019-01-26 18:54:04 +00:00
for ( int j = m . categories [ i ] . items . size ( ) - 1 ; j > = 0 ; j - - ) {
2018-07-25 01:11:03 +00:00
const Metric : : Category : : Item & it = m . categories [ i ] . items [ j ] ;
2016-05-22 00:18:16 +00:00
TreeItem * item = variables - > create_item ( category ) ;
2017-03-05 15:44:50 +00:00
item - > set_cell_mode ( 0 , TreeItem : : CELL_MODE_CHECK ) ;
item - > set_editable ( 0 , true ) ;
item - > set_text ( 0 , it . name ) ;
item - > set_metadata ( 0 , it . signature ) ;
item - > set_metadata ( 1 , it . script ) ;
item - > set_metadata ( 2 , it . line ) ;
2019-12-18 07:16:22 +00:00
item - > set_text_align ( 2 , TreeItem : : ALIGN_RIGHT ) ;
2021-07-18 23:40:05 +00:00
item - > set_tooltip ( 0 , it . name + " \n " + it . script + " : " + itos ( it . line ) ) ;
2016-05-22 00:18:16 +00:00
float time = dtime = = DISPLAY_SELF_TIME ? it . self : it . total ;
2017-03-05 15:44:50 +00:00
item - > set_text ( 1 , _get_time_as_text ( m , time , it . calls ) ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
item - > set_text ( 2 , itos ( it . calls ) ) ;
2016-05-22 00:18:16 +00:00
if ( plot_sigs . has ( it . signature ) ) {
2017-03-05 15:44:50 +00:00
item - > set_checked ( 0 , true ) ;
item - > set_custom_color ( 0 , _get_color_from_signature ( it . signature ) ) ;
2016-05-22 00:18:16 +00:00
}
}
}
2017-03-05 15:44:50 +00:00
updating_frame = false ;
2016-05-22 00:18:16 +00:00
}
void EditorProfiler : : _activate_pressed ( ) {
if ( activate - > is_pressed ( ) ) {
2017-03-05 15:44:50 +00:00
activate - > set_icon ( get_icon ( " Stop " , " EditorIcons " ) ) ;
2018-06-24 01:15:10 +00:00
activate - > set_text ( TTR ( " Stop " ) ) ;
2016-05-22 00:18:16 +00:00
} else {
2017-03-05 15:44:50 +00:00
activate - > set_icon ( get_icon ( " Play " , " EditorIcons " ) ) ;
2018-06-24 01:15:10 +00:00
activate - > set_text ( TTR ( " Start " ) ) ;
2016-05-22 00:18:16 +00:00
}
2017-03-05 15:44:50 +00:00
emit_signal ( " enable_profiling " , activate - > is_pressed ( ) ) ;
2016-05-22 00:18:16 +00:00
}
2018-06-24 01:15:10 +00:00
void EditorProfiler : : _clear_pressed ( ) {
clear ( ) ;
2018-08-28 09:49:12 +00:00
_update_plot ( ) ;
2018-06-24 01:15:10 +00:00
}
2016-05-22 00:18:16 +00:00
void EditorProfiler : : _notification ( int p_what ) {
2022-03-15 15:05:12 +00:00
switch ( p_what ) {
case NOTIFICATION_ENTER_TREE :
case NOTIFICATION_THEME_CHANGED : {
activate - > set_icon ( get_icon ( " Play " , " EditorIcons " ) ) ;
clear_button - > set_icon ( get_icon ( " Clear " , " EditorIcons " ) ) ;
} break ;
2016-05-22 00:18:16 +00:00
}
}
void EditorProfiler : : _graph_tex_draw ( ) {
2021-05-05 10:44:11 +00:00
if ( last_metric < 0 ) {
2016-05-22 00:18:16 +00:00
return ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
if ( seeking ) {
int max_frames = frame_metrics . size ( ) ;
2017-03-05 15:44:50 +00:00
int frame = cursor_metric_edit - > get_value ( ) - ( frame_metrics [ last_metric ] . frame_number - max_frames + 1 ) ;
2021-05-05 10:44:11 +00:00
if ( frame < 0 ) {
2017-03-05 15:44:50 +00:00
frame = 0 ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
int cur_x = frame * graph - > get_size ( ) . x / max_frames ;
2017-03-05 15:44:50 +00:00
graph - > draw_line ( Vector2 ( cur_x , 0 ) , Vector2 ( cur_x , graph - > get_size ( ) . y ) , Color ( 1 , 1 , 1 , 0.8 ) ) ;
2016-05-22 00:18:16 +00:00
}
2017-03-05 15:44:50 +00:00
if ( hover_metric ! = - 1 & & frame_metrics [ hover_metric ] . valid ) {
2016-05-22 00:18:16 +00:00
int max_frames = frame_metrics . size ( ) ;
2017-03-05 15:44:50 +00:00
int frame = frame_metrics [ hover_metric ] . frame_number - ( frame_metrics [ last_metric ] . frame_number - max_frames + 1 ) ;
2021-05-05 10:44:11 +00:00
if ( frame < 0 ) {
2017-03-05 15:44:50 +00:00
frame = 0 ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
int cur_x = frame * graph - > get_size ( ) . x / max_frames ;
2017-03-05 15:44:50 +00:00
graph - > draw_line ( Vector2 ( cur_x , 0 ) , Vector2 ( cur_x , graph - > get_size ( ) . y ) , Color ( 1 , 1 , 1 , 0.4 ) ) ;
2016-05-22 00:18:16 +00:00
}
}
void EditorProfiler : : _graph_tex_mouse_exit ( ) {
2017-03-05 15:44:50 +00:00
hover_metric = - 1 ;
2016-05-22 00:18:16 +00:00
graph - > update ( ) ;
}
void EditorProfiler : : _cursor_metric_changed ( double ) {
2021-05-05 10:44:11 +00:00
if ( updating_frame ) {
2016-05-22 00:18:16 +00:00
return ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
graph - > update ( ) ;
_update_frame ( ) ;
}
2017-05-20 15:38:03 +00:00
void EditorProfiler : : _graph_tex_input ( const Ref < InputEvent > & p_ev ) {
2021-05-05 10:44:11 +00:00
if ( last_metric < 0 ) {
2016-05-22 00:18:16 +00:00
return ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
2017-05-20 15:38:03 +00:00
Ref < InputEventMouse > me = p_ev ;
Ref < InputEventMouseButton > mb = p_ev ;
Ref < InputEventMouseMotion > mm = p_ev ;
2016-05-22 00:18:16 +00:00
if (
2017-05-20 15:38:03 +00:00
( mb . is_valid ( ) & & mb - > get_button_index ( ) = = BUTTON_LEFT & & mb - > is_pressed ( ) ) | |
( mm . is_valid ( ) ) ) {
2017-06-03 08:54:24 +00:00
int x = me - > get_position ( ) . x ;
2017-03-05 15:44:50 +00:00
x = x * frame_metrics . size ( ) / graph - > get_size ( ) . width ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
bool show_hover = x > = 0 & & x < frame_metrics . size ( ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
if ( x < 0 ) {
x = 0 ;
2016-05-22 00:18:16 +00:00
}
2017-03-05 15:44:50 +00:00
if ( x > = frame_metrics . size ( ) ) {
x = frame_metrics . size ( ) - 1 ;
2016-05-22 00:18:16 +00:00
}
2017-03-05 15:44:50 +00:00
int metric = frame_metrics . size ( ) - x - 1 ;
metric = last_metric - metric ;
while ( metric < 0 ) {
metric + = frame_metrics . size ( ) ;
2016-05-22 00:18:16 +00:00
}
if ( show_hover ) {
2017-03-05 15:44:50 +00:00
hover_metric = metric ;
2016-05-22 00:18:16 +00:00
} else {
2017-03-05 15:44:50 +00:00
hover_metric = - 1 ;
2016-05-22 00:18:16 +00:00
}
2017-05-27 09:17:05 +00:00
if ( mb . is_valid ( ) | | mm - > get_button_mask ( ) & BUTTON_MASK_LEFT ) {
2016-05-22 00:18:16 +00:00
//cursor_metric=x;
2017-03-05 15:44:50 +00:00
updating_frame = true ;
2016-05-22 00:18:16 +00:00
//metric may be invalid, so look for closest metric that is valid, this makes snap feel better
2017-03-05 15:44:50 +00:00
bool valid = false ;
for ( int i = 0 ; i < frame_metrics . size ( ) ; i + + ) {
2016-05-22 00:18:16 +00:00
if ( frame_metrics [ metric ] . valid ) {
2017-03-05 15:44:50 +00:00
valid = true ;
2016-05-22 00:18:16 +00:00
break ;
}
metric + + ;
2021-05-05 10:44:11 +00:00
if ( metric > = frame_metrics . size ( ) ) {
2017-03-05 15:44:50 +00:00
metric = 0 ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
}
2021-05-05 10:44:11 +00:00
if ( valid ) {
2017-01-04 04:16:14 +00:00
cursor_metric_edit - > set_value ( frame_metrics [ metric ] . frame_number ) ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
updating_frame = false ;
2016-05-22 00:18:16 +00:00
if ( activate - > is_pressed ( ) ) {
if ( ! seeking ) {
emit_signal ( " break_request " ) ;
}
}
2017-03-05 15:44:50 +00:00
seeking = true ;
2016-05-22 00:18:16 +00:00
if ( ! frame_delay - > is_processing ( ) ) {
frame_delay - > set_wait_time ( 0.1 ) ;
frame_delay - > start ( ) ;
}
}
graph - > update ( ) ;
}
}
int EditorProfiler : : _get_cursor_index ( ) const {
2021-05-05 10:44:11 +00:00
if ( last_metric < 0 ) {
2016-05-22 00:18:16 +00:00
return 0 ;
2021-05-05 10:44:11 +00:00
}
if ( ! frame_metrics [ last_metric ] . valid ) {
2016-05-22 00:18:16 +00:00
return 0 ;
2021-05-05 10:44:11 +00:00
}
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
int diff = ( frame_metrics [ last_metric ] . frame_number - cursor_metric_edit - > get_value ( ) ) ;
2016-05-22 00:18:16 +00:00
int idx = last_metric - diff ;
2017-03-05 15:44:50 +00:00
while ( idx < 0 ) {
idx + = frame_metrics . size ( ) ;
2016-05-22 00:18:16 +00:00
}
return idx ;
}
void EditorProfiler : : disable_seeking ( ) {
2017-03-05 15:44:50 +00:00
seeking = false ;
2016-05-22 00:18:16 +00:00
graph - > update ( ) ;
}
void EditorProfiler : : _combo_changed ( int ) {
_update_frame ( ) ;
_update_plot ( ) ;
}
void EditorProfiler : : _bind_methods ( ) {
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " _update_frame " ) , & EditorProfiler : : _update_frame ) ;
ClassDB : : bind_method ( D_METHOD ( " _update_plot " ) , & EditorProfiler : : _update_plot ) ;
ClassDB : : bind_method ( D_METHOD ( " _activate_pressed " ) , & EditorProfiler : : _activate_pressed ) ;
2018-06-24 01:15:10 +00:00
ClassDB : : bind_method ( D_METHOD ( " _clear_pressed " ) , & EditorProfiler : : _clear_pressed ) ;
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " _graph_tex_draw " ) , & EditorProfiler : : _graph_tex_draw ) ;
ClassDB : : bind_method ( D_METHOD ( " _graph_tex_input " ) , & EditorProfiler : : _graph_tex_input ) ;
ClassDB : : bind_method ( D_METHOD ( " _graph_tex_mouse_exit " ) , & EditorProfiler : : _graph_tex_mouse_exit ) ;
ClassDB : : bind_method ( D_METHOD ( " _cursor_metric_changed " ) , & EditorProfiler : : _cursor_metric_changed ) ;
ClassDB : : bind_method ( D_METHOD ( " _combo_changed " ) , & EditorProfiler : : _combo_changed ) ;
ClassDB : : bind_method ( D_METHOD ( " _item_edited " ) , & EditorProfiler : : _item_edited ) ;
ADD_SIGNAL ( MethodInfo ( " enable_profiling " , PropertyInfo ( Variant : : BOOL , " enable " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " break_request " ) ) ;
2016-05-22 00:18:16 +00:00
}
void EditorProfiler : : set_enabled ( bool p_enable ) {
activate - > set_disabled ( ! p_enable ) ;
}
bool EditorProfiler : : is_profiling ( ) {
return activate - > is_pressed ( ) ;
}
2021-05-04 12:20:36 +00:00
Vector < Vector < String > > EditorProfiler : : get_data_as_csv ( ) const {
Vector < Vector < String > > res ;
2019-02-18 00:25:26 +00:00
if ( frame_metrics . empty ( ) ) {
return res ;
}
2021-05-21 07:40:03 +00:00
// Different metrics may contain different number of categories.
Set < StringName > possible_signatures ;
for ( int i = 0 ; i < frame_metrics . size ( ) ; i + + ) {
const Metric & m = frame_metrics [ i ] ;
if ( ! m . valid ) {
continue ;
}
for ( Map < StringName , Metric : : Category * > : : Element * E = m . category_ptrs . front ( ) ; E ; E = E - > next ( ) ) {
possible_signatures . insert ( E - > key ( ) ) ;
}
for ( Map < StringName , Metric : : Category : : Item * > : : Element * E = m . item_ptrs . front ( ) ; E ; E = E - > next ( ) ) {
possible_signatures . insert ( E - > key ( ) ) ;
2019-02-18 00:25:26 +00:00
}
}
2021-05-21 07:40:03 +00:00
// Generate CSV header and cache indices.
Map < StringName , int > sig_map ;
Vector < String > signatures ;
signatures . resize ( possible_signatures . size ( ) ) ;
int sig_index = 0 ;
for ( const Set < StringName > : : Element * E = possible_signatures . front ( ) ; E ; E = E - > next ( ) ) {
signatures . write [ sig_index ] = E - > get ( ) ;
sig_map [ E - > get ( ) ] = sig_index ;
sig_index + + ;
}
2019-02-18 00:25:26 +00:00
res . push_back ( signatures ) ;
// values
Vector < String > values ;
int index = last_metric ;
for ( int i = 0 ; i < frame_metrics . size ( ) ; i + + ) {
+ + index ;
if ( index > = frame_metrics . size ( ) ) {
index = 0 ;
}
2021-05-21 07:40:03 +00:00
const Metric & m = frame_metrics [ index ] ;
if ( ! m . valid ) {
2019-02-18 00:25:26 +00:00
continue ;
}
2021-05-21 07:40:03 +00:00
// Don't keep old values since there may be empty cells.
values . clear ( ) ;
values . resize ( possible_signatures . size ( ) ) ;
2019-02-18 00:25:26 +00:00
2021-05-21 07:40:03 +00:00
for ( Map < StringName , Metric : : Category * > : : Element * E = m . category_ptrs . front ( ) ; E ; E = E - > next ( ) ) {
values . write [ sig_map [ E - > key ( ) ] ] = String : : num_real ( E - > value ( ) - > total_time ) ;
2019-02-18 00:25:26 +00:00
}
2021-05-21 07:40:03 +00:00
for ( Map < StringName , Metric : : Category : : Item * > : : Element * E = m . item_ptrs . front ( ) ; E ; E = E - > next ( ) ) {
values . write [ sig_map [ E - > key ( ) ] ] = String : : num_real ( E - > value ( ) - > total ) ;
}
2019-02-18 00:25:26 +00:00
res . push_back ( values ) ;
}
return res ;
}
2017-03-05 15:44:50 +00:00
EditorProfiler : : EditorProfiler ( ) {
HBoxContainer * hb = memnew ( HBoxContainer ) ;
2016-05-22 00:18:16 +00:00
add_child ( hb ) ;
2017-03-05 15:44:50 +00:00
activate = memnew ( Button ) ;
2016-05-22 00:18:16 +00:00
activate - > set_toggle_mode ( true ) ;
2018-06-24 01:15:10 +00:00
activate - > set_text ( TTR ( " Start " ) ) ;
2017-03-05 15:44:50 +00:00
activate - > connect ( " pressed " , this , " _activate_pressed " ) ;
2016-05-22 00:18:16 +00:00
hb - > add_child ( activate ) ;
2018-06-24 01:15:10 +00:00
clear_button = memnew ( Button ) ;
clear_button - > set_text ( TTR ( " Clear " ) ) ;
clear_button - > connect ( " pressed " , this , " _clear_pressed " ) ;
hb - > add_child ( clear_button ) ;
2017-03-05 15:44:50 +00:00
hb - > add_child ( memnew ( Label ( TTR ( " Measure: " ) ) ) ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
display_mode = memnew ( OptionButton ) ;
2021-07-24 04:15:49 +00:00
display_mode - > add_item ( TTR ( " Frame Time (ms) " ) ) ;
display_mode - > add_item ( TTR ( " Average Time (ms) " ) ) ;
2016-05-22 00:18:16 +00:00
display_mode - > add_item ( TTR ( " Frame % " ) ) ;
2017-10-21 14:28:08 +00:00
display_mode - > add_item ( TTR ( " Physics Frame % " ) ) ;
2017-03-05 15:44:50 +00:00
display_mode - > connect ( " item_selected " , this , " _combo_changed " ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
hb - > add_child ( display_mode ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
hb - > add_child ( memnew ( Label ( TTR ( " Time: " ) ) ) ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
display_time = memnew ( OptionButton ) ;
2016-05-22 00:18:16 +00:00
display_time - > add_item ( TTR ( " Inclusive " ) ) ;
display_time - > add_item ( TTR ( " Self " ) ) ;
2021-07-24 04:15:49 +00:00
display_time - > set_tooltip ( TTR ( " Inclusive: Includes time from other functions called by this function. \n Use this to spot bottlenecks. \n \n Self: Only count the time spent in the function itself, not in other functions called by that function. \n Use this to find individual functions to optimize. " ) ) ;
2017-03-05 15:44:50 +00:00
display_time - > connect ( " item_selected " , this , " _combo_changed " ) ;
2016-05-22 00:18:16 +00:00
hb - > add_child ( display_time ) ;
hb - > add_spacer ( ) ;
2017-03-05 15:44:50 +00:00
hb - > add_child ( memnew ( Label ( TTR ( " Frame #: " ) ) ) ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
cursor_metric_edit = memnew ( SpinBox ) ;
2016-05-22 00:18:16 +00:00
cursor_metric_edit - > set_h_size_flags ( SIZE_FILL ) ;
hb - > add_child ( cursor_metric_edit ) ;
2017-03-05 15:44:50 +00:00
cursor_metric_edit - > connect ( " value_changed " , this , " _cursor_metric_changed " ) ;
2016-05-22 00:18:16 +00:00
2017-09-18 19:39:37 +00:00
hb - > add_constant_override ( " separation " , 8 * EDSCALE ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
h_split = memnew ( HSplitContainer ) ;
2016-05-22 00:18:16 +00:00
add_child ( h_split ) ;
h_split - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
2017-03-05 15:44:50 +00:00
variables = memnew ( Tree ) ;
2020-01-24 11:07:38 +00:00
variables - > set_custom_minimum_size ( Size2 ( 320 , 0 ) * EDSCALE ) ;
2016-05-22 00:18:16 +00:00
variables - > set_hide_folding ( true ) ;
h_split - > add_child ( variables ) ;
variables - > set_hide_root ( true ) ;
variables - > set_columns ( 3 ) ;
variables - > set_column_titles_visible ( true ) ;
2017-12-02 09:58:58 +00:00
variables - > set_column_title ( 0 , TTR ( " Name " ) ) ;
2017-03-05 15:44:50 +00:00
variables - > set_column_expand ( 0 , true ) ;
2020-01-24 11:07:38 +00:00
variables - > set_column_min_width ( 0 , 60 * EDSCALE ) ;
2017-12-02 09:58:58 +00:00
variables - > set_column_title ( 1 , TTR ( " Time " ) ) ;
2017-03-05 15:44:50 +00:00
variables - > set_column_expand ( 1 , false ) ;
2020-01-24 11:07:38 +00:00
variables - > set_column_min_width ( 1 , 100 * EDSCALE ) ;
2017-12-02 09:58:58 +00:00
variables - > set_column_title ( 2 , TTR ( " Calls " ) ) ;
2017-03-05 15:44:50 +00:00
variables - > set_column_expand ( 2 , false ) ;
2017-09-18 19:39:37 +00:00
variables - > set_column_min_width ( 2 , 60 * EDSCALE ) ;
2017-03-05 15:44:50 +00:00
variables - > connect ( " item_edited " , this , " _item_edited " ) ;
graph = memnew ( TextureRect ) ;
2016-05-22 00:18:16 +00:00
graph - > set_expand ( true ) ;
2017-01-08 22:54:19 +00:00
graph - > set_mouse_filter ( MOUSE_FILTER_STOP ) ;
2017-03-05 15:44:50 +00:00
graph - > connect ( " draw " , this , " _graph_tex_draw " ) ;
graph - > connect ( " gui_input " , this , " _graph_tex_input " ) ;
graph - > connect ( " mouse_exited " , this , " _graph_tex_mouse_exit " ) ;
2016-05-22 00:18:16 +00:00
h_split - > add_child ( graph ) ;
graph - > set_h_size_flags ( SIZE_EXPAND_FILL ) ;
2017-03-05 15:44:50 +00:00
int metric_size = CLAMP ( int ( EDITOR_DEF ( " debugger/profiler_frame_history_size " , 600 ) ) , 60 , 1024 ) ;
2016-05-22 00:18:16 +00:00
frame_metrics . resize ( metric_size ) ;
2017-03-05 15:44:50 +00:00
last_metric = - 1 ;
hover_metric = - 1 ;
2016-05-22 00:18:16 +00:00
2020-11-19 18:37:22 +00:00
EDITOR_DEF ( " debugger/profiler_frame_max_functions " , 512 ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
frame_delay = memnew ( Timer ) ;
2016-05-22 00:18:16 +00:00
frame_delay - > set_wait_time ( 0.1 ) ;
frame_delay - > set_one_shot ( true ) ;
add_child ( frame_delay ) ;
2017-03-05 15:44:50 +00:00
frame_delay - > connect ( " timeout " , this , " _update_frame " ) ;
2016-05-22 00:18:16 +00:00
2017-03-05 15:44:50 +00:00
plot_delay = memnew ( Timer ) ;
2016-05-22 00:18:16 +00:00
plot_delay - > set_wait_time ( 0.1 ) ;
plot_delay - > set_one_shot ( true ) ;
add_child ( plot_delay ) ;
2017-03-05 15:44:50 +00:00
plot_delay - > connect ( " timeout " , this , " _update_plot " ) ;
2016-05-22 00:18:16 +00:00
2017-09-30 14:19:07 +00:00
plot_sigs . insert ( " physics_frame_time " ) ;
2016-05-22 00:18:16 +00:00
plot_sigs . insert ( " category_frame_time " ) ;
2017-03-05 15:44:50 +00:00
seeking = false ;
graph_height = 1 ;
2016-05-22 00:18:16 +00:00
}