KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gl_context_mgr.h
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 *
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef GL_CONTEXT_MANAGER_H
24#define GL_CONTEXT_MANAGER_H
25
26#include <kicad_gl/kiglad.h> // Must be included first
27#include <wx/glcanvas.h>
28
29#include <kicommon.h>
30
31#include <mutex>
32#include <map>
33#include <set>
34
36{
37public:
38
39 GL_CONTEXT_MANAGER() : m_glCtx( nullptr ) {}
40
52 wxGLContext* CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther = nullptr );
53
61 void DestroyCtx( wxGLContext* aContext );
62
68 void DeleteAll();
69
83 bool LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas );
84
91 void UnlockCtx( wxGLContext* aContext );
92
98 wxGLContext* GetCurrentCtx() const
99 {
100 return m_glCtx;
101 }
102
108 wxGLCanvas* GetCurrentCanvas() const
109 {
110 auto it = m_glContexts.find( m_glCtx );
111 return it != m_glContexts.end() ? it->second : nullptr;
112 }
113
118 bool IsLoseOnReset( const wxGLContext* aContext ) const
119 {
120 return m_loseOnResetContexts.count( aContext ) > 0;
121 }
122
129 template<typename Func, typename... Args>
130 auto RunWithoutCtxLock( Func&& aFunction, Args&&... args )
131 {
132 wxGLContext* currentCtx = GetCurrentCtx();
133 wxGLCanvas* currentCanvas = GetCurrentCanvas();
134 UnlockCtx( currentCtx );
135
136 if constexpr (std::is_void_v<decltype(aFunction(std::forward<Args>(args)...))>)
137 {
138 std::forward<Func>(aFunction)(std::forward<Args>(args)...);
139 LockCtx( currentCtx, currentCanvas );
140 return;
141 }
142 else
143 {
144 auto result = std::forward<Func>(aFunction)(std::forward<Args>(args)...);
145 LockCtx( currentCtx, currentCanvas );
146 return result;
147 }
148 }
149
150private:
152 std::map<wxGLContext*, wxGLCanvas*> m_glContexts;
153
155 wxGLContext* m_glCtx;
156
158 std::set<const wxGLContext*> m_loseOnResetContexts;
159
161 std::mutex m_glCtxMutex;
162};
163
164#endif /* GL_CONTEXT_MANAGER_H */
165
void UnlockCtx(wxGLContext *aContext)
Allow other canvases to bind an OpenGL context.
wxGLContext * m_glCtx
Currently bound GL context.
std::mutex m_glCtxMutex
Lock to prevent unexpected GL context switching.
std::set< const wxGLContext * > m_loseOnResetContexts
Contexts created with the lose-on-reset notification strategy.
wxGLContext * GetCurrentCtx() const
Get the currently bound GL context.
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
Map of GL contexts & their parent canvases.
auto RunWithoutCtxLock(Func &&aFunction, Args &&... args)
Run the given function first releasing the GL context lock, then restoring it.
bool LockCtx(wxGLContext *aContext, wxGLCanvas *aCanvas)
Set a context as current and prevents other canvases from switching it.
wxGLCanvas * GetCurrentCanvas() const
Get the currently bound GL canvas.
bool IsLoseOnReset(const wxGLContext *aContext) const
#define KICOMMON_API
Definition kicommon.h:27
wxString result
Test unit parsing edge cases and error handling.