From 765ff0130ab58c67e82a2ed17c64c577c6434d57 Mon Sep 17 00:00:00 2001 From: Karl Kurbjun Date: Sat, 7 Nov 2009 18:38:46 +0000 Subject: Add optional viewport clipping, can be enabled with HAVE_VIEWPORT_CLIP. A simulator check is also added to set_viewport that will show an error/note when DEBUGF is enabled. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@23551 a1c6a512-1295-4272-9138-f99709370657 --- firmware/drivers/lcd-2bit-vi.c | 167 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 153 insertions(+), 14 deletions(-) (limited to 'firmware/drivers/lcd-2bit-vi.c') diff --git a/firmware/drivers/lcd-2bit-vi.c b/firmware/drivers/lcd-2bit-vi.c index 9ce952b9e0..5edb588ce0 100644 --- a/firmware/drivers/lcd-2bit-vi.c +++ b/firmware/drivers/lcd-2bit-vi.c @@ -82,6 +82,28 @@ void LCDFN(set_viewport)(struct viewport* vp) fg_pattern = patterns[current_vp->fg_pattern & 3]; bg_pattern = patterns[current_vp->bg_pattern & 3]; + +#if defined(SIMULATOR) + /* Force the viewport to be within bounds. If this happens it should + * be considered an error - the viewport will not draw as it might be + * expected. + */ + if((unsigned) current_vp->x > (unsigned) LCDM(WIDTH) + || (unsigned) current_vp->y > (unsigned) LCDM(HEIGHT) + || current_vp->x + current_vp->width > LCDM(WIDTH) + || current_vp->y + current_vp->height > LCDM(HEIGHT)) + { +#if !defined(HAVE_VIEWPORT_CLIP) + DEBUGF("ERROR: " +#else + DEBUGF("NOTE: " +#endif + "set_viewport out of bounds: x: %d y: %d width: %d height:%d\n", + current_vp->x, current_vp->y, + current_vp->width, current_vp->height); + } + +#endif } void LCDFN(update_viewport)(void) @@ -443,8 +465,13 @@ void LCDFN(clear_viewport)(void) /* Set a single pixel */ void LCDFN(drawpixel)(int x, int y) { - if (((unsigned)x < (unsigned)current_vp->width) && - ((unsigned)y < (unsigned)current_vp->height)) + if ( ((unsigned)x < (unsigned)current_vp->width) + && ((unsigned)y < (unsigned)current_vp->height) +#if defined(HAVE_VIEWPORT_CLIP) + && ((unsigned)x < (unsigned)LCDM(WIDTH)) + && ((unsigned)y < (unsigned)LCDM(HEIGHT)) +#endif + ) LCDFN(pixelfuncs)[current_vp->drawmode](current_vp->x+x, current_vp->y+y); } @@ -513,8 +540,13 @@ void LCDFN(drawline)(int x1, int y1, int x2, int y2) for (i = 0; i < numpixels; i++) { - if (((unsigned)x < (unsigned)current_vp->width) && - ((unsigned)y < (unsigned)current_vp->height)) + if ( ((unsigned)x < (unsigned)current_vp->width) + && ((unsigned)y < (unsigned)current_vp->height) +#if defined(HAVE_VIEWPORT_CLIP) + && ((unsigned)x < (unsigned)LCDM(WIDTH)) + && ((unsigned)y < (unsigned)LCDM(HEIGHT)) +#endif + ) pfunc(current_vp->x + x, current_vp->y + y); if (d < 0) @@ -548,24 +580,40 @@ void LCDFN(hline)(int x1, int x2, int y) x1 = x2; x2 = x; } - + + /******************** In viewport clipping **********************/ /* nothing to draw? */ if (((unsigned)y >= (unsigned)current_vp->height) || (x1 >= current_vp->width) || (x2 < 0)) return; - - /* clipping */ + if (x1 < 0) x1 = 0; if (x2 >= current_vp->width) x2 = current_vp->width-1; - width = x2 - x1 + 1; - /* adjust x1 and y to viewport */ x1 += current_vp->x; y += current_vp->y; +#if defined(HAVE_VIEWPORT_CLIP) + x2 += current_vp->x; + + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if (((unsigned)y >= (unsigned) LCDM(HEIGHT)) || (x1 >= LCDM(WIDTH)) + || (x2 < 0)) + return; + + /* clipping */ + if (x1 < 0) + x1 = 0; + if (x2 >= LCDM(WIDTH)) + x2 = LCDM(WIDTH)-1; +#endif + + width = x2 - x1 + 1; + bfunc = LCDFN(blockfuncs)[current_vp->drawmode]; dst = &LCDFN(framebuffer)[y>>3][x1]; mask = 0x0101 << (y & 7); @@ -592,12 +640,12 @@ void LCDFN(vline)(int x, int y1, int y2) y2 = ny; } + /******************** In viewport clipping **********************/ /* nothing to draw? */ if (((unsigned)x >= (unsigned)current_vp->width) || (y1 >= current_vp->height) || (y2 < 0)) return; - /* clipping */ if (y1 < 0) y1 = 0; if (y2 >= current_vp->height) @@ -607,6 +655,20 @@ void LCDFN(vline)(int x, int y1, int y2) y1 += current_vp->y; y2 += current_vp->y; x += current_vp->x; + +#if defined(HAVE_VIEWPORT_CLIP) + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if (( (unsigned) x >= (unsigned)LCDM(WIDTH)) || (y1 >= LCDM(HEIGHT)) + || (y2 < 0)) + return; + + /* clipping */ + if (y1 < 0) + y1 = 0; + if (y2 >= LCDM(HEIGHT)) + y2 = LCDM(HEIGHT)-1; +#endif bfunc = LCDFN(blockfuncs)[current_vp->drawmode]; dst = &LCDFN(framebuffer)[y1>>3][x]; @@ -651,12 +713,12 @@ void LCDFN(fillrect)(int x, int y, int width, int height) LCDFN(blockfunc_type) *bfunc; bool fillopt = false; + /******************** In viewport clipping **********************/ /* nothing to draw? */ if ((width <= 0) || (height <= 0) || (x >= current_vp->width) || (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0)) return; - - /* clipping */ + if (x < 0) { width += x; @@ -675,6 +737,31 @@ void LCDFN(fillrect)(int x, int y, int width, int height) /* adjust for viewport */ x += current_vp->x; y += current_vp->y; + +#if defined(HAVE_VIEWPORT_CLIP) + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if ((x >= LCDM(WIDTH)) || (y >= LCDM(HEIGHT)) + || (x + width <= 0) || (y + height <= 0)) + return; + + /* clip image in viewport in screen */ + if (x < 0) + { + width += x; + x = 0; + } + if (y < 0) + { + height += y; + y = 0; + } + if (x + width > LCDM(WIDTH)) + width = LCDM(WIDTH) - x; + if (y + height > LCDM(HEIGHT)) + height = LCDM(HEIGHT) - y; +#endif + if (current_vp->drawmode & DRMODE_INVERSEVID) { @@ -751,12 +838,12 @@ void ICODE_ATTR LCDFN(mono_bitmap_part)(const unsigned char *src, int src_x, unsigned data, mask, mask_bottom; LCDFN(blockfunc_type) *bfunc; + /******************** Image in viewport clipping **********************/ /* nothing to draw? */ if ((width <= 0) || (height <= 0) || (x >= current_vp->width) || (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0)) return; - /* clipping */ if (x < 0) { width += x; @@ -777,6 +864,32 @@ void ICODE_ATTR LCDFN(mono_bitmap_part)(const unsigned char *src, int src_x, /* adjust for viewport */ x += current_vp->x; y += current_vp->y; + +#if defined(HAVE_VIEWPORT_CLIP) + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if ((x >= LCDM(WIDTH)) || (y >= LCDM(HEIGHT)) + || (x + width <= 0) || (y + height <= 0)) + return; + + /* clip image in viewport in screen */ + if (x < 0) + { + width += x; + src_x -= x; + x = 0; + } + if (y < 0) + { + height += y; + src_y -= y; + y = 0; + } + if (x + width > LCDM(WIDTH)) + width = LCDM(WIDTH) - x; + if (y + height > LCDM(HEIGHT)) + height = LCDM(HEIGHT) - y; +#endif src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; @@ -893,12 +1006,12 @@ void ICODE_ATTR LCDFN(bitmap_part)(const FBFN(data) *src, int src_x, FBFN(data) *dst, *dst_end; unsigned mask, mask_bottom; + /******************** Image in viewport clipping **********************/ /* nothing to draw? */ if ((width <= 0) || (height <= 0) || (x >= current_vp->width) || (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0)) return; - /* clipping */ if (x < 0) { width += x; @@ -920,6 +1033,32 @@ void ICODE_ATTR LCDFN(bitmap_part)(const FBFN(data) *src, int src_x, x += current_vp->x; y += current_vp->y; +#if defined(HAVE_VIEWPORT_CLIP) + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if ((x >= LCDM(WIDTH)) || (y >= LCDM(HEIGHT)) + || (x + width <= 0) || (y + height <= 0)) + return; + + /* clip image in viewport in screen */ + if (x < 0) + { + width += x; + src_x -= x; + x = 0; + } + if (y < 0) + { + height += y; + src_y -= y; + y = 0; + } + if (x + width > LCDM(WIDTH)) + width = LCDM(WIDTH) - x; + if (y + height > LCDM(HEIGHT)) + height = LCDM(HEIGHT) - y; +#endif + src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; y -= src_y; -- cgit v1.2.3