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...
 
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::GL_CONTEXT_MANAGER ( )
private

Definition at line 112 of file gl_context_mgr.cpp.

113  : m_glCtx( NULL )
114 {
115 }
wxGLContext * m_glCtx
Currently bound GL context.
GL_CONTEXT_MANAGER::GL_CONTEXT_MANAGER ( const GL_CONTEXT_MANAGER )
private

Member Function Documentation

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 35 of file gl_context_mgr.cpp.

References m_glContexts.

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

36 {
37  wxGLContext* context = new wxGLContext( aCanvas, aOther );
38  assert( context /* && context->IsOK() */ ); // IsOK() is available in wx3.1+
39  assert( m_glContexts.count( context ) == 0 );
40  m_glContexts.insert( std::make_pair( context, aCanvas ) );
41 
42  return context;
43 }
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
Map of GL contexts & their parent canvases.
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 66 of file gl_context_mgr.cpp.

References m_glContexts.

67 {
68  for( auto& ctx : m_glContexts )
69  delete ctx.first;
70 
71  m_glContexts.clear();
72 }
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
Map of GL contexts & their parent canvases.
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 46 of file gl_context_mgr.cpp.

References m_glContexts, and m_glCtx.

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

47 {
48  if( m_glContexts.count( aContext ) )
49  {
50  m_glContexts.erase( aContext );
51  delete aContext;
52  }
53  else
54  {
55  // Do not delete unknown GL contexts
56  assert( false );
57  }
58 
59  if( m_glCtx == aContext )
60  {
61  m_glCtx = NULL;
62  }
63 }
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
Map of GL contexts & their parent canvases.
wxGLContext * m_glCtx
Currently bound GL context.
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 75 of file gl_context_mgr.cpp.

References m_glContexts, m_glCtx, and m_glCtxMutex.

Referenced by KIGFX::OPENGL_GAL::BeginDrawing(), KIGFX::OPENGL_GAL::BeginUpdate(), KIGFX::OPENGL_GAL::init(), C3D_MODEL_VIEWER::OnPaint(), EDA_3D_CANVAS::OnPaint(), EDA_3D_CANVAS::releaseOpenGL(), C3D_MODEL_VIEWER::~C3D_MODEL_VIEWER(), and KIGFX::OPENGL_GAL::~OPENGL_GAL().

76 {
77  assert( aCanvas || m_glContexts.count( aContext ) > 0 );
78 
79  m_glCtxMutex.lock();
80  wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext );
81 
82  // Prevent assertion failure in wxGLContext::SetCurrent during GAL teardown
83 #ifdef __WXGTK__
84  if( canvas->GetXWindow() )
85 #endif
86  {
87  canvas->SetCurrent( *aContext );
88  }
89 
90  m_glCtx = aContext;
91 }
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
Map of GL contexts & their parent canvases.
MUTEX m_glCtxMutex
Lock to prevent unexpected GL context switching.
wxGLContext * m_glCtx
Currently bound GL context.
void GL_CONTEXT_MANAGER::operator= ( const GL_CONTEXT_MANAGER )
private
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 94 of file gl_context_mgr.cpp.

References m_glContexts, m_glCtx, and m_glCtxMutex.

Referenced by KIGFX::OPENGL_GAL::BeginDrawing(), KIGFX::OPENGL_GAL::EndDrawing(), KIGFX::OPENGL_GAL::EndUpdate(), KIGFX::OPENGL_GAL::init(), C3D_MODEL_VIEWER::OnPaint(), EDA_3D_CANVAS::OnPaint(), EDA_3D_CANVAS::releaseOpenGL(), C3D_MODEL_VIEWER::~C3D_MODEL_VIEWER(), and KIGFX::OPENGL_GAL::~OPENGL_GAL().

95 {
96  assert( m_glContexts.count( aContext ) > 0 );
97 
98  if( m_glCtx == aContext )
99  {
100  m_glCtxMutex.unlock();
101  m_glCtx = NULL;
102  }
103  else
104  {
105  wxLogDebug(
106  "Trying to unlock GL context mutex from a wrong context: aContext %p m_glCtx %p",
107  aContext, m_glCtx );
108  }
109 }
std::map< wxGLContext *, wxGLCanvas * > m_glContexts
Map of GL contexts & their parent canvases.
MUTEX m_glCtxMutex
Lock to prevent unexpected GL context switching.
wxGLContext * m_glCtx
Currently bound GL context.

Member Data Documentation

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().

wxGLContext* GL_CONTEXT_MANAGER::m_glCtx
private

Currently bound GL context.

Definition at line 89 of file gl_context_mgr.h.

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

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 LockCtx(), and UnlockCtx().


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