diff --git a/components/resource/imagemanager.cpp b/components/resource/imagemanager.cpp index e1c0572c7..bd422fe2a 100644 --- a/components/resource/imagemanager.cpp +++ b/components/resource/imagemanager.cpp @@ -6,6 +6,8 @@ #include +#include "objectcache.hpp" + #ifdef OSG_LIBRARY_STATIC // This list of plugins should match with the list in the top-level CMakelists.txt. USE_OSGPLUGIN(png) @@ -41,6 +43,7 @@ namespace Resource ImageManager::ImageManager(const VFS::Manager *vfs) : mVFS(vfs) + , mCache(new osgDB::ObjectCache) , mWarningImage(createWarningImage()) , mOptions(new osgDB::Options("dds_flip dds_dxt1_detect_rgba")) { @@ -88,9 +91,10 @@ namespace Resource { std::string normalized = filename; mVFS->normalizeFilename(normalized); - std::map >::iterator found = mImages.find(normalized); - if (found != mImages.end()) - return found->second; + + osg::ref_ptr obj = mCache->getRefFromObjectCache(normalized); + if (obj) + return osg::ref_ptr(static_cast(obj.get())); else { Files::IStreamPtr stream; @@ -101,6 +105,7 @@ namespace Resource catch (std::exception& e) { std::cerr << "Failed to open image: " << e.what() << std::endl; + mCache->addEntryToObjectCache(normalized, mWarningImage); return mWarningImage; } @@ -112,6 +117,7 @@ namespace Resource if (!reader) { std::cerr << "Error loading " << filename << ": no readerwriter for '" << ext << "' found" << std::endl; + mCache->addEntryToObjectCache(normalized, mWarningImage); return mWarningImage; } @@ -119,16 +125,18 @@ namespace Resource if (!result.success()) { std::cerr << "Error loading " << filename << ": " << result.message() << " code " << result.status() << std::endl; + mCache->addEntryToObjectCache(normalized, mWarningImage); return mWarningImage; } osg::Image* image = result.getImage(); if (!checkSupported(image, filename)) { + mCache->addEntryToObjectCache(normalized, mWarningImage); return mWarningImage; } - mImages.insert(std::make_pair(normalized, image)); + mCache->addEntryToObjectCache(normalized, image); return image; } } diff --git a/components/resource/imagemanager.hpp b/components/resource/imagemanager.hpp index 3b2bacc13..bc1d7b4e4 100644 --- a/components/resource/imagemanager.hpp +++ b/components/resource/imagemanager.hpp @@ -21,12 +21,14 @@ namespace VFS namespace osgDB { class Options; + class ObjectCache; } namespace Resource { /// @brief Handles loading/caching of Images. + /// @note May be used from any thread. class ImageManager { public: @@ -44,8 +46,7 @@ namespace Resource private: const VFS::Manager* mVFS; - // TODO: use ObjectCache - std::map > mImages; + osg::ref_ptr mCache; osg::ref_ptr mWarningImage; osg::ref_ptr mOptions;