BLF: Mutex Lock Glyph Cache Per Font, Not Global

Only lock access to our glyph caches per-font, rather than globally.
Also upgrade from spinlocks to mutexes.

See D15644 for more details.

Differential Revision: https://developer.blender.org/D15644

Reviewed by Brecht Van Lommel
This commit is contained in:
Harley Acheson 2022-08-11 12:52:07 -07:00
parent 2fc7e15164
commit d2b1e4712d
3 changed files with 18 additions and 16 deletions

View File

@ -56,8 +56,8 @@ BatchBLF g_batch;
/* freetype2 handle ONLY for this file! */
static FT_Library ft_lib = NULL;
static SpinLock ft_lib_mutex;
static SpinLock blf_glyph_cache_mutex;
/* Lock for FreeType library, used around face creation and deletion. */
static ThreadMutex ft_lib_mutex;
/* May be set to #UI_widgetbase_draw_cache_flush. */
static void (*blf_draw_cache_flush)(void) = NULL;
@ -1162,19 +1162,17 @@ char *blf_display_name(FontBLF *font)
int blf_font_init(void)
{
memset(&g_batch, 0, sizeof(g_batch));
BLI_spin_init(&ft_lib_mutex);
BLI_spin_init(&blf_glyph_cache_mutex);
BLI_mutex_init(&ft_lib_mutex);
int err = FT_Init_FreeType(&ft_lib);
return err;
}
void blf_font_exit(void)
{
BLI_spin_end(&ft_lib_mutex);
BLI_mutex_end(&ft_lib_mutex);
if (ft_lib) {
FT_Done_FreeType(ft_lib);
}
BLI_spin_end(&blf_glyph_cache_mutex);
blf_batch_draw_exit();
}
@ -1233,8 +1231,6 @@ static void blf_font_fill(FontBLF *font)
font->buf_info.col_init[3] = 0;
font->ft_lib = ft_lib;
font->ft_lib_mutex = &ft_lib_mutex;
font->glyph_cache_mutex = &blf_glyph_cache_mutex;
}
/**
@ -1252,12 +1248,14 @@ bool blf_ensure_face(FontBLF *font)
FT_Error err;
BLI_mutex_lock(&ft_lib_mutex);
if (font->filepath) {
err = FT_New_Face(ft_lib, font->filepath, 0, &font->face);
}
if (font->mem) {
err = FT_New_Memory_Face(ft_lib, font->mem, (FT_Long)font->mem_size, 0, &font->face);
}
BLI_mutex_unlock(&ft_lib_mutex);
if (err) {
if (ELEM(err, FT_Err_Unknown_File_Format, FT_Err_Unimplemented_Feature)) {
@ -1384,6 +1382,8 @@ static FontBLF *blf_font_new_ex(const char *name,
}
blf_font_fill(font);
BLI_mutex_init(&font->glyph_cache_mutex);
/* If we have static details about this font we don't need to load the Face. */
const eFaceDetails *static_details = NULL;
char filename[256];
@ -1450,7 +1450,9 @@ void blf_font_free(FontBLF *font)
}
if (font->face) {
BLI_mutex_lock(&ft_lib_mutex);
FT_Done_Face(font->face);
BLI_mutex_unlock(&ft_lib_mutex);
font->face = NULL;
}
if (font->filepath) {
@ -1459,6 +1461,9 @@ void blf_font_free(FontBLF *font)
if (font->name) {
MEM_freeN(font->name);
}
BLI_mutex_end(&font->glyph_cache_mutex);
MEM_freeN(font);
}

View File

@ -115,7 +115,7 @@ static GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
GlyphCacheBLF *blf_glyph_cache_acquire(FontBLF *font)
{
BLI_spin_lock(font->glyph_cache_mutex);
BLI_mutex_lock(&font->glyph_cache_mutex);
GlyphCacheBLF *gc = blf_glyph_cache_find(font, font->size, font->dpi);
@ -128,7 +128,7 @@ GlyphCacheBLF *blf_glyph_cache_acquire(FontBLF *font)
void blf_glyph_cache_release(FontBLF *font)
{
BLI_spin_unlock(font->glyph_cache_mutex);
BLI_mutex_unlock(&font->glyph_cache_mutex);
}
static void blf_glyph_cache_free(GlyphCacheBLF *gc)
@ -152,13 +152,13 @@ void blf_glyph_cache_clear(FontBLF *font)
{
GlyphCacheBLF *gc;
BLI_spin_lock(font->glyph_cache_mutex);
BLI_mutex_lock(&font->glyph_cache_mutex);
while ((gc = BLI_pophead(&font->cache))) {
blf_glyph_cache_free(gc);
}
BLI_spin_unlock(font->glyph_cache_mutex);
BLI_mutex_unlock(&font->glyph_cache_mutex);
}
/**

View File

@ -323,9 +323,6 @@ typedef struct FontBLF {
/* freetype2 lib handle. */
FT_Library ft_lib;
/* Mutex lock for library */
SpinLock *ft_lib_mutex;
/* freetype2 face. */
FT_Face face;
@ -339,7 +336,7 @@ typedef struct FontBLF {
FontBufInfoBLF buf_info;
/* Mutex lock for glyph cache. */
SpinLock *glyph_cache_mutex;
ThreadMutex glyph_cache_mutex;
} FontBLF;
typedef struct DirBLF {