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
35{
36public:
37
38 GL_CONTEXT_MANAGER() : m_glCtx( nullptr ) {}
39
48 wxGLContext* CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther = nullptr );
49
57 void DestroyCtx( wxGLContext* aContext );
58
64 void DeleteAll();
65
76 void LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas );
77
84 void UnlockCtx( wxGLContext* aContext );
85
91 wxGLContext* GetCurrentCtx() const
92 {
93 return m_glCtx;
94 }
95
101 wxGLCanvas* GetCurrentCanvas() const
102 {
103 auto it = m_glContexts.find( m_glCtx );
104 return it != m_glContexts.end() ? it->second : nullptr;
105 }
106
112 template<typename Func, typename... Args>
113 auto RunWithoutCtxLock( Func&& aFunction, Args&&... args )
114 {
115 wxGLContext* currentCtx = GetCurrentCtx();
116 wxGLCanvas* currentCanvas = GetCurrentCanvas();
117 UnlockCtx( currentCtx );
118
119 if constexpr (std::is_void_v<decltype(aFunction(std::forward<Args>(args)...))>)
120 {
121 std::forward<Func>(aFunction)(std::forward<Args>(args)...);
122 LockCtx( currentCtx, currentCanvas );
123 return;
124 }
125 else
126 {
127 auto result = std::forward<Func>(aFunction)(std::forward<Args>(args)...);
128 LockCtx( currentCtx, currentCanvas );
129 return result;
130 }
131 }
132
133private:
135 std::map<wxGLContext*, wxGLCanvas*> m_glContexts;
136
138 wxGLContext* m_glCtx;
139
141 std::mutex m_glCtxMutex;
142};
143
144#endif /* GL_CONTEXT_MANAGER_H */
145
void UnlockCtx(wxGLContext *aContext)
Allow other canvases to bind an OpenGL context.
wxGLContext * m_glCtx
Lock to prevent unexpected GL context switching.
wxGLContext * GetCurrentCtx() const
Get the currently bound GL 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.
auto RunWithoutCtxLock(Func &&aFunction, Args &&... args)
Run the given function first releasing the GL context lock, then restoring it.
wxGLCanvas * GetCurrentCanvas() const
Get the currently bound GL canvas.
#define KICOMMON_API
Definition kicommon.h:27
wxString result
Test unit parsing edge cases and error handling.