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.

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