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-16bit-vert.c | 283 ++++++++++++++++++++++++++++------ firmware/drivers/lcd-16bit.c | 315 ++++++++++++++++++++++++++++++-------- firmware/drivers/lcd-1bit-vert.c | 133 ++++++++++++++-- firmware/drivers/lcd-2bit-horz.c | 165 ++++++++++++++++++-- firmware/drivers/lcd-2bit-vert.c | 165 ++++++++++++++++++-- firmware/drivers/lcd-2bit-vi.c | 167 ++++++++++++++++++-- firmware/drivers/lcd-charcell.c | 27 ++++ firmware/export/config-mrobe500.h | 3 + 8 files changed, 1092 insertions(+), 166 deletions(-) diff --git a/firmware/drivers/lcd-16bit-vert.c b/firmware/drivers/lcd-16bit-vert.c index 1e49bb354d..586feabeca 100644 --- a/firmware/drivers/lcd-16bit-vert.c +++ b/firmware/drivers/lcd-16bit-vert.c @@ -92,6 +92,28 @@ void lcd_set_viewport(struct viewport* vp) current_vp = &default_vp; else current_vp = vp; + +#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) LCD_WIDTH + || (unsigned) current_vp->y > (unsigned) LCD_HEIGHT + || current_vp->x + current_vp->width > LCD_WIDTH + || current_vp->y + current_vp->height > LCD_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 lcd_update_viewport(void) @@ -312,8 +334,13 @@ void lcd_clear_display(void) /* Set a single pixel */ void lcd_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)LCD_WIDTH) + && ((unsigned)y < (unsigned)LCD_HEIGHT) +#endif + ) lcd_fastpixelfuncs[current_vp->drawmode](LCDADDR(current_vp->x+x, current_vp->y+y)); } @@ -382,7 +409,13 @@ void lcd_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)LCD_WIDTH) + && ((unsigned)y < (unsigned)LCD_HEIGHT) +#endif + ) pfunc(LCDADDR(x + current_vp->x, y + current_vp->y)); if (d < 0) @@ -415,19 +448,38 @@ void lcd_hline(int x1, int x2, int y) 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; + + /* Adjust x1 and y to viewport */ + x1 += current_vp->x; + x2 += current_vp->x; + y += current_vp->y; + +#if defined(HAVE_VIEWPORT_CLIP) + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if (((unsigned)y >= (unsigned) LCD_HEIGHT) || (x1 >= LCD_WIDTH) + || (x2 < 0)) + return; + + /* clipping */ + if (x1 < 0) + x1 = 0; + if (x2 >= LCD_WIDTH) + x2 = LCD_WIDTH-1; +#endif - dst = LCDADDR(x1 + current_vp->x, y + current_vp->y); + dst = LCDADDR(x1 , y ); dst_end = dst + (x2 - x1) * LCD_HEIGHT; do @@ -454,12 +506,39 @@ void lcd_vline(int x, int y1, int y2) y2 = y; } + /******************** In viewport clipping **********************/ /* nothing to draw? */ - if (((unsigned)x >= (unsigned)current_vp->width) || + if (((unsigned)x >= (unsigned)current_vp->width) || (y1 >= current_vp->height) || (y2 < 0)) return; + if (y1 < 0) + y1 = 0; + if (y2 >= current_vp->height) + y2 = current_vp->height-1; + + /* adjust for viewport */ + x += current_vp->x; + y1 += current_vp->y; + y2 += current_vp->y; + +#if defined(HAVE_VIEWPORT_CLIP) + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if (( (unsigned) x >= (unsigned)LCD_WIDTH) || (y1 >= LCD_HEIGHT) + || (y2 < 0)) + return; + + /* clipping */ + if (y1 < 0) + y1 = 0; + if (y2 >= LCD_HEIGHT) + y2 = LCD_HEIGHT-1; +#endif + + height = y2 - y1 + 1; + /* drawmode and optimisation */ if (current_vp->drawmode & DRMODE_INVERSEVID) { @@ -485,18 +564,6 @@ void lcd_vline(int x, int y1, int y2) if (fillopt == OPT_NONE && current_vp->drawmode != DRMODE_COMPLEMENT) return; - /* clipping */ - if (y1 < 0) - y1 = 0; - if (y2 >= current_vp->height) - y2 = current_vp->height-1; - - height = y2 - y1 + 1; - - /* Adjust y1 and x to viewport */ - y1 += current_vp->y; - x += current_vp->x; - dst = LCDADDR(x, y1); switch (fillopt) @@ -541,11 +608,55 @@ void lcd_fillrect(int x, int y, int width, int height) enum fill_opt fillopt = OPT_NONE; fb_data *dst, *dst_end; + /******************** 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; + if (x < 0) + { + width += x; + x = 0; + } + if (y < 0) + { + height += y; + y = 0; + } + if (x + width > current_vp->width) + width = current_vp->width - x; + if (y + height > current_vp->height) + height = current_vp->height - y; + + /* 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 >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif + /* drawmode and optimisation */ if (current_vp->drawmode & DRMODE_INVERSEVID) { @@ -571,23 +682,7 @@ void lcd_fillrect(int x, int y, int width, int height) if (fillopt == OPT_NONE && current_vp->drawmode != DRMODE_COMPLEMENT) return; - /* clipping */ - if (x < 0) - { - width += x; - x = 0; - } - if (y < 0) - { - height += y; - y = 0; - } - if (x + width > current_vp->width) - width = current_vp->width - x; - if (y + height > current_vp->height) - height = current_vp->height - y; - - dst = LCDADDR(current_vp->x + x, current_vp->y + y); + dst = LCDADDR(x, y); dst_end = dst + width * LCD_HEIGHT; do @@ -641,12 +736,12 @@ void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x, unsigned dmask = 0x100; /* bit 8 == sentinel */ int drmode = current_vp->drawmode; + /******************** 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; @@ -663,11 +758,41 @@ void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x, width = current_vp->width - x; if (y + height > current_vp->height) height = current_vp->height - y; + + /* 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 >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; src_end = src + width; - dst = LCDADDR(current_vp->x + x, current_vp->y + y); + dst = LCDADDR(x, y); dst_end = dst + height; if (drmode & DRMODE_INVERSEVID) @@ -793,12 +918,12 @@ void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y, { fb_data *dst, *dst_end; + /******************** Image in viewport clipping **********************/ /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= current_vp->width) || + 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; @@ -811,13 +936,44 @@ void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y, src_y -= y; y = 0; } + if (x + width > current_vp->width) width = current_vp->width - x; if (y + height > current_vp->height) - height = current_vp->height - y; + height = current_vp->height - y; + + /* 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 >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif src += stride * src_x + src_y; /* move starting point */ - dst = LCDADDR(current_vp->x + x, current_vp->y + y); + dst = LCDADDR(x, y); dst_end = dst + width * LCD_HEIGHT; do @@ -843,13 +999,13 @@ void ICODE_ATTR lcd_bitmap_transparent_part(const fb_data *src, int src_x, int y, int width, int height) { fb_data *dst, *dst_end; - + + /******************** Image in viewport clipping **********************/ /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= current_vp->width) || + 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; @@ -862,13 +1018,44 @@ void ICODE_ATTR lcd_bitmap_transparent_part(const fb_data *src, int src_x, src_y -= y; y = 0; } + if (x + width > current_vp->width) width = current_vp->width - x; if (y + height > current_vp->height) - height = current_vp->height - y; + height = current_vp->height - y; + + /* 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 >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif src += stride * src_x + src_y; /* move starting point */ - dst = LCDADDR(current_vp->x + x, current_vp->y + y); + dst = LCDADDR(x, y); dst_end = dst + width * LCD_HEIGHT; do diff --git a/firmware/drivers/lcd-16bit.c b/firmware/drivers/lcd-16bit.c index 6d6a3b2104..d8405235f7 100644 --- a/firmware/drivers/lcd-16bit.c +++ b/firmware/drivers/lcd-16bit.c @@ -85,6 +85,28 @@ void lcd_set_viewport(struct viewport* vp) current_vp = &default_vp; else current_vp = vp; + +#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) LCD_WIDTH + || (unsigned) current_vp->y > (unsigned) LCD_HEIGHT + || current_vp->x + current_vp->width > LCD_WIDTH + || current_vp->y + current_vp->height > LCD_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 lcd_update_viewport(void) @@ -305,8 +327,13 @@ void lcd_clear_display(void) /* Set a single pixel */ void lcd_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)LCD_WIDTH) + && ((unsigned)y < (unsigned)LCD_HEIGHT) +#endif + ) lcd_fastpixelfuncs[current_vp->drawmode](LCDADDR(current_vp->x+x, current_vp->y+y)); } @@ -375,7 +402,13 @@ void lcd_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)LCD_WIDTH) + && ((unsigned)y < (unsigned)LCD_HEIGHT) +#endif + ) pfunc(LCDADDR(x + current_vp->x, y + current_vp->y)); if (d < 0) @@ -409,11 +442,38 @@ void lcd_hline(int x1, int x2, int y) x2 = x; } + /******************** In viewport clipping **********************/ /* nothing to draw? */ if (((unsigned)y >= (unsigned)current_vp->height) || (x1 >= current_vp->width) || (x2 < 0)) return; + + if (x1 < 0) + x1 = 0; + if (x2 >= current_vp->width) + x2 = current_vp->width-1; + + /* Adjust x1 and y to viewport */ + x1 += current_vp->x; + x2 += current_vp->x; + y += current_vp->y; + +#if defined(HAVE_VIEWPORT_CLIP) + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if (((unsigned)y >= (unsigned) LCD_HEIGHT) || (x1 >= LCD_WIDTH) + || (x2 < 0)) + return; + + /* clipping */ + if (x1 < 0) + x1 = 0; + if (x2 >= LCD_WIDTH) + x2 = LCD_WIDTH-1; +#endif + + width = x2 - x1 + 1; /* drawmode and optimisation */ if (current_vp->drawmode & DRMODE_INVERSEVID) @@ -440,18 +500,6 @@ void lcd_hline(int x1, int x2, int y) if (fillopt == OPT_NONE && current_vp->drawmode != DRMODE_COMPLEMENT) 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; - dst = LCDADDR(x1, y); switch (fillopt) @@ -489,19 +537,38 @@ void lcd_vline(int x, int y1, int y2) y2 = y; } + /******************** 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) y2 = current_vp->height-1; + + /* adjust for viewport */ + x += current_vp->x; + y1 += current_vp->y; + y2 += current_vp->y; + +#if defined(HAVE_VIEWPORT_CLIP) + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if (( (unsigned) x >= (unsigned)LCD_WIDTH) || (y1 >= LCD_HEIGHT) + || (y2 < 0)) + return; + + /* clipping */ + if (y1 < 0) + y1 = 0; + if (y2 >= LCD_HEIGHT) + y2 = LCD_HEIGHT-1; +#endif - dst = LCDADDR(x + current_vp->x, y1 + current_vp->y); + dst = LCDADDR(x , y1); dst_end = dst + (y2 - y1) * LCD_WIDTH; do @@ -534,11 +601,55 @@ void lcd_fillrect(int x, int y, int width, int height) enum fill_opt fillopt = OPT_NONE; fb_data *dst, *dst_end; + /******************** 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; + if (x < 0) + { + width += x; + x = 0; + } + if (y < 0) + { + height += y; + y = 0; + } + if (x + width > current_vp->width) + width = current_vp->width - x; + if (y + height > current_vp->height) + height = current_vp->height - y; + + /* 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 >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif + /* drawmode and optimisation */ if (current_vp->drawmode & DRMODE_INVERSEVID) { @@ -564,23 +675,7 @@ void lcd_fillrect(int x, int y, int width, int height) if (fillopt == OPT_NONE && current_vp->drawmode != DRMODE_COMPLEMENT) return; - /* clipping */ - if (x < 0) - { - width += x; - x = 0; - } - if (y < 0) - { - height += y; - y = 0; - } - if (x + width > current_vp->width) - width = current_vp->width - x; - if (y + height > current_vp->height) - height = current_vp->height - y; - - dst = LCDADDR(current_vp->x + x, current_vp->y + y); + dst = LCDADDR(x, y); dst_end = dst + height * LCD_WIDTH; do @@ -634,12 +729,12 @@ void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x, unsigned dmask = 0x100; /* bit 8 == sentinel */ int drmode = current_vp->drawmode; + /******************** 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; @@ -656,11 +751,41 @@ void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x, width = current_vp->width - x; if (y + height > current_vp->height) height = current_vp->height - y; + + /* 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 >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; src_end = src + width; - dst = LCDADDR(current_vp->x + x, current_vp->y + y); + dst = LCDADDR(x, y); dst_end = dst + height * LCD_WIDTH; if (drmode & DRMODE_INVERSEVID) @@ -784,34 +909,62 @@ void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y, { fb_data *dst; - if (x + width > current_vp->width) - width = current_vp->width - x; /* Clip right */ - - if (x < 0) /* Clip left */ + /******************** 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; + + if (x < 0) { width += x; src_x -= x; x = 0; } - - if (width <= 0) - return; /* nothing left to do */ - + if (y < 0) + { + height += y; + src_y -= y; + y = 0; + } + + if (x + width > current_vp->width) + width = current_vp->width - x; if (y + height > current_vp->height) - height = current_vp->height - y; /* Clip bottom */ - - if (y < 0) /* Clip top */ + height = current_vp->height - y; + + /* 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 >= LCD_WIDTH) || (y >= LCD_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 (height <= 0) - return; /* nothing left to do */ - + if (x + width > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif + src += stride * src_y + src_x; /* move starting point */ - dst = LCDADDR(current_vp->x + x, current_vp->y + y); + dst = LCDADDR(x, y); do { @@ -836,34 +989,62 @@ void ICODE_ATTR lcd_bitmap_transparent_part(const fb_data *src, int src_x, fb_data *dst; unsigned fg = current_vp->fg_pattern; - if (x + width > current_vp->width) - width = current_vp->width - x; /* Clip right */ - - if (x < 0) /* Clip left */ + /******************** 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; + + if (x < 0) { width += x; src_x -= x; x = 0; } - - if (width <= 0) - return; /* nothing left to do */ - + if (y < 0) + { + height += y; + src_y -= y; + y = 0; + } + + if (x + width > current_vp->width) + width = current_vp->width - x; if (y + height > current_vp->height) - height = current_vp->height - y; /* Clip bottom */ - - if (y < 0) /* Clip top */ + height = current_vp->height - y; + + /* 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 >= LCD_WIDTH) || (y >= LCD_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 (height <= 0) - return; /* nothing left to do */ + if (x + width > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif src += stride * src_y + src_x; /* move starting point */ - dst = LCDADDR(current_vp->x + x, current_vp->y + y); + dst = LCDADDR(x, y); #ifdef CPU_ARM { diff --git a/firmware/drivers/lcd-1bit-vert.c b/firmware/drivers/lcd-1bit-vert.c index 9607f284aa..ae7cddb19a 100644 --- a/firmware/drivers/lcd-1bit-vert.c +++ b/firmware/drivers/lcd-1bit-vert.c @@ -66,6 +66,28 @@ void LCDFN(set_viewport)(struct viewport* vp) current_vp = &default_vp; else current_vp = vp; + +#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) @@ -254,8 +276,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); } @@ -324,8 +351,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) @@ -359,22 +391,38 @@ void LCDFN(hline)(int x1, int x2, int y) 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 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]; @@ -402,12 +450,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) @@ -417,6 +465,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]; @@ -459,12 +521,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; @@ -483,6 +545,30 @@ 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) { @@ -556,12 +642,13 @@ void ICODE_ATTR LCDFN(bitmap_part)(const unsigned char *src, int src_x, unsigned 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 */ + /* clip image in viewport */ if (x < 0) { width += x; @@ -582,6 +669,32 @@ void ICODE_ATTR LCDFN(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; diff --git a/firmware/drivers/lcd-2bit-horz.c b/firmware/drivers/lcd-2bit-horz.c index f9a53ee20c..2e965b35fa 100644 --- a/firmware/drivers/lcd-2bit-horz.c +++ b/firmware/drivers/lcd-2bit-horz.c @@ -87,6 +87,28 @@ void lcd_set_viewport(struct viewport* vp) fg_pattern = 0x55 * (~current_vp->fg_pattern & 3); bg_pattern = 0x55 * (~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) LCD_WIDTH + || (unsigned) current_vp->y > (unsigned) LCD_HEIGHT + || current_vp->x + current_vp->width > LCD_WIDTH + || current_vp->y + current_vp->height > LCD_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 lcd_update_viewport(void) @@ -415,8 +437,13 @@ void lcd_clear_viewport(void) /* Set a single pixel */ void lcd_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)LCD_WIDTH) + && ((unsigned)y < (unsigned)LCD_HEIGHT) +#endif + ) lcd_pixelfuncs[current_vp->drawmode](current_vp->x + x, current_vp->y + y); } @@ -485,8 +512,13 @@ void lcd_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)LCD_WIDTH) + && ((unsigned)y < (unsigned)LCD_HEIGHT) +#endif + ) pfunc(current_vp->x + x, current_vp->y + y); if (d < 0) @@ -520,12 +552,12 @@ void lcd_hline(int x1, int x2, int y) x2 = nx; } + /******************** 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) @@ -535,6 +567,20 @@ void lcd_hline(int x1, int x2, int y) x1 += current_vp->x; x2 += current_vp->x; y += current_vp->y; + +#if defined(HAVE_VIEWPORT_CLIP) + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if (((unsigned)y >= (unsigned) LCD_HEIGHT) || (x1 >= LCD_WIDTH) + || (x2 < 0)) + return; + + /* clipping */ + if (x1 < 0) + x1 = 0; + if (x2 >= LCD_WIDTH) + x2 = LCD_WIDTH-1; +#endif bfunc = lcd_blockfuncs[current_vp->drawmode]; dst = &lcd_framebuffer[y][x1>>2]; @@ -567,12 +613,12 @@ void lcd_vline(int x, int y1, int y2) y2 = y; } + /******************** 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) @@ -582,6 +628,20 @@ void lcd_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)LCD_WIDTH) || (y1 >= LCD_HEIGHT) + || (y2 < 0)) + return; + + /* clipping */ + if (y1 < 0) + y1 = 0; + if (y2 >= LCD_HEIGHT) + y2 = LCD_HEIGHT-1; +#endif bfunc = lcd_blockfuncs[current_vp->drawmode]; dst = &lcd_framebuffer[y1][x>>2]; @@ -619,12 +679,12 @@ void lcd_fillrect(int x, int y, int width, int height) unsigned mask, mask_right; lcd_blockfunc_type *bfunc; + /******************** 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; @@ -643,6 +703,30 @@ void lcd_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 >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif bfunc = lcd_blockfuncs[current_vp->drawmode]; dst = &lcd_framebuffer[y][x>>2]; @@ -696,12 +780,12 @@ void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x, unsigned dst_mask; int drmode = current_vp->drawmode; + /******************** 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; @@ -718,12 +802,41 @@ void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x, width = current_vp->width - x; if (y + height > current_vp->height) height = current_vp->height - y; + + x += current_vp->x; /* adjust for viewport */ + y += current_vp->y; /* adjust for viewport */ + +#if defined(HAVE_VIEWPORT_CLIP) + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if ((x >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; src_end = src + width; - x += current_vp->x; /* adjust for viewport */ - dst = &lcd_framebuffer[current_vp->y + y][x >> 2]; + + dst = &lcd_framebuffer[y][x >> 2]; dst_end = dst + height * LCD_FBWIDTH; dst_mask = pixmask[x & 3]; @@ -879,12 +992,12 @@ void ICODE_ATTR lcd_bitmap_part(const unsigned char *src, int src_x, unsigned char *dst, *dst_end; unsigned mask, mask_right; + /******************** 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; @@ -905,6 +1018,32 @@ void ICODE_ATTR lcd_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 >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif stride = (stride + 3) >> 2; /* convert to no. of bytes */ diff --git a/firmware/drivers/lcd-2bit-vert.c b/firmware/drivers/lcd-2bit-vert.c index a124e3e15d..3a089722f4 100644 --- a/firmware/drivers/lcd-2bit-vert.c +++ b/firmware/drivers/lcd-2bit-vert.c @@ -89,6 +89,28 @@ void lcd_set_viewport(struct viewport* vp) fg_pattern = 0x55 * (~current_vp->fg_pattern & 3); bg_pattern = 0x55 * (~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) LCD_WIDTH + || (unsigned) current_vp->y > (unsigned) LCD_HEIGHT + || current_vp->x + current_vp->width > LCD_WIDTH + || current_vp->y + current_vp->height > LCD_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 lcd_update_viewport(void) @@ -418,8 +440,13 @@ void lcd_clear_viewport(void) /* Set a single pixel */ void lcd_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)LCD_WIDTH) + && ((unsigned)y < (unsigned)LCD_HEIGHT) +#endif + ) lcd_pixelfuncs[current_vp->drawmode](current_vp->x + x, current_vp->y + y); } @@ -488,8 +515,13 @@ void lcd_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)LCD_WIDTH) + && ((unsigned)y < (unsigned)LCD_HEIGHT) +#endif + ) pfunc(current_vp->x + x, current_vp->y + y); if (d < 0) @@ -524,22 +556,37 @@ void lcd_hline(int x1, int x2, int y) 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; + x2 += current_vp->x; y += current_vp->y; + +#if defined(HAVE_VIEWPORT_CLIP) + /********************* Viewport on screen clipping ********************/ + /* nothing to draw? */ + if (((unsigned)y >= (unsigned) LCD_HEIGHT) || (x1 >= LCD_WIDTH) + || (x2 < 0)) + return; + + /* clipping */ + if (x1 < 0) + x1 = 0; + if (x2 >= LCD_WIDTH) + x2 = LCD_WIDTH-1; +#endif + + width = x2 - x1 + 1; bfunc = lcd_blockfuncs[current_vp->drawmode]; dst = &lcd_framebuffer[y>>2][x1]; @@ -567,12 +614,12 @@ void lcd_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) @@ -582,6 +629,20 @@ void lcd_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)LCD_WIDTH) || (y1 >= LCD_HEIGHT) + || (y2 < 0)) + return; + + /* clipping */ + if (y1 < 0) + y1 = 0; + if (y2 >= LCD_HEIGHT) + y2 = LCD_HEIGHT-1; +#endif bfunc = lcd_blockfuncs[current_vp->drawmode]; dst = &lcd_framebuffer[y1>>2][x]; @@ -624,12 +685,12 @@ void lcd_fillrect(int x, int y, int width, int height) lcd_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; @@ -648,6 +709,30 @@ void lcd_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 >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif if (current_vp->drawmode & DRMODE_INVERSEVID) { @@ -722,12 +807,12 @@ void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x, unsigned mask, mask_bottom; lcd_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; @@ -748,6 +833,32 @@ void ICODE_ATTR lcd_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 >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; @@ -893,12 +1004,12 @@ void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y, fb_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; @@ -919,6 +1030,32 @@ void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y, /* 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 >= LCD_WIDTH) || (y >= LCD_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 > LCD_WIDTH) + width = LCD_WIDTH - x; + if (y + height > LCD_HEIGHT) + height = LCD_HEIGHT - y; +#endif src += stride * (src_y >> 2) + src_x; /* move starting point */ src_y &= 3; 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; diff --git a/firmware/drivers/lcd-charcell.c b/firmware/drivers/lcd-charcell.c index d02c5eeaad..9fabeb4a22 100644 --- a/firmware/drivers/lcd-charcell.c +++ b/firmware/drivers/lcd-charcell.c @@ -84,6 +84,28 @@ void lcd_set_viewport(struct viewport* vp) current_vp = &default_vp; else current_vp = vp; + +#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) LCD_WIDTH + || (unsigned) current_vp->y > (unsigned) LCD_HEIGHT + || current_vp->x + current_vp->width > LCD_WIDTH + || current_vp->y + current_vp->height > LCD_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 lcd_update_viewport(void) @@ -250,6 +272,11 @@ static void lcd_putxchar(int x, int y, int xchar) x += current_vp->x; y += current_vp->y; +#if defined(HAVE_VIEWPORT_CLIP) + if((unsigned)x > (unsigned)LCD_WIDTH || (unsigned)y > (unsigned)LCD_HEIGHT) + return; +#endif + lcd_char = lcd_charbuffer[y][x]; if (lcd_char < lcd_pattern_count) /* old char was soft */ diff --git a/firmware/export/config-mrobe500.h b/firmware/export/config-mrobe500.h index 7b73fe7ad6..82ba8a8e83 100644 --- a/firmware/export/config-mrobe500.h +++ b/firmware/export/config-mrobe500.h @@ -65,6 +65,9 @@ /* define this if the target has volume keys which can be used in the lists */ #define HAVE_VOLUME_IN_LIST +/* define this if you want viewport clipping enabled for safe LCD functions */ +#define HAVE_VIEWPORT_CLIP + /* LCD dimensions */ #define CONFIG_LCD LCD_MROBE500 -- cgit v1.2.3