You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.2 KiB

3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 CERN
  5. * Copyright (C) 2017-2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <gl_context_mgr.h>
  26. #include <wx/debug.h>
  27. GL_CONTEXT_MANAGER& GL_CONTEXT_MANAGER::Get()
  28. {
  29. static GL_CONTEXT_MANAGER instance;
  30. return instance;
  31. }
  32. wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther )
  33. {
  34. wxGLContext* context = new wxGLContext( aCanvas, aOther );
  35. wxCHECK( context, nullptr );
  36. #if wxCHECK_VERSION( 3, 1, 0 )
  37. if( !context->IsOK() )
  38. {
  39. delete context;
  40. return nullptr;
  41. }
  42. #endif /* wxCHECK_VERSION( 3, 1, 0 ) */
  43. m_glContexts.insert( std::make_pair( context, aCanvas ) );
  44. return context;
  45. }
  46. void GL_CONTEXT_MANAGER::DestroyCtx( wxGLContext* aContext )
  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. wxFAIL;
  57. }
  58. if( m_glCtx == aContext )
  59. m_glCtx = nullptr;
  60. }
  61. void GL_CONTEXT_MANAGER::DeleteAll()
  62. {
  63. m_glCtxMutex.lock();
  64. for( auto& ctx : m_glContexts )
  65. delete ctx.first;
  66. m_glContexts.clear();
  67. m_glCtx = nullptr;
  68. m_glCtxMutex.unlock();
  69. }
  70. void GL_CONTEXT_MANAGER::LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas )
  71. {
  72. wxCHECK( aCanvas || m_glContexts.count( aContext ) > 0, /* void */ );
  73. m_glCtxMutex.lock();
  74. wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext );
  75. // Prevent assertion failure in wxGLContext::SetCurrent during GAL teardown
  76. #ifdef __WXGTK__
  77. if( canvas->GetXWindow() )
  78. #endif
  79. {
  80. canvas->SetCurrent( *aContext );
  81. }
  82. m_glCtx = aContext;
  83. }
  84. void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext )
  85. {
  86. wxCHECK( m_glContexts.count( aContext ) > 0, /* void */ );
  87. if( m_glCtx == aContext )
  88. {
  89. m_glCtxMutex.unlock();
  90. m_glCtx = nullptr;
  91. }
  92. else
  93. {
  94. wxFAIL_MSG( wxString::Format( wxS( "Trying to unlock GL context mutex from "
  95. "a wrong context: aContext %p m_glCtx %p" ), aContext, m_glCtx ) );
  96. }
  97. }
  98. GL_CONTEXT_MANAGER::GL_CONTEXT_MANAGER()
  99. : m_glCtx( nullptr )
  100. {
  101. }