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.

203 lines
6.7KB

  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. pixelFormat (pixelFormat_),
  30. swapInterval (0)
  31. {
  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, 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. OpenGLPixelFormat getPixelFormat() const
  111. {
  112. return pixelFormat;
  113. }
  114. void* getRawContext() const noexcept
  115. {
  116. return renderContext;
  117. }
  118. void updateWindowPosition (const Rectangle<int>& bounds)
  119. {
  120. ScopedXLock xlock;
  121. XMoveResizeWindow (display, embeddedWindow,
  122. bounds.getX(), bounds.getY(), jmax (1, bounds.getWidth()), jmax (1, bounds.getHeight()));
  123. }
  124. void swapBuffers()
  125. {
  126. ScopedXLock xlock;
  127. glXSwapBuffers (display, embeddedWindow);
  128. }
  129. bool setSwapInterval (const int numFramesPerSwap)
  130. {
  131. static PFNGLXSWAPINTERVALSGIPROC GLXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC) glXGetProcAddress ((const GLubyte*) "glXSwapIntervalSGI");
  132. if (GLXSwapIntervalSGI != 0)
  133. {
  134. swapInterval = numFramesPerSwap;
  135. GLXSwapIntervalSGI (numFramesPerSwap);
  136. return true;
  137. }
  138. return false;
  139. }
  140. int getSwapInterval() const { return swapInterval; }
  141. void repaint() {}
  142. //==============================================================================
  143. GLXContext renderContext;
  144. private:
  145. Window embeddedWindow;
  146. OpenGLPixelFormat pixelFormat;
  147. int swapInterval;
  148. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  149. };
  150. //==============================================================================
  151. OpenGLContext* OpenGLComponent::createContext()
  152. {
  153. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this, preferredPixelFormat,
  154. contextToShareListsWith != 0 ? (GLXContext) contextToShareListsWith->getRawContext() : 0));
  155. return (c->renderContext != 0) ? c.release() : nullptr;
  156. }
  157. void OpenGLPixelFormat::getAvailablePixelFormats (Component* component, OwnedArray <OpenGLPixelFormat>& results)
  158. {
  159. results.add (new OpenGLPixelFormat()); // xxx
  160. }