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.

330 lines
12KB

  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 ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component&, void* parent);
  21. //==============================================================================
  22. class OpenGLContext::NativeContext : private ComponentPeer::ScaleFactorListener
  23. {
  24. public:
  25. NativeContext (Component& component,
  26. const OpenGLPixelFormat& pixelFormat,
  27. void* contextToShareWithIn,
  28. bool /*useMultisampling*/,
  29. OpenGLVersion)
  30. {
  31. dummyComponent.reset (new DummyComponent (*this));
  32. createNativeWindow (component);
  33. PIXELFORMATDESCRIPTOR pfd;
  34. initialisePixelFormatDescriptor (pfd, pixelFormat);
  35. auto pixFormat = ChoosePixelFormat (dc, &pfd);
  36. if (pixFormat != 0)
  37. SetPixelFormat (dc, pixFormat, &pfd);
  38. renderContext = wglCreateContext (dc);
  39. if (renderContext != nullptr)
  40. {
  41. makeActive();
  42. initialiseGLExtensions();
  43. auto wglFormat = wglChoosePixelFormatExtension (pixelFormat);
  44. deactivateCurrentContext();
  45. if (wglFormat != pixFormat && wglFormat != 0)
  46. {
  47. // can't change the pixel format of a window, so need to delete the
  48. // old one and create a new one..
  49. releaseDC();
  50. nativeWindow = nullptr;
  51. createNativeWindow (component);
  52. if (SetPixelFormat (dc, wglFormat, &pfd))
  53. {
  54. deleteRenderContext();
  55. renderContext = wglCreateContext (dc);
  56. }
  57. }
  58. if (contextToShareWithIn != nullptr)
  59. wglShareLists ((HGLRC) contextToShareWithIn, renderContext);
  60. component.getTopLevelComponent()->repaint();
  61. component.repaint();
  62. }
  63. }
  64. ~NativeContext() override
  65. {
  66. deleteRenderContext();
  67. releaseDC();
  68. if (safeComponent != nullptr)
  69. if (auto* peer = safeComponent->getTopLevelComponent()->getPeer())
  70. peer->removeScaleFactorListener (this);
  71. }
  72. bool initialiseOnRenderThread (OpenGLContext& c)
  73. {
  74. threadAwarenessSetter = std::make_unique<ScopedThreadDPIAwarenessSetter> (nativeWindow->getNativeHandle());
  75. context = &c;
  76. return true;
  77. }
  78. void shutdownOnRenderThread()
  79. {
  80. deactivateCurrentContext();
  81. context = nullptr;
  82. threadAwarenessSetter = nullptr;
  83. }
  84. static void deactivateCurrentContext() { wglMakeCurrent (nullptr, nullptr); }
  85. bool makeActive() const noexcept { return isActive() || wglMakeCurrent (dc, renderContext) != FALSE; }
  86. bool isActive() const noexcept { return wglGetCurrentContext() == renderContext; }
  87. void swapBuffers() const noexcept { SwapBuffers (dc); }
  88. bool setSwapInterval (int numFramesPerSwap)
  89. {
  90. jassert (isActive()); // this can only be called when the context is active..
  91. return wglSwapIntervalEXT != nullptr && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  92. }
  93. int getSwapInterval() const
  94. {
  95. jassert (isActive()); // this can only be called when the context is active..
  96. return wglGetSwapIntervalEXT != nullptr ? wglGetSwapIntervalEXT() : 0;
  97. }
  98. void updateWindowPosition (Rectangle<int> bounds)
  99. {
  100. if (nativeWindow != nullptr)
  101. {
  102. if (! approximatelyEqual (nativeScaleFactor, 1.0))
  103. bounds = (bounds.toDouble() * nativeScaleFactor).toNearestInt();
  104. SetWindowPos ((HWND) nativeWindow->getNativeHandle(), nullptr,
  105. bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
  106. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  107. }
  108. }
  109. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  110. void* getRawContext() const noexcept { return renderContext; }
  111. unsigned int getFrameBufferID() const noexcept { return 0; }
  112. void triggerRepaint()
  113. {
  114. if (context != nullptr)
  115. context->triggerRepaint();
  116. }
  117. struct Locker { Locker (NativeContext&) {} };
  118. HWND getNativeHandle()
  119. {
  120. if (nativeWindow != nullptr)
  121. return (HWND) nativeWindow->getNativeHandle();
  122. return nullptr;
  123. }
  124. private:
  125. struct DummyComponent : public Component
  126. {
  127. DummyComponent (NativeContext& c) : context (c) {}
  128. // The windowing code will call this when a paint callback happens
  129. void handleCommandMessage (int) override { context.triggerRepaint(); }
  130. NativeContext& context;
  131. };
  132. std::unique_ptr<DummyComponent> dummyComponent;
  133. std::unique_ptr<ComponentPeer> nativeWindow;
  134. std::unique_ptr<ScopedThreadDPIAwarenessSetter> threadAwarenessSetter;
  135. HGLRC renderContext;
  136. HDC dc;
  137. OpenGLContext* context = {};
  138. Component::SafePointer<Component> safeComponent;
  139. double nativeScaleFactor = 1.0;
  140. #define JUCE_DECLARE_WGL_EXTENSION_FUNCTION(name, returnType, params) \
  141. typedef returnType (__stdcall *type_ ## name) params; type_ ## name name;
  142. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB, BOOL, (HDC, const int*, const FLOAT*, UINT, int*, UINT*))
  143. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT, BOOL, (int))
  144. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT, int, ())
  145. #undef JUCE_DECLARE_WGL_EXTENSION_FUNCTION
  146. void nativeScaleFactorChanged (double newScaleFactor) override
  147. {
  148. if (approximatelyEqual (newScaleFactor, nativeScaleFactor)
  149. || safeComponent == nullptr)
  150. return;
  151. if (auto* peer = safeComponent->getTopLevelComponent()->getPeer())
  152. {
  153. nativeScaleFactor = newScaleFactor;
  154. updateWindowPosition (peer->getAreaCoveredBy (*safeComponent));
  155. }
  156. }
  157. void initialiseGLExtensions()
  158. {
  159. #define JUCE_INIT_WGL_FUNCTION(name) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  160. JUCE_INIT_WGL_FUNCTION (wglChoosePixelFormatARB);
  161. JUCE_INIT_WGL_FUNCTION (wglSwapIntervalEXT);
  162. JUCE_INIT_WGL_FUNCTION (wglGetSwapIntervalEXT);
  163. #undef JUCE_INIT_WGL_FUNCTION
  164. }
  165. void createNativeWindow (Component& component)
  166. {
  167. auto* topComp = component.getTopLevelComponent();
  168. {
  169. auto* parentHWND = topComp->getWindowHandle();
  170. ScopedThreadDPIAwarenessSetter setter { parentHWND };
  171. nativeWindow.reset (createNonRepaintingEmbeddedWindowsPeer (*dummyComponent, parentHWND));
  172. }
  173. if (auto* peer = topComp->getPeer())
  174. {
  175. safeComponent = Component::SafePointer<Component> (&component);
  176. nativeScaleFactor = peer->getPlatformScaleFactor();
  177. updateWindowPosition (peer->getAreaCoveredBy (component));
  178. peer->addScaleFactorListener (this);
  179. }
  180. nativeWindow->setVisible (true);
  181. dc = GetDC ((HWND) nativeWindow->getNativeHandle());
  182. }
  183. void deleteRenderContext()
  184. {
  185. if (renderContext != nullptr)
  186. {
  187. wglDeleteContext (renderContext);
  188. renderContext = nullptr;
  189. }
  190. }
  191. void releaseDC()
  192. {
  193. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  194. }
  195. static void initialisePixelFormatDescriptor (PIXELFORMATDESCRIPTOR& pfd, const OpenGLPixelFormat& pixelFormat)
  196. {
  197. zerostruct (pfd);
  198. pfd.nSize = sizeof (pfd);
  199. pfd.nVersion = 1;
  200. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  201. pfd.iPixelType = PFD_TYPE_RGBA;
  202. pfd.iLayerType = PFD_MAIN_PLANE;
  203. pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
  204. pfd.cRedBits = (BYTE) pixelFormat.redBits;
  205. pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
  206. pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
  207. pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
  208. pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
  209. pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
  210. pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  211. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
  212. pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
  213. pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
  214. pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
  215. pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
  216. }
  217. int wglChoosePixelFormatExtension (const OpenGLPixelFormat& pixelFormat) const
  218. {
  219. int format = 0;
  220. if (wglChoosePixelFormatARB != nullptr)
  221. {
  222. int atts[64];
  223. int n = 0;
  224. atts[n++] = WGL_DRAW_TO_WINDOW_ARB; atts[n++] = GL_TRUE;
  225. atts[n++] = WGL_SUPPORT_OPENGL_ARB; atts[n++] = GL_TRUE;
  226. atts[n++] = WGL_DOUBLE_BUFFER_ARB; atts[n++] = GL_TRUE;
  227. atts[n++] = WGL_PIXEL_TYPE_ARB; atts[n++] = WGL_TYPE_RGBA_ARB;
  228. atts[n++] = WGL_ACCELERATION_ARB;
  229. atts[n++] = WGL_FULL_ACCELERATION_ARB;
  230. atts[n++] = WGL_COLOR_BITS_ARB; atts[n++] = pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits;
  231. atts[n++] = WGL_RED_BITS_ARB; atts[n++] = pixelFormat.redBits;
  232. atts[n++] = WGL_GREEN_BITS_ARB; atts[n++] = pixelFormat.greenBits;
  233. atts[n++] = WGL_BLUE_BITS_ARB; atts[n++] = pixelFormat.blueBits;
  234. atts[n++] = WGL_ALPHA_BITS_ARB; atts[n++] = pixelFormat.alphaBits;
  235. atts[n++] = WGL_DEPTH_BITS_ARB; atts[n++] = pixelFormat.depthBufferBits;
  236. atts[n++] = WGL_STENCIL_BITS_ARB; atts[n++] = pixelFormat.stencilBufferBits;
  237. atts[n++] = WGL_ACCUM_RED_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferRedBits;
  238. atts[n++] = WGL_ACCUM_GREEN_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferGreenBits;
  239. atts[n++] = WGL_ACCUM_BLUE_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferBlueBits;
  240. atts[n++] = WGL_ACCUM_ALPHA_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferAlphaBits;
  241. if (pixelFormat.multisamplingLevel > 0
  242. && OpenGLHelpers::isExtensionSupported ("GL_ARB_multisample"))
  243. {
  244. atts[n++] = WGL_SAMPLE_BUFFERS_ARB;
  245. atts[n++] = 1;
  246. atts[n++] = WGL_SAMPLES_ARB;
  247. atts[n++] = pixelFormat.multisamplingLevel;
  248. }
  249. atts[n++] = 0;
  250. jassert (n <= numElementsInArray (atts));
  251. UINT formatsCount = 0;
  252. wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount);
  253. }
  254. return format;
  255. }
  256. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  257. };
  258. //==============================================================================
  259. bool OpenGLHelpers::isContextActive()
  260. {
  261. return wglGetCurrentContext() != nullptr;
  262. }
  263. } // namespace juce