33
33
#include "shared-bindings/vectorio/VectorShape.h"
34
34
#endif
35
35
36
+ #include <string.h>
37
+ #define LIST_MIN_ALLOC 4
38
+
39
+ STATIC mp_obj_t list_pop (size_t n_args , const mp_obj_t * args ) {
40
+ mp_check_self (MP_OBJ_IS_TYPE (args [0 ], & mp_type_list ));
41
+ mp_obj_list_t * self = mp_instance_cast_to_native_base (args [0 ], & mp_type_list );
42
+ if (self -> len == 0 ) {
43
+ mp_raise_IndexError_varg (translate ("pop from empty %q" ), MP_QSTR_list );
44
+ }
45
+ size_t index = mp_get_index (self -> base .type , self -> len , n_args == 1 ? MP_OBJ_NEW_SMALL_INT (-1 ) : args [1 ], false);
46
+ mp_obj_t ret = self -> items [index ];
47
+ self -> len -= 1 ;
48
+ memmove (self -> items + index , self -> items + index + 1 , (self -> len - index ) * sizeof (mp_obj_t ));
49
+ // Clear stale pointer from slot which just got freed to prevent GC issues
50
+ self -> items [self -> len ] = MP_OBJ_NULL ;
51
+ if (self -> alloc > LIST_MIN_ALLOC && self -> alloc > 2 * self -> len ) {
52
+ self -> items = m_renew (mp_obj_t , self -> items , self -> alloc , self -> alloc /2 );
53
+ self -> alloc /= 2 ;
54
+ }
55
+ return ret ;
56
+ }
36
57
37
58
void common_hal_displayio_group_construct (displayio_group_t * self , uint32_t max_size , uint32_t scale , mp_int_t x , mp_int_t y ) {
38
- displayio_group_child_t * children = m_new ( displayio_group_child_t , max_size );
39
- displayio_group_construct (self , children , max_size , scale , x , y );
59
+ mp_obj_list_t * members = mp_obj_new_list ( 0 , NULL );
60
+ displayio_group_construct (self , members , scale , x , y );
40
61
}
41
62
42
63
bool common_hal_displayio_group_get_hidden (displayio_group_t * self ) {
@@ -51,16 +72,16 @@ void common_hal_displayio_group_set_hidden(displayio_group_t* self, bool hidden)
51
72
if (self -> hidden_by_parent ) {
52
73
return ;
53
74
}
54
- for (size_t i = 0 ; i < self -> size ; i ++ ) {
75
+ for (size_t i = 0 ; i < self -> members -> len ; i ++ ) {
55
76
mp_obj_t layer ;
56
77
layer = mp_instance_cast_to_native_base (
57
- self -> children [i ]. original , & displayio_tilegrid_type );
78
+ self -> members -> items [i ], & displayio_tilegrid_type );
58
79
if (layer != MP_OBJ_NULL ) {
59
80
displayio_tilegrid_set_hidden_by_parent (layer , hidden );
60
81
continue ;
61
82
}
62
83
layer = mp_instance_cast_to_native_base (
63
- self -> children [i ]. original , & displayio_group_type );
84
+ self -> members -> items [i ], & displayio_group_type );
64
85
if (layer != MP_OBJ_NULL ) {
65
86
displayio_group_set_hidden_by_parent (layer , hidden );
66
87
continue ;
@@ -77,16 +98,16 @@ void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden)
77
98
if (self -> hidden ) {
78
99
return ;
79
100
}
80
- for (size_t i = 0 ; i < self -> size ; i ++ ) {
101
+ for (size_t i = 0 ; i < self -> members -> len ; i ++ ) {
81
102
mp_obj_t layer ;
82
103
layer = mp_instance_cast_to_native_base (
83
- self -> children [i ]. original , & displayio_tilegrid_type );
104
+ self -> members -> items [i ], & displayio_tilegrid_type );
84
105
if (layer != MP_OBJ_NULL ) {
85
106
displayio_tilegrid_set_hidden_by_parent (layer , hidden );
86
107
continue ;
87
108
}
88
109
layer = mp_instance_cast_to_native_base (
89
- self -> children [i ]. original , & displayio_group_type );
110
+ self -> members -> items [i ], & displayio_group_type );
90
111
if (layer != MP_OBJ_NULL ) {
91
112
displayio_group_set_hidden_by_parent (layer , hidden );
92
113
continue ;
@@ -100,18 +121,18 @@ uint32_t common_hal_displayio_group_get_scale(displayio_group_t* self) {
100
121
101
122
bool displayio_group_get_previous_area (displayio_group_t * self , displayio_area_t * area ) {
102
123
bool first = true;
103
- for (size_t i = 0 ; i < self -> size ; i ++ ) {
124
+ for (size_t i = 0 ; i < self -> members -> len ; i ++ ) {
104
125
mp_obj_t layer ;
105
126
displayio_area_t layer_area ;
106
127
layer = mp_instance_cast_to_native_base (
107
- self -> children [i ]. original , & displayio_tilegrid_type );
128
+ self -> members -> items [i ], & displayio_tilegrid_type );
108
129
if (layer != MP_OBJ_NULL ) {
109
130
if (!displayio_tilegrid_get_previous_area (layer , & layer_area )) {
110
131
continue ;
111
132
}
112
133
} else {
113
134
layer = mp_instance_cast_to_native_base (
114
- self -> children [i ]. original , & displayio_group_type );
135
+ self -> members -> items [i ], & displayio_group_type );
115
136
if (layer != MP_OBJ_NULL ) {
116
137
if (!displayio_group_get_previous_area (layer , & layer_area )) {
117
138
continue ;
@@ -140,24 +161,24 @@ static void _update_child_transforms(displayio_group_t* self) {
140
161
if (!self -> in_group ) {
141
162
return ;
142
163
}
143
- for (size_t i = 0 ; i < self -> size ; i ++ ) {
164
+ for (size_t i = 0 ; i < self -> members -> len ; i ++ ) {
144
165
mp_obj_t layer ;
145
166
#if CIRCUITPY_VECTORIO
146
167
layer = mp_instance_cast_to_native_base (
147
- self -> children [i ]. original , & vectorio_vector_shape_type );
168
+ self -> members -> items [i ], & vectorio_vector_shape_type );
148
169
if (layer != MP_OBJ_NULL ) {
149
170
vectorio_vector_shape_update_transform (layer , & self -> absolute_transform );
150
171
continue ;
151
172
}
152
173
#endif
153
174
layer = mp_instance_cast_to_native_base (
154
- self -> children [i ]. original , & displayio_tilegrid_type );
175
+ self -> members -> items [i ], & displayio_tilegrid_type );
155
176
if (layer != MP_OBJ_NULL ) {
156
177
displayio_tilegrid_update_transform (layer , & self -> absolute_transform );
157
178
continue ;
158
179
}
159
180
layer = mp_instance_cast_to_native_base (
160
- self -> children [i ]. original , & displayio_group_type );
181
+ self -> members -> items [i ], & displayio_group_type );
161
182
if (layer != MP_OBJ_NULL ) {
162
183
displayio_group_update_transform (layer , & self -> absolute_transform );
163
184
continue ;
@@ -279,22 +300,22 @@ static void _remove_layer(displayio_group_t* self, size_t index) {
279
300
bool rendered_last_frame = false;
280
301
#if CIRCUITPY_VECTORIO
281
302
layer = mp_instance_cast_to_native_base (
282
- self -> children [index ]. original , & vectorio_vector_shape_type );
303
+ self -> members -> items [index ], & vectorio_vector_shape_type );
283
304
if (layer != MP_OBJ_NULL ) {
284
305
bool has_dirty_area = vectorio_vector_shape_get_dirty_area (layer , & layer_area );
285
306
rendered_last_frame = has_dirty_area ;
286
307
vectorio_vector_shape_update_transform (layer , NULL );
287
308
}
288
309
#endif
289
310
layer = mp_instance_cast_to_native_base (
290
- self -> children [index ]. original , & displayio_tilegrid_type );
311
+ self -> members -> items [index ], & displayio_tilegrid_type );
291
312
if (layer != MP_OBJ_NULL ) {
292
313
displayio_tilegrid_t * tilegrid = layer ;
293
314
rendered_last_frame = displayio_tilegrid_get_previous_area (tilegrid , & layer_area );
294
315
displayio_tilegrid_update_transform (tilegrid , NULL );
295
316
}
296
317
layer = mp_instance_cast_to_native_base (
297
- self -> children [index ]. original , & displayio_group_type );
318
+ self -> members -> items [index ], & displayio_group_type );
298
319
if (layer != MP_OBJ_NULL ) {
299
320
displayio_group_t * group = layer ;
300
321
rendered_last_frame = displayio_group_get_previous_area (group , & layer_area );
@@ -312,59 +333,45 @@ static void _remove_layer(displayio_group_t* self, size_t index) {
312
333
}
313
334
314
335
void common_hal_displayio_group_insert (displayio_group_t * self , size_t index , mp_obj_t layer ) {
315
- if (self -> size == self -> max_size ) {
316
- mp_raise_RuntimeError (translate ("Group full" ));
317
- }
318
336
_add_layer (self , layer );
319
- // Shift everything right.
320
- for (size_t i = self -> size ; i > index ; i -- ) {
321
- self -> children [i ] = self -> children [i - 1 ];
337
+ mp_obj_list_append ( self -> members , mp_const_none );
338
+ for (size_t i = self -> members -> len - 1 ; i > index ; i -- ) {
339
+ self -> members -> items [i ] = self -> members -> items [i - 1 ];
322
340
}
323
- self -> children [index ].original = layer ;
324
- self -> size ++ ;
341
+ self -> members -> items [index ] = layer ;
325
342
}
326
343
327
344
mp_obj_t common_hal_displayio_group_pop (displayio_group_t * self , size_t index ) {
328
- self -> size -- ;
329
- mp_obj_t item = self -> children [index ].original ;
330
345
_remove_layer (self , index );
331
-
332
- // Shift everything left.
333
- for (size_t i = index ; i < self -> size ; i ++ ) {
334
- self -> children [i ] = self -> children [i + 1 ];
335
- }
336
- self -> children [self -> size ].original = NULL ;
337
- return item ;
346
+ mp_obj_t args [] = {self -> members , MP_OBJ_NEW_SMALL_INT (index )};
347
+ return list_pop (2 , args );
338
348
}
339
349
340
350
mp_int_t common_hal_displayio_group_index (displayio_group_t * self , mp_obj_t layer ) {
341
- for (size_t i = 0 ; i < self -> size ; i ++ ) {
342
- if (self -> children [i ].original == layer ) {
343
- return i ;
344
- }
345
- }
346
- return -1 ;
351
+ mp_obj_t args [] = {self -> members , layer };
352
+ mp_obj_t * index = mp_seq_index_obj (
353
+ self -> members -> items , self -> members -> len , 2 , args );
354
+ return MP_OBJ_SMALL_INT_VALUE (index );
347
355
}
348
356
349
357
size_t common_hal_displayio_group_get_len (displayio_group_t * self ) {
350
- return self -> size ;
358
+ return self -> members -> len ;
351
359
}
352
360
353
361
mp_obj_t common_hal_displayio_group_get (displayio_group_t * self , size_t index ) {
354
- return self -> children [index ]. original ;
362
+ return self -> members -> items [index ];
355
363
}
356
364
357
365
void common_hal_displayio_group_set (displayio_group_t * self , size_t index , mp_obj_t layer ) {
358
366
_add_layer (self , layer );
359
367
_remove_layer (self , index );
360
- self -> children [ index ]. original = layer ;
368
+ mp_obj_list_store ( self , MP_OBJ_NEW_SMALL_INT ( index ), layer ) ;
361
369
}
362
370
363
- void displayio_group_construct (displayio_group_t * self , displayio_group_child_t * child_array , uint32_t max_size , uint32_t scale , mp_int_t x , mp_int_t y ) {
371
+ void displayio_group_construct (displayio_group_t * self , mp_obj_list_t * members , uint32_t scale , mp_int_t x , mp_int_t y ) {
364
372
self -> x = x ;
365
373
self -> y = y ;
366
- self -> children = child_array ;
367
- self -> max_size = max_size ;
374
+ self -> members = members ;
368
375
self -> item_removed = false;
369
376
self -> scale = scale ;
370
377
self -> in_group = false;
@@ -373,11 +380,11 @@ void displayio_group_construct(displayio_group_t* self, displayio_group_child_t*
373
380
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 ) {
374
381
// Track if any of the layers finishes filling in the given area. We can ignore any remaining
375
382
// layers at that point.
376
- for (int32_t i = self -> size - 1 ; i >= 0 ; i -- ) {
383
+ for (int32_t i = self -> members -> len - 1 ; i >= 0 ; i -- ) {
377
384
mp_obj_t layer ;
378
385
#if CIRCUITPY_VECTORIO
379
386
layer = mp_instance_cast_to_native_base (
380
- self -> children [i ]. original , & vectorio_vector_shape_type );
387
+ self -> members -> items [i ], & vectorio_vector_shape_type );
381
388
if (layer != MP_OBJ_NULL ) {
382
389
if (vectorio_vector_shape_fill_area (layer , colorspace , area , mask , buffer )) {
383
390
return true;
@@ -386,15 +393,15 @@ bool displayio_group_fill_area(displayio_group_t *self, const _displayio_colorsp
386
393
}
387
394
#endif
388
395
layer = mp_instance_cast_to_native_base (
389
- self -> children [i ]. original , & displayio_tilegrid_type );
396
+ self -> members -> items [i ], & displayio_tilegrid_type );
390
397
if (layer != MP_OBJ_NULL ) {
391
398
if (displayio_tilegrid_fill_area (layer , colorspace , area , mask , buffer )) {
392
399
return true;
393
400
}
394
401
continue ;
395
402
}
396
403
layer = mp_instance_cast_to_native_base (
397
- self -> children [i ]. original , & displayio_group_type );
404
+ self -> members -> items [i ], & displayio_group_type );
398
405
if (layer != MP_OBJ_NULL ) {
399
406
if (displayio_group_fill_area (layer , colorspace , area , mask , buffer )) {
400
407
return true;
@@ -407,24 +414,24 @@ bool displayio_group_fill_area(displayio_group_t *self, const _displayio_colorsp
407
414
408
415
void displayio_group_finish_refresh (displayio_group_t * self ) {
409
416
self -> item_removed = false;
410
- for (int32_t i = self -> size - 1 ; i >= 0 ; i -- ) {
417
+ for (int32_t i = self -> members -> len - 1 ; i >= 0 ; i -- ) {
411
418
mp_obj_t layer ;
412
419
#if CIRCUITPY_VECTORIO
413
420
layer = mp_instance_cast_to_native_base (
414
- self -> children [i ]. original , & vectorio_vector_shape_type );
421
+ self -> members -> items [i ], & vectorio_vector_shape_type );
415
422
if (layer != MP_OBJ_NULL ) {
416
423
vectorio_vector_shape_finish_refresh (layer );
417
424
continue ;
418
425
}
419
426
#endif
420
427
layer = mp_instance_cast_to_native_base (
421
- self -> children [i ]. original , & displayio_tilegrid_type );
428
+ self -> members -> items [i ], & displayio_tilegrid_type );
422
429
if (layer != MP_OBJ_NULL ) {
423
430
displayio_tilegrid_finish_refresh (layer );
424
431
continue ;
425
432
}
426
433
layer = mp_instance_cast_to_native_base (
427
- self -> children [i ]. original , & displayio_group_type );
434
+ self -> members -> items [i ], & displayio_group_type );
428
435
if (layer != MP_OBJ_NULL ) {
429
436
displayio_group_finish_refresh (layer );
430
437
continue ;
@@ -438,24 +445,24 @@ displayio_area_t* displayio_group_get_refresh_areas(displayio_group_t *self, dis
438
445
tail = & self -> dirty_area ;
439
446
}
440
447
441
- for (int32_t i = self -> size - 1 ; i >= 0 ; i -- ) {
448
+ for (int32_t i = self -> members -> len - 1 ; i >= 0 ; i -- ) {
442
449
mp_obj_t layer ;
443
450
#if CIRCUITPY_VECTORIO
444
451
layer = mp_instance_cast_to_native_base (
445
- self -> children [i ]. original , & vectorio_vector_shape_type );
452
+ self -> members -> items [i ], & vectorio_vector_shape_type );
446
453
if (layer != MP_OBJ_NULL ) {
447
454
tail = vectorio_vector_shape_get_refresh_areas (layer , tail );
448
455
continue ;
449
456
}
450
457
#endif
451
458
layer = mp_instance_cast_to_native_base (
452
- self -> children [i ]. original , & displayio_tilegrid_type );
459
+ self -> members -> items [i ], & displayio_tilegrid_type );
453
460
if (layer != MP_OBJ_NULL ) {
454
461
tail = displayio_tilegrid_get_refresh_areas (layer , tail );
455
462
continue ;
456
463
}
457
464
layer = mp_instance_cast_to_native_base (
458
- self -> children [i ]. original , & displayio_group_type );
465
+ self -> members -> items [i ], & displayio_group_type );
459
466
if (layer != MP_OBJ_NULL ) {
460
467
tail = displayio_group_get_refresh_areas (layer , tail );
461
468
continue ;
0 commit comments