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.

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