mirror of https://github.com/OpenMW/openmw.git
Preload instances in SceneManager
parent
2e62298bd3
commit
e28dc3e72f
@ -0,0 +1,79 @@
|
||||
#include "multiobjectcache.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <osg/Object>
|
||||
|
||||
namespace Resource
|
||||
{
|
||||
|
||||
MultiObjectCache::MultiObjectCache()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MultiObjectCache::~MultiObjectCache()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void MultiObjectCache::removeUnreferencedObjectsInCache()
|
||||
{
|
||||
std::vector<osg::ref_ptr<osg::Object> > objectsToRemove;
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
|
||||
// Remove unreferenced entries from object cache
|
||||
ObjectCacheMap::iterator oitr = _objectCache.begin();
|
||||
while(oitr != _objectCache.end())
|
||||
{
|
||||
if (oitr->second->referenceCount() <= 1)
|
||||
{
|
||||
objectsToRemove.push_back(oitr->second);
|
||||
_objectCache.erase(oitr++);
|
||||
}
|
||||
else
|
||||
{
|
||||
++oitr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// note, actual unref happens outside of the lock
|
||||
objectsToRemove.clear();
|
||||
}
|
||||
|
||||
void MultiObjectCache::addEntryToObjectCache(const std::string &filename, osg::Object *object)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
_objectCache.insert(std::make_pair(filename, object));
|
||||
}
|
||||
|
||||
osg::ref_ptr<osg::Object> MultiObjectCache::takeFromObjectCache(const std::string &fileName)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
ObjectCacheMap::iterator found = _objectCache.find(fileName);
|
||||
if (found == _objectCache.end())
|
||||
return osg::ref_ptr<osg::Object>();
|
||||
else
|
||||
{
|
||||
osg::ref_ptr<osg::Object> object = found->second;
|
||||
_objectCache.erase(found);
|
||||
return object;
|
||||
}
|
||||
}
|
||||
|
||||
void MultiObjectCache::releaseGLObjects(osg::State *state)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
|
||||
for(ObjectCacheMap::iterator itr = _objectCache.begin();
|
||||
itr != _objectCache.end();
|
||||
++itr)
|
||||
{
|
||||
osg::Object* object = itr->second.get();
|
||||
object->releaseGLObjects(state);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
#ifndef OPENMW_COMPONENTS_MULTIOBJECTCACHE_H
|
||||
#define OPENMW_COMPONENTS_MULTIOBJECTCACHE_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
#include <osg/Referenced>
|
||||
|
||||
namespace osg
|
||||
{
|
||||
class Object;
|
||||
class State;
|
||||
}
|
||||
|
||||
namespace Resource
|
||||
{
|
||||
|
||||
/// @brief Cache for "non reusable" objects.
|
||||
class MultiObjectCache : public osg::Referenced
|
||||
{
|
||||
public:
|
||||
MultiObjectCache();
|
||||
~MultiObjectCache();
|
||||
|
||||
void removeUnreferencedObjectsInCache();
|
||||
|
||||
void addEntryToObjectCache(const std::string& filename, osg::Object* object);
|
||||
|
||||
/** Take an Object from cache. Return NULL if no object found. */
|
||||
osg::ref_ptr<osg::Object> takeFromObjectCache(const std::string& fileName);
|
||||
|
||||
/** call releaseGLObjects on all objects attached to the object cache.*/
|
||||
void releaseGLObjects(osg::State* state);
|
||||
|
||||
protected:
|
||||
|
||||
typedef std::multimap<std::string, osg::ref_ptr<osg::Object> > ObjectCacheMap;
|
||||
|
||||
ObjectCacheMap _objectCache;
|
||||
OpenThreads::Mutex _objectCacheMutex;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue