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.

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