2016-06-03 13:46:12 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 CERN
|
2017-03-06 22:30:24 +00:00
|
|
|
* Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-06-03 13:46:12 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gl_context_mgr.h>
|
|
|
|
|
|
|
|
GL_CONTEXT_MANAGER& GL_CONTEXT_MANAGER::Get()
|
|
|
|
{
|
|
|
|
static GL_CONTEXT_MANAGER instance;
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther )
|
|
|
|
{
|
|
|
|
wxGLContext* context = new wxGLContext( aCanvas, aOther );
|
|
|
|
assert( context /* && context->IsOK() */ ); // IsOK() is available in wx3.1+
|
|
|
|
assert( m_glContexts.count( context ) == 0 );
|
|
|
|
m_glContexts.insert( std::make_pair( context, aCanvas ) );
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-03 13:46:13 +00:00
|
|
|
void GL_CONTEXT_MANAGER::DestroyCtx( wxGLContext* aContext )
|
|
|
|
{
|
2016-06-10 15:06:54 +00:00
|
|
|
if( m_glContexts.count( aContext ) )
|
2016-06-03 13:46:13 +00:00
|
|
|
{
|
|
|
|
m_glContexts.erase( aContext );
|
|
|
|
delete aContext;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-10 15:06:54 +00:00
|
|
|
// Do not delete unknown GL contexts
|
2016-06-03 13:46:13 +00:00
|
|
|
assert( false );
|
|
|
|
}
|
2016-06-10 15:06:54 +00:00
|
|
|
|
|
|
|
if( m_glCtx == aContext )
|
|
|
|
{
|
|
|
|
m_glCtx = NULL;
|
|
|
|
}
|
2016-06-03 13:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-03 13:46:12 +00:00
|
|
|
void GL_CONTEXT_MANAGER::DeleteAll()
|
|
|
|
{
|
|
|
|
for( auto& ctx : m_glContexts )
|
|
|
|
delete ctx.first;
|
|
|
|
|
|
|
|
m_glContexts.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GL_CONTEXT_MANAGER::LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas )
|
|
|
|
{
|
|
|
|
assert( aCanvas || m_glContexts.count( aContext ) > 0 );
|
|
|
|
|
|
|
|
m_glCtxMutex.lock();
|
|
|
|
wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext );
|
2016-06-09 11:44:59 +00:00
|
|
|
|
2017-03-06 22:30:24 +00:00
|
|
|
// Prevent assertion failure in wxGLContext::SetCurrent during GAL teardown
|
2017-03-07 22:51:25 +00:00
|
|
|
#ifdef __WXGTK__
|
2017-03-06 22:30:24 +00:00
|
|
|
if( canvas->GetXWindow() )
|
2017-03-07 22:51:25 +00:00
|
|
|
#endif
|
2017-03-06 22:30:24 +00:00
|
|
|
{
|
|
|
|
canvas->SetCurrent( *aContext );
|
|
|
|
}
|
|
|
|
|
2016-06-13 14:43:33 +00:00
|
|
|
m_glCtx = aContext;
|
2016-06-03 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext )
|
|
|
|
{
|
|
|
|
assert( m_glContexts.count( aContext ) > 0 );
|
|
|
|
|
2016-06-09 11:44:59 +00:00
|
|
|
if( m_glCtx == aContext )
|
|
|
|
{
|
2016-06-03 13:46:12 +00:00
|
|
|
m_glCtxMutex.unlock();
|
2016-06-13 14:43:33 +00:00
|
|
|
m_glCtx = NULL;
|
2016-06-03 13:46:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-07 17:33:13 +00:00
|
|
|
wxLogDebug(
|
|
|
|
"Trying to unlock GL context mutex from a wrong context: aContext %p m_glCtx %p",
|
|
|
|
aContext, m_glCtx );
|
2016-06-03 13:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GL_CONTEXT_MANAGER::GL_CONTEXT_MANAGER()
|
2016-06-13 14:43:33 +00:00
|
|
|
: m_glCtx( NULL )
|
2016-06-03 13:46:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|