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 #include <wx/debug.h>
28 
30 {
31  static GL_CONTEXT_MANAGER instance;
32 
33  return instance;
34 }
35 
36 wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther )
37 {
38  wxGLContext* context = new wxGLContext( aCanvas, aOther );
39  wxCHECK( context, nullptr );
40 
41 #if wxCHECK_VERSION( 3, 1, 0 )
42  if( !context->IsOK() )
43  {
44  delete context;
45  return nullptr;
46  }
47 #endif /* wxCHECK_VERSION( 3, 1, 0 ) */
48 
49  m_glContexts.insert( std::make_pair( context, aCanvas ) );
50 
51  return context;
52 }
53 
54 
55 void GL_CONTEXT_MANAGER::DestroyCtx( wxGLContext* aContext )
56 {
57  if( m_glContexts.count( aContext ) )
58  {
59  m_glContexts.erase( aContext );
60  delete aContext;
61  }
62  else
63  {
64  // Do not delete unknown GL contexts
65  wxFAIL;
66  }
67 
68  if( m_glCtx == aContext )
69  m_glCtx = NULL;
70 }
71 
72 
74 {
75  m_glCtxMutex.lock();
76 
77  for( auto& ctx : m_glContexts )
78  delete ctx.first;
79 
80  m_glContexts.clear();
81  m_glCtx = NULL;
82  m_glCtxMutex.unlock();
83 }
84 
85 
86 void GL_CONTEXT_MANAGER::LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas )
87 {
88  wxCHECK( aCanvas || m_glContexts.count( aContext ) > 0, /* void */ );
89 
90  m_glCtxMutex.lock();
91  wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext );
92 
93  // Prevent assertion failure in wxGLContext::SetCurrent during GAL teardown
94 #ifdef __WXGTK__
95  if( canvas->GetXWindow() )
96 #endif
97  {
98  canvas->SetCurrent( *aContext );
99  }
100 
101  m_glCtx = aContext;
102 }
103 
104 
105 void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext )
106 {
107  wxCHECK( m_glContexts.count( aContext ) > 0, /* void */ );
108 
109  if( m_glCtx == aContext )
110  {
111  m_glCtxMutex.unlock();
112  m_glCtx = NULL;
113  }
114  else
115  {
116  wxFAIL_MSG( wxString::Format( "Trying to unlock GL context mutex from "
117  "a wrong context: aContext %p m_glCtx %p", aContext, m_glCtx ) );
118  }
119 }
120 
121 
123  : m_glCtx( NULL )
124 {
125 }
126 
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 Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
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.