a bit of code cleanup.. use a single function to get the statusbar height (or lack...
[Rockbox.git] / firmware / drivers / lcd-2bit-vert.c
blobd43bf6cc81f2df13f46b350dbd60471b8e4f087d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
21 #include "system.h"
22 #include "cpu.h"
23 #include "kernel.h"
24 #include "lcd.h"
25 #include "thread.h"
26 #include <string.h>
27 #include <stdlib.h>
28 #include "file.h"
29 #include "debug.h"
30 #include "font.h"
31 #include "rbunicode.h"
32 #include "bidi.h"
33 #include "scroll_engine.h"
35 /*** globals ***/
37 fb_data lcd_framebuffer[LCD_FBHEIGHT][LCD_FBWIDTH] IBSS_ATTR;
39 const unsigned char lcd_dibits[16] ICONST_ATTR = {
40 0x00, 0x03, 0x0C, 0x0F, 0x30, 0x33, 0x3C, 0x3F,
41 0xC0, 0xC3, 0xCC, 0xCF, 0xF0, 0xF3, 0xFC, 0xFF
44 static const unsigned char pixmask[4] ICONST_ATTR = {
45 0x03, 0x0C, 0x30, 0xC0
48 static fb_data* lcd_backdrop = NULL;
49 static long lcd_backdrop_offset IDATA_ATTR = 0;
51 static struct viewport default_vp =
53 .x = 0,
54 .y = 0,
55 .width = LCD_WIDTH,
56 .height = LCD_HEIGHT,
57 .font = FONT_SYSFIXED,
58 .drawmode = DRMODE_SOLID,
59 .xmargin = 0,
60 .ymargin = 0,
61 .fg_pattern = LCD_DEFAULT_FG,
62 .bg_pattern = LCD_DEFAULT_BG
65 static struct viewport* current_vp IBSS_ATTR;
66 static unsigned fg_pattern IBSS_ATTR;
67 static unsigned bg_pattern IBSS_ATTR;
69 /* LCD init */
70 void lcd_init(void)
72 /* Initialise the viewport */
73 lcd_set_viewport(NULL);
75 lcd_clear_display();
76 /* Call device specific init */
77 lcd_init_device();
78 scroll_init();
81 /*** Viewports ***/
83 void lcd_set_viewport(struct viewport* vp)
85 if (vp == NULL)
86 current_vp = &default_vp;
87 else
88 current_vp = vp;
90 fg_pattern = 0x55 * (~current_vp->fg_pattern & 3);
91 bg_pattern = 0x55 * (~current_vp->bg_pattern & 3);
94 void lcd_update_viewport(void)
96 lcd_update_rect(current_vp->x, current_vp->y,
97 current_vp->width, current_vp->height);
100 void lcd_update_viewport_rect(int x, int y, int width, int height)
102 lcd_update_rect(current_vp->x + x, current_vp->y + y, width, height);
106 /*** parameter handling ***/
108 void lcd_set_drawmode(int mode)
110 current_vp->drawmode = mode & (DRMODE_SOLID|DRMODE_INVERSEVID);
113 int lcd_get_drawmode(void)
115 return current_vp->drawmode;
118 void lcd_set_foreground(unsigned brightness)
120 current_vp->fg_pattern = brightness;
121 fg_pattern = 0x55 * (~brightness & 3);
124 unsigned lcd_get_foreground(void)
126 return current_vp->fg_pattern;
129 void lcd_set_background(unsigned brightness)
131 current_vp->bg_pattern = brightness;
132 bg_pattern = 0x55 * (~brightness & 3);
135 unsigned lcd_get_background(void)
137 return current_vp->bg_pattern;
140 void lcd_set_drawinfo(int mode, unsigned fg_brightness, unsigned bg_brightness)
142 lcd_set_drawmode(mode);
143 lcd_set_foreground(fg_brightness);
144 lcd_set_background(bg_brightness);
147 void lcd_setmargins(int x, int y)
149 current_vp->xmargin = x;
150 current_vp->ymargin = y;
153 int lcd_getxmargin(void)
155 return current_vp->xmargin;
158 int lcd_getymargin(void)
160 return current_vp->ymargin;
163 int lcd_getwidth(void)
165 return current_vp->width;
168 int lcd_getheight(void)
170 return current_vp->height;
173 void lcd_setfont(int newfont)
175 current_vp->font = newfont;
178 int lcd_getfont(void)
180 return current_vp->font;
183 int lcd_getstringsize(const unsigned char *str, int *w, int *h)
185 return font_getstringsize(str, w, h, current_vp->font);
188 /*** low-level drawing functions ***/
190 static void setpixel(int x, int y)
192 unsigned mask = pixmask[y & 3];
193 fb_data *address = &lcd_framebuffer[y>>2][x];
194 unsigned data = *address;
196 *address = data ^ ((data ^ fg_pattern) & mask);
199 static void clearpixel(int x, int y)
201 unsigned mask = pixmask[y & 3];
202 fb_data *address = &lcd_framebuffer[y>>2][x];
203 unsigned data = *address;
205 *address = data ^ ((data ^ bg_pattern) & mask);
208 static void clearimgpixel(int x, int y)
210 unsigned mask = pixmask[y & 3];
211 fb_data *address = &lcd_framebuffer[y>>2][x];
212 unsigned data = *address;
214 *address = data ^ ((data ^ *(address + lcd_backdrop_offset)) & mask);
217 static void flippixel(int x, int y)
219 unsigned mask = pixmask[y & 3];
220 fb_data *address = &lcd_framebuffer[y>>2][x];
222 *address ^= mask;
225 static void nopixel(int x, int y)
227 (void)x;
228 (void)y;
231 lcd_pixelfunc_type* const lcd_pixelfuncs_bgcolor[8] = {
232 flippixel, nopixel, setpixel, setpixel,
233 nopixel, clearpixel, nopixel, clearpixel
236 lcd_pixelfunc_type* const lcd_pixelfuncs_backdrop[8] = {
237 flippixel, nopixel, setpixel, setpixel,
238 nopixel, clearimgpixel, nopixel, clearimgpixel
242 lcd_pixelfunc_type* const * lcd_pixelfuncs = lcd_pixelfuncs_bgcolor;
244 /* 'mask' and 'bits' contain 2 bits per pixel */
245 static void ICODE_ATTR flipblock(fb_data *address, unsigned mask,
246 unsigned bits)
248 *address ^= bits & mask;
251 static void ICODE_ATTR bgblock(fb_data *address, unsigned mask,
252 unsigned bits)
254 unsigned data = *address;
256 *address = data ^ ((data ^ bg_pattern) & mask & ~bits);
259 static void ICODE_ATTR bgimgblock(fb_data *address, unsigned mask,
260 unsigned bits)
262 unsigned data = *address;
264 *address = data ^ ((data ^ *(address + lcd_backdrop_offset)) & mask & ~bits);
267 static void ICODE_ATTR fgblock(fb_data *address, unsigned mask,
268 unsigned bits)
270 unsigned data = *address;
272 *address = data ^ ((data ^ fg_pattern) & mask & bits);
275 static void ICODE_ATTR solidblock(fb_data *address, unsigned mask,
276 unsigned bits)
278 unsigned data = *address;
279 unsigned bgp = bg_pattern;
281 bits = bgp ^ ((bgp ^ fg_pattern) & bits);
282 *address = data ^ ((data ^ bits) & mask);
285 static void ICODE_ATTR solidimgblock(fb_data *address, unsigned mask,
286 unsigned bits)
288 unsigned data = *address;
289 unsigned bgp = *(address + lcd_backdrop_offset);
291 bits = bgp ^ ((bgp ^ fg_pattern) & bits);
292 *address = data ^ ((data ^ bits) & mask);
295 static void ICODE_ATTR flipinvblock(fb_data *address, unsigned mask,
296 unsigned bits)
298 *address ^= ~bits & mask;
301 static void ICODE_ATTR bginvblock(fb_data *address, unsigned mask,
302 unsigned bits)
304 unsigned data = *address;
306 *address = data ^ ((data ^ bg_pattern) & mask & bits);
309 static void ICODE_ATTR bgimginvblock(fb_data *address, unsigned mask,
310 unsigned bits)
312 unsigned data = *address;
314 *address = data ^ ((data ^ *(address + lcd_backdrop_offset)) & mask & bits);
317 static void ICODE_ATTR fginvblock(fb_data *address, unsigned mask,
318 unsigned bits)
320 unsigned data = *address;
322 *address = data ^ ((data ^ fg_pattern) & mask & ~bits);
325 static void ICODE_ATTR solidinvblock(fb_data *address, unsigned mask,
326 unsigned bits)
328 unsigned data = *address;
329 unsigned fgp = fg_pattern;
331 bits = fgp ^ ((fgp ^ bg_pattern) & bits);
332 *address = data ^ ((data ^ bits) & mask);
335 static void ICODE_ATTR solidimginvblock(fb_data *address, unsigned mask,
336 unsigned bits)
338 unsigned data = *address;
339 unsigned fgp = fg_pattern;
341 bits = fgp ^ ((fgp ^ *(address + lcd_backdrop_offset)) & bits);
342 *address = data ^ ((data ^ bits) & mask);
345 lcd_blockfunc_type* const lcd_blockfuncs_bgcolor[8] = {
346 flipblock, bgblock, fgblock, solidblock,
347 flipinvblock, bginvblock, fginvblock, solidinvblock
350 lcd_blockfunc_type* const lcd_blockfuncs_backdrop[8] = {
351 flipblock, bgimgblock, fgblock, solidimgblock,
352 flipinvblock, bgimginvblock, fginvblock, solidimginvblock
355 lcd_blockfunc_type* const * lcd_blockfuncs = lcd_blockfuncs_bgcolor;
358 void lcd_set_backdrop(fb_data* backdrop)
360 lcd_backdrop = backdrop;
361 if (backdrop)
363 lcd_backdrop_offset = (long)backdrop - (long)lcd_framebuffer;
364 lcd_pixelfuncs = lcd_pixelfuncs_backdrop;
365 lcd_blockfuncs = lcd_blockfuncs_backdrop;
367 else
369 lcd_backdrop_offset = 0;
370 lcd_pixelfuncs = lcd_pixelfuncs_bgcolor;
371 lcd_blockfuncs = lcd_blockfuncs_bgcolor;
375 fb_data* lcd_get_backdrop(void)
377 return lcd_backdrop;
381 static inline void setblock(fb_data *address, unsigned mask, unsigned bits)
383 unsigned data = *address;
385 bits ^= data;
386 *address = data ^ (bits & mask);
389 /*** drawing functions ***/
391 /* Clear the whole display */
392 void lcd_clear_display(void)
394 if (current_vp->drawmode & DRMODE_INVERSEVID)
396 memset(lcd_framebuffer, fg_pattern, sizeof lcd_framebuffer);
398 else
400 if (lcd_backdrop)
401 memcpy(lcd_framebuffer, lcd_backdrop, sizeof lcd_framebuffer);
402 else
403 memset(lcd_framebuffer, bg_pattern, sizeof lcd_framebuffer);
406 lcd_scroll_info.lines = 0;
409 /* Clear the current viewport */
410 void lcd_clear_viewport(void)
412 int lastmode;
414 if (current_vp == &default_vp)
416 lcd_clear_display();
418 else
420 lastmode = current_vp->drawmode;
422 /* Invert the INVERSEVID bit and set basic mode to SOLID */
423 current_vp->drawmode = (~lastmode & DRMODE_INVERSEVID) |
424 DRMODE_SOLID;
426 lcd_fillrect(0, 0, current_vp->width, current_vp->height);
428 current_vp->drawmode = lastmode;
430 lcd_scroll_stop(current_vp);
434 /* Set a single pixel */
435 void lcd_drawpixel(int x, int y)
437 if (((unsigned)x < (unsigned)current_vp->width) &&
438 ((unsigned)y < (unsigned)current_vp->height))
439 lcd_pixelfuncs[current_vp->drawmode](current_vp->x + x, current_vp->y + y);
442 /* Draw a line */
443 void lcd_drawline(int x1, int y1, int x2, int y2)
445 int numpixels;
446 int i;
447 int deltax, deltay;
448 int d, dinc1, dinc2;
449 int x, xinc1, xinc2;
450 int y, yinc1, yinc2;
451 lcd_pixelfunc_type *pfunc = lcd_pixelfuncs[current_vp->drawmode];
453 deltax = abs(x2 - x1);
454 if (deltax == 0)
456 DEBUGF("lcd_drawline() called for vertical line - optimisation.\n");
457 lcd_vline(x1, y1, y2);
458 return;
460 deltay = abs(y2 - y1);
461 if (deltay == 0)
463 DEBUGF("lcd_drawline() called for horizontal line - optimisation.\n");
464 lcd_hline(x1, x2, y1);
465 return;
467 xinc2 = 1;
468 yinc2 = 1;
470 if (deltax >= deltay)
472 numpixels = deltax;
473 d = 2 * deltay - deltax;
474 dinc1 = deltay * 2;
475 dinc2 = (deltay - deltax) * 2;
476 xinc1 = 1;
477 yinc1 = 0;
479 else
481 numpixels = deltay;
482 d = 2 * deltax - deltay;
483 dinc1 = deltax * 2;
484 dinc2 = (deltax - deltay) * 2;
485 xinc1 = 0;
486 yinc1 = 1;
488 numpixels++; /* include endpoints */
490 if (x1 > x2)
492 xinc1 = -xinc1;
493 xinc2 = -xinc2;
496 if (y1 > y2)
498 yinc1 = -yinc1;
499 yinc2 = -yinc2;
502 x = x1;
503 y = y1;
505 for (i = 0; i < numpixels; i++)
507 if (((unsigned)x < (unsigned)current_vp->width) &&
508 ((unsigned)y < (unsigned)current_vp->height))
509 pfunc(current_vp->x + x, current_vp->y + y);
511 if (d < 0)
513 d += dinc1;
514 x += xinc1;
515 y += yinc1;
517 else
519 d += dinc2;
520 x += xinc2;
521 y += yinc2;
526 /* Draw a horizontal line (optimised) */
527 void lcd_hline(int x1, int x2, int y)
529 int x;
530 int width;
531 fb_data *dst, *dst_end;
532 unsigned mask;
533 lcd_blockfunc_type *bfunc;
535 /* direction flip */
536 if (x2 < x1)
538 x = x1;
539 x1 = x2;
540 x2 = x;
543 /* nothing to draw? */
544 if (((unsigned)y >= (unsigned)current_vp->height) || (x1 >= current_vp->width)
545 || (x2 < 0))
546 return;
548 /* clipping */
549 if (x1 < 0)
550 x1 = 0;
551 if (x2 >= current_vp->width)
552 x2 = current_vp->width-1;
554 width = x2 - x1 + 1;
556 /* adjust x1 and y to viewport */
557 x1 += current_vp->x;
558 y += current_vp->y;
560 bfunc = lcd_blockfuncs[current_vp->drawmode];
561 dst = &lcd_framebuffer[y>>2][x1];
562 mask = pixmask[y & 3];
564 dst_end = dst + width;
566 bfunc(dst++, mask, 0xFFu);
567 while (dst < dst_end);
570 /* Draw a vertical line (optimised) */
571 void lcd_vline(int x, int y1, int y2)
573 int ny;
574 fb_data *dst;
575 unsigned mask, mask_bottom;
576 lcd_blockfunc_type *bfunc;
578 /* direction flip */
579 if (y2 < y1)
581 ny = y1;
582 y1 = y2;
583 y2 = ny;
586 /* nothing to draw? */
587 if (((unsigned)x >= (unsigned)current_vp->width) || (y1 >= current_vp->height)
588 || (y2 < 0))
589 return;
591 /* clipping */
592 if (y1 < 0)
593 y1 = 0;
594 if (y2 >= current_vp->height)
595 y2 = current_vp->height-1;
597 /* adjust for viewport */
598 y1 += current_vp->y;
599 y2 += current_vp->y;
600 x += current_vp->x;
602 bfunc = lcd_blockfuncs[current_vp->drawmode];
603 dst = &lcd_framebuffer[y1>>2][x];
604 ny = y2 - (y1 & ~3);
605 mask = 0xFFu << (2 * (y1 & 3));
606 mask_bottom = 0xFFu >> (2 * (~ny & 3));
608 for (; ny >= 4; ny -= 4)
610 bfunc(dst, mask, 0xFFu);
611 dst += LCD_WIDTH;
612 mask = 0xFFu;
614 mask &= mask_bottom;
615 bfunc(dst, mask, 0xFFu);
618 /* Draw a rectangular box */
619 void lcd_drawrect(int x, int y, int width, int height)
621 if ((width <= 0) || (height <= 0))
622 return;
624 int x2 = x + width - 1;
625 int y2 = y + height - 1;
627 lcd_vline(x, y, y2);
628 lcd_vline(x2, y, y2);
629 lcd_hline(x, x2, y);
630 lcd_hline(x, x2, y2);
633 /* Fill a rectangular area */
634 void lcd_fillrect(int x, int y, int width, int height)
636 int ny;
637 fb_data *dst, *dst_end;
638 unsigned mask, mask_bottom;
639 unsigned bits = 0;
640 lcd_blockfunc_type *bfunc;
641 bool fillopt = false;
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 x = 0;
654 if (y < 0)
656 height += y;
657 y = 0;
659 if (x + width > current_vp->width)
660 width = current_vp->width - x;
661 if (y + height > current_vp->height)
662 height = current_vp->height - y;
664 /* adjust for viewport */
665 x += current_vp->x;
666 y += current_vp->y;
668 if (current_vp->drawmode & DRMODE_INVERSEVID)
670 if ((current_vp->drawmode & DRMODE_BG) && !lcd_backdrop)
672 fillopt = true;
673 bits = bg_pattern;
676 else
678 if (current_vp->drawmode & DRMODE_FG)
680 fillopt = true;
681 bits = fg_pattern;
684 bfunc = lcd_blockfuncs[current_vp->drawmode];
685 dst = &lcd_framebuffer[y>>2][x];
686 ny = height - 1 + (y & 3);
687 mask = 0xFFu << (2 * (y & 3));
688 mask_bottom = 0xFFu >> (2 * (~ny & 3));
690 for (; ny >= 4; ny -= 4)
692 if (fillopt && (mask == 0xFFu))
693 memset(dst, bits, width);
694 else
696 fb_data *dst_row = dst;
698 dst_end = dst_row + width;
700 bfunc(dst_row++, mask, 0xFFu);
701 while (dst_row < dst_end);
704 dst += LCD_WIDTH;
705 mask = 0xFFu;
707 mask &= mask_bottom;
709 if (fillopt && (mask == 0xFFu))
710 memset(dst, bits, width);
711 else
713 dst_end = dst + width;
715 bfunc(dst++, mask, 0xFFu);
716 while (dst < dst_end);
720 /* About Rockbox' internal monochrome bitmap format:
722 * A bitmap contains one bit for every pixel that defines if that pixel is
723 * black (1) or white (0). Bits within a byte are arranged vertically, LSB
724 * at top.
725 * The bytes are stored in row-major order, with byte 0 being top left,
726 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
727 * 0..7, the second row defines pixel row 8..15 etc.
729 * This is similar to the internal lcd hw format. */
731 /* Draw a partial monochrome bitmap */
732 void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x,
733 int src_y, int stride, int x, int y,
734 int width, int height)
736 int shift, ny;
737 fb_data *dst, *dst_end;
738 unsigned mask, mask_bottom;
739 lcd_blockfunc_type *bfunc;
741 /* nothing to draw? */
742 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
743 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
744 return;
746 /* clipping */
747 if (x < 0)
749 width += x;
750 src_x -= x;
751 x = 0;
753 if (y < 0)
755 height += y;
756 src_y -= y;
757 y = 0;
759 if (x + width > current_vp->width)
760 width = current_vp->width - x;
761 if (y + height > current_vp->height)
762 height = current_vp->height - y;
764 /* adjust for viewport */
765 x += current_vp->x;
766 y += current_vp->y;
768 src += stride * (src_y >> 3) + src_x; /* move starting point */
769 src_y &= 7;
770 y -= src_y;
771 dst = &lcd_framebuffer[y>>2][x];
772 shift = y & 3;
773 ny = height - 1 + shift + src_y;
775 bfunc = lcd_blockfuncs[current_vp->drawmode];
776 mask = 0xFFu << (shift + src_y);
777 mask_bottom = 0xFFu >> (~ny & 7);
779 if (shift == 0)
781 unsigned dmask1, dmask2, data;
783 for (; ny >= 8; ny -= 8)
785 const unsigned char *src_row = src;
786 fb_data *dst_row = dst + LCD_WIDTH;
788 dmask1 = lcd_dibits[mask&0x0F];
789 dmask2 = lcd_dibits[(mask>>4)&0x0F];
790 dst_end = dst_row + width;
792 if (dmask1 != 0)
796 data = *src_row++;
797 bfunc(dst_row - LCD_WIDTH, dmask1, lcd_dibits[data&0x0F]);
798 bfunc(dst_row++, dmask2, lcd_dibits[(data>>4)&0x0F]);
800 while (dst_row < dst_end);
802 else
805 bfunc(dst_row++, dmask2, lcd_dibits[((*src_row++)>>4)&0x0F]);
806 while (dst_row < dst_end);
808 src += stride;
809 dst += 2*LCD_WIDTH;
810 mask = 0xFFu;
812 mask &= mask_bottom;
813 dmask1 = lcd_dibits[mask&0x0F];
814 dmask2 = lcd_dibits[(mask>>4)&0x0F];
815 dst_end = dst + width;
817 if (dmask1 != 0)
819 if (dmask2 != 0)
823 data = *src++;
824 bfunc(dst, dmask1, lcd_dibits[data&0x0F]);
825 bfunc((dst++) + LCD_WIDTH, dmask2, lcd_dibits[(data>>4)&0x0F]);
827 while (dst < dst_end);
829 else
832 bfunc(dst++, dmask1, lcd_dibits[(*src++)&0x0F]);
833 while (dst < dst_end);
836 else
839 bfunc((dst++) + LCD_WIDTH, dmask2, lcd_dibits[((*src++)>>4)&0x0F]);
840 while (dst < dst_end);
843 else
845 dst_end = dst + width;
848 const unsigned char *src_col = src++;
849 fb_data *dst_col = dst++;
850 unsigned mask_col = mask;
851 unsigned data = 0;
853 for (y = ny; y >= 8; y -= 8)
855 data |= *src_col << shift;
857 if (mask_col & 0xFFu)
859 if (mask_col & 0x0F)
860 bfunc(dst_col, lcd_dibits[mask_col&0x0F], lcd_dibits[data&0x0F]);
861 bfunc(dst_col + LCD_WIDTH, lcd_dibits[(mask_col>>4)&0x0F],
862 lcd_dibits[(data>>4)&0x0F]);
863 mask_col = 0xFFu;
865 else
866 mask_col >>= 8;
868 src_col += stride;
869 dst_col += 2*LCD_WIDTH;
870 data >>= 8;
872 data |= *src_col << shift;
873 mask_bottom &= mask_col;
874 if (mask_bottom & 0x0F)
875 bfunc(dst_col, lcd_dibits[mask_bottom&0x0F], lcd_dibits[data&0x0F]);
876 if (mask_bottom & 0xF0)
877 bfunc(dst_col + LCD_WIDTH, lcd_dibits[(mask_bottom&0xF0)>>4],
878 lcd_dibits[(data>>4)&0x0F]);
880 while (dst < dst_end);
884 /* Draw a full monochrome bitmap */
885 void lcd_mono_bitmap(const unsigned char *src, int x, int y, int width, int height)
887 lcd_mono_bitmap_part(src, 0, 0, width, x, y, width, height);
890 /* About Rockbox' internal native bitmap format:
892 * A bitmap contains two bits for every pixel. 00 = white, 01 = light grey,
893 * 10 = dark grey, 11 = black. Bits within a byte are arranged vertically, LSB
894 * at top.
895 * The bytes are stored in row-major order, with byte 0 being top left,
896 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
897 * 0..3, the second row defines pixel row 4..7 etc.
899 * This is the same as the internal lcd hw format. */
901 /* Draw a partial native bitmap */
902 void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y,
903 int stride, int x, int y, int width,
904 int height)
906 int shift, ny;
907 fb_data *dst, *dst_end;
908 unsigned mask, mask_bottom;
910 /* nothing to draw? */
911 if ((width <= 0) || (height <= 0) || (x >= current_vp->width)
912 || (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
913 return;
915 /* clipping */
916 if (x < 0)
918 width += x;
919 src_x -= x;
920 x = 0;
922 if (y < 0)
924 height += y;
925 src_y -= y;
926 y = 0;
928 if (x + width > current_vp->width)
929 width = current_vp->width - x;
930 if (y + height > current_vp->height)
931 height = current_vp->height - y;
933 /* adjust for viewport */
934 x += current_vp->x;
935 y += current_vp->y;
937 src += stride * (src_y >> 2) + src_x; /* move starting point */
938 src_y &= 3;
939 y -= src_y;
940 dst = &lcd_framebuffer[y>>2][x];
941 shift = y & 3;
942 ny = height - 1 + shift + src_y;
944 mask = 0xFFu << (2 * (shift + src_y));
945 mask_bottom = 0xFFu >> (2 * (~ny & 3));
947 if (shift == 0)
949 for (; ny >= 4; ny -= 4)
951 if (mask == 0xFFu)
952 memcpy(dst, src, width);
953 else
955 const fb_data *src_row = src;
956 fb_data *dst_row = dst;
958 dst_end = dst_row + width;
960 setblock(dst_row++, mask, *src_row++);
961 while (dst_row < dst_end);
963 src += stride;
964 dst += LCD_WIDTH;
965 mask = 0xFFu;
967 mask &= mask_bottom;
969 if (mask == 0xFFu)
970 memcpy(dst, src, width);
971 else
973 dst_end = dst + width;
975 setblock(dst++, mask, *src++);
976 while (dst < dst_end);
979 else
981 shift *= 2;
982 dst_end = dst + width;
985 const fb_data *src_col = src++;
986 fb_data *dst_col = dst++;
987 unsigned mask_col = mask;
988 unsigned data = 0;
990 for (y = ny; y >= 4; y -= 4)
992 data |= *src_col << shift;
994 if (mask_col & 0xFFu)
996 setblock(dst_col, mask_col, data);
997 mask_col = 0xFFu;
999 else
1000 mask_col >>= 8;
1002 src_col += stride;
1003 dst_col += LCD_WIDTH;
1004 data >>= 8;
1006 data |= *src_col << shift;
1007 setblock(dst_col, mask_col & mask_bottom, data);
1009 while (dst < dst_end);
1013 /* Draw a full native bitmap */
1014 void lcd_bitmap(const fb_data *src, int x, int y, int width, int height)
1016 lcd_bitmap_part(src, 0, 0, width, x, y, width, height);
1019 /* put a string at a given pixel position, skipping first ofs pixel columns */
1020 static void lcd_putsxyofs(int x, int y, int ofs, const unsigned char *str)
1022 unsigned short ch;
1023 unsigned short *ucs;
1024 struct font* pf = font_get(current_vp->font);
1026 ucs = bidi_l2v(str, 1);
1028 while ((ch = *ucs++) != 0 && x < current_vp->width)
1030 int width;
1031 const unsigned char *bits;
1033 /* get proportional width and glyph bits */
1034 width = font_get_width(pf,ch);
1036 if (ofs > width)
1038 ofs -= width;
1039 continue;
1042 bits = font_get_bits(pf, ch);
1044 lcd_mono_bitmap_part(bits, ofs, 0, width, x, y, width - ofs,
1045 pf->height);
1047 x += width - ofs;
1048 ofs = 0;
1052 /* put a string at a given pixel position */
1053 void lcd_putsxy(int x, int y, const unsigned char *str)
1055 lcd_putsxyofs(x, y, 0, str);
1058 /*** line oriented text output ***/
1060 /* put a string at a given char position */
1061 void lcd_puts(int x, int y, const unsigned char *str)
1063 lcd_puts_style_offset(x, y, str, STYLE_DEFAULT, 0);
1066 void lcd_puts_style(int x, int y, const unsigned char *str, int style)
1068 lcd_puts_style_offset(x, y, str, style, 0);
1071 void lcd_puts_offset(int x, int y, const unsigned char *str, int offset)
1073 lcd_puts_style_offset(x, y, str, STYLE_DEFAULT, offset);
1076 /* put a string at a given char position, style, and pixel position,
1077 * skipping first offset pixel columns */
1078 void lcd_puts_style_offset(int x, int y, const unsigned char *str,
1079 int style, int offset)
1081 int xpos,ypos,w,h,xrect;
1082 int lastmode = current_vp->drawmode;
1084 /* make sure scrolling is turned off on the line we are updating */
1085 lcd_scroll_stop_line(current_vp, y);
1087 if(!str || !str[0])
1088 return;
1090 lcd_getstringsize(str, &w, &h);
1091 xpos = current_vp->xmargin + x*w / utf8length((char *)str);
1092 ypos = current_vp->ymargin + y*h;
1093 current_vp->drawmode = (style & STYLE_INVERT) ?
1094 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
1095 lcd_putsxyofs(xpos, ypos, offset, str);
1096 current_vp->drawmode ^= DRMODE_INVERSEVID;
1097 xrect = xpos + MAX(w - offset, 0);
1098 lcd_fillrect(xrect, ypos, current_vp->width - xrect, h);
1099 current_vp->drawmode = lastmode;
1102 /*** scrolling ***/
1104 void lcd_puts_scroll(int x, int y, const unsigned char *string)
1106 lcd_puts_scroll_style(x, y, string, STYLE_DEFAULT);
1109 void lcd_puts_scroll_style(int x, int y, const unsigned char *string, int style)
1111 lcd_puts_scroll_style_offset(x, y, string, style, 0);
1114 void lcd_puts_scroll_offset(int x, int y, const unsigned char *string, int offset)
1116 lcd_puts_scroll_style_offset(x, y, string, STYLE_DEFAULT, offset);
1119 void lcd_puts_scroll_style_offset(int x, int y, const unsigned char *string,
1120 int style, int offset)
1122 struct scrollinfo* s;
1123 int w, h;
1125 if ((unsigned)y >= (unsigned)current_vp->height)
1126 return;
1128 /* remove any previously scrolling line at the same location */
1129 lcd_scroll_stop_line(current_vp, y);
1131 if (lcd_scroll_info.lines >= LCD_SCROLLABLE_LINES) return;
1133 s = &lcd_scroll_info.scroll[lcd_scroll_info.lines];
1135 s->start_tick = current_tick + lcd_scroll_info.delay;
1136 s->style = style;
1137 if (style & STYLE_INVERT) {
1138 lcd_puts_style_offset(x,y,string,STYLE_INVERT,offset);
1140 else
1141 lcd_puts_offset(x,y,string,offset);
1143 lcd_getstringsize(string, &w, &h);
1145 if (current_vp->width - x * 8 - current_vp->xmargin < w) {
1146 /* prepare scroll line */
1147 char *end;
1149 memset(s->line, 0, sizeof s->line);
1150 strcpy(s->line, (char *)string);
1152 /* get width */
1153 s->width = lcd_getstringsize((unsigned char *)s->line, &w, &h);
1155 /* scroll bidirectional or forward only depending on the string
1156 width */
1157 if ( lcd_scroll_info.bidir_limit ) {
1158 s->bidir = s->width < (current_vp->width - current_vp->xmargin) *
1159 (100 + lcd_scroll_info.bidir_limit) / 100;
1161 else
1162 s->bidir = false;
1164 if (!s->bidir) { /* add spaces if scrolling in the round */
1165 strcat(s->line, " ");
1166 /* get new width incl. spaces */
1167 s->width = lcd_getstringsize((unsigned char *)s->line, &w, &h);
1170 end = strchr(s->line, '\0');
1171 strncpy(end, (char *)string, current_vp->width/2);
1173 s->vp = current_vp;
1174 s->y = y;
1175 s->len = utf8length((char *)string);
1176 s->offset = offset;
1177 s->startx = current_vp->xmargin + x * s->width / s->len;
1178 s->backward = false;
1180 lcd_scroll_info.lines++;
1184 void lcd_scroll_fn(void)
1186 struct font* pf;
1187 struct scrollinfo* s;
1188 int index;
1189 int xpos, ypos;
1190 int lastmode;
1191 struct viewport* old_vp = current_vp;
1193 for ( index = 0; index < lcd_scroll_info.lines; index++ ) {
1194 s = &lcd_scroll_info.scroll[index];
1196 /* check pause */
1197 if (TIME_BEFORE(current_tick, s->start_tick))
1198 continue;
1200 lcd_set_viewport(s->vp);
1202 if (s->backward)
1203 s->offset -= lcd_scroll_info.step;
1204 else
1205 s->offset += lcd_scroll_info.step;
1207 pf = font_get(current_vp->font);
1208 xpos = s->startx;
1209 ypos = current_vp->ymargin + s->y * pf->height;
1211 if (s->bidir) { /* scroll bidirectional */
1212 if (s->offset <= 0) {
1213 /* at beginning of line */
1214 s->offset = 0;
1215 s->backward = false;
1216 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
1218 if (s->offset >= s->width - (current_vp->width - xpos)) {
1219 /* at end of line */
1220 s->offset = s->width - (current_vp->width - xpos);
1221 s->backward = true;
1222 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
1225 else {
1226 /* scroll forward the whole time */
1227 if (s->offset >= s->width)
1228 s->offset %= s->width;
1231 lastmode = current_vp->drawmode;
1232 current_vp->drawmode = (s->style&STYLE_INVERT) ?
1233 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
1234 lcd_putsxyofs(xpos, ypos, s->offset, s->line);
1235 current_vp->drawmode = lastmode;
1236 lcd_update_viewport_rect(xpos, ypos,
1237 current_vp->width - xpos, pf->height);
1240 lcd_set_viewport(old_vp);