Fix warning about missing newline at the EOF
[maemo-rb.git] / apps / plugins / test_fps.c
blob4514aa61bbe480bc81ed5b4c44d81e4f5b246d89
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 CONFIG_KEYPAD == MPIO_HD300_PAD
37 #define FPS_QUIT (BUTTON_REC|BUTTON_REPEAT)
38 #elif CONFIG_KEYPAD == RK27XX_GENERIC_PAD
39 #define FPS_QUIT (BUTTON_M|BUTTON_REPEAT)
40 #elif CONFIG_KEYPAD == SAMSUNG_YPR0_PAD
41 #define FPS_QUIT BUTTON_BACK
42 #elif defined(BUTTON_OFF)
43 #define FPS_QUIT BUTTON_OFF
44 #else
45 #define FPS_QUIT BUTTON_POWER
46 #endif
48 #define DURATION (2*HZ) /* longer duration gives more precise results */
52 /* Screen logging */
53 static int line;
54 static int max_line;
55 #ifdef HAVE_REMOTE_LCD
56 static int remote_line;
57 static int remote_max_line;
58 #endif
60 static void log_init(void)
62 int h;
64 rb->lcd_getstringsize("A", NULL, &h);
65 max_line = LCD_HEIGHT / h;
66 line = 0;
67 rb->lcd_clear_display();
68 rb->lcd_update();
69 #ifdef HAVE_REMOTE_LCD
70 rb->lcd_remote_getstringsize("A", NULL, &h);
71 remote_max_line = LCD_REMOTE_HEIGHT / h;
72 remote_line = 0;
73 rb->lcd_remote_clear_display();
74 rb->lcd_remote_update();
75 #endif
78 static void log_text(char *text)
80 rb->lcd_puts(0, line, text);
81 if (++line >= max_line)
82 line = 0;
83 rb->lcd_update();
84 #ifdef HAVE_REMOTE_LCD
85 rb->lcd_remote_puts(0, remote_line, text);
86 if (++remote_line >= remote_max_line)
87 remote_line = 0;
88 rb->lcd_remote_update();
89 #endif
92 static int calc_tenth_fps(int framecount, long ticks)
94 return (10*HZ) * framecount / ticks;
97 static void time_main_update(void)
99 char str[32]; /* text buffer */
100 long time_start; /* start tickcount */
101 long time_end; /* end tickcount */
102 int frame_count;
103 int fps;
105 const int part14_x = LCD_WIDTH/4; /* x-offset for 1/4 update test */
106 const int part14_w = LCD_WIDTH/2; /* x-size for 1/4 update test */
107 const int part14_y = LCD_HEIGHT/4; /* y-offset for 1/4 update test */
108 const int part14_h = LCD_HEIGHT/2; /* y-size for 1/4 update test */
110 log_text("Main LCD Update");
112 /* Test 1: full LCD update */
113 frame_count = 0;
114 rb->sleep(0); /* sync to tick */
115 time_start = *rb->current_tick;
116 while((time_end = *rb->current_tick) - time_start < DURATION)
118 rb->lcd_update();
119 frame_count++;
121 fps = calc_tenth_fps(frame_count, time_end - time_start);
122 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
123 log_text(str);
125 /* Test 2: quarter LCD update */
126 frame_count = 0;
127 rb->sleep(0); /* sync to tick */
128 time_start = *rb->current_tick;
129 while((time_end = *rb->current_tick) - time_start < DURATION)
131 rb->lcd_update_rect(part14_x, part14_y, part14_w, part14_h);
132 frame_count++;
134 fps = calc_tenth_fps(frame_count, time_end - time_start);
135 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
136 log_text(str);
139 #if defined(HAVE_LCD_COLOR) && (MEMORYSIZE > 2)
141 #if LCD_WIDTH >= LCD_HEIGHT
142 #define YUV_WIDTH LCD_WIDTH
143 #define YUV_HEIGHT LCD_HEIGHT
144 #else /* Assume the screen is rotated on portrait LCDs */
145 #define YUV_WIDTH LCD_HEIGHT
146 #define YUV_HEIGHT LCD_WIDTH
147 #endif
149 static unsigned char ydata[YUV_HEIGHT][YUV_WIDTH];
150 static unsigned char udata[YUV_HEIGHT/2][YUV_WIDTH/2];
151 static unsigned char vdata[YUV_HEIGHT/2][YUV_WIDTH/2];
153 static unsigned char * const yuvbuf[3] = {
154 (void*)ydata,
155 (void*)udata,
156 (void*)vdata
159 static void make_gradient_rect(int width, int height)
161 unsigned char vline[YUV_WIDTH/2];
162 int x, y;
164 width /= 2;
165 height /= 2;
167 for (x = 0; x < width; x++)
168 vline[x] = (x << 8) / width;
169 for (y = 0; y < height; y++)
171 rb->memset(udata[y], (y << 8) / height, width);
172 rb->memcpy(vdata[y], vline, width);
176 static void time_main_yuv(void)
178 char str[32]; /* text buffer */
179 long time_start; /* start tickcount */
180 long time_end; /* end tickcount */
181 int frame_count;
182 int fps;
184 const int part14_x = YUV_WIDTH/4; /* x-offset for 1/4 update test */
185 const int part14_w = YUV_WIDTH/2; /* x-size for 1/4 update test */
186 const int part14_y = YUV_HEIGHT/4; /* y-offset for 1/4 update test */
187 const int part14_h = YUV_HEIGHT/2; /* y-size for 1/4 update test */
189 log_text("Main LCD YUV");
191 rb->memset(ydata, 128, sizeof(ydata)); /* medium grey */
193 /* Test 1: full LCD update */
194 make_gradient_rect(YUV_WIDTH, YUV_HEIGHT);
196 frame_count = 0;
197 rb->sleep(0); /* sync to tick */
198 time_start = *rb->current_tick;
199 while((time_end = *rb->current_tick) - time_start < DURATION)
201 rb->lcd_blit_yuv(yuvbuf, 0, 0, YUV_WIDTH,
202 0, 0, YUV_WIDTH, YUV_HEIGHT);
203 frame_count++;
205 fps = calc_tenth_fps(frame_count, time_end - time_start);
206 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
207 log_text(str);
209 /* Test 2: quarter LCD update */
210 make_gradient_rect(YUV_WIDTH/2, YUV_HEIGHT/2);
212 frame_count = 0;
213 rb->sleep(0); /* sync to tick */
214 time_start = *rb->current_tick;
215 while((time_end = *rb->current_tick) - time_start < DURATION)
217 rb->lcd_blit_yuv(yuvbuf, 0, 0, YUV_WIDTH,
218 part14_x, part14_y, part14_w, part14_h);
219 frame_count++;
221 fps = calc_tenth_fps(frame_count, time_end - time_start);
222 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
223 log_text(str);
225 #endif
227 #ifdef HAVE_REMOTE_LCD
228 static void time_remote_update(void)
230 char str[32]; /* text buffer */
231 long time_start; /* start tickcount */
232 long time_end; /* end tickcount */
233 int frame_count;
234 int fps;
236 const int part14_x = LCD_REMOTE_WIDTH/4; /* x-offset for 1/4 update test */
237 const int part14_w = LCD_REMOTE_WIDTH/2; /* x-size for 1/4 update test */
238 const int part14_y = LCD_REMOTE_HEIGHT/4; /* y-offset for 1/4 update test */
239 const int part14_h = LCD_REMOTE_HEIGHT/2; /* y-size for 1/4 update test */
241 log_text("Remote LCD Update");
243 /* Test 1: full LCD update */
244 frame_count = 0;
245 rb->sleep(0); /* sync to tick */
246 time_start = *rb->current_tick;
247 while((time_end = *rb->current_tick) - time_start < DURATION)
249 rb->lcd_remote_update();
250 frame_count++;
252 fps = calc_tenth_fps(frame_count, time_end - time_start);
253 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
254 log_text(str);
256 /* Test 2: quarter LCD update */
257 frame_count = 0;
258 rb->sleep(0); /* sync to tick */
259 time_start = *rb->current_tick;
260 while((time_end = *rb->current_tick) - time_start < DURATION)
262 rb->lcd_remote_update_rect(part14_x, part14_y, part14_w, part14_h);
263 frame_count++;
265 fps = calc_tenth_fps(frame_count, time_end - time_start);
266 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
267 log_text(str);
269 #endif
271 #if LCD_DEPTH < 4
273 GREY_INFO_STRUCT_IRAM
274 static unsigned char greydata[LCD_HEIGHT][LCD_WIDTH];
276 static void make_grey_rect(int width, int height)
278 unsigned char vline[LCD_WIDTH];
279 int x, y;
281 for (x = 0; x < width; x++)
282 vline[x] = (x << 8) / width;
283 for (y = 0; y < height; y++)
284 rb->memcpy(greydata[y], vline, width);
287 static void time_greyscale(void)
289 char str[32]; /* text buffer */
290 long time_start; /* start tickcount */
291 long time_end; /* end tickcount */
292 long time_1, time_2;
293 int frames_1, frames_2;
294 int fps, load;
295 size_t gbuf_size;
296 unsigned char *gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
298 #if NUM_CORES > 1
299 int i;
300 for (i = 0; i < NUM_CORES; i++)
302 rb->snprintf(str, sizeof(str), "Greyscale (%s)",
303 (i > 0) ? "COP" : "CPU");
304 log_text(str);
305 #else
306 const int i = 0;
307 log_text("Greyscale library");
309 #endif
311 if (!grey_init(gbuf, gbuf_size, (i > 0) ? GREY_ON_COP : 0,
312 LCD_WIDTH, LCD_HEIGHT, NULL))
314 log_text("greylib: out of memory.");
315 return;
317 make_grey_rect(LCD_WIDTH, LCD_HEIGHT);
319 /* Test 1 - greyscale overlay not yet enabled */
320 frames_1 = 0;
321 rb->sleep(0); /* sync to tick */
322 time_start = *rb->current_tick;
323 while((time_end = *rb->current_tick) - time_start < DURATION)
325 grey_ub_gray_bitmap(greydata[0], 0, 0, LCD_WIDTH, LCD_HEIGHT);
326 frames_1++;
328 time_1 = time_end - time_start;
330 /* Test 2 - greyscale overlay enabled */
331 grey_show(true);
332 frames_2 = 0;
333 rb->sleep(0); /* sync to tick */
334 time_start = *rb->current_tick;
335 while((time_end = *rb->current_tick) - time_start < DURATION)
337 grey_ub_gray_bitmap(greydata[0], 0, 0, LCD_WIDTH, LCD_HEIGHT);
338 frames_2++;
340 time_2 = time_end - time_start;
342 grey_release();
343 fps = calc_tenth_fps(frames_2, time_2);
344 load = 100 - (100 * frames_2 * time_1) / (frames_1 * time_2);
345 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
346 log_text(str);
348 if (load > 0 && load < 100)
350 rb->snprintf(str, sizeof(str), "CPU load: %d%%", load);
351 log_text(str);
353 else
354 log_text("CPU load err (boost?)");
357 #endif
359 /* plugin entry point */
360 enum plugin_status plugin_start(const void* parameter)
362 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
363 char str[32];
364 int cpu_freq;
365 #endif
367 /* standard stuff */
368 (void)parameter;
370 log_init();
371 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
372 cpu_freq = *rb->cpu_frequency; /* remember CPU frequency */
373 #endif
374 backlight_ignore_timeout();
376 time_main_update();
377 rb->sleep(HZ);
378 #if defined(HAVE_LCD_COLOR) && (MEMORYSIZE > 2)
379 time_main_yuv();
380 #endif
381 #if LCD_DEPTH < 4
382 time_greyscale();
383 #endif
384 #ifdef HAVE_REMOTE_LCD
385 time_remote_update();
386 #endif
388 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
389 if (*rb->cpu_frequency != cpu_freq)
390 rb->snprintf(str, sizeof(str), "CPU clock changed!");
391 else
392 rb->snprintf(str, sizeof(str), "CPU: %d MHz",
393 (cpu_freq + 500000) / 1000000);
394 log_text(str);
395 #endif
396 backlight_use_settings();
398 /* wait until user closes plugin */
399 while (rb->button_get(true) != FPS_QUIT);
401 return PLUGIN_OK;