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.

344 lines
12KB

  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. enum
  19. {
  20. WGL_NUMBER_PIXEL_FORMATS_ARB = 0x2000,
  21. WGL_DRAW_TO_WINDOW_ARB = 0x2001,
  22. WGL_ACCELERATION_ARB = 0x2003,
  23. WGL_SWAP_METHOD_ARB = 0x2007,
  24. WGL_SUPPORT_OPENGL_ARB = 0x2010,
  25. WGL_PIXEL_TYPE_ARB = 0x2013,
  26. WGL_DOUBLE_BUFFER_ARB = 0x2011,
  27. WGL_COLOR_BITS_ARB = 0x2014,
  28. WGL_RED_BITS_ARB = 0x2015,
  29. WGL_GREEN_BITS_ARB = 0x2017,
  30. WGL_BLUE_BITS_ARB = 0x2019,
  31. WGL_ALPHA_BITS_ARB = 0x201B,
  32. WGL_DEPTH_BITS_ARB = 0x2022,
  33. WGL_STENCIL_BITS_ARB = 0x2023,
  34. WGL_FULL_ACCELERATION_ARB = 0x2027,
  35. WGL_ACCUM_RED_BITS_ARB = 0x201E,
  36. WGL_ACCUM_GREEN_BITS_ARB = 0x201F,
  37. WGL_ACCUM_BLUE_BITS_ARB = 0x2020,
  38. WGL_ACCUM_ALPHA_BITS_ARB = 0x2021,
  39. WGL_STEREO_ARB = 0x2012,
  40. WGL_SAMPLE_BUFFERS_ARB = 0x2041,
  41. WGL_SAMPLES_ARB = 0x2042,
  42. WGL_TYPE_RGBA_ARB = 0x202B
  43. };
  44. typedef BOOL (WINAPI* PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC, const int*, const FLOAT*, UINT, int*, UINT*);
  45. typedef BOOL (WINAPI* PFNWGLSWAPINTERVALEXTPROC) (int);
  46. typedef int (WINAPI* PFNWGLGETSWAPINTERVALEXTPROC)();
  47. static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = 0;
  48. static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = 0;
  49. static PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT = 0;
  50. static void initialiseGLExtensions()
  51. {
  52. if (wglChoosePixelFormatARB == 0)
  53. {
  54. wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC) OpenGLHelpers::getExtensionFunction ("wglChoosePixelFormatARB");
  55. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) OpenGLHelpers::getExtensionFunction ("wglSwapIntervalEXT");
  56. wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC) OpenGLHelpers::getExtensionFunction ("wglGetSwapIntervalEXT");
  57. }
  58. }
  59. extern ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component* component, void* parent);
  60. //==============================================================================
  61. class WindowedGLContext : public OpenGLContext
  62. {
  63. public:
  64. WindowedGLContext (Component* const component_,
  65. HGLRC contextToShareWith,
  66. const OpenGLPixelFormat& pixelFormat)
  67. : renderContext (0),
  68. component (component_),
  69. dc (0)
  70. {
  71. jassert (component != nullptr);
  72. createNativeWindow();
  73. PIXELFORMATDESCRIPTOR pfd;
  74. initialisePixelFormatDescriptor (pfd, pixelFormat);
  75. const int format = ChoosePixelFormat (dc, &pfd);
  76. if (format != 0)
  77. SetPixelFormat (dc, format, &pfd);
  78. renderContext = wglCreateContext (dc);
  79. if (renderContext != 0)
  80. {
  81. makeActive();
  82. initialiseGLExtensions();
  83. setPixelFormat (pixelFormat);
  84. if (contextToShareWith != 0)
  85. wglShareLists (contextToShareWith, renderContext);
  86. }
  87. }
  88. ~WindowedGLContext()
  89. {
  90. deleteContext();
  91. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  92. nativeWindow = nullptr;
  93. }
  94. void deleteContext()
  95. {
  96. makeInactive();
  97. if (renderContext != 0)
  98. {
  99. wglDeleteContext (renderContext);
  100. renderContext = 0;
  101. }
  102. }
  103. bool makeActive() const noexcept
  104. {
  105. jassert (renderContext != 0);
  106. return wglMakeCurrent (dc, renderContext) != 0;
  107. }
  108. bool makeInactive() const noexcept
  109. {
  110. return (! isActive()) || (wglMakeCurrent (0, 0) != 0);
  111. }
  112. bool isActive() const noexcept
  113. {
  114. return wglGetCurrentContext() == renderContext;
  115. }
  116. void* getRawContext() const noexcept
  117. {
  118. return renderContext;
  119. }
  120. unsigned int getFrameBufferID() const
  121. {
  122. return 0;
  123. }
  124. bool setPixelFormat (const OpenGLPixelFormat& pixelFormat)
  125. {
  126. makeActive();
  127. PIXELFORMATDESCRIPTOR pfd;
  128. initialisePixelFormatDescriptor (pfd, pixelFormat);
  129. int format = 0;
  130. if (wglChoosePixelFormatARB != nullptr)
  131. {
  132. int atts[64];
  133. int n = 0;
  134. atts[n++] = WGL_DRAW_TO_WINDOW_ARB;
  135. atts[n++] = GL_TRUE;
  136. atts[n++] = WGL_SUPPORT_OPENGL_ARB;
  137. atts[n++] = GL_TRUE;
  138. atts[n++] = WGL_ACCELERATION_ARB;
  139. atts[n++] = WGL_FULL_ACCELERATION_ARB;
  140. atts[n++] = WGL_DOUBLE_BUFFER_ARB;
  141. atts[n++] = GL_TRUE;
  142. atts[n++] = WGL_PIXEL_TYPE_ARB;
  143. atts[n++] = WGL_TYPE_RGBA_ARB;
  144. atts[n++] = WGL_COLOR_BITS_ARB;
  145. atts[n++] = pfd.cColorBits;
  146. atts[n++] = WGL_RED_BITS_ARB;
  147. atts[n++] = pixelFormat.redBits;
  148. atts[n++] = WGL_GREEN_BITS_ARB;
  149. atts[n++] = pixelFormat.greenBits;
  150. atts[n++] = WGL_BLUE_BITS_ARB;
  151. atts[n++] = pixelFormat.blueBits;
  152. atts[n++] = WGL_ALPHA_BITS_ARB;
  153. atts[n++] = pixelFormat.alphaBits;
  154. atts[n++] = WGL_DEPTH_BITS_ARB;
  155. atts[n++] = pixelFormat.depthBufferBits;
  156. atts[n++] = WGL_STENCIL_BITS_ARB;
  157. atts[n++] = pixelFormat.stencilBufferBits;
  158. atts[n++] = WGL_ACCUM_RED_BITS_ARB;
  159. atts[n++] = pixelFormat.accumulationBufferRedBits;
  160. atts[n++] = WGL_ACCUM_GREEN_BITS_ARB;
  161. atts[n++] = pixelFormat.accumulationBufferGreenBits;
  162. atts[n++] = WGL_ACCUM_BLUE_BITS_ARB;
  163. atts[n++] = pixelFormat.accumulationBufferBlueBits;
  164. atts[n++] = WGL_ACCUM_ALPHA_BITS_ARB;
  165. atts[n++] = pixelFormat.accumulationBufferAlphaBits;
  166. if (pixelFormat.multisamplingLevel > 0
  167. && OpenGLHelpers::isExtensionSupported ("GL_ARB_multisample"))
  168. {
  169. atts[n++] = WGL_SAMPLE_BUFFERS_ARB;
  170. atts[n++] = 1;
  171. atts[n++] = WGL_SAMPLES_ARB;
  172. atts[n++] = pixelFormat.multisamplingLevel;
  173. }
  174. atts[n++] = 0;
  175. jassert (n <= numElementsInArray (atts));
  176. UINT formatsCount = 0;
  177. wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount);
  178. }
  179. if (format == 0)
  180. format = ChoosePixelFormat (dc, &pfd);
  181. if (format != 0)
  182. {
  183. makeInactive();
  184. // win32 can't change the pixel format of a window, so need to delete the
  185. // old one and create a new one..
  186. jassert (nativeWindow != 0);
  187. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  188. nativeWindow = nullptr;
  189. createNativeWindow();
  190. if (SetPixelFormat (dc, format, &pfd))
  191. {
  192. wglDeleteContext (renderContext);
  193. renderContext = wglCreateContext (dc);
  194. jassert (renderContext != 0);
  195. return renderContext != 0;
  196. }
  197. }
  198. return false;
  199. }
  200. void swapBuffers()
  201. {
  202. SwapBuffers (dc);
  203. }
  204. bool setSwapInterval (int numFramesPerSwap)
  205. {
  206. makeActive();
  207. return wglSwapIntervalEXT != nullptr && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  208. }
  209. int getSwapInterval() const
  210. {
  211. makeActive();
  212. return wglGetSwapIntervalEXT != nullptr ? wglGetSwapIntervalEXT() : 0;
  213. }
  214. void* getNativeWindowHandle() const
  215. {
  216. return nativeWindow != nullptr ? nativeWindow->getNativeHandle() : nullptr;
  217. }
  218. //==============================================================================
  219. HGLRC renderContext;
  220. ScopedPointer<ComponentPeer> nativeWindow;
  221. private:
  222. Component* const component;
  223. HDC dc;
  224. //==============================================================================
  225. void createNativeWindow()
  226. {
  227. nativeWindow = createNonRepaintingEmbeddedWindowsPeer (component, component->getTopLevelComponent()->getWindowHandle());
  228. nativeWindow->setVisible (true);
  229. dc = GetDC ((HWND) nativeWindow->getNativeHandle());
  230. }
  231. static void initialisePixelFormatDescriptor (PIXELFORMATDESCRIPTOR& pfd, const OpenGLPixelFormat& pixelFormat)
  232. {
  233. zerostruct (pfd);
  234. pfd.nSize = sizeof (pfd);
  235. pfd.nVersion = 1;
  236. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  237. pfd.iPixelType = PFD_TYPE_RGBA;
  238. pfd.iLayerType = PFD_MAIN_PLANE;
  239. pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
  240. pfd.cRedBits = (BYTE) pixelFormat.redBits;
  241. pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
  242. pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
  243. pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
  244. pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
  245. pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
  246. pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  247. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
  248. pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
  249. pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
  250. pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
  251. pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
  252. }
  253. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  254. };
  255. //==============================================================================
  256. OpenGLContext* OpenGLComponent::createContext()
  257. {
  258. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this,
  259. contextToShareListsWith != nullptr ? (HGLRC) contextToShareListsWith->getRawContext() : 0,
  260. preferredPixelFormat));
  261. return (c->renderContext != 0) ? c.release() : nullptr;
  262. }
  263. void* OpenGLComponent::getNativeWindowHandle() const
  264. {
  265. return context != nullptr ? static_cast<WindowedGLContext*> (context.get())->getNativeWindowHandle() : nullptr;
  266. }
  267. void OpenGLComponent::updateEmbeddedPosition (const Rectangle<int>& bounds)
  268. {
  269. if (context != nullptr)
  270. {
  271. ComponentPeer* peer = static_cast<WindowedGLContext*> (context.get())->nativeWindow;
  272. SetWindowPos ((HWND) peer->getNativeHandle(), 0,
  273. bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
  274. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  275. }
  276. }
  277. //==============================================================================
  278. bool OpenGLHelpers::isContextActive()
  279. {
  280. return wglGetCurrentContext() != 0;
  281. }