refactor(freetype): refactor freetype params

This commit is contained in:
Benign X
2024-03-15 12:41:03 +08:00
committed by VIFEXT
parent 0b3016c0e5
commit 0c84cc0b3b
15 changed files with 39 additions and 101 deletions
+2 -7
View File
@@ -667,16 +667,11 @@
/*FreeType library*/ /*FreeType library*/
#define LV_USE_FREETYPE 0 #define LV_USE_FREETYPE 0
#if LV_USE_FREETYPE #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*/ /*Let FreeType to use LVGL memory and file porting*/
#define LV_FREETYPE_USE_LVGL_PORT 0 #define LV_FREETYPE_USE_LVGL_PORT 0
/* Maximum number of opened FT_Face/FT_Size objects managed by this cache instance. */ /*Cache count of the glyphs in FreeType. It means the number of glyphs that can be cached.
/* (0:use system defaults) */ *The higher the value, the more memory will be used.*/
#define LV_FREETYPE_CACHE_FT_FACES 8
#define LV_FREETYPE_CACHE_FT_SIZES 8
#define LV_FREETYPE_CACHE_FT_GLYPH_CNT 256 #define LV_FREETYPE_CACHE_FT_GLYPH_CNT 256
#endif #endif
-12
View File
@@ -1103,22 +1103,10 @@ menu "LVGL configuration"
config LV_USE_FREETYPE config LV_USE_FREETYPE
bool "FreeType library" 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 config LV_FREETYPE_USE_LVGL_PORT
bool "Let FreeType to use LVGL memory and file porting" bool "Let FreeType to use LVGL memory and file porting"
default n default n
depends on LV_USE_FREETYPE 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 config LV_FREETYPE_CACHE_FT_GLYPH_CNT
int "The maximum number of Glyph in count" int "The maximum number of Glyph in count"
default 256 default 256
+1 -4
View File
@@ -69,10 +69,7 @@ Enable :c:macro:`LV_USE_FREETYPE` in ``lv_conf.h``.
Cache configuration: Cache configuration:
- :c:macro:`LV_FREETYPE_CACHE_SIZE` Maximum memory(Bytes) used to cache font bitmap, outline, character maps, etc. - :c:macro:`LV_FREETYPE_CACHE_FT_GLYPH_CNT` Maximum number of cached glyphs., 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.
By default, the FreeType extension doesn't use LVGL's file system. You 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 can simply pass the path to the font as usual on your operating system
+3 -14
View File
@@ -637,23 +637,12 @@
/*FreeType library*/ /*FreeType library*/
#if LV_USE_FREETYPE #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*/ /*Let FreeType to use LVGL memory and file porting*/
#define LV_FREETYPE_USE_LVGL_PORT 0 #define LV_FREETYPE_USE_LVGL_PORT 0
/*FreeType cache type: /*Cache count of the glyphs in FreeType. It means the number of glyphs that can be cached.
* LV_FREETYPE_CACHE_TYPE_IMAGE - Image cache *The higher the value, the more memory will be used.*/
* LV_FREETYPE_CACHE_TYPE_SBIT - Sbit cache #define LV_FREETYPE_CACHE_FT_GLYPH_CNT 256
* 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
#endif #endif
/* Built-in TTF decoder */ /* Built-in TTF decoder */
+2 -7
View File
@@ -697,16 +697,11 @@
/*FreeType library*/ /*FreeType library*/
#define LV_USE_FREETYPE 0 #define LV_USE_FREETYPE 0
#if LV_USE_FREETYPE #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*/ /*Let FreeType to use LVGL memory and file porting*/
#define LV_FREETYPE_USE_LVGL_PORT 0 #define LV_FREETYPE_USE_LVGL_PORT 0
/* Maximum number of opened FT_Face/FT_Size objects managed by this cache instance. */ /*Cache count of the glyphs in FreeType. It means the number of glyphs that can be cached.
/* (0:use system defaults) */ *The higher the value, the more memory will be used.*/
#define LV_FREETYPE_CACHE_FT_FACES 8
#define LV_FREETYPE_CACHE_FT_SIZES 8
#define LV_FREETYPE_CACHE_FT_GLYPH_CNT 256 #define LV_FREETYPE_CACHE_FT_GLYPH_CNT 256
#endif #endif
+14 -7
View File
@@ -43,7 +43,7 @@ typedef struct {
static void lv_freetype_cleanup(lv_freetype_context_t * ctx); 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 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 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 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); 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 * GLOBAL FUNCTIONS
**********************/ **********************/
lv_result_t lv_freetype_init(void) lv_result_t lv_freetype_init(uint32_t max_glyph_cnt)
{ {
if(ft_ctx) { if(ft_ctx) {
LV_LOG_WARN("freetype already initialized"); 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(); lv_freetype_context_t * ctx = lv_freetype_get_context();
ctx->max_glyph_cnt = max_glyph_cnt;
FT_Error error; FT_Error error;
error = FT_Init_FreeType(&ctx->library); 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 = lv_cache_entry_get_data(cache_node_entry);
dsc->cache_node_entry = 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_cache_release(ctx->cache_node_cache, dsc->cache_node_entry, NULL);
lv_freetype_drop_face_id(ctx, dsc->face_id); lv_freetype_drop_face_id(ctx, dsc->face_id);
lv_free(dsc); lv_free(dsc);
@@ -223,9 +226,13 @@ const char * lv_freetype_get_pathname(FTC_FaceID face_id)
* STATIC FUNCTIONS * 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) { if(glyph_cache == NULL) {
LV_LOG_ERROR("glyph cache creating failed"); LV_LOG_ERROR("glyph cache creating failed");
return false; 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; lv_cache_t * draw_data_cache = NULL;
if(dsc->render_mode == LV_FREETYPE_FONT_RENDER_MODE_BITMAP) { 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) { 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 { else {
LV_LOG_ERROR("unknown render mode"); LV_LOG_ERROR("unknown render mode");
+1 -1
View File
@@ -78,7 +78,7 @@ typedef struct {
* Initialize the freetype library. * Initialize the freetype library.
* @return LV_RESULT_OK on success, otherwise LV_RESULT_INVALID. * @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 * Uninitialize the freetype library
+2 -3
View File
@@ -15,7 +15,6 @@
* DEFINES * DEFINES
*********************/ *********************/
#define LV_FREETYPE_GLYPH_DSC_CACHE_SIZE (LV_FREETYPE_CACHE_FT_GLYPH_CNT * 2)
/********************** /**********************
* TYPEDEFS * TYPEDEFS
**********************/ **********************/
@@ -49,7 +48,7 @@ static lv_cache_compare_res_t freetype_glyph_compare_cb(const lv_freetype_glyph_
* GLOBAL FUNCTIONS * 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 = { lv_cache_ops_t ops = {
.create_cb = (lv_cache_create_cb_t)freetype_glyph_create_cb, .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_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; return glyph_cache;
} }
+2 -2
View File
@@ -51,7 +51,7 @@ static void freetype_image_release_cb(const lv_font_t * font, lv_font_glyph_dsc_
* GLOBAL FUNCTIONS * 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 = { lv_cache_ops_t ops = {
.compare_cb = (lv_cache_compare_cb_t)freetype_image_compare_cb, .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_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; return draw_data_cache;
} }
+2 -2
View File
@@ -56,7 +56,7 @@ static lv_cache_compare_res_t freetype_glyph_outline_cmp_cb(const lv_freetype_ou
* GLOBAL FUNCTIONS * 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 = { lv_cache_ops_t glyph_outline_cache_ops = {
.create_cb = (lv_cache_create_cb_t)freetype_glyph_outline_create_cb, .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_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); glyph_outline_cache_ops);
return draw_data_cache; return draw_data_cache;
+5 -7
View File
@@ -38,10 +38,6 @@ extern "C" {
LV_LOG_ERROR(msg " error(0x%x)", (int)error_code) LV_LOG_ERROR(msg " error(0x%x)", (int)error_code)
#endif #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_MAGIC_NUM 0x5F5F4654 /* '__FT' */
#define LV_FREETYPE_FONT_DSC_HAS_MAGIC_NUM(dsc) ((dsc)->magic_num == LV_FREETYPE_FONT_DSC_MAGIC_NUM) #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) \ #define LV_ASSERT_FREETYPE_FONT_DSC(dsc) \
@@ -83,6 +79,8 @@ typedef struct _lv_freetype_context_t {
lv_ll_t face_id_ll; lv_ll_t face_id_ll;
lv_event_cb_t event_cb; lv_event_cb_t event_cb;
uint32_t max_glyph_cnt;
lv_cache_t * cache_node_cache; lv_cache_t * cache_node_cache;
} lv_freetype_context_t; } 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); 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); 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); 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); void lv_freetype_set_cbs_outline_font(lv_freetype_font_dsc_t * dsc);
/********************** /**********************
+2 -25
View File
@@ -2285,15 +2285,6 @@
#endif #endif
#endif #endif
#if LV_USE_FREETYPE #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*/ /*Let FreeType to use LVGL memory and file porting*/
#ifndef LV_FREETYPE_USE_LVGL_PORT #ifndef LV_FREETYPE_USE_LVGL_PORT
#ifdef CONFIG_LV_FREETYPE_USE_LVGL_PORT #ifdef CONFIG_LV_FREETYPE_USE_LVGL_PORT
@@ -2303,22 +2294,8 @@
#endif #endif
#endif #endif
/* Maximum number of opened FT_Face/FT_Size objects managed by this cache instance. */ /*Cache count of the glyphs in FreeType. It means the number of glyphs that can be cached.
/* (0:use system defaults) */ *The higher the value, the more memory will be used.*/
#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
#ifndef LV_FREETYPE_CACHE_FT_GLYPH_CNT #ifndef LV_FREETYPE_CACHE_FT_GLYPH_CNT
#ifdef CONFIG_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 #define LV_FREETYPE_CACHE_FT_GLYPH_CNT CONFIG_LV_FREETYPE_CACHE_FT_GLYPH_CNT
+1 -5
View File
@@ -296,11 +296,7 @@ void lv_init(void)
#if LV_USE_FREETYPE #if LV_USE_FREETYPE
/*Init freetype library*/ /*Init freetype library*/
# if LV_FREETYPE_CACHE_SIZE >= 0 lv_freetype_init(LV_FREETYPE_CACHE_FT_GLYPH_CNT);
lv_freetype_init();
# else
lv_freetype_init(0, 0, 0);
# endif
#endif #endif
#if LV_USE_TINY_TTF #if LV_USE_TINY_TTF
+1 -4
View File
@@ -123,8 +123,5 @@
#endif #endif
#define LV_USE_FREETYPE 1 #define LV_USE_FREETYPE 1
#define LV_FREETYPE_CACHE_SIZE 768
#define LV_FREETYPE_USE_LVGL_PORT 0 #define LV_FREETYPE_USE_LVGL_PORT 0
#define LV_FREETYPE_CACHE_FT_FACES 8 #define LV_FREETYPE_CACHE_FT_GLYPH_CNT 10
#define LV_FREETYPE_CACHE_FT_SIZES 8
#define LV_FREETYPE_CACHE_FT_OUTLINES 10
+1 -1
View File
@@ -151,7 +151,7 @@ static void update_cb(void)
void setUp(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(); g_ctx.par = lv_scr_act();