From 74f1887d7e04a7c9ea5cf36cad5a49194f1d507f Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Mon, 12 Feb 2024 17:10:27 -0800 Subject: [PATCH 1/3] Refactor: BLF Use Vector for Glyph Cache List Replace FontBLF's ListBase with a GlyphCacheListBLF class that uses a vector of GlyphCacheBLF, holds the ThreadMutex, has methods for acquire, release, clear. Seems like a simple first step. --- source/blender/blenfont/intern/blf.cc | 2 +- source/blender/blenfont/intern/blf_font.cc | 43 ++++----- source/blender/blenfont/intern/blf_glyph.cc | 92 ++++++++++--------- .../blender/blenfont/intern/blf_internal.hh | 4 - .../blenfont/intern/blf_internal_types.hh | 26 +++++- 5 files changed, 93 insertions(+), 74 deletions(-) diff --git a/source/blender/blenfont/intern/blf.cc b/source/blender/blenfont/intern/blf.cc index 1f7059cfb62..252fe0efee2 100644 --- a/source/blender/blenfont/intern/blf.cc +++ b/source/blender/blenfont/intern/blf.cc @@ -101,7 +101,7 @@ void BLF_cache_clear() for (int i = 0; i < BLF_MAX_FONT; i++) { FontBLF *font = global_font[i]; if (font) { - blf_glyph_cache_clear(font); + font->cache->clear(); } } } diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index dcd8b550df2..93a385a7874 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -482,9 +482,9 @@ static void blf_font_draw_ex(FontBLF *font, } void blf_font_draw(FontBLF *font, const char *str, const size_t str_len, ResultBLF *r_info) { - GlyphCacheBLF *gc = blf_glyph_cache_acquire(font); + GlyphCacheBLF *gc = font->cache->acquire(); blf_font_draw_ex(font, gc, str, str_len, r_info, 0); - blf_glyph_cache_release(font); + font->cache->release(); } int blf_font_draw_mono( @@ -497,7 +497,7 @@ int blf_font_draw_mono( size_t i = 0; - GlyphCacheBLF *gc = blf_glyph_cache_acquire(font); + GlyphCacheBLF *gc = font->cache->acquire(); blf_batch_draw_begin(font); @@ -518,7 +518,7 @@ int blf_font_draw_mono( blf_batch_draw_end(); - blf_glyph_cache_release(font); + font->cache->release(); return columns; } @@ -674,9 +674,9 @@ static void blf_font_draw_buffer_ex(FontBLF *font, void blf_font_draw_buffer(FontBLF *font, const char *str, const size_t str_len, ResultBLF *r_info) { - GlyphCacheBLF *gc = blf_glyph_cache_acquire(font); + GlyphCacheBLF *gc = font->cache->acquire(); blf_font_draw_buffer_ex(font, gc, str, str_len, r_info, 0); - blf_glyph_cache_release(font); + font->cache->release(); } /** \} */ @@ -731,7 +731,7 @@ size_t blf_font_width_to_strlen( ft_pix width_new; size_t i, i_prev; - GlyphCacheBLF *gc = blf_glyph_cache_acquire(font); + GlyphCacheBLF *gc = font->cache->acquire(); const int width_i = int(width); for (i_prev = i = 0, width_new = pen_x = 0, g_prev = nullptr; (i < str_len) && str[i]; @@ -747,7 +747,7 @@ size_t blf_font_width_to_strlen( *r_width = ft_pix_to_int(width_new); } - blf_glyph_cache_release(font); + font->cache->release(); return i_prev; } @@ -759,7 +759,7 @@ size_t blf_font_width_to_rstrlen( size_t i, i_prev, i_tmp; const char *s, *s_prev; - GlyphCacheBLF *gc = blf_glyph_cache_acquire(font); + GlyphCacheBLF *gc = font->cache->acquire(); i = BLI_strnlen(str, str_len); s = BLI_str_find_prev_char_utf8(&str[i], str); @@ -790,7 +790,7 @@ size_t blf_font_width_to_rstrlen( *r_width = ft_pix_to_int(width_new); } - blf_glyph_cache_release(font); + font->cache->release(); return i; } @@ -867,9 +867,9 @@ static void blf_font_boundbox_ex(FontBLF *font, void blf_font_boundbox( FontBLF *font, const char *str, const size_t str_len, rcti *r_box, ResultBLF *r_info) { - GlyphCacheBLF *gc = blf_glyph_cache_acquire(font); + GlyphCacheBLF *gc = font->cache->acquire(); blf_font_boundbox_ex(font, gc, str, str_len, r_box, r_info, 0); - blf_glyph_cache_release(font); + font->cache->release(); } void blf_font_width_and_height(FontBLF *font, @@ -945,9 +945,9 @@ float blf_font_height(FontBLF *font, const char *str, const size_t str_len, Resu float blf_font_fixed_width(FontBLF *font) { - GlyphCacheBLF *gc = blf_glyph_cache_acquire(font); + GlyphCacheBLF *gc = font->cache->acquire(); float width = (gc) ? float(gc->fixed_width) : font->size / 2.0f; - blf_glyph_cache_release(font); + font->cache->release(); return width; } @@ -966,7 +966,7 @@ void blf_font_boundbox_foreach_glyph(FontBLF *font, ft_pix pen_x = 0; size_t i = 0; - GlyphCacheBLF *gc = blf_glyph_cache_acquire(font); + GlyphCacheBLF *gc = font->cache->acquire(); while ((i < str_len) && str[i]) { const size_t i_curr = i; @@ -987,7 +987,7 @@ void blf_font_boundbox_foreach_glyph(FontBLF *font, pen_x += g->advance_x; } - blf_glyph_cache_release(font); + font->cache->release(); } struct CursorPositionForeachGlyph_Data { @@ -1104,7 +1104,7 @@ static void blf_font_wrap_apply(FontBLF *font, ft_pix line_height = blf_font_height_max_ft_pix(font); - GlyphCacheBLF *gc = blf_glyph_cache_acquire(font); + GlyphCacheBLF *gc = font->cache->acquire(); struct WordWrapVars { ft_pix wrap_width; @@ -1183,7 +1183,7 @@ static void blf_font_wrap_apply(FontBLF *font, r_info->width = ft_pix_to_int(pen_x_next); } - blf_glyph_cache_release(font); + font->cache->release(); } /** Utility for #blf_font_draw__wrap. */ @@ -1376,7 +1376,6 @@ static void blf_font_fill(FontBLF *font) font->char_width = 1.0f; font->char_spacing = 0.0f; - BLI_listbase_clear(&font->cache); font->kerning_cache = nullptr; #if BLF_BLUR_ENABLE font->blur = 0; @@ -1756,7 +1755,7 @@ static FontBLF *blf_font_new_impl(const char *filepath, font->ft_lib = ft_library ? (FT_Library)ft_library : ft_lib; - BLI_mutex_init(&font->glyph_cache_mutex); + font->cache = new GlyphCacheListBLF(font); /* If we have static details about this font file, we don't have to load the Face yet. */ bool face_needed = true; @@ -1826,7 +1825,7 @@ void blf_font_attach_from_mem(FontBLF *font, const uchar *mem, const size_t mem_ void blf_font_free(FontBLF *font) { - blf_glyph_cache_clear(font); + delete font->cache; if (font->kerning_cache) { MEM_freeN(font->kerning_cache); @@ -1854,8 +1853,6 @@ void blf_font_free(FontBLF *font) MEM_freeN(font->mem_name); } - BLI_mutex_end(&font->glyph_cache_mutex); - MEM_freeN(font); } diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index 3fdb2d99db3..0c138b953fb 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -71,24 +71,9 @@ static float from_16dot16(FT_Fixed value) /** \} */ /* -------------------------------------------------------------------- */ -/** \name Glyph Cache +/** \name Glyph Cache Entries * \{ */ -static GlyphCacheBLF *blf_glyph_cache_find(const FontBLF *font, const float size) -{ - GlyphCacheBLF *gc = (GlyphCacheBLF *)font->cache.first; - while (gc) { - if (gc->size == size && (gc->bold == ((font->flags & BLF_BOLD) != 0)) && - (gc->italic == ((font->flags & BLF_ITALIC) != 0)) && - (gc->char_weight == font->char_weight) && (gc->char_slant == font->char_slant) && - (gc->char_width == font->char_width) && (gc->char_spacing == font->char_spacing)) - { - return gc; - } - gc = gc->next; - } - return nullptr; -} static GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font) { @@ -124,28 +109,9 @@ static GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font) gc->fixed_width = 1; } - BLI_addhead(&font->cache, gc); return gc; } -GlyphCacheBLF *blf_glyph_cache_acquire(FontBLF *font) -{ - BLI_mutex_lock(&font->glyph_cache_mutex); - - GlyphCacheBLF *gc = blf_glyph_cache_find(font, font->size); - - if (!gc) { - gc = blf_glyph_cache_new(font); - } - - return gc; -} - -void blf_glyph_cache_release(FontBLF *font) -{ - BLI_mutex_unlock(&font->glyph_cache_mutex); -} - static void blf_glyph_cache_free(GlyphCacheBLF *gc) { for (uint i = 0; i < ARRAY_SIZE(gc->bucket); i++) { @@ -162,15 +128,59 @@ static void blf_glyph_cache_free(GlyphCacheBLF *gc) MEM_freeN(gc); } -void blf_glyph_cache_clear(FontBLF *font) -{ - BLI_mutex_lock(&font->glyph_cache_mutex); +/* -------------------------------------------------------------------- */ +/** \name Glyph Cache List + * \{ */ - while (GlyphCacheBLF *gc = static_cast(BLI_pophead(&font->cache))) { - blf_glyph_cache_free(gc); +GlyphCacheListBLF::GlyphCacheListBLF(FontBLF *font) : list{}, font(font) +{ + BLI_mutex_init(&glyph_cache_mutex); +} + +GlyphCacheListBLF ::~GlyphCacheListBLF() +{ + clear(); + BLI_mutex_end(&glyph_cache_mutex); +}; + +GlyphCacheBLF *GlyphCacheListBLF::acquire() +{ + BLI_mutex_lock(&glyph_cache_mutex); + + GlyphCacheBLF *gc = nullptr; + + for (auto &entry : list) { + if (entry->size == font->size && (entry->bold == ((font->flags & BLF_BOLD) != 0)) && + (entry->italic == ((font->flags & BLF_ITALIC) != 0)) && + (entry->char_weight == font->char_weight) && (entry->char_slant == font->char_slant) && + (entry->char_width == font->char_width) && (entry->char_spacing == font->char_spacing)) + { + gc = entry; + break; + } } - BLI_mutex_unlock(&font->glyph_cache_mutex); + if (!gc) { + gc = blf_glyph_cache_new(font); + list.push_back(gc); + } + + return gc; +} + +void GlyphCacheListBLF::release() +{ + BLI_mutex_unlock(&glyph_cache_mutex); +} + +void GlyphCacheListBLF::clear() +{ + BLI_mutex_lock(&glyph_cache_mutex); + for (auto &entry : list) { + blf_glyph_cache_free(entry); + } + list.clear(); + BLI_mutex_unlock(&glyph_cache_mutex); } /** diff --git a/source/blender/blenfont/intern/blf_internal.hh b/source/blender/blenfont/intern/blf_internal.hh index 5c0bafa2d89..c1a8312fefe 100644 --- a/source/blender/blenfont/intern/blf_internal.hh +++ b/source/blender/blenfont/intern/blf_internal.hh @@ -166,10 +166,6 @@ void blf_str_offset_to_glyph_bounds(struct FontBLF *font, void blf_font_free(struct FontBLF *font); -struct GlyphCacheBLF *blf_glyph_cache_acquire(struct FontBLF *font); -void blf_glyph_cache_release(struct FontBLF *font); -void blf_glyph_cache_clear(struct FontBLF *font); - /** * Create (or load from cache) a fully-rendered bitmap glyph. */ diff --git a/source/blender/blenfont/intern/blf_internal_types.hh b/source/blender/blenfont/intern/blf_internal_types.hh index b92e951894d..f22db15244a 100644 --- a/source/blender/blenfont/intern/blf_internal_types.hh +++ b/source/blender/blenfont/intern/blf_internal_types.hh @@ -8,6 +8,8 @@ #pragma once +#include + #include "GPU_texture.h" #include "GPU_vertex_buffer.h" @@ -106,6 +108,23 @@ typedef struct KerningCacheBLF { int ascii_table[KERNING_CACHE_TABLE_SIZE][KERNING_CACHE_TABLE_SIZE]; } KerningCacheBLF; +class GlyphCacheListBLF { + private: + std::vector list; + + FontBLF *font; + + /** Mutex lock for glyph cache. */ + ThreadMutex glyph_cache_mutex; + + public: + GlyphCacheListBLF(FontBLF *font); + ~GlyphCacheListBLF(); + GlyphCacheBLF *acquire(); + void release(); + void clear(); +}; + typedef struct GlyphCacheBLF { struct GlyphCacheBLF *next; struct GlyphCacheBLF *prev; @@ -355,9 +374,9 @@ typedef struct FontBLF { /** * List of glyph caches (#GlyphCacheBLF) for this font for size, DPI, bold, italic. - * Use blf_glyph_cache_acquire(font) and blf_glyph_cache_release(font) to access cache! + * Use font->cache->acquire() and font->cache->release() to access cache! */ - ListBase cache; + GlyphCacheListBLF *cache; /** Cache of unscaled kerning values. Will be NULL if font does not have kerning. */ KerningCacheBLF *kerning_cache; @@ -379,7 +398,4 @@ typedef struct FontBLF { /** Data for buffer usage (drawing into a texture buffer) */ FontBufInfoBLF buf_info; - - /** Mutex lock for glyph cache. */ - ThreadMutex glyph_cache_mutex; } FontBLF; -- 2.30.2 From adec426ed00c9182631a8d7029ed2d6227117326 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Mon, 12 Feb 2024 17:11:09 -0800 Subject: [PATCH 2/3] Formatting changes --- source/blender/blenfont/intern/blf_glyph.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index 0c138b953fb..d3f11f08406 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -74,7 +74,6 @@ static float from_16dot16(FT_Fixed value) /** \name Glyph Cache Entries * \{ */ - static GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font) { GlyphCacheBLF *gc = (GlyphCacheBLF *)MEM_callocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new"); -- 2.30.2 From 3ee1c4e62ed20519fa916fecf83c04943feb3a3d Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Tue, 13 Feb 2024 11:16:39 -0800 Subject: [PATCH 3/3] Changes requested by review. --- source/blender/blenfont/intern/blf_glyph.cc | 20 ++++++++----------- .../blenfont/intern/blf_internal_types.hh | 12 +++++------ 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index d3f11f08406..b6937d945ff 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -131,24 +131,20 @@ static void blf_glyph_cache_free(GlyphCacheBLF *gc) /** \name Glyph Cache List * \{ */ -GlyphCacheListBLF::GlyphCacheListBLF(FontBLF *font) : list{}, font(font) -{ - BLI_mutex_init(&glyph_cache_mutex); -} +GlyphCacheListBLF::GlyphCacheListBLF(FontBLF *font) : list{}, font(font) {} GlyphCacheListBLF ::~GlyphCacheListBLF() { clear(); - BLI_mutex_end(&glyph_cache_mutex); }; GlyphCacheBLF *GlyphCacheListBLF::acquire() { - BLI_mutex_lock(&glyph_cache_mutex); + glyph_cache_mutex.lock(); GlyphCacheBLF *gc = nullptr; - for (auto &entry : list) { + for (GlyphCacheBLF *entry : list) { if (entry->size == font->size && (entry->bold == ((font->flags & BLF_BOLD) != 0)) && (entry->italic == ((font->flags & BLF_ITALIC) != 0)) && (entry->char_weight == font->char_weight) && (entry->char_slant == font->char_slant) && @@ -161,7 +157,7 @@ GlyphCacheBLF *GlyphCacheListBLF::acquire() if (!gc) { gc = blf_glyph_cache_new(font); - list.push_back(gc); + list.append(gc); } return gc; @@ -169,17 +165,17 @@ GlyphCacheBLF *GlyphCacheListBLF::acquire() void GlyphCacheListBLF::release() { - BLI_mutex_unlock(&glyph_cache_mutex); + this->glyph_cache_mutex.unlock(); } void GlyphCacheListBLF::clear() { - BLI_mutex_lock(&glyph_cache_mutex); - for (auto &entry : list) { + glyph_cache_mutex.lock(); + for (GlyphCacheBLF *entry : list) { blf_glyph_cache_free(entry); } list.clear(); - BLI_mutex_unlock(&glyph_cache_mutex); + glyph_cache_mutex.unlock(); } /** diff --git a/source/blender/blenfont/intern/blf_internal_types.hh b/source/blender/blenfont/intern/blf_internal_types.hh index f22db15244a..a73f8fdb42d 100644 --- a/source/blender/blenfont/intern/blf_internal_types.hh +++ b/source/blender/blenfont/intern/blf_internal_types.hh @@ -8,7 +8,9 @@ #pragma once -#include +#include + +#include "BLI_vector.hh" #include "GPU_texture.h" #include "GPU_vertex_buffer.h" @@ -109,13 +111,9 @@ typedef struct KerningCacheBLF { } KerningCacheBLF; class GlyphCacheListBLF { - private: - std::vector list; - + blender::Vector list; FontBLF *font; - - /** Mutex lock for glyph cache. */ - ThreadMutex glyph_cache_mutex; + std::mutex glyph_cache_mutex; public: GlyphCacheListBLF(FontBLF *font); -- 2.30.2