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.

280 lines
9.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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 useMultisamplingIn,
  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. const std::vector<GLint> optionalAttribs
  49. {
  50. GLX_SAMPLE_BUFFERS, useMultisamplingIn ? 1 : 0,
  51. GLX_SAMPLES, cPixelFormat.multisamplingLevel
  52. };
  53. if (! tryChooseVisual (cPixelFormat, optionalAttribs) && ! tryChooseVisual (cPixelFormat, {}))
  54. return;
  55. auto* peer = component.getPeer();
  56. jassert (peer != nullptr);
  57. auto windowH = (Window) peer->getNativeHandle();
  58. auto colourMap = X11Symbols::getInstance()->xCreateColormap (display, windowH, bestVisual->visual, AllocNone);
  59. XSetWindowAttributes swa;
  60. swa.colormap = colourMap;
  61. swa.border_pixel = 0;
  62. swa.event_mask = embeddedWindowEventMask;
  63. auto glBounds = component.getTopLevelComponent()
  64. ->getLocalArea (&component, component.getLocalBounds());
  65. glBounds = Desktop::getInstance().getDisplays().logicalToPhysical (glBounds);
  66. embeddedWindow = X11Symbols::getInstance()->xCreateWindow (display, windowH,
  67. glBounds.getX(), glBounds.getY(),
  68. (unsigned int) jmax (1, glBounds.getWidth()),
  69. (unsigned int) jmax (1, glBounds.getHeight()),
  70. 0, bestVisual->depth,
  71. InputOutput,
  72. bestVisual->visual,
  73. CWBorderPixel | CWColormap | CWEventMask,
  74. &swa);
  75. X11Symbols::getInstance()->xSaveContext (display, (XID) embeddedWindow, windowHandleXContext, (XPointer) peer);
  76. X11Symbols::getInstance()->xMapWindow (display, embeddedWindow);
  77. X11Symbols::getInstance()->xFreeColormap (display, colourMap);
  78. X11Symbols::getInstance()->xSync (display, False);
  79. juce_LinuxAddRepaintListener (peer, &dummy);
  80. }
  81. ~NativeContext()
  82. {
  83. if (auto* peer = component.getPeer())
  84. {
  85. juce_LinuxRemoveRepaintListener (peer, &dummy);
  86. if (embeddedWindow != 0)
  87. {
  88. XWindowSystemUtilities::ScopedXLock xLock;
  89. X11Symbols::getInstance()->xUnmapWindow (display, embeddedWindow);
  90. X11Symbols::getInstance()->xDestroyWindow (display, embeddedWindow);
  91. X11Symbols::getInstance()->xSync (display, False);
  92. XEvent event;
  93. while (X11Symbols::getInstance()->xCheckWindowEvent (display,
  94. embeddedWindow,
  95. embeddedWindowEventMask,
  96. &event) == True)
  97. {
  98. }
  99. }
  100. }
  101. if (bestVisual != nullptr)
  102. X11Symbols::getInstance()->xFree (bestVisual);
  103. }
  104. bool initialiseOnRenderThread (OpenGLContext& c)
  105. {
  106. XWindowSystemUtilities::ScopedXLock xLock;
  107. renderContext = glXCreateContext (display, bestVisual, (GLXContext) contextToShareWith, GL_TRUE);
  108. c.makeActive();
  109. context = &c;
  110. return true;
  111. }
  112. void shutdownOnRenderThread()
  113. {
  114. XWindowSystemUtilities::ScopedXLock xLock;
  115. context = nullptr;
  116. deactivateCurrentContext();
  117. glXDestroyContext (display, renderContext);
  118. renderContext = nullptr;
  119. }
  120. bool makeActive() const noexcept
  121. {
  122. XWindowSystemUtilities::ScopedXLock xLock;
  123. return renderContext != nullptr
  124. && glXMakeCurrent (display, embeddedWindow, renderContext);
  125. }
  126. bool isActive() const noexcept
  127. {
  128. XWindowSystemUtilities::ScopedXLock xLock;
  129. return glXGetCurrentContext() == renderContext && renderContext != nullptr;
  130. }
  131. static void deactivateCurrentContext()
  132. {
  133. if (auto* display = XWindowSystem::getInstance()->getDisplay())
  134. {
  135. XWindowSystemUtilities::ScopedXLock xLock;
  136. glXMakeCurrent (display, None, nullptr);
  137. }
  138. }
  139. void swapBuffers()
  140. {
  141. XWindowSystemUtilities::ScopedXLock xLock;
  142. glXSwapBuffers (display, embeddedWindow);
  143. }
  144. void updateWindowPosition (Rectangle<int> newBounds)
  145. {
  146. bounds = newBounds;
  147. auto physicalBounds = Desktop::getInstance().getDisplays().logicalToPhysical (bounds);
  148. XWindowSystemUtilities::ScopedXLock xLock;
  149. X11Symbols::getInstance()->xMoveResizeWindow (display, embeddedWindow,
  150. physicalBounds.getX(), physicalBounds.getY(),
  151. (unsigned int) jmax (1, physicalBounds.getWidth()),
  152. (unsigned int) jmax (1, physicalBounds.getHeight()));
  153. }
  154. bool setSwapInterval (int numFramesPerSwap)
  155. {
  156. if (numFramesPerSwap == swapFrames)
  157. return true;
  158. if (auto GLXSwapIntervalSGI
  159. = (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI"))
  160. {
  161. XWindowSystemUtilities::ScopedXLock xLock;
  162. swapFrames = numFramesPerSwap;
  163. GLXSwapIntervalSGI (numFramesPerSwap);
  164. return true;
  165. }
  166. return false;
  167. }
  168. int getSwapInterval() const { return swapFrames; }
  169. bool createdOk() const noexcept { return true; }
  170. void* getRawContext() const noexcept { return renderContext; }
  171. GLuint getFrameBufferID() const noexcept { return 0; }
  172. void triggerRepaint()
  173. {
  174. if (context != nullptr)
  175. context->triggerRepaint();
  176. }
  177. struct Locker { Locker (NativeContext&) {} };
  178. private:
  179. bool tryChooseVisual (const OpenGLPixelFormat& format, const std::vector<GLint>& optionalAttribs)
  180. {
  181. std::vector<GLint> allAttribs
  182. {
  183. GLX_RGBA,
  184. GLX_DOUBLEBUFFER,
  185. GLX_RED_SIZE, format.redBits,
  186. GLX_GREEN_SIZE, format.greenBits,
  187. GLX_BLUE_SIZE, format.blueBits,
  188. GLX_ALPHA_SIZE, format.alphaBits,
  189. GLX_DEPTH_SIZE, format.depthBufferBits,
  190. GLX_STENCIL_SIZE, format.stencilBufferBits,
  191. GLX_ACCUM_RED_SIZE, format.accumulationBufferRedBits,
  192. GLX_ACCUM_GREEN_SIZE, format.accumulationBufferGreenBits,
  193. GLX_ACCUM_BLUE_SIZE, format.accumulationBufferBlueBits,
  194. GLX_ACCUM_ALPHA_SIZE, format.accumulationBufferAlphaBits
  195. };
  196. allAttribs.insert (allAttribs.end(), optionalAttribs.begin(), optionalAttribs.end());
  197. allAttribs.push_back (None);
  198. bestVisual = glXChooseVisual (display, X11Symbols::getInstance()->xDefaultScreen (display), allAttribs.data());
  199. return bestVisual != nullptr;
  200. }
  201. static constexpr int embeddedWindowEventMask = ExposureMask | StructureNotifyMask;
  202. Component& component;
  203. GLXContext renderContext = {};
  204. Window embeddedWindow = {};
  205. int swapFrames = 1;
  206. Rectangle<int> bounds;
  207. XVisualInfo* bestVisual = nullptr;
  208. void* contextToShareWith;
  209. OpenGLContext* context = nullptr;
  210. DummyComponent dummy;
  211. ::Display* display = nullptr;
  212. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  213. };
  214. //==============================================================================
  215. bool OpenGLHelpers::isContextActive()
  216. {
  217. XWindowSystemUtilities::ScopedXLock xLock;
  218. return glXGetCurrentContext() != nullptr;
  219. }
  220. } // namespace juce