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.

juce_OpenGL_linux_X11.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. extern XContext windowHandleXContext;
  21. //==============================================================================
  22. // Defined juce_linux_Windowing.cpp
  23. void juce_LinuxAddRepaintListener (ComponentPeer*, Component* dummy);
  24. void juce_LinuxRemoveRepaintListener (ComponentPeer*, Component* dummy);
  25. //==============================================================================
  26. class OpenGLContext::NativeContext
  27. {
  28. private:
  29. struct DummyComponent : public Component
  30. {
  31. DummyComponent (OpenGLContext::NativeContext& nativeParentContext)
  32. : native (nativeParentContext)
  33. {
  34. }
  35. void handleCommandMessage (int commandId) override
  36. {
  37. if (commandId == 0)
  38. native.triggerRepaint();
  39. }
  40. OpenGLContext::NativeContext& native;
  41. };
  42. public:
  43. NativeContext (Component& comp,
  44. const OpenGLPixelFormat& cPixelFormat,
  45. void* shareContext,
  46. bool /*useMultisampling*/,
  47. OpenGLVersion)
  48. : component (comp), contextToShareWith (shareContext), dummy (*this)
  49. {
  50. display = XWindowSystem::getInstance()->getDisplay();
  51. XWindowSystemUtilities::ScopedXLock xLock;
  52. X11Symbols::getInstance()->xSync (display, False);
  53. GLint attribs[] =
  54. {
  55. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  56. GLX_DOUBLEBUFFER, True,
  57. GLX_RED_SIZE, cPixelFormat.redBits,
  58. GLX_GREEN_SIZE, cPixelFormat.greenBits,
  59. GLX_BLUE_SIZE, cPixelFormat.blueBits,
  60. GLX_ALPHA_SIZE, cPixelFormat.alphaBits,
  61. GLX_DEPTH_SIZE, cPixelFormat.depthBufferBits,
  62. GLX_STENCIL_SIZE, cPixelFormat.stencilBufferBits,
  63. GLX_ACCUM_RED_SIZE, cPixelFormat.accumulationBufferRedBits,
  64. GLX_ACCUM_GREEN_SIZE, cPixelFormat.accumulationBufferGreenBits,
  65. GLX_ACCUM_BLUE_SIZE, cPixelFormat.accumulationBufferBlueBits,
  66. GLX_ACCUM_ALPHA_SIZE, cPixelFormat.accumulationBufferAlphaBits,
  67. GLX_X_RENDERABLE, True,
  68. None
  69. };
  70. int countFbConfigs;
  71. fbConfig = glXChooseFBConfig (display, DefaultScreen (display), attribs, &countFbConfigs);
  72. if (fbConfig == nullptr)
  73. return;
  74. bestVisual = glXGetVisualFromFBConfig (display, *fbConfig);
  75. if (bestVisual == nullptr) {
  76. X11Symbols::getInstance()->xFree (fbConfig);
  77. return;
  78. }
  79. auto* peer = component.getPeer();
  80. jassert (peer != nullptr);
  81. auto windowH = (Window) peer->getNativeHandle();
  82. auto colourMap = X11Symbols::getInstance()->xCreateColormap (display, windowH, bestVisual->visual, AllocNone);
  83. XSetWindowAttributes swa;
  84. swa.colormap = colourMap;
  85. swa.border_pixel = 0;
  86. swa.event_mask = embeddedWindowEventMask;
  87. auto glBounds = component.getTopLevelComponent()
  88. ->getLocalArea (&component, component.getLocalBounds());
  89. if (JUCEApplicationBase::isStandaloneApp())
  90. glBounds = Desktop::getInstance().getDisplays().logicalToPhysical (glBounds);
  91. embeddedWindow = X11Symbols::getInstance()->xCreateWindow (display, windowH,
  92. glBounds.getX(), glBounds.getY(),
  93. (unsigned int) jmax (1, glBounds.getWidth()),
  94. (unsigned int) jmax (1, glBounds.getHeight()),
  95. 0, bestVisual->depth,
  96. InputOutput,
  97. bestVisual->visual,
  98. CWBorderPixel | CWColormap | CWEventMask,
  99. &swa);
  100. X11Symbols::getInstance()->xSaveContext (display, (XID) embeddedWindow, windowHandleXContext, (XPointer) peer);
  101. X11Symbols::getInstance()->xMapWindow (display, embeddedWindow);
  102. X11Symbols::getInstance()->xFreeColormap (display, colourMap);
  103. X11Symbols::getInstance()->xSync (display, False);
  104. juce_LinuxAddRepaintListener (peer, &dummy);
  105. }
  106. ~NativeContext()
  107. {
  108. if (auto* peer = component.getPeer())
  109. {
  110. juce_LinuxRemoveRepaintListener (peer, &dummy);
  111. if (embeddedWindow != 0)
  112. {
  113. XWindowSystemUtilities::ScopedXLock xLock;
  114. X11Symbols::getInstance()->xUnmapWindow (display, embeddedWindow);
  115. X11Symbols::getInstance()->xDestroyWindow (display, embeddedWindow);
  116. X11Symbols::getInstance()->xSync (display, False);
  117. XEvent event;
  118. while (X11Symbols::getInstance()->xCheckWindowEvent (display,
  119. embeddedWindow,
  120. embeddedWindowEventMask,
  121. &event) == True)
  122. {
  123. }
  124. }
  125. }
  126. if (fbConfig != nullptr)
  127. X11Symbols::getInstance()->xFree (fbConfig);
  128. if (bestVisual != nullptr)
  129. X11Symbols::getInstance()->xFree (bestVisual);
  130. }
  131. bool initialiseOnRenderThread (OpenGLContext& c)
  132. {
  133. XWindowSystemUtilities::ScopedXLock xLock;
  134. PFNGLXCREATECONTEXTATTRIBSARBPROC createContextAttribs;
  135. int attribs[] = {
  136. GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
  137. GLX_CONTEXT_MINOR_VERSION_ARB, 2,
  138. GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  139. 0
  140. };
  141. createContextAttribs = (PFNGLXCREATECONTEXTATTRIBSARBPROC)
  142. OpenGLHelpers::getExtensionFunction("glXCreateContextAttribsARB");
  143. renderContext = createContextAttribs (display, *fbConfig, (GLXContext) contextToShareWith, GL_TRUE, attribs);
  144. c.makeActive();
  145. context = &c;
  146. return true;
  147. }
  148. void shutdownOnRenderThread()
  149. {
  150. XWindowSystemUtilities::ScopedXLock xLock;
  151. context = nullptr;
  152. deactivateCurrentContext();
  153. glXDestroyContext (display, renderContext);
  154. renderContext = nullptr;
  155. }
  156. bool makeActive() const noexcept
  157. {
  158. XWindowSystemUtilities::ScopedXLock xLock;
  159. return renderContext != nullptr
  160. && glXMakeCurrent (display, embeddedWindow, renderContext);
  161. }
  162. bool isActive() const noexcept
  163. {
  164. XWindowSystemUtilities::ScopedXLock xLock;
  165. return glXGetCurrentContext() == renderContext && renderContext != nullptr;
  166. }
  167. static void deactivateCurrentContext()
  168. {
  169. if (auto* display = XWindowSystem::getInstance()->getDisplay())
  170. {
  171. XWindowSystemUtilities::ScopedXLock xLock;
  172. glXMakeCurrent (display, None, nullptr);
  173. }
  174. }
  175. void swapBuffers()
  176. {
  177. XWindowSystemUtilities::ScopedXLock xLock;
  178. glXSwapBuffers (display, embeddedWindow);
  179. }
  180. void updateWindowPosition (Rectangle<int> newBounds)
  181. {
  182. bounds = newBounds;
  183. auto physicalBounds = Desktop::getInstance().getDisplays().logicalToPhysical (bounds);
  184. XWindowSystemUtilities::ScopedXLock xLock;
  185. if (JUCEApplicationBase::isStandaloneApp())
  186. X11Symbols::getInstance()->xMoveResizeWindow (display, embeddedWindow,
  187. physicalBounds.getX(), physicalBounds.getY(),
  188. (unsigned int) jmax (1, physicalBounds.getWidth()),
  189. (unsigned int) jmax (1, physicalBounds.getHeight()));
  190. else
  191. X11Symbols::getInstance()->xResizeWindow (display, embeddedWindow,
  192. (unsigned int) jmax (1, physicalBounds.getWidth()),
  193. (unsigned int) jmax (1, physicalBounds.getHeight()));
  194. }
  195. bool setSwapInterval (int numFramesPerSwap)
  196. {
  197. if (numFramesPerSwap == swapFrames)
  198. return true;
  199. if (auto GLXSwapIntervalSGI
  200. = (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI"))
  201. {
  202. XWindowSystemUtilities::ScopedXLock xLock;
  203. swapFrames = numFramesPerSwap;
  204. GLXSwapIntervalSGI (numFramesPerSwap);
  205. return true;
  206. }
  207. return false;
  208. }
  209. int getSwapInterval() const { return swapFrames; }
  210. bool createdOk() const noexcept { return true; }
  211. void* getRawContext() const noexcept { return renderContext; }
  212. GLuint getFrameBufferID() const noexcept { return 0; }
  213. void triggerRepaint()
  214. {
  215. if (context != nullptr)
  216. context->triggerRepaint();
  217. }
  218. struct Locker { Locker (NativeContext&) {} };
  219. private:
  220. static constexpr int embeddedWindowEventMask = ExposureMask | StructureNotifyMask;
  221. Component& component;
  222. GLXContext renderContext = {};
  223. Window embeddedWindow = {};
  224. int swapFrames = 1;
  225. Rectangle<int> bounds;
  226. XVisualInfo* bestVisual = nullptr;
  227. GLXFBConfig* fbConfig = nullptr;
  228. void* contextToShareWith;
  229. OpenGLContext* context = nullptr;
  230. DummyComponent dummy;
  231. ::Display* display = nullptr;
  232. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  233. };
  234. //==============================================================================
  235. bool OpenGLHelpers::isContextActive()
  236. {
  237. XWindowSystemUtilities::ScopedXLock xLock;
  238. return glXGetCurrentContext() != nullptr;
  239. }
  240. } // namespace juce