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.

534 lines
20KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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. // (This file gets included by juce_win32_NativeCode.cpp, rather than being
  19. // compiled on its own).
  20. #if JUCE_INCLUDED_FILE && JUCE_OPENGL
  21. //==============================================================================
  22. #define WGL_EXT_FUNCTION_INIT(extType, extFunc) \
  23. ((extFunc = (extType) wglGetProcAddress (#extFunc)) != 0)
  24. typedef const char* (WINAPI* PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
  25. typedef BOOL (WINAPI * PFNWGLGETPIXELFORMATATTRIBIVARBPROC) (HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues);
  26. typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int* piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
  27. typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
  28. typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
  29. #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
  30. #define WGL_DRAW_TO_WINDOW_ARB 0x2001
  31. #define WGL_ACCELERATION_ARB 0x2003
  32. #define WGL_SWAP_METHOD_ARB 0x2007
  33. #define WGL_SUPPORT_OPENGL_ARB 0x2010
  34. #define WGL_PIXEL_TYPE_ARB 0x2013
  35. #define WGL_DOUBLE_BUFFER_ARB 0x2011
  36. #define WGL_COLOR_BITS_ARB 0x2014
  37. #define WGL_RED_BITS_ARB 0x2015
  38. #define WGL_GREEN_BITS_ARB 0x2017
  39. #define WGL_BLUE_BITS_ARB 0x2019
  40. #define WGL_ALPHA_BITS_ARB 0x201B
  41. #define WGL_DEPTH_BITS_ARB 0x2022
  42. #define WGL_STENCIL_BITS_ARB 0x2023
  43. #define WGL_FULL_ACCELERATION_ARB 0x2027
  44. #define WGL_ACCUM_RED_BITS_ARB 0x201E
  45. #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
  46. #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
  47. #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
  48. #define WGL_STEREO_ARB 0x2012
  49. #define WGL_SAMPLE_BUFFERS_ARB 0x2041
  50. #define WGL_SAMPLES_ARB 0x2042
  51. #define WGL_TYPE_RGBA_ARB 0x202B
  52. static void getWglExtensions (HDC dc, StringArray& result) throw()
  53. {
  54. PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = 0;
  55. if (WGL_EXT_FUNCTION_INIT (PFNWGLGETEXTENSIONSSTRINGARBPROC, wglGetExtensionsStringARB))
  56. result.addTokens (String (wglGetExtensionsStringARB (dc)), false);
  57. else
  58. jassertfalse // If this fails, it may be because you didn't activate the openGL context
  59. }
  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. nativeWindow (0),
  69. dc (0),
  70. component (component_)
  71. {
  72. jassert (component != 0);
  73. createNativeWindow();
  74. // Use a default pixel format that should be supported everywhere
  75. PIXELFORMATDESCRIPTOR pfd;
  76. zerostruct (pfd);
  77. pfd.nSize = sizeof (pfd);
  78. pfd.nVersion = 1;
  79. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  80. pfd.iPixelType = PFD_TYPE_RGBA;
  81. pfd.cColorBits = 24;
  82. pfd.cDepthBits = 16;
  83. const int format = ChoosePixelFormat (dc, &pfd);
  84. if (format != 0)
  85. SetPixelFormat (dc, format, &pfd);
  86. renderContext = wglCreateContext (dc);
  87. makeActive();
  88. setPixelFormat (pixelFormat);
  89. if (contextToShareWith != 0 && renderContext != 0)
  90. wglShareLists (contextToShareWith, renderContext);
  91. }
  92. ~WindowedGLContext()
  93. {
  94. makeInactive();
  95. wglDeleteContext (renderContext);
  96. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  97. delete nativeWindow;
  98. }
  99. bool makeActive() const throw()
  100. {
  101. jassert (renderContext != 0);
  102. return wglMakeCurrent (dc, renderContext) != 0;
  103. }
  104. bool makeInactive() const throw()
  105. {
  106. return (! isActive()) || (wglMakeCurrent (0, 0) != 0);
  107. }
  108. bool isActive() const throw()
  109. {
  110. return wglGetCurrentContext() == renderContext;
  111. }
  112. const OpenGLPixelFormat getPixelFormat() const
  113. {
  114. OpenGLPixelFormat pf;
  115. makeActive();
  116. StringArray availableExtensions;
  117. getWglExtensions (dc, availableExtensions);
  118. fillInPixelFormatDetails (GetPixelFormat (dc), pf, availableExtensions);
  119. return pf;
  120. }
  121. void* getRawContext() const throw()
  122. {
  123. return renderContext;
  124. }
  125. bool setPixelFormat (const OpenGLPixelFormat& pixelFormat)
  126. {
  127. makeActive();
  128. PIXELFORMATDESCRIPTOR pfd;
  129. zerostruct (pfd);
  130. pfd.nSize = sizeof (pfd);
  131. pfd.nVersion = 1;
  132. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  133. pfd.iPixelType = PFD_TYPE_RGBA;
  134. pfd.iLayerType = PFD_MAIN_PLANE;
  135. pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
  136. pfd.cRedBits = (BYTE) pixelFormat.redBits;
  137. pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
  138. pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
  139. pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
  140. pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
  141. pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
  142. pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  143. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
  144. pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
  145. pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
  146. pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
  147. pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
  148. int format = 0;
  149. PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = 0;
  150. StringArray availableExtensions;
  151. getWglExtensions (dc, availableExtensions);
  152. if (availableExtensions.contains ("WGL_ARB_pixel_format")
  153. && WGL_EXT_FUNCTION_INIT (PFNWGLCHOOSEPIXELFORMATARBPROC, wglChoosePixelFormatARB))
  154. {
  155. int attributes[64];
  156. int n = 0;
  157. attributes[n++] = WGL_DRAW_TO_WINDOW_ARB;
  158. attributes[n++] = GL_TRUE;
  159. attributes[n++] = WGL_SUPPORT_OPENGL_ARB;
  160. attributes[n++] = GL_TRUE;
  161. attributes[n++] = WGL_ACCELERATION_ARB;
  162. attributes[n++] = WGL_FULL_ACCELERATION_ARB;
  163. attributes[n++] = WGL_DOUBLE_BUFFER_ARB;
  164. attributes[n++] = GL_TRUE;
  165. attributes[n++] = WGL_PIXEL_TYPE_ARB;
  166. attributes[n++] = WGL_TYPE_RGBA_ARB;
  167. attributes[n++] = WGL_COLOR_BITS_ARB;
  168. attributes[n++] = pfd.cColorBits;
  169. attributes[n++] = WGL_RED_BITS_ARB;
  170. attributes[n++] = pixelFormat.redBits;
  171. attributes[n++] = WGL_GREEN_BITS_ARB;
  172. attributes[n++] = pixelFormat.greenBits;
  173. attributes[n++] = WGL_BLUE_BITS_ARB;
  174. attributes[n++] = pixelFormat.blueBits;
  175. attributes[n++] = WGL_ALPHA_BITS_ARB;
  176. attributes[n++] = pixelFormat.alphaBits;
  177. attributes[n++] = WGL_DEPTH_BITS_ARB;
  178. attributes[n++] = pixelFormat.depthBufferBits;
  179. if (pixelFormat.stencilBufferBits > 0)
  180. {
  181. attributes[n++] = WGL_STENCIL_BITS_ARB;
  182. attributes[n++] = pixelFormat.stencilBufferBits;
  183. }
  184. attributes[n++] = WGL_ACCUM_RED_BITS_ARB;
  185. attributes[n++] = pixelFormat.accumulationBufferRedBits;
  186. attributes[n++] = WGL_ACCUM_GREEN_BITS_ARB;
  187. attributes[n++] = pixelFormat.accumulationBufferGreenBits;
  188. attributes[n++] = WGL_ACCUM_BLUE_BITS_ARB;
  189. attributes[n++] = pixelFormat.accumulationBufferBlueBits;
  190. attributes[n++] = WGL_ACCUM_ALPHA_BITS_ARB;
  191. attributes[n++] = pixelFormat.accumulationBufferAlphaBits;
  192. if (availableExtensions.contains ("WGL_ARB_multisample")
  193. && pixelFormat.fullSceneAntiAliasingNumSamples > 0)
  194. {
  195. attributes[n++] = WGL_SAMPLE_BUFFERS_ARB;
  196. attributes[n++] = 1;
  197. attributes[n++] = WGL_SAMPLES_ARB;
  198. attributes[n++] = pixelFormat.fullSceneAntiAliasingNumSamples;
  199. }
  200. attributes[n++] = 0;
  201. UINT formatsCount;
  202. const BOOL ok = wglChoosePixelFormatARB (dc, attributes, 0, 1, &format, &formatsCount);
  203. (void) ok;
  204. jassert (ok);
  205. }
  206. else
  207. {
  208. format = ChoosePixelFormat (dc, &pfd);
  209. }
  210. if (format != 0)
  211. {
  212. makeInactive();
  213. // win32 can't change the pixel format of a window, so need to delete the
  214. // old one and create a new one..
  215. jassert (nativeWindow != 0);
  216. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  217. delete nativeWindow;
  218. createNativeWindow();
  219. if (SetPixelFormat (dc, format, &pfd))
  220. {
  221. wglDeleteContext (renderContext);
  222. renderContext = wglCreateContext (dc);
  223. jassert (renderContext != 0);
  224. return renderContext != 0;
  225. }
  226. }
  227. return false;
  228. }
  229. void updateWindowPosition (int x, int y, int w, int h, int)
  230. {
  231. SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
  232. x, y, w, h,
  233. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  234. }
  235. void repaint()
  236. {
  237. const Rectangle<int> bounds (nativeWindow->getBounds());
  238. nativeWindow->repaint (0, 0, bounds.getWidth(), bounds.getHeight());
  239. }
  240. void swapBuffers()
  241. {
  242. SwapBuffers (dc);
  243. }
  244. bool setSwapInterval (int numFramesPerSwap)
  245. {
  246. makeActive();
  247. StringArray availableExtensions;
  248. getWglExtensions (dc, availableExtensions);
  249. PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = 0;
  250. return availableExtensions.contains ("WGL_EXT_swap_control")
  251. && WGL_EXT_FUNCTION_INIT (PFNWGLSWAPINTERVALEXTPROC, wglSwapIntervalEXT)
  252. && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  253. }
  254. int getSwapInterval() const
  255. {
  256. makeActive();
  257. StringArray availableExtensions;
  258. getWglExtensions (dc, availableExtensions);
  259. PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT = 0;
  260. if (availableExtensions.contains ("WGL_EXT_swap_control")
  261. && WGL_EXT_FUNCTION_INIT (PFNWGLGETSWAPINTERVALEXTPROC, wglGetSwapIntervalEXT))
  262. return wglGetSwapIntervalEXT();
  263. return 0;
  264. }
  265. void findAlternativeOpenGLPixelFormats (OwnedArray <OpenGLPixelFormat>& results)
  266. {
  267. jassert (isActive());
  268. StringArray availableExtensions;
  269. getWglExtensions (dc, availableExtensions);
  270. PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;
  271. int numTypes = 0;
  272. if (availableExtensions.contains("WGL_ARB_pixel_format")
  273. && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
  274. {
  275. int attributes = WGL_NUMBER_PIXEL_FORMATS_ARB;
  276. if (! wglGetPixelFormatAttribivARB (dc, 1, 0, 1, &attributes, &numTypes))
  277. jassertfalse
  278. }
  279. else
  280. {
  281. numTypes = DescribePixelFormat (dc, 0, 0, 0);
  282. }
  283. OpenGLPixelFormat pf;
  284. for (int i = 0; i < numTypes; ++i)
  285. {
  286. if (fillInPixelFormatDetails (i + 1, pf, availableExtensions))
  287. {
  288. bool alreadyListed = false;
  289. for (int j = results.size(); --j >= 0;)
  290. if (pf == *results.getUnchecked(j))
  291. alreadyListed = true;
  292. if (! alreadyListed)
  293. results.add (new OpenGLPixelFormat (pf));
  294. }
  295. }
  296. }
  297. void* getNativeWindowHandle() const
  298. {
  299. return nativeWindow != 0 ? nativeWindow->getNativeHandle() : 0;
  300. }
  301. //==============================================================================
  302. juce_UseDebuggingNewOperator
  303. HGLRC renderContext;
  304. private:
  305. Win32ComponentPeer* nativeWindow;
  306. Component* const component;
  307. HDC dc;
  308. //==============================================================================
  309. void createNativeWindow()
  310. {
  311. nativeWindow = new Win32ComponentPeer (component, 0);
  312. nativeWindow->dontRepaint = true;
  313. nativeWindow->setVisible (true);
  314. HWND hwnd = (HWND) nativeWindow->getNativeHandle();
  315. Win32ComponentPeer* const peer = dynamic_cast <Win32ComponentPeer*> (component->getTopLevelComponent()->getPeer());
  316. if (peer != 0)
  317. {
  318. SetParent (hwnd, (HWND) peer->getNativeHandle());
  319. juce_setWindowStyleBit (hwnd, GWL_STYLE, WS_CHILD, true);
  320. juce_setWindowStyleBit (hwnd, GWL_STYLE, WS_POPUP, false);
  321. }
  322. dc = GetDC (hwnd);
  323. }
  324. bool fillInPixelFormatDetails (const int pixelFormatIndex,
  325. OpenGLPixelFormat& result,
  326. const StringArray& availableExtensions) const throw()
  327. {
  328. PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;
  329. if (availableExtensions.contains ("WGL_ARB_pixel_format")
  330. && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
  331. {
  332. int attributes[32];
  333. int numAttributes = 0;
  334. attributes[numAttributes++] = WGL_DRAW_TO_WINDOW_ARB;
  335. attributes[numAttributes++] = WGL_SUPPORT_OPENGL_ARB;
  336. attributes[numAttributes++] = WGL_ACCELERATION_ARB;
  337. attributes[numAttributes++] = WGL_DOUBLE_BUFFER_ARB;
  338. attributes[numAttributes++] = WGL_PIXEL_TYPE_ARB;
  339. attributes[numAttributes++] = WGL_RED_BITS_ARB;
  340. attributes[numAttributes++] = WGL_GREEN_BITS_ARB;
  341. attributes[numAttributes++] = WGL_BLUE_BITS_ARB;
  342. attributes[numAttributes++] = WGL_ALPHA_BITS_ARB;
  343. attributes[numAttributes++] = WGL_DEPTH_BITS_ARB;
  344. attributes[numAttributes++] = WGL_STENCIL_BITS_ARB;
  345. attributes[numAttributes++] = WGL_ACCUM_RED_BITS_ARB;
  346. attributes[numAttributes++] = WGL_ACCUM_GREEN_BITS_ARB;
  347. attributes[numAttributes++] = WGL_ACCUM_BLUE_BITS_ARB;
  348. attributes[numAttributes++] = WGL_ACCUM_ALPHA_BITS_ARB;
  349. if (availableExtensions.contains ("WGL_ARB_multisample"))
  350. attributes[numAttributes++] = WGL_SAMPLES_ARB;
  351. int values[32];
  352. zeromem (values, sizeof (values));
  353. if (wglGetPixelFormatAttribivARB (dc, pixelFormatIndex, 0, numAttributes, attributes, values))
  354. {
  355. int n = 0;
  356. bool isValidFormat = (values[n++] == GL_TRUE); // WGL_DRAW_TO_WINDOW_ARB
  357. isValidFormat = (values[n++] == GL_TRUE) && isValidFormat; // WGL_SUPPORT_OPENGL_ARB
  358. isValidFormat = (values[n++] == WGL_FULL_ACCELERATION_ARB) && isValidFormat; // WGL_ACCELERATION_ARB
  359. isValidFormat = (values[n++] == GL_TRUE) && isValidFormat; // WGL_DOUBLE_BUFFER_ARB:
  360. isValidFormat = (values[n++] == WGL_TYPE_RGBA_ARB) && isValidFormat; // WGL_PIXEL_TYPE_ARB
  361. result.redBits = values[n++]; // WGL_RED_BITS_ARB
  362. result.greenBits = values[n++]; // WGL_GREEN_BITS_ARB
  363. result.blueBits = values[n++]; // WGL_BLUE_BITS_ARB
  364. result.alphaBits = values[n++]; // WGL_ALPHA_BITS_ARB
  365. result.depthBufferBits = values[n++]; // WGL_DEPTH_BITS_ARB
  366. result.stencilBufferBits = values[n++]; // WGL_STENCIL_BITS_ARB
  367. result.accumulationBufferRedBits = values[n++]; // WGL_ACCUM_RED_BITS_ARB
  368. result.accumulationBufferGreenBits = values[n++]; // WGL_ACCUM_GREEN_BITS_ARB
  369. result.accumulationBufferBlueBits = values[n++]; // WGL_ACCUM_BLUE_BITS_ARB
  370. result.accumulationBufferAlphaBits = values[n++]; // WGL_ACCUM_ALPHA_BITS_ARB
  371. result.fullSceneAntiAliasingNumSamples = (uint8) values[n++]; // WGL_SAMPLES_ARB
  372. return isValidFormat;
  373. }
  374. else
  375. {
  376. jassertfalse
  377. }
  378. }
  379. else
  380. {
  381. PIXELFORMATDESCRIPTOR pfd;
  382. if (DescribePixelFormat (dc, pixelFormatIndex, sizeof (pfd), &pfd))
  383. {
  384. result.redBits = pfd.cRedBits;
  385. result.greenBits = pfd.cGreenBits;
  386. result.blueBits = pfd.cBlueBits;
  387. result.alphaBits = pfd.cAlphaBits;
  388. result.depthBufferBits = pfd.cDepthBits;
  389. result.stencilBufferBits = pfd.cStencilBits;
  390. result.accumulationBufferRedBits = pfd.cAccumRedBits;
  391. result.accumulationBufferGreenBits = pfd.cAccumGreenBits;
  392. result.accumulationBufferBlueBits = pfd.cAccumBlueBits;
  393. result.accumulationBufferAlphaBits = pfd.cAccumAlphaBits;
  394. result.fullSceneAntiAliasingNumSamples = 0;
  395. return true;
  396. }
  397. else
  398. {
  399. jassertfalse
  400. }
  401. }
  402. return false;
  403. }
  404. WindowedGLContext (const WindowedGLContext&);
  405. WindowedGLContext& operator= (const WindowedGLContext&);
  406. };
  407. //==============================================================================
  408. OpenGLContext* OpenGLComponent::createContext()
  409. {
  410. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this,
  411. contextToShareListsWith != 0 ? (HGLRC) contextToShareListsWith->getRawContext() : 0,
  412. preferredPixelFormat));
  413. return (c->renderContext != 0) ? c.release() : 0;
  414. }
  415. void* OpenGLComponent::getNativeWindowHandle() const
  416. {
  417. return context != 0 ? static_cast<WindowedGLContext*> (static_cast<OpenGLContext*> (context))->getNativeWindowHandle() : 0;
  418. }
  419. void juce_glViewport (const int w, const int h)
  420. {
  421. glViewport (0, 0, w, h);
  422. }
  423. void OpenGLPixelFormat::getAvailablePixelFormats (Component* component,
  424. OwnedArray <OpenGLPixelFormat>& results)
  425. {
  426. Component tempComp;
  427. {
  428. WindowedGLContext wc (component, 0, OpenGLPixelFormat (8, 8, 16, 0));
  429. wc.makeActive();
  430. wc.findAlternativeOpenGLPixelFormats (results);
  431. }
  432. }
  433. #endif