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.

211 lines
6.8KB

  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. jassert (component != nullptr);
  32. ComponentPeer* const peer = component->getTopLevelComponent()->getPeer();
  33. if (peer == nullptr)
  34. return;
  35. ScopedXLock xlock;
  36. XSync (display, False);
  37. GLint attribs[] =
  38. {
  39. GLX_RGBA,
  40. GLX_DOUBLEBUFFER,
  41. GLX_RED_SIZE, pixelFormat.redBits,
  42. GLX_GREEN_SIZE, pixelFormat.greenBits,
  43. GLX_BLUE_SIZE, pixelFormat.blueBits,
  44. GLX_ALPHA_SIZE, pixelFormat.alphaBits,
  45. GLX_DEPTH_SIZE, pixelFormat.depthBufferBits,
  46. GLX_STENCIL_SIZE, pixelFormat.stencilBufferBits,
  47. GLX_ACCUM_RED_SIZE, pixelFormat.accumulationBufferRedBits,
  48. GLX_ACCUM_GREEN_SIZE, pixelFormat.accumulationBufferGreenBits,
  49. GLX_ACCUM_BLUE_SIZE, pixelFormat.accumulationBufferBlueBits,
  50. GLX_ACCUM_ALPHA_SIZE, pixelFormat.accumulationBufferAlphaBits,
  51. None
  52. };
  53. XVisualInfo* const bestVisual = glXChooseVisual (display, DefaultScreen (display), attribs);
  54. if (bestVisual == 0)
  55. return;
  56. renderContext = glXCreateContext (display, bestVisual, sharedContext, GL_TRUE);
  57. Window windowH = (Window) peer->getNativeHandle();
  58. Colormap colourMap = XCreateColormap (display, windowH, bestVisual->visual, AllocNone);
  59. XSetWindowAttributes swa;
  60. swa.colormap = colourMap;
  61. swa.border_pixel = 0;
  62. swa.event_mask = ExposureMask | StructureNotifyMask;
  63. embeddedWindow = XCreateWindow (display, windowH,
  64. 0, 0, 1, 1, 0,
  65. 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. XFree (bestVisual);
  74. XSync (display, False);
  75. }
  76. ~WindowedGLContext()
  77. {
  78. ScopedXLock xlock;
  79. deleteContext();
  80. XUnmapWindow (display, embeddedWindow);
  81. XDestroyWindow (display, embeddedWindow);
  82. }
  83. void deleteContext()
  84. {
  85. makeInactive();
  86. if (renderContext != 0)
  87. {
  88. ScopedXLock xlock;
  89. glXDestroyContext (display, renderContext);
  90. renderContext = nullptr;
  91. }
  92. }
  93. bool makeActive() const noexcept
  94. {
  95. jassert (renderContext != 0);
  96. ScopedXLock xlock;
  97. return glXMakeCurrent (display, embeddedWindow, renderContext)
  98. && XSync (display, False);
  99. }
  100. bool makeInactive() const noexcept
  101. {
  102. ScopedXLock xlock;
  103. return (! isActive()) || glXMakeCurrent (display, None, 0);
  104. }
  105. bool isActive() const noexcept
  106. {
  107. ScopedXLock xlock;
  108. return glXGetCurrentContext() == renderContext;
  109. }
  110. void* getRawContext() const noexcept
  111. {
  112. return renderContext;
  113. }
  114. unsigned int getFrameBufferID() const
  115. {
  116. return 0;
  117. }
  118. void swapBuffers()
  119. {
  120. ScopedXLock xlock;
  121. glXSwapBuffers (display, embeddedWindow);
  122. }
  123. bool setSwapInterval (const int newSwapInterval)
  124. {
  125. if (newSwapInterval == swapInterval)
  126. return true;
  127. PFNGLXSWAPINTERVALSGIPROC GLXSwapIntervalSGI
  128. = (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI");
  129. if (GLXSwapIntervalSGI != nullptr)
  130. {
  131. swapInterval = newSwapInterval;
  132. GLXSwapIntervalSGI (newSwapInterval);
  133. return true;
  134. }
  135. return false;
  136. }
  137. int getSwapInterval() const { return swapInterval; }
  138. GLXContext renderContext;
  139. Window embeddedWindow;
  140. private:
  141. int swapInterval;
  142. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  143. };
  144. //==============================================================================
  145. OpenGLContext* OpenGLComponent::createContext()
  146. {
  147. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this, preferredPixelFormat,
  148. contextToShareListsWith != 0 ? (GLXContext) contextToShareListsWith->getRawContext() : 0));
  149. return (c->renderContext != 0) ? c.release() : nullptr;
  150. }
  151. void OpenGLComponent::updateEmbeddedPosition (const Rectangle<int>& bounds)
  152. {
  153. if (context != nullptr)
  154. {
  155. Window embeddedWindow = static_cast<WindowedGLContext*> (context.get())->embeddedWindow;
  156. ScopedXLock xlock;
  157. XMoveResizeWindow (display, embeddedWindow,
  158. bounds.getX(), bounds.getY(), jmax (1, bounds.getWidth()), jmax (1, bounds.getHeight()));
  159. }
  160. }
  161. //==============================================================================
  162. bool OpenGLHelpers::isContextActive()
  163. {
  164. ScopedXLock xlock;
  165. return glXGetCurrentContext() != 0;
  166. }