The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

194 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. extern Display* display;
  18. extern XContext windowHandleXContext;
  19. //==============================================================================
  20. class OpenGLContext::NativeContext
  21. {
  22. public:
  23. NativeContext (Component& component,
  24. const OpenGLPixelFormat& pixelFormat,
  25. void* shareContext)
  26. : renderContext (0), embeddedWindow (0), swapFrames (0), bestVisual (0),
  27. contextToShareWith (shareContext)
  28. {
  29. ScopedXLock xlock;
  30. XSync (display, False);
  31. GLint attribs[] =
  32. {
  33. GLX_RGBA,
  34. GLX_DOUBLEBUFFER,
  35. GLX_RED_SIZE, pixelFormat.redBits,
  36. GLX_GREEN_SIZE, pixelFormat.greenBits,
  37. GLX_BLUE_SIZE, pixelFormat.blueBits,
  38. GLX_ALPHA_SIZE, pixelFormat.alphaBits,
  39. GLX_DEPTH_SIZE, pixelFormat.depthBufferBits,
  40. GLX_STENCIL_SIZE, pixelFormat.stencilBufferBits,
  41. GLX_ACCUM_RED_SIZE, pixelFormat.accumulationBufferRedBits,
  42. GLX_ACCUM_GREEN_SIZE, pixelFormat.accumulationBufferGreenBits,
  43. GLX_ACCUM_BLUE_SIZE, pixelFormat.accumulationBufferBlueBits,
  44. GLX_ACCUM_ALPHA_SIZE, pixelFormat.accumulationBufferAlphaBits,
  45. None
  46. };
  47. bestVisual = glXChooseVisual (display, DefaultScreen (display), attribs);
  48. if (bestVisual == nullptr)
  49. return;
  50. ComponentPeer* const peer = component.getPeer();
  51. Window windowH = (Window) peer->getNativeHandle();
  52. Colormap colourMap = XCreateColormap (display, windowH, bestVisual->visual, AllocNone);
  53. XSetWindowAttributes swa;
  54. swa.colormap = colourMap;
  55. swa.border_pixel = 0;
  56. swa.event_mask = ExposureMask | StructureNotifyMask;
  57. const Rectangle<int> bounds (component.getTopLevelComponent()
  58. ->getLocalArea (&component, component.getLocalBounds()));
  59. embeddedWindow = XCreateWindow (display, windowH,
  60. bounds.getX(), bounds.getY(),
  61. jmax (1, bounds.getWidth()),
  62. jmax (1, bounds.getHeight()),
  63. 0, bestVisual->depth,
  64. InputOutput,
  65. bestVisual->visual,
  66. CWBorderPixel | CWColormap | CWEventMask,
  67. &swa);
  68. XSaveContext (display, (XID) embeddedWindow, windowHandleXContext, (XPointer) peer);
  69. XMapWindow (display, embeddedWindow);
  70. XFreeColormap (display, colourMap);
  71. XSync (display, False);
  72. }
  73. ~NativeContext()
  74. {
  75. if (embeddedWindow != 0)
  76. {
  77. ScopedXLock xlock;
  78. XUnmapWindow (display, embeddedWindow);
  79. XDestroyWindow (display, embeddedWindow);
  80. }
  81. if (bestVisual != nullptr)
  82. XFree (bestVisual);
  83. }
  84. void initialiseOnRenderThread (OpenGLContext& context)
  85. {
  86. ScopedXLock xlock;
  87. renderContext = glXCreateContext (display, bestVisual, (GLXContext) contextToShareWith, GL_TRUE);
  88. context.makeActive();
  89. }
  90. void shutdownOnRenderThread()
  91. {
  92. deactivateCurrentContext();
  93. glXDestroyContext (display, renderContext);
  94. renderContext = nullptr;
  95. }
  96. bool makeActive() const noexcept
  97. {
  98. return renderContext != 0
  99. && glXMakeCurrent (display, embeddedWindow, renderContext);
  100. }
  101. bool isActive() const noexcept
  102. {
  103. return glXGetCurrentContext() == renderContext && renderContext != 0;
  104. }
  105. static void deactivateCurrentContext()
  106. {
  107. glXMakeCurrent (display, None, 0);
  108. }
  109. void swapBuffers()
  110. {
  111. glXSwapBuffers (display, embeddedWindow);
  112. }
  113. void updateWindowPosition (const Rectangle<int>& newBounds)
  114. {
  115. bounds = newBounds;
  116. ScopedXLock xlock;
  117. XMoveResizeWindow (display, embeddedWindow,
  118. bounds.getX(), bounds.getY(),
  119. jmax (1, bounds.getWidth()),
  120. jmax (1, bounds.getHeight()));
  121. }
  122. bool setSwapInterval (int numFramesPerSwap)
  123. {
  124. if (numFramesPerSwap == swapFrames)
  125. return true;
  126. PFNGLXSWAPINTERVALSGIPROC GLXSwapIntervalSGI
  127. = (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI");
  128. if (GLXSwapIntervalSGI != nullptr)
  129. {
  130. swapFrames = numFramesPerSwap;
  131. GLXSwapIntervalSGI (numFramesPerSwap);
  132. return true;
  133. }
  134. return false;
  135. }
  136. int getSwapInterval() const { return swapFrames; }
  137. bool createdOk() const noexcept { return true; }
  138. void* getRawContext() const noexcept { return renderContext; }
  139. GLuint getFrameBufferID() const noexcept { return 0; }
  140. struct Locker { Locker (NativeContext&) {} };
  141. private:
  142. GLXContext renderContext;
  143. Window embeddedWindow;
  144. int swapFrames;
  145. Rectangle<int> bounds;
  146. XVisualInfo* bestVisual;
  147. void* contextToShareWith;
  148. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  149. };
  150. //==============================================================================
  151. bool OpenGLHelpers::isContextActive()
  152. {
  153. ScopedXLock xlock;
  154. return glXGetCurrentContext() != 0;
  155. }