1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Alan Korr
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 ****************************************************************************/
33 #include "rbunicode.h"
35 #include "scroll_engine.h"
37 #ifndef LCDFN /* Not compiling for remote - define macros for main LCD. */
38 #define LCDFN(fn) lcd_ ## fn
39 #define FBFN(fn) fb_ ## fn
40 #define LCDM(ma) LCD_ ## ma
41 #define LCDNAME "lcd_"
47 FBFN(data
) LCDFN(framebuffer
)[LCDM(FBHEIGHT
)][LCDM(FBWIDTH
)] IRAM_LCDFRAMEBUFFER
;
49 static struct viewport default_vp
=
54 .height
= LCDM(HEIGHT
),
55 .font
= FONT_SYSFIXED
,
56 .drawmode
= DRMODE_SOLID
,
59 static struct viewport
* current_vp
= &default_vp
;
63 void LCDFN(set_viewport
)(struct viewport
* vp
)
66 current_vp
= &default_vp
;
70 #if defined(SIMULATOR)
71 /* Force the viewport to be within bounds. If this happens it should
72 * be considered an error - the viewport will not draw as it might be
75 if((unsigned) current_vp
->x
> (unsigned) LCDM(WIDTH
)
76 || (unsigned) current_vp
->y
> (unsigned) LCDM(HEIGHT
)
77 || current_vp
->x
+ current_vp
->width
> LCDM(WIDTH
)
78 || current_vp
->y
+ current_vp
->height
> LCDM(HEIGHT
))
80 #if !defined(HAVE_VIEWPORT_CLIP)
85 "set_viewport out of bounds: x: %d y: %d width: %d height:%d\n",
86 current_vp
->x
, current_vp
->y
,
87 current_vp
->width
, current_vp
->height
);
93 void LCDFN(update_viewport
)(void)
95 LCDFN(update_rect
)(current_vp
->x
, current_vp
->y
,
96 current_vp
->width
, current_vp
->height
);
99 void LCDFN(update_viewport_rect
)(int x
, int y
, int width
, int height
)
101 LCDFN(update_rect
)(current_vp
->x
+ x
, current_vp
->y
+ y
, width
, height
);
105 void LCDFN(init
)(void)
107 LCDFN(clear_display
)();
109 LCDFN(init_device
)();
116 /*** parameter handling ***/
118 void LCDFN(set_drawmode
)(int mode
)
120 current_vp
->drawmode
= mode
& (DRMODE_SOLID
|DRMODE_INVERSEVID
);
123 int LCDFN(get_drawmode
)(void)
125 return current_vp
->drawmode
;
128 int LCDFN(getwidth
)(void)
130 return current_vp
->width
;
133 int LCDFN(getheight
)(void)
135 return current_vp
->height
;
138 void LCDFN(setfont
)(int newfont
)
140 current_vp
->font
= newfont
;
143 int LCDFN(getfont
)(void)
145 return current_vp
->font
;
148 int LCDFN(getstringsize
)(const unsigned char *str
, int *w
, int *h
)
150 return font_getstringsize(str
, w
, h
, current_vp
->font
);
153 /*** low-level drawing functions ***/
155 static void setpixel(int x
, int y
)
157 LCDFN(framebuffer
)[y
>>3][x
] |= BIT_N(y
& 7);
160 static void clearpixel(int x
, int y
)
162 LCDFN(framebuffer
)[y
>>3][x
] &= ~BIT_N(y
& 7);
165 static void flippixel(int x
, int y
)
167 LCDFN(framebuffer
)[y
>>3][x
] ^= BIT_N(y
& 7);
170 static void nopixel(int x
, int y
)
176 LCDFN(pixelfunc_type
)* const LCDFN(pixelfuncs
)[8] = {
177 flippixel
, nopixel
, setpixel
, setpixel
,
178 nopixel
, clearpixel
, nopixel
, clearpixel
181 static void ICODE_ATTR
flipblock(FBFN(data
) *address
, unsigned mask
,
184 *address
^= bits
& mask
;
187 static void ICODE_ATTR
bgblock(FBFN(data
) *address
, unsigned mask
,
190 *address
&= bits
| ~mask
;
193 static void ICODE_ATTR
fgblock(FBFN(data
) *address
, unsigned mask
,
196 *address
|= bits
& mask
;
199 static void ICODE_ATTR
solidblock(FBFN(data
) *address
, unsigned mask
,
202 unsigned data
= *(char*)address
;
205 *address
= data
^ (bits
& mask
);
208 static void ICODE_ATTR
flipinvblock(FBFN(data
) *address
, unsigned mask
,
211 *address
^= ~bits
& mask
;
214 static void ICODE_ATTR
bginvblock(FBFN(data
) *address
, unsigned mask
,
217 *address
&= ~(bits
& mask
);
220 static void ICODE_ATTR
fginvblock(FBFN(data
) *address
, unsigned mask
,
223 *address
|= ~bits
& mask
;
226 static void ICODE_ATTR
solidinvblock(FBFN(data
) *address
, unsigned mask
,
229 unsigned data
= *(char *)address
;
232 *address
= data
^ (bits
& mask
);
235 LCDFN(blockfunc_type
)* const LCDFN(blockfuncs
)[8] = {
236 flipblock
, bgblock
, fgblock
, solidblock
,
237 flipinvblock
, bginvblock
, fginvblock
, solidinvblock
240 /*** drawing functions ***/
242 /* Clear the whole display */
243 void LCDFN(clear_display
)(void)
245 unsigned bits
= (current_vp
->drawmode
& DRMODE_INVERSEVID
) ? 0xFFu
: 0;
247 memset(LCDFN(framebuffer
), bits
, sizeof LCDFN(framebuffer
));
248 LCDFN(scroll_info
).lines
= 0;
251 /* Clear the current viewport */
252 void LCDFN(clear_viewport
)(void)
256 if (current_vp
== &default_vp
)
258 LCDFN(clear_display
)();
262 oldmode
= current_vp
->drawmode
;
264 /* Invert the INVERSEVID bit and set basic mode to SOLID */
265 current_vp
->drawmode
= (~current_vp
->drawmode
& DRMODE_INVERSEVID
) |
268 LCDFN(fillrect
)(0, 0, current_vp
->width
, current_vp
->height
);
270 current_vp
->drawmode
= oldmode
;
272 LCDFN(scroll_stop
)(current_vp
);
276 /* Set a single pixel */
277 void LCDFN(drawpixel
)(int x
, int y
)
279 if ( ((unsigned)x
< (unsigned)current_vp
->width
)
280 && ((unsigned)y
< (unsigned)current_vp
->height
)
281 #if defined(HAVE_VIEWPORT_CLIP)
282 && ((unsigned)x
< (unsigned)LCDM(WIDTH
))
283 && ((unsigned)y
< (unsigned)LCDM(HEIGHT
))
286 LCDFN(pixelfuncs
)[current_vp
->drawmode
](current_vp
->x
+ x
, current_vp
->y
+ y
);
290 void LCDFN(drawline
)(int x1
, int y1
, int x2
, int y2
)
298 LCDFN(pixelfunc_type
) *pfunc
= LCDFN(pixelfuncs
)[current_vp
->drawmode
];
300 deltax
= abs(x2
- x1
);
303 DEBUGF(LCDNAME
"drawline() called for vertical line - optimisation.\n");
304 LCDFN(vline
)(x1
, y1
, y2
);
307 deltay
= abs(y2
- y1
);
310 DEBUGF(LCDNAME
"drawline() called for horizontal line - optimisation.\n");
311 LCDFN(hline
)(x1
, x2
, y1
);
317 if (deltax
>= deltay
)
320 d
= 2 * deltay
- deltax
;
322 dinc2
= (deltay
- deltax
) * 2;
329 d
= 2 * deltax
- deltay
;
331 dinc2
= (deltax
- deltay
) * 2;
335 numpixels
++; /* include endpoints */
352 for (i
= 0; i
< numpixels
; i
++)
354 if ( ((unsigned)x
< (unsigned)current_vp
->width
)
355 && ((unsigned)y
< (unsigned)current_vp
->height
)
356 #if defined(HAVE_VIEWPORT_CLIP)
357 && ((unsigned)x
< (unsigned)LCDM(WIDTH
))
358 && ((unsigned)y
< (unsigned)LCDM(HEIGHT
))
361 pfunc(current_vp
->x
+ x
, current_vp
->y
+ y
);
378 /* Draw a horizontal line (optimised) */
379 void LCDFN(hline
)(int x1
, int x2
, int y
)
382 unsigned char *dst
, *dst_end
;
384 LCDFN(blockfunc_type
) *bfunc
;
394 /******************** In viewport clipping **********************/
395 /* nothing to draw? */
396 if (((unsigned)y
>= (unsigned)current_vp
->height
) || (x1
>= current_vp
->width
)
402 if (x2
>= current_vp
->width
)
403 x2
= current_vp
->width
-1;
405 /* adjust to viewport */
410 #if defined(HAVE_VIEWPORT_CLIP)
411 /********************* Viewport on screen clipping ********************/
412 /* nothing to draw? */
413 if (((unsigned)y
>= (unsigned) LCDM(HEIGHT
)) || (x1
>= LCDM(WIDTH
))
420 if (x2
>= LCDM(WIDTH
))
426 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
427 dst
= &LCDFN(framebuffer
)[y
>>3][x1
];
430 dst_end
= dst
+ width
;
432 bfunc(dst
++, mask
, 0xFFu
);
433 while (dst
< dst_end
);
436 /* Draw a vertical line (optimised) */
437 void LCDFN(vline
)(int x
, int y1
, int y2
)
441 unsigned mask
, mask_bottom
;
442 LCDFN(blockfunc_type
) *bfunc
;
452 /******************** In viewport clipping **********************/
453 /* nothing to draw? */
454 if (((unsigned)x
>= (unsigned)current_vp
->width
) || (y1
>= current_vp
->height
)
460 if (y2
>= current_vp
->height
)
461 y2
= current_vp
->height
-1;
463 /* adjust for viewport */
468 #if defined(HAVE_VIEWPORT_CLIP)
469 /********************* Viewport on screen clipping ********************/
470 /* nothing to draw? */
471 if (( (unsigned) x
>= (unsigned)LCDM(WIDTH
)) || (y1
>= LCDM(HEIGHT
))
478 if (y2
>= LCDM(HEIGHT
))
482 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
483 dst
= &LCDFN(framebuffer
)[y1
>>3][x
];
485 mask
= 0xFFu
<< (y1
& 7);
486 mask_bottom
= 0xFFu
>> (~ny
& 7);
488 for (; ny
>= 8; ny
-= 8)
490 bfunc(dst
, mask
, 0xFFu
);
495 bfunc(dst
, mask
, 0xFFu
);
498 /* Draw a rectangular box */
499 void LCDFN(drawrect
)(int x
, int y
, int width
, int height
)
501 if ((width
<= 0) || (height
<= 0))
504 int x2
= x
+ width
- 1;
505 int y2
= y
+ height
- 1;
507 LCDFN(vline
)(x
, y
, y2
);
508 LCDFN(vline
)(x2
, y
, y2
);
509 LCDFN(hline
)(x
, x2
, y
);
510 LCDFN(hline
)(x
, x2
, y2
);
513 /* Fill a rectangular area */
514 void LCDFN(fillrect
)(int x
, int y
, int width
, int height
)
517 FBFN(data
) *dst
, *dst_end
;
518 unsigned mask
, mask_bottom
;
520 LCDFN(blockfunc_type
) *bfunc
;
521 bool fillopt
= false;
523 /******************** In viewport clipping **********************/
524 /* nothing to draw? */
525 if ((width
<= 0) || (height
<= 0) || (x
>= current_vp
->width
)
526 || (y
>= current_vp
->height
) || (x
+ width
<= 0) || (y
+ height
<= 0))
539 if (x
+ width
> current_vp
->width
)
540 width
= current_vp
->width
- x
;
541 if (y
+ height
> current_vp
->height
)
542 height
= current_vp
->height
- y
;
544 /* adjust for viewport */
548 #if defined(HAVE_VIEWPORT_CLIP)
549 /********************* Viewport on screen clipping ********************/
550 /* nothing to draw? */
551 if ((x
>= LCDM(WIDTH
)) || (y
>= LCDM(HEIGHT
))
552 || (x
+ width
<= 0) || (y
+ height
<= 0))
555 /* clip image in viewport in screen */
566 if (x
+ width
> LCDM(WIDTH
))
567 width
= LCDM(WIDTH
) - x
;
568 if (y
+ height
> LCDM(HEIGHT
))
569 height
= LCDM(HEIGHT
) - y
;
572 if (current_vp
->drawmode
& DRMODE_INVERSEVID
)
574 if (current_vp
->drawmode
& DRMODE_BG
)
581 if (current_vp
->drawmode
& DRMODE_FG
)
587 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
588 dst
= &LCDFN(framebuffer
)[y
>>3][x
];
589 ny
= height
- 1 + (y
& 7);
590 mask
= 0xFFu
<< (y
& 7);
591 mask_bottom
= 0xFFu
>> (~ny
& 7);
593 for (; ny
>= 8; ny
-= 8)
595 if (fillopt
&& (mask
== 0xFFu
))
596 memset(dst
, bits
, width
);
599 FBFN(data
) *dst_row
= dst
;
601 dst_end
= dst_row
+ width
;
603 bfunc(dst_row
++, mask
, 0xFFu
);
604 while (dst_row
< dst_end
);
612 if (fillopt
&& (mask
== 0xFFu
))
613 memset(dst
, bits
, width
);
616 dst_end
= dst
+ width
;
618 bfunc(dst
++, mask
, 0xFFu
);
619 while (dst
< dst_end
);
623 /* About Rockbox' internal bitmap format:
625 * A bitmap contains one bit for every pixel that defines if that pixel is
626 * black (1) or white (0). Bits within a byte are arranged vertically, LSB
628 * The bytes are stored in row-major order, with byte 0 being top left,
629 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
630 * 0..7, the second row defines pixel row 8..15 etc.
632 * This is the same as the internal lcd hw format. */
634 /* Draw a partial bitmap */
635 void ICODE_ATTR
LCDFN(bitmap_part
)(const unsigned char *src
, int src_x
,
636 int src_y
, int stride
, int x
, int y
,
637 int width
, int height
)
640 FBFN(data
) *dst
, *dst_end
;
641 unsigned mask
, mask_bottom
;
642 LCDFN(blockfunc_type
) *bfunc
;
644 /******************** Image in viewport clipping **********************/
645 /* nothing to draw? */
646 if ((width
<= 0) || (height
<= 0) || (x
>= current_vp
->width
)
647 || (y
>= current_vp
->height
) || (x
+ width
<= 0) || (y
+ height
<= 0))
650 /* clip image in viewport */
663 if (x
+ width
> current_vp
->width
)
664 width
= current_vp
->width
- x
;
665 if (y
+ height
> current_vp
->height
)
666 height
= current_vp
->height
- y
;
668 /* adjust for viewport */
672 #if defined(HAVE_VIEWPORT_CLIP)
673 /********************* Viewport on screen clipping ********************/
674 /* nothing to draw? */
675 if ((x
>= LCDM(WIDTH
)) || (y
>= LCDM(HEIGHT
))
676 || (x
+ width
<= 0) || (y
+ height
<= 0))
679 /* clip image in viewport in screen */
692 if (x
+ width
> LCDM(WIDTH
))
693 width
= LCDM(WIDTH
) - x
;
694 if (y
+ height
> LCDM(HEIGHT
))
695 height
= LCDM(HEIGHT
) - y
;
698 src
+= stride
* (src_y
>> 3) + src_x
; /* move starting point */
701 dst
= &LCDFN(framebuffer
)[y
>>3][x
];
703 ny
= height
- 1 + shift
+ src_y
;
705 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
706 mask
= 0xFFu
<< (shift
+ src_y
);
707 mask_bottom
= 0xFFu
>> (~ny
& 7);
711 bool copyopt
= (current_vp
->drawmode
== DRMODE_SOLID
);
713 for (; ny
>= 8; ny
-= 8)
715 if (copyopt
&& (mask
== 0xFFu
))
716 memcpy(dst
, src
, width
);
719 const unsigned char *src_row
= src
;
720 FBFN(data
) *dst_row
= dst
;
722 dst_end
= dst_row
+ width
;
724 bfunc(dst_row
++, mask
, *src_row
++);
725 while (dst_row
< dst_end
);
734 if (copyopt
&& (mask
== 0xFFu
))
735 memcpy(dst
, src
, width
);
738 dst_end
= dst
+ width
;
740 bfunc(dst
++, mask
, *src
++);
741 while (dst
< dst_end
);
746 dst_end
= dst
+ width
;
749 const unsigned char *src_col
= src
++;
750 FBFN(data
) *dst_col
= dst
++;
751 unsigned mask_col
= mask
;
754 for (y
= ny
; y
>= 8; y
-= 8)
756 data
|= *src_col
<< shift
;
758 if (mask_col
& 0xFFu
)
760 bfunc(dst_col
, mask_col
, data
);
767 dst_col
+= LCDM(WIDTH
);
770 data
|= *src_col
<< shift
;
771 bfunc(dst_col
, mask_col
& mask_bottom
, data
);
773 while (dst
< dst_end
);
777 /* Draw a full bitmap */
778 void LCDFN(bitmap
)(const unsigned char *src
, int x
, int y
, int width
,
781 LCDFN(bitmap_part
)(src
, 0, 0, width
, x
, y
, width
, height
);
784 #include "lcd-bitmap-common.c"