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.

213 lines
6.9KB

  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. initialiseGLExtensions();
  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. }
  77. ~WindowedGLContext()
  78. {
  79. ScopedXLock xlock;
  80. deleteContext();
  81. XUnmapWindow (display, embeddedWindow);
  82. XDestroyWindow (display, embeddedWindow);
  83. }
  84. void deleteContext()
  85. {
  86. makeInactive();
  87. if (renderContext != 0)
  88. {
  89. ScopedXLock xlock;
  90. glXDestroyContext (display, renderContext);
  91. renderContext = nullptr;
  92. }
  93. }
  94. bool makeActive() const noexcept
  95. {
  96. jassert (renderContext != 0);
  97. ScopedXLock xlock;
  98. return glXMakeCurrent (display, embeddedWindow, renderContext)
  99. && XSync (display, False);
  100. }
  101. bool makeInactive() const noexcept
  102. {
  103. ScopedXLock xlock;
  104. return (! isActive()) || glXMakeCurrent (display, None, 0);
  105. }
  106. bool isActive() const noexcept
  107. {
  108. ScopedXLock xlock;
  109. return glXGetCurrentContext() == renderContext;
  110. }
  111. void* getRawContext() const noexcept
  112. {
  113. return renderContext;
  114. }
  115. unsigned int getFrameBufferID() const
  116. {
  117. return 0;
  118. }
  119. void swapBuffers()
  120. {
  121. ScopedXLock xlock;
  122. glXSwapBuffers (display, embeddedWindow);
  123. }
  124. bool setSwapInterval (const int newSwapInterval)
  125. {
  126. if (newSwapInterval == swapInterval)
  127. return true;
  128. PFNGLXSWAPINTERVALSGIPROC GLXSwapIntervalSGI
  129. = (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI");
  130. if (GLXSwapIntervalSGI != nullptr)
  131. {
  132. swapInterval = newSwapInterval;
  133. GLXSwapIntervalSGI (newSwapInterval);
  134. return true;
  135. }
  136. return false;
  137. }
  138. int getSwapInterval() const { return swapInterval; }
  139. GLXContext renderContext;
  140. Window embeddedWindow;
  141. private:
  142. int swapInterval;
  143. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  144. };
  145. //==============================================================================
  146. OpenGLContext* OpenGLComponent::createContext()
  147. {
  148. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this, preferredPixelFormat,
  149. contextToShareListsWith != 0 ? (GLXContext) contextToShareListsWith->getRawContext() : 0));
  150. return (c->renderContext != 0) ? c.release() : nullptr;
  151. }
  152. void OpenGLComponent::updateEmbeddedPosition (const Rectangle<int>& bounds)
  153. {
  154. if (context != nullptr)
  155. {
  156. Window embeddedWindow = static_cast<WindowedGLContext*> (context.get())->embeddedWindow;
  157. ScopedXLock xlock;
  158. XMoveResizeWindow (display, embeddedWindow,
  159. bounds.getX(), bounds.getY(), jmax (1, bounds.getWidth()), jmax (1, bounds.getHeight()));
  160. }
  161. }
  162. //==============================================================================
  163. bool OpenGLHelpers::isContextActive()
  164. {
  165. ScopedXLock xlock;
  166. return glXGetCurrentContext() != 0;
  167. }