Add support to buildzip.pl for Lua scripts
[kugel-rb.git] / firmware / drivers / lcd-1bit-vert.c
blob9607f284aa29cb0ddcd279491b2c45e1d57e18c2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
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 "file.h"
30 #include "debug.h"
31 #include "system.h"
32 #include "font.h"
33 #include "rbunicode.h"
34 #include "bidi.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_"
42 #define MAIN_LCD
43 #endif
45 /*** globals ***/
47 FBFN(data) LCDFN(framebuffer)[LCDM(FBHEIGHT)][LCDM(FBWIDTH)] IRAM_LCDFRAMEBUFFER;
49 static struct viewport default_vp =
51 .x = 0,
52 .y = 0,
53 .width = LCDM(WIDTH),
54 .height = LCDM(HEIGHT),
55 .font = FONT_SYSFIXED,
56 .drawmode = DRMODE_SOLID,
59 static struct viewport* current_vp = &default_vp;
61 /*** Viewports ***/
63 void LCDFN(set_viewport)(struct viewport* vp)
65 if (vp == NULL)
66 current_vp = &default_vp;
67 else
68 current_vp = 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);
82 /* LCD init */
83 void LCDFN(init)(void)
85 LCDFN(clear_display)();
86 #ifndef SIMULATOR
87 LCDFN(init_device)();
88 #endif
89 #ifdef MAIN_LCD
90 scroll_init();
91 #endif
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)
150 (void)x;
151 (void)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,
160 unsigned bits)
162 *address ^= bits & mask;
165 static void ICODE_ATTR bgblock(FBFN(data) *address, unsigned mask,
166 unsigned bits)
168 *address &= bits | ~mask;
171 static void ICODE_ATTR fgblock(FBFN(data) *address, unsigned mask,
172 unsigned bits)
174 *address |= bits & mask;
177 static void ICODE_ATTR solidblock(FBFN(data) *address, unsigned mask,
178 unsigned bits)
180 unsigned data = *(char*)address;
182 bits ^= data;
183 *address = data ^ (bits & mask);
186 static void ICODE_ATTR flipinvblock(FBFN(data) *address, unsigned mask,
187 unsigned bits)
189 *address ^= ~bits & mask;
192 static void ICODE_ATTR bginvblock(FBFN(data) *address, unsigned mask,
193 unsigned bits)
195 *address &= ~(bits & mask);
198 static void ICODE_ATTR fginvblock(FBFN(data) *address, unsigned mask,
199 unsigned bits)
201 *address |= ~bits & mask;
204 static void ICODE_ATTR solidinvblock(FBFN(data) *address, unsigned mask,
205 unsigned bits)
207 unsigned data = *(char *)address;
209 bits = ~bits ^ data;
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)
232 int oldmode;
234 if (current_vp == &default_vp)
236 LCDFN(clear_display)();
238 else
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) |
244 DRMODE_SOLID;
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);
262 /* Draw a line */
263 void LCDFN(drawline)(int x1, int y1, int x2, int y2)
265 int numpixels;
266 int i;
267 int deltax, deltay;
268 int d, dinc1, dinc2;
269 int x, xinc1, xinc2;
270 int y, yinc1, yinc2;
271 LCDFN(pixelfunc_type) *pfunc = LCDFN(pixelfuncs)[current_vp->drawmode];
273 deltax = abs(x2 - x1);
274 if (deltax == 0)
276 DEBUGF(LCDNAME "drawline() called for vertical line - optimisation.\n");
277 LCDFN(vline)(x1, y1, y2);
278 return;
280 deltay = abs(y2 - y1);
281 if (deltay == 0)
283 DEBUGF(LCDNAME "drawline() called for horizontal line - optimisation.\n");
284 LCDFN(hline)(x1, x2, y1);
285 return;
287 xinc2 = 1;
288 yinc2 = 1;
290 if (deltax >= deltay)
292 numpixels = deltax;
293 d = 2 * deltay - deltax;
294 dinc1 = deltay * 2;
295 dinc2 = (deltay - deltax) * 2;
296 xinc1 = 1;
297 yinc1 = 0;
299 else
301 numpixels = deltay;
302 d = 2 * deltax - deltay;
303 dinc1 = deltax * 2;
304 dinc2 = (deltax - deltay) * 2;
305 xinc1 = 0;
306 yinc1 = 1;
308 numpixels++; /* include endpoints */
310 if (x1 > x2)
312 xinc1 = -xinc1;
313 xinc2 = -xinc2;
316 if (y1 > y2)
318 yinc1 = -yinc1;
319 yinc2 = -yinc2;
322 x = x1;
323 y = y1;
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);
331 if (d < 0)
333 d += dinc1;
334 x += xinc1;
335 y += yinc1;
337 else
339 d += dinc2;
340 x += xinc2;
341 y += yinc2;
346 /* Draw a horizontal line (optimised) */
347 void LCDFN(hline)(int x1, int x2, int y)
349 int x, width;
350 unsigned char *dst, *dst_end;
351 unsigned mask;
352 LCDFN(blockfunc_type) *bfunc;
354 /* direction flip */
355 if (x2 < x1)
357 x = x1;
358 x1 = x2;
359 x2 = x;
362 /* nothing to draw? */
363 if (((unsigned)y >= (unsigned)current_vp->height) || (x1 >= current_vp->width)
364 || (x2 < 0))
365 return;
367 /* clipping */
368 if (x1 < 0)
369 x1 = 0;
370 if (x2 >= current_vp->width)
371 x2 = current_vp->width-1;
373 width = x2 - x1 + 1;
375 /* adjust to viewport */
376 x1 += current_vp->x;
377 y += current_vp->y;
379 bfunc = LCDFN(blockfuncs)[current_vp->drawmode];
380 dst = &LCDFN(framebuffer)[y>>3][x1];
381 mask = BIT_N(y & 7);
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)
392 int ny;
393 FBFN(data) *dst;
394 unsigned mask, mask_bottom;
395 LCDFN(blockfunc_type) *bfunc;
397 /* direction flip */
398 if (y2 < y1)
400 ny = y1;
401 y1 = y2;
402 y2 = ny;
405 /* nothing to draw? */
406 if (((unsigned)x >= (unsigned)current_vp->width) || (y1 >= current_vp->height)
407 || (y2 < 0))
408 return;
410 /* clipping */
411 if (y1 < 0)
412 y1 = 0;
413 if (y2 >= current_vp->height)
414 y2 = current_vp->height-1;
416 /* adjust for viewport */
417 y1 += current_vp->y;
418 y2 += current_vp->y;
419 x += current_vp->x;
421 bfunc = LCDFN(blockfuncs)[current_vp->drawmode];
422 dst = &LCDFN(framebuffer)[y1>>3][x];
423 ny = y2 - (y1 & ~7);
424 mask = 0xFFu << (y1 & 7);
425 mask_bottom = 0xFFu >> (~ny & 7);
427 for (; ny >= 8; ny -= 8)
429 bfunc(dst, mask, 0xFFu);
430 dst += LCDM(WIDTH);
431 mask = 0xFFu;
433 mask &= mask_bottom;
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))
441 return;
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)
455 int ny;
456 FBFN(data) *dst, *dst_end;
457 unsigned mask, mask_bottom;
458 unsigned bits = 0;
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))
465 return;
467 /* clipping */
468 if (x < 0)
470 width += x;
471 x = 0;
473 if (y < 0)
475 height += y;
476 y = 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 */
484 x += current_vp->x;
485 y += current_vp->y;
487 if (current_vp->drawmode & DRMODE_INVERSEVID)
489 if (current_vp->drawmode & DRMODE_BG)
491 fillopt = true;
494 else
496 if (current_vp->drawmode & DRMODE_FG)
498 fillopt = true;
499 bits = 0xFFu;
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);
512 else
514 FBFN(data) *dst_row = dst;
516 dst_end = dst_row + width;
518 bfunc(dst_row++, mask, 0xFFu);
519 while (dst_row < dst_end);
522 dst += LCDM(WIDTH);
523 mask = 0xFFu;
525 mask &= mask_bottom;
527 if (fillopt && (mask == 0xFFu))
528 memset(dst, bits, width);
529 else
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
542 * at top.
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)
554 int shift, ny;
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))
562 return;
564 /* clipping */
565 if (x < 0)
567 width += x;
568 src_x -= x;
569 x = 0;
571 if (y < 0)
573 height += y;
574 src_y -= y;
575 y = 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 */
583 x += current_vp->x;
584 y += current_vp->y;
586 src += stride * (src_y >> 3) + src_x; /* move starting point */
587 src_y &= 7;
588 y -= src_y;
589 dst = &LCDFN(framebuffer)[y>>3][x];
590 shift = y & 7;
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);
597 if (shift == 0)
599 bool copyopt = (current_vp->drawmode == DRMODE_SOLID);
601 for (; ny >= 8; ny -= 8)
603 if (copyopt && (mask == 0xFFu))
604 memcpy(dst, src, width);
605 else
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);
616 src += stride;
617 dst += LCDM(WIDTH);
618 mask = 0xFFu;
620 mask &= mask_bottom;
622 if (copyopt && (mask == 0xFFu))
623 memcpy(dst, src, width);
624 else
626 dst_end = dst + width;
628 bfunc(dst++, mask, *src++);
629 while (dst < dst_end);
632 else
634 dst_end = dst + width;
637 const unsigned char *src_col = src++;
638 FBFN(data) *dst_col = dst++;
639 unsigned mask_col = mask;
640 unsigned data = 0;
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);
649 mask_col = 0xFFu;
651 else
652 mask_col >>= 8;
654 src_col += stride;
655 dst_col += LCDM(WIDTH);
656 data >>= 8;
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,
667 int height)
669 LCDFN(bitmap_part)(src, 0, 0, width, x, y, width, height);
672 #include "lcd-bitmap-common.c"