Skip to content

Commit f5fd42c

Browse files
committed
displayio: Move bitmap read-only checking to displayio_bitmap_set_dirty_area
This is a modest code savings, but more importantly it reduces boilerplate in bitmap-modifying routines. Callers need only ensure they call displayio_bitmap_set_dirty_area in advance of the bitmap modifications they perform. (note that this assumes that no bitmap operation can enter background tasks. If an operation COULD enter background tasks, it MUST re-dirty the area it touches when it exits, simply by a fresh call to set_dirty_area with the same area as before)
1 parent 36d608a commit f5fd42c

File tree

2 files changed

+11
-34
lines changed

2 files changed

+11
-34
lines changed

shared-module/bitmaptools/__init__.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ void common_hal_bitmaptools_rotozoom(displayio_bitmap_t *self, int16_t ox, int16
9595
// # */
9696

9797

98-
if (self->read_only) {
99-
mp_raise_RuntimeError(translate("Read-only object"));
100-
}
101-
10298
int16_t x,y;
10399

104100
int16_t minx = dest_clip1_x;
@@ -199,6 +195,9 @@ void common_hal_bitmaptools_rotozoom(displayio_bitmap_t *self, int16_t ox, int16
199195
float rowu = startu + miny * duCol;
200196
float rowv = startv + miny * dvCol;
201197

198+
displayio_area_t dirty_area = {minx, miny, maxx, maxy};
199+
displayio_bitmap_set_dirty_area(self, &dirty_area);
200+
202201
for (y = miny; y <= maxy; y++) {
203202
float u = rowu + minx * duRow;
204203
float v = rowv + minx * dvRow;
@@ -236,10 +235,6 @@ void common_hal_bitmaptools_fill_region(displayio_bitmap_t *destination,
236235
//
237236
// input checks should ensure that x1 < x2 and y1 < y2 and are within the bitmap region
238237

239-
if (destination->read_only) {
240-
mp_raise_RuntimeError(translate("Read-only object"));
241-
}
242-
243238
displayio_area_t area = { x1, y1, x2, y2 };
244239
displayio_area_canon(&area);
245240

@@ -262,10 +257,6 @@ void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination,
262257
int16_t x1, int16_t y1,
263258
uint32_t value) {
264259

265-
if (destination->read_only) {
266-
mp_raise_RuntimeError(translate("Read-only object"));
267-
}
268-
269260
//
270261
// adapted from Adafruit_CircuitPython_Display_Shapes.Polygon._line
271262
//
@@ -394,9 +385,8 @@ void common_hal_bitmaptools_arrayblit(displayio_bitmap_t *self, void *data, int
394385
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) {
395386
uint32_t mask = (1 << common_hal_displayio_bitmap_get_bits_per_value(self)) - 1;
396387

397-
if (self->read_only) {
398-
mp_raise_RuntimeError(translate("Read-only object"));
399-
}
388+
displayio_area_t a = {0, 0, self->width, self->height};
389+
displayio_bitmap_set_dirty_area(self, &a);
400390

401391
size_t elements_per_row = (self->width * bits_per_pixel + element_size * 8 - 1) / (element_size * 8);
402392
size_t rowsize = element_size * elements_per_row;
@@ -472,7 +462,4 @@ void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, pyb_file_obj_t *f
472462
displayio_bitmap_write_pixel(self, x, y, value & mask);
473463
}
474464
}
475-
476-
displayio_area_t a = {0, 0, self->width, self->height};
477-
displayio_bitmap_set_dirty_area(self, &a);
478465
}

shared-module/displayio/Bitmap.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t
106106
}
107107

108108
void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *dirty_area) {
109+
if (self->read_only) {
110+
mp_raise_RuntimeError(translate("Read-only object"));
111+
}
112+
109113
displayio_area_t area = *dirty_area;
110114
displayio_area_canon(&area);
111115
displayio_area_union(&area, &self->dirty_area, &area);
@@ -146,10 +150,6 @@ void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16
146150
// If skip_value is `None`, then all pixels are copied.
147151
// This function assumes input checks were performed for pixel index entries.
148152

149-
if (self->read_only) {
150-
mp_raise_RuntimeError(translate("Read-only object"));
151-
}
152-
153153
// Update the dirty area
154154
int16_t dirty_x_max = (x + (x2 - x1));
155155
if (dirty_x_max > self->width) {
@@ -198,10 +198,6 @@ void common_hal_displayio_bitmap_blit(displayio_bitmap_t *self, int16_t x, int16
198198
}
199199

200200
void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) {
201-
if (self->read_only) {
202-
mp_raise_RuntimeError(translate("Read-only object"));
203-
}
204-
205201
// update the dirty region
206202
displayio_area_t a = {x, y, x + 1, y + 1};
207203
displayio_bitmap_set_dirty_area(self, &a);
@@ -225,14 +221,8 @@ void displayio_bitmap_finish_refresh(displayio_bitmap_t *self) {
225221
}
226222

227223
void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value) {
228-
if (self->read_only) {
229-
mp_raise_RuntimeError(translate("Read-only object"));
230-
}
231-
// Update the dirty area.
232-
self->dirty_area.x1 = 0;
233-
self->dirty_area.x2 = self->width;
234-
self->dirty_area.y1 = 0;
235-
self->dirty_area.y2 = self->height;
224+
displayio_area_t a = {0, 0, self->width, self->height};
225+
displayio_bitmap_set_dirty_area(self, &a);
236226

237227
// build the packed word
238228
uint32_t word = 0;

0 commit comments

Comments
 (0)