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.

279 lines
11KB

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