[Bf-blender-cvs] [a960dc45193] master: BLF: make library glyph cache handling thread-safe
Richard Antalik
noreply at git.blender.org
Sat Sep 14 03:00:49 CEST 2019
Commit: a960dc451930796ca310cc88f00fb701374b2f7f
Author: Richard Antalik
Date: Fri Sep 13 17:22:42 2019 -0700
Branches: master
https://developer.blender.org/rBa960dc451930796ca310cc88f00fb701374b2f7f
BLF: make library glyph cache handling thread-safe
Functions that utilize glyph cache should lock and unlock cache by
calling `blf_glyph_cache_acquire()` and `blf_glyph_cache_release()`.
Function `blf_glyph_cache_acquire()` will create glyph cache, if it doesn't exist.
Locking mutex is global and shared by all fonts.
Reviewed By: brecht
Differential Revision: https://developer.blender.org/D5701
===================================================================
M source/blender/blenfont/intern/blf.c
M source/blender/blenfont/intern/blf_font.c
M source/blender/blenfont/intern/blf_glyph.c
M source/blender/blenfont/intern/blf_internal.h
M source/blender/blenfont/intern/blf_internal_types.h
===================================================================
diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c
index 793e9805899..8e1ff77b1c7 100644
--- a/source/blender/blenfont/intern/blf.c
+++ b/source/blender/blenfont/intern/blf.c
@@ -608,7 +608,7 @@ void BLF_draw_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_in
BLF_RESULT_CHECK_INIT(r_info);
- if (font && font->glyph_cache) {
+ if (font) {
blf_draw_gl__start(font);
if (font->flags & BLF_WORD_WRAP) {
blf_font_draw__wrap(font, str, len, r_info);
@@ -634,7 +634,7 @@ void BLF_draw_ascii_ex(int fontid, const char *str, size_t len, struct ResultBLF
BLF_RESULT_CHECK_INIT(r_info);
- if (font && font->glyph_cache) {
+ if (font) {
blf_draw_gl__start(font);
if (font->flags & BLF_WORD_WRAP) {
/* use non-ascii draw function for word-wrap */
@@ -646,6 +646,7 @@ void BLF_draw_ascii_ex(int fontid, const char *str, size_t len, struct ResultBLF
blf_draw_gl__end(font);
}
}
+
void BLF_draw_ascii(int fontid, const char *str, size_t len)
{
if (len == 0 || str[0] == '\0') {
@@ -664,7 +665,7 @@ int BLF_draw_mono(int fontid, const char *str, size_t len, int cwidth)
FontBLF *font = blf_get(fontid);
int columns = 0;
- if (font && font->glyph_cache) {
+ if (font) {
blf_draw_gl__start(font);
columns = blf_font_draw_mono(font, str, len, cwidth);
blf_draw_gl__end(font);
@@ -729,6 +730,7 @@ void BLF_boundbox_ex(
}
}
}
+
void BLF_boundbox(int fontid, const char *str, size_t len, rctf *r_box)
{
BLF_boundbox_ex(fontid, str, len, r_box, NULL);
@@ -738,7 +740,7 @@ void BLF_width_and_height(int fontid, const char *str, size_t len, float *r_widt
{
FontBLF *font = blf_get(fontid);
- if (font && font->glyph_cache) {
+ if (font) {
blf_font_width_and_height(font, str, len, r_width, r_height, NULL);
}
else {
@@ -752,12 +754,13 @@ float BLF_width_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_
BLF_RESULT_CHECK_INIT(r_info);
- if (font && font->glyph_cache) {
+ if (font) {
return blf_font_width(font, str, len, r_info);
}
return 0.0f;
}
+
float BLF_width(int fontid, const char *str, size_t len)
{
return BLF_width_ex(fontid, str, len, NULL);
@@ -767,7 +770,7 @@ float BLF_fixed_width(int fontid)
{
FontBLF *font = blf_get(fontid);
- if (font && font->glyph_cache) {
+ if (font) {
return blf_font_fixed_width(font);
}
@@ -780,12 +783,13 @@ float BLF_height_ex(int fontid, const char *str, size_t len, struct ResultBLF *r
BLF_RESULT_CHECK_INIT(r_info);
- if (font && font->glyph_cache) {
+ if (font) {
return blf_font_height(font, str, len, r_info);
}
return 0.0f;
}
+
float BLF_height(int fontid, const char *str, size_t len)
{
return BLF_height_ex(fontid, str, len, NULL);
@@ -795,8 +799,8 @@ int BLF_height_max(int fontid)
{
FontBLF *font = blf_get(fontid);
- if (font && font->glyph_cache) {
- return font->glyph_cache->glyph_height_max;
+ if (font) {
+ return blf_font_height_max(font);
}
return 0;
@@ -806,8 +810,8 @@ float BLF_width_max(int fontid)
{
FontBLF *font = blf_get(fontid);
- if (font && font->glyph_cache) {
- return font->glyph_cache->glyph_width_max;
+ if (font) {
+ return blf_font_width_max(font);
}
return 0.0f;
@@ -817,8 +821,8 @@ float BLF_descender(int fontid)
{
FontBLF *font = blf_get(fontid);
- if (font && font->glyph_cache) {
- return font->glyph_cache->descender;
+ if (font) {
+ return blf_font_descender(font);
}
return 0.0f;
@@ -828,8 +832,8 @@ float BLF_ascender(int fontid)
{
FontBLF *font = blf_get(fontid);
- if (font && font->glyph_cache) {
- return font->glyph_cache->ascender;
+ if (font) {
+ return blf_font_ascender(font);
}
return 0.0f;
@@ -939,7 +943,7 @@ void BLF_draw_buffer_ex(int fontid, const char *str, size_t len, struct ResultBL
{
FontBLF *font = blf_get(fontid);
- if (font && font->glyph_cache && (font->buf_info.fbuf || font->buf_info.cbuf)) {
+ if (font && (font->buf_info.fbuf || font->buf_info.cbuf)) {
blf_draw_buffer__start(font);
if (font->flags & BLF_WORD_WRAP) {
blf_font_draw_buffer__wrap(font, str, len, r_info);
diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c
index d75da6a126f..e0dfa6a2223 100644
--- a/source/blender/blenfont/intern/blf_font.c
+++ b/source/blender/blenfont/intern/blf_font.c
@@ -68,6 +68,7 @@ BatchBLF g_batch;
/* freetype2 handle ONLY for this file!. */
static FT_Library ft_lib;
static SpinLock ft_lib_mutex;
+static SpinLock blf_glyph_cache_mutex;
/* -------------------------------------------------------------------- */
/** \name Glyph Batching
@@ -216,6 +217,7 @@ 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);
return FT_Init_FreeType(&ft_lib);
}
@@ -223,6 +225,7 @@ void blf_font_exit(void)
{
FT_Done_FreeType(ft_lib);
BLI_spin_end(&ft_lib_mutex);
+ BLI_spin_end(&blf_glyph_cache_mutex);
blf_batch_draw_exit();
}
@@ -231,11 +234,14 @@ void blf_font_size(FontBLF *font, unsigned int size, unsigned int dpi)
GlyphCacheBLF *gc;
FT_Error err;
+ blf_glyph_cache_acquire(font);
+
gc = blf_glyph_cache_find(font, size, dpi);
if (gc) {
font->glyph_cache = gc;
/* Optimization: do not call FT_Set_Char_Size if size did not change. */
if (font->size == size && font->dpi == dpi) {
+ blf_glyph_cache_release(font);
return;
}
}
@@ -244,6 +250,8 @@ void blf_font_size(FontBLF *font, unsigned int size, unsigned int dpi)
if (err) {
/* FIXME: here we can go through the fixed size and choice a close one */
printf("The current font don't support the size, %u and dpi, %u\n", size, dpi);
+
+ blf_glyph_cache_release(font);
return;
}
@@ -259,28 +267,35 @@ void blf_font_size(FontBLF *font, unsigned int size, unsigned int dpi)
font->glyph_cache = NULL;
}
}
+ blf_glyph_cache_release(font);
}
-static void blf_font_ensure_ascii_table(FontBLF *font)
+static GlyphBLF **blf_font_ensure_ascii_table(FontBLF *font, GlyphCacheBLF *gc)
{
- GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
+ GlyphBLF **glyph_ascii_table;
+
+ glyph_ascii_table = gc->glyph_ascii_table;
/* build ascii on demand */
if (glyph_ascii_table['0'] == NULL) {
GlyphBLF *g;
unsigned int i;
for (i = 0; i < 256; i++) {
- g = blf_glyph_search(font->glyph_cache, i);
+ g = blf_glyph_search(gc, i);
if (!g) {
FT_UInt glyph_index = FT_Get_Char_Index(font->face, i);
- g = blf_glyph_add(font, glyph_index, i);
+ g = blf_glyph_add(font, gc, glyph_index, i);
}
glyph_ascii_table[i] = g;
}
}
+
+ return glyph_ascii_table;
}
-static void blf_font_ensure_ascii_kerning(FontBLF *font, const FT_UInt kern_mode)
+static void blf_font_ensure_ascii_kerning(FontBLF *font,
+ GlyphCacheBLF *gc,
+ const FT_UInt kern_mode)
{
KerningCacheBLF *kc = font->kerning_cache;
@@ -289,7 +304,7 @@ static void blf_font_ensure_ascii_kerning(FontBLF *font, const FT_UInt kern_mode
if (!kc || kc->mode != kern_mode) {
font->kerning_cache = kc = blf_kerning_cache_find(font);
if (!kc) {
- font->kerning_cache = kc = blf_kerning_cache_new(font);
+ font->kerning_cache = kc = blf_kerning_cache_new(font, gc);
}
}
}
@@ -300,16 +315,16 @@ static void blf_font_ensure_ascii_kerning(FontBLF *font, const FT_UInt kern_mode
*/
/* Note,
- * blf_font_ensure_ascii_table(font); must be called before this macro */
+ * blf_font_ensure_ascii_table(font, gc); must be called before this macro */
-#define BLF_UTF8_NEXT_FAST(_font, _g, _str, _i, _c, _glyph_ascii_table) \
+#define BLF_UTF8_NEXT_FAST(_font, _gc, _g, _str, _i, _c, _glyph_ascii_table) \
if (((_c) = (_str)[_i]) < 0x80) { \
_g = (_glyph_ascii_table)[_c]; \
_i++; \
} \
else if ((_c = BLI_str_utf8_as_unicode_step(_str, &(_i))) != BLI_UTF8_ERR) { \
- if ((_g = blf_glyph_search((_font)->glyph_cache, _c)) == NULL) { \
- _g = blf_glyph_add(_font, FT_Get_Char_Index((_font)->face, _c), _c); \
+ if ((_g = blf_glyph_search(_gc, _c)) == NULL) { \
+ _g = blf_glyph_add(_font, _gc, FT_Get_Char_Index((_font)->face, _c), _c); \
} \
} \
else { \
@@ -325,7 +340,7 @@ static void blf_font_ensure_ascii_kerning(FontBLF *font, const FT_UInt kern_mode
(FT_UInt)FT_KERNING_UNFITTED)
/* Note,
- * blf_font_ensure_ascii_kerning(font, kern_mode); must be called before this macro */
+ * blf_font_ensure_ascii_kerning(font, gc, kern_mode); must be called before this macro */
#define BLF_KERNING_STEP_FAST(_font, _kern_mode, _g_prev, _g, _c_prev, _c, _pen_x) \
{ \
@@ -353,29 +368,33 @@ static void blf_font_ensure_ascii_kerning(FontBLF *font, const FT_UInt kern_mode
} \
(void)0
-static void blf_font_draw_ex(
- FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, int pen_y)
+static void blf_font_draw_ex(FontBLF *font,
+ GlyphCacheBLF *gc,
+ const char *str,
+ size_t len,
+ struct ResultBLF *r_info,
+ int pen_y)
{
unsigned int c, c_prev = BLI_UTF8_ERR;
GlyphBLF *g, *g_prev = NULL;
int pen_x = 0;
size_t i = 0;
- GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
if (len == 0) {
/* early output, don't do any IMM OpenGL. */
return;
}
+ GlyphBLF **glyph_ascii_table = blf_font_ensure_ascii_table(font, gc);
+
BLF_KERNING_VARS(font, has_kerning, kern_mode);
- blf_font_ensure_ascii_table(font);
- blf_font_ensure_ascii_kerning(font, kern_mode);
+ blf_font_ensure_ascii_kerning(font, gc, kern_mode);
blf_batch_draw_begin(font);
while ((i < len) && str[i]) {
- BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
+ BLF_UTF8_NEXT_FAST(font, gc, g, str, i, c,
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list