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-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <[email protected]>
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 
29 
31 {
32  static GL_CONTEXT_MANAGER instance;
33 
34  return instance;
35 }
36 
37 
38 wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther )
39 {
40  wxGLContext* context = new wxGLContext( aCanvas, aOther );
41  wxCHECK( context, nullptr );
42 
43 #if wxCHECK_VERSION( 3, 1, 0 )
44  if( !context->IsOK() )
45  {
46  delete context;
47  return nullptr;
48  }
49 #endif /* wxCHECK_VERSION( 3, 1, 0 ) */
50 
51  m_glContexts.insert( std::make_pair( context, aCanvas ) );
52 
53  return context;
54 }
55 
56 
57 void GL_CONTEXT_MANAGER::DestroyCtx( wxGLContext* aContext )
58 {
59  if( m_glContexts.count( aContext ) )
60  {
61  m_glContexts.erase( aContext );
62  delete aContext;
63  }
64  else
65  {
66  // Do not delete unknown GL contexts
67  wxFAIL;
68  }
69 
70  if( m_glCtx == aContext )
71  m_glCtx = nullptr;
72 }
73 
74 
76 {
77  m_glCtxMutex.lock();
78 
79  for( auto& ctx : m_glContexts )
80  delete ctx.first;
81 
82  m_glContexts.clear();
83  m_glCtx = nullptr;
84  m_glCtxMutex.unlock();
85 }
86 
87 
88 void GL_CONTEXT_MANAGER::LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas )
89 {
90  wxCHECK( aCanvas || m_glContexts.count( aContext ) > 0, /* void */ );
91 
92  m_glCtxMutex.lock();
93  wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext );
94 
95  // Prevent assertion failure in wxGLContext::SetCurrent during GAL teardown
96 #ifdef __WXGTK__
97  if( canvas->GetXWindow() )
98 #endif
99  {
100  canvas->SetCurrent( *aContext );
101  }
102 
103  m_glCtx = aContext;
104 }
105 
106 
107 void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext )
108 {
109  wxCHECK( m_glContexts.count( aContext ) > 0, /* void */ );
110 
111  if( m_glCtx == aContext )
112  {
113  m_glCtxMutex.unlock();
114  m_glCtx = nullptr;
115  }
116  else
117  {
118  wxFAIL_MSG( wxString::Format( "Trying to unlock GL context mutex from "
119  "a wrong context: aContext %p m_glCtx %p", aContext, m_glCtx ) );
120  }
121 }
122 
123 
125  : m_glCtx( nullptr )
126 {
127 }
128 
void DeleteAll()
Destroy all managed OpenGL contexts.
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
< Map of GL contexts & their parent canvases.
wxGLContext * m_glCtx
Lock to prevent unexpected GL context switching.
void UnlockCtx(wxGLContext *aContext)
Allow other canvases to bind an OpenGL context.
static GL_CONTEXT_MANAGER & Get()
Return the GL_CONTEXT_MANAGER instance (singleton).
wxGLContext * CreateCtx(wxGLCanvas *aCanvas, const wxGLContext *aOther=nullptr)
Create a managed OpenGL context.
void LockCtx(wxGLContext *aContext, wxGLCanvas *aCanvas)
Set a context as current and prevents other canvases from switching it.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
std::mutex m_glCtxMutex
void DestroyCtx(wxGLContext *aContext)
Destroy a managed OpenGL context.