vectorio: add draw protocol
* Removes VectorShape from user python interactions * Re-integrates vectorio with displayio behind draw protocol implementations * Implements draw protocol with VectorShape * Composes VectorShape behaviors into Rectangle, Circle and Polygon * Fixes terrible pixel garbage being left behind * Improves redraw performance (heuristically) by tracking dirty area separately from current area. Known Issues: It does not work with transposed views.
This commit is contained in:
parent
1b67b91edd
commit
8607cdd783
@ -1,5 +1,6 @@
|
||||
|
||||
#include "shared-bindings/vectorio/__init__.h"
|
||||
#include "shared-bindings/vectorio/Circle.h"
|
||||
#include "shared-bindings/vectorio/VectorShape.h"
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
@ -11,15 +12,21 @@
|
||||
|
||||
//| class Circle:
|
||||
//|
|
||||
//| def __init__(self, radius: int) -> None:
|
||||
//| def __init__(self, pixel_shader: Union[ColorConverter, Palette] radius: int, x: int, y: int) -> None:
|
||||
//| """Circle is positioned on screen by its center point.
|
||||
//|
|
||||
//| :param radius: The radius of the circle in pixels"""
|
||||
//| :param pixel_shader: The pixel shader that produces colors from values
|
||||
//| :param radius: The radius of the circle in pixels
|
||||
//| :param x: Initial x position of the axis.
|
||||
//| :param y: Initial y position of the axis."""
|
||||
//|
|
||||
static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_radius };
|
||||
enum { ARG_pixel_shader, ARG_radius, ARG_x, ARG_y };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
|
||||
{ MP_QSTR_radius, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
||||
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
||||
};
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
@ -33,9 +40,22 @@ static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_arg
|
||||
self->base.type = &vectorio_circle_type;
|
||||
common_hal_vectorio_circle_construct(self, radius);
|
||||
|
||||
// VectorShape parts
|
||||
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
|
||||
int16_t x = args[ARG_x].u_int;
|
||||
int16_t y = args[ARG_y].u_int;
|
||||
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
|
||||
self->draw_protocol_instance = vector_shape;
|
||||
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
STATIC const vectorio_draw_protocol_t circle_draw_protocol = {
|
||||
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw)
|
||||
.draw_get_protocol_self = (draw_get_protocol_self_fun)common_hal_vectorio_circle_get_draw_protocol,
|
||||
.draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl
|
||||
};
|
||||
|
||||
|
||||
//| radius : int
|
||||
//| """The radius of the circle in pixels."""
|
||||
@ -62,13 +82,21 @@ const mp_obj_property_t vectorio_circle_radius_obj = {
|
||||
|
||||
|
||||
STATIC const mp_rom_map_elem_t vectorio_circle_locals_dict_table[] = {
|
||||
// Properties
|
||||
{ MP_ROM_QSTR(MP_QSTR_radius), MP_ROM_PTR(&vectorio_circle_radius_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(vectorio_circle_locals_dict, vectorio_circle_locals_dict_table);
|
||||
|
||||
const mp_obj_type_t vectorio_circle_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Circle,
|
||||
.flags = MP_TYPE_FLAG_EXTENDED,
|
||||
.make_new = vectorio_circle_make_new,
|
||||
.locals_dict = (mp_obj_dict_t *)&vectorio_circle_locals_dict,
|
||||
MP_TYPE_EXTENDED_FIELDS(
|
||||
.protocol = &circle_draw_protocol,
|
||||
),
|
||||
};
|
||||
|
@ -19,4 +19,6 @@ void common_hal_vectorio_circle_get_area(void *circle, displayio_area_t *out_are
|
||||
int16_t common_hal_vectorio_circle_get_radius(void *circle);
|
||||
void common_hal_vectorio_circle_set_radius(void *circle, int16_t radius);
|
||||
|
||||
mp_obj_t common_hal_vectorio_circle_get_draw_protocol(void *circle);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_CIRCLE_H
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
#include "shared-bindings/vectorio/__init__.h"
|
||||
#include "shared-module/vectorio/__init__.h"
|
||||
#include "shared-bindings/vectorio/Polygon.h"
|
||||
#include "shared-bindings/vectorio/VectorShape.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@ -16,15 +17,21 @@
|
||||
|
||||
|
||||
//| class Polygon:
|
||||
//| def __init__(self, points: List[Tuple[int, int]]) -> None:
|
||||
//| def __init__(self, pixel_shader: Union[ColorConverter, Palette], points: List[Tuple[int, int]], x: int, y: int) -> None:
|
||||
//| """Represents a closed shape by ordered vertices
|
||||
//|
|
||||
//| :param points: Vertices for the polygon"""
|
||||
//| :param pixel_shader: The pixel shader that produces colors from values
|
||||
//| :param points: Vertices for the polygon
|
||||
//| :param x: Initial screen x position of the 0,0 origin in the points list.
|
||||
//| :param y: Initial screen y position of the 0,0 origin in the points list."""
|
||||
//|
|
||||
static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_points_list };
|
||||
enum { ARG_pixel_shader, ARG_points_list, ARG_x, ARG_y };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
|
||||
{ MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
||||
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
||||
};
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
@ -38,9 +45,22 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar
|
||||
|
||||
common_hal_vectorio_polygon_construct(self, args[ARG_points_list].u_obj);
|
||||
|
||||
// VectorShape parts
|
||||
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
|
||||
int16_t x = args[ARG_x].u_int;
|
||||
int16_t y = args[ARG_y].u_int;
|
||||
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
|
||||
self->draw_protocol_instance = vector_shape;
|
||||
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
STATIC const vectorio_draw_protocol_t polygon_draw_protocol = {
|
||||
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw)
|
||||
.draw_get_protocol_self = (draw_get_protocol_self_fun)common_hal_vectorio_polygon_get_draw_protocol,
|
||||
.draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl
|
||||
};
|
||||
|
||||
|
||||
//| points: List[Tuple[int, int]]
|
||||
//| """Set a new look and shape for this polygon"""
|
||||
@ -67,13 +87,21 @@ const mp_obj_property_t vectorio_polygon_points_obj = {
|
||||
};
|
||||
|
||||
STATIC const mp_rom_map_elem_t vectorio_polygon_locals_dict_table[] = {
|
||||
// Properties
|
||||
{ MP_ROM_QSTR(MP_QSTR_points), MP_ROM_PTR(&vectorio_polygon_points_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(vectorio_polygon_locals_dict, vectorio_polygon_locals_dict_table);
|
||||
|
||||
const mp_obj_type_t vectorio_polygon_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Polygon,
|
||||
.flags = MP_TYPE_FLAG_EXTENDED,
|
||||
.make_new = vectorio_polygon_make_new,
|
||||
.locals_dict = (mp_obj_dict_t *)&vectorio_polygon_locals_dict,
|
||||
MP_TYPE_EXTENDED_FIELDS(
|
||||
.protocol = &polygon_draw_protocol,
|
||||
),
|
||||
};
|
||||
|
@ -20,5 +20,7 @@ void common_hal_vectorio_polygon_get_area(void *polygon, displayio_area_t *out_a
|
||||
mp_obj_t common_hal_vectorio_polygon_get_points(vectorio_polygon_t *self);
|
||||
void common_hal_vectorio_polygon_set_points(vectorio_polygon_t *self, mp_obj_t points_list);
|
||||
|
||||
mp_obj_t common_hal_vectorio_polygon_get_draw_protocol(void *polygon);
|
||||
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_POLYGON_H
|
||||
|
@ -1,5 +1,7 @@
|
||||
|
||||
#include "shared-bindings/vectorio/__init__.h"
|
||||
#include "shared-bindings/vectorio/Rectangle.h"
|
||||
#include "shared-module/vectorio/VectorShape.h"
|
||||
#include "shared-bindings/vectorio/VectorShape.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@ -8,17 +10,23 @@
|
||||
#include "supervisor/shared/translate.h"
|
||||
|
||||
//| class Rectangle:
|
||||
//| def __init__(self, width: int, height: int) -> None:
|
||||
//| def __init__(self, pixel_shader: Union[ColorConverter, Palette], width: int, height: int, x: int, y: int) -> None:
|
||||
//| """Represents a rectangle by defining its bounds
|
||||
//|
|
||||
//| :param pixel_shader: The pixel shader that produces colors from values
|
||||
//| :param width: The number of pixels wide
|
||||
//| :param height: The number of pixels high"""
|
||||
//| :param height: The number of pixels high
|
||||
//| :param x: Initial x position of the top left corner.
|
||||
//| :param y: Initial y position of the top left corner."""
|
||||
//|
|
||||
static mp_obj_t vectorio_rectangle_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_width, ARG_height };
|
||||
enum { ARG_pixel_shader, ARG_width, ARG_height, ARG_x, ARG_y };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
|
||||
{ MP_QSTR_width, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_height, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
||||
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
||||
};
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
@ -36,17 +44,37 @@ static mp_obj_t vectorio_rectangle_make_new(const mp_obj_type_t *type, size_t n_
|
||||
self->base.type = &vectorio_rectangle_type;
|
||||
common_hal_vectorio_rectangle_construct(self, width, height);
|
||||
|
||||
// VectorShape parts
|
||||
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
|
||||
int16_t x = args[ARG_x].u_int;
|
||||
int16_t y = args[ARG_y].u_int;
|
||||
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
|
||||
self->draw_protocol_instance = vector_shape;
|
||||
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
STATIC const vectorio_draw_protocol_t rectangle_draw_protocol = {
|
||||
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw)
|
||||
.draw_get_protocol_self = (draw_get_protocol_self_fun)common_hal_vectorio_rectangle_get_draw_protocol,
|
||||
.draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl
|
||||
};
|
||||
|
||||
STATIC const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = {
|
||||
// Properties
|
||||
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(vectorio_rectangle_locals_dict, vectorio_rectangle_locals_dict_table);
|
||||
|
||||
const mp_obj_type_t vectorio_rectangle_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Rectangle,
|
||||
.flags = MP_TYPE_FLAG_EXTENDED,
|
||||
.make_new = vectorio_rectangle_make_new,
|
||||
.locals_dict = (mp_obj_dict_t *)&vectorio_rectangle_locals_dict,
|
||||
MP_TYPE_EXTENDED_FIELDS(
|
||||
.protocol = &rectangle_draw_protocol,
|
||||
),
|
||||
};
|
||||
|
@ -12,4 +12,6 @@ uint32_t common_hal_vectorio_rectangle_get_pixel(void *rectangle, int16_t x, int
|
||||
|
||||
void common_hal_vectorio_rectangle_get_area(void *rectangle, displayio_area_t *out_area);
|
||||
|
||||
mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_RECTANGLE_H
|
||||
|
@ -19,37 +19,16 @@
|
||||
#include "supervisor/shared/translate.h"
|
||||
|
||||
|
||||
//| class VectorShape:
|
||||
//| def __init__(self, shape: Union[Polygon, Rectangle, Circle], pixel_shader: Union[displayio.ColorConverter, displayio.Palette], x: int=0, y: int=0) -> None:
|
||||
//| """Binds a vector shape to a location and pixel shader. The shader can be a displayio.Palette(1); it will be asked to color pixel value 0.
|
||||
//|
|
||||
//| :param shape: The shape to draw.
|
||||
//| :param pixel_shader: The pixel shader that produces colors from values
|
||||
//| :param x: Initial x position of the center axis of the shape within the parent.
|
||||
//| :param y: Initial y position of the center axis of the shape within the parent."""
|
||||
//| ...
|
||||
//|
|
||||
STATIC mp_obj_t vectorio_vector_shape_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_shape, ARG_pixel_shader, ARG_x, ARG_y };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_shape, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
|
||||
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
|
||||
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
||||
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
||||
};
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
|
||||
// shape: The shape implementation to draw.
|
||||
// pixel_shader: The pixel shader that produces colors from values. The shader can be a displayio.Palette(1); it will be asked to color pixel value 0.
|
||||
// x: Initial x position of the center axis of the shape within the parent.
|
||||
// y: Initial y position of the center axis of the shape within the parent."""
|
||||
mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int16_t x, int16_t y) {
|
||||
if (!mp_obj_is_type(pixel_shader, &displayio_colorconverter_type) &&
|
||||
!mp_obj_is_type(pixel_shader, &displayio_palette_type)) {
|
||||
mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_pixel_shader);
|
||||
}
|
||||
|
||||
int16_t x = args[ARG_x].u_int;
|
||||
int16_t y = args[ARG_y].u_int;
|
||||
|
||||
mp_obj_t shape = args[ARG_shape].u_obj;
|
||||
vectorio_ishape_t ishape;
|
||||
// Wire up shape functions
|
||||
if (mp_obj_is_type(shape, &vectorio_polygon_type)) {
|
||||
@ -92,18 +71,31 @@ STATIC mp_obj_t vectorio_vector_shape_make_new(const mp_obj_type_t *type, size_t
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
vectorio_draw_protocol_impl_t vectorio_vector_shape_draw_protocol_impl = {
|
||||
.draw_fill_area = (draw_fill_area_fun)vectorio_vector_shape_fill_area,
|
||||
.draw_get_dirty_area = (draw_get_dirty_area_fun)vectorio_vector_shape_get_dirty_area,
|
||||
.draw_update_transform = (draw_update_transform_fun)vectorio_vector_shape_update_transform,
|
||||
.draw_finish_refresh = (draw_finish_refresh_fun)vectorio_vector_shape_finish_refresh,
|
||||
.draw_get_refresh_areas = (draw_get_refresh_areas_fun)vectorio_vector_shape_get_refresh_areas,
|
||||
};
|
||||
|
||||
|
||||
//| x: int
|
||||
//| """X position of the center point of the shape in the parent."""
|
||||
//|
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_get_x(mp_obj_t self_in) {
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_get_x(mp_obj_t wrapper_shape) {
|
||||
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
||||
|
||||
return MP_OBJ_NEW_SMALL_INT(common_hal_vectorio_vector_shape_get_x(self));
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_vector_shape_get_x_obj, vectorio_vector_shape_obj_get_x);
|
||||
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_set_x(mp_obj_t self_in, mp_obj_t x_obj) {
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_set_x(mp_obj_t wrapper_shape, mp_obj_t x_obj) {
|
||||
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
||||
|
||||
mp_int_t x = mp_obj_get_int(x_obj);
|
||||
common_hal_vectorio_vector_shape_set_x(self, x);
|
||||
@ -122,14 +114,19 @@ const mp_obj_property_t vectorio_vector_shape_x_obj = {
|
||||
//| y: int
|
||||
//| """Y position of the center point of the shape in the parent."""
|
||||
//|
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_get_y(mp_obj_t self_in) {
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_get_y(mp_obj_t wrapper_shape) {
|
||||
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
||||
|
||||
return MP_OBJ_NEW_SMALL_INT(common_hal_vectorio_vector_shape_get_y(self));
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_vector_shape_get_y_obj, vectorio_vector_shape_obj_get_y);
|
||||
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_set_y(mp_obj_t self_in, mp_obj_t y_obj) {
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_set_y(mp_obj_t wrapper_shape, mp_obj_t y_obj) {
|
||||
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
||||
|
||||
mp_int_t y = mp_obj_get_int(y_obj);
|
||||
common_hal_vectorio_vector_shape_set_y(self, y);
|
||||
@ -148,14 +145,20 @@ const mp_obj_property_t vectorio_vector_shape_y_obj = {
|
||||
//| pixel_shader: Union[displayio.ColorConverter, displayio.Palette]
|
||||
//| """The pixel shader of the shape."""
|
||||
//|
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_get_pixel_shader(mp_obj_t self_in) {
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_get_pixel_shader(mp_obj_t wrapper_shape) {
|
||||
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
||||
|
||||
return common_hal_vectorio_vector_shape_get_pixel_shader(self);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_vector_shape_get_pixel_shader_obj, vectorio_vector_shape_obj_get_pixel_shader);
|
||||
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_set_pixel_shader(mp_obj_t self_in, mp_obj_t pixel_shader) {
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
STATIC mp_obj_t vectorio_vector_shape_obj_set_pixel_shader(mp_obj_t wrapper_shape, mp_obj_t pixel_shader) {
|
||||
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
||||
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
||||
|
||||
if (!mp_obj_is_type(pixel_shader, &displayio_palette_type) && !mp_obj_is_type(pixel_shader, &displayio_colorconverter_type)) {
|
||||
mp_raise_TypeError(translate("pixel_shader must be displayio.Palette or displayio.ColorConverter"));
|
||||
}
|
||||
@ -175,16 +178,11 @@ const mp_obj_property_t vectorio_vector_shape_pixel_shader_obj = {
|
||||
|
||||
|
||||
STATIC const mp_rom_map_elem_t vectorio_vector_shape_locals_dict_table[] = {
|
||||
// Properties
|
||||
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(vectorio_vector_shape_locals_dict, vectorio_vector_shape_locals_dict_table);
|
||||
|
||||
const mp_obj_type_t vectorio_vector_shape_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_VectorShape,
|
||||
.make_new = vectorio_vector_shape_make_new,
|
||||
.locals_dict = (mp_obj_dict_t *)&vectorio_vector_shape_locals_dict,
|
||||
};
|
||||
|
@ -1,11 +1,18 @@
|
||||
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_SHAPE_H
|
||||
#define MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_SHAPE_H
|
||||
|
||||
#include "py/objproperty.h"
|
||||
|
||||
#include "shared-bindings/vectorio/__init__.h"
|
||||
#include "shared-module/vectorio/VectorShape.h"
|
||||
#include "shared-module/displayio/area.h"
|
||||
|
||||
extern const mp_obj_type_t vectorio_vector_shape_type;
|
||||
|
||||
// Python shared bindings constructor
|
||||
mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int16_t x, int16_t y);
|
||||
|
||||
// C data constructor
|
||||
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);
|
||||
@ -21,7 +28,12 @@ void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_in
|
||||
mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self);
|
||||
void common_hal_vectorio_vector_shape_set_pixel_shader(vectorio_vector_shape_t *self, mp_obj_t pixel_shader);
|
||||
|
||||
|
||||
void vectorio_vector_shape_update_transform(vectorio_vector_shape_t *self, displayio_buffer_transform_t *group_transform);
|
||||
|
||||
// Composable property definition for shapes that use VectorShape
|
||||
extern vectorio_draw_protocol_impl_t vectorio_vector_shape_draw_protocol_impl;
|
||||
extern const mp_obj_property_t vectorio_vector_shape_x_obj;
|
||||
extern const mp_obj_property_t vectorio_vector_shape_y_obj;
|
||||
extern const mp_obj_property_t vectorio_vector_shape_pixel_shader_obj;
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_SHAPE_H
|
||||
|
@ -16,7 +16,7 @@ STATIC const mp_rom_map_elem_t vectorio_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_Circle), MP_ROM_PTR(&vectorio_circle_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Polygon), MP_ROM_PTR(&vectorio_polygon_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Rectangle), MP_ROM_PTR(&vectorio_rectangle_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_VectorShape), MP_ROM_PTR(&vectorio_vector_shape_type) },
|
||||
// { MP_ROM_QSTR(MP_QSTR_VectorShape), MP_ROM_PTR(&vectorio_vector_shape_type) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(vectorio_module_globals, vectorio_module_globals_table);
|
||||
|
41
shared-bindings/vectorio/__init__.h
Normal file
41
shared-bindings/vectorio/__init__.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef SHARED_MODULE_VECTORIO__INIT__H
|
||||
#define SHARED_MODULE_VECTORIO__INIT__H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/proto.h"
|
||||
|
||||
#include "shared-module/displayio/area.h"
|
||||
#include "shared-module/displayio/Palette.h"
|
||||
|
||||
// Returns the object on which the rest of the draw protocol methods are invoked.
|
||||
typedef mp_obj_t (*draw_get_protocol_self_fun)(mp_obj_t protocol_container);
|
||||
|
||||
typedef bool (*draw_fill_area_fun)(mp_obj_t draw_protocol_self, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer);
|
||||
typedef bool (*draw_get_dirty_area_fun)(mp_obj_t draw_protocol_self, displayio_area_t *current_dirty_area);
|
||||
typedef void (*draw_update_transform_fun)(mp_obj_t draw_protocol_self, displayio_buffer_transform_t *group_transform);
|
||||
typedef void (*draw_finish_refresh_fun)(mp_obj_t draw_protocol_self);
|
||||
typedef displayio_area_t *(*draw_get_refresh_areas_fun)(mp_obj_t draw_protocol_self, displayio_area_t *tail);
|
||||
|
||||
typedef struct _vectorio_draw_protocol_impl_t {
|
||||
draw_fill_area_fun draw_fill_area;
|
||||
draw_get_dirty_area_fun draw_get_dirty_area;
|
||||
draw_update_transform_fun draw_update_transform;
|
||||
draw_finish_refresh_fun draw_finish_refresh;
|
||||
draw_get_refresh_areas_fun draw_get_refresh_areas;
|
||||
} vectorio_draw_protocol_impl_t;
|
||||
|
||||
// Draw protocol
|
||||
typedef struct _vectorio_draw_protocol_t {
|
||||
MP_PROTOCOL_HEAD // MP_QSTR_protocol_draw
|
||||
|
||||
// Instance of the draw protocol
|
||||
draw_get_protocol_self_fun draw_get_protocol_self;
|
||||
|
||||
// Implementation functions for the draw protocol
|
||||
vectorio_draw_protocol_impl_t *draw_protocol_impl;
|
||||
} vectorio_draw_protocol_t;
|
||||
|
||||
#endif
|
@ -144,10 +144,10 @@ static void _update_child_transforms(displayio_group_t *self) {
|
||||
for (size_t i = 0; i < self->members->len; i++) {
|
||||
mp_obj_t layer;
|
||||
#if CIRCUITPY_VECTORIO
|
||||
layer = mp_obj_cast_to_native_base(
|
||||
self->members->items[i], &vectorio_vector_shape_type);
|
||||
if (layer != MP_OBJ_NULL) {
|
||||
vectorio_vector_shape_update_transform(layer, &self->absolute_transform);
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, self->members->items[i]);
|
||||
if (draw_protocol != NULL) {
|
||||
layer = draw_protocol->draw_get_protocol_self(self->members->items[i]);
|
||||
draw_protocol->draw_protocol_impl->draw_update_transform(layer, &self->absolute_transform);
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
@ -241,15 +241,14 @@ void common_hal_displayio_group_set_y(displayio_group_t *self, mp_int_t y) {
|
||||
}
|
||||
|
||||
static void _add_layer(displayio_group_t *self, mp_obj_t layer) {
|
||||
mp_obj_t native_layer;
|
||||
#if CIRCUITPY_VECTORIO
|
||||
native_layer = mp_obj_cast_to_native_base(layer, &vectorio_vector_shape_type);
|
||||
if (native_layer != MP_OBJ_NULL) {
|
||||
vectorio_vector_shape_update_transform(native_layer, &self->absolute_transform);
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, layer);
|
||||
if (draw_protocol != NULL) {
|
||||
draw_protocol->draw_protocol_impl->draw_update_transform(draw_protocol->draw_get_protocol_self(layer), &self->absolute_transform);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
native_layer = mp_obj_cast_to_native_base(layer, &displayio_tilegrid_type);
|
||||
mp_obj_t native_layer = mp_obj_cast_to_native_base(layer, &displayio_tilegrid_type);
|
||||
if (native_layer != MP_OBJ_NULL) {
|
||||
displayio_tilegrid_t *tilegrid = native_layer;
|
||||
if (tilegrid->in_group) {
|
||||
@ -283,12 +282,12 @@ static void _remove_layer(displayio_group_t *self, size_t index) {
|
||||
displayio_area_t layer_area;
|
||||
bool rendered_last_frame = false;
|
||||
#if CIRCUITPY_VECTORIO
|
||||
layer = mp_obj_cast_to_native_base(
|
||||
self->members->items[index], &vectorio_vector_shape_type);
|
||||
if (layer != MP_OBJ_NULL) {
|
||||
bool has_dirty_area = vectorio_vector_shape_get_dirty_area(layer, &layer_area);
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, self->members->items[index]);
|
||||
if (draw_protocol != NULL) {
|
||||
layer = draw_protocol->draw_get_protocol_self(self->members->items[index]);
|
||||
bool has_dirty_area = draw_protocol->draw_protocol_impl->draw_get_dirty_area(layer, &layer_area);
|
||||
rendered_last_frame = has_dirty_area;
|
||||
vectorio_vector_shape_update_transform(layer, NULL);
|
||||
draw_protocol->draw_protocol_impl->draw_update_transform(layer, NULL);
|
||||
}
|
||||
#endif
|
||||
layer = mp_obj_cast_to_native_base(
|
||||
@ -362,10 +361,10 @@ bool displayio_group_fill_area(displayio_group_t *self, const _displayio_colorsp
|
||||
for (int32_t i = self->members->len - 1; i >= 0; i--) {
|
||||
mp_obj_t layer;
|
||||
#if CIRCUITPY_VECTORIO
|
||||
layer = mp_obj_cast_to_native_base(
|
||||
self->members->items[i], &vectorio_vector_shape_type);
|
||||
if (layer != MP_OBJ_NULL) {
|
||||
if (vectorio_vector_shape_fill_area(layer, colorspace, area, mask, buffer)) {
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, self->members->items[i]);
|
||||
if (draw_protocol != NULL) {
|
||||
layer = draw_protocol->draw_get_protocol_self(self->members->items[i]);
|
||||
if (draw_protocol->draw_protocol_impl->draw_fill_area(layer, colorspace, area, mask, buffer)) {
|
||||
return true;
|
||||
}
|
||||
continue;
|
||||
@ -396,10 +395,10 @@ void displayio_group_finish_refresh(displayio_group_t *self) {
|
||||
for (int32_t i = self->members->len - 1; i >= 0; i--) {
|
||||
mp_obj_t layer;
|
||||
#if CIRCUITPY_VECTORIO
|
||||
layer = mp_obj_cast_to_native_base(
|
||||
self->members->items[i], &vectorio_vector_shape_type);
|
||||
if (layer != MP_OBJ_NULL) {
|
||||
vectorio_vector_shape_finish_refresh(layer);
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, self->members->items[i]);
|
||||
if (draw_protocol != NULL) {
|
||||
layer = draw_protocol->draw_get_protocol_self(self->members->items[i]);
|
||||
draw_protocol->draw_protocol_impl->draw_finish_refresh(layer);
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
@ -427,10 +426,10 @@ displayio_area_t *displayio_group_get_refresh_areas(displayio_group_t *self, dis
|
||||
for (int32_t i = self->members->len - 1; i >= 0; i--) {
|
||||
mp_obj_t layer;
|
||||
#if CIRCUITPY_VECTORIO
|
||||
layer = mp_obj_cast_to_native_base(
|
||||
self->members->items[i], &vectorio_vector_shape_type);
|
||||
if (layer != MP_OBJ_NULL) {
|
||||
tail = vectorio_vector_shape_get_refresh_areas(layer, tail);
|
||||
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, self->members->items[i]);
|
||||
if (draw_protocol != NULL) {
|
||||
layer = draw_protocol->draw_get_protocol_self(self->members->items[i]);
|
||||
tail = draw_protocol->draw_protocol_impl->draw_get_refresh_areas(layer, tail);
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
@ -59,3 +59,8 @@ void common_hal_vectorio_circle_set_radius(void *obj, int16_t radius) {
|
||||
self->on_dirty.event(self->on_dirty.obj);
|
||||
}
|
||||
}
|
||||
|
||||
mp_obj_t common_hal_vectorio_circle_get_draw_protocol(void *circle) {
|
||||
vectorio_circle_t *self = circle;
|
||||
return self->draw_protocol_instance;
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ typedef struct {
|
||||
mp_obj_base_t base;
|
||||
uint16_t radius;
|
||||
vectorio_event_t on_dirty;
|
||||
mp_obj_t draw_protocol_instance;
|
||||
} vectorio_circle_t;
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_CIRCLE_H
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
#include "shared-module/vectorio/__init__.h"
|
||||
#include "shared-bindings/vectorio/Polygon.h"
|
||||
#include "shared-module/displayio/area.h"
|
||||
@ -108,17 +107,17 @@ void common_hal_vectorio_polygon_get_area(void *polygon, displayio_area_t *area)
|
||||
int x = self->points_list[i];
|
||||
++i;
|
||||
int y = self->points_list[i];
|
||||
if (x <= area->x1) {
|
||||
area->x1 = x - 1;
|
||||
if (x < area->x1) {
|
||||
area->x1 = x;
|
||||
}
|
||||
if (y <= area->y1) {
|
||||
area->y1 = y - 1;
|
||||
if (y < area->y1) {
|
||||
area->y1 = y;
|
||||
}
|
||||
if (x >= area->x2) {
|
||||
area->x2 = x + 1;
|
||||
if (x > area->x2) {
|
||||
area->x2 = x;
|
||||
}
|
||||
if (y >= area->y2) {
|
||||
area->y2 = y + 1;
|
||||
if (y > area->y2) {
|
||||
area->y2 = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -167,3 +166,8 @@ uint32_t common_hal_vectorio_polygon_get_pixel(void *obj, int16_t x, int16_t y)
|
||||
}
|
||||
return winding_number == 0 ? 0 : 1;
|
||||
}
|
||||
|
||||
mp_obj_t common_hal_vectorio_polygon_get_draw_protocol(void *polygon) {
|
||||
vectorio_polygon_t *self = polygon;
|
||||
return self->draw_protocol_instance;
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ typedef struct {
|
||||
int *points_list;
|
||||
size_t len;
|
||||
vectorio_event_t on_dirty;
|
||||
mp_obj_t draw_protocol_instance;
|
||||
} vectorio_polygon_t;
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_POLYGON_H
|
||||
|
@ -12,10 +12,10 @@ void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_
|
||||
|
||||
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;
|
||||
if (x >= 0 && y >= 0 && x < self->width && y < self->height ) {
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -32,3 +32,8 @@ uint32_t common_hal_vectorio_rectangle_get_height(void *rectangle) {
|
||||
vectorio_rectangle_t *self = rectangle;
|
||||
return self->height;
|
||||
}
|
||||
|
||||
mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle) {
|
||||
vectorio_rectangle_t *self = rectangle;
|
||||
return self->draw_protocol_instance;
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ typedef struct {
|
||||
mp_obj_base_t base;
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
mp_obj_t draw_protocol_instance;
|
||||
} vectorio_rectangle_t;
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_RECTANGLE_H
|
||||
|
@ -15,16 +15,16 @@
|
||||
|
||||
// Lifecycle actions.
|
||||
#define VECTORIO_SHAPE_DEBUG(...) (void)0
|
||||
// #define VECTORIO_SHAPE_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
|
||||
// #define VECTORIO_SHAPE_DEBUG(...) mp_printf(&mp_plat_print, __VA_ARGS__)
|
||||
|
||||
|
||||
// Used in both logging and ifdefs, for extra variables
|
||||
// #define VECTORIO_PERF(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
|
||||
// #define VECTORIO_PERF(...) mp_printf(&mp_plat_print, __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__)
|
||||
// #define VECTORIO_SHAPE_PIXEL_DEBUG(...) mp_printf(&mp_plat_print, __VA_ARGS__)
|
||||
|
||||
|
||||
inline __attribute__((always_inline))
|
||||
@ -32,6 +32,11 @@ static int32_t max(int32_t a, int32_t b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
inline __attribute__((always_inline))
|
||||
static int32_t min(int32_t a, int32_t b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
|
||||
inline __attribute__((always_inline))
|
||||
static void _get_screen_area(vectorio_vector_shape_t *self, displayio_area_t *out_area) {
|
||||
@ -39,48 +44,67 @@ static void _get_screen_area(vectorio_vector_shape_t *self, displayio_area_t *ou
|
||||
self->absolute_transform->x, self->absolute_transform->y, self->absolute_transform->dx, self->absolute_transform->dy, self->absolute_transform->scale,
|
||||
self->absolute_transform->width, self->absolute_transform->height, self->absolute_transform->mirror_x, self->absolute_transform->mirror_y, self->absolute_transform->transpose_xy
|
||||
);
|
||||
self->ishape.get_area(self->ishape.shape, out_area);
|
||||
VECTORIO_SHAPE_DEBUG(" in:{(%5d,%5d), (%5d,%5d)}", out_area->x1, out_area->y1, out_area->x2, out_area->y2);
|
||||
if (self->absolute_transform->transpose_xy) {
|
||||
int16_t swap = out_area->x1;
|
||||
out_area->x1 = (out_area->y1 + self->y) * self->absolute_transform->dx + self->absolute_transform->x;
|
||||
out_area->y1 = (swap + self->x) * self->absolute_transform->dy + self->absolute_transform->y;
|
||||
swap = out_area->x2;
|
||||
out_area->x2 = (out_area->y2 + self->y) * self->absolute_transform->dx + self->absolute_transform->x;
|
||||
out_area->y2 = (swap + self->x) * self->absolute_transform->dy + self->absolute_transform->y;
|
||||
} else {
|
||||
out_area->x1 = (out_area->x1 + self->x) * self->absolute_transform->dx + self->absolute_transform->x;
|
||||
out_area->y1 = (out_area->y1 + self->y) * self->absolute_transform->dy + self->absolute_transform->y;
|
||||
out_area->x2 = (out_area->x2 + self->x) * self->absolute_transform->dx + self->absolute_transform->x;
|
||||
out_area->y2 = (out_area->y2 + self->y) * self->absolute_transform->dy + self->absolute_transform->y;
|
||||
}
|
||||
// We might have mirrored due to dx
|
||||
displayio_area_canon(out_area);
|
||||
displayio_area_t shape_area;
|
||||
self->ishape.get_area(self->ishape.shape, &shape_area);
|
||||
VECTORIO_SHAPE_DEBUG(" in:{(%5d,%5d), (%5d,%5d)}", shape_area.x1, shape_area.y1, shape_area.x2, shape_area.y2);
|
||||
|
||||
displayio_area_shift(
|
||||
&shape_area,
|
||||
self->x * self->absolute_transform->dx + min(0, self->absolute_transform->dx * displayio_area_width(&shape_area)),
|
||||
self->y * self->absolute_transform->dy + min(0, self->absolute_transform->dy * displayio_area_height(&shape_area))
|
||||
);
|
||||
|
||||
displayio_area_transform_within(
|
||||
false,
|
||||
false,
|
||||
self->absolute_transform->transpose_xy,
|
||||
&shape_area, &shape_area, out_area
|
||||
);
|
||||
|
||||
displayio_area_shift(
|
||||
out_area,
|
||||
self->absolute_transform->x,
|
||||
self->absolute_transform->y
|
||||
);
|
||||
|
||||
VECTORIO_SHAPE_DEBUG(" out:{(%5d,%5d), (%5d,%5d)}\n", out_area->x1, out_area->y1, out_area->x2, out_area->y2);
|
||||
}
|
||||
|
||||
|
||||
// For use by Group to know where it needs to redraw on layer removal.
|
||||
bool vectorio_vector_shape_get_dirty_area(vectorio_vector_shape_t *self, displayio_area_t *out_area) {
|
||||
displayio_area_copy(&self->ephemeral_dirty_area, out_area);
|
||||
out_area->x1 = out_area->x2;
|
||||
displayio_area_union(
|
||||
&self->ephemeral_dirty_area,
|
||||
&self->current_area,
|
||||
out_area
|
||||
);
|
||||
return true; // For now just always redraw.
|
||||
}
|
||||
|
||||
|
||||
// This must be invoked each time a shape changes its position or its shape in any way.
|
||||
// This must be invoked after each time a shape changes its position, shape or appearance 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, ¤t_area);
|
||||
VECTORIO_SHAPE_DEBUG("%p shape_dirty current:{(%3d,%3d), (%3d,%3d)} dirty:{(%3d,%3d), (%3d,%3d)}",
|
||||
VECTORIO_SHAPE_DEBUG("%p shape_dirty new:{(%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_union(&self->ephemeral_dirty_area, ¤t_area, &self->ephemeral_dirty_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);
|
||||
|
||||
bool moved = !displayio_area_equal(¤t_area, &self->current_area);
|
||||
if (moved) {
|
||||
displayio_area_union(&self->current_area, &self->ephemeral_dirty_area, &self->ephemeral_dirty_area);
|
||||
VECTORIO_SHAPE_DEBUG(" stale:{(%3d,%3d), (%3d,%3d)} -> expanded:{(%3d,%3d), (%3d,%3d)}\n",
|
||||
self->current_area.x1, self->current_area.y1, self->current_area.x2, self->current_area.y2,
|
||||
self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
|
||||
|
||||
// Dirty area tracks the shape's footprint between draws. It's reset on refresh finish.
|
||||
displayio_area_copy(¤t_area, &self->current_area);
|
||||
}
|
||||
self->current_area_dirty = true;
|
||||
}
|
||||
|
||||
|
||||
@ -92,10 +116,11 @@ void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self,
|
||||
self->y = y;
|
||||
self->pixel_shader = pixel_shader;
|
||||
self->ishape = ishape;
|
||||
self->dirty = true;
|
||||
self->absolute_transform = &null_transform; // Critical to have a valid transform before getting screen area.
|
||||
_get_screen_area(self, &self->ephemeral_dirty_area);
|
||||
self->ephemeral_dirty_area.x1 = self->ephemeral_dirty_area.x2; // Cheat to set area to 0
|
||||
self->ephemeral_dirty_area.next = NULL;
|
||||
self->current_area_dirty = true;
|
||||
_get_screen_area(self, &self->current_area);
|
||||
}
|
||||
|
||||
|
||||
@ -153,13 +178,12 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ
|
||||
uint64_t start = common_hal_time_monotonic_ns();
|
||||
uint64_t pixel_time = 0;
|
||||
#endif
|
||||
displayio_area_t overlap;
|
||||
VECTORIO_SHAPE_DEBUG("%p fill_area dirty:%d fill: {(%5d,%5d), (%5d,%5d)} dirty: {(%5d,%5d), (%5d,%5d)}",
|
||||
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
|
||||
VECTORIO_SHAPE_DEBUG("%p fill_area: fill: {(%5d,%5d), (%5d,%5d)}",
|
||||
self,
|
||||
area->x1, area->y1, area->x2, area->y2
|
||||
);
|
||||
if (!displayio_area_compute_overlap(area, &self->ephemeral_dirty_area, &overlap)) {
|
||||
displayio_area_t overlap;
|
||||
if (!displayio_area_compute_overlap(area, &self->current_area, &overlap)) {
|
||||
VECTORIO_SHAPE_DEBUG(" no overlap\n");
|
||||
return false;
|
||||
}
|
||||
@ -168,6 +192,11 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ
|
||||
bool full_coverage = displayio_area_equal(area, &overlap);
|
||||
|
||||
uint8_t pixels_per_byte = 8 / colorspace->depth;
|
||||
VECTORIO_SHAPE_DEBUG(" xy:(%3d %3d) tform:{x:%d y:%d dx:%d dy:%d scl:%d w:%d h:%d mx:%d my:%d tr:%d}",
|
||||
self->x, self->y,
|
||||
self->absolute_transform->x, self->absolute_transform->y, self->absolute_transform->dx, self->absolute_transform->dy, self->absolute_transform->scale,
|
||||
self->absolute_transform->width, self->absolute_transform->height, self->absolute_transform->mirror_x, self->absolute_transform->mirror_y, self->absolute_transform->transpose_xy
|
||||
);
|
||||
|
||||
uint32_t linestride_px = displayio_area_width(area);
|
||||
uint32_t line_dirty_offset_px = (overlap.y1 - area->y1) * linestride_px;
|
||||
@ -178,6 +207,25 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ
|
||||
displayio_input_pixel_t input_pixel;
|
||||
displayio_output_pixel_t output_pixel;
|
||||
|
||||
int16_t math_transform_offset_x;
|
||||
int16_t math_transform_offset_y;
|
||||
int16_t math_shape_offset_x;
|
||||
int16_t math_shape_offset_y;
|
||||
if (self->absolute_transform->transpose_xy) {
|
||||
math_transform_offset_x = self->absolute_transform->dy * self->y;
|
||||
math_transform_offset_y = self->absolute_transform->dx * self->x;
|
||||
math_shape_offset_x = min(0, self->absolute_transform->dy * displayio_area_width(&self->current_area));
|
||||
math_shape_offset_y = min(0, self->absolute_transform->dx * displayio_area_height(&self->current_area));
|
||||
} else {
|
||||
math_transform_offset_x = self->absolute_transform->dx * self->x;
|
||||
math_transform_offset_y = self->absolute_transform->dy * self->y;
|
||||
math_shape_offset_x = min(0, self->absolute_transform->dx * displayio_area_width(&self->current_area));
|
||||
math_shape_offset_y = min(0, self->absolute_transform->dy * displayio_area_height(&self->current_area));
|
||||
}
|
||||
|
||||
VECTORIO_SHAPE_DEBUG(", transform_offset: (%3d,%3d), shape_offset: (%3d,%3d)", math_transform_offset_x, math_transform_offset_y, math_shape_offset_x, math_shape_offset_y);
|
||||
|
||||
|
||||
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;
|
||||
@ -186,9 +234,9 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ
|
||||
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);
|
||||
VECTORIO_SHAPE_PIXEL_DEBUG("\n%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");
|
||||
VECTORIO_SHAPE_PIXEL_DEBUG(" masked");
|
||||
continue;
|
||||
}
|
||||
output_pixel.pixel = 0;
|
||||
@ -197,11 +245,11 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ
|
||||
int16_t pixel_to_get_x;
|
||||
int16_t pixel_to_get_y;
|
||||
if (self->absolute_transform->transpose_xy) {
|
||||
pixel_to_get_x = (input_pixel.y - self->absolute_transform->dy * self->x - self->absolute_transform->y) / self->absolute_transform->dy;
|
||||
pixel_to_get_y = (input_pixel.x - self->absolute_transform->dx * self->y - self->absolute_transform->x) / self->absolute_transform->dx;
|
||||
pixel_to_get_x = (input_pixel.y - math_transform_offset_y - self->absolute_transform->y) - math_shape_offset_y;
|
||||
pixel_to_get_y = (input_pixel.x - math_transform_offset_x - self->absolute_transform->x) - math_shape_offset_x;
|
||||
} else {
|
||||
pixel_to_get_x = (input_pixel.x - self->absolute_transform->dx * self->x - self->absolute_transform->x) / self->absolute_transform->dx;
|
||||
pixel_to_get_y = (input_pixel.y - self->absolute_transform->dy * self->y - self->absolute_transform->y) / self->absolute_transform->dy;
|
||||
pixel_to_get_x = (input_pixel.x - math_transform_offset_x - self->absolute_transform->x) - math_shape_offset_x;
|
||||
pixel_to_get_y = (input_pixel.y - math_transform_offset_y - self->absolute_transform->y) - math_shape_offset_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);
|
||||
#ifdef VECTORIO_PERF
|
||||
@ -217,7 +265,7 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ
|
||||
// vectorio shapes use 0 to mean "area is not covered."
|
||||
// We can skip all the rest of the work for this pixel if it's not currently covered by the shape.
|
||||
if (input_pixel.pixel == 0) {
|
||||
VECTORIO_SHAPE_PIXEL_DEBUG(" (encountered transparent pixel; input area is not fully covered)\n");
|
||||
VECTORIO_SHAPE_PIXEL_DEBUG(" (encountered transparent pixel; input area is not fully covered)");
|
||||
full_coverage = false;
|
||||
} else {
|
||||
// Pixel is not transparent. Let's pull the pixel value index down to 0-base for more error-resistant palettes.
|
||||
@ -234,16 +282,16 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ
|
||||
|
||||
// We double-check this to fast-path the case when a pixel is not covered by the shape & not call the color converter unnecessarily.
|
||||
if (!output_pixel.opaque) {
|
||||
VECTORIO_SHAPE_PIXEL_DEBUG(" (encountered transparent pixel from colorconverter; input area is not fully covered)\n");
|
||||
VECTORIO_SHAPE_PIXEL_DEBUG(" (encountered transparent pixel from colorconverter; input area is not fully covered)");
|
||||
full_coverage = false;
|
||||
}
|
||||
|
||||
*mask_doubleword |= 1u << mask_bit;
|
||||
if (colorspace->depth == 16) {
|
||||
VECTORIO_SHAPE_PIXEL_DEBUG(" buffer = %04x 16\n", output_pixel.pixel);
|
||||
VECTORIO_SHAPE_PIXEL_DEBUG(" buffer = %04x 16", 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);
|
||||
VECTORIO_SHAPE_PIXEL_DEBUG(" buffer = %02x 8", 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).
|
||||
@ -258,7 +306,7 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ
|
||||
// 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);
|
||||
VECTORIO_SHAPE_PIXEL_DEBUG(" buffer = %2d %d", output_pixel.pixel, colorspace->depth);
|
||||
((uint8_t *)buffer)[pixel_index / pixels_per_byte] |= output_pixel.pixel << shift;
|
||||
}
|
||||
}
|
||||
@ -277,20 +325,23 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ
|
||||
(double)(pixel_time / 1000.0 / pixels)
|
||||
);
|
||||
#endif
|
||||
VECTORIO_SHAPE_DEBUG(" -> pixels:%4d\n");
|
||||
VECTORIO_SHAPE_DEBUG(" -> pixels:%4d\n", (overlap.x2 - overlap.x1) * (overlap.y2 - overlap.y1));
|
||||
return full_coverage;
|
||||
}
|
||||
|
||||
|
||||
void vectorio_vector_shape_finish_refresh(vectorio_vector_shape_t *self) {
|
||||
if (!self->dirty) {
|
||||
if (displayio_area_empty(&self->ephemeral_dirty_area)) {
|
||||
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);
|
||||
// Reset dirty area to nothing
|
||||
self->ephemeral_dirty_area.x1 = self->ephemeral_dirty_area.x2; // Cheat to set area to empty
|
||||
self->ephemeral_dirty_area.next = NULL;
|
||||
|
||||
self->current_area_dirty = false; // We don't clear current area so we can remember what to clean up if we move
|
||||
self->current_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)) {
|
||||
@ -303,21 +354,30 @@ void vectorio_vector_shape_finish_refresh(vectorio_vector_shape_t *self) {
|
||||
|
||||
// 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
|
||||
displayio_area_t *new_tail = tail;
|
||||
if (!displayio_area_empty(&self->ephemeral_dirty_area)) {
|
||||
// vector.add_to_head
|
||||
self->ephemeral_dirty_area.next = tail;
|
||||
new_tail = &self->ephemeral_dirty_area;
|
||||
VECTORIO_SHAPE_DEBUG("%p get_refresh_area dirty: {(%3d,%3d), (%3d,%3d)}", self, self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
|
||||
}
|
||||
if (self->current_area_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;
|
||||
self->current_area.next = new_tail;
|
||||
new_tail = &self->current_area;
|
||||
VECTORIO_SHAPE_DEBUG(" redrawing current: {(%3d,%3d), (%3d,%3d)}", self->current_area.x1, self->current_area.y1, self->current_area.x2, self->current_area.y2);
|
||||
}
|
||||
return tail;
|
||||
#ifdef VECTORIO_SHAPE_DEBUG
|
||||
if (new_tail != tail) {
|
||||
VECTORIO_SHAPE_DEBUG("\n");
|
||||
}
|
||||
#endif
|
||||
return new_tail;
|
||||
}
|
||||
|
||||
void vectorio_vector_shape_update_transform(vectorio_vector_shape_t *self, displayio_buffer_transform_t *group_transform) {
|
||||
self->absolute_transform = group_transform == NULL ? &null_transform : group_transform;
|
||||
common_hal_vectorio_vector_shape_set_dirty(self);
|
||||
self->absolute_transform = group_transform == NULL ? &null_transform : group_transform;
|
||||
}
|
||||
|
@ -31,11 +31,12 @@ typedef struct {
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
displayio_buffer_transform_t *absolute_transform;
|
||||
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;
|
||||
displayio_area_t current_area;
|
||||
bool current_area_dirty;
|
||||
} vectorio_vector_shape_t;
|
||||
|
||||
displayio_area_t *vectorio_vector_shape_get_refresh_areas(vectorio_vector_shape_t *self, displayio_area_t *tail);
|
||||
|
Loading…
x
Reference in New Issue
Block a user