From 24d61c2df0c207dc04754754c49674ace768331c Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 25 Jul 2019 14:10:01 +0100 Subject: [PATCH] Change the DefaultFont so that it's managemed via the ObjectCache to enabl it to be release and cleared in a central manner. Added call to Registry::releaseGLObjects() to osgViewer/Renderer.cpp to enable automatic clean up of objects in the ObjectCache. --- include/osgText/Font | 2 +- src/osgText/DefaultFont.h | 2 ++ src/osgText/Font.cpp | 16 +++++++++++++--- src/osgViewer/Renderer.cpp | 3 +++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/osgText/Font b/include/osgText/Font index ab4641c93..7f359f3f9 100644 --- a/include/osgText/Font +++ b/include/osgText/Font @@ -83,7 +83,7 @@ public: virtual std::string getFileName() const; - static osg::ref_ptr& getDefaultFont(); + static osg::ref_ptr getDefaultFont(); typedef std::vector< osg::ref_ptr > StateSets; StateSets& getCachedStateSets() { return _statesets; } diff --git a/src/osgText/DefaultFont.h b/src/osgText/DefaultFont.h index c578fa0ee..79aacf637 100644 --- a/src/osgText/DefaultFont.h +++ b/src/osgText/DefaultFont.h @@ -28,6 +28,8 @@ public: DefaultFont(); + virtual const char* className() const { return "DefaultFont"; } + virtual std::string getFileName() const { return ""; } virtual bool supportsMultipleFontResolutions() const { return false; } diff --git a/src/osgText/Font.cpp b/src/osgText/Font.cpp index 7422e62d4..1a3e17607 100644 --- a/src/osgText/Font.cpp +++ b/src/osgText/Font.cpp @@ -36,13 +36,23 @@ using namespace osgText; using namespace std; -osg::ref_ptr& Font::getDefaultFont() +osg::ref_ptr Font::getDefaultFont() { static OpenThreads::Mutex s_DefaultFontMutex; OpenThreads::ScopedLock lock(s_DefaultFontMutex); - static osg::ref_ptr s_defaultFont = new DefaultFont; - return s_defaultFont; + osg::ref_ptr object = osgDB::Registry::instance()->getObjectCache()->getFromObjectCache("DefaultFont"); + osg::ref_ptr font = dynamic_cast(object.get()); + if (!font) + { + font = new DefaultFont; + osgDB::Registry::instance()->getObjectCache()->addEntryToObjectCache("DefaultFont", font.get()); + return font; + } + else + { + return font; + } } static OpenThreads::ReentrantMutex& getFontFileMutex() diff --git a/src/osgViewer/Renderer.cpp b/src/osgViewer/Renderer.cpp index ab9330818..ab6e18460 100644 --- a/src/osgViewer/Renderer.cpp +++ b/src/osgViewer/Renderer.cpp @@ -25,6 +25,7 @@ #include #include +#include #include @@ -960,6 +961,8 @@ void Renderer::resizeGLObjectBuffers(unsigned int maxSize) void Renderer::releaseGLObjects(osg::State* state) const { + osgDB::Registry::instance()->releaseGLObjects(state); + if (_sceneView[0].valid()) _sceneView[0]->releaseGLObjects(state); if (_sceneView[1].valid()) _sceneView[1]->releaseGLObjects(state); }