Added a concrete osg::DeleteHandler implementation which provides support for
retain objects for several frames before deleting them. Also added RenderStageCache into CullVistor.cpp that is used for handling RTT osg::Camera's that are being used in double buffered SceneView usage.
This commit is contained in:
126
src/osg/DeleteHandler.cpp
Normal file
126
src/osg/DeleteHandler.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/* -*-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 <osg/DeleteHandler>
|
||||
#include <osg/Notify>
|
||||
|
||||
namespace osg
|
||||
{
|
||||
|
||||
// specialzed smart pointer, used to get round auto_ptr<>'s lack of the destructor reseting itself to 0.
|
||||
struct DeleteHandlerPointer
|
||||
{
|
||||
DeleteHandlerPointer():
|
||||
_ptr(0) {}
|
||||
|
||||
DeleteHandlerPointer(DeleteHandler* ptr):
|
||||
_ptr(ptr) {}
|
||||
|
||||
~DeleteHandlerPointer()
|
||||
{
|
||||
delete _ptr;
|
||||
_ptr = 0;
|
||||
}
|
||||
|
||||
inline DeleteHandlerPointer& operator = (DeleteHandler* ptr)
|
||||
{
|
||||
if (_ptr==ptr) return *this;
|
||||
delete _ptr;
|
||||
_ptr = ptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void reset(DeleteHandler* ptr)
|
||||
{
|
||||
if (_ptr==ptr) return;
|
||||
delete _ptr;
|
||||
_ptr = ptr;
|
||||
}
|
||||
|
||||
inline DeleteHandler& operator*() { return *_ptr; }
|
||||
|
||||
inline const DeleteHandler& operator*() const { return *_ptr; }
|
||||
|
||||
inline DeleteHandler* operator->() { return _ptr; }
|
||||
|
||||
inline const DeleteHandler* operator->() const { return _ptr; }
|
||||
|
||||
DeleteHandler* get() { return _ptr; }
|
||||
|
||||
const DeleteHandler* get() const { return _ptr; }
|
||||
|
||||
DeleteHandler* _ptr;
|
||||
};
|
||||
|
||||
|
||||
DeleteHandler::DeleteHandler(int numberOfFramesToRetainObjects):
|
||||
_numFramesToRetainObjects(numberOfFramesToRetainObjects)
|
||||
{
|
||||
}
|
||||
|
||||
void DeleteHandler::flush()
|
||||
{
|
||||
typedef std::list<const osg::Referenced*> DeletionList;
|
||||
DeletionList deletionList;
|
||||
|
||||
{
|
||||
// gather all the objects to delete whilst holding the mutex to the _objectsToDelete
|
||||
// list, but delete the objects outside this scoped lock so that if any objects deleted
|
||||
// unref their children then no deadlock happens.
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
||||
int frameNumberToClearTo = _currentFrameNumber - _numFramesToRetainObjects;
|
||||
|
||||
ObjectsToDeleteList::iterator itr;
|
||||
for(itr = _objectsToDelete.begin();
|
||||
itr != _objectsToDelete.end();
|
||||
++itr)
|
||||
{
|
||||
if (itr->first > frameNumberToClearTo) break;
|
||||
|
||||
deletionList.push_back(itr->second);
|
||||
|
||||
itr->second = 0;
|
||||
}
|
||||
|
||||
_objectsToDelete.erase( _objectsToDelete.begin(), itr);
|
||||
}
|
||||
|
||||
for(DeletionList::iterator ditr = deletionList.begin();
|
||||
ditr != deletionList.end();
|
||||
++ditr)
|
||||
{
|
||||
doDelete(*ditr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DeleteHandler::flushAll()
|
||||
{
|
||||
int temp = _numFramesToRetainObjects;
|
||||
_numFramesToRetainObjects = 0;
|
||||
|
||||
flush();
|
||||
|
||||
_numFramesToRetainObjects = temp;
|
||||
}
|
||||
|
||||
void DeleteHandler::requestDelete(const osg::Referenced* object)
|
||||
{
|
||||
if (_numFramesToRetainObjects==0) doDelete(object);
|
||||
else
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
||||
_objectsToDelete.push_back(FrameNumberObjectPair(_currentFrameNumber,object));
|
||||
}
|
||||
}
|
||||
|
||||
}; // end of namespace osg
|
||||
@@ -33,6 +33,7 @@ CXXFILES =\
|
||||
CullingSet.cpp\
|
||||
CullSettings.cpp\
|
||||
CullStack.cpp\
|
||||
DeleteHandler.cpp\
|
||||
Depth.cpp\
|
||||
DisplaySettings.cpp\
|
||||
Drawable.cpp\
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
#include <OpenThreads/ScopedLock>
|
||||
#include <OpenThreads/Mutex>
|
||||
|
||||
#include <osg/DeleteHandler>
|
||||
|
||||
#ifndef OSG_JAVA_BUILD
|
||||
|
||||
namespace osg
|
||||
@@ -177,43 +179,7 @@ void Referenced::setThreadSafeRefUnref(bool threadSafe)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
void Referenced::ref() const
|
||||
{
|
||||
if (_refMutex)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex);
|
||||
++_refCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
++_refCount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Referenced::unref() const
|
||||
{
|
||||
bool needDelete = false;
|
||||
if (_refMutex)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex);
|
||||
--_refCount;
|
||||
needDelete = _refCount<=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
--_refCount;
|
||||
needDelete = _refCount<=0;
|
||||
}
|
||||
|
||||
if (needDelete)
|
||||
{
|
||||
if (getDeleteHandler()) getDeleteHandler()->requestDelete(this);
|
||||
else delete this;
|
||||
}
|
||||
}
|
||||
*/
|
||||
void Referenced::unref_nodelete() const
|
||||
{
|
||||
if (_refMutex)
|
||||
@@ -257,6 +223,11 @@ void Referenced::removeObserver(Observer* observer)
|
||||
}
|
||||
}
|
||||
|
||||
void Referenced::deletUsingDeleteHandler() const
|
||||
{
|
||||
getDeleteHandler()->requestDelete(this);
|
||||
}
|
||||
|
||||
}; // end of namespace osg
|
||||
|
||||
#endif //OSG_JAVA_BUILD
|
||||
|
||||
Reference in New Issue
Block a user