[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [57845] trunk/blender/source/blender: Fix #35884: crash opening . blend with generated color grid image and preview render.

Brecht Van Lommel brechtvanlommel at pandora.be
Fri Jun 28 15:05:16 CEST 2013


Revision: 57845
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=57845
Author:   blendix
Date:     2013-06-28 13:05:15 +0000 (Fri, 28 Jun 2013)
Log Message:
-----------
Fix #35884: crash opening .blend with generated color grid image and preview render.

Printing text on the color grid image would initialize font glyphs from a thread at
the same time as the UI, causing conflicts. The freetype glyph renderer needs to be
mutex locked because it uses a shared buffer internally even when rendering for
different fonts. Also needed to change the image generate function to use the render
monospace font to avoid conflicts in blenfont.

What's still weak in the blenfont API is that there is no distinction between a font
and a thread using that font to render with some particular size, style, etc.

Modified Paths:
--------------
    trunk/blender/source/blender/blenfont/intern/blf.c
    trunk/blender/source/blender/blenfont/intern/blf_dir.c
    trunk/blender/source/blender/blenfont/intern/blf_font.c
    trunk/blender/source/blender/blenfont/intern/blf_glyph.c
    trunk/blender/source/blender/blenfont/intern/blf_internal_types.h
    trunk/blender/source/blender/blenkernel/intern/image_gen.c

Modified: trunk/blender/source/blender/blenfont/intern/blf.c
===================================================================
--- trunk/blender/source/blender/blenfont/intern/blf.c	2013-06-28 06:54:49 UTC (rev 57844)
+++ trunk/blender/source/blender/blenfont/intern/blf.c	2013-06-28 13:05:15 UTC (rev 57845)
@@ -44,6 +44,7 @@
 #include "DNA_vec_types.h"
 
 #include "BLI_math.h"
+#include "BLI_threads.h"
 
 #include "BIF_gl.h"
 #include "BLF_api.h"

Modified: trunk/blender/source/blender/blenfont/intern/blf_dir.c
===================================================================
--- trunk/blender/source/blender/blenfont/intern/blf_dir.c	2013-06-28 06:54:49 UTC (rev 57844)
+++ trunk/blender/source/blender/blenfont/intern/blf_dir.c	2013-06-28 13:05:15 UTC (rev 57845)
@@ -46,6 +46,7 @@
 #include "BLI_listbase.h"
 #include "BLI_path_util.h"
 #include "BLI_string.h"
+#include "BLI_threads.h"
 
 #include "BIF_gl.h"
 

Modified: trunk/blender/source/blender/blenfont/intern/blf_font.c
===================================================================
--- trunk/blender/source/blender/blenfont/intern/blf_font.c	2013-06-28 06:54:49 UTC (rev 57844)
+++ trunk/blender/source/blender/blenfont/intern/blf_font.c	2013-06-28 13:05:15 UTC (rev 57845)
@@ -48,6 +48,7 @@
 #include "BLI_rect.h"
 #include "BLI_string.h"
 #include "BLI_string_utf8.h"
+#include "BLI_threads.h"
 #include "BLI_linklist.h"  /* linknode */
 
 #include "BIF_gl.h"
@@ -64,15 +65,18 @@
 
 /* freetype2 handle ONLY for this file!. */
 static FT_Library ft_lib;
+static SpinLock ft_lib_mutex;
 
 int blf_font_init(void)
 {
+	BLI_spin_init(&ft_lib_mutex);
 	return FT_Init_FreeType(&ft_lib);
 }
 
 void blf_font_exit(void)
 {
 	FT_Done_FreeType(ft_lib);
+	BLI_spin_end(&ft_lib_mutex);
 }
 
 void blf_font_size(FontBLF *font, unsigned int size, unsigned int dpi)
@@ -572,6 +576,7 @@
 	font->buf_info.col[3] = 0;
 
 	font->ft_lib = ft_lib;
+	font->ft_lib_mutex = &ft_lib_mutex;
 }
 
 FontBLF *blf_font_new(const char *name, const char *filename)

Modified: trunk/blender/source/blender/blenfont/intern/blf_glyph.c
===================================================================
--- trunk/blender/source/blender/blenfont/intern/blf_glyph.c	2013-06-28 06:54:49 UTC (rev 57844)
+++ trunk/blender/source/blender/blenfont/intern/blf_glyph.c	2013-06-28 13:05:15 UTC (rev 57845)
@@ -48,6 +48,7 @@
 
 #include "BLI_listbase.h"
 #include "BLI_rect.h"
+#include "BLI_threads.h"
 
 #include "BIF_gl.h"
 #include "BLF_api.h"
@@ -224,6 +225,19 @@
 	if (g)
 		return g;
 
+	/* glyphs are dynamically created as needed by font rendering. this means that
+	 * to make font rendering thread safe we have to do locking here. note that this
+	 * must be a lock for the whole library and not just per font, because the font
+	 * renderer uses a shared buffer internally */
+	BLI_spin_lock(font->ft_lib_mutex);
+
+	/* search again after locking */
+	g = blf_glyph_search(font->glyph_cache, c);
+	if (g) {
+		BLI_spin_unlock(font->ft_lib_mutex);
+		return g;
+	}
+
 	if (font->flags & BLF_HINTING)
 		flags &= ~FT_LOAD_NO_HINTING;
 	
@@ -231,8 +245,11 @@
 		err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO);
 	else
 		err = FT_Load_Glyph(font->face, (FT_UInt)index, flags);  
-	if (err)
+
+	if (err) {
+		BLI_spin_unlock(font->ft_lib_mutex);
 		return NULL;
+	}
 
 	/* get the glyph. */
 	slot = font->face->glyph;
@@ -251,8 +268,10 @@
 		err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
 	}
 
-	if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
+	if (err || slot->format != FT_GLYPH_FORMAT_BITMAP) {
+		BLI_spin_unlock(font->ft_lib_mutex);
 		return NULL;
+	}
 
 	g = (GlyphBLF *)MEM_callocN(sizeof(GlyphBLF), "blf_glyph_add");
 	g->c = c;
@@ -289,6 +308,9 @@
 
 	key = blf_hash(g->c);
 	BLI_addhead(&(font->glyph_cache->bucket[key]), g);
+
+	BLI_spin_unlock(font->ft_lib_mutex);
+
 	return g;
 }
 

Modified: trunk/blender/source/blender/blenfont/intern/blf_internal_types.h
===================================================================
--- trunk/blender/source/blender/blenfont/intern/blf_internal_types.h	2013-06-28 06:54:49 UTC (rev 57844)
+++ trunk/blender/source/blender/blenfont/intern/blf_internal_types.h	2013-06-28 13:05:15 UTC (rev 57845)
@@ -217,6 +217,9 @@
 	/* freetype2 lib handle. */
 	FT_Library ft_lib;
 
+	/* Mutex lock for library */
+	SpinLock *ft_lib_mutex;
+
 	/* freetype2 face. */
 	FT_Face face;
 

Modified: trunk/blender/source/blender/blenkernel/intern/image_gen.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/image_gen.c	2013-06-28 06:54:49 UTC (rev 57844)
+++ trunk/blender/source/blender/blenkernel/intern/image_gen.c	2013-06-28 13:05:15 UTC (rev 57845)
@@ -287,7 +287,7 @@
 	int x, y;
 	int pen_x, pen_y;
 	char text[3] = {'A', '1', '\0'};
-	const int mono = blf_mono_font;
+	const int mono = blf_mono_font_render;
 
 	BLF_size(mono, 54, 72); /* hard coded size! */
 




More information about the Bf-blender-cvs mailing list