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.

546 lines
21KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. // (This file gets included by juce_win32_NativeCode.cpp, rather than being
  24. // compiled on its own).
  25. #if JUCE_INCLUDED_FILE && JUCE_OPENGL
  26. //==============================================================================
  27. #define WGL_EXT_FUNCTION_INIT(extType, extFunc) \
  28. ((extFunc = (extType) wglGetProcAddress (#extFunc)) != 0)
  29. typedef const char* (WINAPI* PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
  30. typedef BOOL (WINAPI * PFNWGLGETPIXELFORMATATTRIBIVARBPROC) (HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues);
  31. typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int* piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
  32. typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
  33. typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
  34. #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
  35. #define WGL_DRAW_TO_WINDOW_ARB 0x2001
  36. #define WGL_ACCELERATION_ARB 0x2003
  37. #define WGL_SWAP_METHOD_ARB 0x2007
  38. #define WGL_SUPPORT_OPENGL_ARB 0x2010
  39. #define WGL_PIXEL_TYPE_ARB 0x2013
  40. #define WGL_DOUBLE_BUFFER_ARB 0x2011
  41. #define WGL_COLOR_BITS_ARB 0x2014
  42. #define WGL_RED_BITS_ARB 0x2015
  43. #define WGL_GREEN_BITS_ARB 0x2017
  44. #define WGL_BLUE_BITS_ARB 0x2019
  45. #define WGL_ALPHA_BITS_ARB 0x201B
  46. #define WGL_DEPTH_BITS_ARB 0x2022
  47. #define WGL_STENCIL_BITS_ARB 0x2023
  48. #define WGL_FULL_ACCELERATION_ARB 0x2027
  49. #define WGL_ACCUM_RED_BITS_ARB 0x201E
  50. #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
  51. #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
  52. #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
  53. #define WGL_STEREO_ARB 0x2012
  54. #define WGL_SAMPLE_BUFFERS_ARB 0x2041
  55. #define WGL_SAMPLES_ARB 0x2042
  56. #define WGL_TYPE_RGBA_ARB 0x202B
  57. static void getWglExtensions (HDC dc, StringArray& result) throw()
  58. {
  59. PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = 0;
  60. if (WGL_EXT_FUNCTION_INIT (PFNWGLGETEXTENSIONSSTRINGARBPROC, wglGetExtensionsStringARB))
  61. result.addTokens (String (wglGetExtensionsStringARB (dc)), false);
  62. else
  63. jassertfalse // If this fails, it may be because you didn't activate the openGL context
  64. }
  65. //==============================================================================
  66. class WindowedGLContext : public OpenGLContext
  67. {
  68. public:
  69. WindowedGLContext (Component* const component_,
  70. HGLRC contextToShareWith,
  71. const OpenGLPixelFormat& pixelFormat)
  72. : renderContext (0),
  73. nativeWindow (0),
  74. dc (0),
  75. component (component_)
  76. {
  77. jassert (component != 0);
  78. createNativeWindow();
  79. // Use a default pixel format that should be supported everywhere
  80. PIXELFORMATDESCRIPTOR pfd;
  81. zerostruct (pfd);
  82. pfd.nSize = sizeof (pfd);
  83. pfd.nVersion = 1;
  84. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  85. pfd.iPixelType = PFD_TYPE_RGBA;
  86. pfd.cColorBits = 24;
  87. pfd.cDepthBits = 16;
  88. const int format = ChoosePixelFormat (dc, &pfd);
  89. if (format != 0)
  90. SetPixelFormat (dc, format, &pfd);
  91. renderContext = wglCreateContext (dc);
  92. makeActive();
  93. setPixelFormat (pixelFormat);
  94. if (contextToShareWith != 0 && renderContext != 0)
  95. wglShareLists (contextToShareWith, renderContext);
  96. }
  97. ~WindowedGLContext()
  98. {
  99. makeInactive();
  100. wglDeleteContext (renderContext);
  101. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  102. delete nativeWindow;
  103. }
  104. bool makeActive() const throw()
  105. {
  106. jassert (renderContext != 0);
  107. return wglMakeCurrent (dc, renderContext) != 0;
  108. }
  109. bool makeInactive() const throw()
  110. {
  111. return (! isActive()) || (wglMakeCurrent (0, 0) != 0);
  112. }
  113. bool isActive() const throw()
  114. {
  115. return wglGetCurrentContext() == renderContext;
  116. }
  117. const OpenGLPixelFormat getPixelFormat() const
  118. {
  119. OpenGLPixelFormat pf;
  120. makeActive();
  121. StringArray availableExtensions;
  122. getWglExtensions (dc, availableExtensions);
  123. fillInPixelFormatDetails (GetPixelFormat (dc), pf, availableExtensions);
  124. return pf;
  125. }
  126. void* getRawContext() const throw()
  127. {
  128. return renderContext;
  129. }
  130. bool setPixelFormat (const OpenGLPixelFormat& pixelFormat)
  131. {
  132. makeActive();
  133. PIXELFORMATDESCRIPTOR pfd;
  134. zerostruct (pfd);
  135. pfd.nSize = sizeof (pfd);
  136. pfd.nVersion = 1;
  137. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  138. pfd.iPixelType = PFD_TYPE_RGBA;
  139. pfd.iLayerType = PFD_MAIN_PLANE;
  140. pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
  141. pfd.cRedBits = (BYTE) pixelFormat.redBits;
  142. pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
  143. pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
  144. pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
  145. pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
  146. pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
  147. pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  148. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
  149. pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
  150. pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
  151. pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
  152. pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
  153. int format = 0;
  154. PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = 0;
  155. StringArray availableExtensions;
  156. getWglExtensions (dc, availableExtensions);
  157. if (availableExtensions.contains ("WGL_ARB_pixel_format")
  158. && WGL_EXT_FUNCTION_INIT (PFNWGLCHOOSEPIXELFORMATARBPROC, wglChoosePixelFormatARB))
  159. {
  160. int attributes[64];
  161. int n = 0;
  162. attributes[n++] = WGL_DRAW_TO_WINDOW_ARB;
  163. attributes[n++] = GL_TRUE;
  164. attributes[n++] = WGL_SUPPORT_OPENGL_ARB;
  165. attributes[n++] = GL_TRUE;
  166. attributes[n++] = WGL_ACCELERATION_ARB;
  167. attributes[n++] = WGL_FULL_ACCELERATION_ARB;
  168. attributes[n++] = WGL_DOUBLE_BUFFER_ARB;
  169. attributes[n++] = GL_TRUE;
  170. attributes[n++] = WGL_PIXEL_TYPE_ARB;
  171. attributes[n++] = WGL_TYPE_RGBA_ARB;
  172. attributes[n++] = WGL_COLOR_BITS_ARB;
  173. attributes[n++] = pfd.cColorBits;
  174. attributes[n++] = WGL_RED_BITS_ARB;
  175. attributes[n++] = pixelFormat.redBits;
  176. attributes[n++] = WGL_GREEN_BITS_ARB;
  177. attributes[n++] = pixelFormat.greenBits;
  178. attributes[n++] = WGL_BLUE_BITS_ARB;
  179. attributes[n++] = pixelFormat.blueBits;
  180. attributes[n++] = WGL_ALPHA_BITS_ARB;
  181. attributes[n++] = pixelFormat.alphaBits;
  182. attributes[n++] = WGL_DEPTH_BITS_ARB;
  183. attributes[n++] = pixelFormat.depthBufferBits;
  184. if (pixelFormat.stencilBufferBits > 0)
  185. {
  186. attributes[n++] = WGL_STENCIL_BITS_ARB;
  187. attributes[n++] = pixelFormat.stencilBufferBits;
  188. }
  189. attributes[n++] = WGL_ACCUM_RED_BITS_ARB;
  190. attributes[n++] = pixelFormat.accumulationBufferRedBits;
  191. attributes[n++] = WGL_ACCUM_GREEN_BITS_ARB;
  192. attributes[n++] = pixelFormat.accumulationBufferGreenBits;
  193. attributes[n++] = WGL_ACCUM_BLUE_BITS_ARB;
  194. attributes[n++] = pixelFormat.accumulationBufferBlueBits;
  195. attributes[n++] = WGL_ACCUM_ALPHA_BITS_ARB;
  196. attributes[n++] = pixelFormat.accumulationBufferAlphaBits;
  197. if (availableExtensions.contains ("WGL_ARB_multisample")
  198. && pixelFormat.fullSceneAntiAliasingNumSamples > 0)
  199. {
  200. attributes[n++] = WGL_SAMPLE_BUFFERS_ARB;
  201. attributes[n++] = 1;
  202. attributes[n++] = WGL_SAMPLES_ARB;
  203. attributes[n++] = pixelFormat.fullSceneAntiAliasingNumSamples;
  204. }
  205. attributes[n++] = 0;
  206. UINT formatsCount;
  207. const BOOL ok = wglChoosePixelFormatARB (dc, attributes, 0, 1, &format, &formatsCount);
  208. (void) ok;
  209. jassert (ok);
  210. }
  211. else
  212. {
  213. format = ChoosePixelFormat (dc, &pfd);
  214. }
  215. if (format != 0)
  216. {
  217. makeInactive();
  218. // win32 can't change the pixel format of a window, so need to delete the
  219. // old one and create a new one..
  220. jassert (nativeWindow != 0);
  221. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  222. delete nativeWindow;
  223. createNativeWindow();
  224. if (SetPixelFormat (dc, format, &pfd))
  225. {
  226. wglDeleteContext (renderContext);
  227. renderContext = wglCreateContext (dc);
  228. jassert (renderContext != 0);
  229. return renderContext != 0;
  230. }
  231. }
  232. return false;
  233. }
  234. void updateWindowPosition (int x, int y, int w, int h, int)
  235. {
  236. SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
  237. x, y, w, h,
  238. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  239. }
  240. void repaint()
  241. {
  242. int x, y, w, h;
  243. nativeWindow->getBounds (x, y, w, h);
  244. nativeWindow->repaint (0, 0, w, h);
  245. }
  246. void swapBuffers()
  247. {
  248. SwapBuffers (dc);
  249. }
  250. bool setSwapInterval (const 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. 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. const WindowedGLContext& operator= (const WindowedGLContext&);
  412. };
  413. //==============================================================================
  414. OpenGLContext* OpenGLContext::createContextForWindow (Component* const component,
  415. const OpenGLPixelFormat& pixelFormat,
  416. const OpenGLContext* const contextToShareWith)
  417. {
  418. WindowedGLContext* c = new WindowedGLContext (component,
  419. contextToShareWith != 0 ? (HGLRC) contextToShareWith->getRawContext() : 0,
  420. pixelFormat);
  421. if (c->renderContext == 0)
  422. deleteAndZero (c);
  423. return c;
  424. }
  425. void* OpenGLComponent::getNativeWindowHandle() const
  426. {
  427. return context != 0 ? ((WindowedGLContext*) context)->getNativeWindowHandle() : 0;
  428. }
  429. void juce_glViewport (const int w, const int h)
  430. {
  431. glViewport (0, 0, w, h);
  432. }
  433. void OpenGLPixelFormat::getAvailablePixelFormats (Component* component,
  434. OwnedArray <OpenGLPixelFormat>& results)
  435. {
  436. Component tempComp;
  437. {
  438. WindowedGLContext wc (component, 0, OpenGLPixelFormat (8, 8, 16, 0));
  439. wc.makeActive();
  440. wc.findAlternativeOpenGLPixelFormats (results);
  441. }
  442. }
  443. #endif