From 49fff2d9b47a51e0f2f600602aaa2fddc234b4be Mon Sep 17 00:00:00 2001 From: caternuson Date: Thu, 9 Apr 2020 08:37:07 -0700 Subject: [PATCH 1/3] initial working fill --- shared-bindings/displayio/Bitmap.c | 19 ++++++++++++++++++ shared-bindings/displayio/Bitmap.h | 1 + shared-module/displayio/Bitmap.c | 32 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/shared-bindings/displayio/Bitmap.c b/shared-bindings/displayio/Bitmap.c index 91c17f2d1342..48d04c2157c3 100644 --- a/shared-bindings/displayio/Bitmap.c +++ b/shared-bindings/displayio/Bitmap.c @@ -178,9 +178,28 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val return mp_const_none; } +//| .. method:: fill() +//| +//| Fills the bitmap. +//| +STATIC mp_obj_t displayio_bitmap_obj_fill(mp_obj_t self_in, mp_obj_t value_obj) { + displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in); + + mp_int_t value = mp_obj_get_int(value_obj); + if (value >= 1 << common_hal_displayio_bitmap_get_bits_per_value(self)) { + mp_raise_ValueError(translate("pixel value requires too many bits")); + } + common_hal_displayio_bitmap_fill(self, value); + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(displayio_bitmap_fill_obj, displayio_bitmap_obj_fill); + STATIC const mp_rom_map_elem_t displayio_bitmap_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_bitmap_height_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_bitmap_width_obj) }, + { MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&displayio_bitmap_fill_obj) }, + }; STATIC MP_DEFINE_CONST_DICT(displayio_bitmap_locals_dict, displayio_bitmap_locals_dict_table); diff --git a/shared-bindings/displayio/Bitmap.h b/shared-bindings/displayio/Bitmap.h index 90694951fa4b..46c337329286 100644 --- a/shared-bindings/displayio/Bitmap.h +++ b/shared-bindings/displayio/Bitmap.h @@ -41,5 +41,6 @@ uint16_t common_hal_displayio_bitmap_get_width(displayio_bitmap_t *self); uint32_t common_hal_displayio_bitmap_get_bits_per_value(displayio_bitmap_t *self); void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y, uint32_t value); uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *bitmap, int16_t x, int16_t y); +void common_hal_displayio_bitmap_fill(displayio_bitmap_t *bitmap, uint32_t value); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_BITMAP_H diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c index 59971d25cc87..2b0165f2f718 100644 --- a/shared-module/displayio/Bitmap.c +++ b/shared-module/displayio/Bitmap.c @@ -162,3 +162,35 @@ void displayio_bitmap_finish_refresh(displayio_bitmap_t *self) { self->dirty_area.x1 = 0; self->dirty_area.x2 = 0; } + +void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value) { + + for (uint32_t x=0; xwidth; x++) { + for (uint32_t y=0; yheight; y++) { + int32_t row_start = y * self->stride; + uint32_t bytes_per_value = self->bits_per_value / 8; + if (bytes_per_value < 1) { + uint32_t bit_position = (sizeof(size_t) * 8 - ((x & self->x_mask) + 1) * self->bits_per_value); + uint32_t index = row_start + (x >> self->x_shift); + uint32_t word = self->data[index]; + word &= ~(self->bitmask << bit_position); + word |= (value & self->bitmask) << bit_position; + self->data[index] = word; + } else { + size_t* row = self->data + row_start; + if (bytes_per_value == 1) { + ((uint8_t*) row)[x] = value; + } else if (bytes_per_value == 2) { + ((uint16_t*) row)[x] = value; + } else if (bytes_per_value == 4) { + ((uint32_t*) row)[x] = value; + } + } + } + } + + self->dirty_area.x1 = 0; + self->dirty_area.x2 = self->width; + self->dirty_area.y1 = 0; + self->dirty_area.y2 = self->height; +} From dc7574684297cd3812fdb82967d0dfa7443af027 Mon Sep 17 00:00:00 2001 From: caternuson Date: Thu, 9 Apr 2020 08:59:26 -0700 Subject: [PATCH 2/3] add docstring, clean up --- shared-bindings/displayio/Bitmap.c | 4 ++-- shared-module/displayio/Bitmap.c | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/shared-bindings/displayio/Bitmap.c b/shared-bindings/displayio/Bitmap.c index 48d04c2157c3..391f3e595562 100644 --- a/shared-bindings/displayio/Bitmap.c +++ b/shared-bindings/displayio/Bitmap.c @@ -178,9 +178,9 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val return mp_const_none; } -//| .. method:: fill() +//| .. method:: fill(value) //| -//| Fills the bitmap. +//| Fills the bitmap with the supplied palette index value. //| STATIC mp_obj_t displayio_bitmap_obj_fill(mp_obj_t self_in, mp_obj_t value_obj) { displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c index 2b0165f2f718..503c3607305f 100644 --- a/shared-module/displayio/Bitmap.c +++ b/shared-module/displayio/Bitmap.c @@ -164,11 +164,21 @@ void displayio_bitmap_finish_refresh(displayio_bitmap_t *self) { } void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value) { + if (self->read_only) { + mp_raise_RuntimeError(translate("Read-only object")); + } + // Update the dirty area. + self->dirty_area.x1 = 0; + self->dirty_area.x2 = self->width; + self->dirty_area.y1 = 0; + self->dirty_area.y2 = self->height; + // Update our data + int32_t row_start; + uint32_t bytes_per_value = self->bits_per_value / 8; for (uint32_t x=0; xwidth; x++) { for (uint32_t y=0; yheight; y++) { - int32_t row_start = y * self->stride; - uint32_t bytes_per_value = self->bits_per_value / 8; + row_start = y * self->stride; if (bytes_per_value < 1) { uint32_t bit_position = (sizeof(size_t) * 8 - ((x & self->x_mask) + 1) * self->bits_per_value); uint32_t index = row_start + (x >> self->x_shift); @@ -188,9 +198,4 @@ void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value) } } } - - self->dirty_area.x1 = 0; - self->dirty_area.x2 = self->width; - self->dirty_area.y1 = 0; - self->dirty_area.y2 = self->height; } From a9fb34eb93ff19df1bcf6498d492a31fd00c2c96 Mon Sep 17 00:00:00 2001 From: caternuson Date: Mon, 13 Apr 2020 16:48:27 -0700 Subject: [PATCH 3/3] make packed word and copy it in --- shared-module/displayio/Bitmap.c | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c index 503c3607305f..8bcda6086fc9 100644 --- a/shared-module/displayio/Bitmap.c +++ b/shared-module/displayio/Bitmap.c @@ -173,29 +173,13 @@ void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value) self->dirty_area.y1 = 0; self->dirty_area.y2 = self->height; - // Update our data - int32_t row_start; - uint32_t bytes_per_value = self->bits_per_value / 8; - for (uint32_t x=0; xwidth; x++) { - for (uint32_t y=0; yheight; y++) { - row_start = y * self->stride; - if (bytes_per_value < 1) { - uint32_t bit_position = (sizeof(size_t) * 8 - ((x & self->x_mask) + 1) * self->bits_per_value); - uint32_t index = row_start + (x >> self->x_shift); - uint32_t word = self->data[index]; - word &= ~(self->bitmask << bit_position); - word |= (value & self->bitmask) << bit_position; - self->data[index] = word; - } else { - size_t* row = self->data + row_start; - if (bytes_per_value == 1) { - ((uint8_t*) row)[x] = value; - } else if (bytes_per_value == 2) { - ((uint16_t*) row)[x] = value; - } else if (bytes_per_value == 4) { - ((uint32_t*) row)[x] = value; - } - } - } + // build the packed word + uint32_t word = 0; + for (uint8_t i=0; i<32 / self->bits_per_value; i++) { + word |= (value & self->bitmask) << (32 - ((i+1)*self->bits_per_value)); + } + // copy it in + for (uint32_t i=0; istride * self->height; i++) { + self->data[i] = word; } }