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, see <https://www.gnu.org/licenses/>.
20 */
21
23#include <wx/debug.h>
24
25
26wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther )
27{
28 wxGLContext* context = new wxGLContext( aCanvas, aOther );
29 wxCHECK( context, nullptr );
30
31 if( !context->IsOK() )
32 {
33 delete context;
34 return nullptr;
35 }
36
37 m_glContexts.insert( std::make_pair( context, aCanvas ) );
38
39 return context;
40}
41
42
43void GL_CONTEXT_MANAGER::DestroyCtx( wxGLContext* aContext )
44{
45 if( m_glContexts.count( aContext ) )
46 {
47 m_glContexts.erase( aContext );
48 delete aContext;
49 }
50 else
51 {
52 // Do not delete unknown GL contexts
53 wxFAIL;
54 }
55
56 if( m_glCtx == aContext )
57 m_glCtx = nullptr;
58}
59
60
62{
63 m_glCtxMutex.lock();
64
65 for( auto& ctx : m_glContexts )
66 delete ctx.first;
67
68 m_glContexts.clear();
69 m_glCtx = nullptr;
70 m_glCtxMutex.unlock();
71}
72
73
74void GL_CONTEXT_MANAGER::LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas )
75{
76 wxCHECK( aContext && m_glContexts.count( aContext ) > 0, /* void */ );
77
78 m_glCtxMutex.lock();
79 wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext );
80
81#ifdef __WXGTK__
82 // Prevent assertion failure in wxGLContext::SetCurrent during GAL teardown
83 if( canvas->GTKGetDrawingWindow() )
84#endif // __WXGTK__
85 {
86 canvas->SetCurrent( *aContext );
87 }
88
89 m_glCtx = aContext;
90}
91
92
93void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext )
94{
95 wxCHECK( aContext && m_glContexts.count( aContext ) > 0, /* void */ );
96
97 if( m_glCtx == aContext )
98 {
99 m_glCtxMutex.unlock();
100 m_glCtx = nullptr;
101 }
102 else
103 {
104 wxFAIL_MSG( wxString::Format( wxS( "Trying to unlock GL context mutex from "
105 "a wrong context: aContext %p m_glCtx %p" ), aContext, m_glCtx ) );
106 }
107}
108
void UnlockCtx(wxGLContext *aContext)
Allow other canvases to bind an OpenGL context.
wxGLContext * m_glCtx
Lock to prevent unexpected GL context switching.
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.