KiCad PCB EDA Suite
GL_CONTEXT_MANAGER Class Reference

#include <gl_context_mgr.h>

Public Member Functions

wxGLContext * CreateCtx (wxGLCanvas *aCanvas, const wxGLContext *aOther=NULL)
 Function CreateCtx creates a managed OpenGL context. More...
 
void DestroyCtx (wxGLContext *aContext)
 Function DestroyCtx destroys a managed OpenGL context. More...
 
void DeleteAll ()
 Function DeleteAll destroys all managed OpenGL contexts. More...
 
void LockCtx (wxGLContext *aContext, wxGLCanvas *aCanvas)
 Function LockCtx sets a context as current and prevents other canvases from switching it. More...
 
void UnlockCtx (wxGLContext *aContext)
 Function UnlockCtx allows other canvases to bind an OpenGL context. More...
 

Static Public Member Functions

static GL_CONTEXT_MANAGERGet ()
 Function Get returns 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
 

Currently bound GL context.

More...
 
std::mutex m_glCtxMutex
 

Lock to prevent unexpected GL context switching.

More...
 

Detailed Description

Definition at line 32 of file gl_context_mgr.h.

Constructor & Destructor Documentation

◆ GL_CONTEXT_MANAGER() [1/2]

GL_CONTEXT_MANAGER::GL_CONTEXT_MANAGER ( )
private

Definition at line 122 of file gl_context_mgr.cpp.

123  : m_glCtx( NULL )
124 {
125 }
wxGLContext * m_glCtx
Currently bound GL context.
#define NULL

◆ 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 = NULL 
)

Function CreateCtx creates 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 36 of file gl_context_mgr.cpp.

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

References m_glContexts.

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

◆ DeleteAll()

void GL_CONTEXT_MANAGER::DeleteAll ( )

Function DeleteAll destroys all managed OpenGL contexts.

This method should be called in the final deinitialization routine.

Definition at line 73 of file gl_context_mgr.cpp.

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

References m_glContexts, m_glCtx, m_glCtxMutex, and NULL.

◆ DestroyCtx()

void GL_CONTEXT_MANAGER::DestroyCtx ( wxGLContext *  aContext)

Function DestroyCtx destroys 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 55 of file gl_context_mgr.cpp.

56 {
57  if( m_glContexts.count( aContext ) )
58  {
59  m_glContexts.erase( aContext );
60  delete aContext;
61  }
62  else
63  {
64  // Do not delete unknown GL contexts
65  wxFAIL;
66  }
67 
68  if( m_glCtx == aContext )
69  m_glCtx = NULL;
70 }
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
Map of GL contexts & their parent canvases.
wxGLContext * m_glCtx
Currently bound GL context.
#define NULL

References m_glContexts, m_glCtx, and NULL.

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

◆ Get()

◆ LockCtx()

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

Function LockCtx sets 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 86 of file gl_context_mgr.cpp.

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

References m_glContexts, m_glCtx, and m_glCtxMutex.

Referenced by EDA_3D_CANVAS::DoRePaint(), KIGFX::OPENGL_GAL::lockContext(), C3D_MODEL_VIEWER::OnPaint(), EDA_3D_CANVAS::releaseOpenGL(), C3D_MODEL_VIEWER::~C3D_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)

Function UnlockCtx allows 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 105 of file gl_context_mgr.cpp.

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

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

Referenced by EDA_3D_CANVAS::DoRePaint(), C3D_MODEL_VIEWER::OnPaint(), EDA_3D_CANVAS::releaseOpenGL(), KIGFX::OPENGL_GAL::unlockContext(), C3D_MODEL_VIEWER::~C3D_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.

Definition at line 86 of file gl_context_mgr.h.

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

◆ m_glCtx

wxGLContext* GL_CONTEXT_MANAGER::m_glCtx
private

Currently bound GL context.

Definition at line 89 of file gl_context_mgr.h.

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

◆ m_glCtxMutex

std::mutex GL_CONTEXT_MANAGER::m_glCtxMutex
private

Lock to prevent unexpected GL context switching.

Definition at line 92 of file gl_context_mgr.h.

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


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