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.

207 lines
7.1KB

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