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)
{
std::cout<<"FrameOperation draw begin"<<context<<std::endl;
_sceneView->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"<<context<<std::endl;
}
osg::ref_ptr<osg::CameraNode> _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."<<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.
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 "<<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++);
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);
@ -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 "<<std::endl;
osg::notify(osg::INFO)<<"Join frameEndBarrierOp block "<<std::endl;
osg::Timer_t before_tick = osg::Timer::instance()->tick();
frameEndBarrierOp->block();
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();
// preSwapBarrierOp->block();
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();
gitr != graphicsContextSet.end();
++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

View File

@ -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> _traits;
ref_ptr<State> _state;
OpenThreads::Mutex _mutex;

View File

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

View File

@ -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()<<std::endl;
osg::notify(osg::INFO)<<"Doing GraphicsContext::makeCurrent"<<(unsigned int)OpenThreads::Thread::CurrentThread()<<std::endl;
ReleaseContext_Block_MakeCurrentOperation* rcbmco = 0;
@ -177,7 +172,7 @@ void GraphicsContext::makeCurrent()
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();
@ -190,7 +185,7 @@ void GraphicsContext::makeCurrent()
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)
@ -211,18 +206,18 @@ void GraphicsContext::swapBuffers()
{
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();
}
else if (_graphicsThread.valid() &&
_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);
}
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();
swapBuffersImplementation();
releaseContext();
@ -256,7 +251,7 @@ void GraphicsContext::setGraphicsThread(GraphicsThread* gt)
{
_graphicsThread->_graphicsContext = this;
if (!_graphicsThread->isRunning() && isRealized())
if (!_graphicsThread->isRunning())
{
_graphicsThread->startThread();
}

View File

@ -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<<std::endl;
glFlush();
release();
}
@ -41,14 +40,14 @@ GraphicsThread::GraphicsThread():
GraphicsThread::~GraphicsThread()
{
osg::notify(osg::NOTICE)<<"Destructing graphics thread"<<std::endl;
osg::notify(osg::INFO)<<"Destructing graphics thread"<<std::endl;
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;
if( isRunning() )
@ -68,7 +67,7 @@ int GraphicsThread::cancel()
{
// commenting out debug info as it was cashing crash on exit, presumable
// 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();
}
}
@ -78,7 +77,7 @@ int GraphicsThread::cancel()
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;
@ -110,11 +109,16 @@ void GraphicsThread::run()
// make the graphics context current.
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();
}
osg::notify(osg::NOTICE)<<"Doing run"<<std::endl;
osg::notify(osg::INFO)<<"Doing run"<<std::endl;
bool firstTime = true;

View File

@ -161,6 +161,7 @@ GraphicsContextImplementation::GraphicsContextImplementation(Producer::RenderSur
GraphicsContextImplementation::~GraphicsContextImplementation()
{
if (_closeOnDestruction) close();
}
bool GraphicsContextImplementation::realizeImplementation()
@ -187,13 +188,24 @@ bool GraphicsContextImplementation::realizeImplementation()
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->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()

View File

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