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 "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<std::string, osg::ref_ptr<osg::Image> >::iterator found = mImages.find(normalized);
if (found != mImages.end())
return found->second;
osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(normalized);
if (obj)
return osg::ref_ptr<osg::Image>(static_cast<osg::Image*>(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;
}
}

@ -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<std::string, osg::ref_ptr<osg::Image> > mImages;
osg::ref_ptr<osgDB::ObjectCache> mCache;
osg::ref_ptr<osg::Image> mWarningImage;
osg::ref_ptr<osgDB::Options> mOptions;

Loading…
Cancel
Save