Made Refernced::setThreadSafeReferenceCounting(bool) a virtual then overrode

this in various scene graph classes to ensure that the scene graph gets
updated as well as the objects that the initialial call is made from.
This commit is contained in:
Robert Osfield 2007-01-04 16:49:58 +00:00
parent cce656a19b
commit 73fffe1800
19 changed files with 140 additions and 16 deletions

View File

@ -150,9 +150,6 @@ int main( int argc, char **argv )
} }
} }
// osg::DisplaySettings::instance()->setMaxNumberOfGraphicsContexts(4);
osg::Referenced::setThreadSafeReferenceCounting(true);
// load the scene. // load the scene.
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
if (!loadedModel) if (!loadedModel)
@ -172,7 +169,7 @@ int main( int argc, char **argv )
if (apm.valid()) viewer.setCameraManipulator(apm.get()); if (apm.valid()) viewer.setCameraManipulator(apm.get());
else viewer.setCameraManipulator( new osgGA::TrackballManipulator() ); else viewer.setCameraManipulator( new osgGA::TrackballManipulator() );
#if 1 #if 0
// singleWindowMultipleCameras(viewer); // singleWindowMultipleCameras(viewer);
@ -184,13 +181,10 @@ int main( int argc, char **argv )
viewer.setUpViewAcrossAllScreens(); viewer.setUpViewAcrossAllScreens();
#endif #endif
loadedModel->resizeGLObjectBuffers(osg::DisplaySettings::instance()->getMaxNumberOfGraphicsContexts());
viewer.realize(); viewer.realize();
bool limitNumberOfFrames = false; bool limitNumberOfFrames = true;
unsigned int numFrames = 0; unsigned int numFrames = 0;
unsigned int maxFrames = 10; unsigned int maxFrames = 10;

View File

@ -1518,7 +1518,6 @@ int main( int argc, char **argv )
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
// todo for osgViewer // todo for osgViewer
osg::DisplaySettings::instance()->setMaxNumberOfGraphicsContexts(2);
osg::Referenced::setThreadSafeReferenceCounting(true); osg::Referenced::setThreadSafeReferenceCounting(true);
// construct the viewer. // construct the viewer.

View File

@ -278,6 +278,9 @@ class OSG_EXPORT Drawable : public Object
*/ */
virtual void compileGLObjects(RenderInfo& renderInfo) const; virtual void compileGLObjects(RenderInfo& renderInfo) const;
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe);
/** Resize any per context GLObject buffers to specified size. */ /** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize); virtual void resizeGLObjectBuffers(unsigned int maxSize);

View File

@ -138,6 +138,9 @@ class OSG_EXPORT Geode : public Node
virtual BoundingSphere computeBound() const; virtual BoundingSphere computeBound() const;
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe);
/** Resize any per context GLObject buffers to specified size. */ /** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize); virtual void resizeGLObjectBuffers(unsigned int maxSize);

View File

@ -142,6 +142,9 @@ class OSG_EXPORT Group : public Node
return _children.size(); // node not found. return _children.size(); // node not found.
} }
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe);
/** Resize any per context GLObject buffers to specified size. */ /** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize); virtual void resizeGLObjectBuffers(unsigned int maxSize);

View File

@ -299,6 +299,9 @@ class OSG_EXPORT Node : public Object
/** Get the const compute bound callback.*/ /** Get the const compute bound callback.*/
const ComputeBoundingSphereCallback* getComputeBoundingSphereCallback() const { return _computeBoundCallback.get(); } const ComputeBoundingSphereCallback* getComputeBoundingSphereCallback() const { return _computeBoundCallback.get(); }
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe);
/** Resize any per context GLObject buffers to specified size. */ /** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/); virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/);

View File

@ -61,6 +61,10 @@ class OSG_EXPORT Program : public osg::StateAttribute
* performing any rebuild operations that might be pending. */ * performing any rebuild operations that might be pending. */
virtual void apply(osg::State& state) const; virtual void apply(osg::State& state) const;
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe);
/** Compile program and associated shaders.*/
virtual void compileGLObjects(osg::State& state) const; virtual void compileGLObjects(osg::State& state) const;
/** Resize any per context GLObject buffers to specified size. */ /** Resize any per context GLObject buffers to specified size. */

View File

@ -53,7 +53,7 @@ class OSG_EXPORT Referenced
inline Referenced& operator = (const Referenced&) { return *this; } inline Referenced& operator = (const Referenced&) { return *this; }
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/ /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
void setThreadSafeRefUnref(bool threadSafe); virtual void setThreadSafeRefUnref(bool threadSafe);
/** Get whether a mutex is used to ensure ref() and unref() are thread safe.*/ /** Get whether a mutex is used to ensure ref() and unref() are thread safe.*/
bool getThreadSafeRefUnref() const { return _refMutex!=0; } bool getThreadSafeRefUnref() const { return _refMutex!=0; }

View File

@ -414,6 +414,9 @@ class OSG_EXPORT StateSet : public Object
* Return true if all associated modes are valid.*/ * Return true if all associated modes are valid.*/
bool checkValidityOfAssociatedModes(State& state) const; bool checkValidityOfAssociatedModes(State& state) const;
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe);
/** call compile on all StateAttributes contained within this StateSet.*/ /** call compile on all StateAttributes contained within this StateSet.*/
void compileGLObjects(State& state) const; void compileGLObjects(State& state) const;
@ -423,6 +426,8 @@ class OSG_EXPORT StateSet : public Object
/** call release on all StateAttributes contained within this StateSet.*/ /** call release on all StateAttributes contained within this StateSet.*/
virtual void releaseGLObjects(State* state=0) const; virtual void releaseGLObjects(State* state=0) const;
protected : protected :

View File

@ -479,6 +479,20 @@ void Drawable::compileGLObjects(RenderInfo& renderInfo) const
} }
void Drawable::setThreadSafeRefUnref(bool threadSafe)
{
Object::setThreadSafeRefUnref(threadSafe);
if (_stateset.valid()) _stateset->setThreadSafeRefUnref(threadSafe);
if (_updateCallback.valid()) _updateCallback->setThreadSafeRefUnref(threadSafe);
if (_eventCallback.valid()) _eventCallback->setThreadSafeRefUnref(threadSafe);
if (_cullCallback.valid()) _cullCallback->setThreadSafeRefUnref(threadSafe);
if (_drawCallback.valid()) _drawCallback->setThreadSafeRefUnref(threadSafe);
if (_userData.valid()) _userData->setThreadSafeRefUnref(threadSafe);
}
void Drawable::resizeGLObjectBuffers(unsigned int maxSize) void Drawable::resizeGLObjectBuffers(unsigned int maxSize)
{ {
if (_stateset.valid()) _stateset->resizeGLObjectBuffers(maxSize); if (_stateset.valid()) _stateset->resizeGLObjectBuffers(maxSize);

View File

@ -209,6 +209,18 @@ void Geode::compileDrawables(RenderInfo& renderInfo)
} }
} }
void Geode::setThreadSafeRefUnref(bool threadSafe)
{
Node::setThreadSafeRefUnref(threadSafe);
for(DrawableList::const_iterator itr=_drawables.begin();
itr!=_drawables.end();
++itr)
{
(*itr)->setThreadSafeRefUnref(threadSafe);
}
}
void Geode::resizeGLObjectBuffers(unsigned int maxSize) void Geode::resizeGLObjectBuffers(unsigned int maxSize)
{ {
Node::resizeGLObjectBuffers(maxSize); Node::resizeGLObjectBuffers(maxSize);

View File

@ -128,6 +128,7 @@ void GraphicsContext::decrementContextIDUsageCount(unsigned int contextID)
GraphicsContext::GraphicsContext(): GraphicsContext::GraphicsContext():
_threadOfLastMakeCurrent(0) _threadOfLastMakeCurrent(0)
{ {
setThreadSafeRefUnref(true);
_operationsBlock = new Block; _operationsBlock = new Block;
} }

View File

@ -379,6 +379,18 @@ BoundingSphere Group::computeBound() const
return bsphere; return bsphere;
} }
void Group::setThreadSafeRefUnref(bool threadSafe)
{
Node::setThreadSafeRefUnref(threadSafe);
for(NodeList::const_iterator itr=_children.begin();
itr!=_children.end();
++itr)
{
(*itr)->setThreadSafeRefUnref(threadSafe);
}
}
void Group::resizeGLObjectBuffers(unsigned int maxSize) void Group::resizeGLObjectBuffers(unsigned int maxSize)
{ {
Node::resizeGLObjectBuffers(maxSize); Node::resizeGLObjectBuffers(maxSize);

View File

@ -491,6 +491,19 @@ void Node::dirtyBound()
} }
} }
void Node::setThreadSafeRefUnref(bool threadSafe)
{
Object::setThreadSafeRefUnref(threadSafe);
if (_stateset.valid()) _stateset->setThreadSafeRefUnref(threadSafe);
if (_updateCallback.valid()) _updateCallback->setThreadSafeRefUnref(threadSafe);
if (_eventCallback.valid()) _eventCallback->setThreadSafeRefUnref(threadSafe);
if (_cullCallback.valid()) _cullCallback->setThreadSafeRefUnref(threadSafe);
if (_userData.valid()) _userData->setThreadSafeRefUnref(threadSafe);
}
void Node::resizeGLObjectBuffers(unsigned int maxSize) void Node::resizeGLObjectBuffers(unsigned int maxSize)
{ {
if (_stateset.valid()) _stateset->resizeGLObjectBuffers(maxSize); if (_stateset.valid()) _stateset->resizeGLObjectBuffers(maxSize);

View File

@ -1983,6 +1983,15 @@ void Program::compileGLObjects( osg::State& state ) const
getPCP( contextID )->linkProgram(); getPCP( contextID )->linkProgram();
} }
void Program::setThreadSafeRefUnref(bool threadSafe)
{
StateAttribute::setThreadSafeRefUnref(threadSafe);
for( unsigned int i=0; i < _shaderList.size(); ++i )
{
if (_shaderList[i].valid()) _shaderList[i]->setThreadSafeRefUnref(threadSafe);
}
}
void Program::dirtyProgram() void Program::dirtyProgram()
{ {

View File

@ -1180,6 +1180,30 @@ bool StateSet::checkValidityOfAssociatedModes(osg::State& state) const
return modesValid; return modesValid;
} }
void StateSet::setThreadSafeRefUnref(bool threadSafe)
{
Object::setThreadSafeRefUnref(threadSafe);
for(AttributeList::const_iterator itr = _attributeList.begin();
itr!=_attributeList.end();
++itr)
{
itr->second.first->setThreadSafeRefUnref(threadSafe);
}
for(TextureAttributeList::const_iterator taitr=_textureAttributeList.begin();
taitr!=_textureAttributeList.end();
++taitr)
{
for(AttributeList::const_iterator itr = taitr->begin();
itr!=taitr->end();
++itr)
{
itr->second.first->setThreadSafeRefUnref(threadSafe);
}
}
}
void StateSet::compileGLObjects(State& state) const void StateSet::compileGLObjects(State& state) const
{ {
for(AttributeList::const_iterator itr = _attributeList.begin(); for(AttributeList::const_iterator itr = _attributeList.begin();

View File

@ -585,6 +585,8 @@ void GraphicsWindowX11::closeImplementation()
XFlush( _display ); XFlush( _display );
XSync( _display,0 ); XSync( _display,0 );
XCloseDisplay( _display );
} }
_window = 0; _window = 0;
@ -626,6 +628,7 @@ void GraphicsWindowX11::checkEvents()
int windowWidth = _traits->width; int windowWidth = _traits->width;
int windowHeight = _traits->height; int windowHeight = _traits->height;
bool destroyWindowRequested = false;
// osg::notify(osg::NOTICE)<<"Check events"<<std::endl; // osg::notify(osg::NOTICE)<<"Check events"<<std::endl;
while( XPending(_display) ) while( XPending(_display) )
@ -640,7 +643,7 @@ void GraphicsWindowX11::checkEvents()
if (static_cast<Atom>(ev.xclient.data.l[0]) == _deleteWindow) if (static_cast<Atom>(ev.xclient.data.l[0]) == _deleteWindow)
{ {
osg::notify(osg::INFO)<<"DeleteWindow event recieved"<<std::endl; osg::notify(osg::INFO)<<"DeleteWindow event recieved"<<std::endl;
close(); destroyWindowRequested = true;
} }
} }
case Expose : case Expose :
@ -842,6 +845,12 @@ void GraphicsWindowX11::checkEvents()
resized(windowX, windowY, windowWidth, windowHeight); resized(windowX, windowY, windowWidth, windowHeight);
getEventQueue()->windowResize(windowX, windowY, windowWidth, windowHeight); getEventQueue()->windowResize(windowX, windowY, windowWidth, windowHeight);
} }
if (destroyWindowRequested)
{
close();
}
} }
void GraphicsWindowX11::grabFocus() void GraphicsWindowX11::grabFocus()

View File

@ -22,7 +22,11 @@ using namespace osgViewer;
View::View() View::View()
{ {
// osg::notify(osg::NOTICE)<<"Constructing osgViewer::View"<<std::endl; // osg::notify(osg::NOTICE)<<"Constructing osgViewer::View"<<std::endl;
setEventQueue(new osgGA::EventQueue);
// make sure View is safe to reference multi-threaded.
setThreadSafeRefUnref(true);
setEventQueue(new osgGA::EventQueue);
} }
View::~View() View::~View()

View File

@ -158,7 +158,19 @@ void Viewer::startThreading()
stopThreading(); stopThreading();
} }
osg::notify(osg::INFO)<<"Viewer::startThreading() - starting threading"<<std::endl; // using multi-threading so make sure that new objects are allocated with thread safe ref/unref
osg::Referenced::setThreadSafeReferenceCounting(true);
if (getSceneData())
{
// make sure that existing scene graph objects are allocated with thread safe ref/unref
getSceneData()->setThreadSafeRefUnref(true);
// update the scene graph so that it has enough GL object buffer memory for the graphics contexts that will be using it.
getSceneData()->resizeGLObjectBuffers(osg::DisplaySettings::instance()->getMaxNumberOfGraphicsContexts());
}
osg::notify(osg::NOTICE)<<"Viewer::startThreading() - starting threading"<<std::endl;
Contexts contexts; Contexts contexts;
getContexts(contexts); getContexts(contexts);
@ -320,7 +332,6 @@ void Viewer::realize()
++citr) ++citr)
{ {
(*citr)->realize(); (*citr)->realize();
// OpenThreads::Thread::YieldCurrentThread();
} }
bool grabFocus = true; bool grabFocus = true;
@ -336,8 +347,9 @@ void Viewer::realize()
gw->grabFocusIfPointerInWindow(); gw->grabFocusIfPointerInWindow();
} }
} }
} }
startThreading(); startThreading();
// initialize the global timer to be relative to the current time. // initialize the global timer to be relative to the current time.