summaryrefslogtreecommitdiff
path: root/shared-module/vectorio
diff options
context:
space:
mode:
authorwarriorofwire <3454741+WarriorOfWire@users.noreply.github.com>2020-05-02 02:21:35 -0700
committerwarriorofwire <3454741+WarriorOfWire@users.noreply.github.com>2020-05-09 15:38:22 -0700
commit206d0e598adb66e2b23a39a7d11e2f5087513205 (patch)
treecea5a2e50ec96edd8b4c0bfc09e0d9570694a2ee /shared-module/vectorio
parent90bd931808fa797938b723e8d30889ec4a0f1654 (diff)
Add vectorio: for drawing shapes
vectorio builds on m4 express feather Concrete shapes are composed into a VectorShape which is put into a displayio Group for display. VectorShape provides transpose and x/y positioning for shape implementations. Included Shapes: * Circle - A radius; Circle is positioned at its axis in the VectorShape. - You can freely modify the radius to grow and shrink the circle in-place. * Polygon - An ordered list of points. - Beteween each successive point an edge is inferred. A final edge closing the shape is inferred between the last point and the first point. - You can modify the points in a Polygon. The points' coordinate system is relative to (0, 0) so if you'd like a top-center justified 10x20 rectangle you can do points [(-5, 0), (5, 0), (5, 20), (0, 20)] and your VectorShape x and y properties will position the rectangle relative to its top center point * Rectangle A width and a height.
Diffstat (limited to 'shared-module/vectorio')
-rw-r--r--shared-module/vectorio/Circle.c55
-rw-r--r--shared-module/vectorio/Circle.h17
-rw-r--r--shared-module/vectorio/Polygon.c110
-rw-r--r--shared-module/vectorio/Polygon.h16
-rw-r--r--shared-module/vectorio/Rectangle.c35
-rw-r--r--shared-module/vectorio/Rectangle.h15
-rw-r--r--shared-module/vectorio/VectorShape.c282
-rw-r--r--shared-module/vectorio/VectorShape.h51
-rw-r--r--shared-module/vectorio/__init__.c3
-rw-r--r--shared-module/vectorio/__init__.h15
10 files changed, 599 insertions, 0 deletions
diff --git a/shared-module/vectorio/Circle.c b/shared-module/vectorio/Circle.c
new file mode 100644
index 000000000..6bcd6318f
--- /dev/null
+++ b/shared-module/vectorio/Circle.c
@@ -0,0 +1,55 @@
+
+#include "shared-bindings/vectorio/Circle.h"
+#include "shared-module/vectorio/__init__.h"
+#include "shared-module/displayio/area.h"
+
+#include "py/runtime.h"
+#include "stdlib.h"
+
+
+void common_hal_vectorio_circle_construct(vectorio_circle_t *self, uint16_t radius) {
+ self->radius = radius;
+ self->on_dirty.obj = NULL;
+}
+
+void common_hal_vectorio_circle_set_on_dirty(vectorio_circle_t *self, vectorio_event_t on_dirty) {
+ if (self->on_dirty.obj != NULL) {
+ mp_raise_TypeError(translate("circle can only be registered in one parent"));
+ }
+ self->on_dirty = on_dirty;
+}
+
+
+uint32_t common_hal_vectorio_circle_get_pixel(void *obj, int16_t x, int16_t y) {
+ vectorio_circle_t *self = obj;
+ int16_t radius = abs(self->radius);
+ x = abs(x);
+ y = abs(y);
+ if (x+y <= radius) return 1;
+ if (x > radius) return 0;
+ if (y > radius) return 0;
+ return (int32_t)x*x + (int32_t)y*y <= (int32_t)radius*radius;
+}
+
+
+void common_hal_vectorio_circle_get_area(void *circle, displayio_area_t *out_area) {
+ vectorio_circle_t *self = circle;
+ out_area->x1 = -1 * self->radius;
+ out_area->y1 = -1 * self->radius;
+ out_area->x2 = self->radius + 1;
+ out_area->y2 = self->radius + 1;
+}
+
+int16_t common_hal_vectorio_circle_get_radius(void *obj) {
+ vectorio_circle_t *self = obj;
+ return self->radius;
+}
+
+void common_hal_vectorio_circle_set_radius(void *obj, int16_t radius) {
+ vectorio_circle_t *self = obj;
+ self->radius = abs(radius);
+ if (self->on_dirty.obj != NULL) {
+ self->on_dirty.event(self->on_dirty.obj);
+ }
+}
+
diff --git a/shared-module/vectorio/Circle.h b/shared-module/vectorio/Circle.h
new file mode 100644
index 000000000..4b43d767e
--- /dev/null
+++ b/shared-module/vectorio/Circle.h
@@ -0,0 +1,17 @@
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_CIRCLE_H
+#define MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_CIRCLE_H
+
+#include <stdint.h>
+
+#include "py/obj.h"
+
+#include "shared-module/vectorio/__init__.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ uint16_t radius;
+ vectorio_event_t on_dirty;
+} vectorio_circle_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_CIRCLE_H
+
diff --git a/shared-module/vectorio/Polygon.c b/shared-module/vectorio/Polygon.c
new file mode 100644
index 000000000..6722912c2
--- /dev/null
+++ b/shared-module/vectorio/Polygon.c
@@ -0,0 +1,110 @@
+
+#include "shared-module/vectorio/__init__.h"
+#include "shared-bindings/vectorio/Polygon.h"
+#include "shared-module/displayio/area.h"
+
+#include "py/runtime.h"
+#include "stdlib.h"
+#include <stdio.h>
+
+
+#define VECTORIO_POLYGON_DEBUG(...) (void)0
+// #define VECTORIO_POLYGON_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
+
+
+void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t points_list) {
+ VECTORIO_POLYGON_DEBUG("%p polygon_construct\n", self);
+ self->points_list = points_list;
+ self->on_dirty.obj = NULL;
+}
+
+
+mp_obj_t common_hal_vectorio_polygon_get_points(vectorio_polygon_t *self) {
+ return self->points_list;
+}
+void common_hal_vectorio_polygon_set_points(vectorio_polygon_t *self, mp_obj_t points_list) {
+ self->points_list = points_list;
+ if (self->on_dirty.obj != NULL) {
+ self->on_dirty.event(self->on_dirty.obj);
+ }
+}
+
+void common_hal_vectorio_polygon_set_on_dirty(vectorio_polygon_t *self, vectorio_event_t notification) {
+ if ( self->on_dirty.obj != NULL ) {
+ mp_raise_TypeError(translate("polygon can only be registered in one parent"));
+ }
+ self->on_dirty = notification;
+}
+
+
+void common_hal_vectorio_polygon_get_area(void *polygon, displayio_area_t *area) {
+ VECTORIO_POLYGON_DEBUG("%p polygon get_area", polygon);
+ vectorio_polygon_t *self = polygon;
+ size_t len;
+ mp_obj_t *points;
+ mp_obj_list_get(self->points_list, &len, &points);
+ VECTORIO_POLYGON_DEBUG(" len: %2d, points: %d\n", len, len/2);
+
+ area->x1 = SHRT_MAX;
+ area->y1 = SHRT_MAX;
+ area->x2 = SHRT_MIN;
+ area->y2 = SHRT_MIN;
+ for (size_t i=0; i < len; ++i) {
+ mp_int_t x = mp_obj_get_int(points[i]);
+ ++i;
+ mp_int_t y = mp_obj_get_int(points[i]);
+ if (x <= area->x1) area->x1 = x-1;
+ if (y <= area->y1) area->y1 = y-1;
+ if (x >= area->x2) area->x2 = x+1;
+ if (y >= area->y2) area->y2 = y+1;
+ }
+}
+
+
+// <0 if the point is to the left of the line vector
+// 0 if the point is on the line
+// >0 if the point is to the right of the line vector
+__attribute__((always_inline)) static inline int line_side( mp_int_t x1, mp_int_t y1, mp_int_t x2, mp_int_t y2, int16_t px, int16_t py ) {
+ return (px - x1) * (y2 - y1)
+ - (py - y1) * (x2 - x1);
+}
+
+
+uint32_t common_hal_vectorio_polygon_get_pixel(void *obj, int16_t x, int16_t y) {
+ VECTORIO_POLYGON_DEBUG("%p polygon get_pixel %d, %d\n", obj, x, y);
+ vectorio_polygon_t *self = obj;
+ size_t len;
+ mp_obj_t *points;
+ mp_obj_list_get(self->points_list, &len, &points);
+
+ if (len == 0) {
+ return 0;
+ }
+
+ int winding_number = 0;
+ mp_int_t x1 = mp_obj_get_int(points[0]);
+ mp_int_t y1 = mp_obj_get_int(points[1]);
+ for (size_t i=2; i <= len + 1; ++i) {
+ VECTORIO_POLYGON_DEBUG(" {(%3d, %3d),", x1, y1);
+ mp_int_t x2 = mp_obj_get_int(points[i % len]);
+ ++i;
+ mp_int_t y2 = mp_obj_get_int(points[i % len]);
+ VECTORIO_POLYGON_DEBUG(" (%3d, %3d)}\n", x2, y2);
+ if ( y1 <= y ) {
+ if ( y2 > y && line_side(x1, y1, x2, y2, x, y) > 0 ) {
+ // Wind up, point is to the right of the edge vector
+ ++winding_number;
+ VECTORIO_POLYGON_DEBUG(" wind:%2d winding_number:%2d\n", 1, winding_number);
+ }
+ } else if ( y2 <= y && line_side(x1, y1, x2, y2, x, y) < 0 ) {
+ // Wind down, point is to the left of the edge vector
+ --winding_number;
+ VECTORIO_POLYGON_DEBUG(" wind:%2d winding_number:%2d\n", -1, winding_number);
+ }
+
+ x1 = x2;
+ y1 = y2;
+ }
+ return winding_number == 0 ? 0 : 1;
+}
+
diff --git a/shared-module/vectorio/Polygon.h b/shared-module/vectorio/Polygon.h
new file mode 100644
index 000000000..1aef854a7
--- /dev/null
+++ b/shared-module/vectorio/Polygon.h
@@ -0,0 +1,16 @@
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_POLYGON_H
+#define MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_POLYGON_H
+
+#include <stdint.h>
+
+#include "py/obj.h"
+#include "shared-module/vectorio/__init__.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ // A micropython List[ x, y, ... ]
+ mp_obj_t points_list;
+ vectorio_event_t on_dirty;
+} vectorio_polygon_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_POLYGON_H
diff --git a/shared-module/vectorio/Rectangle.c b/shared-module/vectorio/Rectangle.c
new file mode 100644
index 000000000..dfad58a4d
--- /dev/null
+++ b/shared-module/vectorio/Rectangle.c
@@ -0,0 +1,35 @@
+#include "shared-bindings/vectorio/Rectangle.h"
+#include "shared-module/displayio/area.h"
+
+#include "py/runtime.h"
+
+
+void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height) {
+ self->width = width;
+ self->height = height;
+}
+
+
+uint32_t common_hal_vectorio_rectangle_get_pixel(void *obj, int16_t x, int16_t y) {
+ vectorio_rectangle_t *self = obj;
+ if (x < 0 || x >= self->width || y >= self->height || y < 0) {
+ return 0;
+ }
+ return 1;
+}
+
+
+void common_hal_vectorio_rectangle_get_area(void *rectangle, displayio_area_t *out_area) {
+ vectorio_rectangle_t *self = rectangle;
+ out_area->x1 = 0;
+ out_area->y1 = 0;
+ out_area->x2 = self->width;
+ out_area->y2 = self->height;
+}
+
+
+uint32_t common_hal_vectorio_rectangle_get_height(void *rectangle) {
+ vectorio_rectangle_t *self = rectangle;
+ return self->height;
+}
+
diff --git a/shared-module/vectorio/Rectangle.h b/shared-module/vectorio/Rectangle.h
new file mode 100644
index 000000000..c1f2a7a64
--- /dev/null
+++ b/shared-module/vectorio/Rectangle.h
@@ -0,0 +1,15 @@
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_RECTANGLE_H
+#define MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_RECTANGLE_H
+
+#include <stdint.h>
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ uint16_t width;
+ uint16_t height;
+} vectorio_rectangle_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_RECTANGLE_H
+
diff --git a/shared-module/vectorio/VectorShape.c b/shared-module/vectorio/VectorShape.c
new file mode 100644
index 000000000..985b77858
--- /dev/null
+++ b/shared-module/vectorio/VectorShape.c
@@ -0,0 +1,282 @@
+
+#include "stdlib.h"
+
+#include "shared-module/vectorio/__init__.h"
+#include "shared-bindings/vectorio/VectorShape.h"
+
+#include "py/runtime.h"
+#include "shared-bindings/displayio/ColorConverter.h"
+#include "shared-bindings/displayio/Palette.h"
+
+#include "shared-bindings/vectorio/Circle.h"
+#include "shared-bindings/vectorio/Polygon.h"
+#include "shared-bindings/vectorio/Rectangle.h"
+
+// Lifecycle actions.
+#define VECTORIO_SHAPE_DEBUG(...) (void)0
+// #define VECTORIO_SHAPE_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
+
+
+// Really verbose.
+#define VECTORIO_SHAPE_PIXEL_DEBUG(...) (void)0
+// #define VECTORIO_SHAPE_PIXEL_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
+
+
+inline __attribute__((always_inline))
+static int32_t max(int32_t a, int32_t b) {
+ return a > b ? a : b;
+}
+
+
+inline __attribute__((always_inline))
+static void _transpose_area(displayio_area_t *out_area) {
+ int16_t swap = out_area->x1;
+ out_area->x1 = out_area->y1;
+ out_area->y1 = swap;
+ swap = out_area->x2;
+ out_area->x2 = out_area->y2;
+ out_area->y2 = swap;
+}
+
+
+inline __attribute__((always_inline))
+static void _get_shape_area(vectorio_vector_shape_t *self, displayio_area_t *out_area) {
+ VECTORIO_SHAPE_DEBUG("%p get_area\n", self);
+ self->ishape.get_area(self->ishape.shape, out_area);
+}
+
+
+inline __attribute__((always_inline))
+static void _get_screen_area(vectorio_vector_shape_t *self, displayio_area_t *out_area) {
+ VECTORIO_SHAPE_DEBUG("%p get_screen_area\n", self);
+ self->ishape.get_area(self->ishape.shape, out_area);
+ if (self->transpose_xy) {
+ _transpose_area(out_area);
+ displayio_area_shift(out_area, self->y, self->x);
+ } else {
+ displayio_area_shift(out_area, self->x, self->y);
+ }
+}
+
+
+// This must be invoked each time a shape changes its position or its shape in any way.
+void common_hal_vectorio_vector_shape_set_dirty(void *vector_shape) {
+ vectorio_vector_shape_t *self = vector_shape;
+ // In screen space. Need to offset the shape space.
+ displayio_area_t current_area;
+ _get_screen_area(self, &current_area);
+ VECTORIO_SHAPE_DEBUG("%p shape_dirty current:{(%3d,%3d), (%3d,%3d)} dirty:{(%3d,%3d), (%3d,%3d)}",
+ self,
+ current_area.x1, current_area.y1, current_area.x2, current_area.y2,
+ self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
+ self->dirty = true;
+ // Dirty area tracks the shape's footprint between draws. It's reset on refresh finish,
+ displayio_area_expand(&self->ephemeral_dirty_area, &current_area);
+ VECTORIO_SHAPE_DEBUG(" -> expanded:{(%3d,%3d), (%3d,%3d)}\n", self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
+}
+
+
+void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self,
+ vectorio_ishape_t ishape,
+ mp_obj_t pixel_shader, uint16_t x, uint16_t y, bool transpose_xy) {
+ VECTORIO_SHAPE_DEBUG("%p vector_shape_construct x:%3d, y:%3d\n", self, x, y);
+ self->x = x;
+ self->y = y;
+ self->pixel_shader = pixel_shader;
+ self->ishape = ishape;
+ self->transpose_xy = transpose_xy;
+ self->dirty = true;
+ _get_screen_area(self, &self->ephemeral_dirty_area);
+ self->ephemeral_dirty_area.next = NULL;
+}
+
+
+mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self) {
+ VECTORIO_SHAPE_DEBUG("%p get_x\n", self);
+ return self->x;
+}
+
+
+void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) {
+ VECTORIO_SHAPE_DEBUG("%p set_x %d\n", self, x);
+ if (self->x == x) {
+ return;
+ }
+ self->x = x;
+ common_hal_vectorio_vector_shape_set_dirty(self);
+}
+
+
+mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self) {
+ VECTORIO_SHAPE_DEBUG("%p get_y\n", self);
+ return self->y;
+}
+
+
+void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) {
+ VECTORIO_SHAPE_DEBUG("%p set_y %d\n", self, y);
+ if (self->y == y) {
+ return;
+ }
+ self->y = y;
+ common_hal_vectorio_vector_shape_set_dirty(self);
+}
+
+
+bool common_hal_vectorio_vector_shape_get_transpose_xy(vectorio_vector_shape_t *self) {
+ VECTORIO_SHAPE_DEBUG("%p get_transpose_xy\n", self);
+ return self->transpose_xy;
+}
+
+
+mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self) {
+ VECTORIO_SHAPE_DEBUG("%p get_pixel_shader\n", self);
+ return self->pixel_shader;
+}
+
+void common_hal_vectorio_vector_shape_set_pixel_shader(vectorio_vector_shape_t *self, mp_obj_t pixel_shader) {
+ VECTORIO_SHAPE_DEBUG("%p set_pixel_shader\n", self);
+ self->pixel_shader = pixel_shader;
+ common_hal_vectorio_vector_shape_set_dirty(self);
+}
+
+
+bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displayio_colorspace_t* colorspace, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer) {
+ // Shape areas are relative to 0,0. This will allow rotation about a known axis.
+ // The consequence is that the area reported by the shape itself is _relative_ to 0,0.
+ // To make it relative to the VectorShape position, we must shift it.
+ // Pixels are drawn on the screen_area (shifted) coordinate space, while pixels are _determined_ from
+ // the shape_area (unshifted) space.
+ displayio_area_t overlap;
+ displayio_area_t shape_area;
+ _get_shape_area(self, &shape_area);
+ VECTORIO_SHAPE_DEBUG("%p fill_area dirty:%d fill: {(%3d,%3d), (%3d,%3d)} dirty: {(%3d,%3d), (%3d,%3d)}",
+ self, self->dirty,
+ area->x1, area->y1, area->x2, area->y2,
+ self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2
+ );
+ if (!displayio_area_compute_overlap(area, &self->ephemeral_dirty_area, &overlap)) {
+ VECTORIO_SHAPE_DEBUG(" no overlap\n");
+ return false;
+ }
+ VECTORIO_SHAPE_DEBUG(", overlap: {(%3d,%3d), (%3d,%3d)}", overlap.x1, overlap.y1, overlap.x2, overlap.y2);
+
+ bool full_coverage = displayio_area_equal(area, &overlap);
+
+ uint8_t pixels_per_byte = 8 / colorspace->depth;
+
+ uint32_t linestride_px = displayio_area_width(area);
+ uint32_t line_dirty_offset_px = (overlap.y1 - area->y1) * linestride_px;
+ uint32_t column_dirty_offset_px = overlap.x1 - area->x1;
+ VECTORIO_SHAPE_DEBUG(", linestride:%3d line_offset:%3d col_offset:%3d depth:%2d ppb:%2d\n", linestride_px, line_dirty_offset_px, column_dirty_offset_px, colorspace->depth, pixels_per_byte);
+
+ displayio_input_pixel_t input_pixel;
+ displayio_output_pixel_t output_pixel;
+
+ uint32_t mask_start_px = line_dirty_offset_px;
+ for (input_pixel.y = overlap.y1; input_pixel.y < overlap.y2; ++input_pixel.y) {
+ mask_start_px += column_dirty_offset_px;
+ for (input_pixel.x = overlap.x1; input_pixel.x < overlap.x2; ++input_pixel.x) {
+ // Check the mask first to see if the pixel has already been set.
+ uint32_t pixel_index = mask_start_px + (input_pixel.x - overlap.x1);
+ uint32_t *mask_doubleword = &(mask[pixel_index / 32]);
+ uint8_t mask_bit = pixel_index % 32;
+ VECTORIO_SHAPE_PIXEL_DEBUG("%p pixel_index: %5u mask_bit: %2u", self, pixel_index, mask_bit);
+ if ((*mask_doubleword & (1u << mask_bit)) != 0) {
+ VECTORIO_SHAPE_PIXEL_DEBUG(" masked\n");
+ continue;
+ }
+ output_pixel.pixel = 0;
+
+ // Get the target pixel based on the shape's coordinate space
+ int16_t pixel_to_get_x;
+ int16_t pixel_to_get_y;
+ if (self->transpose_xy) {
+ pixel_to_get_x = input_pixel.y - self->x;
+ pixel_to_get_y = input_pixel.x - self->y;
+ } else {
+ pixel_to_get_x = input_pixel.x - self->x;
+ pixel_to_get_y = input_pixel.y - self->y;
+ }
+ VECTORIO_SHAPE_PIXEL_DEBUG(" get_pixel %p (%3d, %3d) -> ( %3d, %3d )", self->ishape.shape, input_pixel.x, input_pixel.y, pixel_to_get_x, pixel_to_get_y);
+ input_pixel.pixel = self->ishape.get_pixel(self->ishape.shape, pixel_to_get_x, pixel_to_get_y);
+ VECTORIO_SHAPE_PIXEL_DEBUG(" -> %d", input_pixel.pixel);
+
+ output_pixel.opaque = true;
+ if (self->pixel_shader == mp_const_none) {
+ output_pixel.pixel = input_pixel.pixel;
+ } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type)) {
+ output_pixel.opaque = displayio_palette_get_color(self->pixel_shader, colorspace, input_pixel.pixel, &output_pixel.pixel);
+ } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type)) {
+ displayio_colorconverter_convert(self->pixel_shader, colorspace, &input_pixel, &output_pixel);
+ }
+ if (!output_pixel.opaque) {
+ VECTORIO_SHAPE_PIXEL_DEBUG(" (encountered transparent pixel; input area is not fully covered)\n");
+ full_coverage = false;
+ } else {
+ *mask_doubleword |= 1u << mask_bit;
+ if (colorspace->depth == 16) {
+ VECTORIO_SHAPE_PIXEL_DEBUG(" buffer = %04x 16\n", output_pixel.pixel);
+ *(((uint16_t*) buffer) + pixel_index) = output_pixel.pixel;
+ } else if (colorspace->depth == 8) {
+ VECTORIO_SHAPE_PIXEL_DEBUG(" buffer = %02x 8\n", output_pixel.pixel);
+ *(((uint8_t*) buffer) + pixel_index) = output_pixel.pixel;
+ } else if (colorspace->depth < 8) {
+ // Reorder the offsets to pack multiple rows into a byte (meaning they share a column).
+ if (!colorspace->pixels_in_byte_share_row) {
+ uint16_t width = linestride_px;
+ uint16_t row = pixel_index / width;
+ uint16_t col = pixel_index % width;
+ pixel_index = col * pixels_per_byte + (row / pixels_per_byte) * pixels_per_byte * width + row % pixels_per_byte;
+ }
+ uint8_t shift = (pixel_index % pixels_per_byte) * colorspace->depth;
+ if (colorspace->reverse_pixels_in_byte) {
+ // Reverse the shift by subtracting it from the leftmost shift.
+ shift = (pixels_per_byte - 1) * colorspace->depth - shift;
+ }
+ VECTORIO_SHAPE_PIXEL_DEBUG(" buffer = %2d %d\n", output_pixel.pixel, colorspace->depth);
+ ((uint8_t*)buffer)[pixel_index / pixels_per_byte] |= output_pixel.pixel << shift;
+ }
+ }
+ }
+ mask_start_px += linestride_px - column_dirty_offset_px;
+ }
+ return full_coverage;
+}
+
+
+void vectorio_vector_shape_finish_refresh(vectorio_vector_shape_t *self) {
+ if ( !self->dirty ) {
+ return;
+ }
+ VECTORIO_SHAPE_DEBUG("%p finish_refresh was:{(%3d,%3d), (%3d,%3d)}\n", self, self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
+ self->dirty = false;
+ // Reset dirty area tracking to current footprint
+ _get_screen_area(self, &self->ephemeral_dirty_area);
+ self->ephemeral_dirty_area.next = NULL;
+ VECTORIO_SHAPE_DEBUG("%p finish_refresh now:{(%3d,%3d), (%3d,%3d)}\n", self, self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
+
+ 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)) {
+ displayio_colorconverter_finish_refresh(self->pixel_shader);
+ }
+}
+
+
+// Assembles a singly linked list of dirty areas from all components on the display.
+displayio_area_t* vectorio_vector_shape_get_refresh_areas(vectorio_vector_shape_t *self, displayio_area_t* tail) {
+ if (self->dirty
+ || (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))
+ ) {
+ VECTORIO_SHAPE_DEBUG("%p get_refresh_area dirty:%d {(%3d,%3d), (%3d,%3d)}", self, self->dirty, self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
+ common_hal_vectorio_vector_shape_set_dirty(self);
+ // vector.add_to_head
+ self->ephemeral_dirty_area.next = tail;
+ VECTORIO_SHAPE_DEBUG(" this_area: %p next: %p after: %p\n", &self->ephemeral_dirty_area, tail, tail == NULL ? NULL : tail->next);
+ return &self->ephemeral_dirty_area;
+ }
+ return tail;
+}
+
diff --git a/shared-module/vectorio/VectorShape.h b/shared-module/vectorio/VectorShape.h
new file mode 100644
index 000000000..ac60e5b98
--- /dev/null
+++ b/shared-module/vectorio/VectorShape.h
@@ -0,0 +1,51 @@
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_SHAPE_H
+#define MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_SHAPE_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "py/obj.h"
+#include "shared-module/displayio/area.h"
+#include "shared-module/displayio/Palette.h"
+
+typedef void get_area_function(mp_obj_t shape, displayio_area_t *out_area);
+typedef uint32_t get_pixel_function(mp_obj_t shape, int16_t x, int16_t y);
+
+// This struct binds a shape's common Shape support functions (its vector shape interface)
+// to its instance pointer. We only check at construction time what the type of the
+// associated shape is and link the correct functions up.
+// Later when using the shape for drawing logic these functions may be invoked
+// unconditionally. This simplifies the addition of new types and restricts the
+// respective responsibilities of VectorShape and actual shape implementations.
+typedef struct {
+ mp_obj_t shape;
+ get_area_function *get_area;
+ get_pixel_function *get_pixel;
+} vectorio_ishape_t;
+
+typedef struct {
+ mp_obj_base_t base;
+ vectorio_ishape_t ishape;
+ mp_obj_t pixel_shader;
+ int16_t x;
+ int16_t y;
+ bool transpose_xy;
+ bool dirty; // True if we need to draw
+ // Tracks current shape footprint and expands outward as the shape dirties and changes.
+ // This is suboptimal if you move your shape far. Could add more state to only redraw
+ // exactly what we left behind.
+ displayio_area_t ephemeral_dirty_area;
+} vectorio_vector_shape_t;
+
+displayio_area_t* vectorio_vector_shape_get_refresh_areas(vectorio_vector_shape_t *self, displayio_area_t* tail);
+
+// Area is always in absolute screen coordinates.
+bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer);
+
+// Fills in out_area with the maximum bounds of all related pixels in the last rendered frame. Returns
+// false if the vector shape wasn't rendered in the last frame.
+bool vectorio_vector_shape_get_previous_area(vectorio_vector_shape_t *self, displayio_area_t *out_area);
+void vectorio_vector_shape_finish_refresh(vectorio_vector_shape_t *self);
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_SHAPE_H
diff --git a/shared-module/vectorio/__init__.c b/shared-module/vectorio/__init__.c
new file mode 100644
index 000000000..473c509c3
--- /dev/null
+++ b/shared-module/vectorio/__init__.c
@@ -0,0 +1,3 @@
+
+// Don't need anything in here yet
+
diff --git a/shared-module/vectorio/__init__.h b/shared-module/vectorio/__init__.h
new file mode 100644
index 000000000..6ae381f06
--- /dev/null
+++ b/shared-module/vectorio/__init__.h
@@ -0,0 +1,15 @@
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_INIT_H
+#define MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_INIT_H
+
+#include "py/obj.h"
+
+typedef void event_function(mp_obj_t obj);
+
+typedef struct {
+ mp_obj_t obj;
+ event_function *event;
+} vectorio_event_t;
+
+
+#endif
+