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.

259 lines
9.9KB

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