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.

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