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
|
2021-07-15 19:26:35 +00:00
|
|
|
* Copyright (C) 2017-2021 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>
|
2018-06-11 10:25:20 +00:00
|
|
|
#include <wx/debug.h>
|
2016-06-03 13:46:12 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
|
2016-06-03 13:46:12 +00:00
|
|
|
GL_CONTEXT_MANAGER& GL_CONTEXT_MANAGER::Get()
|
|
|
|
{
|
|
|
|
static GL_CONTEXT_MANAGER instance;
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
|
2016-06-03 13:46:12 +00:00
|
|
|
wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther )
|
|
|
|
{
|
|
|
|
wxGLContext* context = new wxGLContext( aCanvas, aOther );
|
2018-06-11 10:25:20 +00:00
|
|
|
wxCHECK( context, nullptr );
|
|
|
|
|
|
|
|
#if wxCHECK_VERSION( 3, 1, 0 )
|
|
|
|
if( !context->IsOK() )
|
|
|
|
{
|
|
|
|
delete context;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif /* wxCHECK_VERSION( 3, 1, 0 ) */
|
|
|
|
|
2016-06-03 13:46:12 +00:00
|
|
|
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
|
2018-06-11 10:25:20 +00:00
|
|
|
wxFAIL;
|
2016-06-03 13:46:13 +00:00
|
|
|
}
|
2016-06-10 15:06:54 +00:00
|
|
|
|
|
|
|
if( m_glCtx == aContext )
|
2021-07-15 19:26:35 +00:00
|
|
|
m_glCtx = nullptr;
|
2016-06-03 13:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-03 13:46:12 +00:00
|
|
|
void GL_CONTEXT_MANAGER::DeleteAll()
|
|
|
|
{
|
2018-06-01 15:57:52 +00:00
|
|
|
m_glCtxMutex.lock();
|
|
|
|
|
2016-06-03 13:46:12 +00:00
|
|
|
for( auto& ctx : m_glContexts )
|
|
|
|
delete ctx.first;
|
|
|
|
|
|
|
|
m_glContexts.clear();
|
2021-07-15 19:26:35 +00:00
|
|
|
m_glCtx = nullptr;
|
2018-06-01 15:57:52 +00:00
|
|
|
m_glCtxMutex.unlock();
|
2016-06-03 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GL_CONTEXT_MANAGER::LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas )
|
|
|
|
{
|
2023-06-14 04:45:34 +00:00
|
|
|
wxCHECK( aContext && m_glContexts.count( aContext ) > 0, /* void */ );
|
2016-06-03 13:46:12 +00:00
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
2023-06-11 15:59:00 +00:00
|
|
|
wxCHECK( aContext && m_glContexts.count( aContext ) > 0, /* void */ );
|
2016-06-03 13:46:12 +00:00
|
|
|
|
2016-06-09 11:44:59 +00:00
|
|
|
if( m_glCtx == aContext )
|
|
|
|
{
|
2016-06-03 13:46:12 +00:00
|
|
|
m_glCtxMutex.unlock();
|
2021-07-15 19:26:35 +00:00
|
|
|
m_glCtx = nullptr;
|
2016-06-03 13:46:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-22 14:41:33 +00:00
|
|
|
wxFAIL_MSG( wxString::Format( wxS( "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()
|
2021-07-15 19:26:35 +00:00
|
|
|
: m_glCtx( nullptr )
|
2016-06-03 13:46:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|