From aba3b4fa67ee1455bcafd2a3fd997ca9f8e627f9 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 30 Aug 2005 14:41:08 +0000 Subject: [PATCH] Clean up and fixes to GraphicThread class, and osgcamera example. --- examples/osgcamera/osgcamera.cpp | 41 ++++++------------- include/osg/GraphicsContext | 10 +++-- .../osgProducer/GraphicsContextImplementation | 1 + src/osg/GraphicsContext.cpp | 39 ++++++++---------- src/osg/GraphicsThread.cpp | 18 ++++---- .../GraphicsContextImplementation.cpp | 25 +++++++++-- src/osgUtil/CullVisitor.cpp | 6 ++- 7 files changed, 73 insertions(+), 67 deletions(-) diff --git a/examples/osgcamera/osgcamera.cpp b/examples/osgcamera/osgcamera.cpp index 97ccd9785..90f6335b8 100644 --- a/examples/osgcamera/osgcamera.cpp +++ b/examples/osgcamera/osgcamera.cpp @@ -47,8 +47,6 @@ struct FrameOperation : public osg::GraphicsThread::Operation virtual void operator () (osg::GraphicsContext* context) { - std::cout<<"FrameOperation draw begin"<setState(context->getState()); _sceneView->setProjectionMatrix(_camera->getProjectionMatrix()); _sceneView->setViewMatrix(_camera->getViewMatrix()); @@ -56,8 +54,6 @@ struct FrameOperation : public osg::GraphicsThread::Operation _sceneView->cull(); _sceneView->draw(); - - std::cout<<"FrameOperation draw end"< _camera; @@ -127,9 +123,6 @@ int main( int argc, char **argv ) return 1; } - // realise the window - gfxc->realize(); - camera->setGraphicsContext(gfxc.get()); // initialize the view to look at the center of the scene graph @@ -141,7 +134,7 @@ int main( int argc, char **argv ) camera->setProjectionMatrixAsPerspective(50.0f,1.4f,1.0f,10000.0f); camera->setViewMatrix(viewMatrix); - + // graphics thread will realize the window. gfxc->createGraphicsThread(); cameraMap[camera] = new FrameOperation(camera.get(), frameStamp.get()); @@ -162,31 +155,23 @@ int main( int argc, char **argv ) std::cout<<"nubmer of gfx."<getGraphicsThread()->add(swapOp.get(), true); - } - // record the timer tick at the start of rendering. osg::Timer_t start_tick = osg::Timer::instance()->tick(); + osg::Timer_t previous_tick = start_tick; bool done = false; // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.) while( !done ) { - std::cout<<"Frame "<setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick())); + osg::Timer_t current_tick = osg::Timer::instance()->tick(); + + frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,current_tick)); frameStamp->setFrameNumber(frameNum++); - std::cout<<"Frame rate "<<(double)frameNum / frameStamp->getReferenceTime()<delta_s(previous_tick,current_tick)<accept(updateVisitor); @@ -200,6 +185,7 @@ int main( int argc, char **argv ) camera->getGraphicsContext()->getGraphicsThread()->add( citr->second.get(), false); } + GraphicsContextSet::iterator gitr; for(gitr = graphicsContextSet.begin(); gitr != graphicsContextSet.end(); ++gitr) @@ -209,24 +195,23 @@ int main( int argc, char **argv ) context->getGraphicsThread()->add(preSwapBarrierOp.get(), false); } - std::cout<<"Join frameEndBarrierOp block "<tick(); frameEndBarrierOp->block(); osg::Timer_t after_tick = osg::Timer::instance()->tick(); - std::cout<<"Leave frameEndBarrierOp block "<delta_s(before_tick,after_tick)<delta_s(before_tick,after_tick)<tick(); -// preSwapBarrierOp->block(); after_tick = osg::Timer::instance()->tick(); - std::cout<<"Leave preSwapBarrierOp block "<delta_s(before_tick,after_tick)<delta_s(before_tick,after_tick)<getGraphicsThread()->add(swapOp.get(), false); + context->getGraphicsThread()->add(swapOp.get(), true); } // check if any of the windows are closed diff --git a/include/osg/GraphicsContext b/include/osg/GraphicsContext index ba4745a15..21a400f31 100644 --- a/include/osg/GraphicsContext +++ b/include/osg/GraphicsContext @@ -138,8 +138,12 @@ class OSG_EXPORT GraphicsContext : public Referenced /** Realise the GraphicsContext.*/ bool realize(); - /** close the graphics context.*/ - void close(); + /** close the graphics context. + * close(bool) stops any associated graphics threads, releases the contextID for the GraphicsContext then + * optional calls closeImplementation() to do the actual deletion of the graphics. This call is made optional + * as there are times when the graphics context has already been deleted externally and only the OSG side + * of the its data need to be closed down. */ + void close(bool callCloseImplementation=true); /** swap the front and back buffers.*/ void swapBuffers(); @@ -216,8 +220,6 @@ class OSG_EXPORT GraphicsContext : public Referenced virtual ~GraphicsContext(); - bool _closeOnDestruction; - ref_ptr _traits; ref_ptr _state; OpenThreads::Mutex _mutex; diff --git a/include/osgProducer/GraphicsContextImplementation b/include/osgProducer/GraphicsContextImplementation index e4c10db9a..7a31a448c 100644 --- a/include/osgProducer/GraphicsContextImplementation +++ b/include/osgProducer/GraphicsContextImplementation @@ -70,6 +70,7 @@ class OSGPRODUCER_EXPORT GraphicsContextImplementation : public osg::GraphicsCon virtual ~GraphicsContextImplementation(); + bool _closeOnDestruction; osg::ref_ptr _rs; }; diff --git a/src/osg/GraphicsContext.cpp b/src/osg/GraphicsContext.cpp index 44fe458bd..d8a643468 100644 --- a/src/osg/GraphicsContext.cpp +++ b/src/osg/GraphicsContext.cpp @@ -113,25 +113,13 @@ void GraphicsContext::decrementContextIDUsageCount(unsigned int contextID) GraphicsContext::GraphicsContext(): - _closeOnDestruction(true), _threadOfLastMakeCurrent(0) { } GraphicsContext::~GraphicsContext() { - // switch off the graphics thread... - setGraphicsThread(0); - - if (_state.valid()) - { - decrementContextIDUsageCount(_state->getContextID()); - } - - if (_closeOnDestruction) - { - close(); - } + close(false); } /** Realise the GraphicsContext.*/ @@ -151,18 +139,25 @@ bool GraphicsContext::realize() } } -void GraphicsContext::close() +void GraphicsContext::close(bool callCloseImplementation) { // switch off the graphics thread... setGraphicsThread(0); - closeImplementation(); + if (callCloseImplementation) closeImplementation(); + + if (_state.valid()) + { + decrementContextIDUsageCount(_state->getContextID()); + + _state = 0; + } } void GraphicsContext::makeCurrent() { - osg::notify(osg::NOTICE)<<"Doing GraphicsContext::makeCurrent"<<(unsigned int)OpenThreads::Thread::CurrentThread()<release(); } - osg::notify(osg::NOTICE)<<"Done GraphicsContext::makeCurrentImplementation"<<(unsigned int)OpenThreads::Thread::CurrentThread()<add(new SwapBuffersOperation); } else { - osg::notify(osg::NOTICE)<<"Doing GraphicsContext::swapBuffers() makeCurrent;swapBuffersImplementation;releaseContext"<<(unsigned int)OpenThreads::Thread::CurrentThread()<_graphicsContext = this; - if (!_graphicsThread->isRunning() && isRealized()) + if (!_graphicsThread->isRunning()) { _graphicsThread->startThread(); } diff --git a/src/osg/GraphicsThread.cpp b/src/osg/GraphicsThread.cpp index e71064136..f459307dc 100644 --- a/src/osg/GraphicsThread.cpp +++ b/src/osg/GraphicsThread.cpp @@ -25,7 +25,6 @@ struct BlockOperation : public GraphicsThread::Operation, public Block virtual void operator () (GraphicsContext*) { - //osg::notify(osg::NOTICE)<<"BlockOperation doing release"<<(unsigned int)this<isRealized()) + { + _graphicsContext->realize(); + } + + osg::notify(osg::INFO)<<"Doing make current"<makeCurrent(); } - osg::notify(osg::NOTICE)<<"Doing run"<setReadDrawable( 0 ); - _rs->makeCurrent(); + // comment out right now, as Producer's setReadDrawable() is doing a call for us. + // _rs->makeCurrent(); } void GraphicsContextImplementation::makeContextCurrentImplementation(GraphicsContext* readContext) @@ -206,8 +218,13 @@ void GraphicsContextImplementation::makeContextCurrentImplementation(GraphicsCon { _rs->setReadDrawable( readContextImplemention->getRenderSurface() ); } + else + { + _rs->setReadDrawable( 0 ); + } - _rs->makeCurrent(); + // comment out right now, as Producer's setReadDrawable() is doing a call for us. + // _rs->makeCurrent(); } void GraphicsContextImplementation::closeImplementation() diff --git a/src/osgUtil/CullVisitor.cpp b/src/osgUtil/CullVisitor.cpp index 60a32e171..46134d197 100644 --- a/src/osgUtil/CullVisitor.cpp +++ b/src/osgUtil/CullVisitor.cpp @@ -1332,9 +1332,11 @@ void CullVisitor::apply(osg::CameraNode& camera) rtts->setGraphicsContext(context.get()); - // context->createGraphicsThread(); - +#if 0 + context->createGraphicsThread(); +#else context->realize(); +#endif } }