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
;
71 void LCDFN(update_viewport
)(void)
73 LCDFN(update_rect
)(current_vp
->x
, current_vp
->y
,
74 current_vp
->width
, current_vp
->height
);
77 void LCDFN(update_viewport_rect
)(int x
, int y
, int width
, int height
)
79 LCDFN(update_rect
)(current_vp
->x
+ x
, current_vp
->y
+ y
, width
, height
);
83 void LCDFN(init
)(void)
85 LCDFN(clear_display
)();
94 /*** parameter handling ***/
96 void LCDFN(set_drawmode
)(int mode
)
98 current_vp
->drawmode
= mode
& (DRMODE_SOLID
|DRMODE_INVERSEVID
);
101 int LCDFN(get_drawmode
)(void)
103 return current_vp
->drawmode
;
106 int LCDFN(getwidth
)(void)
108 return current_vp
->width
;
111 int LCDFN(getheight
)(void)
113 return current_vp
->height
;
116 void LCDFN(setfont
)(int newfont
)
118 current_vp
->font
= newfont
;
121 int LCDFN(getfont
)(void)
123 return current_vp
->font
;
126 int LCDFN(getstringsize
)(const unsigned char *str
, int *w
, int *h
)
128 return font_getstringsize(str
, w
, h
, current_vp
->font
);
131 /*** low-level drawing functions ***/
133 static void setpixel(int x
, int y
)
135 LCDFN(framebuffer
)[y
>>3][x
] |= BIT_N(y
& 7);
138 static void clearpixel(int x
, int y
)
140 LCDFN(framebuffer
)[y
>>3][x
] &= ~BIT_N(y
& 7);
143 static void flippixel(int x
, int y
)
145 LCDFN(framebuffer
)[y
>>3][x
] ^= BIT_N(y
& 7);
148 static void nopixel(int x
, int y
)
154 LCDFN(pixelfunc_type
)* const LCDFN(pixelfuncs
)[8] = {
155 flippixel
, nopixel
, setpixel
, setpixel
,
156 nopixel
, clearpixel
, nopixel
, clearpixel
159 static void ICODE_ATTR
flipblock(FBFN(data
) *address
, unsigned mask
,
162 *address
^= bits
& mask
;
165 static void ICODE_ATTR
bgblock(FBFN(data
) *address
, unsigned mask
,
168 *address
&= bits
| ~mask
;
171 static void ICODE_ATTR
fgblock(FBFN(data
) *address
, unsigned mask
,
174 *address
|= bits
& mask
;
177 static void ICODE_ATTR
solidblock(FBFN(data
) *address
, unsigned mask
,
180 unsigned data
= *(char*)address
;
183 *address
= data
^ (bits
& mask
);
186 static void ICODE_ATTR
flipinvblock(FBFN(data
) *address
, unsigned mask
,
189 *address
^= ~bits
& mask
;
192 static void ICODE_ATTR
bginvblock(FBFN(data
) *address
, unsigned mask
,
195 *address
&= ~(bits
& mask
);
198 static void ICODE_ATTR
fginvblock(FBFN(data
) *address
, unsigned mask
,
201 *address
|= ~bits
& mask
;
204 static void ICODE_ATTR
solidinvblock(FBFN(data
) *address
, unsigned mask
,
207 unsigned data
= *(char *)address
;
210 *address
= data
^ (bits
& mask
);
213 LCDFN(blockfunc_type
)* const LCDFN(blockfuncs
)[8] = {
214 flipblock
, bgblock
, fgblock
, solidblock
,
215 flipinvblock
, bginvblock
, fginvblock
, solidinvblock
218 /*** drawing functions ***/
220 /* Clear the whole display */
221 void LCDFN(clear_display
)(void)
223 unsigned bits
= (current_vp
->drawmode
& DRMODE_INVERSEVID
) ? 0xFFu
: 0;
225 memset(LCDFN(framebuffer
), bits
, sizeof LCDFN(framebuffer
));
226 LCDFN(scroll_info
).lines
= 0;
229 /* Clear the current viewport */
230 void LCDFN(clear_viewport
)(void)
234 if (current_vp
== &default_vp
)
236 LCDFN(clear_display
)();
240 oldmode
= current_vp
->drawmode
;
242 /* Invert the INVERSEVID bit and set basic mode to SOLID */
243 current_vp
->drawmode
= (~current_vp
->drawmode
& DRMODE_INVERSEVID
) |
246 LCDFN(fillrect
)(0, 0, current_vp
->width
, current_vp
->height
);
248 current_vp
->drawmode
= oldmode
;
250 LCDFN(scroll_stop
)(current_vp
);
254 /* Set a single pixel */
255 void LCDFN(drawpixel
)(int x
, int y
)
257 if (((unsigned)x
< (unsigned)current_vp
->width
) &&
258 ((unsigned)y
< (unsigned)current_vp
->height
))
259 LCDFN(pixelfuncs
)[current_vp
->drawmode
](current_vp
->x
+ x
, current_vp
->y
+ y
);
263 void LCDFN(drawline
)(int x1
, int y1
, int x2
, int y2
)
271 LCDFN(pixelfunc_type
) *pfunc
= LCDFN(pixelfuncs
)[current_vp
->drawmode
];
273 deltax
= abs(x2
- x1
);
276 DEBUGF(LCDNAME
"drawline() called for vertical line - optimisation.\n");
277 LCDFN(vline
)(x1
, y1
, y2
);
280 deltay
= abs(y2
- y1
);
283 DEBUGF(LCDNAME
"drawline() called for horizontal line - optimisation.\n");
284 LCDFN(hline
)(x1
, x2
, y1
);
290 if (deltax
>= deltay
)
293 d
= 2 * deltay
- deltax
;
295 dinc2
= (deltay
- deltax
) * 2;
302 d
= 2 * deltax
- deltay
;
304 dinc2
= (deltax
- deltay
) * 2;
308 numpixels
++; /* include endpoints */
325 for (i
= 0; i
< numpixels
; i
++)
327 if (((unsigned)x
< (unsigned)current_vp
->width
)
328 && ((unsigned)y
< (unsigned)current_vp
->height
))
329 pfunc(current_vp
->x
+ x
, current_vp
->y
+ y
);
346 /* Draw a horizontal line (optimised) */
347 void LCDFN(hline
)(int x1
, int x2
, int y
)
350 unsigned char *dst
, *dst_end
;
352 LCDFN(blockfunc_type
) *bfunc
;
362 /* nothing to draw? */
363 if (((unsigned)y
>= (unsigned)current_vp
->height
) || (x1
>= current_vp
->width
)
370 if (x2
>= current_vp
->width
)
371 x2
= current_vp
->width
-1;
375 /* adjust to viewport */
379 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
380 dst
= &LCDFN(framebuffer
)[y
>>3][x1
];
383 dst_end
= dst
+ width
;
385 bfunc(dst
++, mask
, 0xFFu
);
386 while (dst
< dst_end
);
389 /* Draw a vertical line (optimised) */
390 void LCDFN(vline
)(int x
, int y1
, int y2
)
394 unsigned mask
, mask_bottom
;
395 LCDFN(blockfunc_type
) *bfunc
;
405 /* nothing to draw? */
406 if (((unsigned)x
>= (unsigned)current_vp
->width
) || (y1
>= current_vp
->height
)
413 if (y2
>= current_vp
->height
)
414 y2
= current_vp
->height
-1;
416 /* adjust for viewport */
421 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
422 dst
= &LCDFN(framebuffer
)[y1
>>3][x
];
424 mask
= 0xFFu
<< (y1
& 7);
425 mask_bottom
= 0xFFu
>> (~ny
& 7);
427 for (; ny
>= 8; ny
-= 8)
429 bfunc(dst
, mask
, 0xFFu
);
434 bfunc(dst
, mask
, 0xFFu
);
437 /* Draw a rectangular box */
438 void LCDFN(drawrect
)(int x
, int y
, int width
, int height
)
440 if ((width
<= 0) || (height
<= 0))
443 int x2
= x
+ width
- 1;
444 int y2
= y
+ height
- 1;
446 LCDFN(vline
)(x
, y
, y2
);
447 LCDFN(vline
)(x2
, y
, y2
);
448 LCDFN(hline
)(x
, x2
, y
);
449 LCDFN(hline
)(x
, x2
, y2
);
452 /* Fill a rectangular area */
453 void LCDFN(fillrect
)(int x
, int y
, int width
, int height
)
456 FBFN(data
) *dst
, *dst_end
;
457 unsigned mask
, mask_bottom
;
459 LCDFN(blockfunc_type
) *bfunc
;
460 bool fillopt
= false;
462 /* nothing to draw? */
463 if ((width
<= 0) || (height
<= 0) || (x
>= current_vp
->width
)
464 || (y
>= current_vp
->height
) || (x
+ width
<= 0) || (y
+ height
<= 0))
478 if (x
+ width
> current_vp
->width
)
479 width
= current_vp
->width
- x
;
480 if (y
+ height
> current_vp
->height
)
481 height
= current_vp
->height
- y
;
483 /* adjust for viewport */
487 if (current_vp
->drawmode
& DRMODE_INVERSEVID
)
489 if (current_vp
->drawmode
& DRMODE_BG
)
496 if (current_vp
->drawmode
& DRMODE_FG
)
502 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
503 dst
= &LCDFN(framebuffer
)[y
>>3][x
];
504 ny
= height
- 1 + (y
& 7);
505 mask
= 0xFFu
<< (y
& 7);
506 mask_bottom
= 0xFFu
>> (~ny
& 7);
508 for (; ny
>= 8; ny
-= 8)
510 if (fillopt
&& (mask
== 0xFFu
))
511 memset(dst
, bits
, width
);
514 FBFN(data
) *dst_row
= dst
;
516 dst_end
= dst_row
+ width
;
518 bfunc(dst_row
++, mask
, 0xFFu
);
519 while (dst_row
< dst_end
);
527 if (fillopt
&& (mask
== 0xFFu
))
528 memset(dst
, bits
, width
);
531 dst_end
= dst
+ width
;
533 bfunc(dst
++, mask
, 0xFFu
);
534 while (dst
< dst_end
);
538 /* About Rockbox' internal bitmap format:
540 * A bitmap contains one bit for every pixel that defines if that pixel is
541 * black (1) or white (0). Bits within a byte are arranged vertically, LSB
543 * The bytes are stored in row-major order, with byte 0 being top left,
544 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
545 * 0..7, the second row defines pixel row 8..15 etc.
547 * This is the same as the internal lcd hw format. */
549 /* Draw a partial bitmap */
550 void ICODE_ATTR
LCDFN(bitmap_part
)(const unsigned char *src
, int src_x
,
551 int src_y
, int stride
, int x
, int y
,
552 int width
, int height
)
555 FBFN(data
) *dst
, *dst_end
;
556 unsigned mask
, mask_bottom
;
557 LCDFN(blockfunc_type
) *bfunc
;
559 /* nothing to draw? */
560 if ((width
<= 0) || (height
<= 0) || (x
>= current_vp
->width
)
561 || (y
>= current_vp
->height
) || (x
+ width
<= 0) || (y
+ height
<= 0))
577 if (x
+ width
> current_vp
->width
)
578 width
= current_vp
->width
- x
;
579 if (y
+ height
> current_vp
->height
)
580 height
= current_vp
->height
- y
;
582 /* adjust for viewport */
586 src
+= stride
* (src_y
>> 3) + src_x
; /* move starting point */
589 dst
= &LCDFN(framebuffer
)[y
>>3][x
];
591 ny
= height
- 1 + shift
+ src_y
;
593 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
594 mask
= 0xFFu
<< (shift
+ src_y
);
595 mask_bottom
= 0xFFu
>> (~ny
& 7);
599 bool copyopt
= (current_vp
->drawmode
== DRMODE_SOLID
);
601 for (; ny
>= 8; ny
-= 8)
603 if (copyopt
&& (mask
== 0xFFu
))
604 memcpy(dst
, src
, width
);
607 const unsigned char *src_row
= src
;
608 FBFN(data
) *dst_row
= dst
;
610 dst_end
= dst_row
+ width
;
612 bfunc(dst_row
++, mask
, *src_row
++);
613 while (dst_row
< dst_end
);
622 if (copyopt
&& (mask
== 0xFFu
))
623 memcpy(dst
, src
, width
);
626 dst_end
= dst
+ width
;
628 bfunc(dst
++, mask
, *src
++);
629 while (dst
< dst_end
);
634 dst_end
= dst
+ width
;
637 const unsigned char *src_col
= src
++;
638 FBFN(data
) *dst_col
= dst
++;
639 unsigned mask_col
= mask
;
642 for (y
= ny
; y
>= 8; y
-= 8)
644 data
|= *src_col
<< shift
;
646 if (mask_col
& 0xFFu
)
648 bfunc(dst_col
, mask_col
, data
);
655 dst_col
+= LCDM(WIDTH
);
658 data
|= *src_col
<< shift
;
659 bfunc(dst_col
, mask_col
& mask_bottom
, data
);
661 while (dst
< dst_end
);
665 /* Draw a full bitmap */
666 void LCDFN(bitmap
)(const unsigned char *src
, int x
, int y
, int width
,
669 LCDFN(bitmap_part
)(src
, 0, 0, width
, x
, y
, width
, height
);
672 #include "lcd-bitmap-common.c"