diff --git a/.devcontainer/__lv_conf.h__ b/.devcontainer/__lv_conf.h__ index f60d1bcb1d..813b71d87e 100644 --- a/.devcontainer/__lv_conf.h__ +++ b/.devcontainer/__lv_conf.h__ @@ -667,16 +667,11 @@ /*FreeType library*/ #define LV_USE_FREETYPE 0 #if LV_USE_FREETYPE - /*Memory used by FreeType to cache characters in kilobytes*/ - #define LV_FREETYPE_CACHE_SIZE 768 - /*Let FreeType to use LVGL memory and file porting*/ #define LV_FREETYPE_USE_LVGL_PORT 0 - /* Maximum number of opened FT_Face/FT_Size objects managed by this cache instance. */ - /* (0:use system defaults) */ - #define LV_FREETYPE_CACHE_FT_FACES 8 - #define LV_FREETYPE_CACHE_FT_SIZES 8 + /*Cache count of the glyphs in FreeType. It means the number of glyphs that can be cached. + *The higher the value, the more memory will be used.*/ #define LV_FREETYPE_CACHE_FT_GLYPH_CNT 256 #endif diff --git a/Kconfig b/Kconfig index b7dfdb0173..c947b375f7 100644 --- a/Kconfig +++ b/Kconfig @@ -1103,22 +1103,10 @@ menu "LVGL configuration" config LV_USE_FREETYPE bool "FreeType library" - config LV_FREETYPE_CACHE_SIZE - int "Memory used by FreeType to cache characters in kilobytes" - default 768 - depends on LV_USE_FREETYPE config LV_FREETYPE_USE_LVGL_PORT bool "Let FreeType to use LVGL memory and file porting" default n depends on LV_USE_FREETYPE - config LV_FREETYPE_CACHE_FT_FACES - int "The maximum number of FT_Face" - default 8 - depends on LV_USE_FREETYPE - config LV_FREETYPE_CACHE_FT_SIZES - int "The maximum number of FT_Size" - default 8 - depends on LV_USE_FREETYPE config LV_FREETYPE_CACHE_FT_GLYPH_CNT int "The maximum number of Glyph in count" default 256 diff --git a/docs/libs/freetype.rst b/docs/libs/freetype.rst index c666e06dff..8adf5c047b 100644 --- a/docs/libs/freetype.rst +++ b/docs/libs/freetype.rst @@ -69,10 +69,7 @@ Enable :c:macro:`LV_USE_FREETYPE` in ``lv_conf.h``. Cache configuration: -- :c:macro:`LV_FREETYPE_CACHE_SIZE` Maximum memory(Bytes) used to cache font bitmap, outline, character maps, etc. - :Note: This value does not include the memory used by ``FT_Face`` and ``FT_Size`` objects -- :c:macro:`LV_FREETYPE_CACHE_FT_FACES`: Maximum open number of ``FT_Face`` objects. -- :c:macro:`LV_FREETYPE_CACHE_FT_SIZES`: Maximum open number of ``FT_Size`` objects. +- :c:macro:`LV_FREETYPE_CACHE_FT_GLYPH_CNT` Maximum number of cached glyphs., etc. By default, the FreeType extension doesn't use LVGL's file system. You can simply pass the path to the font as usual on your operating system diff --git a/env_support/cmsis-pack/lv_conf_cmsis.h b/env_support/cmsis-pack/lv_conf_cmsis.h index c9a99d255c..b481aa4a19 100644 --- a/env_support/cmsis-pack/lv_conf_cmsis.h +++ b/env_support/cmsis-pack/lv_conf_cmsis.h @@ -637,23 +637,12 @@ /*FreeType library*/ #if LV_USE_FREETYPE - /*Memory used by FreeType to cache characters in kilobytes*/ - #define LV_FREETYPE_CACHE_SIZE 768 - /*Let FreeType to use LVGL memory and file porting*/ #define LV_FREETYPE_USE_LVGL_PORT 0 - /*FreeType cache type: - * LV_FREETYPE_CACHE_TYPE_IMAGE - Image cache - * LV_FREETYPE_CACHE_TYPE_SBIT - Sbit cache - * LV_FREETYPE_CACHE_TYPE_OUTLINE - Outline cache*/ - #define LV_FREETYPE_CACHE_TYPE LV_FREETYPE_CACHE_TYPE_IMAGE - - /* Maximum number of opened FT_Face/FT_Size objects managed by this cache instance. */ - /* (0:use system defaults) */ - #define LV_FREETYPE_CACHE_FT_FACES 8 - #define LV_FREETYPE_CACHE_FT_SIZES 8 - #define LV_FREETYPE_CACHE_FT_OUTLINES 256 + /*Cache count of the glyphs in FreeType. It means the number of glyphs that can be cached. + *The higher the value, the more memory will be used.*/ + #define LV_FREETYPE_CACHE_FT_GLYPH_CNT 256 #endif /* Built-in TTF decoder */ diff --git a/lv_conf_template.h b/lv_conf_template.h index 8e1b73eb42..e21e12b73b 100644 --- a/lv_conf_template.h +++ b/lv_conf_template.h @@ -697,16 +697,11 @@ /*FreeType library*/ #define LV_USE_FREETYPE 0 #if LV_USE_FREETYPE - /*Memory used by FreeType to cache characters in kilobytes*/ - #define LV_FREETYPE_CACHE_SIZE 768 - /*Let FreeType to use LVGL memory and file porting*/ #define LV_FREETYPE_USE_LVGL_PORT 0 - /* Maximum number of opened FT_Face/FT_Size objects managed by this cache instance. */ - /* (0:use system defaults) */ - #define LV_FREETYPE_CACHE_FT_FACES 8 - #define LV_FREETYPE_CACHE_FT_SIZES 8 + /*Cache count of the glyphs in FreeType. It means the number of glyphs that can be cached. + *The higher the value, the more memory will be used.*/ #define LV_FREETYPE_CACHE_FT_GLYPH_CNT 256 #endif diff --git a/src/libs/freetype/lv_freetype.c b/src/libs/freetype/lv_freetype.c index 2e6b61b57d..b79fecc84d 100755 --- a/src/libs/freetype/lv_freetype.c +++ b/src/libs/freetype/lv_freetype.c @@ -43,7 +43,7 @@ typedef struct { static void lv_freetype_cleanup(lv_freetype_context_t * ctx); static FTC_FaceID lv_freetype_req_face_id(lv_freetype_context_t * ctx, const char * pathname); static void lv_freetype_drop_face_id(lv_freetype_context_t * ctx, FTC_FaceID face_id); -static bool freetype_on_font_create(lv_freetype_font_dsc_t * dsc); +static bool freetype_on_font_create(lv_freetype_font_dsc_t * dsc, uint32_t max_glyph_cnt); static void freetype_on_font_set_cbs(lv_freetype_font_dsc_t * dsc); static bool cache_node_cache_create_cb(lv_freetype_cache_node_t * node, void * user_data); @@ -62,7 +62,7 @@ static lv_cache_compare_res_t cache_node_cache_compare_cb(const lv_freetype_cach * GLOBAL FUNCTIONS **********************/ -lv_result_t lv_freetype_init(void) +lv_result_t lv_freetype_init(uint32_t max_glyph_cnt) { if(ft_ctx) { LV_LOG_WARN("freetype already initialized"); @@ -77,6 +77,9 @@ lv_result_t lv_freetype_init(void) } lv_freetype_context_t * ctx = lv_freetype_get_context(); + + ctx->max_glyph_cnt = max_glyph_cnt; + FT_Error error; error = FT_Init_FreeType(&ctx->library); @@ -147,7 +150,7 @@ lv_font_t * lv_freetype_font_create(const char * pathname, lv_freetype_font_rend dsc->cache_node = lv_cache_entry_get_data(cache_node_entry); dsc->cache_node_entry = cache_node_entry; - if(cache_hitting == false && freetype_on_font_create(dsc) == false) { + if(cache_hitting == false && freetype_on_font_create(dsc, ctx->max_glyph_cnt) == false) { lv_cache_release(ctx->cache_node_cache, dsc->cache_node_entry, NULL); lv_freetype_drop_face_id(ctx, dsc->face_id); lv_free(dsc); @@ -223,9 +226,13 @@ const char * lv_freetype_get_pathname(FTC_FaceID face_id) * STATIC FUNCTIONS **********************/ -static bool freetype_on_font_create(lv_freetype_font_dsc_t * dsc) +static bool freetype_on_font_create(lv_freetype_font_dsc_t * dsc, uint32_t max_glyph_cnt) { - lv_cache_t * glyph_cache = lv_freetype_create_glyph_cache(); + /* + * Glyph info uses a small amount of memory, and uses glyph info more frequently, + * so it plans to use twice the maximum number of caches here to + * get a better info acquisition performance.*/ + lv_cache_t * glyph_cache = lv_freetype_create_glyph_cache(max_glyph_cnt * 2); if(glyph_cache == NULL) { LV_LOG_ERROR("glyph cache creating failed"); return false; @@ -234,10 +241,10 @@ static bool freetype_on_font_create(lv_freetype_font_dsc_t * dsc) lv_cache_t * draw_data_cache = NULL; if(dsc->render_mode == LV_FREETYPE_FONT_RENDER_MODE_BITMAP) { - draw_data_cache = lv_freetype_create_draw_data_image(); + draw_data_cache = lv_freetype_create_draw_data_image(max_glyph_cnt); } else if(dsc->render_mode == LV_FREETYPE_FONT_RENDER_MODE_OUTLINE) { - draw_data_cache = lv_freetype_create_draw_data_outline(); + draw_data_cache = lv_freetype_create_draw_data_outline(max_glyph_cnt); } else { LV_LOG_ERROR("unknown render mode"); diff --git a/src/libs/freetype/lv_freetype.h b/src/libs/freetype/lv_freetype.h index f1b7627c74..01f1f3e685 100755 --- a/src/libs/freetype/lv_freetype.h +++ b/src/libs/freetype/lv_freetype.h @@ -78,7 +78,7 @@ typedef struct { * Initialize the freetype library. * @return LV_RESULT_OK on success, otherwise LV_RESULT_INVALID. */ -lv_result_t lv_freetype_init(void); +lv_result_t lv_freetype_init(uint32_t max_glyph_cnt); /** * Uninitialize the freetype library diff --git a/src/libs/freetype/lv_freetype_glyph.c b/src/libs/freetype/lv_freetype_glyph.c index 341b300c46..00d159d840 100644 --- a/src/libs/freetype/lv_freetype_glyph.c +++ b/src/libs/freetype/lv_freetype_glyph.c @@ -15,7 +15,6 @@ * DEFINES *********************/ -#define LV_FREETYPE_GLYPH_DSC_CACHE_SIZE (LV_FREETYPE_CACHE_FT_GLYPH_CNT * 2) /********************** * TYPEDEFS **********************/ @@ -49,7 +48,7 @@ static lv_cache_compare_res_t freetype_glyph_compare_cb(const lv_freetype_glyph_ * GLOBAL FUNCTIONS **********************/ -lv_cache_t * lv_freetype_create_glyph_cache(void) +lv_cache_t * lv_freetype_create_glyph_cache(uint32_t cache_size) { lv_cache_ops_t ops = { .create_cb = (lv_cache_create_cb_t)freetype_glyph_create_cb, @@ -58,7 +57,7 @@ lv_cache_t * lv_freetype_create_glyph_cache(void) }; lv_cache_t * glyph_cache = lv_cache_create(&lv_cache_class_lru_rb_count, sizeof(lv_freetype_glyph_cache_data_t), - LV_FREETYPE_GLYPH_DSC_CACHE_SIZE, ops); + cache_size, ops); return glyph_cache; } diff --git a/src/libs/freetype/lv_freetype_image.c b/src/libs/freetype/lv_freetype_image.c index 60be3be1ff..d98e95df08 100755 --- a/src/libs/freetype/lv_freetype_image.c +++ b/src/libs/freetype/lv_freetype_image.c @@ -51,7 +51,7 @@ static void freetype_image_release_cb(const lv_font_t * font, lv_font_glyph_dsc_ * GLOBAL FUNCTIONS **********************/ -lv_cache_t * lv_freetype_create_draw_data_image(void) +lv_cache_t * lv_freetype_create_draw_data_image(uint32_t cache_size) { lv_cache_ops_t ops = { .compare_cb = (lv_cache_compare_cb_t)freetype_image_compare_cb, @@ -60,7 +60,7 @@ lv_cache_t * lv_freetype_create_draw_data_image(void) }; lv_cache_t * draw_data_cache = lv_cache_create(&lv_cache_class_lru_rb_count, sizeof(lv_freetype_image_cache_data_t), - LV_FREETYPE_CACHE_FT_GLYPH_CNT, ops); + cache_size, ops); return draw_data_cache; } diff --git a/src/libs/freetype/lv_freetype_outline.c b/src/libs/freetype/lv_freetype_outline.c index acc9b2866a..788703486e 100755 --- a/src/libs/freetype/lv_freetype_outline.c +++ b/src/libs/freetype/lv_freetype_outline.c @@ -56,7 +56,7 @@ static lv_cache_compare_res_t freetype_glyph_outline_cmp_cb(const lv_freetype_ou * GLOBAL FUNCTIONS **********************/ -lv_cache_t * lv_freetype_create_draw_data_outline(void) +lv_cache_t * lv_freetype_create_draw_data_outline(uint32_t cache_size) { lv_cache_ops_t glyph_outline_cache_ops = { .create_cb = (lv_cache_create_cb_t)freetype_glyph_outline_create_cb, @@ -65,7 +65,7 @@ lv_cache_t * lv_freetype_create_draw_data_outline(void) }; lv_cache_t * draw_data_cache = lv_cache_create(&lv_cache_class_lru_rb_count, sizeof(lv_freetype_outline_node_t), - LV_FREETYPE_CACHE_FT_GLYPH_CNT, + cache_size, glyph_outline_cache_ops); return draw_data_cache; diff --git a/src/libs/freetype/lv_freetype_private.h b/src/libs/freetype/lv_freetype_private.h index c3dd296c55..4bb89007fc 100755 --- a/src/libs/freetype/lv_freetype_private.h +++ b/src/libs/freetype/lv_freetype_private.h @@ -38,10 +38,6 @@ extern "C" { LV_LOG_ERROR(msg " error(0x%x)", (int)error_code) #endif -#if LV_FREETYPE_CACHE_SIZE <= 0 -#error "LV_FREETYPE_CACHE_SIZE must > 0" -#endif - #define LV_FREETYPE_FONT_DSC_MAGIC_NUM 0x5F5F4654 /* '__FT' */ #define LV_FREETYPE_FONT_DSC_HAS_MAGIC_NUM(dsc) ((dsc)->magic_num == LV_FREETYPE_FONT_DSC_MAGIC_NUM) #define LV_ASSERT_FREETYPE_FONT_DSC(dsc) \ @@ -83,6 +79,8 @@ typedef struct _lv_freetype_context_t { lv_ll_t face_id_ll; lv_event_cb_t event_cb; + uint32_t max_glyph_cnt; + lv_cache_t * cache_node_cache; } lv_freetype_context_t; @@ -123,13 +121,13 @@ int32_t lv_freetype_italic_transform_on_pos(lv_point_t point); const char * lv_freetype_get_pathname(FTC_FaceID face_id); -lv_cache_t * lv_freetype_create_glyph_cache(void); +lv_cache_t * lv_freetype_create_glyph_cache(uint32_t cache_size); void lv_freetype_set_cbs_glyph(lv_freetype_font_dsc_t * dsc); -lv_cache_t * lv_freetype_create_draw_data_image(void); +lv_cache_t * lv_freetype_create_draw_data_image(uint32_t cache_size); void lv_freetype_set_cbs_image_font(lv_freetype_font_dsc_t * dsc); -lv_cache_t * lv_freetype_create_draw_data_outline(void); +lv_cache_t * lv_freetype_create_draw_data_outline(uint32_t cache_size); void lv_freetype_set_cbs_outline_font(lv_freetype_font_dsc_t * dsc); /********************** diff --git a/src/lv_conf_internal.h b/src/lv_conf_internal.h index d75c93ea83..a8f7531308 100644 --- a/src/lv_conf_internal.h +++ b/src/lv_conf_internal.h @@ -2285,15 +2285,6 @@ #endif #endif #if LV_USE_FREETYPE - /*Memory used by FreeType to cache characters in kilobytes*/ - #ifndef LV_FREETYPE_CACHE_SIZE - #ifdef CONFIG_LV_FREETYPE_CACHE_SIZE - #define LV_FREETYPE_CACHE_SIZE CONFIG_LV_FREETYPE_CACHE_SIZE - #else - #define LV_FREETYPE_CACHE_SIZE 768 - #endif - #endif - /*Let FreeType to use LVGL memory and file porting*/ #ifndef LV_FREETYPE_USE_LVGL_PORT #ifdef CONFIG_LV_FREETYPE_USE_LVGL_PORT @@ -2303,22 +2294,8 @@ #endif #endif - /* Maximum number of opened FT_Face/FT_Size objects managed by this cache instance. */ - /* (0:use system defaults) */ - #ifndef LV_FREETYPE_CACHE_FT_FACES - #ifdef CONFIG_LV_FREETYPE_CACHE_FT_FACES - #define LV_FREETYPE_CACHE_FT_FACES CONFIG_LV_FREETYPE_CACHE_FT_FACES - #else - #define LV_FREETYPE_CACHE_FT_FACES 8 - #endif - #endif - #ifndef LV_FREETYPE_CACHE_FT_SIZES - #ifdef CONFIG_LV_FREETYPE_CACHE_FT_SIZES - #define LV_FREETYPE_CACHE_FT_SIZES CONFIG_LV_FREETYPE_CACHE_FT_SIZES - #else - #define LV_FREETYPE_CACHE_FT_SIZES 8 - #endif - #endif + /*Cache count of the glyphs in FreeType. It means the number of glyphs that can be cached. + *The higher the value, the more memory will be used.*/ #ifndef LV_FREETYPE_CACHE_FT_GLYPH_CNT #ifdef CONFIG_LV_FREETYPE_CACHE_FT_GLYPH_CNT #define LV_FREETYPE_CACHE_FT_GLYPH_CNT CONFIG_LV_FREETYPE_CACHE_FT_GLYPH_CNT diff --git a/src/lv_init.c b/src/lv_init.c index 9f89e4799c..cb4f23c85e 100644 --- a/src/lv_init.c +++ b/src/lv_init.c @@ -296,11 +296,7 @@ void lv_init(void) #if LV_USE_FREETYPE /*Init freetype library*/ -# if LV_FREETYPE_CACHE_SIZE >= 0 - lv_freetype_init(); -# else - lv_freetype_init(0, 0, 0); -# endif + lv_freetype_init(LV_FREETYPE_CACHE_FT_GLYPH_CNT); #endif #if LV_USE_TINY_TTF diff --git a/tests/src/lv_test_conf_full.h b/tests/src/lv_test_conf_full.h index 251b4aee34..917df77f5c 100644 --- a/tests/src/lv_test_conf_full.h +++ b/tests/src/lv_test_conf_full.h @@ -123,8 +123,5 @@ #endif #define LV_USE_FREETYPE 1 -#define LV_FREETYPE_CACHE_SIZE 768 #define LV_FREETYPE_USE_LVGL_PORT 0 -#define LV_FREETYPE_CACHE_FT_FACES 8 -#define LV_FREETYPE_CACHE_FT_SIZES 8 -#define LV_FREETYPE_CACHE_FT_OUTLINES 10 +#define LV_FREETYPE_CACHE_FT_GLYPH_CNT 10 diff --git a/tests/src/test_cases/libs/test_font_stress.c b/tests/src/test_cases/libs/test_font_stress.c index 0a4031dea8..5fef7546b9 100644 --- a/tests/src/test_cases/libs/test_font_stress.c +++ b/tests/src/test_cases/libs/test_font_stress.c @@ -151,7 +151,7 @@ static void update_cb(void) void setUp(void) { - lv_freetype_init(0, 0, 0); + lv_freetype_init(LV_FREETYPE_CACHE_FT_GLYPH_CNT); g_ctx.par = lv_scr_act();