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.

250 lines
8.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. extern XContext windowHandleXContext;
  16. //==============================================================================
  17. // Defined juce_linux_Windowing.cpp
  18. void juce_LinuxAddRepaintListener (ComponentPeer*, Component* dummy);
  19. void juce_LinuxRemoveRepaintListener (ComponentPeer*, Component* dummy);
  20. //==============================================================================
  21. class OpenGLContext::NativeContext
  22. {
  23. private:
  24. struct DummyComponent : public Component
  25. {
  26. DummyComponent (OpenGLContext::NativeContext& nativeParentContext)
  27. : native (nativeParentContext)
  28. {
  29. }
  30. void handleCommandMessage (int commandId) override
  31. {
  32. if (commandId == 0)
  33. native.triggerRepaint();
  34. }
  35. OpenGLContext::NativeContext& native;
  36. };
  37. public:
  38. NativeContext (Component& comp,
  39. const OpenGLPixelFormat& cPixelFormat,
  40. void* shareContext,
  41. bool /*useMultisampling*/,
  42. OpenGLVersion)
  43. : component (comp), contextToShareWith (shareContext), dummy (*this)
  44. {
  45. display = XWindowSystem::getInstance()->getDisplay();
  46. XWindowSystemUtilities::ScopedXLock xLock;
  47. X11Symbols::getInstance()->xSync (display, False);
  48. GLint attribs[] =
  49. {
  50. GLX_RGBA,
  51. GLX_DOUBLEBUFFER,
  52. GLX_RED_SIZE, cPixelFormat.redBits,
  53. GLX_GREEN_SIZE, cPixelFormat.greenBits,
  54. GLX_BLUE_SIZE, cPixelFormat.blueBits,
  55. GLX_ALPHA_SIZE, cPixelFormat.alphaBits,
  56. GLX_DEPTH_SIZE, cPixelFormat.depthBufferBits,
  57. GLX_STENCIL_SIZE, cPixelFormat.stencilBufferBits,
  58. GLX_ACCUM_RED_SIZE, cPixelFormat.accumulationBufferRedBits,
  59. GLX_ACCUM_GREEN_SIZE, cPixelFormat.accumulationBufferGreenBits,
  60. GLX_ACCUM_BLUE_SIZE, cPixelFormat.accumulationBufferBlueBits,
  61. GLX_ACCUM_ALPHA_SIZE, cPixelFormat.accumulationBufferAlphaBits,
  62. None
  63. };
  64. bestVisual = glXChooseVisual (display, X11Symbols::getInstance()->xDefaultScreen (display), attribs);
  65. if (bestVisual == nullptr)
  66. return;
  67. auto* peer = component.getPeer();
  68. jassert (peer != nullptr);
  69. auto windowH = (Window) peer->getNativeHandle();
  70. auto colourMap = X11Symbols::getInstance()->xCreateColormap (display, windowH, bestVisual->visual, AllocNone);
  71. XSetWindowAttributes swa;
  72. swa.colormap = colourMap;
  73. swa.border_pixel = 0;
  74. swa.event_mask = ExposureMask | StructureNotifyMask;
  75. auto glBounds = component.getTopLevelComponent()
  76. ->getLocalArea (&component, component.getLocalBounds());
  77. glBounds = Desktop::getInstance().getDisplays().logicalToPhysical (glBounds);
  78. embeddedWindow = X11Symbols::getInstance()->xCreateWindow (display, windowH,
  79. glBounds.getX(), glBounds.getY(),
  80. (unsigned int) jmax (1, glBounds.getWidth()),
  81. (unsigned int) jmax (1, glBounds.getHeight()),
  82. 0, bestVisual->depth,
  83. InputOutput,
  84. bestVisual->visual,
  85. CWBorderPixel | CWColormap | CWEventMask,
  86. &swa);
  87. X11Symbols::getInstance()->xSaveContext (display, (XID) embeddedWindow, windowHandleXContext, (XPointer) peer);
  88. X11Symbols::getInstance()->xMapWindow (display, embeddedWindow);
  89. X11Symbols::getInstance()->xFreeColormap (display, colourMap);
  90. X11Symbols::getInstance()->xSync (display, False);
  91. juce_LinuxAddRepaintListener (peer, &dummy);
  92. }
  93. ~NativeContext()
  94. {
  95. juce_LinuxRemoveRepaintListener (component.getPeer(), &dummy);
  96. if (embeddedWindow != 0)
  97. {
  98. XWindowSystemUtilities::ScopedXLock xLock;
  99. X11Symbols::getInstance()->xUnmapWindow (display, embeddedWindow);
  100. X11Symbols::getInstance()->xDestroyWindow (display, embeddedWindow);
  101. }
  102. if (bestVisual != nullptr)
  103. X11Symbols::getInstance()->xFree (bestVisual);
  104. }
  105. bool initialiseOnRenderThread (OpenGLContext& c)
  106. {
  107. XWindowSystemUtilities::ScopedXLock xLock;
  108. renderContext = glXCreateContext (display, bestVisual, (GLXContext) contextToShareWith, GL_TRUE);
  109. c.makeActive();
  110. context = &c;
  111. return true;
  112. }
  113. void shutdownOnRenderThread()
  114. {
  115. XWindowSystemUtilities::ScopedXLock xLock;
  116. context = nullptr;
  117. deactivateCurrentContext();
  118. glXDestroyContext (display, renderContext);
  119. renderContext = nullptr;
  120. }
  121. bool makeActive() const noexcept
  122. {
  123. XWindowSystemUtilities::ScopedXLock xLock;
  124. return renderContext != nullptr
  125. && glXMakeCurrent (display, embeddedWindow, renderContext);
  126. }
  127. bool isActive() const noexcept
  128. {
  129. XWindowSystemUtilities::ScopedXLock xLock;
  130. return glXGetCurrentContext() == renderContext && renderContext != nullptr;
  131. }
  132. static void deactivateCurrentContext()
  133. {
  134. if (auto* display = XWindowSystem::getInstance()->getDisplay())
  135. {
  136. XWindowSystemUtilities::ScopedXLock xLock;
  137. glXMakeCurrent (display, None, nullptr);
  138. }
  139. }
  140. void swapBuffers()
  141. {
  142. XWindowSystemUtilities::ScopedXLock xLock;
  143. glXSwapBuffers (display, embeddedWindow);
  144. }
  145. void updateWindowPosition (Rectangle<int> newBounds)
  146. {
  147. bounds = newBounds;
  148. auto physicalBounds = Desktop::getInstance().getDisplays().logicalToPhysical (bounds);
  149. XWindowSystemUtilities::ScopedXLock xLock;
  150. X11Symbols::getInstance()->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. if (auto GLXSwapIntervalSGI
  160. = (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI"))
  161. {
  162. XWindowSystemUtilities::ScopedXLock xLock;
  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 = 0;
  184. Rectangle<int> bounds;
  185. XVisualInfo* bestVisual = nullptr;
  186. void* contextToShareWith;
  187. OpenGLContext* context = nullptr;
  188. DummyComponent dummy;
  189. ::Display* display = nullptr;
  190. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  191. };
  192. //==============================================================================
  193. bool OpenGLHelpers::isContextActive()
  194. {
  195. XWindowSystemUtilities::ScopedXLock xLock;
  196. return glXGetCurrentContext() != nullptr;
  197. }
  198. } // namespace juce