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.

540 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. int x, y, w, h;
  238. nativeWindow->getBounds (x, y, w, h);
  239. nativeWindow->repaint (0, 0, w, h);
  240. }
  241. void swapBuffers()
  242. {
  243. SwapBuffers (dc);
  244. }
  245. bool setSwapInterval (const int numFramesPerSwap)
  246. {
  247. makeActive();
  248. StringArray availableExtensions;
  249. getWglExtensions (dc, availableExtensions);
  250. PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = 0;
  251. return availableExtensions.contains ("WGL_EXT_swap_control")
  252. && WGL_EXT_FUNCTION_INIT (PFNWGLSWAPINTERVALEXTPROC, wglSwapIntervalEXT)
  253. && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  254. }
  255. int getSwapInterval() const
  256. {
  257. makeActive();
  258. StringArray availableExtensions;
  259. getWglExtensions (dc, availableExtensions);
  260. PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT = 0;
  261. if (availableExtensions.contains ("WGL_EXT_swap_control")
  262. && WGL_EXT_FUNCTION_INIT (PFNWGLGETSWAPINTERVALEXTPROC, wglGetSwapIntervalEXT))
  263. return wglGetSwapIntervalEXT();
  264. return 0;
  265. }
  266. void findAlternativeOpenGLPixelFormats (OwnedArray <OpenGLPixelFormat>& results)
  267. {
  268. jassert (isActive());
  269. StringArray availableExtensions;
  270. getWglExtensions (dc, availableExtensions);
  271. PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;
  272. int numTypes = 0;
  273. if (availableExtensions.contains("WGL_ARB_pixel_format")
  274. && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
  275. {
  276. int attributes = WGL_NUMBER_PIXEL_FORMATS_ARB;
  277. if (! wglGetPixelFormatAttribivARB (dc, 1, 0, 1, &attributes, &numTypes))
  278. jassertfalse
  279. }
  280. else
  281. {
  282. numTypes = DescribePixelFormat (dc, 0, 0, 0);
  283. }
  284. OpenGLPixelFormat pf;
  285. for (int i = 0; i < numTypes; ++i)
  286. {
  287. if (fillInPixelFormatDetails (i + 1, pf, availableExtensions))
  288. {
  289. bool alreadyListed = false;
  290. for (int j = results.size(); --j >= 0;)
  291. if (pf == *results.getUnchecked(j))
  292. alreadyListed = true;
  293. if (! alreadyListed)
  294. results.add (new OpenGLPixelFormat (pf));
  295. }
  296. }
  297. }
  298. void* getNativeWindowHandle() const
  299. {
  300. return nativeWindow != 0 ? nativeWindow->getNativeHandle() : 0;
  301. }
  302. //==============================================================================
  303. juce_UseDebuggingNewOperator
  304. HGLRC renderContext;
  305. private:
  306. Win32ComponentPeer* nativeWindow;
  307. Component* const component;
  308. HDC dc;
  309. //==============================================================================
  310. void createNativeWindow()
  311. {
  312. nativeWindow = new Win32ComponentPeer (component, 0);
  313. nativeWindow->dontRepaint = true;
  314. nativeWindow->setVisible (true);
  315. HWND hwnd = (HWND) nativeWindow->getNativeHandle();
  316. Win32ComponentPeer* const peer = dynamic_cast <Win32ComponentPeer*> (component->getTopLevelComponent()->getPeer());
  317. if (peer != 0)
  318. {
  319. SetParent (hwnd, (HWND) peer->getNativeHandle());
  320. juce_setWindowStyleBit (hwnd, GWL_STYLE, WS_CHILD, true);
  321. juce_setWindowStyleBit (hwnd, GWL_STYLE, WS_POPUP, false);
  322. }
  323. dc = GetDC (hwnd);
  324. }
  325. bool fillInPixelFormatDetails (const int pixelFormatIndex,
  326. OpenGLPixelFormat& result,
  327. const StringArray& availableExtensions) const throw()
  328. {
  329. PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;
  330. if (availableExtensions.contains ("WGL_ARB_pixel_format")
  331. && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
  332. {
  333. int attributes[32];
  334. int numAttributes = 0;
  335. attributes[numAttributes++] = WGL_DRAW_TO_WINDOW_ARB;
  336. attributes[numAttributes++] = WGL_SUPPORT_OPENGL_ARB;
  337. attributes[numAttributes++] = WGL_ACCELERATION_ARB;
  338. attributes[numAttributes++] = WGL_DOUBLE_BUFFER_ARB;
  339. attributes[numAttributes++] = WGL_PIXEL_TYPE_ARB;
  340. attributes[numAttributes++] = WGL_RED_BITS_ARB;
  341. attributes[numAttributes++] = WGL_GREEN_BITS_ARB;
  342. attributes[numAttributes++] = WGL_BLUE_BITS_ARB;
  343. attributes[numAttributes++] = WGL_ALPHA_BITS_ARB;
  344. attributes[numAttributes++] = WGL_DEPTH_BITS_ARB;
  345. attributes[numAttributes++] = WGL_STENCIL_BITS_ARB;
  346. attributes[numAttributes++] = WGL_ACCUM_RED_BITS_ARB;
  347. attributes[numAttributes++] = WGL_ACCUM_GREEN_BITS_ARB;
  348. attributes[numAttributes++] = WGL_ACCUM_BLUE_BITS_ARB;
  349. attributes[numAttributes++] = WGL_ACCUM_ALPHA_BITS_ARB;
  350. if (availableExtensions.contains ("WGL_ARB_multisample"))
  351. attributes[numAttributes++] = WGL_SAMPLES_ARB;
  352. int values[32];
  353. zeromem (values, sizeof (values));
  354. if (wglGetPixelFormatAttribivARB (dc, pixelFormatIndex, 0, numAttributes, attributes, values))
  355. {
  356. int n = 0;
  357. bool isValidFormat = (values[n++] == GL_TRUE); // WGL_DRAW_TO_WINDOW_ARB
  358. isValidFormat = (values[n++] == GL_TRUE) && isValidFormat; // WGL_SUPPORT_OPENGL_ARB
  359. isValidFormat = (values[n++] == WGL_FULL_ACCELERATION_ARB) && isValidFormat; // WGL_ACCELERATION_ARB
  360. isValidFormat = (values[n++] == GL_TRUE) && isValidFormat; // WGL_DOUBLE_BUFFER_ARB:
  361. isValidFormat = (values[n++] == WGL_TYPE_RGBA_ARB) && isValidFormat; // WGL_PIXEL_TYPE_ARB
  362. result.redBits = values[n++]; // WGL_RED_BITS_ARB
  363. result.greenBits = values[n++]; // WGL_GREEN_BITS_ARB
  364. result.blueBits = values[n++]; // WGL_BLUE_BITS_ARB
  365. result.alphaBits = values[n++]; // WGL_ALPHA_BITS_ARB
  366. result.depthBufferBits = values[n++]; // WGL_DEPTH_BITS_ARB
  367. result.stencilBufferBits = values[n++]; // WGL_STENCIL_BITS_ARB
  368. result.accumulationBufferRedBits = values[n++]; // WGL_ACCUM_RED_BITS_ARB
  369. result.accumulationBufferGreenBits = values[n++]; // WGL_ACCUM_GREEN_BITS_ARB
  370. result.accumulationBufferBlueBits = values[n++]; // WGL_ACCUM_BLUE_BITS_ARB
  371. result.accumulationBufferAlphaBits = values[n++]; // WGL_ACCUM_ALPHA_BITS_ARB
  372. result.fullSceneAntiAliasingNumSamples = (uint8) values[n++]; // WGL_SAMPLES_ARB
  373. return isValidFormat;
  374. }
  375. else
  376. {
  377. jassertfalse
  378. }
  379. }
  380. else
  381. {
  382. PIXELFORMATDESCRIPTOR pfd;
  383. if (DescribePixelFormat (dc, pixelFormatIndex, sizeof (pfd), &pfd))
  384. {
  385. result.redBits = pfd.cRedBits;
  386. result.greenBits = pfd.cGreenBits;
  387. result.blueBits = pfd.cBlueBits;
  388. result.alphaBits = pfd.cAlphaBits;
  389. result.depthBufferBits = pfd.cDepthBits;
  390. result.stencilBufferBits = pfd.cStencilBits;
  391. result.accumulationBufferRedBits = pfd.cAccumRedBits;
  392. result.accumulationBufferGreenBits = pfd.cAccumGreenBits;
  393. result.accumulationBufferBlueBits = pfd.cAccumBlueBits;
  394. result.accumulationBufferAlphaBits = pfd.cAccumAlphaBits;
  395. result.fullSceneAntiAliasingNumSamples = 0;
  396. return true;
  397. }
  398. else
  399. {
  400. jassertfalse
  401. }
  402. }
  403. return false;
  404. }
  405. WindowedGLContext (const WindowedGLContext&);
  406. const WindowedGLContext& operator= (const WindowedGLContext&);
  407. };
  408. //==============================================================================
  409. OpenGLContext* OpenGLContext::createContextForWindow (Component* const component,
  410. const OpenGLPixelFormat& pixelFormat,
  411. const OpenGLContext* const contextToShareWith)
  412. {
  413. WindowedGLContext* c = new WindowedGLContext (component,
  414. contextToShareWith != 0 ? (HGLRC) contextToShareWith->getRawContext() : 0,
  415. pixelFormat);
  416. if (c->renderContext == 0)
  417. deleteAndZero (c);
  418. return c;
  419. }
  420. void* OpenGLComponent::getNativeWindowHandle() const
  421. {
  422. return context != 0 ? ((WindowedGLContext*) context)->getNativeWindowHandle() : 0;
  423. }
  424. void juce_glViewport (const int w, const int h)
  425. {
  426. glViewport (0, 0, w, h);
  427. }
  428. void OpenGLPixelFormat::getAvailablePixelFormats (Component* component,
  429. OwnedArray <OpenGLPixelFormat>& results)
  430. {
  431. Component tempComp;
  432. {
  433. WindowedGLContext wc (component, 0, OpenGLPixelFormat (8, 8, 16, 0));
  434. wc.makeActive();
  435. wc.findAlternativeOpenGLPixelFormats (results);
  436. }
  437. }
  438. #endif