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.

196 lines
6.6KB

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