Refactored the mutex usage in osgText and freetype plugin to prevent multi-thread crash

This commit is contained in:
Robert Osfield
2008-02-25 12:54:54 +00:00
parent 3333ca2b46
commit e869200b3d
10 changed files with 169 additions and 639 deletions

View File

@@ -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;
}

View File

@@ -41,7 +41,6 @@ protected:
void setFontResolution(const osgText::FontResolution& fontSize);
OpenThreads::Mutex _mutex;
osgText::FontResolution _currentRes;
std::string _filename;

View File

@@ -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);

View File

@@ -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;
};

View File

@@ -32,12 +32,6 @@ static osg::ApplicationUsageProxy Font_e0(osg::ApplicationUsage::ENVIRONMENTAL_V
static OpenThreads::ReentrantMutex s_FontFileMutex;
Font::FontMutex* osgText::Font::getSerializeFontCallsMutex()
{
static OpenThreads::Mutex s_serializeFontCallsMutex;
return &s_serializeFontCallsMutex;
}
std::string osgText::findFontFile(const std::string& str)
{
// try looking in OSGFILEPATH etc first for fonts.
@@ -330,14 +324,17 @@ osg::Texture::FilterMode Font::getMagFilterHint() const
Font::Glyph* Font::getGlyph(const FontResolution& fontRes, unsigned int charcode)
{
FontSizeGlyphMap::iterator itr = _sizeGlyphMap.find(fontRes);
if (itr!=_sizeGlyphMap.end())
{
GlyphMap& glyphmap = itr->second;
GlyphMap::iterator gitr = glyphmap.find(charcode);
if (gitr!=glyphmap.end()) return gitr->second.get();
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_glyphMapMutex);
FontSizeGlyphMap::iterator itr = _sizeGlyphMap.find(fontRes);
if (itr!=_sizeGlyphMap.end())
{
GlyphMap& glyphmap = itr->second;
GlyphMap::iterator gitr = glyphmap.find(charcode);
if (gitr!=glyphmap.end()) return gitr->second.get();
}
}
if (_implementation.valid()) return _implementation->getGlyph(fontRes, charcode);
else return 0;
}
@@ -400,6 +397,8 @@ bool Font::hasVertical() const
void Font::addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph* glyph)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_glyphMapMutex);
_sizeGlyphMap[fontRes][charcode]=glyph;
int posX=0,posY=0;

View File

@@ -37,12 +37,6 @@ static OpenThreads::ReentrantMutex s_Font3DFileMutex;
namespace osgText
{
Font::FontMutex* Font3D::getSerializeFontCallsMutex()
{
static OpenThreads::Mutex s_serializeFontCallsMutex;
return &s_serializeFontCallsMutex;
}
std::string findFont3DFile(const std::string& str)
{
// try looking in OSGFILEPATH etc first for fonts.

View File

@@ -224,8 +224,6 @@ void Text3D::computeGlyphRepresentation()
return;
}
OpenThreads::ScopedLock<Font3D::Font3DMutex> lock(*(_font->getSerializeFontCallsMutex()));
// initialize bounding box, it will be expanded during glyph position calculation
_textBB.init();