22
33#if IS_DWIN_MARLINUI && HAS_GAMES
44
5- #define PERFORMANCE_COUNTERS 1
5+ // Enable performance counters (draw call count, frame timing) for debugging
6+ #define GAME_PERFORMANCE_COUNTERS 1
67
78#include " ../../menu/game/types.h" // includes e3v2/marlinui/game.h
89#include " ../../lcdprint.h"
910#include " lcdprint_dwin.h"
1011#include " marlinui_dwin.h"
1112
12- #if PERFORMANCE_COUNTERS == 1
13- static uint32_t draw_call_cnt = 0 ; // total number of draw calls in the current frame
14- static millis_t frame_draw_millis = 0 , // time spent drawing the frame
15- frame_wait_millis = 0 ; // time spent waiting for the next frame
16-
17- #define DRAW_CALL_INC (subcall_cnt ) draw_call_cnt++
18- #else
19- #define DRAW_CALL_INC (subcall_cnt )
13+ #if ENABLED(GAME_PERFORMANCE_COUNTERS)
14+ typedef struct {
15+ /* *
16+ * Number of draw calls sent to the LCD
17+ */
18+ uint32_t draw_calls;
19+
20+ /* *
21+ * millis() value at the start of the current frame
22+ */
23+ millis_t frame_draw_millis;
24+
25+ /* *
26+ * millis() value at the end of the previous frame (in frame_start)
27+ * or time spend waiting for the next frame (in frame_end)
28+ */
29+ millis_t frame_wait_millis;
30+ } dwin_game_perf_t ;
31+
32+ static dwin_game_perf_t dwin_game_perf;
2033#endif
2134
35+ #define COUNT_DRAW_CALLS (n ) TERN_(GAME_PERFORMANCE_COUNTERS, dwin_game_perf.draw_calls += n)
36+
2237void MarlinGame::frame_start () {
23- // clear the screen before each frame
38+ // Clear the screen before each frame
2439 // dwinFrameClear(CLEAR_COLOR);
2540
26- // filling the play area should be faster than clearing the whole screen
41+ // Instead of using dwinFrameClear, fill the play area with the background color
42+ // This tends to be faster than clearing the whole screen
2743 const uint16_t fg = dwin_font.fg ;
2844 dwin_font.fg = COLOR_BG_BLACK;
2945 draw_box (0 , 0 , GAME_WIDTH, GAME_HEIGHT);
3046 dwin_font.fg = fg;
3147
48+ // Ensure the correct font is selected
3249 dwin_font.index = DWIN_FONT_MENU;
3350
34- // reset performance counters
35- #if PERFORMANCE_COUNTERS == 1
36- draw_call_cnt = 0 ;
37- frame_draw_millis = millis ();
38- frame_wait_millis = frame_draw_millis - frame_wait_millis;
51+ #if ENABLED(GAME_PERFORMANCE_COUNTERS)
52+ // Reset draw call counters
53+ dwin_game_perf.draw_calls = 0 ;
54+
55+ // Update timing information
56+ const millis_t now = millis ();
57+ dwin_game_perf.frame_draw_millis = now;
58+ dwin_game_perf.frame_wait_millis = now - dwin_game_perf.frame_wait_millis ;
3959 #endif
4060}
4161
4262void MarlinGame::frame_end () {
43- #if PERFORMANCE_COUNTERS == 1
44- const millis_t frame_wait = frame_wait_millis;
45- frame_wait_millis = millis ();
46- frame_draw_millis = frame_wait_millis - frame_draw_millis;
47-
48- // calculate frames per deci-seconds (100 milliseconds)
49- const uint32_t fpds = 100 / (frame_draw_millis + frame_wait);
63+ #if ENABLED(GAME_PERFORMANCE_COUNTERS)
64+ const millis_t now = millis ();
65+ const millis_t frame_wait_millis = dwin_game_perf.frame_wait_millis ;
66+ const millis_t frame_draw_millis = now - dwin_game_perf.frame_draw_millis ;
5067
51- // format the performance counters as a string
52- char perf_str[64 ];
53- sprintf_P (
54- perf_str,
55- PSTR (" d%04lu w%04lu c%04lu f%02lu " ),
56- frame_draw_millis,
57- frame_wait,
58- draw_call_cnt,
59- fpds
60- );
68+ dwin_game_perf.frame_wait_millis = now;
6169
62- // draw the performance counters at the (physical) origin of the screen
70+ // Save previous font settings and set new ones
6371 const uint16_t fg = dwin_font.fg ;
6472 const bool solid = dwin_font.solid ;
65- dwin_font. fg = RGB ( 0x1F , 0x3F , 0x00 );
73+ set_color (color::YELLOW );
6674 dwin_font.solid = true ;
6775
76+ // Draw performance counters information
77+ char perf_str[32 ];
78+ sprintf_P (
79+ perf_str,
80+ PSTR (" d%04lu w%04lu c%04lu" ),
81+ frame_draw_millis,
82+ frame_wait_millis,
83+ dwin_game_perf.draw_calls
84+ );
6885 lcd_moveto_xy (0 , 0 );
6986 lcd_put_u8str (perf_str);
7087
88+ // Restore previous font settings
7189 dwin_font.fg = fg;
7290 dwin_font.solid = solid;
7391 #endif
@@ -107,17 +125,17 @@ void MarlinGame::set_color(const color color) {
107125}
108126
109127void MarlinGame::draw_hline (const game_dim_t x, const game_dim_t y, const game_dim_t w) {
110- // draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
128+ // Draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
111129 draw_box (x, y, w, 1 );
112130
113- DRAW_CALL_INC (1 );
131+ COUNT_DRAW_CALLS (1 );
114132}
115133
116134void MarlinGame::draw_vline (const game_dim_t x, const game_dim_t y, const game_dim_t h) {
117- // draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
135+ // Draw lines as boxes, since DWIN lines are always 1px wide but we want to scale them
118136 draw_box (x, y, 1 , h);
119137
120- DRAW_CALL_INC (1 );
138+ COUNT_DRAW_CALLS (1 );
121139}
122140
123141void MarlinGame::draw_frame (const game_dim_t x, const game_dim_t y, const game_dim_t w, const game_dim_t h) {
@@ -130,7 +148,7 @@ void MarlinGame::draw_frame(const game_dim_t x, const game_dim_t y, const game_d
130148 dwin_game::game_to_screen (h)
131149 );
132150
133- DRAW_CALL_INC ( 0 );
151+ COUNT_DRAW_CALLS ( 1 );
134152}
135153
136154void MarlinGame::draw_box (const game_dim_t x, const game_dim_t y, const game_dim_t w, const game_dim_t h) {
@@ -143,17 +161,15 @@ void MarlinGame::draw_box(const game_dim_t x, const game_dim_t y, const game_dim
143161 dwin_game::game_to_screen (h)
144162 );
145163
146- DRAW_CALL_INC ( 0 );
164+ COUNT_DRAW_CALLS ( 1 );
147165}
148166
149167void MarlinGame::draw_pixel (const game_dim_t x, const game_dim_t y) {
150- // draw pixels using boxes.
151- // while DWIN protocol supports drawing points with different sizes, the
152- // 0x02 'draw point' command is way slower per pixel than 0x05 'fill rectangle'
168+ // Draw pixels using boxes.
169+ // While DWIN protocol supports drawing points with different sizes, the
170+ // 0x02 'draw point' command is slower per pixel than 0x05 'fill rectangle'
153171 // (0.4 us vs 0.14 us per pixel)
154172 draw_box (x, y, 1 , 1 );
155-
156- DRAW_CALL_INC (1 );
157173}
158174
159175void MarlinGame::draw_bitmap (const game_dim_t x, const game_dim_t y, const game_dim_t bytes_per_row, const game_dim_t rows, const pgm_bitmap_t bitmap) {
@@ -163,20 +179,19 @@ void MarlinGame::draw_bitmap(const game_dim_t x, const game_dim_t y, const game_
163179
164180 #if DISABLED(TJC_DISPLAY)
165181 // DWIN T5UI actually supports drawing multiple points in one go using the 0x02 'draw point' command, ever since kernel 1.2.
166- // So we use that to draw the bitmap as a series of points, which should be faster than drawing rectangles using draw_pixel.
167- // This will be somewhat slow, but way faster than drawing rectangles one by one.
182+ // So we use that to draw the bitmap as a series of points, which is faster than drawing rectangles using draw_pixel.
168183 dwinDrawPointMap (
169- dwin_font.fg , // color
170- dwin_game::game_to_screen (1 ), // point size
184+ dwin_font.fg ,
185+ dwin_game::game_to_screen (1 ),
171186 dwin_game::game_to_screen (1 ),
172- dwin_game::game_to_screen (x) + dwin_game::x_offset, // x / y
187+ dwin_game::game_to_screen (x) + dwin_game::x_offset,
173188 dwin_game::game_to_screen (y) + dwin_game::y_offset,
174- bytes_per_row, // bitmap dimensions
189+ bytes_per_row,
175190 rows,
176- bitmap // U8G bitmap format is compatible to DrawPointMap format
191+ bitmap
177192 );
178193
179- DRAW_CALL_INC ( 0 );
194+ COUNT_DRAW_CALLS ( 1 );
180195 #else
181196 // TJC displays don't seem to support the 0x02 'draw point' command, so instead we have to draw the bitmap
182197 // as a series of rectangles using draw_pixel.
@@ -185,10 +200,10 @@ void MarlinGame::draw_bitmap(const game_dim_t x, const game_dim_t y, const game_
185200 for (game_dim_t col = 0 ; col < bytes_per_row; col++) {
186201 const uint8_t byte = bitmap[(row * bytes_per_row) + col];
187202 for (uint8_t bit = 0 ; bit < 8 ; bit++) {
188- // assume that the screen area is cleared before drawing
203+ // Assuming that the drawing area was cleared before drawing
189204 if (byte & (1 << bit)) {
190205 draw_pixel (x + (col * 8 ) + (7 - bit + 1 ), y + row);
191- DRAW_CALL_INC (1 );
206+ COUNT_DRAW_CALLS (1 );
192207 }
193208 }
194209 }
@@ -197,7 +212,7 @@ void MarlinGame::draw_bitmap(const game_dim_t x, const game_dim_t y, const game_
197212}
198213
199214int MarlinGame::draw_string (const game_dim_t x, const game_dim_t y, const char * str) {
200- DRAW_CALL_INC ( 0 );
215+ COUNT_DRAW_CALLS ( 1 );
201216
202217 lcd_moveto_xy (
203218 dwin_game::game_to_screen (x) + dwin_game::x_offset,
@@ -215,7 +230,7 @@ int MarlinGame::draw_string(const game_dim_t x, const game_dim_t y, FSTR_P const
215230}
216231
217232void MarlinGame::draw_int (const game_dim_t x, const game_dim_t y, const int value) {
218- COUNT_DRAW_CALL ( 0 );
233+ COUNT_DRAW_CALLS ( 1 );
219234
220235 lcd_moveto_xy (
221236 dwin_game::game_to_screen (x) + dwin_game::x_offset,
0 commit comments