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.

269 lines
10KB

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