From 4b8fe8acd1c079e75bb9229791170c549188fc08 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Mon, 26 Sep 2022 08:37:00 +0100 Subject: lcd: Consolidate in-viewport clipping routines In-viewport clipping code is duplicated across 8 files, making it a chore to change anything related to clipping; refactor the clipping logic into dedicated functions. Change-Id: I4ab20bb3c59b0406098d0c7d23833025f17a320a --- firmware/drivers/lcd-16bit-common.c | 78 +-------------- firmware/drivers/lcd-16bit-vert.c | 110 ++------------------- firmware/drivers/lcd-16bit.c | 107 ++------------------ firmware/drivers/lcd-1bit-vert.c | 121 ++++------------------- firmware/drivers/lcd-24bit.c | 186 +++-------------------------------- firmware/drivers/lcd-2bit-horz.c | 144 ++++----------------------- firmware/drivers/lcd-2bit-vert.c | 145 ++++----------------------- firmware/drivers/lcd-2bit-vi.c | 145 ++++----------------------- firmware/drivers/lcd-bitmap-common.c | 110 +++++++++++++++++++++ firmware/drivers/lcd-color-common.c | 16 ++- 10 files changed, 214 insertions(+), 948 deletions(-) diff --git a/firmware/drivers/lcd-16bit-common.c b/firmware/drivers/lcd-16bit-common.c index 523354d5d2..ad41485dfe 100644 --- a/firmware/drivers/lcd-16bit-common.c +++ b/firmware/drivers/lcd-16bit-common.c @@ -133,31 +133,9 @@ void lcd_fillrect(int x, int y, int width, int height) fb_data *dst, *dst_end; int len, step; - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, NULL, NULL)) return; - if (x < 0) - { - width += x; - x = 0; - } - if (y < 0) - { - height += y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - /* drawmode and optimisation */ if (lcd_current_viewport->drawmode & DRMODE_INVERSEVID) { @@ -235,33 +213,9 @@ void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x, int src_y, int stride, int x, int y, int width, int height) { - /******************** Image in viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* convert to viewport coordinates */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - /* move starting point */ src += stride * (src_y >> 3) + src_x; src_y &= 7; @@ -465,35 +419,13 @@ static void ICODE_ATTR lcd_alpha_bitmap_part_mix(const fb_data* image, fb_data *dst, *dst_row; unsigned dmask = 0x00000000; int drmode = lcd_current_viewport->drawmode; - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; + /* initialize blending */ BLEND_INIT; - /* clipping */ - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - /* the following drawmode combinations are possible: * 1) COMPLEMENT: just negates the framebuffer contents * 2) BG and BG+backdrop: draws _only_ background pixels with either diff --git a/firmware/drivers/lcd-16bit-vert.c b/firmware/drivers/lcd-16bit-vert.c index 166af02791..c721ef6302 100644 --- a/firmware/drivers/lcd-16bit-vert.c +++ b/firmware/drivers/lcd-16bit-vert.c @@ -54,46 +54,23 @@ static void ICODE_ATTR lcd_alpha_bitmap_part_mix(const fb_data* image, int stride_image, int stride_src); #include "lcd-color-common.c" -#include "lcd-16bit-common.c" #include "lcd-bitmap-common.c" +#include "lcd-16bit-common.c" /*** drawing functions ***/ /* Draw a horizontal line (optimised) */ void lcd_hline(int x1, int x2, int y) { - int x; fb_data *dst, *dst_end; int stride_dst; lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[lcd_current_viewport->drawmode]; - /* direction flip */ - if (x2 < x1) - { - x = x1; - x1 = x2; - x2 = x; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)y >= (unsigned)lcd_current_viewport->height) || - (x1 >= lcd_current_viewport->width) || - (x2 < 0)) + if (!lcd_clip_viewport_hline(&x1, &x2, &y)) return; - if (x1 < 0) - x1 = 0; - if (x2 >= lcd_current_viewport->width) - x2 = lcd_current_viewport->width-1; - - /* Adjust x1 and y to viewport */ - x1 += lcd_current_viewport->x; - x2 += lcd_current_viewport->x; - y += lcd_current_viewport->y; - - dst = FBADDR(x1 , y ); + dst = FBADDR(x1, y); stride_dst = lcd_current_viewport->buffer->stride; dst_end = dst + (x2 - x1) * stride_dst; @@ -108,36 +85,14 @@ void lcd_hline(int x1, int x2, int y) /* Draw a vertical line (optimised) */ void lcd_vline(int x, int y1, int y2) { - int y, height; + int height; unsigned bits = 0; enum fill_opt fillopt = OPT_NONE; fb_data *dst, *dst_end; - /* direction flip */ - if (y2 < y1) - { - y = y1; - y1 = y2; - y2 = y; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)x >= (unsigned)lcd_current_viewport->width) || - (y1 >= lcd_current_viewport->height) || - (y2 < 0)) + if(!lcd_clip_viewport_vline(&x, &y1, &y2)) return; - if (y1 < 0) - y1 = 0; - if (y2 >= lcd_current_viewport->height) - y2 = lcd_current_viewport->height-1; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y1 += lcd_current_viewport->y; - y2 += lcd_current_viewport->y; - height = y2 - y1 + 1; /* drawmode and optimisation */ @@ -194,33 +149,9 @@ void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y, { fb_data *dst; int stride_dst; - /******************** Image in viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) - return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) + return; src += stride * src_x + src_y; /* move starting point */ dst = FBADDR(x, y); @@ -244,34 +175,9 @@ void ICODE_ATTR lcd_bitmap_transparent_part(const fb_data *src, int src_x, fb_data *dst, *dst_end; int stride_dst; - /******************** Image in viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - src += stride * src_x + src_y; /* move starting point */ dst = FBADDR(x, y); stride_dst = lcd_current_viewport->buffer->stride; diff --git a/firmware/drivers/lcd-16bit.c b/firmware/drivers/lcd-16bit.c index 6dff6f3b50..fd032c0956 100644 --- a/firmware/drivers/lcd-16bit.c +++ b/firmware/drivers/lcd-16bit.c @@ -62,36 +62,14 @@ static void ICODE_ATTR lcd_alpha_bitmap_part_mix(const fb_data* image, /* Draw a horizontal line (optimised) */ void lcd_hline(int x1, int x2, int y) { - int x, width; + int width; unsigned bits = 0; enum fill_opt fillopt = OPT_NONE; fb_data *dst, *dst_end; - /* direction flip */ - if (x2 < x1) - { - x = x1; - x1 = x2; - x2 = x; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)y >= (unsigned)lcd_current_viewport->height) || - (x1 >= lcd_current_viewport->width) || - (x2 < 0)) + if (!lcd_clip_viewport_hline(&x1, &x2, &y)) return; - if (x1 < 0) - x1 = 0; - if (x2 >= lcd_current_viewport->width) - x2 = lcd_current_viewport->width-1; - - /* Adjust x1 and y to viewport */ - x1 += lcd_current_viewport->x; - x2 += lcd_current_viewport->x; - y += lcd_current_viewport->y; - width = x2 - x1 + 1; /* drawmode and optimisation */ @@ -144,37 +122,14 @@ void lcd_hline(int x1, int x2, int y) /* Draw a vertical line (optimised) */ void lcd_vline(int x, int y1, int y2) { - int y; fb_data *dst, *dst_end; int stride_dst; lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[lcd_current_viewport->drawmode]; - /* direction flip */ - if (y2 < y1) - { - y = y1; - y1 = y2; - y2 = y; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)x >= (unsigned)lcd_current_viewport->width) || - (y1 >= lcd_current_viewport->height) || - (y2 < 0)) + if (!lcd_clip_viewport_vline(&x, &y1, &y2)) return; - if (y1 < 0) - y1 = 0; - if (y2 >= lcd_current_viewport->height) - y2 = lcd_current_viewport->height-1; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y1 += lcd_current_viewport->y; - y2 += lcd_current_viewport->y; - - dst = FBADDR(x , y1); + dst = FBADDR(x, y1); stride_dst = lcd_current_viewport->buffer->stride; dst_end = dst + (y2 - y1) * stride_dst; @@ -194,34 +149,9 @@ void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y, fb_data *dst; int stride_dst; - /******************** Image in viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - src += stride * src_y + src_x; /* move starting point */ dst = FBADDR(x, y); stride_dst = lcd_current_viewport->buffer->stride; @@ -244,34 +174,9 @@ void ICODE_ATTR lcd_bitmap_transparent_part(const fb_data *src, int src_x, unsigned fg = lcd_current_viewport->fg_pattern; int stride_dst = lcd_current_viewport->buffer->stride; - /******************** Image in viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - src += stride * src_y + src_x; /* move starting point */ dst = FBADDR(x, y); diff --git a/firmware/drivers/lcd-1bit-vert.c b/firmware/drivers/lcd-1bit-vert.c index 49c15c041e..e1b65ca9c0 100644 --- a/firmware/drivers/lcd-1bit-vert.c +++ b/firmware/drivers/lcd-1bit-vert.c @@ -92,6 +92,8 @@ static void *LCDFN(frameaddress_default)(int x, int y) return fb->FBFN(ptr) + element;/*(element % fb->elems);*/ } +#include "lcd-bitmap-common.c" + /* LCD init */ void LCDFN(init)(void) { @@ -272,10 +274,8 @@ 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) - ) - LCDFN(pixelfuncs)[CURRENT_VP->drawmode](CURRENT_VP->x + x, CURRENT_VP->y + y); + if (LCDFN(clip_viewport_pixel)(&x, &y)) + LCDFN(pixelfuncs)[CURRENT_VP->drawmode](x, y); } /* Draw a line */ @@ -287,6 +287,7 @@ void LCDFN(drawline)(int x1, int y1, int x2, int y2) int d, dinc1, dinc2; int x, xinc1, xinc2; int y, yinc1, yinc2; + int x_vp, y_vp, w_vp, h_vp; LCDFN(pixelfunc_type) *pfunc = LCDFN(pixelfuncs)[CURRENT_VP->drawmode]; deltax = abs(x2 - x1); @@ -341,12 +342,15 @@ void LCDFN(drawline)(int x1, int y1, int x2, int y2) x = x1; y = y1; + x_vp = CURRENT_VP->x; + y_vp = CURRENT_VP->y; + w_vp = CURRENT_VP->width; + h_vp = CURRENT_VP->height; + for (i = 0; i < numpixels; i++) { - if ( ((unsigned)x < (unsigned)CURRENT_VP->width) - && ((unsigned)y < (unsigned)CURRENT_VP->height) - ) - pfunc(CURRENT_VP->x + x, CURRENT_VP->y + y); + if (x >= 0 && y >= 0 && x < w_vp && y < h_vp) + pfunc(x + x_vp, y + y_vp); if (d < 0) { @@ -366,35 +370,14 @@ void LCDFN(drawline)(int x1, int y1, int x2, int y2) /* Draw a horizontal line (optimised) */ void LCDFN(hline)(int x1, int x2, int y) { - int x, width; + int width; unsigned char *dst, *dst_end; unsigned mask; LCDFN(blockfunc_type) *bfunc; - /* direction flip */ - if (x2 < x1) - { - x = x1; - x1 = x2; - x2 = x; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)y >= (unsigned)CURRENT_VP->height) || (x1 >= CURRENT_VP->width) - || (x2 < 0)) + if (!LCDFN(clip_viewport_hline)(&x1, &x2, &y)) return; - if (x1 < 0) - x1 = 0; - if (x2 >= CURRENT_VP->width) - x2 = CURRENT_VP->width-1; - - /* adjust to viewport */ - x1 += CURRENT_VP->x; - x2 += CURRENT_VP->x; - y += CURRENT_VP->y; - width = x2 - x1 + 1; bfunc = LCDFN(blockfuncs)[CURRENT_VP->drawmode]; @@ -416,30 +399,9 @@ void LCDFN(vline)(int x, int y1, int y2) unsigned mask, mask_bottom; LCDFN(blockfunc_type) *bfunc; - /* direction flip */ - if (y2 < y1) - { - ny = y1; - y1 = y2; - y2 = ny; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)x >= (unsigned)CURRENT_VP->width) || (y1 >= CURRENT_VP->height) - || (y2 < 0)) + if (!LCDFN(clip_viewport_vline)(&x, &y1, &y2)) return; - if (y1 < 0) - y1 = 0; - if (y2 >= CURRENT_VP->height) - y2 = CURRENT_VP->height-1; - - /* adjust for viewport */ - y1 += CURRENT_VP->y; - y2 += CURRENT_VP->y; - x += CURRENT_VP->x; - bfunc = LCDFN(blockfuncs)[CURRENT_VP->drawmode]; dst = LCDFB(x,y1>>3); ny = y2 - (y1 & ~7); @@ -483,31 +445,9 @@ 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)) + if (!LCDFN(clip_viewport_rect)(&x, &y, &width, &height, NULL, NULL)) 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 (CURRENT_VP->drawmode & DRMODE_INVERSEVID) { if (CURRENT_VP->drawmode & DRMODE_BG) @@ -582,34 +522,9 @@ 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)) + if (!LCDFN(clip_viewport_rect)(&x, &y, &width, &height, &src_x, &src_y)) return; - /* clip image in viewport */ - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - 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; - - /* adjust for viewport */ - x += CURRENT_VP->x; - y += CURRENT_VP->y; - src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; y -= src_y; @@ -696,5 +611,3 @@ void LCDFN(bitmap)(const unsigned char *src, int x, int y, int width, { LCDFN(bitmap_part)(src, 0, 0, width, x, y, width, height); } - -#include "lcd-bitmap-common.c" diff --git a/firmware/drivers/lcd-24bit.c b/firmware/drivers/lcd-24bit.c index 279ed5924a..d95415e957 100644 --- a/firmware/drivers/lcd-24bit.c +++ b/firmware/drivers/lcd-24bit.c @@ -173,31 +173,9 @@ void lcd_fillrect(int x, int y, int width, int height) fb_data bits; memset(&bits, 0, sizeof(fb_data)); - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, NULL, NULL)) return; - if (x < 0) - { - width += x; - x = 0; - } - if (y < 0) - { - height += y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - /* drawmode and optimisation */ if (lcd_current_viewport->drawmode & DRMODE_INVERSEVID) { @@ -287,33 +265,9 @@ void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x, int drmode = lcd_current_viewport->drawmode; int row; - /******************** Image in viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; src_end = src + width; @@ -491,32 +445,9 @@ static void ICODE_ATTR lcd_alpha_bitmap_part_mix(const fb_data* image, fb_data *dst, *dst_row; unsigned dmask = 0x00000000; int drmode = lcd_current_viewport->drawmode; - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) - return; - /* clipping */ - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) + return; /* the following drawmode combinations are possible: * 1) COMPLEMENT: just negates the framebuffer contents @@ -758,38 +689,16 @@ static void ICODE_ATTR lcd_alpha_bitmap_part_mix(const fb_data* image, /* Draw a horizontal line (optimised) */ void lcd_hline(int x1, int x2, int y) { - int x, width; + int width; fb_data *dst, *dst_end; lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[lcd_current_viewport->drawmode]; - /* direction flip */ - if (x2 < x1) - { - x = x1; - x1 = x2; - x2 = x; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)y >= (unsigned)lcd_current_viewport->height) || - (x1 >= lcd_current_viewport->width) || - (x2 < 0)) + if (!lcd_clip_viewport_hline(&x1, &x2, &y)) return; - if (x1 < 0) - x1 = 0; - if (x2 >= lcd_current_viewport->width) - x2 = lcd_current_viewport->width-1; - - /* Adjust x1 and y to viewport */ - x1 += lcd_current_viewport->x; - x2 += lcd_current_viewport->x; - y += lcd_current_viewport->y; - width = x2 - x1 + 1; - dst = FBADDR(x1 , y); + dst = FBADDR(x1, y); dst_end = dst + width; do { @@ -801,36 +710,13 @@ void lcd_hline(int x1, int x2, int y) /* Draw a vertical line (optimised) */ void lcd_vline(int x, int y1, int y2) { - int y; fb_data *dst, *dst_end; lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[lcd_current_viewport->drawmode]; - /* direction flip */ - if (y2 < y1) - { - y = y1; - y1 = y2; - y2 = y; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)x >= (unsigned)lcd_current_viewport->width) || - (y1 >= lcd_current_viewport->height) || - (y2 < 0)) + if (!lcd_clip_viewport_vline(&x, &y1, &y2)) return; - if (y1 < 0) - y1 = 0; - if (y2 >= lcd_current_viewport->height) - y2 = lcd_current_viewport->height-1; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y1 += lcd_current_viewport->y; - y2 += lcd_current_viewport->y; - - dst = FBADDR(x , y1); + dst = FBADDR(x, y1); dst_end = dst + (y2 - y1) * LCD_WIDTH; do @@ -848,34 +734,9 @@ void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y, { fb_data *dst; - /******************** Image in viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - src += stride * src_y + src_x; /* move starting point */ dst = FBADDR(x, y); @@ -896,34 +757,9 @@ void ICODE_ATTR lcd_bitmap_transparent_part(const fb_data *src, int src_x, fb_data *dst; fb_data fg, transparent, replacewithfg; - /******************** Image in viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - src += stride * src_y + src_x; /* move starting point */ dst = FBADDR(x, y); diff --git a/firmware/drivers/lcd-2bit-horz.c b/firmware/drivers/lcd-2bit-horz.c index 1bc489247c..37125679ad 100644 --- a/firmware/drivers/lcd-2bit-horz.c +++ b/firmware/drivers/lcd-2bit-horz.c @@ -89,6 +89,8 @@ static void *lcd_frameaddress_default(int x, int y) return fb->fb_ptr + element;/*(element % fb->elems);*/ } +#include "lcd-bitmap-common.c" + /* LCD init */ void lcd_init(void) { @@ -418,10 +420,8 @@ void lcd_clear_viewport(void) /* Set a single pixel */ void lcd_drawpixel(int x, int y) { - if ( ((unsigned)x < (unsigned)lcd_current_viewport->width) - && ((unsigned)y < (unsigned)lcd_current_viewport->height) - ) - lcd_pixelfuncs[lcd_current_viewport->drawmode](lcd_current_viewport->x + x, lcd_current_viewport->y + y); + if (lcd_clip_viewport_pixel(&x, &y)) + lcd_pixelfuncs[lcd_current_viewport->drawmode](x, y); } /* Draw a line */ @@ -433,6 +433,7 @@ void lcd_drawline(int x1, int y1, int x2, int y2) int d, dinc1, dinc2; int x, xinc1, xinc2; int y, yinc1, yinc2; + int x_vp, y_vp, w_vp, h_vp; lcd_pixelfunc_type *pfunc = lcd_pixelfuncs[lcd_current_viewport->drawmode]; deltay = abs(y2 - y1); @@ -487,12 +488,15 @@ void lcd_drawline(int x1, int y1, int x2, int y2) x = x1; y = y1; + x_vp = lcd_current_viewport->x; + y_vp = lcd_current_viewport->y; + w_vp = lcd_current_viewport->width; + h_vp = lcd_current_viewport->height; + for (i = 0; i < numpixels; i++) { - if ( ((unsigned)x < (unsigned)lcd_current_viewport->width) - && ((unsigned)y < (unsigned)lcd_current_viewport->height) - ) - pfunc(lcd_current_viewport->x + x, lcd_current_viewport->y + y); + if (x >= 0 && y >= 0 && x < w_vp && y < h_vp) + pfunc(x + x_vp, y + y_vp); if (d < 0) { @@ -517,30 +521,9 @@ void lcd_hline(int x1, int x2, int y) unsigned mask, mask_right; lcd_blockfunc_type *bfunc; - /* direction flip */ - if (x2 < x1) - { - nx = x1; - x1 = x2; - x2 = nx; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)y >= (unsigned)lcd_current_viewport->height) || (x1 >= lcd_current_viewport->width) - || (x2 < 0)) + if (!lcd_clip_viewport_hline(&x1, &x2, &y)) return; - if (x1 < 0) - x1 = 0; - if (x2 >= lcd_current_viewport->width) - x2 = lcd_current_viewport->width-1; - - /* adjust to viewport */ - x1 += lcd_current_viewport->x; - x2 += lcd_current_viewport->x; - y += lcd_current_viewport->y; - bfunc = lcd_blockfuncs[lcd_current_viewport->drawmode]; dst = FBADDR(x1>>2,y); nx = x2 - (x1 & ~3); @@ -559,36 +542,14 @@ void lcd_hline(int x1, int x2, int y) /* Draw a vertical line (optimised) */ void lcd_vline(int x, int y1, int y2) { - int y; unsigned char *dst, *dst_end; int stride_dst; unsigned mask; lcd_blockfunc_type *bfunc; - /* direction flip */ - if (y2 < y1) - { - y = y1; - y1 = y2; - y2 = y; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)x >= (unsigned)lcd_current_viewport->width) || (y1 >= lcd_current_viewport->height) - || (y2 < 0)) + if (!lcd_clip_viewport_vline(&x, &y1, &y2)) return; - if (y1 < 0) - y1 = 0; - if (y2 >= lcd_current_viewport->height) - y2 = lcd_current_viewport->height-1; - - /* adjust for viewport */ - y1 += lcd_current_viewport->y; - y2 += lcd_current_viewport->y; - x += lcd_current_viewport->x; - bfunc = lcd_blockfuncs[lcd_current_viewport->drawmode]; dst = FBADDR(x>>2,y1); stride_dst = LCD_FBSTRIDE(lcd_current_viewport->buffer->stride, 0); @@ -627,31 +588,9 @@ 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 >= lcd_current_viewport->width) || (y >= lcd_current_viewport->height) - || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, NULL, NULL)) return; - if (x < 0) - { - width += x; - x = 0; - } - if (y < 0) - { - height += y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - bfunc = lcd_blockfuncs[lcd_current_viewport->drawmode]; dst = FBADDR(x>>2,y); stride_dst = LCD_FBSTRIDE(lcd_current_viewport->buffer->stride, 0); @@ -706,32 +645,9 @@ void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x, unsigned dst_mask; int drmode = lcd_current_viewport->drawmode; - /******************** Image in viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - x += lcd_current_viewport->x; /* adjust for viewport */ - y += lcd_current_viewport->y; /* adjust for viewport */ - src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; src_end = src + width; @@ -894,33 +810,9 @@ void ICODE_ATTR lcd_bitmap_part(const unsigned char *src, int src_x, int stride_dst; unsigned mask, mask_right; - /******************** Image in viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - stride = LCD_FBSTRIDE(stride, 0); /* convert to no. of bytes */ src += stride * src_y + (src_x >> 2); /* move starting point */ @@ -971,5 +863,3 @@ void lcd_bitmap(const unsigned char *src, int x, int y, int width, int height) { lcd_bitmap_part(src, 0, 0, width, x, y, width, height); } - -#include "lcd-bitmap-common.c" diff --git a/firmware/drivers/lcd-2bit-vert.c b/firmware/drivers/lcd-2bit-vert.c index b206a2d816..5fd86c409a 100644 --- a/firmware/drivers/lcd-2bit-vert.c +++ b/firmware/drivers/lcd-2bit-vert.c @@ -91,6 +91,8 @@ static void *lcd_frameaddress_default(int x, int y) return fb->fb_ptr + element; /*(element % fb->elems);*/ } +#include "lcd-bitmap-common.c" + /* LCD init */ void lcd_init(void) { @@ -420,10 +422,8 @@ void lcd_clear_viewport(void) /* Set a single pixel */ void lcd_drawpixel(int x, int y) { - if ( ((unsigned)x < (unsigned)lcd_current_viewport->width) - && ((unsigned)y < (unsigned)lcd_current_viewport->height) - ) - lcd_pixelfuncs[lcd_current_viewport->drawmode](lcd_current_viewport->x + x, lcd_current_viewport->y + y); + if (lcd_clip_viewport_pixel(&x, &y)) + lcd_pixelfuncs[lcd_current_viewport->drawmode](x, y); } /* Draw a line */ @@ -435,6 +435,7 @@ void lcd_drawline(int x1, int y1, int x2, int y2) int d, dinc1, dinc2; int x, xinc1, xinc2; int y, yinc1, yinc2; + int x_vp, y_vp, w_vp, h_vp; lcd_pixelfunc_type *pfunc = lcd_pixelfuncs[lcd_current_viewport->drawmode]; deltax = abs(x2 - x1); @@ -489,12 +490,15 @@ void lcd_drawline(int x1, int y1, int x2, int y2) x = x1; y = y1; + x_vp = lcd_current_viewport->x; + y_vp = lcd_current_viewport->y; + w_vp = lcd_current_viewport->width; + h_vp = lcd_current_viewport->height; + for (i = 0; i < numpixels; i++) { - if ( ((unsigned)x < (unsigned)lcd_current_viewport->width) - && ((unsigned)y < (unsigned)lcd_current_viewport->height) - ) - pfunc(lcd_current_viewport->x + x, lcd_current_viewport->y + y); + if (x >= 0 && y >= 0 && x < w_vp && y < h_vp) + pfunc(x + x_vp, y + y_vp); if (d < 0) { @@ -514,36 +518,14 @@ void lcd_drawline(int x1, int y1, int x2, int y2) /* Draw a horizontal line (optimised) */ void lcd_hline(int x1, int x2, int y) { - int x; int width; fb_data *dst, *dst_end; unsigned mask; lcd_blockfunc_type *bfunc; - /* direction flip */ - if (x2 < x1) - { - x = x1; - x1 = x2; - x2 = x; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)y >= (unsigned)lcd_current_viewport->height) || (x1 >= lcd_current_viewport->width) - || (x2 < 0)) + if (!lcd_clip_viewport_hline(&x1, &x2, &y)) return; - if (x1 < 0) - x1 = 0; - if (x2 >= lcd_current_viewport->width) - x2 = lcd_current_viewport->width-1; - - /* adjust x1 and y to viewport */ - x1 += lcd_current_viewport->x; - x2 += lcd_current_viewport->x; - y += lcd_current_viewport->y; - width = x2 - x1 + 1; bfunc = lcd_blockfuncs[lcd_current_viewport->drawmode]; @@ -565,30 +547,9 @@ void lcd_vline(int x, int y1, int y2) unsigned mask, mask_bottom; lcd_blockfunc_type *bfunc; - /* direction flip */ - if (y2 < y1) - { - ny = y1; - y1 = y2; - y2 = ny; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)x >= (unsigned)lcd_current_viewport->width) || (y1 >= lcd_current_viewport->height) - || (y2 < 0)) + if (!lcd_clip_viewport_vline(&x, &y1, &y2)) return; - if (y1 < 0) - y1 = 0; - if (y2 >= lcd_current_viewport->height) - y2 = lcd_current_viewport->height-1; - - /* adjust for viewport */ - y1 += lcd_current_viewport->y; - y2 += lcd_current_viewport->y; - x += lcd_current_viewport->x; - bfunc = lcd_blockfuncs[lcd_current_viewport->drawmode]; dst = FBADDR(x,y1>>2); stride_dst = lcd_current_viewport->buffer->stride; @@ -632,31 +593,9 @@ 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 >= lcd_current_viewport->width) - || (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, NULL, NULL)) return; - if (x < 0) - { - width += x; - x = 0; - } - if (y < 0) - { - height += y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - if (lcd_current_viewport->drawmode & DRMODE_INVERSEVID) { if ((lcd_current_viewport->drawmode & DRMODE_BG) && !lcd_backdrop) @@ -732,33 +671,9 @@ 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 >= lcd_current_viewport->width) || - (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; y -= src_y; @@ -905,33 +820,9 @@ void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y, int stride_dst; unsigned mask, mask_bottom; - /******************** Image in viewport clipping **********************/ - /* nothing to draw? */ - if ((width <= 0) || (height <= 0) || (x >= lcd_current_viewport->width) - || (y >= lcd_current_viewport->height) || (x + width <= 0) || (y + height <= 0)) + if (!lcd_clip_viewport_rect(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - if (y < 0) - { - height += y; - src_y -= y; - y = 0; - } - if (x + width > lcd_current_viewport->width) - width = lcd_current_viewport->width - x; - if (y + height > lcd_current_viewport->height) - height = lcd_current_viewport->height - y; - - /* adjust for viewport */ - x += lcd_current_viewport->x; - y += lcd_current_viewport->y; - src += stride * (src_y >> 2) + src_x; /* move starting point */ src_y &= 3; y -= src_y; @@ -1014,5 +905,3 @@ void lcd_bitmap(const fb_data *src, int x, int y, int width, int height) { lcd_bitmap_part(src, 0, 0, width, x, y, width, height); } - -#include "lcd-bitmap-common.c" diff --git a/firmware/drivers/lcd-2bit-vi.c b/firmware/drivers/lcd-2bit-vi.c index 0580705a05..b19fdba266 100644 --- a/firmware/drivers/lcd-2bit-vi.c +++ b/firmware/drivers/lcd-2bit-vi.c @@ -103,6 +103,8 @@ static void *LCDFN(frameaddress_default)(int x, int y) return fb->FBFN(ptr) + element;/*(element % fb->elems);*/ } +#include "lcd-bitmap-common.c" + /* LCD init */ void LCDFN(init)(void) { @@ -453,10 +455,8 @@ 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) - ) - LCDFN(pixelfuncs)[CURRENT_VP->drawmode](CURRENT_VP->x+x, CURRENT_VP->y+y); + if (LCDFN(clip_viewport_pixel)(&x, &y)) + LCDFN(pixelfuncs)[CURRENT_VP->drawmode](x, y); } /* Draw a line */ @@ -468,6 +468,7 @@ void LCDFN(drawline)(int x1, int y1, int x2, int y2) int d, dinc1, dinc2; int x, xinc1, xinc2; int y, yinc1, yinc2; + int x_vp, y_vp, w_vp, h_vp; LCDFN(pixelfunc_type) *pfunc = LCDFN(pixelfuncs)[CURRENT_VP->drawmode]; deltax = abs(x2 - x1); @@ -522,12 +523,15 @@ void LCDFN(drawline)(int x1, int y1, int x2, int y2) x = x1; y = y1; + x_vp = CURRENT_VP->x; + y_vp = CURRENT_VP->y; + w_vp = CURRENT_VP->width; + h_vp = CURRENT_VP->height; + for (i = 0; i < numpixels; i++) { - if ( ((unsigned)x < (unsigned)CURRENT_VP->width) - && ((unsigned)y < (unsigned)CURRENT_VP->height) - ) - pfunc(CURRENT_VP->x + x, CURRENT_VP->y + y); + if (x >= 0 && y >= 0 && x < w_vp && y < h_vp) + pfunc(x + x_vp, y + y_vp); if (d < 0) { @@ -547,36 +551,14 @@ void LCDFN(drawline)(int x1, int y1, int x2, int y2) /* Draw a horizontal line (optimised) */ void LCDFN(hline)(int x1, int x2, int y) { - int x; int width; FBFN(data) *dst, *dst_end; unsigned mask; LCDFN(blockfunc_type) *bfunc; - /* direction flip */ - if (x2 < x1) - { - x = x1; - x1 = x2; - x2 = x; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)y >= (unsigned)CURRENT_VP->height) || (x1 >= CURRENT_VP->width) - || (x2 < 0)) + if (!LCDFN(clip_viewport_hline)(&x1, &x2, &y)) 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; - width = x2 - x1 + 1; bfunc = LCDFN(blockfuncs)[CURRENT_VP->drawmode]; @@ -598,30 +580,9 @@ void LCDFN(vline)(int x, int y1, int y2) unsigned mask, mask_bottom; LCDFN(blockfunc_type) *bfunc; - /* direction flip */ - if (y2 < y1) - { - ny = y1; - y1 = y2; - y2 = ny; - } - - /******************** In viewport clipping **********************/ - /* nothing to draw? */ - if (((unsigned)x >= (unsigned)CURRENT_VP->width) || (y1 >= CURRENT_VP->height) - || (y2 < 0)) + if (!LCDFN(clip_viewport_vline)(&x, &y1, &y2)) return; - if (y1 < 0) - y1 = 0; - if (y2 >= CURRENT_VP->height) - y2 = CURRENT_VP->height-1; - - /* adjust for viewport */ - y1 += CURRENT_VP->y; - y2 += CURRENT_VP->y; - x += CURRENT_VP->x; - bfunc = LCDFN(blockfuncs)[CURRENT_VP->drawmode]; dst = LCDFB(x,y1>>3); stride_dst = CURRENT_VP->buffer->stride; @@ -667,31 +628,9 @@ 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)) + if (!LCDFN(clip_viewport_rect)(&x, &y, &width, &height, NULL, NULL)) 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 (CURRENT_VP->drawmode & DRMODE_INVERSEVID) { if ((CURRENT_VP->drawmode & DRMODE_BG) && !backdrop) @@ -769,33 +708,9 @@ 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)) + if (!LCDFN(clip_viewport_rect)(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - 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; - - /* adjust for viewport */ - x += CURRENT_VP->x; - y += CURRENT_VP->y; - src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; y -= src_y; @@ -913,33 +828,9 @@ void ICODE_ATTR LCDFN(bitmap_part)(const FBFN(data) *src, int src_x, int stride_dst; 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)) + if (!LCDFN(clip_viewport_rect)(&x, &y, &width, &height, &src_x, &src_y)) return; - if (x < 0) - { - width += x; - src_x -= x; - x = 0; - } - 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; - - /* adjust for viewport */ - x += CURRENT_VP->x; - y += CURRENT_VP->y; - src += stride * (src_y >> 3) + src_x; /* move starting point */ src_y &= 7; y -= src_y; @@ -1036,5 +927,3 @@ void LCDFN(bitmap)(const FBFN(data) *src, int x, int y, int width, int height) { LCDFN(bitmap_part)(src, 0, 0, width, x, y, width, height); } - -#include "lcd-bitmap-common.c" diff --git a/firmware/drivers/lcd-bitmap-common.c b/firmware/drivers/lcd-bitmap-common.c index 24b302b6d4..c867b214b5 100644 --- a/firmware/drivers/lcd-bitmap-common.c +++ b/firmware/drivers/lcd-bitmap-common.c @@ -53,6 +53,116 @@ extern void viewport_set_buffer(struct viewport *vp, struct frame_buffer_t *buffer, const enum screen_type screen); /* viewport.c */ + +/* + * In-viewport clipping functions: + * + * These clip a primitive (pixel, line, rect) given in + * viewport-relative coordinates to the current viewport, + * and translate it to screen coordinates. They return + * false if the resulting primitive would be off-screen. + */ + +static bool LCDFN(clip_viewport_pixel)(int *x, int *y) +{ + struct viewport *vp = LCDFN(current_viewport); + + if(*x >= vp->width || *y >= vp->height) + return false; + + *x += vp->x; + *y += vp->y; + return true; +} + +static bool LCDFN(clip_viewport_hline)(int *x1, int *x2, int *y) +{ + struct viewport *vp = LCDFN(current_viewport); + + if (*y < 0 || *y > vp->height) + return false; + + if (*x2 < *x1) { + int tmp = *x2; + *x2 = *x1; + *x1 = tmp; + } + + if (*x1 < 0) + *x1 = 0; + else if (*x1 >= vp->width) + return false; + + if (*x2 < 0) + return false; + else if (*x2 >= vp->width) + *x2 = vp->width - 1; + + *x1 += vp->x; + *x2 += vp->x; + *y += vp->y; + return true; +} + +static bool LCDFN(clip_viewport_vline)(int *x, int *y1, int *y2) +{ + struct viewport *vp = LCDFN(current_viewport); + + if (*x < 0 || *x > vp->width) + return false; + + if (*y2 < *y1) { + int tmp = *y2; + *y2 = *y1; + *y1 = tmp; + } + + if (*y1 < 0) + *y1 = 0; + else if (*y1 >= vp->height) + return false; + + if (*y2 < 0) + return false; + else if (*y2 >= vp->height) + *y2 = vp->height - 1; + + *x += vp->x; + *y1 += vp->y; + *y2 += vp->y; + return true; +} + +static bool LCDFN(clip_viewport_rect)(int *x, int *y, int *width, int *height, + int *src_x, int *src_y) +{ + struct viewport *vp = LCDFN(current_viewport); + + if (*x < 0) { + *width += *x; + if (src_x) + *src_x -= *x; + *x = 0; + } + + if (*y < 0) { + *height += *y; + if (src_y) + *src_y -= *y; + *y = 0; + } + + if (*x + *width > vp->width) + *width = vp->width - *x; + + if (*y + *height > vp->height) + *height = vp->height - *y; + + *x += vp->x; + *y += vp->y; + return *width > 0 && *height > 0; +} + /* * draws the borders of the current viewport **/ diff --git a/firmware/drivers/lcd-color-common.c b/firmware/drivers/lcd-color-common.c index 72ce7c209f..8a9ef64cea 100644 --- a/firmware/drivers/lcd-color-common.c +++ b/firmware/drivers/lcd-color-common.c @@ -40,6 +40,7 @@ static fb_data lcd_static_framebuffer[LCD_FBHEIGHT][LCD_FBWIDTH] IRAM_LCDFRAMEBUFFER CACHEALIGN_AT_LEAST_ATTR(16); static void *lcd_frameaddress_default(int x, int y); +static bool lcd_clip_viewport_pixel(int *x, int *y); static fb_data* lcd_backdrop = NULL; static long lcd_backdrop_offset IDATA_ATTR = 0; @@ -196,10 +197,8 @@ fb_data* lcd_get_backdrop(void) /* Set a single pixel */ void lcd_drawpixel(int x, int y) { - if ( ((unsigned)x < (unsigned)lcd_current_viewport->width) - && ((unsigned)y < (unsigned)lcd_current_viewport->height) - ) - lcd_fastpixelfuncs[lcd_current_viewport->drawmode](FBADDR(lcd_current_viewport->x+x, lcd_current_viewport->y+y)); + if (lcd_clip_viewport_pixel(&x, &y)) + lcd_fastpixelfuncs[lcd_current_viewport->drawmode](FBADDR(x, y)); } /* Draw a line */ @@ -274,11 +273,8 @@ void lcd_drawline(int x1, int y1, int x2, int y2) for (i = 0; i < numpixels; i++) { - if ((x >= 0 && y >= 0) - && (x < w_vp) - && (y < h_vp) - ) - pfunc(fbaddr( x + x_vp, y + y_vp)); + if (x >= 0 && y >= 0 && x < w_vp && y < h_vp) + pfunc(fbaddr(x + x_vp, y + y_vp)); if (d < 0) { @@ -401,7 +397,7 @@ void lcd_gradient_fillrect_part(int x, int y, int width, int height, x1 = x; x2 = x + width; - if (height == 0) return; + if (height <= 0) return; step_mul = (1 << 16) / src_height; int h_r = RGB_UNPACK_RED(start_rgb); -- cgit v1.2.3