Clean up and fixes to GraphicThread class, and osgcamera example.

This commit is contained in:
Robert Osfield 2005-08-30 14:41:08 +00:00
parent 3810d3b850
commit aba3b4fa67
7 changed files with 73 additions and 67 deletions

View File

@ -47,8 +47,6 @@ struct FrameOperation : public osg::GraphicsThread::Operation
virtual void operator () (osg::GraphicsContext* context) virtual void operator () (osg::GraphicsContext* context)
{ {
std::cout<<"FrameOperation draw begin"<<context<<std::endl;
_sceneView->setState(context->getState()); _sceneView->setState(context->getState());
_sceneView->setProjectionMatrix(_camera->getProjectionMatrix()); _sceneView->setProjectionMatrix(_camera->getProjectionMatrix());
_sceneView->setViewMatrix(_camera->getViewMatrix()); _sceneView->setViewMatrix(_camera->getViewMatrix());
@ -56,8 +54,6 @@ struct FrameOperation : public osg::GraphicsThread::Operation
_sceneView->cull(); _sceneView->cull();
_sceneView->draw(); _sceneView->draw();
std::cout<<"FrameOperation draw end"<<context<<std::endl;
} }
osg::ref_ptr<osg::CameraNode> _camera; osg::ref_ptr<osg::CameraNode> _camera;
@ -127,9 +123,6 @@ int main( int argc, char **argv )
return 1; return 1;
} }
// realise the window
gfxc->realize();
camera->setGraphicsContext(gfxc.get()); camera->setGraphicsContext(gfxc.get());
// initialize the view to look at the center of the scene graph // 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->setProjectionMatrixAsPerspective(50.0f,1.4f,1.0f,10000.0f);
camera->setViewMatrix(viewMatrix); camera->setViewMatrix(viewMatrix);
// graphics thread will realize the window.
gfxc->createGraphicsThread(); gfxc->createGraphicsThread();
cameraMap[camera] = new FrameOperation(camera.get(), frameStamp.get()); cameraMap[camera] = new FrameOperation(camera.get(), frameStamp.get());
@ -162,31 +155,23 @@ int main( int argc, char **argv )
std::cout<<"nubmer of gfx."<<graphicsContextSet.size()<<std::endl; std::cout<<"nubmer of gfx."<<graphicsContextSet.size()<<std::endl;
GraphicsContextSet::iterator gitr;
for(gitr = graphicsContextSet.begin();
gitr != graphicsContextSet.end();
++gitr)
{
std::cout<<"Issue swap."<<std::endl;
osg::GraphicsContext* context = *gitr;
context->getGraphicsThread()->add(swapOp.get(), true);
}
// record the timer tick at the start of rendering. // record the timer tick at the start of rendering.
osg::Timer_t start_tick = osg::Timer::instance()->tick(); osg::Timer_t start_tick = osg::Timer::instance()->tick();
osg::Timer_t previous_tick = start_tick;
bool done = false; 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.) // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
while( !done ) while( !done )
{ {
std::cout<<"Frame "<<frameNum<<std::endl;
frameStamp->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++); frameStamp->setFrameNumber(frameNum++);
std::cout<<"Frame rate "<<(double)frameNum / frameStamp->getReferenceTime()<<std::endl; std::cout<<"Frame rate "<<1.0/osg::Timer::instance()->delta_s(previous_tick,current_tick)<<std::endl;
previous_tick = current_tick;
loadedModel->accept(updateVisitor); loadedModel->accept(updateVisitor);
@ -200,6 +185,7 @@ int main( int argc, char **argv )
camera->getGraphicsContext()->getGraphicsThread()->add( citr->second.get(), false); camera->getGraphicsContext()->getGraphicsThread()->add( citr->second.get(), false);
} }
GraphicsContextSet::iterator gitr;
for(gitr = graphicsContextSet.begin(); for(gitr = graphicsContextSet.begin();
gitr != graphicsContextSet.end(); gitr != graphicsContextSet.end();
++gitr) ++gitr)
@ -209,24 +195,23 @@ int main( int argc, char **argv )
context->getGraphicsThread()->add(preSwapBarrierOp.get(), false); context->getGraphicsThread()->add(preSwapBarrierOp.get(), false);
} }
std::cout<<"Join frameEndBarrierOp block "<<std::endl; osg::notify(osg::INFO)<<"Join frameEndBarrierOp block "<<std::endl;
osg::Timer_t before_tick = osg::Timer::instance()->tick(); osg::Timer_t before_tick = osg::Timer::instance()->tick();
frameEndBarrierOp->block(); frameEndBarrierOp->block();
osg::Timer_t after_tick = osg::Timer::instance()->tick(); osg::Timer_t after_tick = osg::Timer::instance()->tick();
std::cout<<"Leave frameEndBarrierOp block "<<osg::Timer::instance()->delta_s(before_tick,after_tick)<<std::endl; osg::notify(osg::INFO)<<"Leave frameEndBarrierOp block "<<osg::Timer::instance()->delta_s(before_tick,after_tick)<<std::endl;
std::cout<<"Join preSwapBarrierOp block "<<std::endl; osg::notify(osg::INFO)<<"Join preSwapBarrierOp block "<<std::endl;
before_tick = osg::Timer::instance()->tick(); before_tick = osg::Timer::instance()->tick();
// preSwapBarrierOp->block();
after_tick = osg::Timer::instance()->tick(); after_tick = osg::Timer::instance()->tick();
std::cout<<"Leave preSwapBarrierOp block "<<osg::Timer::instance()->delta_s(before_tick,after_tick)<<std::endl; osg::notify(osg::INFO)<<"Leave preSwapBarrierOp block "<<osg::Timer::instance()->delta_s(before_tick,after_tick)<<std::endl;
for(gitr = graphicsContextSet.begin(); for(gitr = graphicsContextSet.begin();
gitr != graphicsContextSet.end(); gitr != graphicsContextSet.end();
++gitr) ++gitr)
{ {
osg::GraphicsContext* context = *gitr; osg::GraphicsContext* context = *gitr;
context->getGraphicsThread()->add(swapOp.get(), false); context->getGraphicsThread()->add(swapOp.get(), true);
} }
// check if any of the windows are closed // check if any of the windows are closed

View File

@ -138,8 +138,12 @@ class OSG_EXPORT GraphicsContext : public Referenced
/** Realise the GraphicsContext.*/ /** Realise the GraphicsContext.*/
bool realize(); bool realize();
/** close the graphics context.*/ /** close the graphics context.
void close(); * 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.*/ /** swap the front and back buffers.*/
void swapBuffers(); void swapBuffers();
@ -216,8 +220,6 @@ class OSG_EXPORT GraphicsContext : public Referenced
virtual ~GraphicsContext(); virtual ~GraphicsContext();
bool _closeOnDestruction;
ref_ptr<Traits> _traits; ref_ptr<Traits> _traits;
ref_ptr<State> _state; ref_ptr<State> _state;
OpenThreads::Mutex _mutex; OpenThreads::Mutex _mutex;

View File

@ -70,6 +70,7 @@ class OSGPRODUCER_EXPORT GraphicsContextImplementation : public osg::GraphicsCon
virtual ~GraphicsContextImplementation(); virtual ~GraphicsContextImplementation();
bool _closeOnDestruction;
osg::ref_ptr<Producer::RenderSurface> _rs; osg::ref_ptr<Producer::RenderSurface> _rs;
}; };

View File

@ -113,25 +113,13 @@ void GraphicsContext::decrementContextIDUsageCount(unsigned int contextID)
GraphicsContext::GraphicsContext(): GraphicsContext::GraphicsContext():
_closeOnDestruction(true),
_threadOfLastMakeCurrent(0) _threadOfLastMakeCurrent(0)
{ {
} }
GraphicsContext::~GraphicsContext() GraphicsContext::~GraphicsContext()
{ {
// switch off the graphics thread... close(false);
setGraphicsThread(0);
if (_state.valid())
{
decrementContextIDUsageCount(_state->getContextID());
}
if (_closeOnDestruction)
{
close();
}
} }
/** Realise the GraphicsContext.*/ /** Realise the GraphicsContext.*/
@ -151,18 +139,25 @@ bool GraphicsContext::realize()
} }
} }
void GraphicsContext::close() void GraphicsContext::close(bool callCloseImplementation)
{ {
// switch off the graphics thread... // switch off the graphics thread...
setGraphicsThread(0); setGraphicsThread(0);
closeImplementation(); if (callCloseImplementation) closeImplementation();
if (_state.valid())
{
decrementContextIDUsageCount(_state->getContextID());
_state = 0;
}
} }
void GraphicsContext::makeCurrent() void GraphicsContext::makeCurrent()
{ {
osg::notify(osg::NOTICE)<<"Doing GraphicsContext::makeCurrent"<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl; osg::notify(osg::INFO)<<"Doing GraphicsContext::makeCurrent"<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl;
ReleaseContext_Block_MakeCurrentOperation* rcbmco = 0; ReleaseContext_Block_MakeCurrentOperation* rcbmco = 0;
@ -177,7 +172,7 @@ void GraphicsContext::makeCurrent()
if (!isCurrent()) _mutex.lock(); if (!isCurrent()) _mutex.lock();
osg::notify(osg::NOTICE)<<"Calling GraphicsContext::makeCurrentImplementation"<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl; osg::notify(osg::INFO)<<"Calling GraphicsContext::makeCurrentImplementation"<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl;
makeCurrentImplementation(); makeCurrentImplementation();
@ -190,7 +185,7 @@ void GraphicsContext::makeCurrent()
rcbmco->release(); rcbmco->release();
} }
osg::notify(osg::NOTICE)<<"Done GraphicsContext::makeCurrentImplementation"<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl; osg::notify(osg::INFO)<<"Done GraphicsContext::makeCurrentImplementation"<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl;
} }
void GraphicsContext::makeContextCurrent(GraphicsContext* readContext) void GraphicsContext::makeContextCurrent(GraphicsContext* readContext)
@ -211,18 +206,18 @@ void GraphicsContext::swapBuffers()
{ {
if (isCurrent()) if (isCurrent())
{ {
osg::notify(osg::NOTICE)<<"Doing GraphicsContext::swapBuffers() call to swapBuffersImplementation() "<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl; osg::notify(osg::INFO)<<"Doing GraphicsContext::swapBuffers() call to swapBuffersImplementation() "<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl;
swapBuffersImplementation(); swapBuffersImplementation();
} }
else if (_graphicsThread.valid() && else if (_graphicsThread.valid() &&
_threadOfLastMakeCurrent == _graphicsThread.get()) _threadOfLastMakeCurrent == _graphicsThread.get())
{ {
osg::notify(osg::NOTICE)<<"Doing GraphicsContext::swapBuffers() registering SwapBuffersOperation"<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl; osg::notify(osg::INFO)<<"Doing GraphicsContext::swapBuffers() registering SwapBuffersOperation"<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl;
_graphicsThread->add(new SwapBuffersOperation); _graphicsThread->add(new SwapBuffersOperation);
} }
else else
{ {
osg::notify(osg::NOTICE)<<"Doing GraphicsContext::swapBuffers() makeCurrent;swapBuffersImplementation;releaseContext"<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl; osg::notify(osg::INFO)<<"Doing GraphicsContext::swapBuffers() makeCurrent;swapBuffersImplementation;releaseContext"<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl;
makeCurrent(); makeCurrent();
swapBuffersImplementation(); swapBuffersImplementation();
releaseContext(); releaseContext();
@ -256,7 +251,7 @@ void GraphicsContext::setGraphicsThread(GraphicsThread* gt)
{ {
_graphicsThread->_graphicsContext = this; _graphicsThread->_graphicsContext = this;
if (!_graphicsThread->isRunning() && isRealized()) if (!_graphicsThread->isRunning())
{ {
_graphicsThread->startThread(); _graphicsThread->startThread();
} }

View File

@ -25,7 +25,6 @@ struct BlockOperation : public GraphicsThread::Operation, public Block
virtual void operator () (GraphicsContext*) virtual void operator () (GraphicsContext*)
{ {
//osg::notify(osg::NOTICE)<<"BlockOperation doing release"<<(unsigned int)this<<std::endl;
glFlush(); glFlush();
release(); release();
} }
@ -41,14 +40,14 @@ GraphicsThread::GraphicsThread():
GraphicsThread::~GraphicsThread() GraphicsThread::~GraphicsThread()
{ {
osg::notify(osg::NOTICE)<<"Destructing graphics thread"<<std::endl; osg::notify(osg::INFO)<<"Destructing graphics thread"<<std::endl;
cancel(); cancel();
} }
int GraphicsThread::cancel() int GraphicsThread::cancel()
{ {
osg::notify(osg::NOTICE)<<"Cancelling graphics thread"<<std::endl; osg::notify(osg::INFO)<<"Cancelling graphics thread"<<std::endl;
int result = 0; int result = 0;
if( isRunning() ) if( isRunning() )
@ -68,7 +67,7 @@ int GraphicsThread::cancel()
{ {
// commenting out debug info as it was cashing crash on exit, presumable // commenting out debug info as it was cashing crash on exit, presumable
// due to osg::notify or std::cout destructing earlier than this destructor. // due to osg::notify or std::cout destructing earlier than this destructor.
osg::notify(osg::NOTICE)<<"Waiting for GraphicsThread to cancel"<<std::endl; osg::notify(osg::INFO)<<"Waiting for GraphicsThread to cancel"<<std::endl;
OpenThreads::Thread::YieldCurrentThread(); OpenThreads::Thread::YieldCurrentThread();
} }
} }
@ -78,7 +77,7 @@ int GraphicsThread::cancel()
void GraphicsThread::add(Operation* operation, bool waitForCompletion) void GraphicsThread::add(Operation* operation, bool waitForCompletion)
{ {
osg::notify(osg::NOTICE)<<"Doing add"<<std::endl; osg::notify(osg::INFO)<<"Doing add"<<std::endl;
BlockOperation* block = 0; BlockOperation* block = 0;
@ -110,11 +109,16 @@ void GraphicsThread::run()
// make the graphics context current. // make the graphics context current.
if (_graphicsContext) if (_graphicsContext)
{ {
osg::notify(osg::NOTICE)<<"Doing make current"<<std::endl; if (!_graphicsContext->isRealized())
{
_graphicsContext->realize();
}
osg::notify(osg::INFO)<<"Doing make current"<<std::endl;
_graphicsContext->makeCurrent(); _graphicsContext->makeCurrent();
} }
osg::notify(osg::NOTICE)<<"Doing run"<<std::endl; osg::notify(osg::INFO)<<"Doing run"<<std::endl;
bool firstTime = true; bool firstTime = true;

View File

@ -161,6 +161,7 @@ GraphicsContextImplementation::GraphicsContextImplementation(Producer::RenderSur
GraphicsContextImplementation::~GraphicsContextImplementation() GraphicsContextImplementation::~GraphicsContextImplementation()
{ {
if (_closeOnDestruction) close();
} }
bool GraphicsContextImplementation::realizeImplementation() bool GraphicsContextImplementation::realizeImplementation()
@ -187,13 +188,24 @@ bool GraphicsContextImplementation::realizeImplementation()
void GraphicsContextImplementation::makeCurrentImplementation() void GraphicsContextImplementation::makeCurrentImplementation()
{ {
if (!_rs) return; if (!_rs)
{
osg::notify(osg::NOTICE)<<"Error: GraphicsContextImplementation::makeCurrentImplementation() no RenderSurface."<<std::endl;
return;
}
osg::notify(osg::NOTICE)<<"GraphicsContextImplementation::makeCurrentImplementation()"<<std::endl; if (!isRealized())
{
osg::notify(osg::NOTICE)<<"Error: GraphicsContextImplementation::makeCurrentImplementation() not Realized."<<std::endl;
return;
}
osg::notify(osg::INFO)<<"GraphicsContextImplementation::makeCurrentImplementation()"<<std::endl;
_rs->setReadDrawable( 0 ); _rs->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) void GraphicsContextImplementation::makeContextCurrentImplementation(GraphicsContext* readContext)
@ -206,8 +218,13 @@ void GraphicsContextImplementation::makeContextCurrentImplementation(GraphicsCon
{ {
_rs->setReadDrawable( readContextImplemention->getRenderSurface() ); _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() void GraphicsContextImplementation::closeImplementation()

View File

@ -1332,9 +1332,11 @@ void CullVisitor::apply(osg::CameraNode& camera)
rtts->setGraphicsContext(context.get()); rtts->setGraphicsContext(context.get());
// context->createGraphicsThread(); #if 0
context->createGraphicsThread();
#else
context->realize(); context->realize();
#endif
} }
} }