Skip to content

Commit

Permalink
refactor(freetype): refactor freetype params
Browse files Browse the repository at this point in the history
  • Loading branch information
W-Mai authored and FASTSHIFT committed Mar 15, 2024
1 parent 0b3016c commit 0c84cc0
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 101 deletions.
9 changes: 2 additions & 7 deletions .devcontainer/__lv_conf.h__
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 0 additions & 12 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions docs/libs/freetype.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 3 additions & 14 deletions env_support/cmsis-pack/lv_conf_cmsis.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
9 changes: 2 additions & 7 deletions lv_conf_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 14 additions & 7 deletions src/libs/freetype/lv_freetype.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/libs/freetype/lv_freetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/libs/freetype/lv_freetype_glyph.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* DEFINES
*********************/

#define LV_FREETYPE_GLYPH_DSC_CACHE_SIZE (LV_FREETYPE_CACHE_FT_GLYPH_CNT * 2)
/**********************
* TYPEDEFS
**********************/
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/libs/freetype/lv_freetype_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/libs/freetype/lv_freetype_outline.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions src/libs/freetype/lv_freetype_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);

/**********************
Expand Down
27 changes: 2 additions & 25 deletions src/lv_conf_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/lv_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions tests/src/lv_test_conf_full.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/src/test_cases/libs/test_font_stress.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 0c84cc0

Please sign in to comment.