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.

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