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.

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