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.

371 lines
14KB

  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 version)
  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. initialiseWGLExtensions (dc);
  39. renderContext = createRenderContext (version, dc);
  40. if (renderContext != nullptr)
  41. {
  42. makeActive();
  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 = createRenderContext (version, 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. //==============================================================================
  126. static void initialiseWGLExtensions (HDC dcIn)
  127. {
  128. static bool initialised = false;
  129. if (initialised)
  130. return;
  131. initialised = true;
  132. const auto dummyContext = wglCreateContext (dcIn);
  133. wglMakeCurrent (dcIn, dummyContext);
  134. #define JUCE_INIT_WGL_FUNCTION(name) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  135. JUCE_INIT_WGL_FUNCTION (wglChoosePixelFormatARB)
  136. JUCE_INIT_WGL_FUNCTION (wglSwapIntervalEXT)
  137. JUCE_INIT_WGL_FUNCTION (wglGetSwapIntervalEXT)
  138. JUCE_INIT_WGL_FUNCTION (wglCreateContextAttribsARB)
  139. #undef JUCE_INIT_WGL_FUNCTION
  140. wglMakeCurrent (nullptr, nullptr);
  141. wglDeleteContext (dummyContext);
  142. }
  143. static void initialisePixelFormatDescriptor (PIXELFORMATDESCRIPTOR& pfd, const OpenGLPixelFormat& pixelFormat)
  144. {
  145. zerostruct (pfd);
  146. pfd.nSize = sizeof (pfd);
  147. pfd.nVersion = 1;
  148. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  149. pfd.iPixelType = PFD_TYPE_RGBA;
  150. pfd.iLayerType = PFD_MAIN_PLANE;
  151. pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
  152. pfd.cRedBits = (BYTE) pixelFormat.redBits;
  153. pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
  154. pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
  155. pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
  156. pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
  157. pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
  158. pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  159. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
  160. pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
  161. pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
  162. pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
  163. pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
  164. }
  165. static HGLRC createRenderContext (OpenGLVersion version, HDC dcIn)
  166. {
  167. if (version >= openGL3_2 && wglCreateContextAttribsARB != nullptr)
  168. {
  169. const int attribs[] =
  170. {
  171. WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  172. WGL_CONTEXT_MINOR_VERSION_ARB, 2,
  173. WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
  174. 0
  175. };
  176. const auto c = wglCreateContextAttribsARB (dcIn, nullptr, attribs);
  177. if (c != nullptr)
  178. return c;
  179. }
  180. return wglCreateContext (dcIn);
  181. }
  182. //==============================================================================
  183. struct DummyComponent : public Component
  184. {
  185. DummyComponent (NativeContext& c) : context (c) {}
  186. // The windowing code will call this when a paint callback happens
  187. void handleCommandMessage (int) override { context.triggerRepaint(); }
  188. NativeContext& context;
  189. };
  190. //==============================================================================
  191. void nativeScaleFactorChanged (double newScaleFactor) override
  192. {
  193. if (approximatelyEqual (newScaleFactor, nativeScaleFactor)
  194. || safeComponent == nullptr)
  195. return;
  196. if (auto* peer = safeComponent->getTopLevelComponent()->getPeer())
  197. {
  198. nativeScaleFactor = newScaleFactor;
  199. updateWindowPosition (peer->getAreaCoveredBy (*safeComponent));
  200. }
  201. }
  202. void createNativeWindow (Component& component)
  203. {
  204. auto* topComp = component.getTopLevelComponent();
  205. {
  206. auto* parentHWND = topComp->getWindowHandle();
  207. ScopedThreadDPIAwarenessSetter setter { parentHWND };
  208. nativeWindow.reset (createNonRepaintingEmbeddedWindowsPeer (*dummyComponent, parentHWND));
  209. }
  210. if (auto* peer = topComp->getPeer())
  211. {
  212. safeComponent = Component::SafePointer<Component> (&component);
  213. nativeScaleFactor = peer->getPlatformScaleFactor();
  214. updateWindowPosition (peer->getAreaCoveredBy (component));
  215. peer->addScaleFactorListener (this);
  216. }
  217. nativeWindow->setVisible (true);
  218. dc = GetDC ((HWND) nativeWindow->getNativeHandle());
  219. }
  220. void deleteRenderContext()
  221. {
  222. if (renderContext != nullptr)
  223. {
  224. wglDeleteContext (renderContext);
  225. renderContext = nullptr;
  226. }
  227. }
  228. void releaseDC()
  229. {
  230. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  231. }
  232. int wglChoosePixelFormatExtension (const OpenGLPixelFormat& pixelFormat) const
  233. {
  234. int format = 0;
  235. if (wglChoosePixelFormatARB != nullptr)
  236. {
  237. int atts[64];
  238. int n = 0;
  239. atts[n++] = WGL_DRAW_TO_WINDOW_ARB; atts[n++] = GL_TRUE;
  240. atts[n++] = WGL_SUPPORT_OPENGL_ARB; atts[n++] = GL_TRUE;
  241. atts[n++] = WGL_DOUBLE_BUFFER_ARB; atts[n++] = GL_TRUE;
  242. atts[n++] = WGL_PIXEL_TYPE_ARB; atts[n++] = WGL_TYPE_RGBA_ARB;
  243. atts[n++] = WGL_ACCELERATION_ARB;
  244. atts[n++] = WGL_FULL_ACCELERATION_ARB;
  245. atts[n++] = WGL_COLOR_BITS_ARB; atts[n++] = pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits;
  246. atts[n++] = WGL_RED_BITS_ARB; atts[n++] = pixelFormat.redBits;
  247. atts[n++] = WGL_GREEN_BITS_ARB; atts[n++] = pixelFormat.greenBits;
  248. atts[n++] = WGL_BLUE_BITS_ARB; atts[n++] = pixelFormat.blueBits;
  249. atts[n++] = WGL_ALPHA_BITS_ARB; atts[n++] = pixelFormat.alphaBits;
  250. atts[n++] = WGL_DEPTH_BITS_ARB; atts[n++] = pixelFormat.depthBufferBits;
  251. atts[n++] = WGL_STENCIL_BITS_ARB; atts[n++] = pixelFormat.stencilBufferBits;
  252. atts[n++] = WGL_ACCUM_RED_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferRedBits;
  253. atts[n++] = WGL_ACCUM_GREEN_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferGreenBits;
  254. atts[n++] = WGL_ACCUM_BLUE_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferBlueBits;
  255. atts[n++] = WGL_ACCUM_ALPHA_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferAlphaBits;
  256. if (pixelFormat.multisamplingLevel > 0
  257. && OpenGLHelpers::isExtensionSupported ("GL_ARB_multisample"))
  258. {
  259. atts[n++] = WGL_SAMPLE_BUFFERS_ARB;
  260. atts[n++] = 1;
  261. atts[n++] = WGL_SAMPLES_ARB;
  262. atts[n++] = pixelFormat.multisamplingLevel;
  263. }
  264. atts[n++] = 0;
  265. jassert (n <= numElementsInArray (atts));
  266. UINT formatsCount = 0;
  267. wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount);
  268. }
  269. return format;
  270. }
  271. //==============================================================================
  272. #define JUCE_DECLARE_WGL_EXTENSION_FUNCTION(name, returnType, params) \
  273. typedef returnType (__stdcall *type_ ## name) params; static type_ ## name name;
  274. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB, BOOL, (HDC, const int*, const FLOAT*, UINT, int*, UINT*))
  275. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT, BOOL, (int))
  276. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT, int, ())
  277. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglCreateContextAttribsARB, HGLRC, (HDC, HGLRC, const int*))
  278. #undef JUCE_DECLARE_WGL_EXTENSION_FUNCTION
  279. //==============================================================================
  280. std::unique_ptr<DummyComponent> dummyComponent;
  281. std::unique_ptr<ComponentPeer> nativeWindow;
  282. std::unique_ptr<ScopedThreadDPIAwarenessSetter> threadAwarenessSetter;
  283. Component::SafePointer<Component> safeComponent;
  284. HGLRC renderContext;
  285. HDC dc;
  286. OpenGLContext* context = nullptr;
  287. double nativeScaleFactor = 1.0;
  288. //==============================================================================
  289. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  290. };
  291. //==============================================================================
  292. bool OpenGLHelpers::isContextActive()
  293. {
  294. return wglGetCurrentContext() != nullptr;
  295. }
  296. } // namespace juce