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.

114 lines
2.9 KiB

  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 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. GL_CONTEXT_MANAGER& GL_CONTEXT_MANAGER::Get()
  27. {
  28. static GL_CONTEXT_MANAGER instance;
  29. return instance;
  30. }
  31. wxGLContext* GL_CONTEXT_MANAGER::CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther )
  32. {
  33. wxGLContext* context = new wxGLContext( aCanvas, aOther );
  34. assert( context /* && context->IsOK() */ ); // IsOK() is available in wx3.1+
  35. assert( m_glContexts.count( context ) == 0 );
  36. m_glContexts.insert( std::make_pair( context, aCanvas ) );
  37. return context;
  38. }
  39. void GL_CONTEXT_MANAGER::DestroyCtx( wxGLContext* aContext )
  40. {
  41. if( m_glContexts.count( aContext ) )
  42. {
  43. m_glContexts.erase( aContext );
  44. delete aContext;
  45. }
  46. else
  47. {
  48. // Do not delete unknown GL contexts
  49. assert( false );
  50. }
  51. if( m_glCtx == aContext )
  52. {
  53. m_glCtx = NULL;
  54. }
  55. }
  56. void GL_CONTEXT_MANAGER::DeleteAll()
  57. {
  58. for( auto& ctx : m_glContexts )
  59. delete ctx.first;
  60. m_glContexts.clear();
  61. }
  62. void GL_CONTEXT_MANAGER::LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas )
  63. {
  64. assert( aCanvas || m_glContexts.count( aContext ) > 0 );
  65. m_glCtxMutex.lock();
  66. wxGLCanvas* canvas = aCanvas ? aCanvas : m_glContexts.at( aContext );
  67. // Prevent assertion failure in wxGLContext::SetCurrent during GAL teardown
  68. #ifdef __WXGTK__
  69. if( canvas->GetXWindow() )
  70. #endif
  71. {
  72. canvas->SetCurrent( *aContext );
  73. }
  74. m_glCtx = aContext;
  75. }
  76. void GL_CONTEXT_MANAGER::UnlockCtx( wxGLContext* aContext )
  77. {
  78. assert( m_glContexts.count( aContext ) > 0 );
  79. if( m_glCtx == aContext )
  80. {
  81. m_glCtxMutex.unlock();
  82. m_glCtx = NULL;
  83. }
  84. else
  85. {
  86. wxLogDebug( "Trying to unlock GL context mutex from a wrong context" );
  87. }
  88. }
  89. GL_CONTEXT_MANAGER::GL_CONTEXT_MANAGER()
  90. : m_glCtx( NULL )
  91. {
  92. }