Add osgDB::ObjectCache to the repository to work around it not being available in OSG 3.2
parent
a29aef14f1
commit
d3fa8a8602
@ -0,0 +1,141 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#include "objectcache.hpp"
|
||||
|
||||
#if OSG_VERSION_LESS_THAN(3,4,0)
|
||||
|
||||
#include <osgDB/ObjectCache>
|
||||
|
||||
using namespace osgDB;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ObjectCache
|
||||
//
|
||||
ObjectCache::ObjectCache():
|
||||
osg::Referenced(true)
|
||||
{
|
||||
// OSG_NOTICE<<"Constructed ObjectCache"<<std::endl;
|
||||
}
|
||||
|
||||
ObjectCache::~ObjectCache()
|
||||
{
|
||||
// OSG_NOTICE<<"Destructed ObjectCache"<<std::endl;
|
||||
}
|
||||
|
||||
void ObjectCache::addObjectCache(ObjectCache* objectCache)
|
||||
{
|
||||
// don't allow a cache to be added to itself.
|
||||
if (objectCache==this) return;
|
||||
|
||||
// lock both ObjectCache to prevent their contents from being modified by other threads while we merge.
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock1(_objectCacheMutex);
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock2(objectCache->_objectCacheMutex);
|
||||
|
||||
// OSG_NOTICE<<"Inserting objects to main ObjectCache "<<objectCache->_objectCache.size()<<std::endl;
|
||||
|
||||
_objectCache.insert(objectCache->_objectCache.begin(), objectCache->_objectCache.end());
|
||||
}
|
||||
|
||||
|
||||
void ObjectCache::addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
_objectCache[filename]=ObjectTimeStampPair(object,timestamp);
|
||||
}
|
||||
|
||||
osg::Object* ObjectCache::getFromObjectCache(const std::string& fileName)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
ObjectCacheMap::iterator itr = _objectCache.find(fileName);
|
||||
if (itr!=_objectCache.end()) return itr->second.first.get();
|
||||
else return 0;
|
||||
}
|
||||
|
||||
osg::ref_ptr<osg::Object> ObjectCache::getRefFromObjectCache(const std::string& fileName)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
ObjectCacheMap::iterator itr = _objectCache.find(fileName);
|
||||
if (itr!=_objectCache.end())
|
||||
{
|
||||
// OSG_NOTICE<<"Found "<<fileName<<" in ObjectCache "<<this<<std::endl;
|
||||
return itr->second.first;
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
|
||||
void ObjectCache::updateTimeStampOfObjectsInCacheWithExternalReferences(double referenceTime)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
|
||||
// look for objects with external references and update their time stamp.
|
||||
for(ObjectCacheMap::iterator itr=_objectCache.begin();
|
||||
itr!=_objectCache.end();
|
||||
++itr)
|
||||
{
|
||||
// if ref count is greater the 1 the object has an external reference.
|
||||
if (itr->second.first->referenceCount()>1)
|
||||
{
|
||||
// so update it time stamp.
|
||||
itr->second.second = referenceTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ObjectCache::removeExpiredObjectsInCache(double expiryTime)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
|
||||
// Remove expired entries from object cache
|
||||
ObjectCacheMap::iterator oitr = _objectCache.begin();
|
||||
while(oitr != _objectCache.end())
|
||||
{
|
||||
if (oitr->second.second<=expiryTime)
|
||||
{
|
||||
_objectCache.erase(oitr++);
|
||||
}
|
||||
else
|
||||
{
|
||||
++oitr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ObjectCache::removeFromObjectCache(const std::string& fileName)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
ObjectCacheMap::iterator itr = _objectCache.find(fileName);
|
||||
if (itr!=_objectCache.end()) _objectCache.erase(itr);
|
||||
}
|
||||
|
||||
void ObjectCache::clear()
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
_objectCache.clear();
|
||||
}
|
||||
|
||||
void ObjectCache::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.first.get();
|
||||
object->releaseGLObjects(state);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,92 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
// Wrapper for osgDB/ObjectCache. Works around ObjectCache not being available in old OSG 3.2.
|
||||
// Use "#include objectcache.hpp" in place of "#include <osgDB/ObjectCache".
|
||||
|
||||
#ifndef OSGDB_OBJECTCACHE_WRAPPER
|
||||
#define OSGDB_OBJECTCACHE_WRAPPER 1
|
||||
|
||||
#include <osg/Version>
|
||||
|
||||
#if OSG_VERSION_GREATER_OR_EQUAL(3,4,0)
|
||||
#include <osgDB/ObjectCache>
|
||||
#else
|
||||
|
||||
#include <osg/Node>
|
||||
|
||||
#include <osgDB/ReaderWriter>
|
||||
#include <osgDB/DatabaseRevisions>
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace osgDB {
|
||||
|
||||
class /*OSGDB_EXPORT*/ ObjectCache : public osg::Referenced
|
||||
{
|
||||
public:
|
||||
|
||||
ObjectCache();
|
||||
|
||||
/** For each object in the cache which has an reference count greater than 1
|
||||
* (and therefore referenced by elsewhere in the application) set the time stamp
|
||||
* for that object in the cache to specified time.
|
||||
* This would typically be called once per frame by applications which are doing database paging,
|
||||
* and need to prune objects that are no longer required.
|
||||
* The time used should be taken from the FrameStamp::getReferenceTime().*/
|
||||
void updateTimeStampOfObjectsInCacheWithExternalReferences(double referenceTime);
|
||||
|
||||
/** Removed object in the cache which have a time stamp at or before the specified expiry time.
|
||||
* This would typically be called once per frame by applications which are doing database paging,
|
||||
* and need to prune objects that are no longer required, and called after the a called
|
||||
* after the call to updateTimeStampOfObjectsInCacheWithExternalReferences(expirtyTime).*/
|
||||
void removeExpiredObjectsInCache(double expiryTime);
|
||||
|
||||
/** Remove all objects in the cache regardless of having external references or expiry times.*/
|
||||
void clear();
|
||||
|
||||
/** Add contents of specified ObjectCache to this object cache.*/
|
||||
void addObjectCache(ObjectCache* object);
|
||||
|
||||
/** Add a filename,object,timestamp triple to the Registry::ObjectCache.*/
|
||||
void addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp = 0.0);
|
||||
|
||||
/** Remove Object from cache.*/
|
||||
void removeFromObjectCache(const std::string& fileName);
|
||||
|
||||
/** Get an Object from the object cache*/
|
||||
osg::Object* getFromObjectCache(const std::string& fileName);
|
||||
|
||||
/** Get an ref_ptr<Object> from the object cache*/
|
||||
osg::ref_ptr<osg::Object> getRefFromObjectCache(const std::string& fileName);
|
||||
|
||||
/** call rleaseGLObjects on all objects attached to the object cache.*/
|
||||
void releaseGLObjects(osg::State* state);
|
||||
|
||||
protected:
|
||||
|
||||
virtual ~ObjectCache();
|
||||
|
||||
typedef std::pair<osg::ref_ptr<osg::Object>, double > ObjectTimeStampPair;
|
||||
typedef std::map<std::string, ObjectTimeStampPair > ObjectCacheMap;
|
||||
|
||||
ObjectCacheMap _objectCache;
|
||||
OpenThreads::Mutex _objectCacheMutex;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue