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.

251 lines
8.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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 XContext windowHandleXContext;
  18. //==============================================================================
  19. // Defined juce_linux_Windowing.cpp
  20. Rectangle<int> juce_LinuxScaledToPhysicalBounds (ComponentPeer* peer, const Rectangle<int>& bounds);
  21. void juce_LinuxAddRepaintListener (ComponentPeer* peer, Component* dummy);
  22. void juce_LinuxRemoveRepaintListener (ComponentPeer* peer, Component* dummy);
  23. //==============================================================================
  24. class OpenGLContext::NativeContext
  25. {
  26. private:
  27. class DummyComponent : public Component
  28. {
  29. public:
  30. DummyComponent (OpenGLContext::NativeContext& nativeParentContext)
  31. : native (nativeParentContext)
  32. {
  33. }
  34. void handleCommandMessage (int commandId) override
  35. {
  36. if (commandId == 0)
  37. native.triggerRepaint();
  38. }
  39. private:
  40. OpenGLContext::NativeContext& native;
  41. };
  42. public:
  43. NativeContext (Component& comp,
  44. const OpenGLPixelFormat& cPixelFormat,
  45. void* shareContext,
  46. bool /*useMultisampling*/,
  47. OpenGLVersion)
  48. : component (comp), renderContext (0), embeddedWindow (0), swapFrames (0), bestVisual (0),
  49. contextToShareWith (shareContext), context (nullptr), dummy (*this)
  50. {
  51. display = XWindowSystem::getInstance()->displayRef();
  52. ScopedXLock xlock (display);
  53. XSync (display, False);
  54. GLint attribs[] =
  55. {
  56. GLX_RGBA,
  57. GLX_DOUBLEBUFFER,
  58. GLX_RED_SIZE, cPixelFormat.redBits,
  59. GLX_GREEN_SIZE, cPixelFormat.greenBits,
  60. GLX_BLUE_SIZE, cPixelFormat.blueBits,
  61. GLX_ALPHA_SIZE, cPixelFormat.alphaBits,
  62. GLX_DEPTH_SIZE, cPixelFormat.depthBufferBits,
  63. GLX_STENCIL_SIZE, cPixelFormat.stencilBufferBits,
  64. GLX_ACCUM_RED_SIZE, cPixelFormat.accumulationBufferRedBits,
  65. GLX_ACCUM_GREEN_SIZE, cPixelFormat.accumulationBufferGreenBits,
  66. GLX_ACCUM_BLUE_SIZE, cPixelFormat.accumulationBufferBlueBits,
  67. GLX_ACCUM_ALPHA_SIZE, cPixelFormat.accumulationBufferAlphaBits,
  68. None
  69. };
  70. bestVisual = glXChooseVisual (display, DefaultScreen (display), attribs);
  71. if (bestVisual == nullptr)
  72. return;
  73. ComponentPeer* const peer = component.getPeer();
  74. Window windowH = (Window) peer->getNativeHandle();
  75. Colormap colourMap = XCreateColormap (display, windowH, bestVisual->visual, AllocNone);
  76. XSetWindowAttributes swa;
  77. swa.colormap = colourMap;
  78. swa.border_pixel = 0;
  79. swa.event_mask = ExposureMask | StructureNotifyMask;
  80. Rectangle<int> glBounds (component.getTopLevelComponent()
  81. ->getLocalArea (&component, component.getLocalBounds()));
  82. glBounds = juce_LinuxScaledToPhysicalBounds (peer, glBounds);
  83. embeddedWindow = XCreateWindow (display, windowH,
  84. glBounds.getX(), glBounds.getY(),
  85. (unsigned int) jmax (1, glBounds.getWidth()),
  86. (unsigned int) jmax (1, glBounds.getHeight()),
  87. 0, bestVisual->depth,
  88. InputOutput,
  89. bestVisual->visual,
  90. CWBorderPixel | CWColormap | CWEventMask,
  91. &swa);
  92. XSaveContext (display, (XID) embeddedWindow, windowHandleXContext, (XPointer) peer);
  93. XMapWindow (display, embeddedWindow);
  94. XFreeColormap (display, colourMap);
  95. XSync (display, False);
  96. juce_LinuxAddRepaintListener (peer, &dummy);
  97. }
  98. ~NativeContext()
  99. {
  100. juce_LinuxRemoveRepaintListener (component.getPeer(), &dummy);
  101. if (embeddedWindow != 0)
  102. {
  103. ScopedXLock xlock (display);
  104. XUnmapWindow (display, embeddedWindow);
  105. XDestroyWindow (display, embeddedWindow);
  106. }
  107. if (bestVisual != nullptr)
  108. XFree (bestVisual);
  109. XWindowSystem::getInstance()->displayUnref();
  110. }
  111. void initialiseOnRenderThread (OpenGLContext& c)
  112. {
  113. ScopedXLock xlock (display);
  114. renderContext = glXCreateContext (display, bestVisual, (GLXContext) contextToShareWith, GL_TRUE);
  115. c.makeActive();
  116. context = &c;
  117. }
  118. void shutdownOnRenderThread()
  119. {
  120. context = nullptr;
  121. deactivateCurrentContext();
  122. glXDestroyContext (display, renderContext);
  123. renderContext = nullptr;
  124. }
  125. bool makeActive() const noexcept
  126. {
  127. return renderContext != 0
  128. && glXMakeCurrent (display, embeddedWindow, renderContext);
  129. }
  130. bool isActive() const noexcept
  131. {
  132. return glXGetCurrentContext() == renderContext && renderContext != 0;
  133. }
  134. static void deactivateCurrentContext()
  135. {
  136. ScopedXDisplay xDisplay;
  137. ::Display* display = xDisplay.get();
  138. glXMakeCurrent (display, None, 0);
  139. }
  140. void swapBuffers()
  141. {
  142. glXSwapBuffers (display, embeddedWindow);
  143. }
  144. void updateWindowPosition (const Rectangle<int>& newBounds)
  145. {
  146. bounds = newBounds;
  147. const Rectangle<int> physicalBounds =
  148. juce_LinuxScaledToPhysicalBounds (component.getPeer(), bounds);
  149. ScopedXLock xlock (display);
  150. XMoveResizeWindow (display, embeddedWindow,
  151. physicalBounds.getX(), physicalBounds.getY(),
  152. (unsigned int) jmax (1, physicalBounds.getWidth()),
  153. (unsigned int) jmax (1, physicalBounds.getHeight()));
  154. }
  155. bool setSwapInterval (int numFramesPerSwap)
  156. {
  157. if (numFramesPerSwap == swapFrames)
  158. return true;
  159. PFNGLXSWAPINTERVALSGIPROC GLXSwapIntervalSGI
  160. = (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI");
  161. if (GLXSwapIntervalSGI != nullptr)
  162. {
  163. swapFrames = numFramesPerSwap;
  164. GLXSwapIntervalSGI (numFramesPerSwap);
  165. return true;
  166. }
  167. return false;
  168. }
  169. int getSwapInterval() const { return swapFrames; }
  170. bool createdOk() const noexcept { return true; }
  171. void* getRawContext() const noexcept { return renderContext; }
  172. GLuint getFrameBufferID() const noexcept { return 0; }
  173. void triggerRepaint()
  174. {
  175. if (context != nullptr)
  176. context->triggerRepaint();
  177. }
  178. struct Locker { Locker (NativeContext&) {} };
  179. private:
  180. Component& component;
  181. GLXContext renderContext;
  182. Window embeddedWindow;
  183. int swapFrames;
  184. Rectangle<int> bounds;
  185. XVisualInfo* bestVisual;
  186. void* contextToShareWith;
  187. OpenGLContext* context;
  188. DummyComponent dummy;
  189. ::Display* display;
  190. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  191. };
  192. //==============================================================================
  193. bool OpenGLHelpers::isContextActive()
  194. {
  195. ScopedXDisplay xDisplay;
  196. ::Display* display = xDisplay.get();
  197. ScopedXLock xlock (display);
  198. return glXGetCurrentContext() != 0;
  199. }