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.

208 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. properties.clear(); // to release any stored programs, etc that may be held in properties.
  84. makeInactive();
  85. if (renderContext != 0)
  86. {
  87. ScopedXLock xlock;
  88. glXDestroyContext (display, renderContext);
  89. renderContext = nullptr;
  90. }
  91. XUnmapWindow (display, embeddedWindow);
  92. XDestroyWindow (display, embeddedWindow);
  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. unsigned int getFrameBufferID() const { return 0; }
  112. void* getRawContext() const noexcept { return renderContext; }
  113. int getWidth() const { return bounds.getWidth(); }
  114. int getHeight() const { return bounds.getHeight(); }
  115. void updateWindowPosition (const Rectangle<int>& newBounds)
  116. {
  117. bounds = newBounds;
  118. ScopedXLock xlock;
  119. XMoveResizeWindow (display, embeddedWindow,
  120. bounds.getX(), bounds.getY(), jmax (1, bounds.getWidth()), jmax (1, bounds.getHeight()));
  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. Rectangle<int> bounds;
  147. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  148. };
  149. //==============================================================================
  150. OpenGLContext* OpenGLComponent::createContext()
  151. {
  152. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this, preferredPixelFormat,
  153. contextToShareListsWith != 0 ? (GLXContext) contextToShareListsWith->getRawContext() : 0));
  154. return (c->renderContext != 0) ? c.release() : nullptr;
  155. }
  156. //==============================================================================
  157. bool OpenGLHelpers::isContextActive()
  158. {
  159. ScopedXLock xlock;
  160. return glXGetCurrentContext() != 0;
  161. }