fix red.
[kugel-rb.git] / apps / plugins / test_fps.c
blob62e7f489b0e4d0a90c37934da56b337d0548c469
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 #ifdef HAVE_LCD_BITMAP
27 PLUGIN_IRAM_DECLARE
29 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
30 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
31 #define FPS_QUIT BUTTON_MENU
32 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
33 #define FPS_QUIT BUTTON_RC_REC
34 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
35 #define FPS_QUIT BUTTON_PLAY
36 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
37 #define FPS_QUIT (BUTTON_HOME|BUTTON_REPEAT)
38 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
39 #define FPS_QUIT (BUTTON_REC|BUTTON_PLAY)
40 #elif defined(BUTTON_OFF)
41 #define FPS_QUIT BUTTON_OFF
42 #else
43 #define FPS_QUIT BUTTON_POWER
44 #endif
46 #define DURATION (2*HZ) /* longer duration gives more precise results */
48 PLUGIN_HEADER
50 /* Screen logging */
51 static int line;
52 static int max_line;
53 #ifdef HAVE_REMOTE_LCD
54 static int remote_line;
55 static int remote_max_line;
56 #endif
57 #if LCD_DEPTH < 4
58 static unsigned char *gbuf;
59 static size_t gbuf_size;
60 #endif
62 static void log_init(void)
64 int h;
66 rb->lcd_getstringsize("A", NULL, &h);
67 max_line = LCD_HEIGHT / h;
68 line = 0;
69 rb->lcd_clear_display();
70 rb->lcd_update();
71 #ifdef HAVE_REMOTE_LCD
72 rb->lcd_remote_getstringsize("A", NULL, &h);
73 remote_max_line = LCD_REMOTE_HEIGHT / h;
74 remote_line = 0;
75 rb->lcd_remote_clear_display();
76 rb->lcd_remote_update();
77 #endif
80 static void log_text(char *text)
82 rb->lcd_puts(0, line, text);
83 if (++line >= max_line)
84 line = 0;
85 rb->lcd_update();
86 #ifdef HAVE_REMOTE_LCD
87 rb->lcd_remote_puts(0, remote_line, text);
88 if (++remote_line >= remote_max_line)
89 remote_line = 0;
90 rb->lcd_remote_update();
91 #endif
94 static int calc_tenth_fps(int framecount, long ticks)
96 return (10*HZ) * framecount / ticks;
99 static void time_main_update(void)
101 char str[32]; /* text buffer */
102 long time_start; /* start tickcount */
103 long time_end; /* end tickcount */
104 int frame_count;
105 int fps;
107 const int part14_x = LCD_WIDTH/4; /* x-offset for 1/4 update test */
108 const int part14_w = LCD_WIDTH/2; /* x-size for 1/4 update test */
109 const int part14_y = LCD_HEIGHT/4; /* y-offset for 1/4 update test */
110 const int part14_h = LCD_HEIGHT/2; /* y-size for 1/4 update test */
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 rb->memset(ydata, 128, sizeof(ydata)); /* medium grey */
191 /* Test 1: full LCD update */
192 make_gradient_rect(YUV_WIDTH, YUV_HEIGHT);
194 frame_count = 0;
195 rb->sleep(0); /* sync to tick */
196 time_start = *rb->current_tick;
197 while((time_end = *rb->current_tick) - time_start < DURATION)
199 rb->lcd_blit_yuv(yuvbuf, 0, 0, YUV_WIDTH,
200 0, 0, YUV_WIDTH, YUV_HEIGHT);
201 frame_count++;
203 fps = calc_tenth_fps(frame_count, time_end - time_start);
204 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
205 log_text(str);
207 /* Test 2: quarter LCD update */
208 make_gradient_rect(YUV_WIDTH/2, YUV_HEIGHT/2);
210 frame_count = 0;
211 rb->sleep(0); /* sync to tick */
212 time_start = *rb->current_tick;
213 while((time_end = *rb->current_tick) - time_start < DURATION)
215 rb->lcd_blit_yuv(yuvbuf, 0, 0, YUV_WIDTH,
216 part14_x, part14_y, part14_w, part14_h);
217 frame_count++;
219 fps = calc_tenth_fps(frame_count, time_end - time_start);
220 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
221 log_text(str);
223 #endif
225 #ifdef HAVE_REMOTE_LCD
226 static void time_remote_update(void)
228 char str[32]; /* text buffer */
229 long time_start; /* start tickcount */
230 long time_end; /* end tickcount */
231 int frame_count;
232 int fps;
234 const int part14_x = LCD_REMOTE_WIDTH/4; /* x-offset for 1/4 update test */
235 const int part14_w = LCD_REMOTE_WIDTH/2; /* x-size for 1/4 update test */
236 const int part14_y = LCD_REMOTE_HEIGHT/4; /* y-offset for 1/4 update test */
237 const int part14_h = LCD_REMOTE_HEIGHT/2; /* y-size for 1/4 update test */
239 /* Test 1: full LCD update */
240 frame_count = 0;
241 rb->sleep(0); /* sync to tick */
242 time_start = *rb->current_tick;
243 while((time_end = *rb->current_tick) - time_start < DURATION)
245 rb->lcd_remote_update();
246 frame_count++;
248 fps = calc_tenth_fps(frame_count, time_end - time_start);
249 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
250 log_text(str);
252 /* Test 2: quarter LCD update */
253 frame_count = 0;
254 rb->sleep(0); /* sync to tick */
255 time_start = *rb->current_tick;
256 while((time_end = *rb->current_tick) - time_start < DURATION)
258 rb->lcd_remote_update_rect(part14_x, part14_y, part14_w, part14_h);
259 frame_count++;
261 fps = calc_tenth_fps(frame_count, time_end - time_start);
262 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
263 log_text(str);
265 #endif
267 #if LCD_DEPTH < 4
269 GREY_INFO_STRUCT_IRAM
270 static unsigned char greydata[LCD_HEIGHT][LCD_WIDTH];
272 static void make_grey_rect(int width, int height)
274 unsigned char vline[LCD_WIDTH];
275 int x, y;
277 for (x = 0; x < width; x++)
278 vline[x] = (x << 8) / width;
279 for (y = 0; y < height; y++)
280 rb->memcpy(greydata[y], vline, width);
283 static void time_greyscale(void)
285 char str[32]; /* text buffer */
286 long time_start; /* start tickcount */
287 long time_end; /* end tickcount */
288 long time_1, time_2;
289 int frames_1, frames_2;
290 int fps, load;
292 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
293 if (!grey_init(gbuf, gbuf_size, GREY_ON_COP,
294 LCD_WIDTH, LCD_HEIGHT, NULL))
296 log_text("greylib: out of memory.");
297 return;
299 make_grey_rect(LCD_WIDTH, LCD_HEIGHT);
301 /* Test 1 - greyscale overlay not yet enabled */
302 frames_1 = 0;
303 rb->sleep(0); /* sync to tick */
304 time_start = *rb->current_tick;
305 while((time_end = *rb->current_tick) - time_start < DURATION)
307 grey_ub_gray_bitmap(greydata[0], 0, 0, LCD_WIDTH, LCD_HEIGHT);
308 frames_1++;
310 time_1 = time_end - time_start;
312 /* Test 2 - greyscale overlay enabled */
313 grey_show(true);
314 frames_2 = 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_2++;
322 time_2 = time_end - time_start;
324 grey_release();
325 fps = calc_tenth_fps(frames_2, time_2);
326 load = 100 - (100 * frames_2 * time_1) / (frames_1 * time_2);
327 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
328 log_text(str);
330 if (load > 0 && load < 100)
332 rb->snprintf(str, sizeof(str), "CPU load: %d%%", load);
333 log_text(str);
335 else
336 log_text("CPU load err (boost?)");
338 #endif
340 /* plugin entry point */
341 enum plugin_status plugin_start(const void* parameter)
343 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
344 char str[32];
345 int cpu_freq;
346 #endif
348 /* standard stuff */
349 PLUGIN_IRAM_INIT(rb)
350 (void)parameter;
352 log_init();
353 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
354 cpu_freq = *rb->cpu_frequency; /* remember CPU frequency */
355 #endif
356 backlight_force_on(); /* backlight control in lib/helper.c */
358 log_text("Main LCD Update");
359 time_main_update();
360 #if defined(HAVE_LCD_COLOR) && (MEMORYSIZE > 2)
361 log_text("Main LCD YUV");
362 time_main_yuv();
363 #endif
364 #if LCD_DEPTH < 4
365 log_text("Greyscale library");
366 time_greyscale();
367 #endif
368 #ifdef HAVE_REMOTE_LCD
369 log_text("Remote LCD Update");
370 time_remote_update();
371 #endif
373 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
374 if (*rb->cpu_frequency != cpu_freq)
375 rb->snprintf(str, sizeof(str), "CPU clock changed!");
376 else
377 rb->snprintf(str, sizeof(str), "CPU: %d MHz",
378 (cpu_freq + 500000) / 1000000);
379 log_text(str);
380 #endif
381 backlight_use_settings(); /* backlight control in lib/helper.c */
383 /* wait until user closes plugin */
384 while (rb->button_get(true) != FPS_QUIT);
386 return PLUGIN_OK;
388 #endif