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 The 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
30wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther )
31{
32 wxGLContext* context = new wxGLContext( aCanvas, aOther );
33 wxCHECK( context, nullptr );
34
35 if( !context->IsOK() )
36 {
37 delete context;
38 return nullptr;
39 }
40
41 m_glContexts.insert( std::make_pair( context, aCanvas ) );
42
43 return context;
44}
45
46
47void GL_CONTEXT_MANAGER::DestroyCtx( wxGLContext* aContext )
48{
49 if( m_glContexts.count( aContext ) )
50 {
51 m_glContexts.erase( aContext );
52 delete aContext;
53 }
54 else
55 {
56 // Do not delete unknown GL contexts
57 wxFAIL;
58 }
59
60 if( m_glCtx == aContext )
61 m_glCtx = nullptr;
62}
63
64
66{
67 m_glCtxMutex.lock();
68
69 for( auto& ctx : m_glContexts )
70 delete ctx.first;
71
72 m_glContexts.clear();
73 m_glCtx = nullptr;
74 m_glCtxMutex.unlock();
75}
76
77
78void GL_CONTEXT_MANAGER::LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas )
79{
80 wxCHECK( aContext && m_glContexts.count( aContext ) > 0, /* void */ );
81
82 m_glCtxMutex.lock();
83 wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext );
84
85 // Prevent assertion failure in wxGLContext::SetCurrent during GAL teardown
86#ifdef __WXGTK__
87
88#ifdef KICAD_USE_EGL
89 if( canvas->GTKGetDrawingWindow() )
90#else
91 if( canvas->GetXWindow() )
92#endif // KICAD_USE_EGL
93
94#endif // __WXGTK__
95 {
96 canvas->SetCurrent( *aContext );
97 }
98
99 m_glCtx = aContext;
100}
101
102
103void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext )
104{
105 wxCHECK( aContext && m_glContexts.count( aContext ) > 0, /* void */ );
106
107 if( m_glCtx == aContext )
108 {
109 m_glCtxMutex.unlock();
110 m_glCtx = nullptr;
111 }
112 else
113 {
114 wxFAIL_MSG( wxString::Format( wxS( "Trying to unlock GL context mutex from "
115 "a wrong context: aContext %p m_glCtx %p" ), aContext, m_glCtx ) );
116 }
117}
118
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.
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.