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.

267 lines
11KB

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