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.

219 lines
7.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. extern Display* display;
  19. extern XContext windowHandleXContext;
  20. //==============================================================================
  21. class WindowedGLContext : public OpenGLContext
  22. {
  23. public:
  24. WindowedGLContext (Component* const component,
  25. const OpenGLPixelFormat& pixelFormat,
  26. GLXContext sharedContext)
  27. : renderContext (0),
  28. embeddedWindow (0),
  29. swapInterval (0)
  30. {
  31. extensions.initialise();
  32. jassert (component != nullptr);
  33. ComponentPeer* const peer = component->getTopLevelComponent()->getPeer();
  34. if (peer == nullptr)
  35. return;
  36. ScopedXLock xlock;
  37. XSync (display, False);
  38. GLint attribs[] =
  39. {
  40. GLX_RGBA,
  41. GLX_DOUBLEBUFFER,
  42. GLX_RED_SIZE, pixelFormat.redBits,
  43. GLX_GREEN_SIZE, pixelFormat.greenBits,
  44. GLX_BLUE_SIZE, pixelFormat.blueBits,
  45. GLX_ALPHA_SIZE, pixelFormat.alphaBits,
  46. GLX_DEPTH_SIZE, pixelFormat.depthBufferBits,
  47. GLX_STENCIL_SIZE, pixelFormat.stencilBufferBits,
  48. GLX_ACCUM_RED_SIZE, pixelFormat.accumulationBufferRedBits,
  49. GLX_ACCUM_GREEN_SIZE, pixelFormat.accumulationBufferGreenBits,
  50. GLX_ACCUM_BLUE_SIZE, pixelFormat.accumulationBufferBlueBits,
  51. GLX_ACCUM_ALPHA_SIZE, pixelFormat.accumulationBufferAlphaBits,
  52. None
  53. };
  54. XVisualInfo* const bestVisual = glXChooseVisual (display, DefaultScreen (display), attribs);
  55. if (bestVisual == 0)
  56. return;
  57. renderContext = glXCreateContext (display, bestVisual, sharedContext, GL_TRUE);
  58. Window windowH = (Window) peer->getNativeHandle();
  59. Colormap colourMap = XCreateColormap (display, windowH, bestVisual->visual, AllocNone);
  60. XSetWindowAttributes swa;
  61. swa.colormap = colourMap;
  62. swa.border_pixel = 0;
  63. swa.event_mask = ExposureMask | StructureNotifyMask;
  64. embeddedWindow = XCreateWindow (display, windowH,
  65. 0, 0, 1, 1, 0,
  66. bestVisual->depth,
  67. InputOutput,
  68. bestVisual->visual,
  69. CWBorderPixel | CWColormap | CWEventMask,
  70. &swa);
  71. XSaveContext (display, (XID) embeddedWindow, windowHandleXContext, (XPointer) peer);
  72. XMapWindow (display, embeddedWindow);
  73. XFreeColormap (display, colourMap);
  74. XFree (bestVisual);
  75. XSync (display, False);
  76. makeActive();
  77. extensions.initialise();
  78. makeInactive();
  79. }
  80. ~WindowedGLContext()
  81. {
  82. ScopedXLock xlock;
  83. deleteContext();
  84. XUnmapWindow (display, embeddedWindow);
  85. XDestroyWindow (display, embeddedWindow);
  86. }
  87. void deleteContext()
  88. {
  89. makeInactive();
  90. if (renderContext != 0)
  91. {
  92. ScopedXLock xlock;
  93. glXDestroyContext (display, renderContext);
  94. renderContext = nullptr;
  95. }
  96. }
  97. bool makeActive() const noexcept
  98. {
  99. jassert (renderContext != 0);
  100. ScopedXLock xlock;
  101. return glXMakeCurrent (display, embeddedWindow, renderContext)
  102. && XSync (display, False);
  103. }
  104. bool makeInactive() const noexcept
  105. {
  106. ScopedXLock xlock;
  107. return (! isActive()) || glXMakeCurrent (display, None, 0);
  108. }
  109. bool isActive() const noexcept
  110. {
  111. ScopedXLock xlock;
  112. return glXGetCurrentContext() == renderContext;
  113. }
  114. void* getRawContext() const noexcept
  115. {
  116. return renderContext;
  117. }
  118. unsigned int getFrameBufferID() const
  119. {
  120. return 0;
  121. }
  122. void swapBuffers()
  123. {
  124. ScopedXLock xlock;
  125. glXSwapBuffers (display, embeddedWindow);
  126. }
  127. bool setSwapInterval (const int newSwapInterval)
  128. {
  129. if (newSwapInterval == swapInterval)
  130. return true;
  131. PFNGLXSWAPINTERVALSGIPROC GLXSwapIntervalSGI
  132. = (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI");
  133. if (GLXSwapIntervalSGI != nullptr)
  134. {
  135. swapInterval = newSwapInterval;
  136. GLXSwapIntervalSGI (newSwapInterval);
  137. return true;
  138. }
  139. return false;
  140. }
  141. int getSwapInterval() const { return swapInterval; }
  142. GLXContext renderContext;
  143. Window embeddedWindow;
  144. private:
  145. int swapInterval;
  146. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  147. };
  148. //==============================================================================
  149. OpenGLContext* OpenGLComponent::createContext()
  150. {
  151. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this, preferredPixelFormat,
  152. contextToShareListsWith != 0 ? (GLXContext) contextToShareListsWith->getRawContext() : 0));
  153. return (c->renderContext != 0) ? c.release() : nullptr;
  154. }
  155. void OpenGLComponent::updateEmbeddedPosition (const Rectangle<int>& bounds)
  156. {
  157. const ScopedLock sl (contextLock);
  158. if (context != nullptr)
  159. {
  160. Window embeddedWindow = static_cast<WindowedGLContext*> (context.get())->embeddedWindow;
  161. ScopedXLock xlock;
  162. XMoveResizeWindow (display, embeddedWindow,
  163. bounds.getX(), bounds.getY(), jmax (1, bounds.getWidth()), jmax (1, bounds.getHeight()));
  164. }
  165. }
  166. //==============================================================================
  167. bool OpenGLHelpers::isContextActive()
  168. {
  169. ScopedXLock xlock;
  170. return glXGetCurrentContext() != 0;
  171. }