summaryrefslogtreecommitdiff
path: root/shared-module/displayio
diff options
context:
space:
mode:
Diffstat (limited to 'shared-module/displayio')
-rw-r--r--shared-module/displayio/Display.c101
-rw-r--r--shared-module/displayio/Display.h38
-rw-r--r--shared-module/displayio/Group.c234
-rw-r--r--shared-module/displayio/Group.h14
-rw-r--r--shared-module/displayio/Palette.c6
-rw-r--r--shared-module/displayio/TileGrid.c388
-rw-r--r--shared-module/displayio/TileGrid.h37
-rw-r--r--shared-module/displayio/__init__.c337
-rw-r--r--shared-module/displayio/__init__.h7
-rw-r--r--shared-module/displayio/area.h73
10 files changed, 982 insertions, 253 deletions
diff --git a/shared-module/displayio/Display.c b/shared-module/displayio/Display.c
index 3b1613974..0ae88300a 100644
--- a/shared-module/displayio/Display.c
+++ b/shared-module/displayio/Display.c
@@ -107,28 +107,26 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
supervisor_start_terminal(width, height);
- // Set the group after initialization otherwise we may send pixels while we delay in
- // initialization.
- self->refresh = true;
- self->current_group = &circuitpython_splash;
-
self->width = width;
self->height = height;
rotation = rotation % 360;
- self->mirror_x = false;
- self->mirror_y = false;
- self->transpose_xy = false;
+ self->transform.x = 0;
+ self->transform.y = 0;
+ self->transform.scale = 1;
+ self->transform.mirror_x = false;
+ self->transform.mirror_y = false;
+ self->transform.transpose_xy = false;
if (rotation == 0 || rotation == 180) {
if (rotation == 180) {
- self->mirror_x = true;
- self->mirror_y = true;
+ self->transform.mirror_x = true;
+ self->transform.mirror_y = true;
}
} else {
- self->transpose_xy = true;
- if (rotation == 90) {
- self->mirror_y = true;
+ self->transform.transpose_xy = true;
+ if (rotation == 270) {
+ self->transform.mirror_y = true;
} else {
- self->mirror_x = true;
+ self->transform.mirror_x = true;
}
}
@@ -148,13 +146,52 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
}
}
}
+
+ self->area.x1 = 0;
+ self->area.y1 = 0;
+ self->area.next = NULL;
+
+ self->transform.dx = 1;
+ self->transform.dy = 1;
+ if (self->transform.transpose_xy) {
+ self->area.x2 = height;
+ self->area.y2 = width;
+ if (self->transform.mirror_x) {
+ self->transform.x = height;
+ self->transform.dx = -1;
+ }
+ if (self->transform.mirror_y) {
+ self->transform.y = width;
+ self->transform.dy = -1;
+ }
+ } else {
+ self->area.x2 = width;
+ self->area.y2 = height;
+ if (self->transform.mirror_x) {
+ self->transform.x = width;
+ self->transform.dx = -1;
+ }
+ if (self->transform.mirror_y) {
+ self->transform.y = height;
+ self->transform.dy = -1;
+ }
+ }
+
+ // Set the group after initialization otherwise we may send pixels while we delay in
+ // initialization.
+ common_hal_displayio_display_show(self, &circuitpython_splash);
}
void common_hal_displayio_display_show(displayio_display_obj_t* self, displayio_group_t* root_group) {
if (root_group == NULL) {
root_group = &circuitpython_splash;
}
+ if (root_group == self->current_group) {
+ return;
+ }
+ displayio_group_update_transform(root_group, &self->transform);
self->current_group = root_group;
+ self->full_refresh = true;
common_hal_displayio_display_refresh_soon(self);
}
@@ -162,6 +199,15 @@ void common_hal_displayio_display_refresh_soon(displayio_display_obj_t* self) {
self->refresh = true;
}
+const displayio_area_t* displayio_display_get_refresh_areas(displayio_display_obj_t *self) {
+ if (self->full_refresh) {
+ self->area.next = NULL;
+ return &self->area;
+ } else {
+ return displayio_group_get_refresh_areas(self->current_group, NULL);
+ }
+}
+
int32_t common_hal_displayio_display_wait_for_frame(displayio_display_obj_t* self) {
uint64_t last_refresh = self->last_refresh;
// Don't try to refresh if we got an exception.
@@ -224,7 +270,6 @@ void displayio_display_end_transaction(displayio_display_obj_t* self) {
}
void displayio_display_set_region_to_update(displayio_display_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
-
self->send(self->bus, true, &self->set_column_command, 1);
bool isCommand = self->data_as_commands;
if (self->single_byte_bounds) {
@@ -255,13 +300,16 @@ void displayio_display_set_region_to_update(displayio_display_obj_t* self, uint1
}
}
-bool displayio_display_frame_queued(displayio_display_obj_t* self) {
- // Refresh at ~30 fps.
- return (ticks_ms - self->last_refresh) > 32;
+void displayio_display_start_refresh(displayio_display_obj_t* self) {
+ self->last_refresh = ticks_ms;
}
-bool displayio_display_refresh_queued(displayio_display_obj_t* self) {
- return self->refresh || (self->current_group != NULL && displayio_group_needs_refresh(self->current_group));
+bool displayio_display_frame_queued(displayio_display_obj_t* self) {
+ if (self->current_group == NULL) {
+ return false;
+ }
+ // Refresh at ~60 fps.
+ return (ticks_ms - self->last_refresh) > 16;
}
void displayio_display_finish_refresh(displayio_display_obj_t* self) {
@@ -269,11 +317,12 @@ void displayio_display_finish_refresh(displayio_display_obj_t* self) {
displayio_group_finish_refresh(self->current_group);
}
self->refresh = false;
+ self->full_refresh = false;
self->last_refresh = ticks_ms;
}
-void displayio_display_send_pixels(displayio_display_obj_t* self, uint32_t* pixels, uint32_t length) {
- self->send(self->bus, false, (uint8_t*) pixels, length * 4);
+void displayio_display_send_pixels(displayio_display_obj_t* self, uint8_t* pixels, uint32_t length) {
+ self->send(self->bus, false, pixels, length);
}
void displayio_display_update_backlight(displayio_display_obj_t* self) {
@@ -298,3 +347,11 @@ void release_display(displayio_display_obj_t* self) {
common_hal_digitalio_digitalinout_deinit(&self->backlight_inout);
}
}
+
+bool displayio_display_fill_area(displayio_display_obj_t *self, displayio_area_t* area, uint32_t* mask, uint32_t *buffer) {
+ return displayio_group_fill_area(self->current_group, area, mask, buffer);
+}
+
+bool displayio_display_clip_area(displayio_display_obj_t *self, const displayio_area_t* area, displayio_area_t* clipped) {
+ return displayio_area_compute_overlap(&self->area, area, clipped);
+}
diff --git a/shared-module/displayio/Display.h b/shared-module/displayio/Display.h
index 52f98a252..fa8902ced 100644
--- a/shared-module/displayio/Display.h
+++ b/shared-module/displayio/Display.h
@@ -31,6 +31,8 @@
#include "shared-bindings/displayio/Group.h"
#include "shared-bindings/pulseio/PWMOut.h"
+#include "shared-module/displayio/area.h"
+
typedef bool (*display_bus_begin_transaction)(mp_obj_t bus);
typedef void (*display_bus_send)(mp_obj_t bus, bool command, uint8_t *data, uint32_t data_length);
typedef void (*display_bus_end_transaction)(mp_obj_t bus);
@@ -38,19 +40,8 @@ typedef void (*display_bus_end_transaction)(mp_obj_t bus);
typedef struct {
mp_obj_base_t base;
mp_obj_t bus;
- uint16_t width;
- uint16_t height;
- uint16_t color_depth;
- uint8_t set_column_command;
- uint8_t set_row_command;
- uint8_t write_ram_command;
displayio_group_t *current_group;
- bool refresh;
uint64_t last_refresh;
- int16_t colstart;
- int16_t rowstart;
- bool single_byte_bounds;
- bool data_as_commands;
display_bus_begin_transaction begin_transaction;
display_bus_send send;
display_bus_end_transaction end_transaction;
@@ -59,14 +50,29 @@ typedef struct {
pulseio_pwmout_obj_t backlight_pwm;
};
uint64_t last_backlight_refresh;
- bool auto_brightness:1;
- bool updating_backlight:1;
- bool mirror_x;
- bool mirror_y;
- bool transpose_xy;
+ displayio_buffer_transform_t transform;
+ displayio_area_t area;
+ uint16_t width;
+ uint16_t height;
+ uint16_t color_depth;
+ int16_t colstart;
+ int16_t rowstart;
+ uint8_t set_column_command;
+ uint8_t set_row_command;
+ uint8_t write_ram_command;
+ bool refresh;
+ bool single_byte_bounds;
+ bool data_as_commands;
+ bool auto_brightness;
+ bool updating_backlight;
+ bool full_refresh; // New group means we need to refresh the whole display.
} displayio_display_obj_t;
+void displayio_display_start_refresh(displayio_display_obj_t* self);
+const displayio_area_t* displayio_display_get_refresh_areas(displayio_display_obj_t *self);
+bool displayio_display_fill_area(displayio_display_obj_t *self, displayio_area_t* area, uint32_t* mask, uint32_t *buffer);
void displayio_display_update_backlight(displayio_display_obj_t* self);
+bool displayio_display_clip_area(displayio_display_obj_t *self, const displayio_area_t* area, displayio_area_t* clipped);
void release_display(displayio_display_obj_t* self);
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_DISPLAY_H
diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/Group.c
index 40f112bf1..15060e87b 100644
--- a/shared-module/displayio/Group.c
+++ b/shared-module/displayio/Group.c
@@ -38,9 +38,85 @@ uint32_t common_hal_displayio_group_get_scale(displayio_group_t* self) {
return self->scale;
}
+bool displayio_group_get_previous_area(displayio_group_t *self, displayio_area_t* area) {
+ bool first = true;
+ for (size_t i = 0; i < self->size; i++) {
+ mp_obj_t layer = self->children[i].native;
+ displayio_area_t layer_area;
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ if (!displayio_tilegrid_get_previous_area(layer, &layer_area)) {
+ continue;
+ }
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ if (!displayio_group_get_previous_area(layer, &layer_area)) {
+ continue;
+ }
+ }
+ if (first) {
+ displayio_area_copy(&layer_area, area);
+ first = false;
+ } else {
+ displayio_area_expand(area, &layer_area);
+ }
+ }
+ if (self->item_removed) {
+ if (first) {
+ displayio_area_copy(&self->dirty_area, area);
+ first = false;
+ } else {
+ displayio_area_expand(area, &self->dirty_area);
+ }
+ }
+ return !first;
+}
+
+static void _update_child_transforms(displayio_group_t* self) {
+ if (!self->in_group) {
+ return;
+ }
+ for (size_t i = 0; i < self->size; i++) {
+ mp_obj_t layer = self->children[i].native;
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ displayio_tilegrid_update_transform(layer, &self->absolute_transform);
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ displayio_group_update_transform(layer, &self->absolute_transform);
+ }
+ }
+}
+
+void displayio_group_update_transform(displayio_group_t *self,
+ const displayio_buffer_transform_t* parent_transform) {
+ self->in_group = parent_transform != NULL;
+ if (self->in_group) {
+ int16_t x = self->x;
+ int16_t y = self->y;
+ if (parent_transform->transpose_xy) {
+ x = y;
+ y = self->x;
+ }
+ self->absolute_transform.x = parent_transform->x + parent_transform->dx * x;
+ self->absolute_transform.y = parent_transform->y + parent_transform->dy * y;
+ self->absolute_transform.dx = parent_transform->dx * self->scale;
+ self->absolute_transform.dy = parent_transform->dy * self->scale;
+ self->absolute_transform.transpose_xy = parent_transform->transpose_xy;
+ self->absolute_transform.mirror_x = parent_transform->mirror_x;
+ self->absolute_transform.mirror_y = parent_transform->mirror_y;
+
+ self->absolute_transform.scale = parent_transform->scale * self->scale;
+ }
+ _update_child_transforms(self);
+}
+
void common_hal_displayio_group_set_scale(displayio_group_t* self, uint32_t scale) {
- self->needs_refresh = self->scale != scale;
+ if (self->scale == scale) {
+ return;
+ }
+ uint8_t parent_scale = self->absolute_transform.scale / self->scale;
+ self->absolute_transform.dx = self->absolute_transform.dx / self->scale * scale;
+ self->absolute_transform.dy = self->absolute_transform.dy / self->scale * scale;
+ self->absolute_transform.scale = parent_scale * scale;
self->scale = scale;
+ _update_child_transforms(self);
}
mp_int_t common_hal_displayio_group_get_x(displayio_group_t* self) {
@@ -48,8 +124,19 @@ mp_int_t common_hal_displayio_group_get_x(displayio_group_t* self) {
}
void common_hal_displayio_group_set_x(displayio_group_t* self, mp_int_t x) {
- self->needs_refresh = self->x != x;
+ if (self->x == x) {
+ return;
+ }
+ if (self->absolute_transform.transpose_xy) {
+ int16_t dy = self->absolute_transform.dy / self->scale;
+ self->absolute_transform.y += dy * (x - self->x);
+ } else {
+ int16_t dx = self->absolute_transform.dx / self->scale;
+ self->absolute_transform.x += dx * (x - self->x);
+ }
+
self->x = x;
+ _update_child_transforms(self);
}
mp_int_t common_hal_displayio_group_get_y(displayio_group_t* self) {
@@ -57,21 +144,75 @@ mp_int_t common_hal_displayio_group_get_y(displayio_group_t* self) {
}
void common_hal_displayio_group_set_y(displayio_group_t* self, mp_int_t y) {
- self->needs_refresh = self->y != y;
+ if (self->y == y) {
+ return;
+ }
+ if (self->absolute_transform.transpose_xy) {
+ int8_t dx = self->absolute_transform.dx / self->scale;
+ self->absolute_transform.x += dx * (y - self->y);
+ } else {
+ int8_t dy = self->absolute_transform.dy / self->scale;
+ self->absolute_transform.y += dy * (y - self->y);
+ }
self->y = y;
+ _update_child_transforms(self);
}
-void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp_obj_t layer) {
- if (self->size == self->max_size) {
- mp_raise_RuntimeError(translate("Group full"));
- }
+static mp_obj_t _add_layer(displayio_group_t* self, mp_obj_t layer) {
mp_obj_t native_layer = mp_instance_cast_to_native_base(layer, &displayio_group_type);
if (native_layer == MP_OBJ_NULL) {
native_layer = mp_instance_cast_to_native_base(layer, &displayio_tilegrid_type);
+ if (native_layer == MP_OBJ_NULL) {
+ mp_raise_ValueError(translate("Layer must be a Group or TileGrid subclass."));
+ }
+ displayio_tilegrid_t* tilegrid = native_layer;
+ if (tilegrid->in_group) {
+ mp_raise_ValueError(translate("Layer already in a group."));
+ } else {
+ tilegrid->in_group = true;
+ }
+ displayio_tilegrid_update_transform(tilegrid, &self->absolute_transform);
+ } else {
+ displayio_group_t* group = native_layer;
+ if (group->in_group) {
+ mp_raise_ValueError(translate("Layer already in a group."));
+ } else {
+ group->in_group = true;
+ }
+ displayio_group_update_transform(group, &self->absolute_transform);
}
- if (native_layer == MP_OBJ_NULL) {
- mp_raise_ValueError(translate("Layer must be a Group or TileGrid subclass."));
+ return native_layer;
+}
+
+static void _remove_layer(displayio_group_t* self, size_t index) {
+ mp_obj_t layer = self->children[index].native;
+ displayio_area_t layer_area;
+ bool rendered_last_frame = false;
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ displayio_tilegrid_t* tilegrid = layer;
+ rendered_last_frame = displayio_tilegrid_get_previous_area(tilegrid, &layer_area);
+ displayio_tilegrid_update_transform(tilegrid, NULL);
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ displayio_group_t* group = layer;
+ rendered_last_frame = displayio_group_get_previous_area(group, &layer_area);
+ displayio_group_update_transform(group, NULL);
+ }
+ if (!rendered_last_frame) {
+ return;
+ }
+ if (!self->item_removed) {
+ displayio_area_copy(&layer_area, &self->dirty_area);
+ } else {
+ displayio_area_expand(&self->dirty_area, &layer_area);
}
+ self->item_removed = true;
+}
+
+void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp_obj_t layer) {
+ if (self->size == self->max_size) {
+ mp_raise_RuntimeError(translate("Group full"));
+ }
+ mp_obj_t native_layer = _add_layer(self, layer);
// Shift everything right.
for (size_t i = self->size; i > index; i--) {
self->children[i] = self->children[i - 1];
@@ -79,19 +220,19 @@ void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp
self->children[index].native = native_layer;
self->children[index].original = layer;
self->size++;
- self->needs_refresh = true;
}
mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index) {
self->size--;
mp_obj_t item = self->children[index].original;
+ _remove_layer(self, index);
+
// Shift everything left.
for (size_t i = index; i < self->size; i++) {
self->children[i] = self->children[i + 1];
}
self->children[self->size].native = NULL;
self->children[self->size].original = NULL;
- self->needs_refresh = true;
return item;
}
@@ -113,16 +254,10 @@ mp_obj_t common_hal_displayio_group_get(displayio_group_t* self, size_t index) {
}
void common_hal_displayio_group_set(displayio_group_t* self, size_t index, mp_obj_t layer) {
- mp_obj_t native_layer = mp_instance_cast_to_native_base(layer, &displayio_group_type);
- if (native_layer == MP_OBJ_NULL) {
- native_layer = mp_instance_cast_to_native_base(layer, &displayio_tilegrid_type);
- }
- if (native_layer == MP_OBJ_NULL) {
- mp_raise_ValueError(translate("Layer must be a Group or TileGrid subclass."));
- }
+ mp_obj_t native_layer = _add_layer(self, layer);
+ _remove_layer(self, index);
self->children[index].native = native_layer;
self->children[index].original = layer;
- self->needs_refresh = true;
}
void displayio_group_construct(displayio_group_t* self, displayio_group_child_t* child_array, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y) {
@@ -130,65 +265,58 @@ void displayio_group_construct(displayio_group_t* self, displayio_group_child_t*
self->y = y;
self->children = child_array;
self->max_size = max_size;
- self->needs_refresh = false;
+ self->item_removed = false;
self->scale = scale;
+ self->in_group = false;
}
-bool displayio_group_get_pixel(displayio_group_t *self, int16_t x, int16_t y, uint16_t* pixel) {
- x -= self->x;
- y -= self->y;
- // When we are scaled we need to substract all but one to ensure -scale to 0 divide down to -1.
- // Normally -scale to scale both divide down to 0 because 0 is unsigned.
- if (x < 0) {
- x -= self->scale - 1;
- }
- if (y < 0) {
- y -= self->scale - 1;
- }
- x /= self->scale;
- y /= self->scale;
+bool displayio_group_fill_area(displayio_group_t *self, const displayio_area_t* area, uint32_t* mask, uint32_t* buffer) {
+ // Track if any of the layers finishes filling in the given area. We can ignore any remaining
+ // layers at that point.
+ bool full_coverage = false;
for (int32_t i = self->size - 1; i >= 0 ; i--) {
mp_obj_t layer = self->children[i].native;
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
- if (displayio_tilegrid_get_pixel(layer, x, y, pixel)) {
- return true;
+ if (displayio_tilegrid_fill_area(layer, area, mask, buffer)) {
+ full_coverage = true;
+ break;
}
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
- if (displayio_group_get_pixel(layer, x, y, pixel)) {
- return true;
+ if (displayio_group_fill_area(layer, area, mask, buffer)) {
+ full_coverage = true;
+ break;
}
}
}
- return false;
+ return full_coverage;
}
-bool displayio_group_needs_refresh(displayio_group_t *self) {
- if (self->needs_refresh) {
- return true;
- }
+void displayio_group_finish_refresh(displayio_group_t *self) {
+ self->item_removed = false;
for (int32_t i = self->size - 1; i >= 0 ; i--) {
mp_obj_t layer = self->children[i].native;
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
- if (displayio_tilegrid_needs_refresh(layer)) {
- return true;
- }
+ displayio_tilegrid_finish_refresh(layer);
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
- if (displayio_group_needs_refresh(layer)) {
- return true;
- }
+ displayio_group_finish_refresh(layer);
}
}
- return false;
}
-void displayio_group_finish_refresh(displayio_group_t *self) {
- self->needs_refresh = false;
+displayio_area_t* displayio_group_get_refresh_areas(displayio_group_t *self, displayio_area_t* tail) {
+ if (self->item_removed) {
+ self->dirty_area.next = tail;
+ tail = &self->dirty_area;
+ }
+
for (int32_t i = self->size - 1; i >= 0 ; i--) {
mp_obj_t layer = self->children[i].native;
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
- displayio_tilegrid_finish_refresh(layer);
+ tail = displayio_tilegrid_get_refresh_areas(layer, tail);
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
- displayio_group_finish_refresh(layer);
+ tail = displayio_group_get_refresh_areas(layer, tail);
}
}
+
+ return tail;
}
diff --git a/shared-module/displayio/Group.h b/shared-module/displayio/Group.h
index 7826b9e66..7ce19a4cd 100644
--- a/shared-module/displayio/Group.h
+++ b/shared-module/displayio/Group.h
@@ -31,6 +31,7 @@
#include <stdint.h>
#include "py/obj.h"
+#include "shared-module/displayio/area.h"
typedef struct {
mp_obj_t native;
@@ -39,18 +40,23 @@ typedef struct {
typedef struct {
mp_obj_base_t base;
+ displayio_group_child_t* children;
int16_t x;
int16_t y;
uint16_t scale;
uint16_t size;
uint16_t max_size;
- displayio_group_child_t* children;
- bool needs_refresh;
+ bool item_removed;
+ bool in_group;
+ displayio_buffer_transform_t absolute_transform;
+ displayio_area_t dirty_area; // Catch all for changed area
} displayio_group_t;
void displayio_group_construct(displayio_group_t* self, displayio_group_child_t* child_array, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y);
-bool displayio_group_get_pixel(displayio_group_t *group, int16_t x, int16_t y, uint16_t *pixel);
-bool displayio_group_needs_refresh(displayio_group_t *self);
+bool displayio_group_get_previous_area(displayio_group_t *group, displayio_area_t* area);
+bool displayio_group_fill_area(displayio_group_t *group, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer);
+void displayio_group_update_transform(displayio_group_t *group, const displayio_buffer_transform_t* parent_transform);
void displayio_group_finish_refresh(displayio_group_t *self);
+displayio_area_t* displayio_group_get_refresh_areas(displayio_group_t *self, displayio_area_t* tail);
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_GROUP_H
diff --git a/shared-module/displayio/Palette.c b/shared-module/displayio/Palette.c
index e810be875..8dc6e766b 100644
--- a/shared-module/displayio/Palette.c
+++ b/shared-module/displayio/Palette.c
@@ -53,7 +53,11 @@ void common_hal_displayio_palette_set_color(displayio_palette_t* self, uint32_t
uint32_t packed = r5 << 11 | g6 << 5 | b5;
// swap bytes
packed = __builtin_bswap16(packed);
- self->colors[palette_index / 2] = masked | packed << shift;
+ uint32_t final_color = masked | packed << shift;
+ if (self->colors[palette_index / 2] == final_color) {
+ return;
+ }
+ self->colors[palette_index / 2] = final_color;
self->needs_refresh = true;
}
diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/TileGrid.c
index 3212dfe8b..97b704c40 100644
--- a/shared-module/displayio/TileGrid.c
+++ b/shared-module/displayio/TileGrid.c
@@ -56,31 +56,123 @@ void common_hal_displayio_tilegrid_construct(displayio_tilegrid_t *self, mp_obj_
self->bitmap_width_in_tiles = bitmap_width_in_tiles;
self->width_in_tiles = width;
self->height_in_tiles = height;
- self->total_width = width * tile_width;
- self->total_height = height * tile_height;
+ self->x = x;
+ self->y = y;
+ self->pixel_width = width * tile_width;
+ self->pixel_height = height * tile_height;
self->tile_width = tile_width;
self->tile_height = tile_height;
self->bitmap = bitmap;
self->pixel_shader = pixel_shader;
- self->x = x;
- self->y = y;
+ self->in_group = false;
+ self->first_draw = true;
+ self->flip_x = false;
+ self->flip_y = false;
+ self->transpose_xy = false;
}
+bool displayio_tilegrid_get_previous_area(displayio_tilegrid_t *self, displayio_area_t* area) {
+ if (self->first_draw) {
+ return false;
+ }
+ displayio_area_copy(&self->previous_area, area);
+ return true;
+}
+
+void _update_current_x(displayio_tilegrid_t *self) {
+ int16_t width;
+ if (self->transpose_xy) {
+ width = self->pixel_height;
+ } else {
+ width = self->pixel_width;
+ }
+ if (self->absolute_transform->transpose_xy) {
+ self->current_area.y1 = self->absolute_transform->y + self->absolute_transform->dy * self->x;
+ self->current_area.y2 = self->absolute_transform->y + self->absolute_transform->dy * (self->x + width);
+ if (self->current_area.y2 < self->current_area.y1) {
+ int16_t temp = self->current_area.y2;
+ self->current_area.y2 = self->current_area.y1;
+ self->current_area.y1 = temp;
+ }
+ } else {
+ self->current_area.x1 = self->absolute_transform->x + self->absolute_transform->dx * self->x;
+ self->current_area.x2 = self->absolute_transform->x + self->absolute_transform->dx * (self->x + width);
+ if (self->current_area.x2 < self->current_area.x1) {
+ int16_t temp = self->current_area.x2;
+ self->current_area.x2 = self->current_area.x1;
+ self->current_area.x1 = temp;
+ }
+ }
+}
+
+void _update_current_y(displayio_tilegrid_t *self) {
+ int16_t height;
+ if (self->transpose_xy) {
+ height = self->pixel_width;
+ } else {
+ height = self->pixel_height;
+ }
+ if (self->absolute_transform->transpose_xy) {
+ self->current_area.x1 = self->absolute_transform->x + self->absolute_transform->dx * self->y;
+ self->current_area.x2 = self->absolute_transform->x + self->absolute_transform->dx * (self->y + height);
+ if (self->current_area.x2 < self->current_area.x1) {
+ int16_t temp = self->current_area.x2;
+ self->current_area.x2 = self->current_area.x1;
+ self->current_area.x1 = temp;
+ }
+ } else {
+ self->current_area.y1 = self->absolute_transform->y + self->absolute_transform->dy * self->y;
+ self->current_area.y2 = self->absolute_transform->y + self->absolute_transform->dy * (self->y + height);
+ if (self->current_area.y2 < self->current_area.y1) {
+ int16_t temp = self->current_area.y2;
+ self->current_area.y2 = self->current_area.y1;
+ self->current_area.y1 = temp;
+ }
+ }
+}
+
+void displayio_tilegrid_update_transform(displayio_tilegrid_t *self,
+ const displayio_buffer_transform_t* absolute_transform) {
+ self->in_group = absolute_transform != NULL;
+ self->absolute_transform = absolute_transform;
+ if (absolute_transform != NULL) {
+ self->moved = !self->first_draw;
+
+ _update_current_x(self);
+ _update_current_y(self);
+ } else {
+ self->first_draw = true;
+ }
+}
mp_int_t common_hal_displayio_tilegrid_get_x(displayio_tilegrid_t *self) {
return self->x;
}
void common_hal_displayio_tilegrid_set_x(displayio_tilegrid_t *self, mp_int_t x) {
- self->needs_refresh = self->x != x;
+ if (self->x == x) {
+ return;
+ }
+
+ self->moved = !self->first_draw;
+
self->x = x;
+ if (self->absolute_transform != NULL) {
+ _update_current_x(self);
+ }
}
mp_int_t common_hal_displayio_tilegrid_get_y(displayio_tilegrid_t *self) {
return self->y;
}
void common_hal_displayio_tilegrid_set_y(displayio_tilegrid_t *self, mp_int_t y) {
- self->needs_refresh = self->y != y;
+ if (self->y == y) {
+ return;
+ }
+ self->moved = !self->first_draw;
self->y = y;
+ if (self->absolute_transform != NULL) {
+ _update_current_y(self);
+ }
}
mp_obj_t common_hal_displayio_tilegrid_get_pixel_shader(displayio_tilegrid_t *self) {
@@ -89,10 +181,9 @@ mp_obj_t common_hal_displayio_tilegrid_get_pixel_shader(displayio_tilegrid_t *se
void common_hal_displayio_tilegrid_set_pixel_shader(displayio_tilegrid_t *self, mp_obj_t pixel_shader) {
self->pixel_shader = pixel_shader;
- self->needs_refresh = true;
+ self->full_change = true;
}
-
uint16_t common_hal_displayio_tilegrid_get_width(displayio_tilegrid_t *self) {
return self->width_in_tiles;
}
@@ -121,21 +212,78 @@ void common_hal_displayio_tilegrid_set_tile(displayio_tilegrid_t *self, uint16_t
return;
}
tiles[y * self->width_in_tiles + x] = tile_index;
- self->needs_refresh = true;
+ displayio_area_t temp_area;
+ displayio_area_t* tile_area;
+ if (!self->partial_change) {
+ tile_area = &self->dirty_area;
+ } else {
+ tile_area = &temp_area;
+ }
+ tile_area->x1 = x * self->tile_width;
+ tile_area->x2 = tile_area->x1 + self->tile_width;
+ tile_area->y1 = y * self->tile_height;
+ tile_area->y2 = tile_area->y1 + self->tile_height;
+ if (self->partial_change) {
+ displayio_area_expand(&self->dirty_area, &temp_area);
+ }
+
+ self->partial_change = true;
+}
+
+bool common_hal_displayio_tilegrid_get_flip_x(displayio_tilegrid_t *self) {
+ return self->flip_x;
+}
+
+void common_hal_displayio_tilegrid_set_flip_x(displayio_tilegrid_t *self, bool flip_x) {
+ if (self->flip_x == flip_x) {
+ return;
+ }
+ self->flip_x = flip_x;
+ self->full_change = true;
+}
+
+bool common_hal_displayio_tilegrid_get_flip_y(displayio_tilegrid_t *self) {
+ return self->flip_y;
+}
+
+void common_hal_displayio_tilegrid_set_flip_y(displayio_tilegrid_t *self, bool flip_y) {
+ if (self->flip_y == flip_y) {
+ return;
+ }
+ self->flip_y = flip_y;
+ self->full_change = true;
}
+bool common_hal_displayio_tilegrid_get_transpose_xy(displayio_tilegrid_t *self) {
+ return self->transpose_xy;
+}
+
+void common_hal_displayio_tilegrid_set_transpose_xy(displayio_tilegrid_t *self, bool transpose_xy) {
+ if (self->transpose_xy == transpose_xy) {
+ return;
+ }
+ self->transpose_xy = transpose_xy;
+
+ // Square TileGrids do not change dimensions when transposed.
+ if (self->pixel_width == self->pixel_height) {
+ self->full_change = true;
+ return;
+ }
+
+ _update_current_x(self);
+ _update_current_y(self);
+
+ self->moved = true;
+}
void common_hal_displayio_tilegrid_set_top_left(displayio_tilegrid_t *self, uint16_t x, uint16_t y) {
self->top_left_x = x;
self->top_left_y = y;
+ self->full_change = true;
}
-bool displayio_tilegrid_get_pixel(displayio_tilegrid_t *self, int16_t x, int16_t y, uint16_t* pixel) {
- x -= self->x;
- y -= self->y;
- if (y < 0 || y >= self->total_height || x >= self->total_width || x < 0) {
- return false;
- }
+bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer) {
+ // If no tiles are present we have no impact.
uint8_t* tiles = self->tiles;
if (self->inline_tiles) {
tiles = (uint8_t*) &self->tiles;
@@ -143,46 +291,143 @@ bool displayio_tilegrid_get_pixel(displayio_tilegrid_t *self, int16_t x, int16_t
if (tiles == NULL) {
return false;
}
- uint16_t tile_location = ((y / self->tile_height + self->top_left_y) % self->height_in_tiles) * self->width_in_tiles + (x / self->tile_width + self->top_left_x) % self->width_in_tiles;
- uint8_t tile = tiles[tile_location];
- uint16_t tile_x = tile_x = (tile % self->bitmap_width_in_tiles) * self->tile_width + x % self->tile_width;
- uint16_t tile_y = tile_y = (tile / self->bitmap_width_in_tiles) * self->tile_height + y % self->tile_height;
- uint32_t value = 0;
- if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_bitmap_type)) {
- value = common_hal_displayio_bitmap_get_pixel(self->bitmap, tile_x, tile_y);
- } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_shape_type)) {
- value = common_hal_displayio_shape_get_pixel(self->bitmap, tile_x, tile_y);
- } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) {
- value = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, tile_x, tile_y);
+ displayio_area_t overlap;
+ if (!displayio_area_compute_overlap(area, &self->current_area, &overlap)) {
+ return false;
}
- if (self->pixel_shader == mp_const_none) {
- *pixel = value;
- return true;
- } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type) && displayio_palette_get_color(self->pixel_shader, value, pixel)) {
- return true;
- } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type) && common_hal_displayio_colorconverter_convert(self->pixel_shader, value, pixel)) {
- return true;
+ int16_t x_stride = 1;
+ int16_t y_stride = displayio_area_width(area);
+
+ bool flip_x = self->flip_x;
+ bool flip_y = self->flip_y;
+ if (self->transpose_xy != self->absolute_transform->transpose_xy) {
+ bool temp_flip = flip_x;
+ flip_x = flip_y;
+ flip_y = temp_flip;
}
- return false;
-}
+ // How many pixels are outside of our area between us and the start of the row.
+ uint16_t start = 0;
+ if ((self->absolute_transform->dx < 0) != flip_x) {
+ start += (area->x2 - area->x1 - 1) * x_stride;
+ x_stride *= -1;
+ }
+ if ((self->absolute_transform->dy < 0) != flip_y) {
+ start += (area->y2 - area->y1 - 1) * y_stride;
+ y_stride *= -1;
+ }
-bool displayio_tilegrid_needs_refresh(displayio_tilegrid_t *self) {
- if (self->needs_refresh) {
- return true;
- } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type)) {
- return displayio_palette_needs_refresh(self->pixel_shader);
- } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type)) {
- return displayio_colorconverter_needs_refresh(self->pixel_shader);
+ // Track if this layer finishes filling in the given area. We can ignore any remaining
+ // layers at that point.
+ bool full_coverage = displayio_area_equal(area, &overlap);
+
+ // TODO(tannewt): Skip coverage tracking if all pixels outside the overlap have already been
+ // set and our palette is all opaque.
+
+ // TODO(tannewt): Check to see if the pixel_shader has any transparency. If it doesn't then we
+ // can either return full coverage or bulk update the mask.
+ displayio_area_t transformed;
+ displayio_area_transform_within(flip_x != (self->absolute_transform->dx < 0), flip_y != (self->absolute_transform->dy < 0), self->transpose_xy != self->absolute_transform->transpose_xy,
+ &overlap,
+ &self->current_area,
+ &transformed);
+
+ int16_t start_x = (transformed.x1 - self->current_area.x1);
+ int16_t end_x = (transformed.x2 - self->current_area.x1);
+ int16_t start_y = (transformed.y1 - self->current_area.y1);
+ int16_t end_y = (transformed.y2 - self->current_area.y1);
+
+ int16_t y_shift = 0;
+ int16_t x_shift = 0;
+ if ((self->absolute_transform->dx < 0) != flip_x) {
+ x_shift = area->x2 - overlap.x2;
+ } else {
+ x_shift = overlap.x1 - area->x1;
+ }
+ if ((self->absolute_transform->dy < 0) != flip_y) {
+ y_shift = area->y2 - overlap.y2;
+ } else {
+ y_shift = overlap.y1 - area->y1;
}
- return false;
+ // This untransposes x and y so it aligns with bitmap rows.
+ if (self->transpose_xy != self->absolute_transform->transpose_xy) {
+ int16_t temp_stride = x_stride;
+ x_stride = y_stride;
+ y_stride = temp_stride;
+ int16_t temp_shift = x_shift;
+ x_shift = y_shift;
+ y_shift = temp_shift;
+ }
+
+ for (int16_t y = start_y; y < end_y; y++) {
+ int16_t row_start = start + (y - start_y + y_shift) * y_stride;
+ int16_t local_y = y / self->absolute_transform->scale;
+ for (int16_t x = start_x; x < end_x; x++) {
+ // Compute the destination pixel in the buffer and mask based on the transformations.
+ int16_t offset = row_start + (x - start_x + x_shift) * x_stride;
+
+ // This is super useful for debugging out range accesses. Uncomment to use.
+ // if (offset < 0 || offset >= (int32_t) displayio_area_size(area)) {
+ // asm("bkpt");
+ // }
+
+ // Check the mask first to see if the pixel has already been set.
+ if ((mask[offset / 32] & (1 << (offset % 32))) != 0) {
+ continue;
+ }
+ int16_t local_x = x / self->absolute_transform->scale;
+ uint16_t tile_location = ((local_y / self->tile_height + self->top_left_y) % self->height_in_tiles) * self->width_in_tiles + (local_x / self->tile_width + self->top_left_x) % self->width_in_tiles;
+ uint8_t tile = tiles[tile_location];
+ uint16_t tile_x = (tile % self->bitmap_width_in_tiles) * self->tile_width + local_x % self->tile_width;
+ uint16_t tile_y = (tile / self->bitmap_width_in_tiles) * self->tile_height + local_y % self->tile_height;
+
+ uint32_t value = 0;
+ // We always want to read bitmap pixels by row first and then transpose into the destination
+ // buffer because most bitmaps are row associated.
+ if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_bitmap_type)) {
+ value = common_hal_displayio_bitmap_get_pixel(self->bitmap, tile_x, tile_y);
+ } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_shape_type)) {
+ value = common_hal_displayio_shape_get_pixel(self->bitmap, tile_x, tile_y);
+ } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) {
+ value = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, tile_x, tile_y);
+ }
+
+ uint16_t* pixel = ((uint16_t*) buffer) + offset;
+ if (self->pixel_shader == mp_const_none) {
+ *pixel = value;
+ return true;
+ } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type)) {
+ if (!displayio_palette_get_color(self->pixel_shader, value, pixel)) {
+ // A pixel is transparent so we haven't fully covered the area ourselves.
+ full_coverage = false;
+ } else {
+ mask[offset / 32] |= 1 << (offset % 32);
+ }
+ } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type)) {
+ if (!common_hal_displayio_colorconverter_convert(self->pixel_shader, value, pixel)) {
+ // A pixel is transparent so we haven't fully covered the area ourselves.
+ full_coverage = false;
+ } else {
+ mask[offset / 32] |= 1 << (offset % 32);
+ }
+ }
+ }
+ }
+ return full_coverage;
}
void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self) {
- self->needs_refresh = false;
+ if (self->moved || self->first_draw) {
+ displayio_area_copy(&self->current_area, &self->previous_area);
+ }
+
+ self->moved = false;
+ self->full_change = false;
+ self->partial_change = false;
+ self->first_draw = false;
if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type)) {
displayio_palette_finish_refresh(self->pixel_shader);
} else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type)) {
@@ -191,3 +436,58 @@ void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self) {
// TODO(tannewt): We could double buffer changes to position and move them over here.
// That way they won't change during a refresh and tear.
}
+
+displayio_area_t* displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *self, displayio_area_t* tail) {
+ if (self->moved && !self->first_draw) {
+ displayio_area_union(&self->previous_area, &self->current_area, &self->dirty_area);
+ if (displayio_area_size(&self->dirty_area) <= 2 * self->pixel_width * self->pixel_height) {
+ self->dirty_area.next = tail;
+ return &self->dirty_area;
+ }
+ self->previous_area.next = tail;
+ self->current_area.next = &self->previous_area;
+ return &self->current_area;
+ }
+
+ // We must recheck if our sources require a refresh because needs_refresh may or may not have
+ // been called.
+ self->full_change = self->full_change ||
+ (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type) &&
+ displayio_palette_needs_refresh(self->pixel_shader)) ||
+ (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type) &&
+ displayio_colorconverter_needs_refresh(self->pixel_shader));
+ if (self->full_change || self->first_draw) {
+ self->current_area.next = tail;
+ return &self->current_area;
+ }
+
+ if (self->partial_change) {
+ if (self->absolute_transform->transpose_xy) {
+ int16_t x1 = self->dirty_area.x1;
+ self->dirty_area.x1 = self->absolute_transform->x + self->absolute_transform->dx * (self->y + self->dirty_area.y1);
+ self->dirty_area.y1 = self->absolute_transform->y + self->absolute_transform->dy * (self->x + x1);
+ int16_t x2 = self->dirty_area.x2;
+ self->dirty_area.x2 = self->absolute_transform->x + self->absolute_transform->dx * (self->y + self->dirty_area.y2);
+ self->dirty_area.y2 = self->absolute_transform->y + self->absolute_transform->dy * (self->x + x2);
+ } else {
+ self->dirty_area.x1 = self->absolute_transform->x + self->absolute_transform->dx * (self->x + self->dirty_area.x1);
+ self->dirty_area.y1 = self->absolute_transform->y + self->absolute_transform->dy * (self->y + self->dirty_area.y1);
+ self->dirty_area.x2 = self->absolute_transform->x + self->absolute_transform->dx * (self->x + self->dirty_area.x2);
+ self->dirty_area.y2 = self->absolute_transform->y + self->absolute_transform->dy * (self->y + self->dirty_area.y2);
+ }
+ if (self->dirty_area.y2 < self->dirty_area.y1) {
+ int16_t temp = self->dirty_area.y2;
+ self->dirty_area.y2 = self->dirty_area.y1;
+ self->dirty_area.y1 = temp;
+ }
+ if (self->dirty_area.x2 < self->dirty_area.x1) {
+ int16_t temp = self->dirty_area.x2;
+ self->dirty_area.x2 = self->dirty_area.x1;
+ self->dirty_area.x1 = temp;
+ }
+
+ self->dirty_area.next = tail;
+ return &self->dirty_area;
+ }
+ return tail;
+}
diff --git a/shared-module/displayio/TileGrid.h b/shared-module/displayio/TileGrid.h
index 59645553d..4d97eccc2 100644
--- a/shared-module/displayio/TileGrid.h
+++ b/shared-module/displayio/TileGrid.h
@@ -31,29 +31,52 @@
#include <stdint.h>
#include "py/obj.h"
+#include "shared-module/displayio/area.h"
typedef struct {
mp_obj_base_t base;
mp_obj_t bitmap;
mp_obj_t pixel_shader;
- uint16_t x;
- uint16_t y;
+ int16_t x;
+ int16_t y;
+ uint16_t pixel_width;
+ uint16_t pixel_height;
uint16_t bitmap_width_in_tiles;
uint16_t width_in_tiles;
uint16_t height_in_tiles;
- uint16_t total_width;
- uint16_t total_height;
uint16_t tile_width;
uint16_t tile_height;
uint16_t top_left_x;
uint16_t top_left_y;
uint8_t* tiles;
- bool needs_refresh;
+ const displayio_buffer_transform_t* absolute_transform;
+ displayio_area_t dirty_area; // Stored as a relative area until the refresh area is fetched.
+ displayio_area_t previous_area; // Stored as an absolute area.
+ displayio_area_t current_area; // Stored as an absolute area so it applies across frames.
+ bool partial_change;
+ bool full_change;
+ bool first_draw;
+ bool moved;
bool inline_tiles;
+ bool in_group;
+ bool flip_x;
+ bool flip_y;
+ bool transpose_xy;
} displayio_tilegrid_t;
-bool displayio_tilegrid_get_pixel(displayio_tilegrid_t *self, int16_t x, int16_t y, uint16_t *pixel);
-bool displayio_tilegrid_needs_refresh(displayio_tilegrid_t *self);
+// Updating the screen is a three stage process.
+
+// The first stage is used to determine i
+displayio_area_t* displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *self, displayio_area_t* tail);
+
+// Area is always in absolute screen coordinates. Update transform is used to inform TileGrids how
+// they relate to it.
+bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer);
+void displayio_tilegrid_update_transform(displayio_tilegrid_t *group, const displayio_buffer_transform_t* parent_transform);
+
+// Fills in area with the maximum bounds of all related pixels in the last rendered frame. Returns
+// false if the tilegrid wasn't rendered in the last frame.
+bool displayio_tilegrid_get_previous_area(displayio_tilegrid_t *self, displayio_area_t* area);
void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self);
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_TILEGRID_H
diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c
index 156640440..b6709e0eb 100644
--- a/shared-module/displayio/__init__.c
+++ b/shared-module/displayio/__init__.c
@@ -1,8 +1,10 @@
#include <string.h>
+
#include "shared-module/displayio/__init__.h"
#include "lib/utils/interrupt_char.h"
+#include "py/gc.h"
#include "py/reload.h"
#include "py/runtime.h"
#include "shared-bindings/board/__init__.h"
@@ -10,6 +12,7 @@
#include "shared-bindings/displayio/Display.h"
#include "shared-bindings/displayio/Group.h"
#include "shared-bindings/displayio/Palette.h"
+#include "shared-module/displayio/area.h"
#include "supervisor/shared/autoreload.h"
#include "supervisor/shared/display.h"
#include "supervisor/memory.h"
@@ -17,14 +20,83 @@
primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT];
-static inline void swap(uint16_t* a, uint16_t* b) {
- uint16_t temp = *a;
- *a = *b;
- *b = temp;
+bool refresh_area(displayio_display_obj_t* display, const displayio_area_t* area) {
+ uint16_t buffer_size = 512;
+
+ displayio_area_t clipped;
+ // Clip the area to the display by overlapping the areas. If there is no overlap then we're done.
+ if (!displayio_display_clip_area(display, area, &clipped)) {
+ return true;
+ }
+ uint16_t subrectangles = 1;
+ uint16_t rows_per_buffer = displayio_area_height(&clipped);
+ if (displayio_area_size(area) > buffer_size) {
+ rows_per_buffer = buffer_size / displayio_area_width(&clipped);
+ subrectangles = displayio_area_height(&clipped) / rows_per_buffer;
+ if (displayio_area_height(&clipped) % rows_per_buffer != 0) {
+ subrectangles++;
+ }
+ buffer_size = rows_per_buffer * displayio_area_width(&clipped);
+ }
+ uint32_t buffer[buffer_size / 2];
+ uint16_t remaining_rows = displayio_area_height(&clipped);
+
+ for (uint16_t j = 0; j < subrectangles; j++) {
+ displayio_area_t subrectangle = {
+ .x1 = clipped.x1,
+ .y1 = clipped.y1 + rows_per_buffer * j,
+ .x2 = clipped.x2,
+ .y2 = clipped.y1 + rows_per_buffer * (j + 1)
+ };
+ if (remaining_rows < rows_per_buffer) {
+ subrectangle.y2 = subrectangle.y1 + remaining_rows;
+ }
+ remaining_rows -= rows_per_buffer;
+
+ displayio_display_begin_transaction(display);
+ displayio_display_set_region_to_update(display, subrectangle.x1, subrectangle.y1,
+ subrectangle.x2, subrectangle.y2);
+ displayio_display_end_transaction(display);
+
+ uint32_t mask[(buffer_size / 32) + 1];
+ for (uint16_t k = 0; k < (buffer_size / 32) + 1; k++) {
+ mask[k] = 0x00000000;
+ }
+
+ bool full_coverage = displayio_display_fill_area(display, &subrectangle, mask, buffer);
+ if (!full_coverage) {
+ uint32_t index = 0;
+ uint32_t current_mask = 0;
+ for (int16_t y = subrectangle.y1; y < subrectangle.y2; y++) {
+ for (int16_t x = subrectangle.x1; x < subrectangle.x2; x++) {
+ if (index % 32 == 0) {
+ current_mask = mask[index / 32];
+ }
+ if ((current_mask & (1 << (index % 32))) == 0) {
+ ((uint16_t*) buffer)[index] = 0x0000;
+ }
+ index++;
+ }
+ }
+ }
+
+ if (!displayio_display_begin_transaction(display)) {
+ // Can't acquire display bus; skip the rest of the data. Try next display.
+ return false;
+ }
+ displayio_display_send_pixels(display, (uint8_t*) buffer, displayio_area_size(&subrectangle) * sizeof(uint16_t));
+ displayio_display_end_transaction(display);
+
+ // TODO(tannewt): Make refresh displays faster so we don't starve other
+ // background tasks.
+ usb_background();
+ }
+ return true;
}
// Check for recursive calls to displayio_refresh_displays.
bool refresh_displays_in_progress = false;
+uint32_t frame_count = 0;
void displayio_refresh_displays(void) {
if (mp_hal_is_interrupted()) {
@@ -55,105 +127,19 @@ void displayio_refresh_displays(void) {
// Too soon. Try next display.
continue;
}
- if (displayio_display_refresh_queued(display)) {
- // We compute the pixels. r and c are row and column to match the display memory
- // structure. x and y match location within the groups.
- uint16_t c0 = 0;
- uint16_t r0 = 0;
- uint16_t c1 = display->width;
- uint16_t r1 = display->height;
- if (display->transpose_xy) {
- swap(&c1, &r1);
- }
-
- if (!displayio_display_begin_transaction(display)) {
- // Can't acquire display bus; skip updating this display. Try next display.
- continue;
- }
- displayio_display_set_region_to_update(display, c0, r0, c1, r1);
- displayio_display_end_transaction(display);
-
- uint16_t x0 = 0;
- uint16_t x1 = display->width - 1;
- uint16_t startx = 0;
- int8_t dx = 1;
- if (display->mirror_x) {
- dx = -1;
- startx = x1;
- }
- uint16_t y0 = 0;
- uint16_t y1 = display->height - 1;
- uint16_t starty = 0;
- int8_t dy = 1;
- if (display->mirror_y) {
- dy = -1;
- starty = y1;
- }
-
- bool transpose = false;
- if (display->transpose_xy) {
- transpose = true;
- int8_t temp_dx = dx;
- dx = dy;
- dy = temp_dx;
-
- swap(&starty, &startx);
- swap(&x0, &y0);
- swap(&x1, &y1);
- }
-
- size_t index = 0;
- uint16_t buffer_size = 256;
- uint32_t buffer[buffer_size / 2];
- bool skip_this_display = false;
-
- for (uint16_t y = starty; y0 <= y && y <= y1; y += dy) {
- for (uint16_t x = startx; x0 <= x && x <= x1; x += dx) {
- uint16_t* pixel = &(((uint16_t*)buffer)[index]);
- *pixel = 0;
-
- if (display->current_group != NULL) {
- if (transpose) {
- displayio_group_get_pixel(display->current_group, y, x, pixel);
- } else {
- displayio_group_get_pixel(display->current_group, x, y, pixel);
- }
- }
-
- index += 1;
- // The buffer is full, send it.
- if (index >= buffer_size) {
- if (!displayio_display_begin_transaction(display)) {
- // Can't acquire display bus; skip the rest of the data. Try next display.
- index = 0;
- skip_this_display = true;
- break;
- }
- displayio_display_send_pixels(display, buffer, buffer_size / 2);
- displayio_display_end_transaction(display);
- // TODO(tannewt): Make refresh displays faster so we don't starve other
- // background tasks.
- usb_background();
- index = 0;
- }
- }
- }
-
- if (skip_this_display) {
- // Go on to next display.
- continue;
- }
- // Send the remaining data.
- if (index) {
- if (!displayio_display_begin_transaction(display)) {
- // Can't get display bus. Skip the rest of the data. Try next display.
- continue;
- }
- displayio_display_send_pixels(display, buffer, index * 2);
- }
- displayio_display_end_transaction(display);
+ if (!displayio_display_begin_transaction(display)) {
+ // Can't acquire display bus; skip updating this display. Try next display.
+ continue;
+ }
+ displayio_display_end_transaction(display);
+ displayio_display_start_refresh(display);
+ const displayio_area_t* current_area = displayio_display_get_refresh_areas(display);
+ while (current_area != NULL) {
+ refresh_area(display, current_area);
+ current_area = current_area->next;
}
displayio_display_finish_refresh(display);
+ frame_count++;
}
// All done.
@@ -181,7 +167,6 @@ void common_hal_displayio_release_displays(void) {
}
void reset_displays(void) {
- #if CIRCUITPY_DISPLAYIO
// The SPI buses used by FourWires may be allocated on the heap so we need to move them inline.
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
if (displays[i].fourwire_bus.base.type != &displayio_fourwire_type) {
@@ -218,5 +203,151 @@ void reset_displays(void) {
display->auto_brightness = true;
common_hal_displayio_display_show(display, &circuitpython_splash);
}
- #endif
+}
+
+void displayio_gc_collect(void) {
+ for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
+ if (displays[i].display.base.type == NULL) {
+ continue;
+ }
+
+ // Alternatively, we could use gc_collect_root over the whole object,
+ // but this is more precise, and is the only field that needs marking.
+ gc_collect_ptr(displays[i].display.current_group);
+
+ }
+}
+
+void displayio_area_expand(displayio_area_t* original, const displayio_area_t* addition) {
+ if (addition->x1 < original->x1) {
+ original->x1 = addition->x1;
+ }
+ if (addition->y1 < original->y1) {
+ original->y1 = addition->y1;
+ }
+ if (addition->x2 > original->x2) {
+ original->x2 = addition->x2;
+ }
+ if (addition->y2 > original->y2) {
+ original->y2 = addition->y2;
+ }
+}
+
+void displayio_area_copy(const displayio_area_t* src, displayio_area_t* dst) {
+ dst->x1 = src->x1;
+ dst->y1 = src->y1;
+ dst->x2 = src->x2;
+ dst->y2 = src->y2;
+}
+
+void displayio_area_scale(displayio_area_t* area, uint16_t scale) {
+ area->x1 *= scale;
+ area->y1 *= scale;
+ area->x2 *= scale;
+ area->y2 *= scale;
+}
+
+void displayio_area_shift(displayio_area_t* area, int16_t dx, int16_t dy) {
+ area->x1 += dx;
+ area->y1 += dy;
+ area->x2 += dx;
+ area->y2 += dy;
+}
+
+bool displayio_area_compute_overlap(const displayio_area_t* a,
+ const displayio_area_t* b,
+ displayio_area_t* overlap) {
+ overlap->x1 = a->x1;
+ if (b->x1 > overlap->x1) {
+ overlap->x1 = b->x1;
+ }
+ overlap->x2 = a->x2;
+ if (b->x2 < overlap->x2) {
+ overlap->x2 = b->x2;
+ }
+ if (overlap->x1 >= overlap->x2) {
+ return false;
+ }
+ overlap->y1 = a->y1;
+ if (b->y1 > overlap->y1) {
+ overlap->y1 = b->y1;
+ }
+ overlap->y2 = a->y2;
+ if (b->y2 < overlap->y2) {
+ overlap->y2 = b->y2;
+ }
+ if (overlap->y1 >= overlap->y2) {
+ return false;
+ }
+ return true;
+}
+
+void displayio_area_union(const displayio_area_t* a,
+ const displayio_area_t* b,
+ displayio_area_t* u) {
+ u->x1 = a->x1;
+ if (b->x1 < u->x1) {
+ u->x1 = b->x1;
+ }
+ u->x2 = a->x2;
+ if (b->x2 > u->x2) {
+ u->x2 = b->x2;
+ }
+
+ u->y1 = a->y1;
+ if (b->y1 < u->y1) {
+ u->y1 = b->y1;
+ }
+ u->y2 = a->y2;
+ if (b->y2 > u->y2) {
+ u->y2 = b->y2;
+ }
+}
+
+uint16_t displayio_area_width(const displayio_area_t* area) {
+ return area->x2 - area->x1;
+}
+
+uint16_t displayio_area_height(const displayio_area_t* area) {
+ return area->y2 - area->y1;
+}
+
+uint32_t displayio_area_size(const displayio_area_t* area) {
+ return displayio_area_width(area) * displayio_area_height(area);
+}
+
+bool displayio_area_equal(const displayio_area_t* a, const displayio_area_t* b) {
+ return a->x1 == b->x1 &&
+ a->y1 == b->y1 &&
+ a->x2 == b->x2 &&
+ a->y2 == b->y2;
+}
+
+// Original and whole must be in the same coordinate space.
+void displayio_area_transform_within(bool mirror_x, bool mirror_y, bool transpose_xy,
+ const displayio_area_t* original,
+ const displayio_area_t* whole,
+ displayio_area_t* transformed) {
+ if (mirror_x) {
+ transformed->x1 = whole->x1 + (whole->x2 - original->x2);
+ transformed->x2 = whole->x2 - (original->x1 - whole->x1);
+ } else {
+ transformed->x1 = original->x1;
+ transformed->x2 = original->x2;
+ }
+ if (mirror_y) {
+ transformed->y1 = whole->y1 + (whole->y2 - original->y2);
+ transformed->y2 = whole->y2 - (original->y1 - whole->y1);
+ } else {
+ transformed->y1 = original->y1;
+ transformed->y2 = original->y2;
+ }
+ if (transpose_xy) {
+ int16_t y1 = transformed->y1;
+ int16_t y2 = transformed->y2;
+ transformed->y1 = whole->y1 + (transformed->x1 - whole->x1);
+ transformed->y2 = whole->y1 + (transformed->x2 - whole->x1);
+ transformed->x2 = whole->x1 + (y2 - whole->y1);
+ transformed->x1 = whole->x1 + (y1 - whole->y1);
+ }
}
diff --git a/shared-module/displayio/__init__.h b/shared-module/displayio/__init__.h
index 5b56ed55c..18c1a4f4a 100644
--- a/shared-module/displayio/__init__.h
+++ b/shared-module/displayio/__init__.h
@@ -24,8 +24,8 @@
* THE SOFTWARE.
*/
-#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H
-#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO___INIT___H
+#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO___INIT___H
#include "shared-bindings/displayio/Display.h"
#include "shared-bindings/displayio/FourWire.h"
@@ -46,5 +46,6 @@ extern displayio_group_t circuitpython_splash;
void displayio_refresh_displays(void);
void reset_displays(void);
+void displayio_gc_collect(void);
-#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H
+#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO___INIT___H
diff --git a/shared-module/displayio/area.h b/shared-module/displayio/area.h
new file mode 100644
index 000000000..ec7c389b4
--- /dev/null
+++ b/shared-module/displayio/area.h
@@ -0,0 +1,73 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_AREA_H
+#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_AREA_H
+
+// Implementations are in __init__.c
+typedef struct _displayio_area_t displayio_area_t;
+
+struct _displayio_area_t {
+ int16_t x1;
+ int16_t y1;
+ int16_t x2; // Second point is exclusive.
+ int16_t y2;
+ const displayio_area_t* next; // Next area in the linked list.
+};
+
+typedef struct {
+ uint16_t x;
+ uint16_t y;
+ int8_t dx;
+ int8_t dy;
+ uint8_t scale;
+ uint16_t width;
+ uint16_t height;
+ bool mirror_x;
+ bool mirror_y;
+ bool transpose_xy;
+} displayio_buffer_transform_t;
+
+void displayio_area_union(const displayio_area_t* a,
+ const displayio_area_t* b,
+ displayio_area_t* u);
+void displayio_area_expand(displayio_area_t* original, const displayio_area_t* addition);
+void displayio_area_copy(const displayio_area_t* src, displayio_area_t* dst);
+void displayio_area_scale(displayio_area_t* area, uint16_t scale);
+void displayio_area_shift(displayio_area_t* area, int16_t dx, int16_t dy);
+bool displayio_area_compute_overlap(const displayio_area_t* a,
+ const displayio_area_t* b,
+ displayio_area_t* overlap);
+uint16_t displayio_area_width(const displayio_area_t* area);
+uint16_t displayio_area_height(const displayio_area_t* area);
+uint32_t displayio_area_size(const displayio_area_t* area);
+bool displayio_area_equal(const displayio_area_t* a, const displayio_area_t* b);
+void displayio_area_transform_within(bool mirror_x, bool mirror_y, bool transpose_xy,
+ const displayio_area_t* original,
+ const displayio_area_t* whole,
+ displayio_area_t* transformed);
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_AREA_H