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.

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