convert to unix line endings.
[Rockbox.git] / firmware / drivers / lcd-2bit-vi.c
blob7d97f191745b36097197b5378b1c00db4bbb749b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Jens Arnold
12 * Rockbox driver for 2bit vertically interleaved LCDs
14 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
24 #include "lcd.h"
25 #include "kernel.h"
26 #include "thread.h"
27 #include <string.h>
28 #include <stdlib.h>
29 #include "memory.h"
30 #include "file.h"
31 #include "debug.h"
32 #include "system.h"
33 #include "font.h"
34 #include "rbunicode.h"
35 #include "bidi.h"
36 #include "scroll_engine.h"
38 #ifndef LCDFN /* Not compiling for remote - define macros for main LCD. */
39 #define LCDFN(fn) lcd_ ## fn
40 #define FBFN(fn) fb_ ## fn
41 #define LCDM(ma) LCD_ ## ma
42 #define LCDNAME "lcd_"
43 #define MAIN_LCD
44 #endif
46 /*** globals ***/
48 FBFN(data) LCDFN(framebuffer)[LCDM(FBHEIGHT)][LCDM(FBWIDTH)] IBSS_ATTR;
50 static const FBFN(data) patterns[4] = {0xFFFF, 0xFF00, 0x00FF, 0x0000};
52 static FBFN(data) *backdrop = NULL;
53 static long backdrop_offset IDATA_ATTR = 0;
55 static struct viewport default_vp =
57 .x = 0,
58 .y = 0,
59 .width = LCDM(WIDTH),
60 .height = LCDM(HEIGHT),
61 .font = FONT_SYSFIXED,
62 .drawmode = DRMODE_SOLID,
63 .xmargin = 0,
64 .ymargin = 0,
65 .fg_pattern = LCDM(DEFAULT_FG),
66 .bg_pattern = LCDM(DEFAULT_BG)
69 static struct viewport *current_vp IBSS_ATTR;
71 static unsigned fg_pattern IBSS_ATTR;
72 static unsigned bg_pattern IBSS_ATTR;
74 /*** Viewports ***/
76 void LCDFN(set_viewport)(struct viewport* vp)
78 if (vp == NULL)
79 current_vp = &default_vp;
80 else
81 current_vp = vp;
83 fg_pattern = patterns[current_vp->fg_pattern & 3];
84 bg_pattern = patterns[current_vp->bg_pattern & 3];
87 void LCDFN(update_viewport)(void)
89 LCDFN(update_rect)(current_vp->x, current_vp->y,
90 current_vp->width, current_vp->height);
93 void LCDFN(update_viewport_rect)(int x, int y, int width, int height)
95 LCDFN(update_rect)(current_vp->x + x, current_vp->y + y, width, height);
98 /* LCD init */
99 void LCDFN(init)(void)
101 LCDFN(set_viewport)(NULL);
102 LCDFN(clear_display)();
103 #ifndef SIMULATOR
104 LCDFN(init_device)();
105 #endif
106 #ifdef MAIN_LCD
107 scroll_init();
108 #endif
111 /*** parameter handling ***/
113 #if !defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
114 /* When compiling for remote LCD and the main LCD is colour. */
115 unsigned lcd_remote_color_to_native(unsigned color)
117 unsigned r = (color & 0xf800) >> 10;
118 unsigned g = (color & 0x07e0) >> 5;
119 unsigned b = (color & 0x001f) << 2;
121 * |R|
122 * |Y'| = |0.299000 0.587000 0.114000| |G|
123 * |B|
125 return (5*r + 9*g + b) >> 8;
127 #endif
129 void LCDFN(set_drawmode)(int mode)
131 current_vp->drawmode = mode & (DRMODE_SOLID|DRMODE_INVERSEVID);
134 int LCDFN(get_drawmode)(void)
136 return current_vp->drawmode;
139 void LCDFN(set_foreground)(unsigned brightness)
141 current_vp->fg_pattern = brightness;
142 fg_pattern = patterns[brightness & 3];
145 unsigned LCDFN(get_foreground)(void)
147 return current_vp->fg_pattern;
150 void LCDFN(set_background)(unsigned brightness)
152 current_vp->bg_pattern = brightness;
153 bg_pattern = patterns[brightness & 3];
156 unsigned LCDFN(get_background)(void)
158 return current_vp->bg_pattern;
161 void LCDFN(set_drawinfo)(int mode, unsigned fg_brightness,
162 unsigned bg_brightness)
164 LCDFN(set_drawmode)(mode);
165 LCDFN(set_foreground)(fg_brightness);
166 LCDFN(set_background)(bg_brightness);
169 int LCDFN(getwidth)(void)
171 return current_vp->width;
174 int LCDFN(getheight)(void)
176 return current_vp->height;
179 void LCDFN(setmargins)(int x, int y)
181 current_vp->xmargin = x;
182 current_vp->ymargin = y;
185 int LCDFN(getxmargin)(void)
187 return current_vp->xmargin;
190 int LCDFN(getymargin)(void)
192 return current_vp->ymargin;
195 void LCDFN(setfont)(int newfont)
197 current_vp->font = newfont;
200 int LCDFN(getfont)(void)
202 return current_vp->font;
205 int LCDFN(getstringsize)(const unsigned char *str, int *w, int *h)
207 return font_getstringsize(str, w, h, current_vp->font);
210 /*** low-level drawing functions ***/
212 static void setpixel(int x, int y)
214 unsigned mask = 0x0101 << (y & 7);
215 FBFN(data) *address = &LCDFN(framebuffer)[y>>3][x];
216 unsigned data = *address;
218 *address = data ^ ((data ^ fg_pattern) & mask);
221 static void clearpixel(int x, int y)
223 unsigned mask = 0x0101 << (y & 7);
224 FBFN(data) *address = &LCDFN(framebuffer)[y>>3][x];
225 unsigned data = *address;
227 *address = data ^ ((data ^ bg_pattern) & mask);
230 static void clearimgpixel(int x, int y)
232 unsigned mask = 0x0101 << (y & 7);
233 FBFN(data) *address = &LCDFN(framebuffer)[y>>3][x];
234 unsigned data = *address;
236 *address = data ^ ((data ^ *(FBFN(data) *)((long)address
237 + backdrop_offset)) & mask);
240 static void flippixel(int x, int y)
242 unsigned mask = 0x0101 << (y & 7);
243 FBFN(data) *address = &LCDFN(framebuffer)[y>>3][x];
245 *address ^= mask;
248 static void nopixel(int x, int y)
250 (void)x;
251 (void)y;
254 LCDFN(pixelfunc_type)* const LCDFN(pixelfuncs_bgcolor)[8] = {
255 flippixel, nopixel, setpixel, setpixel,
256 nopixel, clearpixel, nopixel, clearpixel
259 LCDFN(pixelfunc_type)* const LCDFN(pixelfuncs_backdrop)[8] = {
260 flippixel, nopixel, setpixel, setpixel,
261 nopixel, clearimgpixel, nopixel, clearimgpixel
264 LCDFN(pixelfunc_type)* const *LCDFN(pixelfuncs) = LCDFN(pixelfuncs_bgcolor);
266 /* 'mask' and 'bits' contain 2 bits per pixel */
267 static void ICODE_ATTR flipblock(FBFN(data) *address, unsigned mask,
268 unsigned bits)
270 *address ^= bits & mask;
273 static void ICODE_ATTR bgblock(FBFN(data) *address, unsigned mask,
274 unsigned bits)
276 unsigned data = *address;
278 *address = data ^ ((data ^ bg_pattern) & mask & ~bits);
281 static void ICODE_ATTR bgimgblock(FBFN(data) *address, unsigned mask,
282 unsigned bits)
284 unsigned data = *address;
286 *address = data ^ ((data ^ *(FBFN(data) *)((long)address
287 + backdrop_offset)) & mask & ~bits);
290 static void ICODE_ATTR fgblock(FBFN(data) *address, unsigned mask,
291 unsigned bits)
293 unsigned data = *address;
295 *address = data ^ ((data ^ fg_pattern) & mask & bits);
298 static void ICODE_ATTR solidblock(FBFN(data) *address, unsigned mask,
299 unsigned bits)
301 unsigned data = *address;
302 unsigned bgp = bg_pattern;
304 bits = bgp ^ ((bgp ^ fg_pattern) & bits);
305 *address = data ^ ((data ^ bits) & mask);
308 static void ICODE_ATTR solidimgblock(FBFN(data) *address, unsigned mask,
309 unsigned bits)
311 unsigned data = *address;
312 unsigned bgp = *(FBFN(data) *)((long)address + backdrop_offset);
314 bits = bgp ^ ((bgp ^ fg_pattern) & bits);
315 *address = data ^ ((data ^ bits) & mask);
318 static void ICODE_ATTR flipinvblock(FBFN(data) *address, unsigned mask,
319 unsigned bits)
321 *address ^= ~bits & mask;
324 static void ICODE_ATTR bginvblock(FBFN(data) *address, unsigned mask,
325 unsigned bits)
327 unsigned data = *address;
329 *address = data ^ ((data ^ bg_pattern) & mask & bits);
332 static void ICODE_ATTR bgimginvblock(FBFN(data) *address, unsigned mask,
333 unsigned bits)
335 unsigned data = *address;
337 *address = data ^ ((data ^ *(FBFN(data) *)((long)address
338 + backdrop_offset)) & mask & bits);
341 static void ICODE_ATTR fginvblock(FBFN(data) *address, unsigned mask,
342 unsigned bits)
344 unsigned data = *address;
346 *address = data ^ ((data ^ fg_pattern) & mask & ~bits);
349 static void ICODE_ATTR solidinvblock(FBFN(data) *address, unsigned mask,
350 unsigned bits)
352 unsigned data = *address;
353 unsigned fgp = fg_pattern;
355 bits = fgp ^ ((fgp ^ bg_pattern) & bits);
356 *address = data ^ ((data ^ bits) & mask);
359 static void ICODE_ATTR solidimginvblock(FBFN(data) *address, unsigned mask,
360 unsigned bits)
362 unsigned data = *address;
363 unsigned fgp = fg_pattern;
365 bits = fgp ^ ((fgp ^ *(FBFN(data) *)((long)address
366 + backdrop_offset)) & bits);
367 *address = data ^ ((data ^ bits) & mask);
370 LCDFN(blockfunc_type)* const LCDFN(blockfuncs_bgcolor)[8] = {
371 flipblock, bgblock, fgblock, solidblock,
372 flipinvblock, bginvblock, fginvblock, solidinvblock
375 LCDFN(blockfunc_type)* const LCDFN(blockfuncs_backdrop)[8] = {
376 flipblock, bgimgblock, fgblock, solidimgblock,
377 flipinvblock, bgimginvblock, fginvblock, solidimginvblock
380 LCDFN(blockfunc_type)* const *LCDFN(blockfuncs) = LCDFN(blockfuncs_bgcolor);
383 void LCDFN(set_backdrop)(FBFN(data) *bd)
385 backdrop = bd;
386 if (bd)
388 backdrop_offset = (long)bd - (long)LCDFN(framebuffer);
389 LCDFN(pixelfuncs) = LCDFN(pixelfuncs_backdrop);
390 LCDFN(blockfuncs) = LCDFN(blockfuncs_backdrop);
392 else
394 backdrop_offset = 0;
395 LCDFN(pixelfuncs) = LCDFN(pixelfuncs_bgcolor);
396 LCDFN(blockfuncs) = LCDFN(blockfuncs_bgcolor);
400 FBFN(data)* LCDFN(get_backdrop)(void)
402 return backdrop;
405 static inline void setblock(FBFN(data) *address, unsigned mask, unsigned bits)
407 unsigned data = *address;
409 bits ^= data;
410 *address = data ^ (bits & mask);
413 /*** drawing functions ***/
415 /* Clear the whole display */
416 void LCDFN(clear_display)(void)
418 if (default_vp.drawmode & DRMODE_INVERSEVID)
420 memset(LCDFN(framebuffer), patterns[default_vp.fg_pattern & 3],
421 sizeof LCDFN(framebuffer));
423 else
425 if (backdrop)
426 memcpy(LCDFN(framebuffer), backdrop, sizeof LCDFN(framebuffer));
427 else
428 memset(LCDFN(framebuffer), patterns[default_vp.bg_pattern & 3],
429 sizeof LCDFN(framebuffer));
432 LCDFN(scroll_info).lines = 0;
435 /* Clear the current viewport */
436 void LCDFN(clear_viewport)(void)
438 int lastmode;
440 if (current_vp == &default_vp)
442 LCDFN(clear_display)();
444 else
446 lastmode = current_vp->drawmode;
448 /* Invert the INVERSEVID bit and set basic mode to SOLID */
449 current_vp->drawmode = (~lastmode & DRMODE_INVERSEVID) |
450 DRMODE_SOLID;
452 LCDFN(fillrect)(0, 0, current_vp->width, current_vp->height);
454 current_vp->drawmode = lastmode;
456 LCDFN(scroll_stop)(current_vp);
460 /* Set a single pixel */
461 void LCDFN(drawpixel)(int x, int y)
463 if (((unsigned)x < (unsigned)current_vp->width) &&
464 ((unsigned)y < (unsigned)current_vp->height))
465 LCDFN(pixelfuncs)[current_vp->drawmode](current_vp->x+x, current_vp->y+y);
468 /* Draw a line */
469 void LCDFN(drawline)(int x1, int y1, int x2, int y2)
471 int numpixels;
472 int i;
473 int deltax, deltay;
474 int d, dinc1, dinc2;
475 int x, xinc1, xinc2;
476 int y, yinc1, yinc2;
477 LCDFN(pixelfunc_type) *pfunc = LCDFN(pixelfuncs)[current_vp->drawmode];
479 deltax = abs(x2 - x1);
480 if (deltax == 0)
482 DEBUGF(LCDNAME "drawline() called for vertical line - optimisation.\n");
483 LCDFN(vline)(x1, y1, y2);
484 return;
486 deltay = abs(y2 - y1);
487 if (deltay == 0)
489 DEBUGF(LCDNAME "drawline() called for horizontal line - optimisation.\n");
490 LCDFN(hline)(x1, x2, y1);
491 return;
493 xinc2 = 1;
494 yinc2 = 1;
496 if (deltax >= deltay)
498 numpixels = deltax;
499 d = 2 * deltay - deltax;
500 dinc1 = deltay * 2;
501 dinc2 = (deltay - deltax) * 2;
502 xinc1 = 1;
503 yinc1 = 0;
505 else
507 numpixels = deltay;
508 d = 2 * deltax - deltay;
509 dinc1 = deltax * 2;
510 dinc2 = (deltax - deltay) * 2;
511 xinc1 = 0;
512 yinc1 = 1;
514 numpixels++; /* include endpoints */
516 if (x1 > x2)
518 xinc1 = -xinc1;
519 xinc2 = -xinc2;
522 if (y1 > y2)
524 yinc1 = -yinc1;
525 yinc2 = -yinc2;
528 x = x1;
529 y = y1;
531 for (i = 0; i < numpixels; i++)
533 if (((unsigned)x < (unsigned)current_vp->width) &&
534 ((unsigned)y < (unsigned)current_vp->height))
535 pfunc(current_vp->x + x, current_vp->y + y);
537 if (d < 0)
539 d += dinc1;
540 x += xinc1;
541 y += yinc1;
543 else
545 d += dinc2;
546 x += xinc2;
547 y += yinc2;
552 /* Draw a horizontal line (optimised) */
553 void LCDFN(hline)(int x1, int x2, int y)
555 int x;
556 int width;
557 FBFN(data) *dst, *dst_end;
558 unsigned mask;
559 LCDFN(blockfunc_type) *bfunc;
561 /* direction flip */
562 if (x2 < x1)
564 x = x1;
565 x1 = x2;
566 x2 = x;
569 /* nothing to draw? */
570 if (((unsigned)y >= (unsigned)current_vp->height) || (x1 >= current_vp->width)
571 || (x2 < 0))
572 return;
574 /* clipping */
575 if (x1 < 0)
576 x1 = 0;
577 if (x2 >= current_vp->width)
578 x2 = current_vp->width-1;
580 width = x2 - x1 + 1;
582 /* adjust x1 and y to viewport */
583 x1 += current_vp->x;
584 y += current_vp->y;
586 bfunc = LCDFN(blockfuncs)[current_vp->drawmode];
587 dst = &LCDFN(framebuffer)[y>>3][x1];
588 mask = 0x0101 << (y & 7);
590 dst_end = dst + width;
592 bfunc(dst++, mask, 0xFFFFu);
593 while (dst < dst_end);
596 /* Draw a vertical line (optimised) */
597 void LCDFN(vline)(int x, int y1, int y2)
599 int ny;
600 FBFN(data) *dst;
601 unsigned mask, mask_bottom;
602 LCDFN(blockfunc_type) *bfunc;
604 /* direction flip */
605 if (y2 < y1)
607 ny = y1;
608 y1 = y2;
609 y2 = ny;
612 /* nothing to draw? */
613 if (((unsigned)x >= (unsigned)current_vp->width) || (y1 >= current_vp->height)
614 || (y2 < 0))
615 return;
617 /* clipping */
618 if (y1 < 0)
619 y1 = 0;
620 if (y2 >= current_vp->height)
621 y2 = current_vp->height-1;
623 /* adjust for viewport */
624 y1 += current_vp->y;
625 y2 += current_vp->y;
626 x += current_vp->x;
628 bfunc = LCDFN(blockfuncs)[current_vp->drawmode];
629 dst = &LCDFN(framebuffer)[y1>>3][x];
630 ny = y2 - (y1 & ~7);
631 mask = (0xFFu << (y1 & 7)) & 0xFFu;
632 mask |= mask << 8;
633 mask_bottom = 0xFFu >> (~ny & 7);
634 mask_bottom |= mask_bottom << 8;
636 for (; ny >= 8; ny -= 8)
638 bfunc(dst, mask, 0xFFFFu);
639 dst += LCDM(WIDTH);
640 mask = 0xFFFFu;
642 mask &= mask_bottom;
643 bfunc(dst, mask, 0xFFFFu);
646 /* Draw a rectangular box */
647 void LCDFN(drawrect)(int x, int y, int width, int height)
649 if ((width <= 0) || (height <= 0))
650 return;
652 int x2 = x + width - 1;
653 int y2 = y + height - 1;
655 LCDFN(vline)(x, y, y2);
656 LCDFN(vline)(x2, y, y2);
657 LCDFN(hline)(x, x2, y);
658 LCDFN(hline)(x, x2, y2);
661 /* Fill a rectangular area */
662 void LCDFN(fillrect)(int x, int y, int width, int height)
664 int ny;
665 FBFN(data) *dst, *dst_end;
666 unsigned mask, mask_bottom;
667 unsigned bits = 0;
668 LCDFN(blockfunc_type) *bfunc;
669 bool fillopt = false;
671 /* nothing to draw? */
672 if ((width <= 0) || (height <= 0) || (x >= current_vp->width)
673 || (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
674 return;
676 /* clipping */
677 if (x < 0)
679 width += x;
680 x = 0;
682 if (y < 0)
684 height += y;
685 y = 0;
687 if (x + width > current_vp->width)
688 width = current_vp->width - x;
689 if (y + height > current_vp->height)
690 height = current_vp->height - y;
692 /* adjust for viewport */
693 x += current_vp->x;
694 y += current_vp->y;
696 if (current_vp->drawmode & DRMODE_INVERSEVID)
698 if ((current_vp->drawmode & DRMODE_BG) && !backdrop)
700 fillopt = true;
701 bits = bg_pattern;
704 else
706 if (current_vp->drawmode & DRMODE_FG)
708 fillopt = true;
709 bits = fg_pattern;
712 bfunc = LCDFN(blockfuncs)[current_vp->drawmode];
713 dst = &LCDFN(framebuffer)[y>>3][x];
714 ny = height - 1 + (y & 7);
715 mask = (0xFFu << (y & 7)) & 0xFFu;
716 mask |= mask << 8;
717 mask_bottom = 0xFFu >> (~ny & 7);
718 mask_bottom |= mask_bottom << 8;
720 for (; ny >= 8; ny -= 8)
722 if (fillopt && (mask == 0xFFFFu))
723 memset16(dst, bits, width);
724 else
726 FBFN(data) *dst_row = dst;
728 dst_end = dst_row + width;
730 bfunc(dst_row++, mask, 0xFFFFu);
731 while (dst_row < dst_end);
734 dst += LCDM(WIDTH);
735 mask = 0xFFFFu;
737 mask &= mask_bottom;
739 if (fillopt && (mask == 0xFFFFu))
740 memset16(dst, bits, width);
741 else
743 dst_end = dst + width;
745 bfunc(dst++, mask, 0xFFFFu);
746 while (dst < dst_end);
750 /* About Rockbox' internal monochrome bitmap format:
752 * A bitmap contains one bit for every pixel that defines if that pixel is
753 * black (1) or white (0). Bits within a byte are arranged vertically, LSB
754 * at top.
755 * The bytes are stored in row-major order, with byte 0 being top left,
756 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
757 * 0..7, the second row defines pixel row 8..15 etc.
759 * This is similar to the internal lcd hw format. */
761 /* Draw a partial monochrome bitmap */
762 void ICODE_ATTR LCDFN(mono_bitmap_part)(const unsigned char *src, int src_x,
763 int src_y, int stride, int x, int y,
764 int width, int height)
766 int shift, ny;
767 FBFN(data) *dst, *dst_end;
768 unsigned data, mask, mask_bottom;
769 LCDFN(blockfunc_type) *bfunc;
771 /* nothing to draw? */
772 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
773 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
774 return;
776 /* clipping */
777 if (x < 0)
779 width += x;
780 src_x -= x;
781 x = 0;
783 if (y < 0)
785 height += y;
786 src_y -= y;
787 y = 0;
789 if (x + width > current_vp->width)
790 width = current_vp->width - x;
791 if (y + height > current_vp->height)
792 height = current_vp->height - y;
794 /* adjust for viewport */
795 x += current_vp->x;
796 y += current_vp->y;
798 src += stride * (src_y >> 3) + src_x; /* move starting point */
799 src_y &= 7;
800 y -= src_y;
801 dst = &LCDFN(framebuffer)[y>>3][x];
802 shift = y & 7;
803 ny = height - 1 + shift + src_y;
805 bfunc = LCDFN(blockfuncs)[current_vp->drawmode];
806 mask = 0xFFu << (shift + src_y);
807 /* not byte-doubled here because shift+src_y can be > 7 */
808 mask_bottom = 0xFFu >> (~ny & 7);
809 mask_bottom |= mask_bottom << 8;
811 if (shift == 0)
813 mask &= 0xFFu;
814 mask |= mask << 8;
816 for (; ny >= 8; ny -= 8)
818 const unsigned char *src_row = src;
819 FBFN(data) *dst_row = dst;
821 dst_end = dst_row + width;
824 data = *src_row++;
825 bfunc(dst_row++, mask, data | (data << 8));
827 while (dst_row < dst_end);
829 src += stride;
830 dst += LCDM(WIDTH);
831 mask = 0xFFFFu;
833 mask &= mask_bottom;
835 dst_end = dst + width;
838 data = *src++;
839 bfunc(dst++, mask, data | (data << 8));
841 while (dst < dst_end);
843 else
845 unsigned ddata;
847 dst_end = dst + width;
850 const unsigned char *src_col = src++;
851 FBFN(data) *dst_col = dst++;
852 unsigned mask_col = mask & 0xFFu;
854 mask_col |= mask_col << 8;
855 data = 0;
857 for (y = ny; y >= 8; y -= 8)
859 data |= *src_col << shift;
861 if (mask_col)
863 ddata = data & 0xFFu;
864 bfunc(dst_col, mask_col, ddata | (ddata << 8));
865 mask_col = 0xFFFFu;
867 else
869 mask_col = (mask >> 8) & 0xFFu;
870 mask_col |= mask_col << 8;
873 src_col += stride;
874 dst_col += LCDM(WIDTH);
875 data >>= 8;
877 data |= *src_col << shift;
878 mask_col &= mask_bottom;
879 ddata = data & 0xFFu;
880 bfunc(dst_col, mask_col, ddata | (ddata << 8));
882 while (dst < dst_end);
886 /* Draw a full monochrome bitmap */
887 void LCDFN(mono_bitmap)(const unsigned char *src, int x, int y, int width,
888 int height)
890 LCDFN(mono_bitmap_part)(src, 0, 0, width, x, y, width, height);
893 /* About Rockbox' internal native bitmap format:
895 * A bitmap contains one bit in each byte of a pair of bytes for every pixel.
896 * 00 = white, 01 = light grey, 10 = dark grey, 11 = black. Bits within a byte
897 * are arranged vertically, LSB at top.
898 * The pairs of bytes are stored as shorts, in row-major order, with word 0
899 * being top left, word 1 2nd from left etc. The first row of words defines
900 * pixel rows 0..7, the second row defines pixel row 8..15 etc.
902 * This is the same as the internal lcd hw format. */
904 /* Draw a partial native bitmap */
905 void ICODE_ATTR LCDFN(bitmap_part)(const FBFN(data) *src, int src_x,
906 int src_y, int stride, int x, int y,
907 int width, int height)
909 int shift, ny;
910 FBFN(data) *dst, *dst_end;
911 unsigned mask, mask_bottom;
913 /* nothing to draw? */
914 if ((width <= 0) || (height <= 0) || (x >= current_vp->width)
915 || (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
916 return;
918 /* clipping */
919 if (x < 0)
921 width += x;
922 src_x -= x;
923 x = 0;
925 if (y < 0)
927 height += y;
928 src_y -= y;
929 y = 0;
931 if (x + width > current_vp->width)
932 width = current_vp->width - x;
933 if (y + height > current_vp->height)
934 height = current_vp->height - y;
936 /* adjust for viewport */
937 x += current_vp->x;
938 y += current_vp->y;
940 src += stride * (src_y >> 3) + src_x; /* move starting point */
941 src_y &= 7;
942 y -= src_y;
943 dst = &LCDFN(framebuffer)[y>>3][x];
944 shift = y & 7;
945 ny = height - 1 + shift + src_y;
947 mask = 0xFFu << (shift + src_y);
948 /* not byte-doubled here because shift+src_y can be > 7 */
949 mask_bottom = 0xFFu >> (~ny & 7);
950 mask_bottom |= mask_bottom << 8;
952 if (shift == 0)
954 mask &= 0xFFu;
955 mask |= mask << 8;
957 for (; ny >= 8; ny -= 8)
959 if (mask == 0xFFFFu)
960 memcpy(dst, src, width * sizeof(FBFN(data)));
961 else
963 const FBFN(data) *src_row = src;
964 FBFN(data) *dst_row = dst;
966 dst_end = dst_row + width;
968 setblock(dst_row++, mask, *src_row++);
969 while (dst_row < dst_end);
971 src += stride;
972 dst += LCDM(WIDTH);
973 mask = 0xFFFFu;
975 mask &= mask_bottom;
977 if (mask == 0xFFFFu)
978 memcpy(dst, src, width * sizeof(FBFN(data)));
979 else
981 dst_end = dst + width;
983 setblock(dst++, mask, *src++);
984 while (dst < dst_end);
987 else
989 unsigned datamask = (0xFFu << shift) & 0xFFu;
991 datamask |= datamask << 8;
993 dst_end = dst + width;
996 const FBFN(data) *src_col = src++;
997 FBFN(data) *dst_col = dst++;
998 unsigned mask_col = mask & 0xFFu;
999 unsigned data, olddata = 0;
1001 mask_col |= mask_col << 8;
1003 for (y = ny; y >= 8; y -= 8)
1005 data = *src_col << shift;
1007 if (mask_col)
1009 setblock(dst_col, mask_col,
1010 olddata ^((olddata ^ data) & datamask));
1011 mask_col = 0xFFFFu;
1013 else
1015 mask_col = (mask >> 8) & 0xFFu;
1016 mask_col |= mask_col << 8;
1018 src_col += stride;
1019 dst_col += LCDM(WIDTH);
1020 olddata = data >> 8;
1022 data = *src_col << shift;
1023 setblock(dst_col, mask_col & mask_bottom,
1024 olddata ^((olddata ^ data) & datamask));
1026 while (dst < dst_end);
1030 /* Draw a full native bitmap */
1031 void LCDFN(bitmap)(const FBFN(data) *src, int x, int y, int width, int height)
1033 LCDFN(bitmap_part)(src, 0, 0, width, x, y, width, height);
1036 /* put a string at a given pixel position, skipping first ofs pixel columns */
1037 static void LCDFN(putsxyofs)(int x, int y, int ofs, const unsigned char *str)
1039 unsigned short ch;
1040 unsigned short *ucs;
1041 struct font* pf = font_get(current_vp->font);
1043 ucs = bidi_l2v(str, 1);
1045 while ((ch = *ucs++) != 0 && x < current_vp->width)
1047 int width;
1048 const unsigned char *bits;
1050 /* get proportional width and glyph bits */
1051 width = font_get_width(pf, ch);
1053 if (ofs > width)
1055 ofs -= width;
1056 continue;
1059 bits = font_get_bits(pf, ch);
1061 LCDFN(mono_bitmap_part)(bits, ofs, 0, width, x, y, width - ofs,
1062 pf->height);
1064 x += width - ofs;
1065 ofs = 0;
1069 /* put a string at a given pixel position */
1070 void LCDFN(putsxy)(int x, int y, const unsigned char *str)
1072 LCDFN(putsxyofs)(x, y, 0, str);
1075 /*** line oriented text output ***/
1077 /* put a string at a given char position */
1078 void LCDFN(puts)(int x, int y, const unsigned char *str)
1080 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, 0);
1083 void LCDFN(puts_style)(int x, int y, const unsigned char *str, int style)
1085 LCDFN(puts_style_offset)(x, y, str, style, 0);
1088 void LCDFN(puts_offset)(int x, int y, const unsigned char *str, int offset)
1090 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, offset);
1093 /* put a string at a given char position, style, and pixel position,
1094 * skipping first offset pixel columns */
1095 void LCDFN(puts_style_offset)(int x, int y, const unsigned char *str,
1096 int style, int offset)
1098 int xpos,ypos,w,h,xrect;
1099 int lastmode = current_vp->drawmode;
1101 /* make sure scrolling is turned off on the line we are updating */
1102 LCDFN(scroll_stop_line)(current_vp, y);
1104 if(!str || !str[0])
1105 return;
1107 LCDFN(getstringsize)(str, &w, &h);
1108 xpos = current_vp->xmargin + x*w / utf8length((char *)str);
1109 ypos = current_vp->ymargin + y*h;
1110 current_vp->drawmode = (style & STYLE_INVERT) ?
1111 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
1112 LCDFN(putsxyofs)(xpos, ypos, offset, str);
1113 current_vp->drawmode ^= DRMODE_INVERSEVID;
1114 xrect = xpos + MAX(w - offset, 0);
1115 LCDFN(fillrect)(xrect, ypos, current_vp->width - xrect, h);
1116 current_vp->drawmode = lastmode;
1119 /*** scrolling ***/
1120 void LCDFN(puts_scroll)(int x, int y, const unsigned char *string)
1122 LCDFN(puts_scroll_style)(x, y, string, STYLE_DEFAULT);
1125 void LCDFN(puts_scroll_style)(int x, int y, const unsigned char *string, int style)
1127 LCDFN(puts_scroll_style_offset)(x, y, string, style, 0);
1130 void LCDFN(puts_scroll_offset)(int x, int y, const unsigned char *string, int offset)
1132 LCDFN(puts_scroll_style_offset)(x, y, string, STYLE_DEFAULT, offset);
1135 void LCDFN(puts_scroll_style_offset)(int x, int y, const unsigned char *string,
1136 int style, int offset)
1138 struct scrollinfo* s;
1139 int w, h;
1141 if ((unsigned)y >= (unsigned)current_vp->height)
1142 return;
1144 /* remove any previously scrolling line at the same location */
1145 LCDFN(scroll_stop_line)(current_vp, y);
1147 if (LCDFN(scroll_info).lines >= LCDM(SCROLLABLE_LINES)) return;
1149 s = &LCDFN(scroll_info).scroll[LCDFN(scroll_info).lines];
1151 s->start_tick = current_tick + LCDFN(scroll_info).delay;
1152 s->style = style;
1153 if (style & STYLE_INVERT) {
1154 LCDFN(puts_style_offset)(x,y,string,STYLE_INVERT,offset);
1156 else
1157 LCDFN(puts_offset)(x,y,string,offset);
1159 LCDFN(getstringsize)(string, &w, &h);
1161 if (current_vp->width - x * 8 - current_vp->xmargin < w) {
1162 /* prepare scroll line */
1163 char *end;
1165 memset(s->line, 0, sizeof s->line);
1166 strcpy(s->line, string);
1168 /* get width */
1169 s->width = LCDFN(getstringsize)(s->line, &w, &h);
1171 /* scroll bidirectional or forward only depending on the string
1172 width */
1173 if ( LCDFN(scroll_info).bidir_limit ) {
1174 s->bidir = s->width < (current_vp->width - current_vp->xmargin) *
1175 (100 + LCDFN(scroll_info).bidir_limit) / 100;
1177 else
1178 s->bidir = false;
1180 if (!s->bidir) { /* add spaces if scrolling in the round */
1181 strcat(s->line, " ");
1182 /* get new width incl. spaces */
1183 s->width = LCDFN(getstringsize)(s->line, &w, &h);
1186 end = strchr(s->line, '\0');
1187 strncpy(end, (char *)string, current_vp->width/2);
1189 s->vp = current_vp;
1190 s->y = y;
1191 s->len = utf8length((char *)string);
1192 s->offset = offset;
1193 s->startx = current_vp->xmargin + x * s->width / s->len;
1194 s->backward = false;
1196 LCDFN(scroll_info).lines++;
1200 void LCDFN(scroll_fn)(void)
1202 struct font* pf;
1203 struct scrollinfo* s;
1204 int index;
1205 int xpos, ypos;
1206 int lastmode;
1207 struct viewport* old_vp = current_vp;
1209 for ( index = 0; index < LCDFN(scroll_info).lines; index++ ) {
1210 s = &LCDFN(scroll_info).scroll[index];
1212 /* check pause */
1213 if (TIME_BEFORE(current_tick, s->start_tick))
1214 continue;
1216 LCDFN(set_viewport)(s->vp);
1218 if (s->backward)
1219 s->offset -= LCDFN(scroll_info).step;
1220 else
1221 s->offset += LCDFN(scroll_info).step;
1223 pf = font_get(current_vp->font);
1224 xpos = s->startx;
1225 ypos = current_vp->ymargin + s->y * pf->height;
1227 if (s->bidir) { /* scroll bidirectional */
1228 if (s->offset <= 0) {
1229 /* at beginning of line */
1230 s->offset = 0;
1231 s->backward = false;
1232 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
1234 if (s->offset >= s->width - (current_vp->width - xpos)) {
1235 /* at end of line */
1236 s->offset = s->width - (current_vp->width - xpos);
1237 s->backward = true;
1238 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
1241 else {
1242 /* scroll forward the whole time */
1243 if (s->offset >= s->width)
1244 s->offset %= s->width;
1247 lastmode = current_vp->drawmode;
1248 current_vp->drawmode = (s->style&STYLE_INVERT) ?
1249 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
1250 LCDFN(putsxyofs)(xpos, ypos, s->offset, s->line);
1251 current_vp->drawmode = lastmode;
1252 LCDFN(update_viewport_rect)(xpos, ypos,
1253 current_vp->width - xpos, pf->height);
1256 LCDFN(set_viewport)(old_vp);