KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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
27#include <wx/debug.h>
28
29
31{
32 static GL_CONTEXT_MANAGER instance;
33
34 return instance;
35}
36
37
38wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther )
39{
40 wxGLContext* context = new wxGLContext( aCanvas, aOther );
41 wxCHECK( context, nullptr );
42
43 if( !context->IsOK() )
44 {
45 delete context;
46 return nullptr;
47 }
48
49 m_glContexts.insert( std::make_pair( context, aCanvas ) );
50
51 return context;
52}
53
54
55void 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 = nullptr;
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 = nullptr;
82 m_glCtxMutex.unlock();
83}
84
85
86void GL_CONTEXT_MANAGER::LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas )
87{
88 wxCHECK( aContext && 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
96#ifdef KICAD_USE_EGL
97 if( canvas->GTKGetDrawingWindow() )
98#else
99 if( canvas->GetXWindow() )
100#endif // KICAD_USE_EGL
101
102#endif // __WXGTK__
103 {
104 canvas->SetCurrent( *aContext );
105 }
106
107 m_glCtx = aContext;
108}
109
110
111void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext )
112{
113 wxCHECK( aContext && m_glContexts.count( aContext ) > 0, /* void */ );
114
115 if( m_glCtx == aContext )
116 {
117 m_glCtxMutex.unlock();
118 m_glCtx = nullptr;
119 }
120 else
121 {
122 wxFAIL_MSG( wxString::Format( wxS( "Trying to unlock GL context mutex from "
123 "a wrong context: aContext %p m_glCtx %p" ), aContext, m_glCtx ) );
124 }
125}
126
127
129 : m_glCtx( nullptr )
130{
131}
132
void UnlockCtx(wxGLContext *aContext)
Allow other canvases to bind an OpenGL context.
wxGLContext * m_glCtx
Lock to prevent unexpected GL context switching.
std::mutex m_glCtxMutex
void DestroyCtx(wxGLContext *aContext)
Destroy a managed OpenGL context.
void LockCtx(wxGLContext *aContext, wxGLCanvas *aCanvas)
Set a context as current and prevents other canvases from switching it.
static GL_CONTEXT_MANAGER & Get()
Return the GL_CONTEXT_MANAGER instance (singleton).
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
< Map of GL contexts & their parent canvases.
wxGLContext * CreateCtx(wxGLCanvas *aCanvas, const wxGLContext *aOther=nullptr)
Create a managed OpenGL context.
void DeleteAll()
Destroy all managed OpenGL contexts.