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.

341 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
  23. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  24. : private Timer
  25. #endif
  26. {
  27. public:
  28. NativeContext (Component& component,
  29. const OpenGLPixelFormat& pixelFormat,
  30. void* contextToShareWithIn,
  31. bool /*useMultisampling*/,
  32. OpenGLVersion)
  33. {
  34. dummyComponent.reset (new DummyComponent (*this));
  35. createNativeWindow (component);
  36. PIXELFORMATDESCRIPTOR pfd;
  37. initialisePixelFormatDescriptor (pfd, pixelFormat);
  38. auto pixFormat = ChoosePixelFormat (dc, &pfd);
  39. if (pixFormat != 0)
  40. SetPixelFormat (dc, pixFormat, &pfd);
  41. renderContext = wglCreateContext (dc);
  42. if (renderContext != nullptr)
  43. {
  44. makeActive();
  45. initialiseGLExtensions();
  46. auto wglFormat = wglChoosePixelFormatExtension (pixelFormat);
  47. deactivateCurrentContext();
  48. if (wglFormat != pixFormat && wglFormat != 0)
  49. {
  50. // can't change the pixel format of a window, so need to delete the
  51. // old one and create a new one..
  52. releaseDC();
  53. nativeWindow = nullptr;
  54. createNativeWindow (component);
  55. if (SetPixelFormat (dc, wglFormat, &pfd))
  56. {
  57. deleteRenderContext();
  58. renderContext = wglCreateContext (dc);
  59. }
  60. }
  61. if (contextToShareWithIn != nullptr)
  62. wglShareLists ((HGLRC) contextToShareWithIn, renderContext);
  63. component.getTopLevelComponent()->repaint();
  64. component.repaint();
  65. }
  66. }
  67. ~NativeContext() override
  68. {
  69. deleteRenderContext();
  70. releaseDC();
  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. double nativeScaleFactor = 1.0;
  139. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  140. Component::SafePointer<Component> safeComponent;
  141. #endif
  142. #define JUCE_DECLARE_WGL_EXTENSION_FUNCTION(name, returnType, params) \
  143. typedef returnType (__stdcall *type_ ## name) params; type_ ## name name;
  144. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB, BOOL, (HDC, const int*, const FLOAT*, UINT, int*, UINT*))
  145. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT, BOOL, (int))
  146. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT, int, ())
  147. #undef JUCE_DECLARE_WGL_EXTENSION_FUNCTION
  148. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  149. void timerCallback() override
  150. {
  151. if (safeComponent != nullptr)
  152. {
  153. if (auto* peer = safeComponent->getTopLevelComponent()->getPeer())
  154. {
  155. auto newScale = peer->getPlatformScaleFactor();
  156. if (! approximatelyEqual (newScale, nativeScaleFactor))
  157. {
  158. nativeScaleFactor = newScale;
  159. updateWindowPosition (peer->getAreaCoveredBy (*safeComponent));
  160. }
  161. }
  162. }
  163. }
  164. #endif
  165. void initialiseGLExtensions()
  166. {
  167. #define JUCE_INIT_WGL_FUNCTION(name) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  168. JUCE_INIT_WGL_FUNCTION (wglChoosePixelFormatARB);
  169. JUCE_INIT_WGL_FUNCTION (wglSwapIntervalEXT);
  170. JUCE_INIT_WGL_FUNCTION (wglGetSwapIntervalEXT);
  171. #undef JUCE_INIT_WGL_FUNCTION
  172. }
  173. void createNativeWindow (Component& component)
  174. {
  175. auto* topComp = component.getTopLevelComponent();
  176. {
  177. auto* parentHWND = topComp->getWindowHandle();
  178. ScopedThreadDPIAwarenessSetter setter { parentHWND };
  179. nativeWindow.reset (createNonRepaintingEmbeddedWindowsPeer (*dummyComponent, parentHWND));
  180. }
  181. if (auto* peer = topComp->getPeer())
  182. {
  183. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  184. safeComponent = Component::SafePointer<Component> (&component);
  185. nativeScaleFactor = peer->getPlatformScaleFactor();
  186. startTimer (50);
  187. #endif
  188. updateWindowPosition (peer->getAreaCoveredBy (component));
  189. }
  190. nativeWindow->setVisible (true);
  191. dc = GetDC ((HWND) nativeWindow->getNativeHandle());
  192. }
  193. void deleteRenderContext()
  194. {
  195. if (renderContext != nullptr)
  196. {
  197. wglDeleteContext (renderContext);
  198. renderContext = nullptr;
  199. }
  200. }
  201. void releaseDC()
  202. {
  203. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  204. }
  205. static void initialisePixelFormatDescriptor (PIXELFORMATDESCRIPTOR& pfd, const OpenGLPixelFormat& pixelFormat)
  206. {
  207. zerostruct (pfd);
  208. pfd.nSize = sizeof (pfd);
  209. pfd.nVersion = 1;
  210. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  211. pfd.iPixelType = PFD_TYPE_RGBA;
  212. pfd.iLayerType = PFD_MAIN_PLANE;
  213. pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
  214. pfd.cRedBits = (BYTE) pixelFormat.redBits;
  215. pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
  216. pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
  217. pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
  218. pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
  219. pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
  220. pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  221. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
  222. pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
  223. pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
  224. pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
  225. pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
  226. }
  227. int wglChoosePixelFormatExtension (const OpenGLPixelFormat& pixelFormat) const
  228. {
  229. int format = 0;
  230. if (wglChoosePixelFormatARB != nullptr)
  231. {
  232. int atts[64];
  233. int n = 0;
  234. atts[n++] = WGL_DRAW_TO_WINDOW_ARB; atts[n++] = GL_TRUE;
  235. atts[n++] = WGL_SUPPORT_OPENGL_ARB; atts[n++] = GL_TRUE;
  236. atts[n++] = WGL_DOUBLE_BUFFER_ARB; atts[n++] = GL_TRUE;
  237. atts[n++] = WGL_PIXEL_TYPE_ARB; atts[n++] = WGL_TYPE_RGBA_ARB;
  238. atts[n++] = WGL_ACCELERATION_ARB;
  239. atts[n++] = WGL_FULL_ACCELERATION_ARB;
  240. atts[n++] = WGL_COLOR_BITS_ARB; atts[n++] = pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits;
  241. atts[n++] = WGL_RED_BITS_ARB; atts[n++] = pixelFormat.redBits;
  242. atts[n++] = WGL_GREEN_BITS_ARB; atts[n++] = pixelFormat.greenBits;
  243. atts[n++] = WGL_BLUE_BITS_ARB; atts[n++] = pixelFormat.blueBits;
  244. atts[n++] = WGL_ALPHA_BITS_ARB; atts[n++] = pixelFormat.alphaBits;
  245. atts[n++] = WGL_DEPTH_BITS_ARB; atts[n++] = pixelFormat.depthBufferBits;
  246. atts[n++] = WGL_STENCIL_BITS_ARB; atts[n++] = pixelFormat.stencilBufferBits;
  247. atts[n++] = WGL_ACCUM_RED_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferRedBits;
  248. atts[n++] = WGL_ACCUM_GREEN_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferGreenBits;
  249. atts[n++] = WGL_ACCUM_BLUE_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferBlueBits;
  250. atts[n++] = WGL_ACCUM_ALPHA_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferAlphaBits;
  251. if (pixelFormat.multisamplingLevel > 0
  252. && OpenGLHelpers::isExtensionSupported ("GL_ARB_multisample"))
  253. {
  254. atts[n++] = WGL_SAMPLE_BUFFERS_ARB;
  255. atts[n++] = 1;
  256. atts[n++] = WGL_SAMPLES_ARB;
  257. atts[n++] = pixelFormat.multisamplingLevel;
  258. }
  259. atts[n++] = 0;
  260. jassert (n <= numElementsInArray (atts));
  261. UINT formatsCount = 0;
  262. wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount);
  263. }
  264. return format;
  265. }
  266. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  267. };
  268. //==============================================================================
  269. bool OpenGLHelpers::isContextActive()
  270. {
  271. return wglGetCurrentContext() != nullptr;
  272. }
  273. } // namespace juce