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.

241 lines
8.1KB

  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 ::Display* display;
  18. extern XContext windowHandleXContext;
  19. //==============================================================================
  20. // Defined juce_linux_Windowing.cpp
  21. Rectangle<int> juce_LinuxScaledToPhysicalBounds (ComponentPeer* peer, const Rectangle<int>& bounds);
  22. void juce_LinuxAddRepaintListener (ComponentPeer* peer, Component* dummy);
  23. void juce_LinuxRemoveRepaintListener (ComponentPeer* peer, Component* dummy);
  24. //==============================================================================
  25. class OpenGLContext::NativeContext
  26. {
  27. private:
  28. class DummyComponent : public Component
  29. {
  30. public:
  31. DummyComponent (OpenGLContext::NativeContext& nativeParentContext)
  32. : native (nativeParentContext)
  33. {
  34. }
  35. void handleCommandMessage (int commandId) override
  36. {
  37. if (commandId == 0)
  38. native.triggerRepaint();
  39. }
  40. private:
  41. OpenGLContext::NativeContext& native;
  42. };
  43. public:
  44. NativeContext (Component& comp,
  45. const OpenGLPixelFormat& cPixelFormat,
  46. void* shareContext,
  47. bool /*useMultisampling*/,
  48. OpenGLVersion)
  49. : component (comp), renderContext (0), embeddedWindow (0), swapFrames (0), bestVisual (0),
  50. contextToShareWith (shareContext), context (nullptr), dummy (*this)
  51. {
  52. ScopedXLock xlock;
  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;
  104. XUnmapWindow (display, embeddedWindow);
  105. XDestroyWindow (display, embeddedWindow);
  106. }
  107. if (bestVisual != nullptr)
  108. XFree (bestVisual);
  109. }
  110. void initialiseOnRenderThread (OpenGLContext& c)
  111. {
  112. ScopedXLock xlock;
  113. renderContext = glXCreateContext (display, bestVisual, (GLXContext) contextToShareWith, GL_TRUE);
  114. c.makeActive();
  115. context = &c;
  116. }
  117. void shutdownOnRenderThread()
  118. {
  119. context = nullptr;
  120. deactivateCurrentContext();
  121. glXDestroyContext (display, renderContext);
  122. renderContext = nullptr;
  123. }
  124. bool makeActive() const noexcept
  125. {
  126. return renderContext != 0
  127. && glXMakeCurrent (display, embeddedWindow, renderContext);
  128. }
  129. bool isActive() const noexcept
  130. {
  131. return glXGetCurrentContext() == renderContext && renderContext != 0;
  132. }
  133. static void deactivateCurrentContext()
  134. {
  135. glXMakeCurrent (display, None, 0);
  136. }
  137. void swapBuffers()
  138. {
  139. glXSwapBuffers (display, embeddedWindow);
  140. }
  141. void updateWindowPosition (const Rectangle<int>& newBounds)
  142. {
  143. bounds = newBounds;
  144. const Rectangle<int> physicalBounds =
  145. juce_LinuxScaledToPhysicalBounds (component.getPeer(), bounds);
  146. ScopedXLock xlock;
  147. XMoveResizeWindow (display, embeddedWindow,
  148. physicalBounds.getX(), physicalBounds.getY(),
  149. (unsigned int) jmax (1, physicalBounds.getWidth()),
  150. (unsigned int) jmax (1, physicalBounds.getHeight()));
  151. }
  152. bool setSwapInterval (int numFramesPerSwap)
  153. {
  154. if (numFramesPerSwap == swapFrames)
  155. return true;
  156. PFNGLXSWAPINTERVALSGIPROC GLXSwapIntervalSGI
  157. = (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI");
  158. if (GLXSwapIntervalSGI != nullptr)
  159. {
  160. swapFrames = numFramesPerSwap;
  161. GLXSwapIntervalSGI (numFramesPerSwap);
  162. return true;
  163. }
  164. return false;
  165. }
  166. int getSwapInterval() const { return swapFrames; }
  167. bool createdOk() const noexcept { return true; }
  168. void* getRawContext() const noexcept { return renderContext; }
  169. GLuint getFrameBufferID() const noexcept { return 0; }
  170. void triggerRepaint()
  171. {
  172. if (context != nullptr)
  173. context->triggerRepaint();
  174. }
  175. struct Locker { Locker (NativeContext&) {} };
  176. private:
  177. Component& component;
  178. GLXContext renderContext;
  179. Window embeddedWindow;
  180. int swapFrames;
  181. Rectangle<int> bounds;
  182. XVisualInfo* bestVisual;
  183. void* contextToShareWith;
  184. OpenGLContext* context;
  185. DummyComponent dummy;
  186. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  187. };
  188. //==============================================================================
  189. bool OpenGLHelpers::isContextActive()
  190. {
  191. ScopedXLock xlock;
  192. return glXGetCurrentContext() != 0;
  193. }