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