From cf48783f6fba32fb1e98b899161bd2ed020cd017 Mon Sep 17 00:00:00 2001 From: _VIFEXTech Date: Tue, 7 Feb 2023 15:01:09 +0800 Subject: [PATCH] feat(img_cache): decouple the life flag from _lv_img_cache_entry_t (#3970) Signed-off-by: pengyiqiang --- src/draw/lv_img_cache.h | 6 +----- src/draw/lv_img_cache_builtin.c | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/draw/lv_img_cache.h b/src/draw/lv_img_cache.h index 8b1d78147..5ecb5fcd2 100644 --- a/src/draw/lv_img_cache.h +++ b/src/draw/lv_img_cache.h @@ -30,11 +30,7 @@ extern "C" { */ typedef struct { lv_img_decoder_dsc_t dec_dsc; /**< Image information*/ - - /** Count the cache entries's life. Add `time_to_open` to `life` when the entry is used. - * Decrement all lifes by one every in every ::lv_img_cache_open. - * If life == 0 the entry can be reused*/ - int32_t life; + void * user_data; /**< Image cache entry user data*/ } _lv_img_cache_entry_t; typedef struct { diff --git a/src/draw/lv_img_cache_builtin.c b/src/draw/lv_img_cache_builtin.c index 2c575840c..ca383aca8 100644 --- a/src/draw/lv_img_cache_builtin.c +++ b/src/draw/lv_img_cache_builtin.c @@ -17,6 +17,13 @@ /********************* * DEFINES *********************/ + +/** Count the cache entries's life. Add `time_to_open` to `life` when the entry is used. + * Decrement all lifes by one every in every ::lv_img_cache_open. + * If life == 0 the entry can be reused*/ +#define CACHE_GET_LIFE(entry) ((lv_intptr_t)((entry)->user_data)) +#define CACHE_SET_LIFE(entry, v) ((entry)->user_data = (void*)((lv_intptr_t)(v))) + /*Decrement life with this value on every open*/ #define LV_IMG_CACHE_AGING 1 @@ -100,8 +107,9 @@ static _lv_img_cache_entry_t * _lv_img_cache_open_builtin(const void * src, lv_c /*Decrement all lifes. Make the entries older*/ uint16_t i; for(i = 0; i < entry_cnt; i++) { - if(cache[i].life > INT32_MIN + LV_IMG_CACHE_AGING) { - cache[i].life -= LV_IMG_CACHE_AGING; + int32_t life = CACHE_GET_LIFE(&cache[i]); + if(life > INT32_MIN + LV_IMG_CACHE_AGING) { + CACHE_SET_LIFE(&cache[i], life - LV_IMG_CACHE_AGING); } } @@ -113,8 +121,9 @@ static _lv_img_cache_entry_t * _lv_img_cache_open_builtin(const void * src, lv_c *Image difficult to open should live longer to keep avoid frequent their recaching. *Therefore increase `life` with `time_to_open`*/ cached_src = &cache[i]; - cached_src->life += cached_src->dec_dsc.time_to_open * LV_IMG_CACHE_LIFE_GAIN; - if(cached_src->life > LV_IMG_CACHE_LIFE_LIMIT) cached_src->life = LV_IMG_CACHE_LIFE_LIMIT; + int32_t life = CACHE_GET_LIFE(cached_src); + CACHE_SET_LIFE(cached_src, life + cached_src->dec_dsc.time_to_open * LV_IMG_CACHE_LIFE_GAIN); + if(CACHE_GET_LIFE(cached_src) > LV_IMG_CACHE_LIFE_LIMIT) CACHE_SET_LIFE(cached_src, LV_IMG_CACHE_LIFE_LIMIT); LV_LOG_TRACE("image source found in the cache"); break; } @@ -126,7 +135,7 @@ static _lv_img_cache_entry_t * _lv_img_cache_open_builtin(const void * src, lv_c /*Find an entry to reuse. Select the entry with the least life*/ cached_src = &cache[0]; for(i = 1; i < entry_cnt; i++) { - if(cache[i].life < cached_src->life) { + if(CACHE_GET_LIFE(&cache[i]) < CACHE_GET_LIFE(cached_src)) { cached_src = &cache[i]; } } @@ -148,11 +157,11 @@ static _lv_img_cache_entry_t * _lv_img_cache_open_builtin(const void * src, lv_c if(open_res == LV_RES_INV) { LV_LOG_WARN("Image draw cannot open the image resource"); lv_memzero(cached_src, sizeof(_lv_img_cache_entry_t)); - cached_src->life = INT32_MIN; /*Make the empty entry very "weak" to force its us*/ + CACHE_SET_LIFE(cached_src, INT32_MIN); /*Make the empty entry very "weak" to force its us*/ return NULL; } - cached_src->life = 0; + CACHE_SET_LIFE(cached_src, 0); /*If `time_to_open` was not set in the open function set it here*/ if(cached_src->dec_dsc.time_to_open == 0) {