Fix FS#11593 - %Vf() and %Vb() should be able to be used in conditionals. Remember...
[maemo-rb.git] / apps / plugins / test_fps.c
blob4f197e0d5b13b9b6ca3548d301e1cab611e3f6f0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Peter D'Hoye
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "lib/helper.h"
23 #include "lib/grey.h"
25 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
26 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
27 #define FPS_QUIT BUTTON_MENU
28 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
29 #define FPS_QUIT BUTTON_RC_REC
30 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
31 #define FPS_QUIT BUTTON_PLAY
32 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
33 #define FPS_QUIT (BUTTON_HOME|BUTTON_REPEAT)
34 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
35 #define FPS_QUIT (BUTTON_REC|BUTTON_PLAY)
36 #elif defined(BUTTON_OFF)
37 #define FPS_QUIT BUTTON_OFF
38 #else
39 #define FPS_QUIT BUTTON_POWER
40 #endif
42 #define DURATION (2*HZ) /* longer duration gives more precise results */
46 /* Screen logging */
47 static int line;
48 static int max_line;
49 #ifdef HAVE_REMOTE_LCD
50 static int remote_line;
51 static int remote_max_line;
52 #endif
54 static void log_init(void)
56 int h;
58 rb->lcd_getstringsize("A", NULL, &h);
59 max_line = LCD_HEIGHT / h;
60 line = 0;
61 rb->lcd_clear_display();
62 rb->lcd_update();
63 #ifdef HAVE_REMOTE_LCD
64 rb->lcd_remote_getstringsize("A", NULL, &h);
65 remote_max_line = LCD_REMOTE_HEIGHT / h;
66 remote_line = 0;
67 rb->lcd_remote_clear_display();
68 rb->lcd_remote_update();
69 #endif
72 static void log_text(char *text)
74 rb->lcd_puts(0, line, text);
75 if (++line >= max_line)
76 line = 0;
77 rb->lcd_update();
78 #ifdef HAVE_REMOTE_LCD
79 rb->lcd_remote_puts(0, remote_line, text);
80 if (++remote_line >= remote_max_line)
81 remote_line = 0;
82 rb->lcd_remote_update();
83 #endif
86 static int calc_tenth_fps(int framecount, long ticks)
88 return (10*HZ) * framecount / ticks;
91 static void time_main_update(void)
93 char str[32]; /* text buffer */
94 long time_start; /* start tickcount */
95 long time_end; /* end tickcount */
96 int frame_count;
97 int fps;
99 const int part14_x = LCD_WIDTH/4; /* x-offset for 1/4 update test */
100 const int part14_w = LCD_WIDTH/2; /* x-size for 1/4 update test */
101 const int part14_y = LCD_HEIGHT/4; /* y-offset for 1/4 update test */
102 const int part14_h = LCD_HEIGHT/2; /* y-size for 1/4 update test */
104 log_text("Main LCD Update");
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 #if defined(HAVE_LCD_COLOR) && (MEMORYSIZE > 2)
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 log_text("Main LCD YUV");
185 rb->memset(ydata, 128, sizeof(ydata)); /* medium grey */
187 /* Test 1: full LCD update */
188 make_gradient_rect(YUV_WIDTH, YUV_HEIGHT);
190 frame_count = 0;
191 rb->sleep(0); /* sync to tick */
192 time_start = *rb->current_tick;
193 while((time_end = *rb->current_tick) - time_start < DURATION)
195 rb->lcd_blit_yuv(yuvbuf, 0, 0, YUV_WIDTH,
196 0, 0, YUV_WIDTH, YUV_HEIGHT);
197 frame_count++;
199 fps = calc_tenth_fps(frame_count, time_end - time_start);
200 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
201 log_text(str);
203 /* Test 2: quarter LCD update */
204 make_gradient_rect(YUV_WIDTH/2, YUV_HEIGHT/2);
206 frame_count = 0;
207 rb->sleep(0); /* sync to tick */
208 time_start = *rb->current_tick;
209 while((time_end = *rb->current_tick) - time_start < DURATION)
211 rb->lcd_blit_yuv(yuvbuf, 0, 0, YUV_WIDTH,
212 part14_x, part14_y, part14_w, part14_h);
213 frame_count++;
215 fps = calc_tenth_fps(frame_count, time_end - time_start);
216 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
217 log_text(str);
219 #endif
221 #ifdef HAVE_REMOTE_LCD
222 static void time_remote_update(void)
224 char str[32]; /* text buffer */
225 long time_start; /* start tickcount */
226 long time_end; /* end tickcount */
227 int frame_count;
228 int fps;
230 const int part14_x = LCD_REMOTE_WIDTH/4; /* x-offset for 1/4 update test */
231 const int part14_w = LCD_REMOTE_WIDTH/2; /* x-size for 1/4 update test */
232 const int part14_y = LCD_REMOTE_HEIGHT/4; /* y-offset for 1/4 update test */
233 const int part14_h = LCD_REMOTE_HEIGHT/2; /* y-size for 1/4 update test */
235 log_text("Remote LCD Update");
237 /* Test 1: full LCD update */
238 frame_count = 0;
239 rb->sleep(0); /* sync to tick */
240 time_start = *rb->current_tick;
241 while((time_end = *rb->current_tick) - time_start < DURATION)
243 rb->lcd_remote_update();
244 frame_count++;
246 fps = calc_tenth_fps(frame_count, time_end - time_start);
247 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
248 log_text(str);
250 /* Test 2: quarter LCD update */
251 frame_count = 0;
252 rb->sleep(0); /* sync to tick */
253 time_start = *rb->current_tick;
254 while((time_end = *rb->current_tick) - time_start < DURATION)
256 rb->lcd_remote_update_rect(part14_x, part14_y, part14_w, part14_h);
257 frame_count++;
259 fps = calc_tenth_fps(frame_count, time_end - time_start);
260 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
261 log_text(str);
263 #endif
265 #if LCD_DEPTH < 4
267 GREY_INFO_STRUCT_IRAM
268 static unsigned char greydata[LCD_HEIGHT][LCD_WIDTH];
270 static void make_grey_rect(int width, int height)
272 unsigned char vline[LCD_WIDTH];
273 int x, y;
275 for (x = 0; x < width; x++)
276 vline[x] = (x << 8) / width;
277 for (y = 0; y < height; y++)
278 rb->memcpy(greydata[y], vline, width);
281 static void time_greyscale(void)
283 char str[32]; /* text buffer */
284 long time_start; /* start tickcount */
285 long time_end; /* end tickcount */
286 long time_1, time_2;
287 int frames_1, frames_2;
288 int fps, load;
289 size_t gbuf_size;
290 unsigned char *gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
292 #if NUM_CORES > 1
293 int i;
294 for (i = 0; i < NUM_CORES; i++)
296 rb->snprintf(str, sizeof(str), "Greyscale (%s)",
297 (i > 0) ? "COP" : "CPU");
298 log_text(str);
299 #else
300 const int i = 0;
301 log_text("Greyscale library");
303 #endif
305 if (!grey_init(gbuf, gbuf_size, (i > 0) ? GREY_ON_COP : 0,
306 LCD_WIDTH, LCD_HEIGHT, NULL))
308 log_text("greylib: out of memory.");
309 return;
311 make_grey_rect(LCD_WIDTH, LCD_HEIGHT);
313 /* Test 1 - greyscale overlay not yet enabled */
314 frames_1 = 0;
315 rb->sleep(0); /* sync to tick */
316 time_start = *rb->current_tick;
317 while((time_end = *rb->current_tick) - time_start < DURATION)
319 grey_ub_gray_bitmap(greydata[0], 0, 0, LCD_WIDTH, LCD_HEIGHT);
320 frames_1++;
322 time_1 = time_end - time_start;
324 /* Test 2 - greyscale overlay enabled */
325 grey_show(true);
326 frames_2 = 0;
327 rb->sleep(0); /* sync to tick */
328 time_start = *rb->current_tick;
329 while((time_end = *rb->current_tick) - time_start < DURATION)
331 grey_ub_gray_bitmap(greydata[0], 0, 0, LCD_WIDTH, LCD_HEIGHT);
332 frames_2++;
334 time_2 = time_end - time_start;
336 grey_release();
337 fps = calc_tenth_fps(frames_2, time_2);
338 load = 100 - (100 * frames_2 * time_1) / (frames_1 * time_2);
339 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
340 log_text(str);
342 if (load > 0 && load < 100)
344 rb->snprintf(str, sizeof(str), "CPU load: %d%%", load);
345 log_text(str);
347 else
348 log_text("CPU load err (boost?)");
351 #endif
353 /* plugin entry point */
354 enum plugin_status plugin_start(const void* parameter)
356 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
357 char str[32];
358 int cpu_freq;
359 #endif
361 /* standard stuff */
362 (void)parameter;
364 log_init();
365 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
366 cpu_freq = *rb->cpu_frequency; /* remember CPU frequency */
367 #endif
368 backlight_force_on(); /* backlight control in lib/helper.c */
370 time_main_update();
371 #if defined(HAVE_LCD_COLOR) && (MEMORYSIZE > 2)
372 time_main_yuv();
373 #endif
374 #if LCD_DEPTH < 4
375 time_greyscale();
376 #endif
377 #ifdef HAVE_REMOTE_LCD
378 time_remote_update();
379 #endif
381 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
382 if (*rb->cpu_frequency != cpu_freq)
383 rb->snprintf(str, sizeof(str), "CPU clock changed!");
384 else
385 rb->snprintf(str, sizeof(str), "CPU: %d MHz",
386 (cpu_freq + 500000) / 1000000);
387 log_text(str);
388 #endif
389 backlight_use_settings(); /* backlight control in lib/helper.c */
391 /* wait until user closes plugin */
392 while (rb->button_get(true) != FPS_QUIT);
394 return PLUGIN_OK;