Changed the return types of makeCurrent to bool, and added a bool GraphicsContext::releaseContext method

along with implementations in osgViewer.
This commit is contained in:
Robert Osfield
2007-01-08 19:29:59 +00:00
parent 4a5eda6522
commit 16d1c00a3d
18 changed files with 221 additions and 126 deletions

View File

@@ -171,49 +171,41 @@ bool GraphicsContext::realize()
void GraphicsContext::close(bool callCloseImplementation)
{
osg::notify(osg::NOTICE)<<"close("<<callCloseImplementation<<")"<<this<<std::endl;
osg::notify(osg::INFO)<<"close("<<callCloseImplementation<<")"<<this<<std::endl;
// switch off the graphics thread...
setGraphicsThread(0);
bool sharedContextExists = false;
if (_state.valid())
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
if (s_contextIDMap[_state->getContextID()]>1) sharedContextExists = true;
}
// release all the OpenGL objects in the scene graphs associted with this
for(Cameras::iterator itr = _cameras.begin();
itr != _cameras.end();
++itr)
{
Camera* camera = (*itr);
if (camera)
{
osg::notify(osg::INFO)<<"Releasing GL objects for Camera="<<camera<<" _state="<<_state.get()<<std::endl;
camera->releaseGLObjects(_state.get());
}
}
if (callCloseImplementation && _state.valid() && isRealized())
{
osg::notify(osg::INFO)<<"Closing still viable window "<<sharedContextExists<<" _state->getContextID()="<<_state->getContextID()<<std::endl;
bool sharedContextExists = false;
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
if (s_contextIDMap[_state->getContextID()]>1) sharedContextExists = true;
}
osg::notify(osg::NOTICE)<<"Closing still viable window "<<sharedContextExists<<" _state->getContextID()="<<_state->getContextID()<<std::endl;
#if 1
// release all the OpenGL objects in the scene graphs associted with this
for(Cameras::iterator itr = _cameras.begin();
itr != _cameras.end();
++itr)
{
Camera* camera = (*itr);
if (camera)
{
osg::notify(osg::NOTICE)<<"Releasing GL objects for Camera "<<camera<<std::endl;
#if 1
camera->releaseGLObjects(_state.get());
#else
camera->releaseGLObjects(0);
#endif
}
}
#endif
#if 1
osg::notify(osg::NOTICE)<<"Doing makeCurrent "<<std::endl;
makeCurrent();
osg::notify(osg::NOTICE)<<"Doing Flush"<<std::endl;
osg::notify(osg::INFO)<<"Doing Flush"<<std::endl;
// flush all the OpenGL object buffer for this context.
double availableTime = 100.0f;
@@ -229,8 +221,8 @@ void GraphicsContext::close(bool callCloseImplementation)
osg::Program::flushDeletedGlPrograms(_state->getContextID(),currentTime,availableTime);
osg::Shader::flushDeletedGlShaders(_state->getContextID(),currentTime,availableTime);
osg::notify(osg::NOTICE)<<"Done Flush "<<availableTime<<std::endl;
#endif
osg::notify(osg::INFO)<<"Done Flush "<<availableTime<<std::endl;
_state->reset();
releaseContext();
@@ -247,46 +239,37 @@ void GraphicsContext::close(bool callCloseImplementation)
}
void GraphicsContext::makeCurrent()
bool GraphicsContext::makeCurrent()
{
ReleaseContext_Block_MakeCurrentOperation* rcbmco = 0;
if (_graphicsThread.valid() &&
_threadOfLastMakeCurrent == _graphicsThread.get() &&
_threadOfLastMakeCurrent != OpenThreads::Thread::CurrentThread())
{
// create a relase contex, block and make current operation to stop the graphics thread while we use the graphics context for ourselves
rcbmco = new ReleaseContext_Block_MakeCurrentOperation;
_graphicsThread->add(rcbmco);
}
if (!isCurrent()) _mutex.lock();
makeCurrentImplementation();
bool result = makeCurrentImplementation();
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThread();
if (rcbmco)
if (result)
{
// Let the "relase contex, block and make current operation" proceed, which will now move on to trying to aquire the graphics
// contex itself with a makeCurrent(), this will then block on the GraphicsContext mutex till releaseContext() releases it.
rcbmco->release();
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThread();
}
return result;
}
void GraphicsContext::makeContextCurrent(GraphicsContext* readContext)
bool GraphicsContext::makeContextCurrent(GraphicsContext* readContext)
{
if (!isCurrent()) _mutex.lock();
bool result = makeContextCurrentImplementation(readContext);
makeContextCurrentImplementation(readContext);
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThread();
if (result)
{
_threadOfLastMakeCurrent = OpenThreads::Thread::CurrentThread();
}
return result;
}
void GraphicsContext::releaseContext()
bool GraphicsContext::releaseContext()
{
_mutex.unlock();
_threadOfLastMakeCurrent = reinterpret_cast<OpenThreads::Thread*>(0xffff);
bool result = releaseContextImplementation();
_threadOfLastMakeCurrent = (OpenThreads::Thread*)(-1);
return result;
}
void GraphicsContext::swapBuffers()
@@ -306,7 +289,6 @@ void GraphicsContext::swapBuffers()
makeCurrent();
swapBuffersImplementation();
clear();
releaseContext();
}
}

View File

@@ -218,13 +218,14 @@ void TextureObjectManager::flushAllTextureObjects(unsigned int contextID)
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
Texture::TextureObjectList& tol = _textureObjectListMap[contextID];
osg::notify(osg::NOTICE)<<"Flushing texture objects num="<<tol.size()<<" contextID="<<contextID<<std::endl;
// osg::notify(osg::INFO)<<"Flushing texture objects num="<<tol.size()<<" contextID="<<contextID<<std::endl;
for(Texture::TextureObjectList::iterator itr=tol.begin();
itr!=tol.end();
++itr)
{
osg::notify(osg::NOTICE)<<" deleting texture object "<<(*itr)->_id<<std::endl;
// osg::notify(osg::NOTICE)<<" deleting texture object "<<(*itr)->_id<<std::endl;
glDeleteTextures( 1L, &((*itr)->_id));
}
tol.clear();
@@ -1304,8 +1305,8 @@ void Texture::resizeGLObjectBuffers(unsigned int maxSize)
void Texture::releaseGLObjects(State* state) const
{
if (state) osg::notify(osg::NOTICE)<<"Texture::releaseGLObjects contextID="<<state->getContextID()<<std::endl;
else osg::notify(osg::NOTICE)<<"Texture::releaseGLObjects no State "<<std::endl;
// if (state) osg::notify(osg::NOTICE)<<"Texture::releaseGLObjects contextID="<<state->getContextID()<<std::endl;
// else osg::notify(osg::NOTICE)<<"Texture::releaseGLObjects no State "<<std::endl;
if (!state) const_cast<Texture*>(this)->dirtyTextureObject();
else

View File

@@ -26,7 +26,7 @@ View::View()
View::~View()
{
osg::notify(osg::NOTICE)<<"Destructing osg::View"<<std::endl;
osg::notify(osg::INFO)<<"Destructing osg::View"<<std::endl;
if (_camera.valid())
{
@@ -44,7 +44,7 @@ View::~View()
cd._camera->setCullCallback(0);
}
osg::notify(osg::NOTICE)<<"Done destructing osg::View"<<std::endl;
osg::notify(osg::INFO)<<"Done destructing osg::View"<<std::endl;
}

View File

@@ -325,6 +325,25 @@ void EventQueue::keyRelease(int key, double time)
addEvent(event);
}
void EventQueue::closeWindow(double time)
{
GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
event->setEventType(GUIEventAdapter::CLOSE_WINDOW);
event->setTime(time);
addEvent(event);
}
void EventQueue::quitApplication(double time)
{
GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
event->setEventType(GUIEventAdapter::QUIT_APPLICATION);
event->setTime(time);
addEvent(event);
}
void EventQueue::frame(double time)
{
GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);

View File

@@ -218,18 +218,18 @@ bool GraphicsContextImplementation::realizeImplementation()
}
}
void GraphicsContextImplementation::makeCurrentImplementation()
bool GraphicsContextImplementation::makeCurrentImplementation()
{
if (!_rs)
{
osg::notify(osg::NOTICE)<<"Error: GraphicsContextImplementation::makeCurrentImplementation() no RenderSurface."<<std::endl;
return;
return false;
}
if (!isRealized())
{
osg::notify(osg::NOTICE)<<"Error: GraphicsContextImplementation::makeCurrentImplementation() not Realized."<<std::endl;
return;
return false;
}
// osg::notify(osg::INFO)<<"GraphicsContextImplementation::makeCurrentImplementation()"<<std::endl;
@@ -238,11 +238,13 @@ void GraphicsContextImplementation::makeCurrentImplementation()
// comment out right now, as Producer's setReadDrawable() is doing a call for us.
// _rs->makeCurrent();
return true;
}
void GraphicsContextImplementation::makeContextCurrentImplementation(osg::GraphicsContext* readContext)
bool GraphicsContextImplementation::makeContextCurrentImplementation(osg::GraphicsContext* readContext)
{
if (!_rs) return;
if (!_rs) return false;
GraphicsContextImplementation* readContextImplemention = dynamic_cast<GraphicsContextImplementation*>(readContext);
@@ -257,6 +259,8 @@ void GraphicsContextImplementation::makeContextCurrentImplementation(osg::Graphi
// comment out right now, as Producer's setReadDrawable() is doing a call for us.
// _rs->makeCurrent();
return true;
}
void GraphicsContextImplementation::closeImplementation()

View File

@@ -46,11 +46,14 @@ class GraphicsContextCocoa : public osg::GraphicsContext
/** Make this graphics context current implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual void makeCurrentImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeCurrentImplementation() not implemented."<<std::endl; }
virtual bool makeCurrentImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeCurrentImplementation() not implemented."<<std::endl; return false;}
/** Make this graphics context current with specified read context implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual void makeContextCurrentImplementation(GraphicsContext* /*readContext*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeContextCurrentImplementation(..) not implemented."<<std::endl; }
virtual bool makeContextCurrentImplementation(GraphicsContext* /*readContext*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeContextCurrentImplementation(..) not implemented."<<std::endl; return false; }
/** Release the graphics context.*/
virtual bool releaseContextImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::releaseContextImplementation(..) not implemented."<<std::endl; return false; }
/** Pure virtual, Bind the graphics context to associated texture implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
@@ -84,9 +87,16 @@ bool GraphicsWindowCocoa::realizeImplementation()
return false;
}
void GraphicsWindowCocoa::makeCurrentImplementation()
bool GraphicsWindowCocoa::makeCurrentImplementation()
{
osg::notify(osg::NOTICE)<<"GraphicWindowCocoa::makeCurrentImplementation() Please implement me!"<<std::endl;
return false;
}
bool GraphicsWindowCocoa::releaseContextImplementation()
{
osg::notify(osg::NOTICE)<<"GraphicWindowCocoa::releaseContextImplementation() Please implement me!"<<std::endl;
return false;
}
void GraphicsWindowCocoa::closeImplementation()

View File

@@ -46,11 +46,14 @@ class GraphicsContextWin32 : public osg::GraphicsContext
/** Make this graphics context current implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual void makeCurrentImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeCurrentImplementation() not implemented."<<std::endl; }
virtual bool makeCurrentImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeCurrentImplementation() not implemented."<<std::endl; return false; }
/** Make this graphics context current with specified read context implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual void makeContextCurrentImplementation(GraphicsContext* /*readContext*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeContextCurrentImplementation(..) not implemented."<<std::endl; }
virtual bool makeContextCurrentImplementation(GraphicsContext* /*readContext*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeContextCurrentImplementation(..) not implemented."<<std::endl; return false;}
/** Release the graphics context.*/
virtual bool releaseContextImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::releaseContextImplementation(..) not implemented."<<std::endl; return false; }
/** Pure virtual, Bind the graphics context to associated texture implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
@@ -84,9 +87,17 @@ bool GraphicsWindowWin32::realizeImplementation()
return false;
}
void GraphicsWindowWin32::makeCurrentImplementation()
bool GraphicsWindowWin32::makeCurrentImplementation()
{
osg::notify(osg::NOTICE)<<"GraphicWindowWin32::makeCurrentImplementation() Please implement me!"<<std::endl;
return false;
}
bool GraphicWindowWin32::releaseContextImplementation()
{
osg::notify(osg::NOTICE)<<"GraphicWindowWin32::releaseContextImplementation() Please implement me!"<<std::endl;
return false;
}
void GraphicsWindowWin32::closeImplementation()

View File

@@ -63,11 +63,14 @@ class GraphicsContextX11 : public osg::GraphicsContext
/** Make this graphics context current implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual void makeCurrentImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeCurrentImplementation() not implemented."<<std::endl; }
virtual bool makeCurrentImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeCurrentImplementation() not implemented."<<std::endl; return false;}
/** Make this graphics context current with specified read context implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
virtual void makeContextCurrentImplementation(GraphicsContext* /*readContext*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeContextCurrentImplementation(..) not implemented."<<std::endl; }
virtual bool makeContextCurrentImplementation(GraphicsContext* /*readContext*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeContextCurrentImplementation(..) not implemented."<<std::endl; return false; }
/** Release the graphics context.*/
virtual bool releaseContextImplementation() { osg::notify(osg::NOTICE)<<"GraphicsWindow::releaseContextImplementation(..) not implemented."<<std::endl; return false; }
/** Pure virtual, Bind the graphics context to associated texture implementation.
* Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
@@ -533,38 +536,33 @@ bool GraphicsWindowX11::realizeImplementation()
return true;
}
void GraphicsWindowX11::makeCurrentImplementation()
bool GraphicsWindowX11::makeCurrentImplementation()
{
if (!_realized)
{
osg::notify(osg::NOTICE)<<"Warning: GraphicsWindow not realized, cannot do makeCurrent."<<std::endl;
return;
return false;
}
// osg::notify(osg::NOTICE)<<"makeCurrentImplementation "<<this<<" "<<OpenThreads::Thread::CurrentThread()<<std::endl;
// osg::notify(osg::NOTICE)<<" glXMakeCurrent ("<<_display<<","<<_window<<","<<_glxContext<<std::endl;
if (glXMakeCurrent( _display, _window, _glxContext ))
return glXMakeCurrent( _display, _window, _glxContext )==True;
}
bool GraphicsWindowX11::releaseContextImplementation()
{
if (!_realized)
{
// osg::notify(osg::NOTICE)<<"makeCurrentImplementation sucessful"<<std::endl;
osg::notify(osg::NOTICE)<<"Warning: GraphicsWindow not realized, cannot do makeCurrent."<<std::endl;
return false;
}
else
{
osg::notify(osg::NOTICE)<<"makeCurrentImplementation failed"<<std::endl;
while(!glXMakeCurrent( _display, _window, _glxContext ))
{
OpenThreads::Thread::YieldCurrentThread();
osg::notify(osg::NOTICE)<<" makeCurrentImplementation failed again."<<std::endl;
}
// osg::notify(osg::NOTICE)<<" Succeeded at last."<<std::endl;
}
// osg::notify(osg::NOTICE)<<"makeCurrentImplementation "<<this<<" "<<OpenThreads::Thread::CurrentThread()<<std::endl;
// osg::notify(osg::NOTICE)<<" glXMakeCurrent ("<<_display<<","<<_window<<","<<_glxContext<<std::endl;
return glXMakeCurrent( _display, None, NULL )==True;
}
@@ -640,6 +638,7 @@ void GraphicsWindowX11::checkEvents()
{
osg::notify(osg::INFO)<<"DeleteWindow event recieved"<<std::endl;
destroyWindowRequested = true;
getEventQueue()->closeWindow(eventTime);
}
}
case Expose :
@@ -859,12 +858,13 @@ void GraphicsWindowX11::checkEvents()
resized(windowX, windowY, windowWidth, windowHeight);
getEventQueue()->windowResize(windowX, windowY, windowWidth, windowHeight, resizeTime);
}
#if 0
if (destroyWindowRequested)
{
close();
}
#endif
}
void GraphicsWindowX11::grabFocus()

View File

@@ -605,7 +605,8 @@ void Viewer::eventTraversal()
osgGA::EventQueue::Events gw_events;
gw->getEventQueue()->takeEvents(gw_events);
for(osgGA::EventQueue::Events::iterator itr = gw_events.begin();
osgGA::EventQueue::Events::iterator itr;
for(itr = gw_events.begin();
itr != gw_events.end();
++itr)
{
@@ -692,6 +693,32 @@ void Viewer::eventTraversal()
// osg::notify(osg::NOTICE)<<" mouse Xmin = "<<event->getXmin()<<" Ymin="<<event->getYmin()<<" xMax="<<event->getXmax()<<" Ymax="<<event->getYmax()<<std::endl;
}
for(itr = gw_events.begin();
itr != gw_events.end();
++itr)
{
osgGA::GUIEventAdapter* event = itr->get();
switch(event->getEventType())
{
case(osgGA::GUIEventAdapter::CLOSE_WINDOW):
{
stopThreading();
gw->close();
// setThreadingModel(ThreadPerCamera);
setThreadingModel(SingleThreaded);
startThreading();
break;
}
default:
break;
}
}
events.insert(events.end(), gw_events.begin(), gw_events.end());
}
@@ -792,6 +819,9 @@ void Viewer::eventTraversal()
{
case(osgGA::GUIEventAdapter::KEYUP):
if (event->getKey()==_keySetsDone) _done = true;
else if (event->getKey()=='s') { setThreadingModel(SingleThreaded); }
else if (event->getKey()=='c') { setThreadingModel(ThreadPerCamera); }
else if (event->getKey()=='w') { setThreadingModel(ThreadPerContext); }
break;
default:
break;