From 000699827fd3c6d0fc9281a2c69d2e513cae0c9e Mon Sep 17 00:00:00 2001 From: Benign X <1341398182@qq.com> Date: Wed, 27 Mar 2024 14:36:29 +0800 Subject: [PATCH] feat(nuttx): add defer feature for nuttx image cache (#5967) --- src/drivers/nuttx/lv_nuttx_image_cache.c | 34 ++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/drivers/nuttx/lv_nuttx_image_cache.c b/src/drivers/nuttx/lv_nuttx_image_cache.c index 839704c12..50c9c7312 100644 --- a/src/drivers/nuttx/lv_nuttx_image_cache.c +++ b/src/drivers/nuttx/lv_nuttx_image_cache.c @@ -34,6 +34,8 @@ typedef struct { struct mm_heap_s * heap; uint32_t heap_size; + + bool initialized; } lv_nuttx_ctx_image_cache_t; /********************** * STATIC PROTOTYPES @@ -63,10 +65,29 @@ void lv_nuttx_image_cache_init(void) ctx = lv_malloc_zeroed(sizeof(lv_nuttx_ctx_image_cache_t)); LV_ASSERT_MALLOC(ctx); - ctx->mem_size = LV_CACHE_DEF_SIZE; + ctx->initialized = false; +} + +/********************** + * STATIC FUNCTIONS + **********************/ + +static bool defer_init(void) +{ + if(ctx->mem != NULL && ctx->heap != NULL) { + return true; + } + + ctx->mem_size = img_cache_p->max_size; ctx->mem = malloc(ctx->mem_size); LV_ASSERT_MALLOC(ctx->mem); + if(ctx->mem == NULL) { + LV_LOG_ERROR("Failed to allocate memory for image cache"); + ctx->initialized = false; + return false; + } + ctx->heap = mm_initialize( HEAP_NAME, ctx->mem, @@ -86,16 +107,19 @@ void lv_nuttx_image_cache_init(void) LV_LOG_USER(" mxordblk: %d", info.mxordblk); LV_LOG_USER(" uordblks: %d", info.uordblks); LV_LOG_USER(" fordblks: %d", info.fordblks); -} -/********************** - * STATIC FUNCTIONS - **********************/ + ctx->initialized = true; + return true; +} static void * malloc_cb(size_t size_bytes, lv_color_format_t color_format) { LV_UNUSED(color_format); + if(ctx->initialized == false) { + if(defer_init() == false) return NULL; + } + /*Allocate larger memory to be sure it can be aligned as needed*/ size_bytes += LV_DRAW_BUF_ALIGN - 1; uint32_t cache_max_size = lv_cache_get_max_size(img_cache_p, NULL);