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.

342 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. //==============================================================================
  29. class OpenGLContext::NativeContext
  30. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  31. : private Timer
  32. #endif
  33. {
  34. public:
  35. NativeContext (Component& component,
  36. const OpenGLPixelFormat& pixelFormat,
  37. void* contextToShareWith,
  38. bool /*useMultisampling*/,
  39. OpenGLVersion)
  40. {
  41. dummyComponent.reset (new DummyComponent (*this));
  42. createNativeWindow (component);
  43. PIXELFORMATDESCRIPTOR pfd;
  44. initialisePixelFormatDescriptor (pfd, pixelFormat);
  45. auto pixFormat = ChoosePixelFormat (dc, &pfd);
  46. if (pixFormat != 0)
  47. SetPixelFormat (dc, pixFormat, &pfd);
  48. renderContext = wglCreateContext (dc);
  49. if (renderContext != 0)
  50. {
  51. makeActive();
  52. initialiseGLExtensions();
  53. auto wglFormat = wglChoosePixelFormatExtension (pixelFormat);
  54. deactivateCurrentContext();
  55. if (wglFormat != pixFormat && wglFormat != 0)
  56. {
  57. // can't change the pixel format of a window, so need to delete the
  58. // old one and create a new one..
  59. releaseDC();
  60. nativeWindow = nullptr;
  61. createNativeWindow (component);
  62. if (SetPixelFormat (dc, wglFormat, &pfd))
  63. {
  64. deleteRenderContext();
  65. renderContext = wglCreateContext (dc);
  66. }
  67. }
  68. if (contextToShareWith != nullptr)
  69. wglShareLists ((HGLRC) contextToShareWith, renderContext);
  70. component.getTopLevelComponent()->repaint();
  71. component.repaint();
  72. }
  73. }
  74. ~NativeContext()
  75. {
  76. deleteRenderContext();
  77. releaseDC();
  78. }
  79. bool initialiseOnRenderThread (OpenGLContext& c)
  80. {
  81. context = &c;
  82. return true;
  83. }
  84. void shutdownOnRenderThread() { deactivateCurrentContext(); context = nullptr; }
  85. static void deactivateCurrentContext() { wglMakeCurrent (0, 0); }
  86. bool makeActive() const noexcept { return isActive() || wglMakeCurrent (dc, renderContext) != FALSE; }
  87. bool isActive() const noexcept { return wglGetCurrentContext() == renderContext; }
  88. void swapBuffers() const noexcept { SwapBuffers (dc); }
  89. bool setSwapInterval (int numFramesPerSwap)
  90. {
  91. jassert (isActive()); // this can only be called when the context is active..
  92. return wglSwapIntervalEXT != nullptr && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  93. }
  94. int getSwapInterval() const
  95. {
  96. jassert (isActive()); // this can only be called when the context is active..
  97. return wglGetSwapIntervalEXT != nullptr ? wglGetSwapIntervalEXT() : 0;
  98. }
  99. void updateWindowPosition (Rectangle<int> bounds)
  100. {
  101. if (nativeWindow != nullptr)
  102. {
  103. if (! approximatelyEqual (nativeScaleFactor, 1.0))
  104. bounds = (bounds.toDouble() * nativeScaleFactor).toNearestInt();
  105. SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
  106. bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
  107. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  108. }
  109. }
  110. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  111. void* getRawContext() const noexcept { return renderContext; }
  112. unsigned int getFrameBufferID() const noexcept { return 0; }
  113. void triggerRepaint()
  114. {
  115. if (context != nullptr)
  116. context->triggerRepaint();
  117. }
  118. struct Locker { Locker (NativeContext&) {} };
  119. double getWindowScaleFactor (const Rectangle<int>& screenBounds)
  120. {
  121. if (nativeWindow != nullptr && shouldScaleGLWindow (nativeWindow->getNativeHandle()))
  122. return Desktop::getInstance().getDisplays().findDisplayForRect (screenBounds).scale;
  123. return Desktop::getInstance().getGlobalScaleFactor();
  124. }
  125. private:
  126. struct DummyComponent : public Component
  127. {
  128. DummyComponent (NativeContext& c) : context (c) {}
  129. // The windowing code will call this when a paint callback happens
  130. void handleCommandMessage (int) override { context.triggerRepaint(); }
  131. NativeContext& context;
  132. };
  133. std::unique_ptr<DummyComponent> dummyComponent;
  134. std::unique_ptr<ComponentPeer> nativeWindow;
  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 (juce_shouldDoubleScaleNativeGLWindow())
  157. newScale *= newScale;
  158. if (! approximatelyEqual (newScale, nativeScaleFactor))
  159. {
  160. nativeScaleFactor = newScale;
  161. updateWindowPosition (peer->getAreaCoveredBy (*safeComponent));
  162. }
  163. }
  164. }
  165. }
  166. #endif
  167. void initialiseGLExtensions()
  168. {
  169. #define JUCE_INIT_WGL_FUNCTION(name) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  170. JUCE_INIT_WGL_FUNCTION (wglChoosePixelFormatARB);
  171. JUCE_INIT_WGL_FUNCTION (wglSwapIntervalEXT);
  172. JUCE_INIT_WGL_FUNCTION (wglGetSwapIntervalEXT);
  173. #undef JUCE_INIT_WGL_FUNCTION
  174. }
  175. void createNativeWindow (Component& component)
  176. {
  177. auto* topComp = component.getTopLevelComponent();
  178. nativeWindow.reset (createNonRepaintingEmbeddedWindowsPeer (*dummyComponent, topComp->getWindowHandle()));
  179. if (auto* peer = topComp->getPeer())
  180. {
  181. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  182. safeComponent = Component::SafePointer<Component> (&component);
  183. nativeScaleFactor = peer->getPlatformScaleFactor();
  184. if (juce_shouldDoubleScaleNativeGLWindow())
  185. nativeScaleFactor *= nativeScaleFactor;
  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 != 0)
  196. {
  197. wglDeleteContext (renderContext);
  198. renderContext = 0;
  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() != 0;
  272. }
  273. } // namespace juce