Skip to content

Commit 6e0ce23

Browse files
committed
displaio: don't store group children native object
Since we want to expose the list of group's children to the user, we should only have the original objects in it, without any other additional data, and compute the native object as needed.
1 parent f4886a6 commit 6e0ce23

File tree

3 files changed

+117
-63
lines changed

3 files changed

+117
-63
lines changed

shared-module/displayio/Group.c

Lines changed: 114 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,18 @@ void common_hal_displayio_group_set_hidden(displayio_group_t* self, bool hidden)
5252
return;
5353
}
5454
for (size_t i = 0; i < self->size; i++) {
55-
mp_obj_t layer = self->children[i].native;
56-
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
55+
mp_obj_t layer;
56+
layer = mp_instance_cast_to_native_base(
57+
self->children[i].original, &displayio_tilegrid_type);
58+
if (layer != MP_OBJ_NULL) {
5759
displayio_tilegrid_set_hidden_by_parent(layer, hidden);
58-
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
60+
continue;
61+
}
62+
layer = mp_instance_cast_to_native_base(
63+
self->children[i].original, &displayio_group_type);
64+
if (layer != MP_OBJ_NULL) {
5965
displayio_group_set_hidden_by_parent(layer, hidden);
66+
continue;
6067
}
6168
}
6269
}
@@ -71,11 +78,18 @@ void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden)
7178
return;
7279
}
7380
for (size_t i = 0; i < self->size; i++) {
74-
mp_obj_t layer = self->children[i].native;
75-
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
81+
mp_obj_t layer;
82+
layer = mp_instance_cast_to_native_base(
83+
self->children[i].original, &displayio_tilegrid_type);
84+
if (layer != MP_OBJ_NULL) {
7685
displayio_tilegrid_set_hidden_by_parent(layer, hidden);
77-
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
86+
continue;
87+
}
88+
layer = mp_instance_cast_to_native_base(
89+
self->children[i].original, &displayio_group_type);
90+
if (layer != MP_OBJ_NULL) {
7891
displayio_group_set_hidden_by_parent(layer, hidden);
92+
continue;
7993
}
8094
}
8195
}
@@ -87,15 +101,21 @@ uint32_t common_hal_displayio_group_get_scale(displayio_group_t* self) {
87101
bool displayio_group_get_previous_area(displayio_group_t *self, displayio_area_t* area) {
88102
bool first = true;
89103
for (size_t i = 0; i < self->size; i++) {
90-
mp_obj_t layer = self->children[i].native;
104+
mp_obj_t layer;
91105
displayio_area_t layer_area;
92-
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
106+
layer = mp_instance_cast_to_native_base(
107+
self->children[i].original, &displayio_tilegrid_type);
108+
if (layer != MP_OBJ_NULL) {
93109
if (!displayio_tilegrid_get_previous_area(layer, &layer_area)) {
94110
continue;
95111
}
96-
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
97-
if (!displayio_group_get_previous_area(layer, &layer_area)) {
98-
continue;
112+
} else {
113+
layer = mp_instance_cast_to_native_base(
114+
self->children[i].original, &displayio_group_type);
115+
if (layer != MP_OBJ_NULL) {
116+
if (!displayio_group_get_previous_area(layer, &layer_area)) {
117+
continue;
118+
}
99119
}
100120
}
101121
if (first) {
@@ -121,17 +141,26 @@ static void _update_child_transforms(displayio_group_t* self) {
121141
return;
122142
}
123143
for (size_t i = 0; i < self->size; i++) {
124-
mp_obj_t layer = self->children[i].native;
144+
mp_obj_t layer;
125145
#if CIRCUITPY_VECTORIO
126-
if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
146+
layer = mp_instance_cast_to_native_base(
147+
self->children[i].original, &vectorio_vector_shape_type);
148+
if (layer != MP_OBJ_NULL) {
127149
vectorio_vector_shape_update_transform(layer, &self->absolute_transform);
150+
continue;
128151
}
129-
else
130152
#endif
131-
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
153+
layer = mp_instance_cast_to_native_base(
154+
self->children[i].original, &displayio_tilegrid_type);
155+
if (layer != MP_OBJ_NULL) {
132156
displayio_tilegrid_update_transform(layer, &self->absolute_transform);
133-
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
157+
continue;
158+
}
159+
layer = mp_instance_cast_to_native_base(
160+
self->children[i].original, &displayio_group_type);
161+
if (layer != MP_OBJ_NULL) {
134162
displayio_group_update_transform(layer, &self->absolute_transform);
163+
continue;
135164
}
136165
}
137166
}
@@ -210,57 +239,63 @@ void common_hal_displayio_group_set_y(displayio_group_t* self, mp_int_t y) {
210239
_update_child_transforms(self);
211240
}
212241

213-
static mp_obj_t _add_layer(displayio_group_t* self, mp_obj_t layer) {
242+
static void _add_layer(displayio_group_t* self, mp_obj_t layer) {
214243
mp_obj_t native_layer;
215244
#if CIRCUITPY_VECTORIO
216245
native_layer = mp_instance_cast_to_native_base(layer, &vectorio_vector_shape_type);
217246
if (native_layer != MP_OBJ_NULL) {
218247
vectorio_vector_shape_update_transform(native_layer, &self->absolute_transform);
219-
return native_layer;
248+
return;
220249
}
221250
#endif
222-
native_layer = mp_instance_cast_to_native_base(layer, &displayio_group_type);
223-
if (native_layer == MP_OBJ_NULL) {
224-
native_layer = mp_instance_cast_to_native_base(layer, &displayio_tilegrid_type);
225-
if (native_layer == MP_OBJ_NULL) {
226-
mp_raise_ValueError(translate("Layer must be a Group or TileGrid subclass."));
227-
}
251+
native_layer = mp_instance_cast_to_native_base(layer, &displayio_tilegrid_type);
252+
if (native_layer != MP_OBJ_NULL) {
228253
displayio_tilegrid_t* tilegrid = native_layer;
229254
if (tilegrid->in_group) {
230255
mp_raise_ValueError(translate("Layer already in a group."));
231256
} else {
232257
tilegrid->in_group = true;
233258
}
234259
displayio_tilegrid_update_transform(tilegrid, &self->absolute_transform);
235-
} else {
260+
return;
261+
}
262+
native_layer = mp_instance_cast_to_native_base(layer, &displayio_group_type);
263+
if (native_layer != MP_OBJ_NULL) {
236264
displayio_group_t* group = native_layer;
237265
if (group->in_group) {
238266
mp_raise_ValueError(translate("Layer already in a group."));
239267
} else {
240268
group->in_group = true;
241269
}
242270
displayio_group_update_transform(group, &self->absolute_transform);
271+
return;
243272
}
244-
return native_layer;
273+
mp_raise_ValueError(translate("Layer must be a Group or TileGrid subclass."));
245274
}
246275

247276
static void _remove_layer(displayio_group_t* self, size_t index) {
248-
mp_obj_t layer = self->children[index].native;
277+
mp_obj_t layer;
249278
displayio_area_t layer_area;
250279
bool rendered_last_frame = false;
251280
#if CIRCUITPY_VECTORIO
252-
if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
281+
layer = mp_instance_cast_to_native_base(
282+
self->children[index].original, &vectorio_vector_shape_type);
283+
if (layer != MP_OBJ_NULL) {
253284
bool has_dirty_area = vectorio_vector_shape_get_dirty_area(layer, &layer_area);
254285
rendered_last_frame = has_dirty_area;
255286
vectorio_vector_shape_update_transform(layer, NULL);
256287
}
257-
else
258288
#endif
259-
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
289+
layer = mp_instance_cast_to_native_base(
290+
self->children[index].original, &displayio_tilegrid_type);
291+
if (layer != MP_OBJ_NULL) {
260292
displayio_tilegrid_t* tilegrid = layer;
261293
rendered_last_frame = displayio_tilegrid_get_previous_area(tilegrid, &layer_area);
262294
displayio_tilegrid_update_transform(tilegrid, NULL);
263-
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
295+
}
296+
layer = mp_instance_cast_to_native_base(
297+
self->children[index].original, &displayio_group_type);
298+
if (layer != MP_OBJ_NULL) {
264299
displayio_group_t* group = layer;
265300
rendered_last_frame = displayio_group_get_previous_area(group, &layer_area);
266301
displayio_group_update_transform(group, NULL);
@@ -280,12 +315,11 @@ void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp
280315
if (self->size == self->max_size) {
281316
mp_raise_RuntimeError(translate("Group full"));
282317
}
283-
mp_obj_t native_layer = _add_layer(self, layer);
318+
_add_layer(self, layer);
284319
// Shift everything right.
285320
for (size_t i = self->size; i > index; i--) {
286321
self->children[i] = self->children[i - 1];
287322
}
288-
self->children[index].native = native_layer;
289323
self->children[index].original = layer;
290324
self->size++;
291325
}
@@ -299,7 +333,6 @@ mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index) {
299333
for (size_t i = index; i < self->size; i++) {
300334
self->children[i] = self->children[i + 1];
301335
}
302-
self->children[self->size].native = NULL;
303336
self->children[self->size].original = NULL;
304337
return item;
305338
}
@@ -322,9 +355,8 @@ mp_obj_t common_hal_displayio_group_get(displayio_group_t* self, size_t index) {
322355
}
323356

324357
void common_hal_displayio_group_set(displayio_group_t* self, size_t index, mp_obj_t layer) {
325-
mp_obj_t native_layer = _add_layer(self, layer);
358+
_add_layer(self, layer);
326359
_remove_layer(self, index);
327-
self->children[index].native = native_layer;
328360
self->children[index].original = layer;
329361
}
330362

@@ -341,47 +373,61 @@ void displayio_group_construct(displayio_group_t* self, displayio_group_child_t*
341373
bool displayio_group_fill_area(displayio_group_t *self, const _displayio_colorspace_t* colorspace, const displayio_area_t* area, uint32_t* mask, uint32_t* buffer) {
342374
// Track if any of the layers finishes filling in the given area. We can ignore any remaining
343375
// layers at that point.
344-
bool full_coverage = false;
345376
for (int32_t i = self->size - 1; i >= 0 ; i--) {
346-
mp_obj_t layer = self->children[i].native;
377+
mp_obj_t layer;
347378
#if CIRCUITPY_VECTORIO
348-
if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
379+
layer = mp_instance_cast_to_native_base(
380+
self->children[i].original, &vectorio_vector_shape_type);
381+
if (layer != MP_OBJ_NULL) {
349382
if (vectorio_vector_shape_fill_area(layer, colorspace, area, mask, buffer)) {
350-
full_coverage = true;
351-
break;
383+
return true;
352384
}
385+
continue;
353386
}
354-
else
355387
#endif
356-
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
388+
layer = mp_instance_cast_to_native_base(
389+
self->children[i].original, &displayio_tilegrid_type);
390+
if (layer != MP_OBJ_NULL) {
357391
if (displayio_tilegrid_fill_area(layer, colorspace, area, mask, buffer)) {
358-
full_coverage = true;
359-
break;
392+
return true;
360393
}
361-
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
394+
continue;
395+
}
396+
layer = mp_instance_cast_to_native_base(
397+
self->children[i].original, &displayio_group_type);
398+
if (layer != MP_OBJ_NULL) {
362399
if (displayio_group_fill_area(layer, colorspace, area, mask, buffer)) {
363-
full_coverage = true;
364-
break;
400+
return true;
365401
}
402+
continue;
366403
}
367404
}
368-
return full_coverage;
405+
return false;
369406
}
370407

371408
void displayio_group_finish_refresh(displayio_group_t *self) {
372409
self->item_removed = false;
373410
for (int32_t i = self->size - 1; i >= 0 ; i--) {
374-
mp_obj_t layer = self->children[i].native;
411+
mp_obj_t layer;
375412
#if CIRCUITPY_VECTORIO
376-
if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
413+
layer = mp_instance_cast_to_native_base(
414+
self->children[i].original, &vectorio_vector_shape_type);
415+
if (layer != MP_OBJ_NULL) {
377416
vectorio_vector_shape_finish_refresh(layer);
417+
continue;
378418
}
379-
else
380419
#endif
381-
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
420+
layer = mp_instance_cast_to_native_base(
421+
self->children[i].original, &displayio_tilegrid_type);
422+
if (layer != MP_OBJ_NULL) {
382423
displayio_tilegrid_finish_refresh(layer);
383-
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
424+
continue;
425+
}
426+
layer = mp_instance_cast_to_native_base(
427+
self->children[i].original, &displayio_group_type);
428+
if (layer != MP_OBJ_NULL) {
384429
displayio_group_finish_refresh(layer);
430+
continue;
385431
}
386432
}
387433
}
@@ -393,17 +439,26 @@ displayio_area_t* displayio_group_get_refresh_areas(displayio_group_t *self, dis
393439
}
394440

395441
for (int32_t i = self->size - 1; i >= 0 ; i--) {
396-
mp_obj_t layer = self->children[i].native;
442+
mp_obj_t layer;
397443
#if CIRCUITPY_VECTORIO
398-
if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
444+
layer = mp_instance_cast_to_native_base(
445+
self->children[i].original, &vectorio_vector_shape_type);
446+
if (layer != MP_OBJ_NULL) {
399447
tail = vectorio_vector_shape_get_refresh_areas(layer, tail);
448+
continue;
400449
}
401-
else
402450
#endif
403-
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
451+
layer = mp_instance_cast_to_native_base(
452+
self->children[i].original, &displayio_tilegrid_type);
453+
if (layer != MP_OBJ_NULL) {
404454
tail = displayio_tilegrid_get_refresh_areas(layer, tail);
405-
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
455+
continue;
456+
}
457+
layer = mp_instance_cast_to_native_base(
458+
self->children[i].original, &displayio_group_type);
459+
if (layer != MP_OBJ_NULL) {
406460
tail = displayio_group_get_refresh_areas(layer, tail);
461+
continue;
407462
}
408463
}
409464

shared-module/displayio/Group.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "shared-module/displayio/Palette.h"
3636

3737
typedef struct {
38-
mp_obj_t native;
3938
mp_obj_t original;
4039
} displayio_group_child_t;
4140

supervisor/shared/display.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ displayio_tilegrid_t blinka_sprite = {
272272
#if CIRCUITPY_TERMINALIO
273273
#define CHILD_COUNT 2
274274
displayio_group_child_t splash_children[2] = {
275-
{&blinka_sprite, &blinka_sprite},
276-
{&supervisor_terminal_text_grid, &supervisor_terminal_text_grid}
275+
{&blinka_sprite},
276+
{&supervisor_terminal_text_grid},
277277
};
278278
#else
279279
#define CHILD_COUNT 1
280280
displayio_group_child_t splash_children[1] = {
281-
{&blinka_sprite, &blinka_sprite},
281+
{&blinka_sprite},
282282
};
283283
#endif
284284

0 commit comments

Comments
 (0)