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