lcd: Consolidate drawpixel, drawline, and drawrect functions

All three functions are nearly identical regardless of the
LCD pixel format. Consolidate them into a generic version in
lcd-bitmap-common.c.

Change-Id: Iab13429ea27ea2b0150b9004535bd27d4a4121a0
This commit is contained in:
Aidan MacDonald 2022-09-26 09:23:18 +01:00
parent 4b8fe8acd1
commit 4f9e4ddb99
6 changed files with 127 additions and 559 deletions

View file

@ -271,102 +271,6 @@ void LCDFN(clear_viewport)(void)
CURRENT_VP->flags &= ~(VP_FLAG_VP_SET_CLEAN);
}
/* Set a single pixel */
void LCDFN(drawpixel)(int x, int y)
{
if (LCDFN(clip_viewport_pixel)(&x, &y))
LCDFN(pixelfuncs)[CURRENT_VP->drawmode](x, y);
}
/* Draw a line */
void LCDFN(drawline)(int x1, int y1, int x2, int y2)
{
int numpixels;
int i;
int deltax, deltay;
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);
if (deltax == 0)
{
/* DEBUGF(LCDNAME "drawline() called for vertical line - optimisation.\n"); */
LCDFN(vline)(x1, y1, y2);
return;
}
deltay = abs(y2 - y1);
if (deltay == 0)
{
/* DEBUGF(LCDNAME "drawline() called for horizontal line - optimisation.\n"); */
LCDFN(hline)(x1, x2, y1);
return;
}
xinc2 = 1;
yinc2 = 1;
if (deltax >= deltay)
{
numpixels = deltax;
d = 2 * deltay - deltax;
dinc1 = deltay * 2;
dinc2 = (deltay - deltax) * 2;
xinc1 = 1;
yinc1 = 0;
}
else
{
numpixels = deltay;
d = 2 * deltax - deltay;
dinc1 = deltax * 2;
dinc2 = (deltax - deltay) * 2;
xinc1 = 0;
yinc1 = 1;
}
numpixels++; /* include endpoints */
if (x1 > x2)
{
xinc1 = -xinc1;
xinc2 = -xinc2;
}
if (y1 > y2)
{
yinc1 = -yinc1;
yinc2 = -yinc2;
}
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 (x >= 0 && y >= 0 && x < w_vp && y < h_vp)
pfunc(x + x_vp, y + y_vp);
if (d < 0)
{
d += dinc1;
x += xinc1;
y += yinc1;
}
else
{
d += dinc2;
x += xinc2;
y += yinc2;
}
}
}
/* Draw a horizontal line (optimised) */
void LCDFN(hline)(int x1, int x2, int y)
{
@ -419,21 +323,6 @@ void LCDFN(vline)(int x, int y1, int y2)
bfunc(dst, mask, 0xFFu);
}
/* Draw a rectangular box */
void LCDFN(drawrect)(int x, int y, int width, int height)
{
if ((width <= 0) || (height <= 0))
return;
int x2 = x + width - 1;
int y2 = y + height - 1;
LCDFN(vline)(x, y, y2);
LCDFN(vline)(x2, y, y2);
LCDFN(hline)(x, x2, y);
LCDFN(hline)(x, x2, y2);
}
/* Fill a rectangular area */
void LCDFN(fillrect)(int x, int y, int width, int height)
{

View file

@ -417,102 +417,6 @@ void lcd_clear_viewport(void)
lcd_current_viewport->flags &= ~(VP_FLAG_VP_SET_CLEAN);
}
/* Set a single pixel */
void lcd_drawpixel(int x, int y)
{
if (lcd_clip_viewport_pixel(&x, &y))
lcd_pixelfuncs[lcd_current_viewport->drawmode](x, y);
}
/* Draw a line */
void lcd_drawline(int x1, int y1, int x2, int y2)
{
int numpixels;
int i;
int deltax, deltay;
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);
if (deltay == 0)
{
/* DEBUGF("lcd_drawline() called for horizontal line - optimisation.\n"); */
lcd_hline(x1, x2, y1);
return;
}
deltax = abs(x2 - x1);
if (deltax == 0)
{
/* DEBUGF("lcd_drawline() called for vertical line - optimisation.\n"); */
lcd_vline(x1, y1, y2);
return;
}
xinc2 = 1;
yinc2 = 1;
if (deltax >= deltay)
{
numpixels = deltax;
d = 2 * deltay - deltax;
dinc1 = deltay * 2;
dinc2 = (deltay - deltax) * 2;
xinc1 = 1;
yinc1 = 0;
}
else
{
numpixels = deltay;
d = 2 * deltax - deltay;
dinc1 = deltax * 2;
dinc2 = (deltax - deltay) * 2;
xinc1 = 0;
yinc1 = 1;
}
numpixels++; /* include endpoints */
if (x1 > x2)
{
xinc1 = -xinc1;
xinc2 = -xinc2;
}
if (y1 > y2)
{
yinc1 = -yinc1;
yinc2 = -yinc2;
}
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 (x >= 0 && y >= 0 && x < w_vp && y < h_vp)
pfunc(x + x_vp, y + y_vp);
if (d < 0)
{
d += dinc1;
x += xinc1;
y += yinc1;
}
else
{
d += dinc2;
x += xinc2;
y += yinc2;
}
}
}
/* Draw a horizontal line (optimised) */
void lcd_hline(int x1, int x2, int y)
{
@ -564,21 +468,6 @@ void lcd_vline(int x, int y1, int y2)
while (dst <= dst_end);
}
/* Draw a rectangular box */
void lcd_drawrect(int x, int y, int width, int height)
{
if ((width <= 0) || (height <= 0))
return;
int x2 = x + width - 1;
int y2 = y + height - 1;
lcd_vline(x, y, y2);
lcd_vline(x2, y, y2);
lcd_hline(x, x2, y);
lcd_hline(x, x2, y2);
}
/* Fill a rectangular area */
void lcd_fillrect(int x, int y, int width, int height)
{

View file

@ -419,102 +419,6 @@ void lcd_clear_viewport(void)
lcd_current_viewport->flags &= ~(VP_FLAG_VP_SET_CLEAN);
}
/* Set a single pixel */
void lcd_drawpixel(int x, int y)
{
if (lcd_clip_viewport_pixel(&x, &y))
lcd_pixelfuncs[lcd_current_viewport->drawmode](x, y);
}
/* Draw a line */
void lcd_drawline(int x1, int y1, int x2, int y2)
{
int numpixels;
int i;
int deltax, deltay;
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);
if (deltax == 0)
{
/* DEBUGF("lcd_drawline() called for vertical line - optimisation.\n"); */
lcd_vline(x1, y1, y2);
return;
}
deltay = abs(y2 - y1);
if (deltay == 0)
{
/* DEBUGF("lcd_drawline() called for horizontal line - optimisation.\n"); */
lcd_hline(x1, x2, y1);
return;
}
xinc2 = 1;
yinc2 = 1;
if (deltax >= deltay)
{
numpixels = deltax;
d = 2 * deltay - deltax;
dinc1 = deltay * 2;
dinc2 = (deltay - deltax) * 2;
xinc1 = 1;
yinc1 = 0;
}
else
{
numpixels = deltay;
d = 2 * deltax - deltay;
dinc1 = deltax * 2;
dinc2 = (deltax - deltay) * 2;
xinc1 = 0;
yinc1 = 1;
}
numpixels++; /* include endpoints */
if (x1 > x2)
{
xinc1 = -xinc1;
xinc2 = -xinc2;
}
if (y1 > y2)
{
yinc1 = -yinc1;
yinc2 = -yinc2;
}
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 (x >= 0 && y >= 0 && x < w_vp && y < h_vp)
pfunc(x + x_vp, y + y_vp);
if (d < 0)
{
d += dinc1;
x += xinc1;
y += yinc1;
}
else
{
d += dinc2;
x += xinc2;
y += yinc2;
}
}
}
/* Draw a horizontal line (optimised) */
void lcd_hline(int x1, int x2, int y)
{
@ -567,21 +471,6 @@ void lcd_vline(int x, int y1, int y2)
bfunc(dst, mask, 0xFFu);
}
/* Draw a rectangular box */
void lcd_drawrect(int x, int y, int width, int height)
{
if ((width <= 0) || (height <= 0))
return;
int x2 = x + width - 1;
int y2 = y + height - 1;
lcd_vline(x, y, y2);
lcd_vline(x2, y, y2);
lcd_hline(x, x2, y);
lcd_hline(x, x2, y2);
}
/* Fill a rectangular area */
void lcd_fillrect(int x, int y, int width, int height)
{

View file

@ -452,102 +452,6 @@ void LCDFN(clear_viewport)(void)
CURRENT_VP->flags &= ~(VP_FLAG_VP_SET_CLEAN);
}
/* Set a single pixel */
void LCDFN(drawpixel)(int x, int y)
{
if (LCDFN(clip_viewport_pixel)(&x, &y))
LCDFN(pixelfuncs)[CURRENT_VP->drawmode](x, y);
}
/* Draw a line */
void LCDFN(drawline)(int x1, int y1, int x2, int y2)
{
int numpixels;
int i;
int deltax, deltay;
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);
if (deltax == 0)
{
/* DEBUGF(LCDNAME "drawline() called for vertical line - optimisation.\n"); */
LCDFN(vline)(x1, y1, y2);
return;
}
deltay = abs(y2 - y1);
if (deltay == 0)
{
/* DEBUGF(LCDNAME "drawline() called for horizontal line - optimisation.\n"); */
LCDFN(hline)(x1, x2, y1);
return;
}
xinc2 = 1;
yinc2 = 1;
if (deltax >= deltay)
{
numpixels = deltax;
d = 2 * deltay - deltax;
dinc1 = deltay * 2;
dinc2 = (deltay - deltax) * 2;
xinc1 = 1;
yinc1 = 0;
}
else
{
numpixels = deltay;
d = 2 * deltax - deltay;
dinc1 = deltax * 2;
dinc2 = (deltax - deltay) * 2;
xinc1 = 0;
yinc1 = 1;
}
numpixels++; /* include endpoints */
if (x1 > x2)
{
xinc1 = -xinc1;
xinc2 = -xinc2;
}
if (y1 > y2)
{
yinc1 = -yinc1;
yinc2 = -yinc2;
}
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 (x >= 0 && y >= 0 && x < w_vp && y < h_vp)
pfunc(x + x_vp, y + y_vp);
if (d < 0)
{
d += dinc1;
x += xinc1;
y += yinc1;
}
else
{
d += dinc2;
x += xinc2;
y += yinc2;
}
}
}
/* Draw a horizontal line (optimised) */
void LCDFN(hline)(int x1, int x2, int y)
{
@ -602,21 +506,6 @@ void LCDFN(vline)(int x, int y1, int y2)
bfunc(dst, mask, 0xFFFFu);
}
/* Draw a rectangular box */
void LCDFN(drawrect)(int x, int y, int width, int height)
{
if ((width <= 0) || (height <= 0))
return;
int x2 = x + width - 1;
int y2 = y + height - 1;
LCDFN(vline)(x, y, y2);
LCDFN(vline)(x2, y, y2);
LCDFN(hline)(x, x2, y);
LCDFN(hline)(x, x2, y2);
}
/* Fill a rectangular area */
void LCDFN(fillrect)(int x, int y, int width, int height)
{

View file

@ -67,7 +67,8 @@ static bool LCDFN(clip_viewport_pixel)(int *x, int *y)
{
struct viewport *vp = LCDFN(current_viewport);
if(*x >= vp->width || *y >= vp->height)
if (*x < 0 || *x >= vp->width ||
*y < 0 || *y >= vp->height)
return false;
*x += vp->x;
@ -805,3 +806,128 @@ void LCDFN(nine_segment_bmp)(const struct bitmap* bm, int x, int y,
LCDFN(bmp_part)(bm, bm->width - corner_w, bm->width - corner_h,
width - corner_w, height - corner_h, corner_w, corner_h);
}
void LCDFN(drawpixel)(int x, int y)
{
struct viewport *vp = LCDFN(current_viewport);
if (LCDFN(clip_viewport_pixel(&x, &y)))
{
#if LCDM(DEPTH) >= 8
LCDFN(fastpixelfunc_type) *pfunc = LCDFN(fastpixelfuncs)[vp->drawmode];
void *(*fbaddr)(int x, int y) = vp->buffer->get_address_fn;
pfunc(fbaddr(x, y));
#else
LCDFN(pixelfunc_type) *pfunc = LCDFN(pixelfuncs)[vp->drawmode];
pfunc(x, y);
#endif
}
}
void LCDFN(drawline)(int x1, int y1, int x2, int y2)
{
struct viewport *vp = LCDFN(current_viewport);
int numpixels;
int i;
int deltax, deltay;
int d, dinc1, dinc2;
int x, xinc1, xinc2;
int y, yinc1, yinc2;
deltay = abs(y2 - y1);
if (deltay == 0)
{
lcd_hline(x1, x2, y1);
return;
}
deltax = abs(x2 - x1);
if (deltax == 0)
{
lcd_vline(x1, y1, y2);
return;
}
xinc2 = 1;
yinc2 = 1;
if (deltax >= deltay)
{
numpixels = deltax;
d = 2 * deltay - deltax;
dinc1 = deltay * 2;
dinc2 = (deltay - deltax) * 2;
xinc1 = 1;
yinc1 = 0;
}
else
{
numpixels = deltay;
d = 2 * deltax - deltay;
dinc1 = deltax * 2;
dinc2 = (deltax - deltay) * 2;
xinc1 = 0;
yinc1 = 1;
}
numpixels++; /* include endpoints */
if (x1 > x2)
{
xinc1 = -xinc1;
xinc2 = -xinc2;
}
if (y1 > y2)
{
yinc1 = -yinc1;
yinc2 = -yinc2;
}
x = x1;
y = y1;
#if LCDM(DEPTH) >= 8
LCDFN(fastpixelfunc_type) *pfunc = LCDFN(fastpixelfuncs)[vp->drawmode];
void *(*fbaddr)(int x, int y) = vp->buffer->get_address_fn;
#else
LCDFN(pixelfunc_type) *pfunc = LCDFN(pixelfuncs)[vp->drawmode];
#endif
for (i = 0; i < numpixels; i++)
{
if (x >= 0 && y >= 0 && x < vp->width && y < vp->height)
{
#if LCDM(DEPTH) >= 8
pfunc(fbaddr(x + vp->x, y + vp->y));
#else
pfunc(x + vp->x, y + vp->y);
#endif
}
if (d < 0)
{
d += dinc1;
x += xinc1;
y += yinc1;
}
else
{
d += dinc2;
x += xinc2;
y += yinc2;
}
}
}
void LCDFN(drawrect)(int x, int y, int width, int height)
{
if ((width <= 0) || (height <= 0))
return;
int x2 = x + width - 1;
int y2 = y + height - 1;
LCDFN(vline)(x, y, y2);
LCDFN(vline)(x2, y, y2);
LCDFN(hline)(x, x2, y);
LCDFN(hline)(x, x2, y2);
}

View file

@ -40,7 +40,6 @@ 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;
@ -194,119 +193,6 @@ fb_data* lcd_get_backdrop(void)
return lcd_backdrop;
}
/* Set a single pixel */
void lcd_drawpixel(int x, int y)
{
if (lcd_clip_viewport_pixel(&x, &y))
lcd_fastpixelfuncs[lcd_current_viewport->drawmode](FBADDR(x, y));
}
/* Draw a line */
void lcd_drawline(int x1, int y1, int x2, int y2)
{
int numpixels;
int i;
int deltax, deltay;
int d, dinc1, dinc2;
int x, xinc1, xinc2;
int y, yinc1, yinc2;
int x_vp, y_vp, w_vp, h_vp;
lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[lcd_current_viewport->drawmode];
deltay = abs(y2 - y1);
if (deltay == 0)
{
/* DEBUGF("lcd_drawline() called for horizontal line - optimisation.\n"); */
lcd_hline(x1, x2, y1);
return;
}
deltax = abs(x2 - x1);
if (deltax == 0)
{
/* DEBUGF("lcd_drawline() called for vertical line - optimisation.\n"); */
lcd_vline(x1, y1, y2);
return;
}
xinc2 = 1;
yinc2 = 1;
if (deltax >= deltay)
{
numpixels = deltax;
d = 2 * deltay - deltax;
dinc1 = deltay * 2;
dinc2 = (deltay - deltax) * 2;
xinc1 = 1;
yinc1 = 0;
}
else
{
numpixels = deltay;
d = 2 * deltax - deltay;
dinc1 = deltax * 2;
dinc2 = (deltax - deltay) * 2;
xinc1 = 0;
yinc1 = 1;
}
numpixels++; /* include endpoints */
if (x1 > x2)
{
xinc1 = -xinc1;
xinc2 = -xinc2;
}
if (y1 > y2)
{
yinc1 = -yinc1;
yinc2 = -yinc2;
}
x = x1;
y = y1;
void *(*fbaddr)(int x, int y) = FB_CURRENTVP_BUFFER->get_address_fn;
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 (x >= 0 && y >= 0 && x < w_vp && y < h_vp)
pfunc(fbaddr(x + x_vp, y + y_vp));
if (d < 0)
{
d += dinc1;
x += xinc1;
y += yinc1;
}
else
{
d += dinc2;
x += xinc2;
y += yinc2;
}
}
}
/* Draw a rectangular box */
void lcd_drawrect(int x, int y, int width, int height)
{
if ((width <= 0) || (height <= 0))
return;
int x2 = x + width - 1;
int y2 = y + height - 1;
lcd_vline(x, y, y2);
lcd_vline(x2, y, y2);
lcd_hline(x, x2, y);
lcd_hline(x, x2, y2);
}
/* Draw a full native bitmap */
void lcd_bitmap(const fb_data *src, int x, int y, int width, int height)
{