Remove tabs.
[kugel-rb.git] / firmware / drivers / lcd-16bit-vert.c
blob17a1a2dda28b17036973c733779031f5e7a17409
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Dave Chapman
11 * Copyright (C) 2009 by Karl Kurbjun
13 * Rockbox driver for 16-bit colour LCDs with vertical strides
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "config.h"
26 #include "cpu.h"
27 #include "lcd.h"
28 #include "kernel.h"
29 #include "thread.h"
30 #include <stdlib.h>
31 #include "string-extra.h" /* mem*() */
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(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;
95 #if defined(SIMULATOR)
96 /* Force the viewport to be within bounds. If this happens it should
97 * be considered an error - the viewport will not draw as it might be
98 * expected.
100 if((unsigned) current_vp->x > (unsigned) LCD_WIDTH
101 || (unsigned) current_vp->y > (unsigned) LCD_HEIGHT
102 || current_vp->x + current_vp->width > LCD_WIDTH
103 || current_vp->y + current_vp->height > LCD_HEIGHT)
105 #if !defined(HAVE_VIEWPORT_CLIP)
106 DEBUGF("ERROR: "
107 #else
108 DEBUGF("NOTE: "
109 #endif
110 "set_viewport out of bounds: x: %d y: %d width: %d height:%d\n",
111 current_vp->x, current_vp->y,
112 current_vp->width, current_vp->height);
115 #endif
118 void lcd_update_viewport(void)
120 lcd_update_rect(current_vp->x, current_vp->y,
121 current_vp->width, current_vp->height);
124 void lcd_update_viewport_rect(int x, int y, int width, int height)
126 lcd_update_rect(current_vp->x + x, current_vp->y + y, width, height);
129 /*** parameter handling ***/
131 void lcd_set_drawmode(int mode)
133 current_vp->drawmode = mode & (DRMODE_SOLID|DRMODE_INVERSEVID);
136 int lcd_get_drawmode(void)
138 return current_vp->drawmode;
141 void lcd_set_foreground(unsigned color)
143 current_vp->fg_pattern = color;
146 unsigned lcd_get_foreground(void)
148 return current_vp->fg_pattern;
151 void lcd_set_background(unsigned color)
153 current_vp->bg_pattern = color;
156 unsigned lcd_get_background(void)
158 return current_vp->bg_pattern;
161 void lcd_set_selector_start(unsigned color)
163 current_vp->lss_pattern = color;
166 void lcd_set_selector_end(unsigned color)
168 current_vp->lse_pattern = color;
171 void lcd_set_selector_text(unsigned color)
173 current_vp->lst_pattern = color;
176 void lcd_set_drawinfo(int mode, unsigned fg_color, unsigned bg_color)
178 lcd_set_drawmode(mode);
179 current_vp->fg_pattern = fg_color;
180 current_vp->bg_pattern = bg_color;
183 int lcd_getwidth(void)
185 return current_vp->width;
188 int lcd_getheight(void)
190 return current_vp->height;
193 void lcd_setfont(int newfont)
195 current_vp->font = newfont;
198 int lcd_getfont(void)
200 return current_vp->font;
203 int lcd_getstringsize(const unsigned char *str, int *w, int *h)
205 return font_getstringsize(str, w, h, current_vp->font);
208 /*** low-level drawing functions ***/
210 #define LCDADDR(x, y) (&lcd_framebuffer[0][0] + LCD_HEIGHT*(x) + (y))
212 static void ICODE_ATTR setpixel(fb_data *address)
214 *address = current_vp->fg_pattern;
217 static void ICODE_ATTR clearpixel(fb_data *address)
219 *address = current_vp->bg_pattern;
222 static void ICODE_ATTR clearimgpixel(fb_data *address)
224 *address = *(fb_data *)((long)address + lcd_backdrop_offset);
227 static void ICODE_ATTR flippixel(fb_data *address)
229 *address = ~(*address);
232 static void ICODE_ATTR nopixel(fb_data *address)
234 (void)address;
237 lcd_fastpixelfunc_type* const lcd_fastpixelfuncs_bgcolor[8] = {
238 flippixel, nopixel, setpixel, setpixel,
239 nopixel, clearpixel, nopixel, clearpixel
242 lcd_fastpixelfunc_type* const lcd_fastpixelfuncs_backdrop[8] = {
243 flippixel, nopixel, setpixel, setpixel,
244 nopixel, clearimgpixel, nopixel, clearimgpixel
247 lcd_fastpixelfunc_type* const * lcd_fastpixelfuncs = lcd_fastpixelfuncs_bgcolor;
249 void lcd_set_backdrop(fb_data* backdrop)
251 lcd_backdrop = backdrop;
252 if (backdrop)
254 lcd_backdrop_offset = (long)backdrop - (long)&lcd_framebuffer[0][0];
255 lcd_fastpixelfuncs = lcd_fastpixelfuncs_backdrop;
257 else
259 lcd_backdrop_offset = 0;
260 lcd_fastpixelfuncs = lcd_fastpixelfuncs_bgcolor;
264 fb_data* lcd_get_backdrop(void)
266 return lcd_backdrop;
269 /*** drawing functions ***/
271 /* Clear the current viewport */
272 void lcd_clear_viewport(void)
274 fb_data *dst, *dst_end;
276 dst = LCDADDR(current_vp->x, current_vp->y);
277 dst_end = dst + current_vp->width * LCD_HEIGHT;
279 if (current_vp->drawmode & DRMODE_INVERSEVID)
283 memset16(dst, current_vp->fg_pattern, current_vp->height);
284 dst += LCD_HEIGHT;
286 while (dst < dst_end);
288 else
290 if (!lcd_backdrop)
294 memset16(dst, current_vp->bg_pattern, current_vp->height);
295 dst += LCD_HEIGHT;
297 while (dst < dst_end);
299 else
303 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
304 current_vp->height * sizeof(fb_data));
305 dst += LCD_HEIGHT;
307 while (dst < dst_end);
311 if (current_vp == &default_vp)
313 lcd_scroll_info.lines = 0;
315 else
317 lcd_scroll_stop(current_vp);
321 /* Clear the whole display */
322 void lcd_clear_display(void)
324 struct viewport* old_vp = current_vp;
326 current_vp = &default_vp;
328 lcd_clear_viewport();
330 current_vp = old_vp;
333 /* Set a single pixel */
334 void lcd_drawpixel(int x, int y)
336 if ( ((unsigned)x < (unsigned)current_vp->width)
337 && ((unsigned)y < (unsigned)current_vp->height)
338 #if defined(HAVE_VIEWPORT_CLIP)
339 && ((unsigned)x < (unsigned)LCD_WIDTH)
340 && ((unsigned)y < (unsigned)LCD_HEIGHT)
341 #endif
343 lcd_fastpixelfuncs[current_vp->drawmode](LCDADDR(current_vp->x+x, current_vp->y+y));
346 /* Draw a line */
347 void lcd_drawline(int x1, int y1, int x2, int y2)
349 int numpixels;
350 int i;
351 int deltax, deltay;
352 int d, dinc1, dinc2;
353 int x, xinc1, xinc2;
354 int y, yinc1, yinc2;
355 lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[current_vp->drawmode];
357 deltay = abs(y2 - y1);
358 if (deltay == 0)
360 /* DEBUGF("lcd_drawline() called for horizontal line - optimisation.\n"); */
361 lcd_hline(x1, x2, y1);
362 return;
364 deltax = abs(x2 - x1);
365 if (deltax == 0)
367 /* DEBUGF("lcd_drawline() called for vertical line - optimisation.\n"); */
368 lcd_vline(x1, y1, y2);
369 return;
371 xinc2 = 1;
372 yinc2 = 1;
374 if (deltax >= deltay)
376 numpixels = deltax;
377 d = 2 * deltay - deltax;
378 dinc1 = deltay * 2;
379 dinc2 = (deltay - deltax) * 2;
380 xinc1 = 1;
381 yinc1 = 0;
383 else
385 numpixels = deltay;
386 d = 2 * deltax - deltay;
387 dinc1 = deltax * 2;
388 dinc2 = (deltax - deltay) * 2;
389 xinc1 = 0;
390 yinc1 = 1;
392 numpixels++; /* include endpoints */
394 if (x1 > x2)
396 xinc1 = -xinc1;
397 xinc2 = -xinc2;
400 if (y1 > y2)
402 yinc1 = -yinc1;
403 yinc2 = -yinc2;
406 x = x1;
407 y = y1;
409 for (i = 0; i < numpixels; i++)
411 if ( ((unsigned)x < (unsigned)current_vp->width)
412 && ((unsigned)y < (unsigned)current_vp->height)
413 #if defined(HAVE_VIEWPORT_CLIP)
414 && ((unsigned)x < (unsigned)LCD_WIDTH)
415 && ((unsigned)y < (unsigned)LCD_HEIGHT)
416 #endif
418 pfunc(LCDADDR(x + current_vp->x, y + current_vp->y));
420 if (d < 0)
422 d += dinc1;
423 x += xinc1;
424 y += yinc1;
426 else
428 d += dinc2;
429 x += xinc2;
430 y += yinc2;
435 /* Draw a horizontal line (optimised) */
436 void lcd_hline(int x1, int x2, int y)
438 int x;
439 fb_data *dst, *dst_end;
440 lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[current_vp->drawmode];
442 /* direction flip */
443 if (x2 < x1)
445 x = x1;
446 x1 = x2;
447 x2 = x;
450 /******************** In viewport clipping **********************/
451 /* nothing to draw? */
452 if (((unsigned)y >= (unsigned)current_vp->height) ||
453 (x1 >= current_vp->width) ||
454 (x2 < 0))
455 return;
457 if (x1 < 0)
458 x1 = 0;
459 if (x2 >= current_vp->width)
460 x2 = current_vp->width-1;
462 /* Adjust x1 and y to viewport */
463 x1 += current_vp->x;
464 x2 += current_vp->x;
465 y += current_vp->y;
467 #if defined(HAVE_VIEWPORT_CLIP)
468 /********************* Viewport on screen clipping ********************/
469 /* nothing to draw? */
470 if (((unsigned)y >= (unsigned) LCD_HEIGHT) || (x1 >= LCD_WIDTH)
471 || (x2 < 0))
472 return;
474 /* clipping */
475 if (x1 < 0)
476 x1 = 0;
477 if (x2 >= LCD_WIDTH)
478 x2 = LCD_WIDTH-1;
479 #endif
481 dst = LCDADDR(x1 , y );
482 dst_end = dst + (x2 - x1) * LCD_HEIGHT;
486 pfunc(dst);
487 dst += LCD_HEIGHT;
489 while (dst <= dst_end);
492 /* Draw a vertical line (optimised) */
493 void lcd_vline(int x, int y1, int y2)
495 int y, height;
496 unsigned bits = 0;
497 enum fill_opt fillopt = OPT_NONE;
498 fb_data *dst, *dst_end;
500 /* direction flip */
501 if (y2 < y1)
503 y = y1;
504 y1 = y2;
505 y2 = y;
508 /******************** In viewport clipping **********************/
509 /* nothing to draw? */
510 if (((unsigned)x >= (unsigned)current_vp->width) ||
511 (y1 >= current_vp->height) ||
512 (y2 < 0))
513 return;
515 if (y1 < 0)
516 y1 = 0;
517 if (y2 >= current_vp->height)
518 y2 = current_vp->height-1;
520 /* adjust for viewport */
521 x += current_vp->x;
522 y1 += current_vp->y;
523 y2 += current_vp->y;
525 #if defined(HAVE_VIEWPORT_CLIP)
526 /********************* Viewport on screen clipping ********************/
527 /* nothing to draw? */
528 if (( (unsigned) x >= (unsigned)LCD_WIDTH) || (y1 >= LCD_HEIGHT)
529 || (y2 < 0))
530 return;
532 /* clipping */
533 if (y1 < 0)
534 y1 = 0;
535 if (y2 >= LCD_HEIGHT)
536 y2 = LCD_HEIGHT-1;
537 #endif
539 height = y2 - y1 + 1;
541 /* drawmode and optimisation */
542 if (current_vp->drawmode & DRMODE_INVERSEVID)
544 if (current_vp->drawmode & DRMODE_BG)
546 if (!lcd_backdrop)
548 fillopt = OPT_SET;
549 bits = current_vp->bg_pattern;
551 else
552 fillopt = OPT_COPY;
555 else
557 if (current_vp->drawmode & DRMODE_FG)
559 fillopt = OPT_SET;
560 bits = current_vp->fg_pattern;
563 if (fillopt == OPT_NONE && current_vp->drawmode != DRMODE_COMPLEMENT)
564 return;
566 dst = LCDADDR(x, y1);
568 switch (fillopt)
570 case OPT_SET:
571 memset16(dst, bits, height);
572 break;
574 case OPT_COPY:
575 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
576 height * sizeof(fb_data));
577 break;
579 case OPT_NONE: /* DRMODE_COMPLEMENT */
580 dst_end = dst + height;
582 *dst = ~(*dst);
583 while (++dst < dst_end);
584 break;
588 /* Draw a rectangular box */
589 void lcd_drawrect(int x, int y, int width, int height)
591 if ((width <= 0) || (height <= 0))
592 return;
594 int x2 = x + width - 1;
595 int y2 = y + height - 1;
597 lcd_vline(x, y, y2);
598 lcd_vline(x2, y, y2);
599 lcd_hline(x, x2, y);
600 lcd_hline(x, x2, y2);
603 /* Fill a rectangular area */
604 void lcd_fillrect(int x, int y, int width, int height)
606 unsigned bits = 0;
607 enum fill_opt fillopt = OPT_NONE;
608 fb_data *dst, *dst_end;
610 /******************** In viewport clipping **********************/
611 /* nothing to draw? */
612 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
613 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
614 return;
616 if (x < 0)
618 width += x;
619 x = 0;
621 if (y < 0)
623 height += y;
624 y = 0;
626 if (x + width > current_vp->width)
627 width = current_vp->width - x;
628 if (y + height > current_vp->height)
629 height = current_vp->height - y;
631 /* adjust for viewport */
632 x += current_vp->x;
633 y += current_vp->y;
635 #if defined(HAVE_VIEWPORT_CLIP)
636 /********************* Viewport on screen clipping ********************/
637 /* nothing to draw? */
638 if ((x >= LCD_WIDTH) || (y >= LCD_HEIGHT)
639 || (x + width <= 0) || (y + height <= 0))
640 return;
642 /* clip image in viewport in screen */
643 if (x < 0)
645 width += x;
646 x = 0;
648 if (y < 0)
650 height += y;
651 y = 0;
653 if (x + width > LCD_WIDTH)
654 width = LCD_WIDTH - x;
655 if (y + height > LCD_HEIGHT)
656 height = LCD_HEIGHT - y;
657 #endif
659 /* drawmode and optimisation */
660 if (current_vp->drawmode & DRMODE_INVERSEVID)
662 if (current_vp->drawmode & DRMODE_BG)
664 if (!lcd_backdrop)
666 fillopt = OPT_SET;
667 bits = current_vp->bg_pattern;
669 else
670 fillopt = OPT_COPY;
673 else
675 if (current_vp->drawmode & DRMODE_FG)
677 fillopt = OPT_SET;
678 bits = current_vp->fg_pattern;
681 if (fillopt == OPT_NONE && current_vp->drawmode != DRMODE_COMPLEMENT)
682 return;
684 dst = LCDADDR(x, y);
685 dst_end = dst + width * LCD_HEIGHT;
689 fb_data *dst_col, *col_end;
691 switch (fillopt)
693 case OPT_SET:
694 memset16(dst, bits, height);
695 break;
697 case OPT_COPY:
698 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
699 height * sizeof(fb_data));
700 break;
702 case OPT_NONE: /* DRMODE_COMPLEMENT */
703 dst_col = dst;
704 col_end = dst_col + height;
706 *dst_col = ~(*dst_col);
707 while (++dst_col < col_end);
708 break;
710 dst+=LCD_HEIGHT;
712 while (dst < dst_end);
715 /* Draw a partial native bitmap */
716 void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y,
717 int stride, int x, int y, int width,
718 int height)
720 fb_data *dst, *dst_end;
722 /******************** Image in viewport clipping **********************/
723 /* nothing to draw? */
724 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
725 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
726 return;
728 if (x < 0)
730 width += x;
731 src_x -= x;
732 x = 0;
734 if (y < 0)
736 height += y;
737 src_y -= y;
738 y = 0;
741 if (x + width > current_vp->width)
742 width = current_vp->width - x;
743 if (y + height > current_vp->height)
744 height = current_vp->height - y;
746 /* adjust for viewport */
747 x += current_vp->x;
748 y += current_vp->y;
750 #if defined(HAVE_VIEWPORT_CLIP)
751 /********************* Viewport on screen clipping ********************/
752 /* nothing to draw? */
753 if ((x >= LCD_WIDTH) || (y >= LCD_HEIGHT)
754 || (x + width <= 0) || (y + height <= 0))
755 return;
757 /* clip image in viewport in screen */
758 if (x < 0)
760 width += x;
761 src_x -= x;
762 x = 0;
764 if (y < 0)
766 height += y;
767 src_y -= y;
768 y = 0;
770 if (x + width > LCD_WIDTH)
771 width = LCD_WIDTH - x;
772 if (y + height > LCD_HEIGHT)
773 height = LCD_HEIGHT - y;
774 #endif
776 src += stride * src_x + src_y; /* move starting point */
777 dst = LCDADDR(x, y);
778 dst_end = dst + width * LCD_HEIGHT;
782 memcpy(dst, src, height * sizeof(fb_data));
783 src += stride;
784 dst += LCD_HEIGHT;
786 while (dst < dst_end);
789 /* Draw a full native bitmap */
790 void lcd_bitmap(const fb_data *src, int x, int y, int width, int height)
792 lcd_bitmap_part(src, 0, 0, STRIDE(SCREEN_MAIN, width, height), x, y, width, height);
795 #if !defined(TOSHIBA_GIGABEAT_F) && !defined(TOSHIBA_GIGABEAT_S) \
796 || defined(SIMULATOR)
797 /* Draw a partial native bitmap */
798 void ICODE_ATTR lcd_bitmap_transparent_part(const fb_data *src, int src_x,
799 int src_y, int stride, int x,
800 int y, int width, int height)
802 fb_data *dst, *dst_end;
804 /******************** Image in viewport clipping **********************/
805 /* nothing to draw? */
806 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
807 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
808 return;
810 if (x < 0)
812 width += x;
813 src_x -= x;
814 x = 0;
816 if (y < 0)
818 height += y;
819 src_y -= y;
820 y = 0;
823 if (x + width > current_vp->width)
824 width = current_vp->width - x;
825 if (y + height > current_vp->height)
826 height = current_vp->height - y;
828 /* adjust for viewport */
829 x += current_vp->x;
830 y += current_vp->y;
832 #if defined(HAVE_VIEWPORT_CLIP)
833 /********************* Viewport on screen clipping ********************/
834 /* nothing to draw? */
835 if ((x >= LCD_WIDTH) || (y >= LCD_HEIGHT)
836 || (x + width <= 0) || (y + height <= 0))
837 return;
839 /* clip image in viewport in screen */
840 if (x < 0)
842 width += x;
843 src_x -= x;
844 x = 0;
846 if (y < 0)
848 height += y;
849 src_y -= y;
850 y = 0;
852 if (x + width > LCD_WIDTH)
853 width = LCD_WIDTH - x;
854 if (y + height > LCD_HEIGHT)
855 height = LCD_HEIGHT - y;
856 #endif
858 src += stride * src_x + src_y; /* move starting point */
859 dst = LCDADDR(x, y);
860 dst_end = dst + width * LCD_HEIGHT;
864 int i;
865 for(i = 0;i < height;i++)
867 if (src[i] == REPLACEWITHFG_COLOR)
868 dst[i] = current_vp->fg_pattern;
869 else if(src[i] != TRANSPARENT_COLOR)
870 dst[i] = src[i];
872 src += stride;
873 dst += LCD_HEIGHT;
875 while (dst < dst_end);
877 #endif /* !defined(TOSHIBA_GIGABEAT_F) || defined(SIMULATOR) */
879 /* Draw a full native bitmap with a transparent color */
880 void lcd_bitmap_transparent(const fb_data *src, int x, int y,
881 int width, int height)
883 lcd_bitmap_transparent_part(src, 0, 0,
884 STRIDE(SCREEN_MAIN, width, height), x, y, width, height);
887 #define ROW_INC 1
888 #define COL_INC LCD_HEIGHT
890 #include "lcd-16bit-common.c"
892 #include "lcd-bitmap-common.c"