Refactored the mutex usage in osgText and freetype plugin to prevent multi-thread crash
This commit is contained in:
@@ -97,7 +97,7 @@ void FreeTypeFont::setFontResolution(const osgText::FontResolution& fontSize)
|
||||
|
||||
osgText::Font::Glyph* FreeTypeFont::getGlyph(const osgText::FontResolution& fontRes, unsigned int charcode)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(FreeTypeLibrary::instance()->getMutex());
|
||||
|
||||
setFontResolution(fontRes);
|
||||
|
||||
@@ -201,7 +201,7 @@ osgText::Font::Glyph* FreeTypeFont::getGlyph(const osgText::FontResolution& font
|
||||
|
||||
osg::Vec2 FreeTypeFont::getKerning(const osgText::FontResolution& fontRes, unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType kerningType)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(FreeTypeLibrary::instance()->getMutex());
|
||||
|
||||
if (!FT_HAS_KERNING(_face) || (kerningType == osgText::KERNING_NONE)) return osg::Vec2(0.0f,0.0f);
|
||||
|
||||
@@ -233,5 +233,6 @@ osg::Vec2 FreeTypeFont::getKerning(const osgText::FontResolution& fontRes, unsig
|
||||
|
||||
bool FreeTypeFont::hasVertical() const
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(FreeTypeLibrary::instance()->getMutex());
|
||||
return FT_HAS_VERTICAL(_face)!=0;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,6 @@ protected:
|
||||
|
||||
void setFontResolution(const osgText::FontResolution& fontSize);
|
||||
|
||||
OpenThreads::Mutex _mutex;
|
||||
osgText::FontResolution _currentRes;
|
||||
|
||||
std::string _filename;
|
||||
|
||||
@@ -69,6 +69,8 @@ FreeTypeLibrary* FreeTypeLibrary::instance()
|
||||
|
||||
bool FreeTypeLibrary::getFace(const std::string& fontfile,unsigned int index, FT_Face & face)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getMutex());
|
||||
|
||||
FT_Error error = FT_New_Face( _ftlibrary, fontfile.c_str(), index, &face );
|
||||
if (error == FT_Err_Unknown_File_Format)
|
||||
{
|
||||
@@ -110,6 +112,8 @@ bool FreeTypeLibrary::getFace(const std::string& fontfile,unsigned int index, FT
|
||||
|
||||
FT_Byte* FreeTypeLibrary::getFace(std::istream& fontstream, unsigned int index, FT_Face & face)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getMutex());
|
||||
|
||||
FT_Open_Args args;
|
||||
|
||||
std::streampos start = fontstream.tellg();
|
||||
@@ -159,6 +163,7 @@ osgText::Font* FreeTypeLibrary::getFont(const std::string& fontfile, unsigned in
|
||||
FT_Face face;
|
||||
if (getFace(fontfile, index, face) == false) return (0);
|
||||
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getMutex());
|
||||
|
||||
FreeTypeFont* fontImp = new FreeTypeFont(fontfile,face,flags);
|
||||
osgText::Font* font = new osgText::Font(fontImp);
|
||||
@@ -174,6 +179,8 @@ osgText::Font* FreeTypeLibrary::getFont(std::istream& fontstream, unsigned int i
|
||||
if (face == 0) return (0);
|
||||
|
||||
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getMutex());
|
||||
|
||||
FreeTypeFont* fontImp = new FreeTypeFont(buffer,face,flags);
|
||||
osgText::Font* font = new osgText::Font(fontImp);
|
||||
|
||||
@@ -187,6 +194,7 @@ osgText::Font3D* FreeTypeLibrary::getFont3D(const std::string& fontfile, unsigne
|
||||
FT_Face face;
|
||||
if (getFace(fontfile, index, face) == false) return (0);
|
||||
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getMutex());
|
||||
|
||||
FreeTypeFont3D* font3DImp = new FreeTypeFont3D(fontfile,face,flags);
|
||||
osgText::Font3D* font3D = new osgText::Font3D(font3DImp);
|
||||
@@ -197,11 +205,13 @@ osgText::Font3D* FreeTypeLibrary::getFont3D(const std::string& fontfile, unsigne
|
||||
}
|
||||
osgText::Font3D* FreeTypeLibrary::getFont3D(std::istream& fontstream, unsigned int index, unsigned int flags)
|
||||
{
|
||||
|
||||
FT_Face face = 0;
|
||||
FT_Byte * buffer = getFace(fontstream, index, face);
|
||||
if (face == 0) return (0);
|
||||
|
||||
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getMutex());
|
||||
|
||||
FreeTypeFont3D* font3DImp = new FreeTypeFont3D(buffer,face,flags);
|
||||
osgText::Font3D* font3D = new osgText::Font3D(font3DImp);
|
||||
|
||||
|
||||
@@ -35,6 +35,8 @@ public:
|
||||
/** get the singleton instance.*/
|
||||
static FreeTypeLibrary* instance();
|
||||
|
||||
OpenThreads::Mutex& getMutex() { return _mutex; }
|
||||
|
||||
osgText::Font* getFont(const std::string& fontfile,unsigned int index=0, unsigned int flags=0);
|
||||
osgText::Font* getFont(std::istream& fontstream, unsigned int index=0, unsigned int flags=0);
|
||||
|
||||
@@ -61,9 +63,10 @@ protected:
|
||||
typedef std::set< FreeTypeFont* > FontImplementationSet;
|
||||
typedef std::set< FreeTypeFont3D* > Font3DImplementationSet;
|
||||
|
||||
FT_Library _ftlibrary;
|
||||
FontImplementationSet _fontImplementationSet;
|
||||
Font3DImplementationSet _font3DImplementationSet;
|
||||
mutable OpenThreads::Mutex _mutex;
|
||||
FT_Library _ftlibrary;
|
||||
FontImplementationSet _fontImplementationSet;
|
||||
Font3DImplementationSet _font3DImplementationSet;
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user