From 36d608aa67229653c0be12fab407533d0d8b7d32 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 18 Mar 2021 09:09:29 -0500 Subject: [PATCH] displayio_bitmap_set_dirty_area: rewrite in terms of displayio_area .. simplifying code in the process. For instance, now fill_region uses area routines to order and constrain its coordinates. Happily, this change also frees a modest amount of code space. --- shared-module/bitmaptools/__init__.c | 42 +++++++++---------------- shared-module/displayio/Bitmap.c | 47 ++++++---------------------- shared-module/displayio/Bitmap.h | 2 +- 3 files changed, 26 insertions(+), 65 deletions(-) diff --git a/shared-module/bitmaptools/__init__.c b/shared-module/bitmaptools/__init__.c index 73c8e2f909..604c1a16a1 100644 --- a/shared-module/bitmaptools/__init__.c +++ b/shared-module/bitmaptools/__init__.c @@ -240,30 +240,18 @@ void common_hal_bitmaptools_fill_region(displayio_bitmap_t *destination, mp_raise_RuntimeError(translate("Read-only object")); } - // Ensure x1 < x2 and y1 < y2 - if (x1 > x2) { - int16_t temp = x2; - x2 = x1; - x1 = temp; - } - if (y1 > y2) { - int16_t temp = y2; - y2 = y1; - y1 = temp; - } + displayio_area_t area = { x1, y1, x2, y2 }; + displayio_area_canon(&area); - // constrain to bitmap dimensions - x1 = constrain(x1, 0, destination->width); - x2 = constrain(x2, 0, destination->width); - y1 = constrain(y1, 0, destination->height); - y2 = constrain(y2, 0, destination->height); + displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height }; + displayio_area_compute_overlap(&area, &bitmap_area, &area); // update the dirty rectangle - displayio_bitmap_set_dirty_area(destination, x1, y1, x2, y2); + displayio_bitmap_set_dirty_area(destination, &area); int16_t x, y; - for (x = x1; x < x2; x++) { - for (y = y1; y < y2; y++) { + for (x = area.x1; x < area.x2; x++) { + for (y = area.y1; y < area.y2; y++) { displayio_bitmap_write_pixel(destination, x, y, value); } } @@ -298,13 +286,11 @@ void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination, ybb0 = y1; ybb1 = y0 + 1; } + displayio_area_t area = { xbb0, ybb0, xbb1, ybb1 }; + displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height }; + displayio_area_compute_overlap(&area, &bitmap_area, &area); - xbb0 = constrain(xbb0, 0, destination->width); - xbb1 = constrain(xbb1, 0, destination->width); - ybb0 = constrain(ybb0, 0, destination->height); - ybb1 = constrain(ybb1, 0, destination->height); - - displayio_bitmap_set_dirty_area(destination, xbb0, ybb0, xbb1, ybb1); + displayio_bitmap_set_dirty_area(destination, &area); int16_t temp, x, y; @@ -401,7 +387,8 @@ void common_hal_bitmaptools_arrayblit(displayio_bitmap_t *self, void *data, int } } } - displayio_bitmap_set_dirty_area(self, x1, y1, x2, y2); + displayio_area_t area = { x1, y1, x2, y2 }; + displayio_bitmap_set_dirty_area(self, &area); } void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, pyb_file_obj_t *file, int element_size, int bits_per_pixel, bool reverse_pixels_in_element, bool swap_bytes) { @@ -486,5 +473,6 @@ void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, pyb_file_obj_t *f } } - displayio_bitmap_set_dirty_area(self, 0, 0, self->width, self->height); + displayio_area_t a = {0, 0, self->width, self->height}; + displayio_bitmap_set_dirty_area(self, &a); } diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c index f03ade84b1..d93338dc43 100644 --- a/shared-module/displayio/Bitmap.c +++ b/shared-module/displayio/Bitmap.c @@ -105,41 +105,12 @@ uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t return 0; } -void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, int16_t x1, int16_t y1, int16_t x2, int16_t y2) { - // Update the bitmap's dirty region with the rectangle bounded by (x1,y1) and (x2, y2) - - // Arrange x1 < x2, y1 < y2 - if (x1 > x2) { - int16_t temp = x1; - x1 = x2; - x2 = temp; - } - if (y1 > y2) { - int16_t temp = y1; - y1 = y2; - y2 = temp; - } - - // Update the dirty area. - if (self->dirty_area.x1 == self->dirty_area.x2) { - self->dirty_area.x1 = x1; - self->dirty_area.x2 = x2; - self->dirty_area.y1 = y1; - self->dirty_area.y2 = y2; - } else { - if (x1 < self->dirty_area.x1) { - self->dirty_area.x1 = x1; - } - if (x2 > self->dirty_area.x2) { - self->dirty_area.x2 = x2; - } - if (y1 < self->dirty_area.y1) { - self->dirty_area.y1 = y1; - } - if (y2 > self->dirty_area.y2) { - self->dirty_area.y2 = y2; - } - } +void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *dirty_area) { + displayio_area_t area = *dirty_area; + displayio_area_canon(&area); + displayio_area_union(&area, &self->dirty_area, &area); + displayio_area_t bitmap_area = {0, 0, self->width, self->height}; + displayio_area_compute_overlap(&area, &bitmap_area, &self->dirty_area); } void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) { @@ -189,7 +160,8 @@ void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16 dirty_y_max = self->height; } - displayio_bitmap_set_dirty_area(self, x, y, dirty_x_max, dirty_y_max); + displayio_area_t a = { x, y, dirty_x_max, dirty_y_max}; + displayio_bitmap_set_dirty_area(self, &a); bool x_reverse = false; bool y_reverse = false; @@ -231,7 +203,8 @@ void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x, } // update the dirty region - displayio_bitmap_set_dirty_area(self, x, y, x + 1, y + 1); + displayio_area_t a = {x, y, x + 1, y + 1}; + displayio_bitmap_set_dirty_area(self, &a); // write the pixel displayio_bitmap_write_pixel(self, x, y, value); diff --git a/shared-module/displayio/Bitmap.h b/shared-module/displayio/Bitmap.h index c1ce9612b1..82a3de631a 100644 --- a/shared-module/displayio/Bitmap.h +++ b/shared-module/displayio/Bitmap.h @@ -49,7 +49,7 @@ typedef struct { void displayio_bitmap_finish_refresh(displayio_bitmap_t *self); displayio_area_t *displayio_bitmap_get_refresh_areas(displayio_bitmap_t *self, displayio_area_t *tail); -void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, int16_t x1, int16_t y1, int16_t x2, int16_t y2); +void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *area); void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value); #endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_BITMAP_H