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.

337 lines
12KB

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