GL_CONTEXT_MANAGER: Call SetCurrent() only when a different context is used.

This commit is contained in:
Maciej Suminski 2016-06-10 17:06:54 +02:00
parent 5ce4abd0ac
commit 5850b1ced0
2 changed files with 14 additions and 7 deletions

View File

@ -44,18 +44,22 @@ wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLConte
void GL_CONTEXT_MANAGER::DestroyCtx( wxGLContext* aContext ) void GL_CONTEXT_MANAGER::DestroyCtx( wxGLContext* aContext )
{ {
assert( m_glCtx != aContext ); if( m_glContexts.count( aContext ) )
if( m_glContexts.count( aContext ) && m_glCtx != aContext )
{ {
m_glContexts.erase( aContext ); m_glContexts.erase( aContext );
delete aContext; delete aContext;
} }
else else
{ {
// Do not delete currently used or unknown GL contexts // Do not delete unknown GL contexts
assert( false ); assert( false );
} }
if( m_glCtx == aContext )
{
m_glCtx = NULL;
m_glCanvas = NULL;
}
} }
@ -75,9 +79,10 @@ void GL_CONTEXT_MANAGER::LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas )
m_glCtxMutex.lock(); m_glCtxMutex.lock();
wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext ); wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext );
if( aContext != m_glCtx ) if( aContext != m_glCtx || canvas != m_glCanvas )
{ {
canvas->SetCurrent( *aContext ); canvas->SetCurrent( *aContext );
m_glCanvas = canvas;
m_glCtx = aContext; m_glCtx = aContext;
} }
} }
@ -90,7 +95,6 @@ void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext )
if( m_glCtx == aContext ) if( m_glCtx == aContext )
{ {
m_glCtxMutex.unlock(); m_glCtxMutex.unlock();
m_glCtx = NULL;
} }
else else
{ {
@ -100,7 +104,7 @@ void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext )
GL_CONTEXT_MANAGER::GL_CONTEXT_MANAGER() GL_CONTEXT_MANAGER::GL_CONTEXT_MANAGER()
: m_glCtx( NULL ) : m_glCtx( NULL ), m_glCanvas( NULL )
{ {
} }

View File

@ -88,6 +88,9 @@ private:
///> Currently bound GL context. ///> Currently bound GL context.
wxGLContext* m_glCtx; wxGLContext* m_glCtx;
///> The canvas that uses the current GL context.
wxGLCanvas* m_glCanvas;
///> Lock to prevent unexpected GL context switching. ///> Lock to prevent unexpected GL context switching.
MUTEX m_glCtxMutex; MUTEX m_glCtxMutex;