diff options
author | William Wilgus <wilgus.william@gmail.com> | 2023-01-30 03:43:16 -0500 |
---|---|---|
committer | William Wilgus <wilgus.william@gmail.com> | 2023-01-30 23:36:34 -0500 |
commit | c955d93075a1eaefc409bfa0389769a5353842f3 (patch) | |
tree | 4d7e234e46f9e78ebbb77552919e30287352738e | |
parent | 78718aa7eb2b05d0128a5e08482d4f7c9b716f11 (diff) | |
download | rockbox-c955d93075a1eaefc409bfa0389769a5353842f3.tar.gz rockbox-c955d93075a1eaefc409bfa0389769a5353842f3.zip |
font.c dynamic path name
only alloc enough space for the path instead of a fixed buffer
round up the path buffer size to slightly more thna the longest
common path length to reduce reallocations
Change-Id: I51b8b17584d6a905ea3a66a7c38b7b1b9da2e786
-rw-r--r-- | firmware/font.c | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/firmware/font.c b/firmware/font.c index 2a2975cbf2..69c4fd40f8 100644 --- a/firmware/font.c +++ b/firmware/font.c | |||
@@ -44,8 +44,15 @@ | |||
44 | #include "diacritic.h" | 44 | #include "diacritic.h" |
45 | #include "rbpaths.h" | 45 | #include "rbpaths.h" |
46 | 46 | ||
47 | /* Define LOGF_ENABLE to enable logf output in this file */ | ||
48 | //#define LOGF_ENABLE | ||
49 | #include "logf.h" | ||
50 | |||
47 | #define MAX_FONTSIZE_FOR_16_BIT_OFFSETS 0xFFDB | 51 | #define MAX_FONTSIZE_FOR_16_BIT_OFFSETS 0xFFDB |
48 | 52 | ||
53 | #define FONT_EXT "fnt" | ||
54 | #define GLYPH_CACHE_EXT "gc" | ||
55 | |||
49 | /* max static loadable font buffer size */ | 56 | /* max static loadable font buffer size */ |
50 | #ifndef MAX_FONT_SIZE | 57 | #ifndef MAX_FONT_SIZE |
51 | #if LCD_HEIGHT > 64 | 58 | #if LCD_HEIGHT > 64 |
@@ -90,7 +97,8 @@ extern struct font sysfont; | |||
90 | 97 | ||
91 | struct buflib_alloc_data { | 98 | struct buflib_alloc_data { |
92 | struct font font; /* must be the first member! */ | 99 | struct font font; /* must be the first member! */ |
93 | char path[MAX_PATH]; /* font path and filename */ | 100 | char *path; /* font path and filename (allocd at end of buffer) */ |
101 | size_t path_bufsz; /* size of path buffer */ | ||
94 | int refcount; /* how many times has this font been loaded? */ | 102 | int refcount; /* how many times has this font been loaded? */ |
95 | unsigned char buffer[]; | 103 | unsigned char buffer[]; |
96 | }; | 104 | }; |
@@ -116,10 +124,11 @@ static int buflibmove_callback(int handle, void* current, void* new) | |||
116 | UPDATE(alloc->font.buffer_start); | 124 | UPDATE(alloc->font.buffer_start); |
117 | UPDATE(alloc->font.buffer_end); | 125 | UPDATE(alloc->font.buffer_end); |
118 | UPDATE(alloc->font.buffer_position); | 126 | UPDATE(alloc->font.buffer_position); |
127 | UPDATE(alloc->path); | ||
119 | 128 | ||
120 | UPDATE(alloc->font.cache._index); | 129 | UPDATE(alloc->font.cache._index); |
121 | UPDATE(alloc->font.cache._lru._base); | 130 | UPDATE(alloc->font.cache._lru._base); |
122 | 131 | logf("%s %s", __func__, alloc->path); | |
123 | return BUFLIB_CB_OK; | 132 | return BUFLIB_CB_OK; |
124 | } | 133 | } |
125 | static void lock_font_handle(int handle, bool lock) | 134 | static void lock_font_handle(int handle, bool lock) |
@@ -336,11 +345,16 @@ static int find_font_index(const char* path) | |||
336 | { | 345 | { |
337 | struct buflib_alloc_data *data = core_get_data(handle); | 346 | struct buflib_alloc_data *data = core_get_data(handle); |
338 | if(!strcmp(data->path, path)) | 347 | if(!strcmp(data->path, path)) |
348 | { | ||
349 | logf("%s Found id: [%d], %s", __func__, index, path); | ||
339 | return index; | 350 | return index; |
351 | } | ||
340 | } | 352 | } |
341 | 353 | ||
342 | index++; | 354 | index++; |
343 | } | 355 | } |
356 | logf("%s %s Not found using id: [%d], FONT_SYSFIXED", | ||
357 | __func__, path, FONT_SYSFIXED); | ||
344 | return FONT_SYSFIXED; | 358 | return FONT_SYSFIXED; |
345 | } | 359 | } |
346 | 360 | ||
@@ -352,6 +366,7 @@ const char* font_filename(int font_id) | |||
352 | if (handle > 0) | 366 | if (handle > 0) |
353 | { | 367 | { |
354 | struct buflib_alloc_data *data = core_get_data(handle); | 368 | struct buflib_alloc_data *data = core_get_data(handle); |
369 | logf("%s id: [%d], %s", __func__, font_id, data->path); | ||
355 | return data->path; | 370 | return data->path; |
356 | } | 371 | } |
357 | 372 | ||
@@ -468,7 +483,7 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs ) | |||
468 | { | 483 | { |
469 | /* already loaded, no need to reload */ | 484 | /* already loaded, no need to reload */ |
470 | struct buflib_alloc_data *pd = core_get_data(buflib_allocations[font_id]); | 485 | struct buflib_alloc_data *pd = core_get_data(buflib_allocations[font_id]); |
471 | if (pd->font.buffer_size < bufsize) | 486 | if (pd->font.buffer_size < bufsize || pd->path_bufsz < path_len) |
472 | { | 487 | { |
473 | int old_refcount, old_id; | 488 | int old_refcount, old_id; |
474 | size_t old_bufsize = pd->font.buffer_size; | 489 | size_t old_bufsize = pd->font.buffer_size; |
@@ -523,10 +538,10 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs ) | |||
523 | if ( open_slot == -1 ) | 538 | if ( open_slot == -1 ) |
524 | return -1; | 539 | return -1; |
525 | font_id = open_slot; | 540 | font_id = open_slot; |
526 | 541 | size_t path_bufsz = MAX(path_len + 1, 64); /* enough size for common case */ | |
527 | /* allocate mem */ | 542 | /* allocate mem */ |
528 | int handle = core_alloc_ex( | 543 | int handle = core_alloc_ex( |
529 | bufsize + sizeof( struct buflib_alloc_data ), | 544 | bufsize + path_bufsz + sizeof( struct buflib_alloc_data ), |
530 | &buflibops ); | 545 | &buflibops ); |
531 | if ( handle <= 0 ) | 546 | if ( handle <= 0 ) |
532 | { | 547 | { |
@@ -536,7 +551,7 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs ) | |||
536 | core_pin(handle); | 551 | core_pin(handle); |
537 | pdata = core_get_data(handle); | 552 | pdata = core_get_data(handle); |
538 | pdata->refcount = 1; | 553 | pdata->refcount = 1; |
539 | 554 | pdata->path = pdata->buffer + bufsize; | |
540 | /* save load path so we can recognize this font later */ | 555 | /* save load path so we can recognize this font later */ |
541 | memcpy(pdata->path, path, path_len+1); | 556 | memcpy(pdata->path, path, path_len+1); |
542 | 557 | ||
@@ -592,6 +607,7 @@ int font_load_ex( const char *path, size_t buf_size, int glyphs ) | |||
592 | buflib_allocations[font_id] = handle; | 607 | buflib_allocations[font_id] = handle; |
593 | //printf("%s -> [%d] -> %d\n", path, font_id, *handle); | 608 | //printf("%s -> [%d] -> %d\n", path, font_id, *handle); |
594 | lock_font_handle( handle, false ); | 609 | lock_font_handle( handle, false ); |
610 | logf("%s id: [%d], %s", __func__, font_id, path); | ||
595 | return font_id; /* success!*/ | 611 | return font_id; /* success!*/ |
596 | } | 612 | } |
597 | 613 | ||
@@ -612,6 +628,7 @@ void font_unload(int font_id) | |||
612 | pdata->refcount--; | 628 | pdata->refcount--; |
613 | if (pdata->refcount < 1) | 629 | if (pdata->refcount < 1) |
614 | { | 630 | { |
631 | logf("%s %s", __func__, pdata->path); | ||
615 | if (pf && pf->fd >= 0) | 632 | if (pf && pf->fd >= 0) |
616 | { | 633 | { |
617 | glyph_cache_save(font_id); | 634 | glyph_cache_save(font_id); |
@@ -872,8 +889,8 @@ static void font_path_to_glyph_path( const char *font_path, char *glyph_path) | |||
872 | { | 889 | { |
873 | /* take full file name, cut extension, and add .glyphcache */ | 890 | /* take full file name, cut extension, and add .glyphcache */ |
874 | strmemccpy(glyph_path, font_path, MAX_PATH); | 891 | strmemccpy(glyph_path, font_path, MAX_PATH); |
875 | glyph_path[strlen(glyph_path)-4] = '\0'; | 892 | glyph_path[strlen(glyph_path)- sizeof(FONT_EXT)] = '\0'; |
876 | strcat(glyph_path, ".gc"); | 893 | strcat(glyph_path, "." GLYPH_CACHE_EXT); |
877 | } | 894 | } |
878 | 895 | ||
879 | /* call with NULL to flush */ | 896 | /* call with NULL to flush */ |