Use movable allocation system for Sharp display framebuffer.

Hybrid allocation is now part of the infrastructure. Moving memory contents would not be necessary because displayio can recreate them, but does not hurt.
This commit is contained in:
Christian Walther 2020-10-16 23:08:29 +02:00
parent 2ba9805f84
commit ac91220361
2 changed files with 14 additions and 44 deletions

View File

@ -34,32 +34,10 @@
#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h" #include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"
#include "supervisor/memory.h" #include "supervisor/memory.h"
#include "supervisor/shared/safe_mode.h"
#define SHARPMEM_BIT_WRITECMD_LSB (0x80) #define SHARPMEM_BIT_WRITECMD_LSB (0x80)
#define SHARPMEM_BIT_VCOM_LSB (0x40) #define SHARPMEM_BIT_VCOM_LSB (0x40)
static void *hybrid_alloc(size_t sz) {
supervisor_allocation *allocation = allocate_memory(align32_size(sz), false, false);
if (allocation) {
memset(allocation->ptr, 0, sz);
return allocation->ptr;
}
if (gc_alloc_possible()) {
return m_malloc(sz, true);
}
reset_into_safe_mode(MEM_MANAGE);
return NULL; // unreached
}
static inline void hybrid_free(void *ptr_in) {
supervisor_allocation *allocation = allocation_from_ptr(ptr_in);
if (allocation) {
free_memory(allocation);
}
}
STATIC uint8_t bitrev(uint8_t n) { STATIC uint8_t bitrev(uint8_t n) {
uint8_t r = 0; uint8_t r = 0;
for(int i=0;i<8;i++) r |= ((n>>i) & 1)<<(7-i); for(int i=0;i<8;i++) r |= ((n>>i) & 1)<<(7-i);
@ -102,9 +80,9 @@ void common_hal_sharpdisplay_framebuffer_reset(sharpdisplay_framebuffer_obj_t *s
} }
void common_hal_sharpdisplay_framebuffer_reconstruct(sharpdisplay_framebuffer_obj_t *self) { void common_hal_sharpdisplay_framebuffer_reconstruct(sharpdisplay_framebuffer_obj_t *self) {
if (!allocation_from_ptr(self->bufinfo.buf)) { // Look up the allocation by the old pointer and get the new pointer from it.
self->bufinfo.buf = NULL; supervisor_allocation* alloc = allocation_from_ptr(self->bufinfo.buf);
} self->bufinfo.buf = alloc ? alloc->ptr : NULL;
} }
void common_hal_sharpdisplay_framebuffer_get_bufinfo(sharpdisplay_framebuffer_obj_t *self, mp_buffer_info_t *bufinfo) { void common_hal_sharpdisplay_framebuffer_get_bufinfo(sharpdisplay_framebuffer_obj_t *self, mp_buffer_info_t *bufinfo) {
@ -112,7 +90,12 @@ void common_hal_sharpdisplay_framebuffer_get_bufinfo(sharpdisplay_framebuffer_ob
int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride(self); int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride(self);
int height = common_hal_sharpdisplay_framebuffer_get_height(self); int height = common_hal_sharpdisplay_framebuffer_get_height(self);
self->bufinfo.len = row_stride * height + 2; self->bufinfo.len = row_stride * height + 2;
self->bufinfo.buf = hybrid_alloc(self->bufinfo.len); supervisor_allocation* alloc = allocate_memory(align32_size(self->bufinfo.len), false, true);
if (alloc == NULL) {
m_malloc_fail(self->bufinfo.len);
}
self->bufinfo.buf = alloc->ptr;
memset(alloc->ptr, 0, self->bufinfo.len);
uint8_t *data = self->bufinfo.buf; uint8_t *data = self->bufinfo.buf;
*data++ = SHARPMEM_BIT_WRITECMD_LSB; *data++ = SHARPMEM_BIT_WRITECMD_LSB;
@ -123,8 +106,10 @@ void common_hal_sharpdisplay_framebuffer_get_bufinfo(sharpdisplay_framebuffer_ob
} }
self->full_refresh = true; self->full_refresh = true;
} }
if (bufinfo) {
*bufinfo = self->bufinfo; *bufinfo = self->bufinfo;
} }
}
void common_hal_sharpdisplay_framebuffer_deinit(sharpdisplay_framebuffer_obj_t *self) { void common_hal_sharpdisplay_framebuffer_deinit(sharpdisplay_framebuffer_obj_t *self) {
if (self->base.type != &sharpdisplay_framebuffer_type) { if (self->base.type != &sharpdisplay_framebuffer_type) {
@ -137,7 +122,7 @@ void common_hal_sharpdisplay_framebuffer_deinit(sharpdisplay_framebuffer_obj_t *
common_hal_reset_pin(self->chip_select.pin); common_hal_reset_pin(self->chip_select.pin);
hybrid_free(self->bufinfo.buf); free_memory(allocation_from_ptr(self->bufinfo.buf));
memset(self, 0, sizeof(*self)); memset(self, 0, sizeof(*self));
} }
@ -154,19 +139,7 @@ void common_hal_sharpdisplay_framebuffer_construct(sharpdisplay_framebuffer_obj_
self->height = height; self->height = height;
self->baudrate = baudrate; self->baudrate = baudrate;
int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride(self); common_hal_sharpdisplay_framebuffer_get_bufinfo(self, NULL);
self->bufinfo.len = row_stride * height + 2;
// re-use a supervisor allocation if possible
self->bufinfo.buf = hybrid_alloc(self->bufinfo.len);
uint8_t *data = self->bufinfo.buf;
*data++ = SHARPMEM_BIT_WRITECMD_LSB;
for(int y=0; y<self->height; y++) {
*data = bitrev(y+1);
data += row_stride;
}
self->full_refresh = true;
} }
void common_hal_sharpdisplay_framebuffer_swapbuffers(sharpdisplay_framebuffer_obj_t *self, uint8_t *dirty_row_bitmask) { void common_hal_sharpdisplay_framebuffer_swapbuffers(sharpdisplay_framebuffer_obj_t *self, uint8_t *dirty_row_bitmask) {
@ -271,7 +244,5 @@ const framebuffer_p_t sharpdisplay_framebuffer_proto = {
}; };
void common_hal_sharpdisplay_framebuffer_collect_ptrs(sharpdisplay_framebuffer_obj_t *self) { void common_hal_sharpdisplay_framebuffer_collect_ptrs(sharpdisplay_framebuffer_obj_t *self) {
gc_collect_ptr(self->framebuffer);
gc_collect_ptr(self->bus); gc_collect_ptr(self->bus);
gc_collect_ptr(self->bufinfo.buf);
} }

View File

@ -33,7 +33,6 @@
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
mp_obj_t framebuffer;
busio_spi_obj_t* bus; busio_spi_obj_t* bus;
busio_spi_obj_t inline_bus; busio_spi_obj_t inline_bus;
digitalio_digitalinout_obj_t chip_select; digitalio_digitalinout_obj_t chip_select;