when changing settings from the Talk and Voice window also update the main widgets...
[Rockbox.git] / apps / plugins / test_fps.c
blobedb6a681239c36bb883bcbf88037882fe5c154dd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Peter D'Hoye
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
20 #include "helper.h"
21 #include "grey.h"
23 #ifdef HAVE_LCD_BITMAP
25 PLUGIN_IRAM_DECLARE
27 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
28 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
29 #define FPS_QUIT BUTTON_MENU
30 #elif defined(BUTTON_OFF)
31 #define FPS_QUIT BUTTON_OFF
32 #else
33 #define FPS_QUIT BUTTON_POWER
34 #endif
36 #define DURATION (2*HZ) /* longer duration gives more precise results */
38 PLUGIN_HEADER
40 static struct plugin_api* rb;
42 /* Screen logging */
43 static int line;
44 static int max_line;
45 #ifdef HAVE_REMOTE_LCD
46 static int remote_line;
47 static int remote_max_line;
48 #endif
49 #if LCD_DEPTH < 4
50 static unsigned char *gbuf;
51 static size_t gbuf_size;
52 #endif
54 static void log_init(void)
56 int h;
58 rb->lcd_setmargins(0, 0);
59 rb->lcd_getstringsize("A", NULL, &h);
60 max_line = LCD_HEIGHT / h;
61 line = 0;
62 rb->lcd_clear_display();
63 rb->lcd_update();
64 #ifdef HAVE_REMOTE_LCD
65 rb->lcd_remote_setmargins(0, 0);
66 rb->lcd_remote_getstringsize("A", NULL, &h);
67 remote_max_line = LCD_REMOTE_HEIGHT / h;
68 remote_line = 0;
69 rb->lcd_remote_clear_display();
70 rb->lcd_remote_update();
71 #endif
74 static void log_text(char *text)
76 rb->lcd_puts(0, line, text);
77 if (++line >= max_line)
78 line = 0;
79 rb->lcd_update();
80 #ifdef HAVE_REMOTE_LCD
81 rb->lcd_remote_puts(0, remote_line, text);
82 if (++remote_line >= remote_max_line)
83 remote_line = 0;
84 rb->lcd_remote_update();
85 #endif
88 static int calc_tenth_fps(int framecount, long ticks)
90 return (10*HZ) * framecount / ticks;
93 static void time_main_update(void)
95 char str[32]; /* text buffer */
96 long time_start; /* start tickcount */
97 long time_end; /* end tickcount */
98 int frame_count;
99 int fps;
101 const int part14_x = LCD_WIDTH/4; /* x-offset for 1/4 update test */
102 const int part14_w = LCD_WIDTH/2; /* x-size for 1/4 update test */
103 const int part14_y = LCD_HEIGHT/4; /* y-offset for 1/4 update test */
104 const int part14_h = LCD_HEIGHT/2; /* y-size for 1/4 update test */
106 /* Test 1: full LCD update */
107 frame_count = 0;
108 rb->sleep(0); /* sync to tick */
109 time_start = *rb->current_tick;
110 while((time_end = *rb->current_tick) - time_start < DURATION)
112 rb->lcd_update();
113 frame_count++;
115 fps = calc_tenth_fps(frame_count, time_end - time_start);
116 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
117 log_text(str);
119 /* Test 2: quarter LCD update */
120 frame_count = 0;
121 rb->sleep(0); /* sync to tick */
122 time_start = *rb->current_tick;
123 while((time_end = *rb->current_tick) - time_start < DURATION)
125 rb->lcd_update_rect(part14_x, part14_y, part14_w, part14_h);
126 frame_count++;
128 fps = calc_tenth_fps(frame_count, time_end - time_start);
129 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
130 log_text(str);
133 #ifdef HAVE_LCD_COLOR
135 #if LCD_WIDTH >= LCD_HEIGHT
136 #define YUV_WIDTH LCD_WIDTH
137 #define YUV_HEIGHT LCD_HEIGHT
138 #else /* Assume the screen is rotated on portrait LCDs */
139 #define YUV_WIDTH LCD_HEIGHT
140 #define YUV_HEIGHT LCD_WIDTH
141 #endif
143 static unsigned char ydata[YUV_HEIGHT][YUV_WIDTH];
144 static unsigned char udata[YUV_HEIGHT/2][YUV_WIDTH/2];
145 static unsigned char vdata[YUV_HEIGHT/2][YUV_WIDTH/2];
147 static unsigned char * const yuvbuf[3] = {
148 (void*)ydata,
149 (void*)udata,
150 (void*)vdata
153 static void make_gradient_rect(int width, int height)
155 unsigned char vline[YUV_WIDTH/2];
156 int x, y;
158 width /= 2;
159 height /= 2;
161 for (x = 0; x < width; x++)
162 vline[x] = (x << 8) / width;
163 for (y = 0; y < height; y++)
165 rb->memset(udata[y], (y << 8) / height, width);
166 rb->memcpy(vdata[y], vline, width);
170 static void time_main_yuv(void)
172 char str[32]; /* text buffer */
173 long time_start; /* start tickcount */
174 long time_end; /* end tickcount */
175 int frame_count;
176 int fps;
178 const int part14_x = YUV_WIDTH/4; /* x-offset for 1/4 update test */
179 const int part14_w = YUV_WIDTH/2; /* x-size for 1/4 update test */
180 const int part14_y = YUV_HEIGHT/4; /* y-offset for 1/4 update test */
181 const int part14_h = YUV_HEIGHT/2; /* y-size for 1/4 update test */
183 rb->memset(ydata, 128, sizeof(ydata)); /* medium grey */
185 /* Test 1: full LCD update */
186 make_gradient_rect(YUV_WIDTH, YUV_HEIGHT);
188 frame_count = 0;
189 rb->sleep(0); /* sync to tick */
190 time_start = *rb->current_tick;
191 while((time_end = *rb->current_tick) - time_start < DURATION)
193 rb->lcd_yuv_blit(yuvbuf, 0, 0, YUV_WIDTH,
194 0, 0, YUV_WIDTH, YUV_HEIGHT);
195 frame_count++;
197 fps = calc_tenth_fps(frame_count, time_end - time_start);
198 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
199 log_text(str);
201 /* Test 2: quarter LCD update */
202 make_gradient_rect(YUV_WIDTH/2, YUV_HEIGHT/2);
204 frame_count = 0;
205 rb->sleep(0); /* sync to tick */
206 time_start = *rb->current_tick;
207 while((time_end = *rb->current_tick) - time_start < DURATION)
209 rb->lcd_yuv_blit(yuvbuf, 0, 0, YUV_WIDTH,
210 part14_x, part14_y, part14_w, part14_h);
211 frame_count++;
213 fps = calc_tenth_fps(frame_count, time_end - time_start);
214 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
215 log_text(str);
217 #endif
219 #ifdef HAVE_REMOTE_LCD
220 static void time_remote_update(void)
222 char str[32]; /* text buffer */
223 long time_start; /* start tickcount */
224 long time_end; /* end tickcount */
225 int frame_count;
226 int fps;
228 const int part14_x = LCD_REMOTE_WIDTH/4; /* x-offset for 1/4 update test */
229 const int part14_w = LCD_REMOTE_WIDTH/2; /* x-size for 1/4 update test */
230 const int part14_y = LCD_REMOTE_HEIGHT/4; /* y-offset for 1/4 update test */
231 const int part14_h = LCD_REMOTE_HEIGHT/2; /* y-size for 1/4 update test */
233 /* Test 1: full LCD update */
234 frame_count = 0;
235 rb->sleep(0); /* sync to tick */
236 time_start = *rb->current_tick;
237 while((time_end = *rb->current_tick) - time_start < DURATION)
239 rb->lcd_remote_update();
240 frame_count++;
242 fps = calc_tenth_fps(frame_count, time_end - time_start);
243 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
244 log_text(str);
246 /* Test 2: quarter LCD update */
247 frame_count = 0;
248 rb->sleep(0); /* sync to tick */
249 time_start = *rb->current_tick;
250 while((time_end = *rb->current_tick) - time_start < DURATION)
252 rb->lcd_remote_update_rect(part14_x, part14_y, part14_w, part14_h);
253 frame_count++;
255 fps = calc_tenth_fps(frame_count, time_end - time_start);
256 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
257 log_text(str);
259 #endif
261 #if LCD_DEPTH < 4
263 GREY_INFO_STRUCT_IRAM
264 static unsigned char greydata[LCD_HEIGHT][LCD_WIDTH];
266 static void make_grey_rect(int width, int height)
268 unsigned char vline[LCD_WIDTH];
269 int x, y;
271 for (x = 0; x < width; x++)
272 vline[x] = (x << 8) / width;
273 for (y = 0; y < height; y++)
274 rb->memcpy(greydata[y], vline, width);
277 static void time_greyscale(void)
279 char str[32]; /* text buffer */
280 long time_start; /* start tickcount */
281 long time_end; /* end tickcount */
282 long time_1, time_2;
283 int frames_1, frames_2;
284 int fps, load;
286 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
287 if (!grey_init(rb, gbuf, gbuf_size, 0, LCD_WIDTH, LCD_HEIGHT, NULL))
289 log_text("greylib: out of memory.");
290 return;
292 make_grey_rect(LCD_WIDTH, LCD_HEIGHT);
294 /* Test 1 - greyscale overlay not yet enabled */
295 frames_1 = 0;
296 rb->sleep(0); /* sync to tick */
297 time_start = *rb->current_tick;
298 while((time_end = *rb->current_tick) - time_start < DURATION)
300 grey_ub_gray_bitmap(greydata[0], 0, 0, LCD_WIDTH, LCD_HEIGHT);
301 frames_1++;
303 time_1 = time_end - time_start;
305 /* Test 2 - greyscale overlay enabled */
306 grey_show(true);
307 frames_2 = 0;
308 rb->sleep(0); /* sync to tick */
309 time_start = *rb->current_tick;
310 while((time_end = *rb->current_tick) - time_start < DURATION)
312 grey_ub_gray_bitmap(greydata[0], 0, 0, LCD_WIDTH, LCD_HEIGHT);
313 frames_2++;
315 time_2 = time_end - time_start;
317 grey_release();
318 fps = calc_tenth_fps(frames_2, time_2);
319 load = 100 - (100 * frames_2 * time_1) / (frames_1 * time_2);
320 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
321 log_text(str);
323 if (load > 0 && load < 100)
325 rb->snprintf(str, sizeof(str), "CPU load: %d%%", load);
326 log_text(str);
328 else
329 log_text("CPU load err (boost?)");
331 #endif
333 /* plugin entry point */
334 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
336 #ifndef SIMULATOR
337 char str[32];
338 int cpu_freq;
339 #endif
341 /* standard stuff */
342 PLUGIN_IRAM_INIT(api)
343 (void)parameter;
344 rb = api;
346 log_init();
347 #ifndef SIMULATOR
348 cpu_freq = *rb->cpu_frequency; /* remember CPU frequency */
349 #endif
350 backlight_force_on(rb); /* backlight control in lib/helper.c */
352 log_text("Main LCD Update");
353 time_main_update();
354 #ifdef HAVE_LCD_COLOR
355 log_text("Main LCD YUV");
356 time_main_yuv();
357 #endif
358 #if LCD_DEPTH < 4
359 log_text("Greyscale library");
360 time_greyscale();
361 #endif
362 #ifdef HAVE_REMOTE_LCD
363 log_text("Remote LCD Update");
364 time_remote_update();
365 #endif
367 #ifndef SIMULATOR
368 if (*rb->cpu_frequency != cpu_freq)
369 rb->snprintf(str, sizeof(str), "CPU clock changed!");
370 else
371 rb->snprintf(str, sizeof(str), "CPU: %d MHz",
372 (cpu_freq + 500000) / 1000000);
373 log_text(str);
374 #endif
375 backlight_use_settings(rb); /* backlight control in lib/helper.c */
377 /* wait until user closes plugin */
378 while (rb->button_get(true) != FPS_QUIT);
380 return PLUGIN_OK;
382 #endif