KiCad PCB EDA Suite
GL_CONTEXT_MANAGER Class Reference

#include <gl_context_mgr.h>

Public Member Functions

wxGLContext * CreateCtx (wxGLCanvas *aCanvas, const wxGLContext *aOther=nullptr)
 Create a managed OpenGL context. More...
 
void DestroyCtx (wxGLContext *aContext)
 Destroy a managed OpenGL context. More...
 
void DeleteAll ()
 Destroy all managed OpenGL contexts. More...
 
void LockCtx (wxGLContext *aContext, wxGLCanvas *aCanvas)
 Set a context as current and prevents other canvases from switching it. More...
 
void UnlockCtx (wxGLContext *aContext)
 Allow other canvases to bind an OpenGL context. More...
 

Static Public Member Functions

static GL_CONTEXT_MANAGERGet ()
 Return the GL_CONTEXT_MANAGER instance (singleton). More...
 

Private Member Functions

 GL_CONTEXT_MANAGER ()
 
 GL_CONTEXT_MANAGER (const GL_CONTEXT_MANAGER &)
 
void operator= (const GL_CONTEXT_MANAGER &)
 

Private Attributes

std::map< wxGLContext *, wxGLCanvas * > m_glContexts
 < Map of GL contexts & their parent canvases. More...
 
wxGLContext * m_glCtx
 Lock to prevent unexpected GL context switching. More...
 
std::mutex m_glCtxMutex
 

Detailed Description

Definition at line 34 of file gl_context_mgr.h.

Constructor & Destructor Documentation

◆ GL_CONTEXT_MANAGER() [1/2]

GL_CONTEXT_MANAGER::GL_CONTEXT_MANAGER ( )
private

Definition at line 124 of file gl_context_mgr.cpp.

125  : m_glCtx( nullptr )
126 {
127 }
wxGLContext * m_glCtx
Lock to prevent unexpected GL context switching.

◆ GL_CONTEXT_MANAGER() [2/2]

GL_CONTEXT_MANAGER::GL_CONTEXT_MANAGER ( const GL_CONTEXT_MANAGER )
private

Member Function Documentation

◆ CreateCtx()

wxGLContext * GL_CONTEXT_MANAGER::CreateCtx ( wxGLCanvas *  aCanvas,
const wxGLContext *  aOther = nullptr 
)

Create a managed OpenGL context.

It is assured that the created context is freed upon exit. See wxGLContext documentation for the parameters description.

Returns
Created OpenGL context.

Definition at line 38 of file gl_context_mgr.cpp.

39 {
40  wxGLContext* context = new wxGLContext( aCanvas, aOther );
41  wxCHECK( context, nullptr );
42 
43 #if wxCHECK_VERSION( 3, 1, 0 )
44  if( !context->IsOK() )
45  {
46  delete context;
47  return nullptr;
48  }
49 #endif /* wxCHECK_VERSION( 3, 1, 0 ) */
50 
51  m_glContexts.insert( std::make_pair( context, aCanvas ) );
52 
53  return context;
54 }
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
< Map of GL contexts & their parent canvases.

References m_glContexts.

Referenced by EDA_3D_CANVAS::DoRePaint(), EDA_3D_MODEL_VIEWER::OnPaint(), and KIGFX::OPENGL_GAL::OPENGL_GAL().

◆ DeleteAll()

void GL_CONTEXT_MANAGER::DeleteAll ( )

Destroy all managed OpenGL contexts.

This method should be called in the final deinitialization routine.

Definition at line 75 of file gl_context_mgr.cpp.

76 {
77  m_glCtxMutex.lock();
78 
79  for( auto& ctx : m_glContexts )
80  delete ctx.first;
81 
82  m_glContexts.clear();
83  m_glCtx = nullptr;
84  m_glCtxMutex.unlock();
85 }
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
< Map of GL contexts & their parent canvases.
wxGLContext * m_glCtx
Lock to prevent unexpected GL context switching.
std::mutex m_glCtxMutex

References m_glContexts, m_glCtx, and m_glCtxMutex.

◆ DestroyCtx()

void GL_CONTEXT_MANAGER::DestroyCtx ( wxGLContext *  aContext)

Destroy a managed OpenGL context.

The context to be removed has to be created using GL_CONTEXT_MANAGER::CreateCtx() first.

Parameters
aContextis the OpenGL context to be destroyed. It will not be managed anymore.

Definition at line 57 of file gl_context_mgr.cpp.

58 {
59  if( m_glContexts.count( aContext ) )
60  {
61  m_glContexts.erase( aContext );
62  delete aContext;
63  }
64  else
65  {
66  // Do not delete unknown GL contexts
67  wxFAIL;
68  }
69 
70  if( m_glCtx == aContext )
71  m_glCtx = nullptr;
72 }
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
< Map of GL contexts & their parent canvases.
wxGLContext * m_glCtx
Lock to prevent unexpected GL context switching.

References m_glContexts, and m_glCtx.

Referenced by EDA_3D_CANVAS::releaseOpenGL(), EDA_3D_MODEL_VIEWER::~EDA_3D_MODEL_VIEWER(), and KIGFX::OPENGL_GAL::~OPENGL_GAL().

◆ Get()

◆ LockCtx()

void GL_CONTEXT_MANAGER::LockCtx ( wxGLContext *  aContext,
wxGLCanvas *  aCanvas 
)

Set a context as current and prevents other canvases from switching it.

Requires calling UnlockCtx() when there are no more GL calls for the context. If another canvas has already locked a GL context, then the calling process is blocked.

Parameters
aContextis the GL context to be bound.
aCanvas(optional) allows caller to bind the context to a non-parent canvas (e.g. when a few canvases share a single GL context).

Definition at line 88 of file gl_context_mgr.cpp.

89 {
90  wxCHECK( aCanvas || m_glContexts.count( aContext ) > 0, /* void */ );
91 
92  m_glCtxMutex.lock();
93  wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext );
94 
95  // Prevent assertion failure in wxGLContext::SetCurrent during GAL teardown
96 #ifdef __WXGTK__
97  if( canvas->GetXWindow() )
98 #endif
99  {
100  canvas->SetCurrent( *aContext );
101  }
102 
103  m_glCtx = aContext;
104 }
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
< Map of GL contexts & their parent canvases.
wxGLContext * m_glCtx
Lock to prevent unexpected GL context switching.
std::mutex m_glCtxMutex

References m_glContexts, m_glCtx, and m_glCtxMutex.

Referenced by EDA_3D_CANVAS::DoRePaint(), KIGFX::OPENGL_GAL::LockContext(), EDA_3D_MODEL_VIEWER::OnPaint(), EDA_3D_CANVAS::releaseOpenGL(), EDA_3D_MODEL_VIEWER::~EDA_3D_MODEL_VIEWER(), and KIGFX::OPENGL_GAL::~OPENGL_GAL().

◆ operator=()

void GL_CONTEXT_MANAGER::operator= ( const GL_CONTEXT_MANAGER )
private

◆ UnlockCtx()

void GL_CONTEXT_MANAGER::UnlockCtx ( wxGLContext *  aContext)

Allow other canvases to bind an OpenGL context.

Parameters
aContextis the currently bound context. It is only a check to assure the right canvas wants to unlock GL context.

Definition at line 107 of file gl_context_mgr.cpp.

108 {
109  wxCHECK( m_glContexts.count( aContext ) > 0, /* void */ );
110 
111  if( m_glCtx == aContext )
112  {
113  m_glCtxMutex.unlock();
114  m_glCtx = nullptr;
115  }
116  else
117  {
118  wxFAIL_MSG( wxString::Format( "Trying to unlock GL context mutex from "
119  "a wrong context: aContext %p m_glCtx %p", aContext, m_glCtx ) );
120  }
121 }
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
< Map of GL contexts & their parent canvases.
wxGLContext * m_glCtx
Lock to prevent unexpected GL context switching.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
std::mutex m_glCtxMutex

References Format(), m_glContexts, m_glCtx, and m_glCtxMutex.

Referenced by EDA_3D_CANVAS::DoRePaint(), EDA_3D_MODEL_VIEWER::OnPaint(), EDA_3D_CANVAS::releaseOpenGL(), KIGFX::OPENGL_GAL::UnlockContext(), EDA_3D_MODEL_VIEWER::~EDA_3D_MODEL_VIEWER(), and KIGFX::OPENGL_GAL::~OPENGL_GAL().

Member Data Documentation

◆ m_glContexts

std::map<wxGLContext*, wxGLCanvas*> GL_CONTEXT_MANAGER::m_glContexts
private

< Map of GL contexts & their parent canvases.

Currently bound GL context.

Definition at line 90 of file gl_context_mgr.h.

Referenced by CreateCtx(), DeleteAll(), DestroyCtx(), LockCtx(), and UnlockCtx().

◆ m_glCtx

wxGLContext* GL_CONTEXT_MANAGER::m_glCtx
private

Lock to prevent unexpected GL context switching.

Definition at line 93 of file gl_context_mgr.h.

Referenced by DeleteAll(), DestroyCtx(), LockCtx(), and UnlockCtx().

◆ m_glCtxMutex

std::mutex GL_CONTEXT_MANAGER::m_glCtxMutex
private

Definition at line 96 of file gl_context_mgr.h.

Referenced by DeleteAll(), LockCtx(), and UnlockCtx().


The documentation for this class was generated from the following files: