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.

192 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 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. glXMakeCurrent (display, None, 0);
  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 isActive() const noexcept
  105. {
  106. return glXGetCurrentContext() == renderContext && renderContext != 0;
  107. }
  108. void swapBuffers()
  109. {
  110. glXSwapBuffers (display, embeddedWindow);
  111. }
  112. void updateWindowPosition (const Rectangle<int>& newBounds)
  113. {
  114. bounds = newBounds;
  115. ScopedXLock xlock;
  116. XMoveResizeWindow (display, embeddedWindow,
  117. bounds.getX(), bounds.getY(),
  118. jmax (1, bounds.getWidth()),
  119. jmax (1, bounds.getHeight()));
  120. }
  121. bool setSwapInterval (int numFramesPerSwap)
  122. {
  123. if (numFramesPerSwap == swapFrames)
  124. return true;
  125. PFNGLXSWAPINTERVALSGIPROC GLXSwapIntervalSGI
  126. = (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI");
  127. if (GLXSwapIntervalSGI != nullptr)
  128. {
  129. swapFrames = numFramesPerSwap;
  130. GLXSwapIntervalSGI (numFramesPerSwap);
  131. return true;
  132. }
  133. return false;
  134. }
  135. int getSwapInterval() const { return swapFrames; }
  136. bool createdOk() const noexcept { return true; }
  137. void* getRawContext() const noexcept { return renderContext; }
  138. GLuint getFrameBufferID() const noexcept { return 0; }
  139. struct Locker { Locker (NativeContext&) {} };
  140. private:
  141. GLXContext renderContext;
  142. Window embeddedWindow;
  143. int swapFrames;
  144. Rectangle<int> bounds;
  145. XVisualInfo* bestVisual;
  146. const NativeContext* contextToShareWith;
  147. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext);
  148. };
  149. //==============================================================================
  150. bool OpenGLHelpers::isContextActive()
  151. {
  152. ScopedXLock xlock;
  153. return glXGetCurrentContext() != 0;
  154. }