Forgot that the X5/M5 use a 64/64KB IRAM split, so they can't use the previous optimi...
[Rockbox.git] / uisimulator / sdl / lcd-bitmap.c
blob92480396bc9477f22ddea01d18df2af1a604b280
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Dan Everton
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 ****************************************************************************/
22 #include "debug.h"
23 #include "uisdl.h"
24 #include "lcd-sdl.h"
26 SDL_Surface* lcd_surface;
27 int lcd_backlight_val;
29 #if LCD_DEPTH <= 8
30 #ifdef HAVE_BACKLIGHT
31 SDL_Color lcd_backlight_color_zero = {UI_LCD_BGCOLORLIGHT, 0};
32 SDL_Color lcd_backlight_color_max = {UI_LCD_FGCOLORLIGHT, 0};
33 #endif
34 SDL_Color lcd_color_zero = {UI_LCD_BGCOLOR, 0};
35 SDL_Color lcd_color_max = {UI_LCD_FGCOLOR, 0};
36 #endif
38 #if LCD_DEPTH < 8
39 int lcd_ex_shades = 0;
40 unsigned long (*lcd_ex_getpixel)(int, int) = NULL;
41 #endif
43 static unsigned long get_lcd_pixel(int x, int y)
45 #if LCD_DEPTH == 1
46 return ((lcd_framebuffer[y/8][x] >> (y & 7)) & 1);
47 #elif LCD_DEPTH == 2
48 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
49 return ((lcd_framebuffer[y][x/4] >> (2 * (~x & 3))) & 3);
50 #elif LCD_PIXELFORMAT == VERTICAL_PACKING
51 return ((lcd_framebuffer[y/4][x] >> (2 * (y & 3))) & 3);
52 #elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
53 unsigned bits = (lcd_framebuffer[y/8][x] >> (y & 7)) & 0x0101;
54 return (bits | (bits >> 7)) & 3;
55 #endif
56 #elif LCD_DEPTH == 16
57 #if LCD_PIXELFORMAT == RGB565SWAPPED
58 unsigned bits = lcd_framebuffer[y][x];
59 return (bits >> 8) | (bits << 8);
60 #else
61 return lcd_framebuffer[y][x];
62 #endif
63 #endif
66 void lcd_update(void)
68 /* update a full screen rect */
69 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
72 void lcd_update_rect(int x_start, int y_start, int width, int height)
74 sdl_update_rect(lcd_surface, x_start, y_start, width, height, LCD_WIDTH,
75 LCD_HEIGHT, get_lcd_pixel);
76 sdl_gui_update(lcd_surface, x_start, y_start, width, height, LCD_WIDTH,
77 LCD_HEIGHT, background ? UI_LCD_POSX : 0, background? UI_LCD_POSY : 0);
80 #ifdef HAVE_BACKLIGHT
81 void sim_backlight(int value)
83 lcd_backlight_val = value;
85 #if LCD_DEPTH <= 8
86 if (value > 0) {
87 sdl_set_gradient(lcd_surface, &lcd_backlight_color_zero,
88 &lcd_backlight_color_max, 0, (1<<LCD_DEPTH));
89 } else {
90 sdl_set_gradient(lcd_surface, &lcd_color_zero, &lcd_color_max,
91 0, (1<<LCD_DEPTH));
93 #if LCD_DEPTH < 8
94 if (lcd_ex_shades) {
95 if (value > 0) {
96 sdl_set_gradient(lcd_surface, &lcd_backlight_color_max,
97 &lcd_backlight_color_zero, (1<<LCD_DEPTH),
98 lcd_ex_shades);
99 } else {
100 sdl_set_gradient(lcd_surface, &lcd_color_max, &lcd_color_zero,
101 (1<<LCD_DEPTH), lcd_ex_shades);
104 #endif
106 sdl_gui_update(lcd_surface, 0, 0, LCD_WIDTH, LCD_HEIGHT, LCD_WIDTH,
107 LCD_HEIGHT, background ? UI_LCD_POSX : 0, background? UI_LCD_POSY : 0);
109 #endif
111 #endif
113 /* initialise simulator lcd driver */
114 void sim_lcd_init(void)
116 #if LCD_DEPTH == 16
117 lcd_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, LCD_WIDTH * display_zoom,
118 LCD_HEIGHT * display_zoom, LCD_DEPTH, 0, 0, 0, 0);
119 #else
120 lcd_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, LCD_WIDTH * display_zoom,
121 LCD_HEIGHT * display_zoom, 8, 0, 0, 0, 0);
122 #endif
124 #if LCD_DEPTH <= 8
125 #ifdef HAVE_BACKLIGHT
126 sdl_set_gradient(lcd_surface, &lcd_backlight_color_zero, &lcd_color_max,
127 0, (1<<LCD_DEPTH));
128 #else
129 sdl_set_gradient(lcd_surface, &lcd_color_zero, &lcd_color_max, 0,
130 (1<<LCD_DEPTH));
131 #endif
132 #endif
135 #if LCD_DEPTH < 8
136 void sim_lcd_ex_init(int shades, unsigned long (*getpixel)(int, int))
138 lcd_ex_shades = shades;
139 lcd_ex_getpixel = getpixel;
140 if (shades) {
141 #ifdef HAVE_BACKLIGHT
142 if (lcd_backlight_val > 0) {
143 sdl_set_gradient(lcd_surface, &lcd_color_max,
144 &lcd_backlight_color_zero, (1<<LCD_DEPTH),
145 shades);
147 else
148 #endif
150 sdl_set_gradient(lcd_surface, &lcd_color_max, &lcd_color_zero,
151 (1<<LCD_DEPTH), shades);
156 void sim_lcd_ex_update_rect(int x_start, int y_start, int width, int height)
158 if (lcd_ex_getpixel) {
159 sdl_update_rect(lcd_surface, x_start, y_start, width, height,
160 LCD_WIDTH, LCD_HEIGHT, lcd_ex_getpixel);
161 sdl_gui_update(lcd_surface, x_start, y_start, width, height, LCD_WIDTH,
162 LCD_HEIGHT, background ? UI_LCD_POSX : 0,
163 background? UI_LCD_POSY : 0);
166 #endif
168 #ifdef HAVE_LCD_COLOR
170 * |R| |1.000000 -0.000001 1.402000| |Y'|
171 * |G| = |1.000000 -0.334136 -0.714136| |Pb|
172 * |B| |1.000000 1.772000 0.000000| |Pr|
173 * Scaled, normalized, rounded and tweaked to yield RGB 565:
174 * |R| |74 0 101| |Y' - 16| >> 9
175 * |G| = |74 -24 -51| |Cb - 128| >> 8
176 * |B| |74 128 0| |Cr - 128| >> 9
178 #define YFAC (74)
179 #define RVFAC (101)
180 #define GUFAC (-24)
181 #define GVFAC (-51)
182 #define BUFAC (128)
184 static inline int clamp(int val, int min, int max)
186 if (val < min)
187 val = min;
188 else if (val > max)
189 val = max;
190 return val;
193 void lcd_yuv_set_options(unsigned options)
195 (void)options;
198 /* Draw a partial YUV colour bitmap - similiar behavior to lcd_blit_yuv
199 in the core */
200 void lcd_blit_yuv(unsigned char * const src[3],
201 int src_x, int src_y, int stride,
202 int x, int y, int width, int height)
204 const unsigned char *ysrc, *usrc, *vsrc;
205 int linecounter;
206 fb_data *dst, *row_end;
207 long z;
209 /* width and height must be >= 2 and an even number */
210 width &= ~1;
211 linecounter = height >> 1;
213 #if LCD_WIDTH >= LCD_HEIGHT
214 dst = &lcd_framebuffer[y][x];
215 row_end = dst + width;
216 #else
217 dst = &lcd_framebuffer[x][LCD_WIDTH - y - 1];
218 row_end = dst + LCD_WIDTH * width;
219 #endif
221 z = stride * src_y;
222 ysrc = src[0] + z + src_x;
223 usrc = src[1] + (z >> 2) + (src_x >> 1);
224 vsrc = src[2] + (usrc - src[1]);
226 /* stride => amount to jump from end of last row to start of next */
227 stride -= width;
229 /* upsampling, YUV->RGB conversion and reduction to RGB565 in one go */
235 int y, cb, cr, rv, guv, bu, r, g, b;
237 y = YFAC*(*ysrc++ - 16);
238 cb = *usrc++ - 128;
239 cr = *vsrc++ - 128;
241 rv = RVFAC*cr;
242 guv = GUFAC*cb + GVFAC*cr;
243 bu = BUFAC*cb;
245 r = y + rv;
246 g = y + guv;
247 b = y + bu;
249 if ((unsigned)(r | g | b) > 64*256-1)
251 r = clamp(r, 0, 64*256-1);
252 g = clamp(g, 0, 64*256-1);
253 b = clamp(b, 0, 64*256-1);
256 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
258 #if LCD_WIDTH >= LCD_HEIGHT
259 dst++;
260 #else
261 dst += LCD_WIDTH;
262 #endif
264 y = YFAC*(*ysrc++ - 16);
265 r = y + rv;
266 g = y + guv;
267 b = y + bu;
269 if ((unsigned)(r | g | b) > 64*256-1)
271 r = clamp(r, 0, 64*256-1);
272 g = clamp(g, 0, 64*256-1);
273 b = clamp(b, 0, 64*256-1);
276 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
278 #if LCD_WIDTH >= LCD_HEIGHT
279 dst++;
280 #else
281 dst += LCD_WIDTH;
282 #endif
284 while (dst < row_end);
286 ysrc += stride;
287 usrc -= width >> 1;
288 vsrc -= width >> 1;
290 #if LCD_WIDTH >= LCD_HEIGHT
291 row_end += LCD_WIDTH;
292 dst += LCD_WIDTH - width;
293 #else
294 row_end -= 1;
295 dst -= LCD_WIDTH*width + 1;
296 #endif
300 int y, cb, cr, rv, guv, bu, r, g, b;
302 y = YFAC*(*ysrc++ - 16);
303 cb = *usrc++ - 128;
304 cr = *vsrc++ - 128;
306 rv = RVFAC*cr;
307 guv = GUFAC*cb + GVFAC*cr;
308 bu = BUFAC*cb;
310 r = y + rv;
311 g = y + guv;
312 b = y + bu;
314 if ((unsigned)(r | g | b) > 64*256-1)
316 r = clamp(r, 0, 64*256-1);
317 g = clamp(g, 0, 64*256-1);
318 b = clamp(b, 0, 64*256-1);
321 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
323 #if LCD_WIDTH >= LCD_HEIGHT
324 dst++;
325 #else
326 dst += LCD_WIDTH;
327 #endif
329 y = YFAC*(*ysrc++ - 16);
330 r = y + rv;
331 g = y + guv;
332 b = y + bu;
334 if ((unsigned)(r | g | b) > 64*256-1)
336 r = clamp(r, 0, 64*256-1);
337 g = clamp(g, 0, 64*256-1);
338 b = clamp(b, 0, 64*256-1);
341 *dst = LCD_RGBPACK_LCD(r >> 9, g >> 8, b >> 9);
343 #if LCD_WIDTH >= LCD_HEIGHT
344 dst++;
345 #else
346 dst += LCD_WIDTH;
347 #endif
349 while (dst < row_end);
351 ysrc += stride;
352 usrc += stride >> 1;
353 vsrc += stride >> 1;
355 #if LCD_WIDTH >= LCD_HEIGHT
356 row_end += LCD_WIDTH;
357 dst += LCD_WIDTH - width;
358 #else
359 row_end -= 1;
360 dst -= LCD_WIDTH*width + 1;
361 #endif
363 while (--linecounter > 0);
365 #if LCD_WIDTH >= LCD_HEIGHT
366 lcd_update_rect(x, y, width, height);
367 #else
368 lcd_update_rect(LCD_WIDTH - y - height, x, height, width);
369 #endif
371 #endif /* HAVE_LCD_COLOR */