From f7714649eeb1b8be97099e848a800a4dea941867 Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Fri, 4 Sep 2020 14:15:15 -0500 Subject: [PATCH] Add dirty rectangle tracking to Shape display element --- shared-module/displayio/Shape.c | 74 ++++++++++++++++++++++++++++++ shared-module/displayio/Shape.h | 5 ++ shared-module/displayio/TileGrid.c | 8 +++- 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/shared-module/displayio/Shape.c b/shared-module/displayio/Shape.c index ab9ca735bc..b8412ed9a3 100644 --- a/shared-module/displayio/Shape.c +++ b/shared-module/displayio/Shape.c @@ -49,10 +49,16 @@ void common_hal_displayio_shape_construct(displayio_shape_t *self, uint32_t widt self->half_height = height; self->data = m_malloc(height * sizeof(uint32_t), false); + //for (uint16_t i = 0; i < height; i++) { for (uint16_t i = 0; i <= height; i++) { self->data[2 * i] = 0; self->data[2 * i + 1] = width; } + + self->dirty_area.x1=0; + self->dirty_area.x2=width; + self->dirty_area.y1=0; + self->dirty_area.y2=height; } void common_hal_displayio_shape_set_boundary(displayio_shape_t *self, uint16_t y, uint16_t start_x, uint16_t end_x) { @@ -66,8 +72,58 @@ void common_hal_displayio_shape_set_boundary(displayio_shape_t *self, uint16_t y if (self->mirror_x && (start_x > half_width || end_x > half_width)) { mp_raise_ValueError_varg(translate("Maximum x value when mirrored is %d"), half_width); } + + uint16_t lower_x, upper_x; + + // find x-boundaries for updating based on current data and start_x, end_x + if (start_x < self->data[2 * y]) { + lower_x = start_x; + } else { + lower_x = self->data[2 * y]; + } + + if (self->mirror_x) { + upper_x = self->width-lower_x; + } else { + if (end_x > self->data[2 * y + 1]) { + upper_x = end_x + 1; + } else { + upper_x = self->data[2 * y + 1] + 1; + } + } + self->data[2 * y] = start_x; self->data[2 * y + 1] = end_x; + + if (self->dirty_area.x1 == self->dirty_area.x2) { // Dirty region is empty + self->dirty_area.x1=lower_x; + self->dirty_area.x2=upper_x; + self->dirty_area.y1 = y; + if (self->mirror_y) { + self->dirty_area.y2 = self->height-y; + } else { + self->dirty_area.y2 = y+1; + } + } else { // Dirty region is not empty + if (lower_x < self->dirty_area.x1) { + self->dirty_area.x1 = lower_x; + } + if (upper_x > self->dirty_area.x2) { + self->dirty_area.x2 = upper_x; + } + if (y < self->dirty_area.y1) { + self->dirty_area.y1=y; + if (self->mirror_y) { // if y is mirrored and the lower y was updated, the upper y must be updated too + self->dirty_area.y2=self->height-y; + } + } + else { + if ( !self->mirror_y && (y >= self->dirty_area.y2) ) { // y is not mirrored + self->dirty_area.y2=y+1; + } + } + } + } uint32_t common_hal_displayio_shape_get_pixel(void *obj, int16_t x, int16_t y) { @@ -88,3 +144,21 @@ uint32_t common_hal_displayio_shape_get_pixel(void *obj, int16_t x, int16_t y) { } return 1; } + +displayio_area_t* displayio_shape_get_refresh_areas(displayio_shape_t *self, displayio_area_t* tail) { + if (self->dirty_area.x1 == self->dirty_area.x2) { + return tail; + } + self->dirty_area.next = tail; + return &self->dirty_area; +} + +void displayio_shape_finish_refresh(displayio_shape_t *self) { + self->dirty_area.x1 = 0; + self->dirty_area.x2 = 0; +} + + + + + diff --git a/shared-module/displayio/Shape.h b/shared-module/displayio/Shape.h index ca054fe008..e59ad586e7 100644 --- a/shared-module/displayio/Shape.h +++ b/shared-module/displayio/Shape.h @@ -31,6 +31,7 @@ #include #include "py/obj.h" +#include "shared-module/displayio/area.h" typedef struct { mp_obj_base_t base; @@ -41,6 +42,10 @@ typedef struct { uint16_t* data; bool mirror_x; bool mirror_y; + displayio_area_t dirty_area; } displayio_shape_t; +void displayio_shape_finish_refresh(displayio_shape_t *self); +displayio_area_t* displayio_shape_get_refresh_areas(displayio_shape_t *self, displayio_area_t* tail); + #endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SHAPE_H diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/TileGrid.c index 2766cbecdc..e3642107f8 100644 --- a/shared-module/displayio/TileGrid.c +++ b/shared-module/displayio/TileGrid.c @@ -499,7 +499,7 @@ void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self) { if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_bitmap_type)) { displayio_bitmap_finish_refresh(self->bitmap); } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_shape_type)) { - // TODO: Support shape changes. + displayio_shape_finish_refresh(self->bitmap); } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) { // OnDiskBitmap changes will trigger a complete reload so no need to // track changes. @@ -543,6 +543,12 @@ displayio_area_t* displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *sel self->full_change = true; } } + } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_shape_type)) { + displayio_area_t* refresh_area = displayio_shape_get_refresh_areas(self->bitmap, tail); + if (refresh_area != tail) { + displayio_area_copy(refresh_area, &self->dirty_area); + self->partial_change = true; + } } self->full_change = self->full_change ||