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.

195 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. void* 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, (GLXContext) contextToShareWith, GL_TRUE);
  89. makeActive();
  90. }
  91. void shutdownOnRenderThread()
  92. {
  93. deactivateCurrentContext();
  94. glXDestroyContext (display, renderContext);
  95. renderContext = nullptr;
  96. }
  97. bool makeActive() const noexcept
  98. {
  99. return renderContext != 0
  100. && glXMakeCurrent (display, embeddedWindow, renderContext);
  101. }
  102. bool isActive() const noexcept
  103. {
  104. return glXGetCurrentContext() == renderContext && renderContext != 0;
  105. }
  106. static void deactivateCurrentContext()
  107. {
  108. glXMakeCurrent (display, None, 0);
  109. }
  110. void swapBuffers()
  111. {
  112. glXSwapBuffers (display, embeddedWindow);
  113. }
  114. void updateWindowPosition (const Rectangle<int>& newBounds)
  115. {
  116. bounds = newBounds;
  117. ScopedXLock xlock;
  118. XMoveResizeWindow (display, embeddedWindow,
  119. bounds.getX(), bounds.getY(),
  120. jmax (1, bounds.getWidth()),
  121. jmax (1, bounds.getHeight()));
  122. }
  123. bool setSwapInterval (int numFramesPerSwap)
  124. {
  125. if (numFramesPerSwap == swapFrames)
  126. return true;
  127. PFNGLXSWAPINTERVALSGIPROC GLXSwapIntervalSGI
  128. = (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI");
  129. if (GLXSwapIntervalSGI != nullptr)
  130. {
  131. swapFrames = numFramesPerSwap;
  132. GLXSwapIntervalSGI (numFramesPerSwap);
  133. return true;
  134. }
  135. return false;
  136. }
  137. int getSwapInterval() const { return swapFrames; }
  138. bool createdOk() const noexcept { return true; }
  139. void* getRawContext() const noexcept { return renderContext; }
  140. GLuint getFrameBufferID() const noexcept { return 0; }
  141. struct Locker { Locker (NativeContext&) {} };
  142. private:
  143. GLXContext renderContext;
  144. Window embeddedWindow;
  145. int swapFrames;
  146. Rectangle<int> bounds;
  147. XVisualInfo* bestVisual;
  148. void* 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. }