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.

363 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. extern ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component&, void* parent);
  22. extern bool shouldScaleGLWindow (void* hwnd);
  23. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client && JucePlugin_Build_VST
  24. bool juce_shouldDoubleScaleNativeGLWindow();
  25. #else
  26. bool juce_shouldDoubleScaleNativeGLWindow() { return false; }
  27. #endif
  28. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  29. void setProcessDPIAwarenessIfNecessary (void*);
  30. #endif
  31. void setThreadDPIAwareness (void*);
  32. //==============================================================================
  33. class OpenGLContext::NativeContext
  34. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  35. : private Timer
  36. #endif
  37. {
  38. public:
  39. NativeContext (Component& component,
  40. const OpenGLPixelFormat& pixelFormat,
  41. void* contextToShareWith,
  42. bool /*useMultisampling*/,
  43. OpenGLVersion)
  44. {
  45. dummyComponent.reset (new DummyComponent (*this));
  46. createNativeWindow (component);
  47. PIXELFORMATDESCRIPTOR pfd;
  48. initialisePixelFormatDescriptor (pfd, pixelFormat);
  49. auto pixFormat = ChoosePixelFormat (dc, &pfd);
  50. if (pixFormat != 0)
  51. SetPixelFormat (dc, pixFormat, &pfd);
  52. renderContext = wglCreateContext (dc);
  53. if (renderContext != 0)
  54. {
  55. makeActive();
  56. initialiseGLExtensions();
  57. auto wglFormat = wglChoosePixelFormatExtension (pixelFormat);
  58. deactivateCurrentContext();
  59. if (wglFormat != pixFormat && wglFormat != 0)
  60. {
  61. // can't change the pixel format of a window, so need to delete the
  62. // old one and create a new one..
  63. releaseDC();
  64. nativeWindow = nullptr;
  65. createNativeWindow (component);
  66. if (SetPixelFormat (dc, wglFormat, &pfd))
  67. {
  68. deleteRenderContext();
  69. renderContext = wglCreateContext (dc);
  70. }
  71. }
  72. if (contextToShareWith != nullptr)
  73. wglShareLists ((HGLRC) contextToShareWith, renderContext);
  74. component.getTopLevelComponent()->repaint();
  75. component.repaint();
  76. }
  77. }
  78. ~NativeContext()
  79. {
  80. deleteRenderContext();
  81. releaseDC();
  82. }
  83. bool initialiseOnRenderThread (OpenGLContext& c)
  84. {
  85. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  86. setProcessDPIAwarenessIfNecessary (nativeWindow->getNativeHandle());
  87. #endif
  88. updateThreadDPIAwareness();
  89. context = &c;
  90. return true;
  91. }
  92. void shutdownOnRenderThread() { deactivateCurrentContext(); context = nullptr; }
  93. static void deactivateCurrentContext() { wglMakeCurrent (0, 0); }
  94. bool makeActive() const noexcept { updateThreadDPIAwareness(); return isActive() || wglMakeCurrent (dc, renderContext) != FALSE; }
  95. bool isActive() const noexcept { return wglGetCurrentContext() == renderContext; }
  96. void swapBuffers() const noexcept { SwapBuffers (dc); }
  97. bool setSwapInterval (int numFramesPerSwap)
  98. {
  99. jassert (isActive()); // this can only be called when the context is active..
  100. return wglSwapIntervalEXT != nullptr && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  101. }
  102. int getSwapInterval() const
  103. {
  104. jassert (isActive()); // this can only be called when the context is active..
  105. return wglGetSwapIntervalEXT != nullptr ? wglGetSwapIntervalEXT() : 0;
  106. }
  107. void updateWindowPosition (Rectangle<int> bounds)
  108. {
  109. if (nativeWindow != nullptr)
  110. {
  111. if (! approximatelyEqual (nativeScaleFactor, 1.0))
  112. bounds = (bounds.toDouble() * nativeScaleFactor).toNearestInt();
  113. SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
  114. bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
  115. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  116. }
  117. }
  118. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  119. void* getRawContext() const noexcept { return renderContext; }
  120. unsigned int getFrameBufferID() const noexcept { return 0; }
  121. void triggerRepaint()
  122. {
  123. if (context != nullptr)
  124. context->triggerRepaint();
  125. }
  126. struct Locker { Locker (NativeContext&) {} };
  127. double getWindowScaleFactor (const Rectangle<int>& screenBounds)
  128. {
  129. if (nativeWindow != nullptr && shouldScaleGLWindow (nativeWindow->getNativeHandle()))
  130. return Desktop::getInstance().getDisplays().findDisplayForRect (screenBounds).scale;
  131. return Desktop::getInstance().getGlobalScaleFactor();
  132. }
  133. private:
  134. struct DummyComponent : public Component
  135. {
  136. DummyComponent (NativeContext& c) : context (c) {}
  137. // The windowing code will call this when a paint callback happens
  138. void handleCommandMessage (int) override { context.triggerRepaint(); }
  139. NativeContext& context;
  140. };
  141. std::unique_ptr<DummyComponent> dummyComponent;
  142. std::unique_ptr<ComponentPeer> nativeWindow;
  143. HGLRC renderContext;
  144. HDC dc;
  145. OpenGLContext* context = {};
  146. double nativeScaleFactor = 1.0;
  147. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  148. Component::SafePointer<Component> safeComponent;
  149. #endif
  150. #define JUCE_DECLARE_WGL_EXTENSION_FUNCTION(name, returnType, params) \
  151. typedef returnType (__stdcall *type_ ## name) params; type_ ## name name;
  152. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB, BOOL, (HDC, const int*, const FLOAT*, UINT, int*, UINT*))
  153. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT, BOOL, (int))
  154. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT, int, ())
  155. #undef JUCE_DECLARE_WGL_EXTENSION_FUNCTION
  156. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  157. void timerCallback() override
  158. {
  159. if (safeComponent != nullptr)
  160. {
  161. if (auto* peer = safeComponent->getTopLevelComponent()->getPeer())
  162. {
  163. auto newScale = peer->getPlatformScaleFactor();
  164. if (juce_shouldDoubleScaleNativeGLWindow())
  165. newScale *= newScale;
  166. if (! approximatelyEqual (newScale, nativeScaleFactor))
  167. {
  168. nativeScaleFactor = newScale;
  169. updateWindowPosition (peer->getAreaCoveredBy (*safeComponent));
  170. }
  171. }
  172. }
  173. }
  174. #endif
  175. void initialiseGLExtensions()
  176. {
  177. #define JUCE_INIT_WGL_FUNCTION(name) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  178. JUCE_INIT_WGL_FUNCTION (wglChoosePixelFormatARB);
  179. JUCE_INIT_WGL_FUNCTION (wglSwapIntervalEXT);
  180. JUCE_INIT_WGL_FUNCTION (wglGetSwapIntervalEXT);
  181. #undef JUCE_INIT_WGL_FUNCTION
  182. }
  183. void createNativeWindow (Component& component)
  184. {
  185. auto* topComp = component.getTopLevelComponent();
  186. nativeWindow.reset (createNonRepaintingEmbeddedWindowsPeer (*dummyComponent, topComp->getWindowHandle()));
  187. if (auto* peer = topComp->getPeer())
  188. {
  189. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  190. safeComponent = Component::SafePointer<Component> (&component);
  191. nativeScaleFactor = peer->getPlatformScaleFactor();
  192. if (juce_shouldDoubleScaleNativeGLWindow())
  193. nativeScaleFactor *= nativeScaleFactor;
  194. startTimer (50);
  195. #endif
  196. updateWindowPosition (peer->getAreaCoveredBy (component));
  197. }
  198. nativeWindow->setVisible (true);
  199. dc = GetDC ((HWND) nativeWindow->getNativeHandle());
  200. }
  201. void deleteRenderContext()
  202. {
  203. if (renderContext != 0)
  204. {
  205. wglDeleteContext (renderContext);
  206. renderContext = 0;
  207. }
  208. }
  209. void releaseDC()
  210. {
  211. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  212. }
  213. static void initialisePixelFormatDescriptor (PIXELFORMATDESCRIPTOR& pfd, const OpenGLPixelFormat& pixelFormat)
  214. {
  215. zerostruct (pfd);
  216. pfd.nSize = sizeof (pfd);
  217. pfd.nVersion = 1;
  218. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  219. pfd.iPixelType = PFD_TYPE_RGBA;
  220. pfd.iLayerType = PFD_MAIN_PLANE;
  221. pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
  222. pfd.cRedBits = (BYTE) pixelFormat.redBits;
  223. pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
  224. pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
  225. pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
  226. pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
  227. pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
  228. pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  229. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
  230. pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
  231. pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
  232. pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
  233. pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
  234. }
  235. int wglChoosePixelFormatExtension (const OpenGLPixelFormat& pixelFormat) const
  236. {
  237. int format = 0;
  238. if (wglChoosePixelFormatARB != nullptr)
  239. {
  240. int atts[64];
  241. int n = 0;
  242. atts[n++] = WGL_DRAW_TO_WINDOW_ARB; atts[n++] = GL_TRUE;
  243. atts[n++] = WGL_SUPPORT_OPENGL_ARB; atts[n++] = GL_TRUE;
  244. atts[n++] = WGL_DOUBLE_BUFFER_ARB; atts[n++] = GL_TRUE;
  245. atts[n++] = WGL_PIXEL_TYPE_ARB; atts[n++] = WGL_TYPE_RGBA_ARB;
  246. atts[n++] = WGL_ACCELERATION_ARB;
  247. atts[n++] = WGL_FULL_ACCELERATION_ARB;
  248. atts[n++] = WGL_COLOR_BITS_ARB; atts[n++] = pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits;
  249. atts[n++] = WGL_RED_BITS_ARB; atts[n++] = pixelFormat.redBits;
  250. atts[n++] = WGL_GREEN_BITS_ARB; atts[n++] = pixelFormat.greenBits;
  251. atts[n++] = WGL_BLUE_BITS_ARB; atts[n++] = pixelFormat.blueBits;
  252. atts[n++] = WGL_ALPHA_BITS_ARB; atts[n++] = pixelFormat.alphaBits;
  253. atts[n++] = WGL_DEPTH_BITS_ARB; atts[n++] = pixelFormat.depthBufferBits;
  254. atts[n++] = WGL_STENCIL_BITS_ARB; atts[n++] = pixelFormat.stencilBufferBits;
  255. atts[n++] = WGL_ACCUM_RED_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferRedBits;
  256. atts[n++] = WGL_ACCUM_GREEN_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferGreenBits;
  257. atts[n++] = WGL_ACCUM_BLUE_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferBlueBits;
  258. atts[n++] = WGL_ACCUM_ALPHA_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferAlphaBits;
  259. if (pixelFormat.multisamplingLevel > 0
  260. && OpenGLHelpers::isExtensionSupported ("GL_ARB_multisample"))
  261. {
  262. atts[n++] = WGL_SAMPLE_BUFFERS_ARB;
  263. atts[n++] = 1;
  264. atts[n++] = WGL_SAMPLES_ARB;
  265. atts[n++] = pixelFormat.multisamplingLevel;
  266. }
  267. atts[n++] = 0;
  268. jassert (n <= numElementsInArray (atts));
  269. UINT formatsCount = 0;
  270. wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount);
  271. }
  272. return format;
  273. }
  274. void updateThreadDPIAwareness() const
  275. {
  276. if (nativeWindow.get() != nullptr)
  277. setThreadDPIAwareness (nativeWindow->getNativeHandle());
  278. else
  279. setThreadDPIAwareness (nullptr);
  280. }
  281. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  282. };
  283. //==============================================================================
  284. bool OpenGLHelpers::isContextActive()
  285. {
  286. return wglGetCurrentContext() != 0;
  287. }
  288. } // namespace juce