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.

290 lines
11KB

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