From 5850b1ced0bd1835ebed64fd1a88b2d8235ab95b Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Fri, 10 Jun 2016 17:06:54 +0200 Subject: [PATCH] GL_CONTEXT_MANAGER: Call SetCurrent() only when a different context is used. --- common/gl_context_mgr.cpp | 18 +++++++++++------- include/gl_context_mgr.h | 3 +++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/common/gl_context_mgr.cpp b/common/gl_context_mgr.cpp index 850158dbcf..4b048cc607 100644 --- a/common/gl_context_mgr.cpp +++ b/common/gl_context_mgr.cpp @@ -44,18 +44,22 @@ wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLConte void GL_CONTEXT_MANAGER::DestroyCtx( wxGLContext* aContext ) { - assert( m_glCtx != aContext ); - - if( m_glContexts.count( aContext ) && m_glCtx != aContext ) + if( m_glContexts.count( aContext ) ) { m_glContexts.erase( aContext ); delete aContext; } else { - // Do not delete currently used or unknown GL contexts + // Do not delete unknown GL contexts 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(); wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext ); - if( aContext != m_glCtx ) + if( aContext != m_glCtx || canvas != m_glCanvas ) { canvas->SetCurrent( *aContext ); + m_glCanvas = canvas; m_glCtx = aContext; } } @@ -90,7 +95,6 @@ void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext ) if( m_glCtx == aContext ) { m_glCtxMutex.unlock(); - m_glCtx = NULL; } else { @@ -100,7 +104,7 @@ void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext ) GL_CONTEXT_MANAGER::GL_CONTEXT_MANAGER() - : m_glCtx( NULL ) + : m_glCtx( NULL ), m_glCanvas( NULL ) { } diff --git a/include/gl_context_mgr.h b/include/gl_context_mgr.h index de5f7d0bb4..de45cfab62 100644 --- a/include/gl_context_mgr.h +++ b/include/gl_context_mgr.h @@ -88,6 +88,9 @@ private: ///> Currently bound GL context. wxGLContext* m_glCtx; + ///> The canvas that uses the current GL context. + wxGLCanvas* m_glCanvas; + ///> Lock to prevent unexpected GL context switching. MUTEX m_glCtxMutex;