KiCad PCB EDA Suite
gl_context_mgr.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016 CERN
5  * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <gl_context_mgr.h>
27 
29 {
30  static GL_CONTEXT_MANAGER instance;
31 
32  return instance;
33 }
34 
35 wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther )
36 {
37  wxGLContext* context = new wxGLContext( aCanvas, aOther );
38  assert( context /* && context->IsOK() */ ); // IsOK() is available in wx3.1+
39  assert( m_glContexts.count( context ) == 0 );
40  m_glContexts.insert( std::make_pair( context, aCanvas ) );
41 
42  return context;
43 }
44 
45 
46 void GL_CONTEXT_MANAGER::DestroyCtx( wxGLContext* aContext )
47 {
48  if( m_glContexts.count( aContext ) )
49  {
50  m_glContexts.erase( aContext );
51  delete aContext;
52  }
53  else
54  {
55  // Do not delete unknown GL contexts
56  assert( false );
57  }
58 
59  if( m_glCtx == aContext )
60  {
61  m_glCtx = NULL;
62  }
63 }
64 
65 
67 {
68  for( auto& ctx : m_glContexts )
69  delete ctx.first;
70 
71  m_glContexts.clear();
72 }
73 
74 
75 void GL_CONTEXT_MANAGER::LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas )
76 {
77  assert( aCanvas || m_glContexts.count( aContext ) > 0 );
78 
79  m_glCtxMutex.lock();
80  wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext );
81 
82  // Prevent assertion failure in wxGLContext::SetCurrent during GAL teardown
83 #ifdef __WXGTK__
84  if( canvas->GetXWindow() )
85 #endif
86  {
87  canvas->SetCurrent( *aContext );
88  }
89 
90  m_glCtx = aContext;
91 }
92 
93 
94 void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext )
95 {
96  assert( m_glContexts.count( aContext ) > 0 );
97 
98  if( m_glCtx == aContext )
99  {
100  m_glCtxMutex.unlock();
101  m_glCtx = NULL;
102  }
103  else
104  {
105  wxLogDebug(
106  "Trying to unlock GL context mutex from a wrong context: aContext %p m_glCtx %p",
107  aContext, m_glCtx );
108  }
109 }
110 
111 
113  : m_glCtx( NULL )
114 {
115 }
116 
void DeleteAll()
Function DeleteAll destroys all managed OpenGL contexts.
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
Map of GL contexts & their parent canvases.
MUTEX m_glCtxMutex
Lock to prevent unexpected GL context switching.
wxGLContext * m_glCtx
Currently bound GL context.
void UnlockCtx(wxGLContext *aContext)
Function UnlockCtx allows other canvases to bind an OpenGL context.
static GL_CONTEXT_MANAGER & Get()
Function Get returns the GL_CONTEXT_MANAGER instance (singleton).
void LockCtx(wxGLContext *aContext, wxGLCanvas *aCanvas)
Function LockCtx sets a context as current and prevents other canvases from switching it...
void DestroyCtx(wxGLContext *aContext)
Function DestroyCtx destroys a managed OpenGL context.
wxGLContext * CreateCtx(wxGLCanvas *aCanvas, const wxGLContext *aOther=NULL)
Function CreateCtx creates a managed OpenGL context.