Lua: add do_menu() wrapper. Also fix potential NULL pointer dereference
[kugel-rb.git] / firmware / drivers / lcd-16bit.c
blobd1b417a00a1fb9bf2722d9277124036b1ec0075e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Dave Chapman
12 * Rockbox driver for 16-bit colour LCDs
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "config.h"
25 #include "cpu.h"
26 #include "lcd.h"
27 #include "kernel.h"
28 #include "thread.h"
29 #include <string.h>
30 #include <stdlib.h>
31 #include "memory.h"
32 #include "file.h"
33 #include "debug.h"
34 #include "system.h"
35 #include "font.h"
36 #include "rbunicode.h"
37 #include "bidi.h"
38 #include "scroll_engine.h"
40 enum fill_opt {
41 OPT_NONE = 0,
42 OPT_SET,
43 OPT_COPY
46 /*** globals ***/
47 fb_data lcd_framebuffer[LCD_FBHEIGHT][LCD_FBWIDTH]
48 IRAM_LCDFRAMEBUFFER CACHEALIGN_AT_LEAST_ATTR(16);
51 static fb_data* lcd_backdrop = NULL;
52 static long lcd_backdrop_offset IDATA_ATTR = 0;
54 static struct viewport default_vp =
56 .x = 0,
57 .y = 0,
58 .width = LCD_WIDTH,
59 .height = LCD_HEIGHT,
60 .font = FONT_SYSFIXED,
61 .drawmode = DRMODE_SOLID,
62 .fg_pattern = LCD_DEFAULT_FG,
63 .bg_pattern = LCD_DEFAULT_BG,
64 .lss_pattern = LCD_DEFAULT_BG,
65 .lse_pattern = LCD_DEFAULT_BG,
66 .lst_pattern = LCD_DEFAULT_BG,
69 /* The Gigabeat target build requires access to the current fg_pattern
70 in lcd-meg-fx.c */
71 #if (!defined(TOSHIBA_GIGABEAT_F)&& !defined(TOSHIBA_GIGABEAT_S)) || defined(SIMULATOR)
72 static struct viewport* current_vp IDATA_ATTR = &default_vp;
73 #else
74 struct viewport* current_vp IDATA_ATTR = &default_vp;
75 #endif
77 /* LCD init */
78 void lcd_init(void)
80 lcd_clear_display();
82 /* Call device specific init */
83 lcd_init_device();
84 scroll_init();
86 /*** Viewports ***/
88 void lcd_set_viewport(struct viewport* vp)
90 if (vp == NULL)
91 current_vp = &default_vp;
92 else
93 current_vp = vp;
96 void lcd_update_viewport(void)
98 lcd_update_rect(current_vp->x, current_vp->y,
99 current_vp->width, current_vp->height);
102 void lcd_update_viewport_rect(int x, int y, int width, int height)
104 lcd_update_rect(current_vp->x + x, current_vp->y + y, width, height);
107 /*** parameter handling ***/
109 void lcd_set_drawmode(int mode)
111 current_vp->drawmode = mode & (DRMODE_SOLID|DRMODE_INVERSEVID);
114 int lcd_get_drawmode(void)
116 return current_vp->drawmode;
119 void lcd_set_foreground(unsigned color)
121 current_vp->fg_pattern = color;
124 unsigned lcd_get_foreground(void)
126 return current_vp->fg_pattern;
129 void lcd_set_background(unsigned color)
131 current_vp->bg_pattern = color;
134 unsigned lcd_get_background(void)
136 return current_vp->bg_pattern;
139 void lcd_set_selector_start(unsigned color)
141 current_vp->lss_pattern = color;
144 void lcd_set_selector_end(unsigned color)
146 current_vp->lse_pattern = color;
149 void lcd_set_selector_text(unsigned color)
151 current_vp->lst_pattern = color;
154 void lcd_set_drawinfo(int mode, unsigned fg_color, unsigned bg_color)
156 lcd_set_drawmode(mode);
157 current_vp->fg_pattern = fg_color;
158 current_vp->bg_pattern = bg_color;
161 int lcd_getwidth(void)
163 return current_vp->width;
166 int lcd_getheight(void)
168 return current_vp->height;
171 void lcd_setfont(int newfont)
173 current_vp->font = newfont;
176 int lcd_getfont(void)
178 return current_vp->font;
181 int lcd_getstringsize(const unsigned char *str, int *w, int *h)
183 return font_getstringsize(str, w, h, current_vp->font);
186 /*** low-level drawing functions ***/
188 #define LCDADDR(x, y) (&lcd_framebuffer[(y)][(x)])
190 static void ICODE_ATTR setpixel(fb_data *address)
192 *address = current_vp->fg_pattern;
195 static void ICODE_ATTR clearpixel(fb_data *address)
197 *address = current_vp->bg_pattern;
200 static void ICODE_ATTR clearimgpixel(fb_data *address)
202 *address = *(fb_data *)((long)address + lcd_backdrop_offset);
205 static void ICODE_ATTR flippixel(fb_data *address)
207 *address = ~(*address);
210 static void ICODE_ATTR nopixel(fb_data *address)
212 (void)address;
215 lcd_fastpixelfunc_type* const lcd_fastpixelfuncs_bgcolor[8] = {
216 flippixel, nopixel, setpixel, setpixel,
217 nopixel, clearpixel, nopixel, clearpixel
220 lcd_fastpixelfunc_type* const lcd_fastpixelfuncs_backdrop[8] = {
221 flippixel, nopixel, setpixel, setpixel,
222 nopixel, clearimgpixel, nopixel, clearimgpixel
225 lcd_fastpixelfunc_type* const * lcd_fastpixelfuncs = lcd_fastpixelfuncs_bgcolor;
227 void lcd_set_backdrop(fb_data* backdrop)
229 lcd_backdrop = backdrop;
230 if (backdrop)
232 lcd_backdrop_offset = (long)backdrop - (long)&lcd_framebuffer[0][0];
233 lcd_fastpixelfuncs = lcd_fastpixelfuncs_backdrop;
235 else
237 lcd_backdrop_offset = 0;
238 lcd_fastpixelfuncs = lcd_fastpixelfuncs_bgcolor;
242 fb_data* lcd_get_backdrop(void)
244 return lcd_backdrop;
247 /*** drawing functions ***/
249 /* Clear the current viewport */
250 void lcd_clear_viewport(void)
252 fb_data *dst, *dst_end;
254 dst = LCDADDR(current_vp->x, current_vp->y);
255 dst_end = dst + current_vp->height * LCD_WIDTH;
257 if (current_vp->drawmode & DRMODE_INVERSEVID)
261 memset16(dst, current_vp->fg_pattern, current_vp->width);
262 dst += LCD_WIDTH;
264 while (dst < dst_end);
266 else
268 if (!lcd_backdrop)
272 memset16(dst, current_vp->bg_pattern, current_vp->width);
273 dst += LCD_WIDTH;
275 while (dst < dst_end);
277 else
281 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
282 current_vp->width * sizeof(fb_data));
283 dst += LCD_WIDTH;
285 while (dst < dst_end);
289 if (current_vp == &default_vp)
291 lcd_scroll_info.lines = 0;
293 else
295 lcd_scroll_stop(current_vp);
299 /* Clear the whole display */
300 void lcd_clear_display(void)
302 struct viewport* old_vp = current_vp;
304 current_vp = &default_vp;
306 lcd_clear_viewport();
308 current_vp = old_vp;
311 /* Set a single pixel */
312 void lcd_drawpixel(int x, int y)
314 if (((unsigned)x < (unsigned)current_vp->width) &&
315 ((unsigned)y < (unsigned)current_vp->height))
316 lcd_fastpixelfuncs[current_vp->drawmode](LCDADDR(current_vp->x+x, current_vp->y+y));
319 /* Draw a line */
320 void lcd_drawline(int x1, int y1, int x2, int y2)
322 int numpixels;
323 int i;
324 int deltax, deltay;
325 int d, dinc1, dinc2;
326 int x, xinc1, xinc2;
327 int y, yinc1, yinc2;
328 lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[current_vp->drawmode];
330 deltay = abs(y2 - y1);
331 if (deltay == 0)
333 DEBUGF("lcd_drawline() called for horizontal line - optimisation.\n");
334 lcd_hline(x1, x2, y1);
335 return;
337 deltax = abs(x2 - x1);
338 if (deltax == 0)
340 DEBUGF("lcd_drawline() called for vertical line - optimisation.\n");
341 lcd_vline(x1, y1, y2);
342 return;
344 xinc2 = 1;
345 yinc2 = 1;
347 if (deltax >= deltay)
349 numpixels = deltax;
350 d = 2 * deltay - deltax;
351 dinc1 = deltay * 2;
352 dinc2 = (deltay - deltax) * 2;
353 xinc1 = 1;
354 yinc1 = 0;
356 else
358 numpixels = deltay;
359 d = 2 * deltax - deltay;
360 dinc1 = deltax * 2;
361 dinc2 = (deltax - deltay) * 2;
362 xinc1 = 0;
363 yinc1 = 1;
365 numpixels++; /* include endpoints */
367 if (x1 > x2)
369 xinc1 = -xinc1;
370 xinc2 = -xinc2;
373 if (y1 > y2)
375 yinc1 = -yinc1;
376 yinc2 = -yinc2;
379 x = x1;
380 y = y1;
382 for (i = 0; i < numpixels; i++)
384 if (((unsigned)x < (unsigned)current_vp->width) && ((unsigned)y < (unsigned)current_vp->height))
385 pfunc(LCDADDR(x + current_vp->x, y + current_vp->y));
387 if (d < 0)
389 d += dinc1;
390 x += xinc1;
391 y += yinc1;
393 else
395 d += dinc2;
396 x += xinc2;
397 y += yinc2;
402 /* Draw a horizontal line (optimised) */
403 void lcd_hline(int x1, int x2, int y)
405 int x, width;
406 unsigned bits = 0;
407 enum fill_opt fillopt = OPT_NONE;
408 fb_data *dst, *dst_end;
410 /* direction flip */
411 if (x2 < x1)
413 x = x1;
414 x1 = x2;
415 x2 = x;
418 /* nothing to draw? */
419 if (((unsigned)y >= (unsigned)current_vp->height) ||
420 (x1 >= current_vp->width) ||
421 (x2 < 0))
422 return;
424 /* drawmode and optimisation */
425 if (current_vp->drawmode & DRMODE_INVERSEVID)
427 if (current_vp->drawmode & DRMODE_BG)
429 if (!lcd_backdrop)
431 fillopt = OPT_SET;
432 bits = current_vp->bg_pattern;
434 else
435 fillopt = OPT_COPY;
438 else
440 if (current_vp->drawmode & DRMODE_FG)
442 fillopt = OPT_SET;
443 bits = current_vp->fg_pattern;
446 if (fillopt == OPT_NONE && current_vp->drawmode != DRMODE_COMPLEMENT)
447 return;
449 /* clipping */
450 if (x1 < 0)
451 x1 = 0;
452 if (x2 >= current_vp->width)
453 x2 = current_vp->width-1;
455 width = x2 - x1 + 1;
457 /* Adjust x1 and y to viewport */
458 x1 += current_vp->x;
459 y += current_vp->y;
461 dst = LCDADDR(x1, y);
463 switch (fillopt)
465 case OPT_SET:
466 memset16(dst, bits, width);
467 break;
469 case OPT_COPY:
470 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
471 width * sizeof(fb_data));
472 break;
474 case OPT_NONE: /* DRMODE_COMPLEMENT */
475 dst_end = dst + width;
477 *dst = ~(*dst);
478 while (++dst < dst_end);
479 break;
483 /* Draw a vertical line (optimised) */
484 void lcd_vline(int x, int y1, int y2)
486 int y;
487 fb_data *dst, *dst_end;
488 lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[current_vp->drawmode];
490 /* direction flip */
491 if (y2 < y1)
493 y = y1;
494 y1 = y2;
495 y2 = y;
498 /* nothing to draw? */
499 if (((unsigned)x >= (unsigned)current_vp->width) ||
500 (y1 >= current_vp->height) ||
501 (y2 < 0))
502 return;
504 /* clipping */
505 if (y1 < 0)
506 y1 = 0;
507 if (y2 >= current_vp->height)
508 y2 = current_vp->height-1;
510 dst = LCDADDR(x + current_vp->x, y1 + current_vp->y);
511 dst_end = dst + (y2 - y1) * LCD_WIDTH;
515 pfunc(dst);
516 dst += LCD_WIDTH;
518 while (dst <= dst_end);
521 /* Draw a rectangular box */
522 void lcd_drawrect(int x, int y, int width, int height)
524 if ((width <= 0) || (height <= 0))
525 return;
527 int x2 = x + width - 1;
528 int y2 = y + height - 1;
530 lcd_vline(x, y, y2);
531 lcd_vline(x2, y, y2);
532 lcd_hline(x, x2, y);
533 lcd_hline(x, x2, y2);
536 /* Fill a rectangular area */
537 void lcd_fillrect(int x, int y, int width, int height)
539 unsigned bits = 0;
540 enum fill_opt fillopt = OPT_NONE;
541 fb_data *dst, *dst_end;
543 /* nothing to draw? */
544 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
545 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
546 return;
548 /* drawmode and optimisation */
549 if (current_vp->drawmode & DRMODE_INVERSEVID)
551 if (current_vp->drawmode & DRMODE_BG)
553 if (!lcd_backdrop)
555 fillopt = OPT_SET;
556 bits = current_vp->bg_pattern;
558 else
559 fillopt = OPT_COPY;
562 else
564 if (current_vp->drawmode & DRMODE_FG)
566 fillopt = OPT_SET;
567 bits = current_vp->fg_pattern;
570 if (fillopt == OPT_NONE && current_vp->drawmode != DRMODE_COMPLEMENT)
571 return;
573 /* clipping */
574 if (x < 0)
576 width += x;
577 x = 0;
579 if (y < 0)
581 height += y;
582 y = 0;
584 if (x + width > current_vp->width)
585 width = current_vp->width - x;
586 if (y + height > current_vp->height)
587 height = current_vp->height - y;
589 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
590 dst_end = dst + height * LCD_WIDTH;
594 fb_data *dst_row, *row_end;
596 switch (fillopt)
598 case OPT_SET:
599 memset16(dst, bits, width);
600 break;
602 case OPT_COPY:
603 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
604 width * sizeof(fb_data));
605 break;
607 case OPT_NONE: /* DRMODE_COMPLEMENT */
608 dst_row = dst;
609 row_end = dst_row + width;
611 *dst_row = ~(*dst_row);
612 while (++dst_row < row_end);
613 break;
615 dst += LCD_WIDTH;
617 while (dst < dst_end);
620 /* About Rockbox' internal monochrome bitmap format:
622 * A bitmap contains one bit for every pixel that defines if that pixel is
623 * black (1) or white (0). Bits within a byte are arranged vertically, LSB
624 * at top.
625 * The bytes are stored in row-major order, with byte 0 being top left,
626 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
627 * 0..7, the second row defines pixel row 8..15 etc.
629 * This is the mono bitmap format used on all other targets so far; the
630 * pixel packing doesn't really matter on a 8bit+ target. */
632 /* Draw a partial monochrome bitmap */
634 void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x,
635 int src_y, int stride, int x, int y,
636 int width, int height)
638 const unsigned char *src_end;
639 fb_data *dst, *dst_end;
640 unsigned dmask = 0x100; /* bit 8 == sentinel */
641 int drmode = current_vp->drawmode;
643 /* nothing to draw? */
644 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
645 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
646 return;
648 /* clipping */
649 if (x < 0)
651 width += x;
652 src_x -= x;
653 x = 0;
655 if (y < 0)
657 height += y;
658 src_y -= y;
659 y = 0;
661 if (x + width > current_vp->width)
662 width = current_vp->width - x;
663 if (y + height > current_vp->height)
664 height = current_vp->height - y;
666 src += stride * (src_y >> 3) + src_x; /* move starting point */
667 src_y &= 7;
668 src_end = src + width;
669 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
670 dst_end = dst + height * LCD_WIDTH;
672 if (drmode & DRMODE_INVERSEVID)
674 dmask = 0x1ff; /* bit 8 == sentinel */
675 drmode &= DRMODE_SOLID; /* mask out inversevid */
680 const unsigned char *src_col = src++;
681 unsigned data = (*src_col ^ dmask) >> src_y;
682 fb_data *dst_col = dst++;
683 int fg, bg;
684 long bo;
686 #define UPDATE_SRC do { \
687 data >>= 1; \
688 if (data == 0x001) { \
689 src_col += stride; \
690 data = *src_col ^ dmask; \
692 } while (0)
694 switch (drmode)
696 case DRMODE_COMPLEMENT:
699 if (data & 0x01)
700 *dst_col = ~(*dst_col);
702 dst_col += LCD_WIDTH;
703 UPDATE_SRC;
705 while (dst_col < dst_end);
706 break;
708 case DRMODE_BG:
709 if (lcd_backdrop)
711 bo = lcd_backdrop_offset;
714 if (!(data & 0x01))
715 *dst_col = *(fb_data *)((long)dst_col + bo);
717 dst_col += LCD_WIDTH;
718 UPDATE_SRC;
720 while (dst_col < dst_end);
722 else
724 bg = current_vp->bg_pattern;
727 if (!(data & 0x01))
728 *dst_col = bg;
730 dst_col += LCD_WIDTH;
731 UPDATE_SRC;
733 while (dst_col < dst_end);
735 break;
737 case DRMODE_FG:
738 fg = current_vp->fg_pattern;
741 if (data & 0x01)
742 *dst_col = fg;
744 dst_col += LCD_WIDTH;
745 UPDATE_SRC;
747 while (dst_col < dst_end);
748 break;
750 case DRMODE_SOLID:
751 fg = current_vp->fg_pattern;
752 if (lcd_backdrop)
754 bo = lcd_backdrop_offset;
757 *dst_col = (data & 0x01) ? fg
758 : *(fb_data *)((long)dst_col + bo);
759 dst_col += LCD_WIDTH;
760 UPDATE_SRC;
762 while (dst_col < dst_end);
764 else
766 bg = current_vp->bg_pattern;
769 *dst_col = (data & 0x01) ? fg : bg;
770 dst_col += LCD_WIDTH;
771 UPDATE_SRC;
773 while (dst_col < dst_end);
775 break;
778 while (src < src_end);
780 /* Draw a full monochrome bitmap */
781 void lcd_mono_bitmap(const unsigned char *src, int x, int y, int width, int height)
783 lcd_mono_bitmap_part(src, 0, 0, width, x, y, width, height);
786 /* Draw a partial native bitmap */
787 void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y,
788 int stride, int x, int y, int width,
789 int height)
791 fb_data *dst, *dst_end;
793 /* nothing to draw? */
794 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
795 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
796 return;
798 /* clipping */
799 if (x < 0)
801 width += x;
802 src_x -= x;
803 x = 0;
805 if (y < 0)
807 height += y;
808 src_y -= y;
809 y = 0;
811 if (x + width > current_vp->width)
812 width = current_vp->width - x;
813 if (y + height > current_vp->height)
814 height = current_vp->height - y;
816 src += stride * src_y + src_x; /* move starting point */
817 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
818 dst_end = dst + height * LCD_WIDTH;
822 memcpy(dst, src, width * sizeof(fb_data));
823 src += stride;
824 dst += LCD_WIDTH;
826 while (dst < dst_end);
829 /* Draw a full native bitmap */
830 void lcd_bitmap(const fb_data *src, int x, int y, int width, int height)
832 lcd_bitmap_part(src, 0, 0, width, x, y, width, height);
835 #if !defined(TOSHIBA_GIGABEAT_F) && !defined(TOSHIBA_GIGABEAT_S) \
836 || defined(SIMULATOR)
837 /* Draw a partial native bitmap */
838 void ICODE_ATTR lcd_bitmap_transparent_part(const fb_data *src, int src_x,
839 int src_y, int stride, int x,
840 int y, int width, int height)
842 fb_data *dst, *dst_end;
844 /* nothing to draw? */
845 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
846 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
847 return;
849 /* clipping */
850 if (x < 0)
852 width += x;
853 src_x -= x;
854 x = 0;
856 if (y < 0)
858 height += y;
859 src_y -= y;
860 y = 0;
862 if (x + width > current_vp->width)
863 width = current_vp->width - x;
864 if (y + height > current_vp->height)
865 height = current_vp->height - y;
867 src += stride * src_y + src_x; /* move starting point */
868 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
869 dst_end = dst + height * LCD_WIDTH;
873 int i;
874 for(i = 0;i < width;i++)
876 if (src[i] == REPLACEWITHFG_COLOR)
877 dst[i] = current_vp->fg_pattern;
878 else if(src[i] != TRANSPARENT_COLOR)
879 dst[i] = src[i];
881 src += stride;
882 dst += LCD_WIDTH;
884 while (dst < dst_end);
886 #endif /* !defined(TOSHIBA_GIGABEAT_F) || defined(SIMULATOR) */
888 /* Draw a full native bitmap with a transparent color */
889 void lcd_bitmap_transparent(const fb_data *src, int x, int y,
890 int width, int height)
892 lcd_bitmap_transparent_part(src, 0, 0, width, x, y, width, height);
895 #include "lcd-bitmap-common.c"