From 094fe05bdd68332fa617df8a98858e324afa15ec Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 25 Mar 2020 13:40:48 -0500 Subject: allow retrieving info about a supervisor allocation --- supervisor/memory.h | 1 + supervisor/shared/memory.c | 9 +++++++++ 2 files changed, 10 insertions(+) (limited to 'supervisor') diff --git a/supervisor/memory.h b/supervisor/memory.h index f557744ae..f4359ca46 100755 --- a/supervisor/memory.h +++ b/supervisor/memory.h @@ -43,6 +43,7 @@ typedef struct { void memory_init(void); void free_memory(supervisor_allocation* allocation); +supervisor_allocation* allocation_from_ptr(void *ptr); supervisor_allocation* allocate_remaining_memory(void); // Allocate a piece of a given length in bytes. If high_address is true then it should be allocated diff --git a/supervisor/shared/memory.c b/supervisor/shared/memory.c index 14c3b4979..51037bd6d 100755 --- a/supervisor/shared/memory.c +++ b/supervisor/shared/memory.c @@ -82,6 +82,15 @@ void free_memory(supervisor_allocation* allocation) { allocation->ptr = NULL; } +supervisor_allocation* allocation_from_ptr(void *ptr) { + for (size_t index = 0; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index++) { + if (allocations[index].ptr == ptr) { + return &allocations[index]; + } + } + return NULL; +} + supervisor_allocation* allocate_remaining_memory(void) { if (low_address == high_address) { return NULL; -- cgit v1.2.3 From 3a94412cd35c39bf30f29cb2b79be3870709c809 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 Apr 2020 11:59:15 -0500 Subject: protomatter: more memory allocation fixes - bump supervisor alloc count by 4 (we actually use 5) - move reconstruct to after gc heap is reset - destroy protomatter object entirely if not used by a FramebufferDisplay - ensure previous supervisor allocations are released - zero out pointers so GC can collect them --- shared-module/_protomatter/Protomatter.c | 6 ++++++ shared-module/_protomatter/allocator.h | 4 +--- shared-module/displayio/__init__.c | 16 +++++++++++++++- supervisor/shared/display.c | 9 +++++++++ supervisor/shared/memory.c | 2 +- 5 files changed, 32 insertions(+), 5 deletions(-) (limited to 'supervisor') diff --git a/shared-module/_protomatter/Protomatter.c b/shared-module/_protomatter/Protomatter.c index cbb60db6d..bec9586f8 100644 --- a/shared-module/_protomatter/Protomatter.c +++ b/shared-module/_protomatter/Protomatter.c @@ -78,6 +78,11 @@ void common_hal_protomatter_protomatter_reconstruct(protomatter_protomatter_obj_ // verify that the matrix is big enough mp_get_index(mp_obj_get_type(self->framebuffer), self->bufinfo.len, MP_OBJ_NEW_SMALL_INT(self->bufsize-1), false); } else { + _PM_FREE(self->bufinfo.buf); + _PM_FREE(self->core.rgbPins); + _PM_FREE(self->core.addr); + _PM_FREE(self->core.screenData); + self->framebuffer = NULL; self->bufinfo.buf = _PM_allocator_impl(self->bufsize); self->bufinfo.len = self->bufsize; @@ -156,6 +161,7 @@ void common_hal_protomatter_protomatter_deinit(protomatter_protomatter_obj_t* se if (self->core.rgbPins) { _PM_free(&self->core); } + memset(&self->core, 0, sizeof(self->core)); self->base.type = NULL; } diff --git a/shared-module/_protomatter/allocator.h b/shared-module/_protomatter/allocator.h index 8d9d7ff96..9b7590df3 100644 --- a/shared-module/_protomatter/allocator.h +++ b/shared-module/_protomatter/allocator.h @@ -6,7 +6,7 @@ #include "supervisor/memory.h" #define _PM_ALLOCATOR _PM_allocator_impl -#define _PM_FREE _PM_free_impl +#define _PM_FREE(x) (_PM_free_impl((x)), (x)=NULL, (void)0) static inline void *_PM_allocator_impl(size_t sz) { supervisor_allocation *allocation = allocate_memory(align32_size(sz), true); @@ -22,8 +22,6 @@ static inline void _PM_free_impl(void *ptr_in) { if (allocation) { free_memory(allocation); - } else { - m_free(ptr_in); } } diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c index 225aa2008..dfa3e7473 100644 --- a/shared-module/displayio/__init__.c +++ b/shared-module/displayio/__init__.c @@ -21,6 +21,18 @@ primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT]; +STATIC bool any_display_uses_this_protomatter(protomatter_protomatter_obj_t* pm) { + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) { + framebufferio_framebufferdisplay_obj_t* display = &displays[i].framebuffer_display; + if (display->framebuffer == pm) { + return true; + } + } + } + return false; +} + // Check for recursive calls to displayio_background. bool displayio_background_in_progress = false; @@ -152,7 +164,9 @@ void reset_displays(void) { #if CIRCUITPY_PROTOMATTER } else if (displays[i].protomatter.base.type == &protomatter_Protomatter_type) { protomatter_protomatter_obj_t * pm = &displays[i].protomatter; - common_hal_protomatter_protomatter_reconstruct(pm, NULL); + if(!any_display_uses_this_protomatter(pm)) { + common_hal_protomatter_protomatter_deinit(pm); + } #endif } else { // Not an active display bus. diff --git a/supervisor/shared/display.c b/supervisor/shared/display.c index 855432d64..050ba6e19 100644 --- a/supervisor/shared/display.c +++ b/supervisor/shared/display.c @@ -29,6 +29,7 @@ #include #include "py/mpstate.h" +#include "shared-module/displayio/__init__.h" #include "shared-bindings/displayio/Group.h" #include "shared-bindings/displayio/Palette.h" #include "shared-bindings/displayio/TileGrid.h" @@ -112,6 +113,14 @@ void supervisor_display_move_memory(void) { grid->inline_tiles = false; } MP_STATE_VM(terminal_tilegrid_tiles) = NULL; + #if CIRCUITPY_PROTOMATTER + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + if (displays[i].protomatter.base.type == &protomatter_Protomatter_type) { + protomatter_protomatter_obj_t * pm = &displays[i].protomatter; + common_hal_protomatter_protomatter_reconstruct(pm, NULL); + } + } + #endif #endif } diff --git a/supervisor/shared/memory.c b/supervisor/shared/memory.c index 51037bd6d..d52334eb4 100755 --- a/supervisor/shared/memory.c +++ b/supervisor/shared/memory.c @@ -31,7 +31,7 @@ #include "supervisor/shared/display.h" -#define CIRCUITPY_SUPERVISOR_ALLOC_COUNT 8 +#define CIRCUITPY_SUPERVISOR_ALLOC_COUNT (12) static supervisor_allocation allocations[CIRCUITPY_SUPERVISOR_ALLOC_COUNT]; // We use uint32_t* to ensure word (4 byte) alignment. -- cgit v1.2.3 From 1f3821220e396721ab83127f0965032639d613ba Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 1 Apr 2020 16:10:19 -0500 Subject: fix build for non-displayio & non-protomatter targets --- shared-module/displayio/__init__.c | 2 ++ supervisor/shared/display.c | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'supervisor') diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c index dfa3e7473..21cbfa83a 100644 --- a/shared-module/displayio/__init__.c +++ b/shared-module/displayio/__init__.c @@ -21,6 +21,7 @@ primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT]; +#if CIRCUITPY_PROTOMATTER STATIC bool any_display_uses_this_protomatter(protomatter_protomatter_obj_t* pm) { for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) { @@ -32,6 +33,7 @@ STATIC bool any_display_uses_this_protomatter(protomatter_protomatter_obj_t* pm) } return false; } +#endif // Check for recursive calls to displayio_background. bool displayio_background_in_progress = false; diff --git a/supervisor/shared/display.c b/supervisor/shared/display.c index 050ba6e19..cdf710d26 100644 --- a/supervisor/shared/display.c +++ b/supervisor/shared/display.c @@ -29,12 +29,15 @@ #include #include "py/mpstate.h" -#include "shared-module/displayio/__init__.h" #include "shared-bindings/displayio/Group.h" #include "shared-bindings/displayio/Palette.h" #include "shared-bindings/displayio/TileGrid.h" #include "supervisor/memory.h" +#if CIRCUITPY_PROTOMATTER +#include "shared-module/displayio/__init__.h" +#endif + extern size_t blinka_bitmap_data[]; extern displayio_bitmap_t blinka_bitmap; extern displayio_group_t circuitpython_splash; -- cgit v1.2.3 From d8362ef654d86d29561db2cbcc2a20ab488316a8 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 14 Apr 2020 17:34:53 -0500 Subject: displayio: swap colors in palettes too .. change the in-rom palette to be in RGB565 order --- shared-module/displayio/Palette.c | 7 ++++++- supervisor/shared/display.c | 10 +++++----- 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'supervisor') diff --git a/shared-module/displayio/Palette.c b/shared-module/displayio/Palette.c index 3bce86f48..1ef03aab4 100644 --- a/shared-module/displayio/Palette.c +++ b/shared-module/displayio/Palette.c @@ -83,7 +83,12 @@ bool displayio_palette_get_color(displayio_palette_t *self, const _displayio_col } else if (colorspace->grayscale) { *color = self->colors[palette_index].luma >> (8 - colorspace->depth); } else { - *color = self->colors[palette_index].rgb565; + uint16_t packed = self->colors[palette_index].rgb565; + if (colorspace->reverse_bytes_in_word) { + // swap bytes + packed = __builtin_bswap16(packed); + } + *color = packed; } return true; diff --git a/supervisor/shared/display.c b/supervisor/shared/display.c index cdf710d26..95926bc9c 100644 --- a/supervisor/shared/display.c +++ b/supervisor/shared/display.c @@ -169,28 +169,28 @@ _displayio_color_t blinka_colors[7] = { }, { .rgb888 = 0x8428bc, - .rgb565 = 0x7889, + .rgb565 = 0x8978, .luma = 0xff, // We cheat the luma here. It is actually 0x60 .hue = 184, .chroma = 148 }, { .rgb888 = 0xff89bc, - .rgb565 = 0xB8FC, + .rgb565 = 0xFCB8, .luma = 0xb5, .hue = 222, .chroma = 118 }, { .rgb888 = 0x7beffe, - .rgb565 = 0x9F86, + .rgb565 = 0x869F, .luma = 0xe0, .hue = 124, .chroma = 131 }, { .rgb888 = 0x51395f, - .rgb565 = 0x0D5A, + .rgb565 = 0x5A0D, .luma = 0x47, .hue = 185, .chroma = 38 @@ -203,7 +203,7 @@ _displayio_color_t blinka_colors[7] = { }, { .rgb888 = 0x0736a0, - .rgb565 = 0xf501, + .rgb565 = 0x01f5, .luma = 0x44, .hue = 147, .chroma = 153 -- cgit v1.2.3