New plugin: FFT, A frequency analyzer plugin
[kugel-rb.git] / apps / plugins / test_fps.c
blobf7706f64bd87b7ec4513e05b1b95481d9ef8638e
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 defined(BUTTON_OFF)
39 #define FPS_QUIT BUTTON_OFF
40 #else
41 #define FPS_QUIT BUTTON_POWER
42 #endif
44 #define DURATION (2*HZ) /* longer duration gives more precise results */
46 PLUGIN_HEADER
48 /* Screen logging */
49 static int line;
50 static int max_line;
51 #ifdef HAVE_REMOTE_LCD
52 static int remote_line;
53 static int remote_max_line;
54 #endif
55 #if LCD_DEPTH < 4
56 static unsigned char *gbuf;
57 static size_t gbuf_size;
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 /* Test 1: full LCD update */
111 frame_count = 0;
112 rb->sleep(0); /* sync to tick */
113 time_start = *rb->current_tick;
114 while((time_end = *rb->current_tick) - time_start < DURATION)
116 rb->lcd_update();
117 frame_count++;
119 fps = calc_tenth_fps(frame_count, time_end - time_start);
120 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
121 log_text(str);
123 /* Test 2: quarter LCD update */
124 frame_count = 0;
125 rb->sleep(0); /* sync to tick */
126 time_start = *rb->current_tick;
127 while((time_end = *rb->current_tick) - time_start < DURATION)
129 rb->lcd_update_rect(part14_x, part14_y, part14_w, part14_h);
130 frame_count++;
132 fps = calc_tenth_fps(frame_count, time_end - time_start);
133 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
134 log_text(str);
137 #if defined(HAVE_LCD_COLOR) && (MEMORYSIZE > 2)
139 #if LCD_WIDTH >= LCD_HEIGHT
140 #define YUV_WIDTH LCD_WIDTH
141 #define YUV_HEIGHT LCD_HEIGHT
142 #else /* Assume the screen is rotated on portrait LCDs */
143 #define YUV_WIDTH LCD_HEIGHT
144 #define YUV_HEIGHT LCD_WIDTH
145 #endif
147 static unsigned char ydata[YUV_HEIGHT][YUV_WIDTH];
148 static unsigned char udata[YUV_HEIGHT/2][YUV_WIDTH/2];
149 static unsigned char vdata[YUV_HEIGHT/2][YUV_WIDTH/2];
151 static unsigned char * const yuvbuf[3] = {
152 (void*)ydata,
153 (void*)udata,
154 (void*)vdata
157 static void make_gradient_rect(int width, int height)
159 unsigned char vline[YUV_WIDTH/2];
160 int x, y;
162 width /= 2;
163 height /= 2;
165 for (x = 0; x < width; x++)
166 vline[x] = (x << 8) / width;
167 for (y = 0; y < height; y++)
169 rb->memset(udata[y], (y << 8) / height, width);
170 rb->memcpy(vdata[y], vline, width);
174 static void time_main_yuv(void)
176 char str[32]; /* text buffer */
177 long time_start; /* start tickcount */
178 long time_end; /* end tickcount */
179 int frame_count;
180 int fps;
182 const int part14_x = YUV_WIDTH/4; /* x-offset for 1/4 update test */
183 const int part14_w = YUV_WIDTH/2; /* x-size for 1/4 update test */
184 const int part14_y = YUV_HEIGHT/4; /* y-offset for 1/4 update test */
185 const int part14_h = YUV_HEIGHT/2; /* y-size for 1/4 update test */
187 rb->memset(ydata, 128, sizeof(ydata)); /* medium grey */
189 /* Test 1: full LCD update */
190 make_gradient_rect(YUV_WIDTH, YUV_HEIGHT);
192 frame_count = 0;
193 rb->sleep(0); /* sync to tick */
194 time_start = *rb->current_tick;
195 while((time_end = *rb->current_tick) - time_start < DURATION)
197 rb->lcd_blit_yuv(yuvbuf, 0, 0, YUV_WIDTH,
198 0, 0, YUV_WIDTH, YUV_HEIGHT);
199 frame_count++;
201 fps = calc_tenth_fps(frame_count, time_end - time_start);
202 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
203 log_text(str);
205 /* Test 2: quarter LCD update */
206 make_gradient_rect(YUV_WIDTH/2, YUV_HEIGHT/2);
208 frame_count = 0;
209 rb->sleep(0); /* sync to tick */
210 time_start = *rb->current_tick;
211 while((time_end = *rb->current_tick) - time_start < DURATION)
213 rb->lcd_blit_yuv(yuvbuf, 0, 0, YUV_WIDTH,
214 part14_x, part14_y, part14_w, part14_h);
215 frame_count++;
217 fps = calc_tenth_fps(frame_count, time_end - time_start);
218 rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
219 log_text(str);
221 #endif
223 #ifdef HAVE_REMOTE_LCD
224 static void time_remote_update(void)
226 char str[32]; /* text buffer */
227 long time_start; /* start tickcount */
228 long time_end; /* end tickcount */
229 int frame_count;
230 int fps;
232 const int part14_x = LCD_REMOTE_WIDTH/4; /* x-offset for 1/4 update test */
233 const int part14_w = LCD_REMOTE_WIDTH/2; /* x-size for 1/4 update test */
234 const int part14_y = LCD_REMOTE_HEIGHT/4; /* y-offset for 1/4 update test */
235 const int part14_h = LCD_REMOTE_HEIGHT/2; /* y-size for 1/4 update test */
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;
290 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
291 if (!grey_init(gbuf, gbuf_size, GREY_ON_COP,
292 LCD_WIDTH, LCD_HEIGHT, NULL))
294 log_text("greylib: out of memory.");
295 return;
297 make_grey_rect(LCD_WIDTH, LCD_HEIGHT);
299 /* Test 1 - greyscale overlay not yet enabled */
300 frames_1 = 0;
301 rb->sleep(0); /* sync to tick */
302 time_start = *rb->current_tick;
303 while((time_end = *rb->current_tick) - time_start < DURATION)
305 grey_ub_gray_bitmap(greydata[0], 0, 0, LCD_WIDTH, LCD_HEIGHT);
306 frames_1++;
308 time_1 = time_end - time_start;
310 /* Test 2 - greyscale overlay enabled */
311 grey_show(true);
312 frames_2 = 0;
313 rb->sleep(0); /* sync to tick */
314 time_start = *rb->current_tick;
315 while((time_end = *rb->current_tick) - time_start < DURATION)
317 grey_ub_gray_bitmap(greydata[0], 0, 0, LCD_WIDTH, LCD_HEIGHT);
318 frames_2++;
320 time_2 = time_end - time_start;
322 grey_release();
323 fps = calc_tenth_fps(frames_2, time_2);
324 load = 100 - (100 * frames_2 * time_1) / (frames_1 * time_2);
325 rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
326 log_text(str);
328 if (load > 0 && load < 100)
330 rb->snprintf(str, sizeof(str), "CPU load: %d%%", load);
331 log_text(str);
333 else
334 log_text("CPU load err (boost?)");
336 #endif
338 /* plugin entry point */
339 enum plugin_status plugin_start(const void* parameter)
341 #ifndef SIMULATOR
342 char str[32];
343 int cpu_freq;
344 #endif
346 /* standard stuff */
347 PLUGIN_IRAM_INIT(rb)
348 (void)parameter;
350 log_init();
351 #ifndef SIMULATOR
352 cpu_freq = *rb->cpu_frequency; /* remember CPU frequency */
353 #endif
354 backlight_force_on(); /* backlight control in lib/helper.c */
356 log_text("Main LCD Update");
357 time_main_update();
358 #if defined(HAVE_LCD_COLOR) && (MEMORYSIZE > 2)
359 log_text("Main LCD YUV");
360 time_main_yuv();
361 #endif
362 #if LCD_DEPTH < 4
363 log_text("Greyscale library");
364 time_greyscale();
365 #endif
366 #ifdef HAVE_REMOTE_LCD
367 log_text("Remote LCD Update");
368 time_remote_update();
369 #endif
371 #ifndef SIMULATOR
372 if (*rb->cpu_frequency != cpu_freq)
373 rb->snprintf(str, sizeof(str), "CPU clock changed!");
374 else
375 rb->snprintf(str, sizeof(str), "CPU: %d MHz",
376 (cpu_freq + 500000) / 1000000);
377 log_text(str);
378 #endif
379 backlight_use_settings(); /* backlight control in lib/helper.c */
381 /* wait until user closes plugin */
382 while (rb->button_get(true) != FPS_QUIT);
384 return PLUGIN_OK;
386 #endif