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.

105 lines
3.4 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) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * @author Maciej Suminski <maciej.suminski@cern.ch>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #ifndef GL_CONTEXT_MANAGER_H
  27. #define GL_CONTEXT_MANAGER_H
  28. #include <wx/glcanvas.h>
  29. #include <mutex>
  30. #include <map>
  31. class GL_CONTEXT_MANAGER
  32. {
  33. public:
  34. /**
  35. * Return the GL_CONTEXT_MANAGER instance (singleton).
  36. */
  37. static GL_CONTEXT_MANAGER& Get();
  38. /**
  39. * Create a managed OpenGL context.
  40. *
  41. * It is assured that the created context is freed upon exit. See wxGLContext
  42. * documentation for the parameters description.
  43. *
  44. * @return Created OpenGL context.
  45. */
  46. wxGLContext* CreateCtx( wxGLCanvas* aCanvas, const wxGLContext* aOther = nullptr );
  47. /**
  48. * Destroy a managed OpenGL context.
  49. *
  50. * The context to be removed has to be created using GL_CONTEXT_MANAGER::CreateCtx() first.
  51. *
  52. * @param aContext is the OpenGL context to be destroyed. It will not be managed anymore.
  53. */
  54. void DestroyCtx( wxGLContext* aContext );
  55. /**
  56. * Destroy all managed OpenGL contexts.
  57. *
  58. * This method should be called in the final deinitialization routine.
  59. */
  60. void DeleteAll();
  61. /**
  62. * Set a context as current and prevents other canvases from switching it.
  63. *
  64. * Requires calling UnlockCtx() when there are no more GL calls for the context. If
  65. * another canvas has already locked a GL context, then the calling process is blocked.
  66. *
  67. * @param aContext is the GL context to be bound.
  68. * @param aCanvas (optional) allows caller to bind the context to a non-parent canvas
  69. * (e.g. when a few canvases share a single GL context).
  70. */
  71. void LockCtx( wxGLContext* aContext, wxGLCanvas* aCanvas );
  72. /**
  73. * Allow other canvases to bind an OpenGL context.
  74. *
  75. * @param aContext is the currently bound context. It is only a check to assure the right
  76. * canvas wants to unlock GL context.
  77. */
  78. void UnlockCtx( wxGLContext* aContext );
  79. private:
  80. ///< Map of GL contexts & their parent canvases.
  81. std::map<wxGLContext*, wxGLCanvas*> m_glContexts;
  82. ///< Currently bound GL context.
  83. wxGLContext* m_glCtx;
  84. ///< Lock to prevent unexpected GL context switching.
  85. std::mutex m_glCtxMutex;
  86. // Singleton
  87. GL_CONTEXT_MANAGER();
  88. GL_CONTEXT_MANAGER( const GL_CONTEXT_MANAGER& );
  89. void operator=( const GL_CONTEXT_MANAGER& );
  90. };
  91. #endif /* GL_CONTEXT_MANAGER_H */