Skip to content

Commit f771464

Browse files
committed
Add dirty rectangle tracking to Shape display element
1 parent d224183 commit f771464

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

shared-module/displayio/Shape.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ void common_hal_displayio_shape_construct(displayio_shape_t *self, uint32_t widt
4949
self->half_height = height;
5050

5151
self->data = m_malloc(height * sizeof(uint32_t), false);
52+
//for (uint16_t i = 0; i < height; i++) {
5253
for (uint16_t i = 0; i <= height; i++) {
5354
self->data[2 * i] = 0;
5455
self->data[2 * i + 1] = width;
5556
}
57+
58+
self->dirty_area.x1=0;
59+
self->dirty_area.x2=width;
60+
self->dirty_area.y1=0;
61+
self->dirty_area.y2=height;
5662
}
5763

5864
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
6672
if (self->mirror_x && (start_x > half_width || end_x > half_width)) {
6773
mp_raise_ValueError_varg(translate("Maximum x value when mirrored is %d"), half_width);
6874
}
75+
76+
uint16_t lower_x, upper_x;
77+
78+
// find x-boundaries for updating based on current data and start_x, end_x
79+
if (start_x < self->data[2 * y]) {
80+
lower_x = start_x;
81+
} else {
82+
lower_x = self->data[2 * y];
83+
}
84+
85+
if (self->mirror_x) {
86+
upper_x = self->width-lower_x;
87+
} else {
88+
if (end_x > self->data[2 * y + 1]) {
89+
upper_x = end_x + 1;
90+
} else {
91+
upper_x = self->data[2 * y + 1] + 1;
92+
}
93+
}
94+
6995
self->data[2 * y] = start_x;
7096
self->data[2 * y + 1] = end_x;
97+
98+
if (self->dirty_area.x1 == self->dirty_area.x2) { // Dirty region is empty
99+
self->dirty_area.x1=lower_x;
100+
self->dirty_area.x2=upper_x;
101+
self->dirty_area.y1 = y;
102+
if (self->mirror_y) {
103+
self->dirty_area.y2 = self->height-y;
104+
} else {
105+
self->dirty_area.y2 = y+1;
106+
}
107+
} else { // Dirty region is not empty
108+
if (lower_x < self->dirty_area.x1) {
109+
self->dirty_area.x1 = lower_x;
110+
}
111+
if (upper_x > self->dirty_area.x2) {
112+
self->dirty_area.x2 = upper_x;
113+
}
114+
if (y < self->dirty_area.y1) {
115+
self->dirty_area.y1=y;
116+
if (self->mirror_y) { // if y is mirrored and the lower y was updated, the upper y must be updated too
117+
self->dirty_area.y2=self->height-y;
118+
}
119+
}
120+
else {
121+
if ( !self->mirror_y && (y >= self->dirty_area.y2) ) { // y is not mirrored
122+
self->dirty_area.y2=y+1;
123+
}
124+
}
125+
}
126+
71127
}
72128

73129
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) {
88144
}
89145
return 1;
90146
}
147+
148+
displayio_area_t* displayio_shape_get_refresh_areas(displayio_shape_t *self, displayio_area_t* tail) {
149+
if (self->dirty_area.x1 == self->dirty_area.x2) {
150+
return tail;
151+
}
152+
self->dirty_area.next = tail;
153+
return &self->dirty_area;
154+
}
155+
156+
void displayio_shape_finish_refresh(displayio_shape_t *self) {
157+
self->dirty_area.x1 = 0;
158+
self->dirty_area.x2 = 0;
159+
}
160+
161+
162+
163+
164+

shared-module/displayio/Shape.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <stdint.h>
3232

3333
#include "py/obj.h"
34+
#include "shared-module/displayio/area.h"
3435

3536
typedef struct {
3637
mp_obj_base_t base;
@@ -41,6 +42,10 @@ typedef struct {
4142
uint16_t* data;
4243
bool mirror_x;
4344
bool mirror_y;
45+
displayio_area_t dirty_area;
4446
} displayio_shape_t;
4547

48+
void displayio_shape_finish_refresh(displayio_shape_t *self);
49+
displayio_area_t* displayio_shape_get_refresh_areas(displayio_shape_t *self, displayio_area_t* tail);
50+
4651
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_SHAPE_H

shared-module/displayio/TileGrid.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self) {
499499
if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_bitmap_type)) {
500500
displayio_bitmap_finish_refresh(self->bitmap);
501501
} else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_shape_type)) {
502-
// TODO: Support shape changes.
502+
displayio_shape_finish_refresh(self->bitmap);
503503
} else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) {
504504
// OnDiskBitmap changes will trigger a complete reload so no need to
505505
// track changes.
@@ -543,6 +543,12 @@ displayio_area_t* displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *sel
543543
self->full_change = true;
544544
}
545545
}
546+
} else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_shape_type)) {
547+
displayio_area_t* refresh_area = displayio_shape_get_refresh_areas(self->bitmap, tail);
548+
if (refresh_area != tail) {
549+
displayio_area_copy(refresh_area, &self->dirty_area);
550+
self->partial_change = true;
551+
}
546552
}
547553

548554
self->full_change = self->full_change ||

0 commit comments

Comments
 (0)