FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / firmware / target / arm / imx31 / gigabeat-s / lcd-imx31.c
blobc8d8b8a81e1d671205790e65fd7ad26956c1f150
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Will Robertson
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 <sys/types.h>
23 #include "config.h"
24 #include "cpu.h"
25 #include "string.h"
26 #include "lcd.h"
27 #include "kernel.h"
28 #include "lcd-target.h"
29 #include "backlight-target.h"
31 #define MAIN_LCD_IDMAC_CHANNEL 14
32 #define LCDADDR(x, y) (&lcd_framebuffer[(y)][(x)])
34 static bool lcd_on = true;
35 static bool lcd_powered = true;
36 static unsigned lcd_yuv_options = 0;
38 ** This is imported from lcd-16bit.c
40 extern struct viewport* current_vp;
42 /* Copies a rectangle from one framebuffer to another. Can be used in
43 single transfer mode with width = num pixels, and height = 1 which
44 allows a full-width rectangle to be copied more efficiently. */
45 extern void lcd_copy_buffer_rect(fb_data *dst, const fb_data *src,
46 int width, int height);
48 /* LCD init */
49 void lcd_init_device(void)
51 /* Move the framebuffer */
52 #ifdef BOOTLOADER
53 /* Only do this once to avoid flicker */
54 memset(FRAME, 0x00, FRAME_SIZE);
55 #endif
56 IPU_IDMAC_CHA_EN &= ~(1ul << MAIN_LCD_IDMAC_CHANNEL);
57 IPU_IMA_ADDR = ((0x1 << 16) | (MAIN_LCD_IDMAC_CHANNEL << 4)) + (1 << 3);
58 IPU_IMA_DATA = FRAME_PHYS_ADDR;
59 IPU_IDMAC_CHA_EN |= (1ul << MAIN_LCD_IDMAC_CHANNEL);
62 /* Update a fraction of the display. */
63 void lcd_update_rect(int x, int y, int width, int height)
65 fb_data *dst, *src;
67 if (!lcd_on)
68 return;
70 if (x + width > LCD_WIDTH)
71 width = LCD_WIDTH - x; /* Clip right */
72 if (x < 0)
73 width += x, x = 0; /* Clip left */
74 if (width <= 0)
75 return; /* nothing left to do */
77 if (y + height > LCD_HEIGHT)
78 height = LCD_HEIGHT - y; /* Clip bottom */
79 if (y < 0)
80 height += y, y = 0; /* Clip top */
81 if (height <= 0)
82 return; /* nothing left to do */
84 /* TODO: It may be faster to swap the addresses of lcd_driver_framebuffer
85 * and lcd_framebuffer */
86 dst = (fb_data *)FRAME + LCD_WIDTH*y + x;
87 src = &lcd_framebuffer[y][x];
89 /* Copy part of the Rockbox framebuffer to the second framebuffer */
90 if (width < LCD_WIDTH)
92 /* Not full width - do line-by-line */
93 lcd_copy_buffer_rect(dst, src, width, height);
95 else
97 /* Full width - copy as one line */
98 lcd_copy_buffer_rect(dst, src, LCD_WIDTH*height, 1);
102 void lcd_sleep(void)
104 if (lcd_powered)
106 lcd_enable(false);
107 lcd_powered = false;
108 IPU_IDMAC_CHA_EN &= ~(1ul << MAIN_LCD_IDMAC_CHANNEL);
109 _backlight_lcd_sleep();
113 void lcd_enable(bool state)
115 if (state == lcd_on)
116 return;
118 if (state)
120 IPU_IDMAC_CHA_EN |= 1ul << MAIN_LCD_IDMAC_CHANNEL;
121 sleep(HZ/50);
122 lcd_powered = true;
123 lcd_on = true;
124 lcd_update();
125 lcd_activation_call_hook();
127 else
129 lcd_on = false;
133 bool lcd_active(void)
135 return lcd_on;
138 /* Update the display.
139 This must be called after all other LCD functions that change the display. */
140 void lcd_update(void)
142 if (!lcd_on)
143 return;
145 lcd_copy_buffer_rect((fb_data *)FRAME, &lcd_framebuffer[0][0],
146 LCD_WIDTH*LCD_HEIGHT, 1);
149 void lcd_bitmap_transparent_part(const fb_data *src, int src_x, int src_y,
150 int stride, int x, int y, int width,
151 int height)
153 int w, px;
154 fb_data *dst;
156 if (x + width > current_vp->width)
157 width = current_vp->width - x; /* Clip right */
159 if (x < 0) /* Clip left */
161 width += x;
162 src_x -= x;
163 x = 0;
166 if (width <= 0)
167 return; /* nothing left to do */
169 if (y + height > current_vp->height)
170 height = current_vp->height - y; /* Clip bottom */
172 if (y < 0) /* Clip top */
174 height += y;
175 src_y -= y;
176 y = 0;
179 if (height <= 0)
180 return; /* nothing left to do */
182 src += stride * src_y + src_x; /* move starting point */
183 dst = &lcd_framebuffer[current_vp->y+y][current_vp->x+x];
185 asm volatile (
186 ".rowstart: \r\n"
187 "mov %[w], %[width] \r\n" /* Load width for inner loop */
188 ".nextpixel: \r\n"
189 "ldrh %[px], [%[s]], #2 \r\n" /* Load src pixel */
190 "add %[d], %[d], #2 \r\n" /* Uncoditionally increment dst */
191 "cmp %[px], %[fgcolor] \r\n" /* Compare to foreground color */
192 "streqh %[fgpat], [%[d], #-2] \r\n" /* Store foregroud if match */
193 "cmpne %[px], %[transcolor] \r\n" /* Compare to transparent color */
194 "strneh %[px], [%[d], #-2] \r\n" /* Store dst if not transparent */
195 "subs %[w], %[w], #1 \r\n" /* Width counter has run down? */
196 "bgt .nextpixel \r\n" /* More in this row? */
197 "add %[s], %[s], %[sstp], lsl #1 \r\n" /* Skip over to start of next line */
198 "add %[d], %[d], %[dstp], lsl #1 \r\n"
199 "subs %[h], %[h], #1 \r\n" /* Height counter has run down? */
200 "bgt .rowstart \r\n" /* More rows? */
201 : [w]"=&r"(w), [h]"+&r"(height), [px]"=&r"(px),
202 [s]"+&r"(src), [d]"+&r"(dst)
203 : [width]"r"(width),
204 [sstp]"r"(stride - width),
205 [dstp]"r"(LCD_WIDTH - width),
206 [transcolor]"r"(TRANSPARENT_COLOR),
207 [fgcolor]"r"(REPLACEWITHFG_COLOR),
208 [fgpat]"r"(current_vp->fg_pattern)
212 void lcd_yuv_set_options(unsigned options)
214 lcd_yuv_options = options;
217 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
218 extern void lcd_write_yuv420_lines(fb_data *dst,
219 unsigned char const * const src[3],
220 int width,
221 int stride);
222 extern void lcd_write_yuv420_lines_odither(fb_data *dst,
223 unsigned char const * const src[3],
224 int width,
225 int stride,
226 int x_screen, /* To align dither pattern */
227 int y_screen);
228 /* Performance function to blit a YUV bitmap directly to the LCD */
229 /* For the Gigabeat - show it rotated */
230 /* So the LCD_WIDTH is now the height */
231 void lcd_blit_yuv(unsigned char * const src[3],
232 int src_x, int src_y, int stride,
233 int x, int y, int width, int height)
235 /* Caches for chroma data so it only need be recaculated every other
236 line */
237 unsigned char const * yuv_src[3];
238 off_t z;
240 if (!lcd_on)
241 return;
243 /* Sorry, but width and height must be >= 2 or else */
244 width &= ~1;
245 height >>= 1;
247 y = LCD_WIDTH - 1 - y;
248 fb_data *dst = (fb_data*)FRAME + x * LCD_WIDTH + y;
250 z = stride*src_y;
251 yuv_src[0] = src[0] + z + src_x;
252 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
253 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
255 if (lcd_yuv_options & LCD_YUV_DITHER)
259 lcd_write_yuv420_lines_odither(dst, yuv_src, width, stride, y, x);
260 yuv_src[0] += stride << 1; /* Skip down two luma lines */
261 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
262 yuv_src[2] += stride >> 1;
263 dst -= 2;
264 y -= 2;
266 while (--height > 0);
268 else
272 lcd_write_yuv420_lines(dst, yuv_src, width, stride);
273 yuv_src[0] += stride << 1; /* Skip down two luma lines */
274 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
275 yuv_src[2] += stride >> 1;
276 dst -= 2;
278 while (--height > 0);
282 void lcd_set_contrast(int val) {
283 (void) val;
284 // TODO:
287 void lcd_set_invert_display(bool yesno) {
288 (void) yesno;
289 // TODO:
292 void lcd_set_flip(bool yesno) {
293 (void) yesno;
294 // TODO: