Use the osgDB::ObjectCache in ImageManager

Should be thread safe now.
coverity_scan
scrawl 9 years ago
parent a72af4a1a3
commit 6c1c653cba

@ -6,6 +6,8 @@
#include <components/vfs/manager.hpp> #include <components/vfs/manager.hpp>
#include "objectcache.hpp"
#ifdef OSG_LIBRARY_STATIC #ifdef OSG_LIBRARY_STATIC
// This list of plugins should match with the list in the top-level CMakelists.txt. // This list of plugins should match with the list in the top-level CMakelists.txt.
USE_OSGPLUGIN(png) USE_OSGPLUGIN(png)
@ -41,6 +43,7 @@ namespace Resource
ImageManager::ImageManager(const VFS::Manager *vfs) ImageManager::ImageManager(const VFS::Manager *vfs)
: mVFS(vfs) : mVFS(vfs)
, mCache(new osgDB::ObjectCache)
, mWarningImage(createWarningImage()) , mWarningImage(createWarningImage())
, mOptions(new osgDB::Options("dds_flip dds_dxt1_detect_rgba")) , mOptions(new osgDB::Options("dds_flip dds_dxt1_detect_rgba"))
{ {
@ -88,9 +91,10 @@ namespace Resource
{ {
std::string normalized = filename; std::string normalized = filename;
mVFS->normalizeFilename(normalized); mVFS->normalizeFilename(normalized);
std::map<std::string, osg::ref_ptr<osg::Image> >::iterator found = mImages.find(normalized);
if (found != mImages.end()) osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(normalized);
return found->second; if (obj)
return osg::ref_ptr<osg::Image>(static_cast<osg::Image*>(obj.get()));
else else
{ {
Files::IStreamPtr stream; Files::IStreamPtr stream;
@ -101,6 +105,7 @@ namespace Resource
catch (std::exception& e) catch (std::exception& e)
{ {
std::cerr << "Failed to open image: " << e.what() << std::endl; std::cerr << "Failed to open image: " << e.what() << std::endl;
mCache->addEntryToObjectCache(normalized, mWarningImage);
return mWarningImage; return mWarningImage;
} }
@ -112,6 +117,7 @@ namespace Resource
if (!reader) if (!reader)
{ {
std::cerr << "Error loading " << filename << ": no readerwriter for '" << ext << "' found" << std::endl; std::cerr << "Error loading " << filename << ": no readerwriter for '" << ext << "' found" << std::endl;
mCache->addEntryToObjectCache(normalized, mWarningImage);
return mWarningImage; return mWarningImage;
} }
@ -119,16 +125,18 @@ namespace Resource
if (!result.success()) if (!result.success())
{ {
std::cerr << "Error loading " << filename << ": " << result.message() << " code " << result.status() << std::endl; std::cerr << "Error loading " << filename << ": " << result.message() << " code " << result.status() << std::endl;
mCache->addEntryToObjectCache(normalized, mWarningImage);
return mWarningImage; return mWarningImage;
} }
osg::Image* image = result.getImage(); osg::Image* image = result.getImage();
if (!checkSupported(image, filename)) if (!checkSupported(image, filename))
{ {
mCache->addEntryToObjectCache(normalized, mWarningImage);
return mWarningImage; return mWarningImage;
} }
mImages.insert(std::make_pair(normalized, image)); mCache->addEntryToObjectCache(normalized, image);
return image; return image;
} }
} }

@ -21,12 +21,14 @@ namespace VFS
namespace osgDB namespace osgDB
{ {
class Options; class Options;
class ObjectCache;
} }
namespace Resource namespace Resource
{ {
/// @brief Handles loading/caching of Images. /// @brief Handles loading/caching of Images.
/// @note May be used from any thread.
class ImageManager class ImageManager
{ {
public: public:
@ -44,8 +46,7 @@ namespace Resource
private: private:
const VFS::Manager* mVFS; const VFS::Manager* mVFS;
// TODO: use ObjectCache osg::ref_ptr<osgDB::ObjectCache> mCache;
std::map<std::string, osg::ref_ptr<osg::Image> > mImages;
osg::ref_ptr<osg::Image> mWarningImage; osg::ref_ptr<osg::Image> mWarningImage;
osg::ref_ptr<osgDB::Options> mOptions; osg::ref_ptr<osgDB::Options> mOptions;

Loading…
Cancel
Save