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.

512 lines
20KB

  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. #define WGL_EXT_FUNCTION_INIT(extType, extFunc) \
  19. ((extFunc = (extType) wglGetProcAddress (#extFunc)) != 0)
  20. typedef const char* (WINAPI* PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
  21. typedef BOOL (WINAPI * PFNWGLGETPIXELFORMATATTRIBIVARBPROC) (HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues);
  22. typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int* piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
  23. typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
  24. typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
  25. enum
  26. {
  27. WGL_NUMBER_PIXEL_FORMATS_ARB = 0x2000,
  28. WGL_DRAW_TO_WINDOW_ARB = 0x2001,
  29. WGL_ACCELERATION_ARB = 0x2003,
  30. WGL_SWAP_METHOD_ARB = 0x2007,
  31. WGL_SUPPORT_OPENGL_ARB = 0x2010,
  32. WGL_PIXEL_TYPE_ARB = 0x2013,
  33. WGL_DOUBLE_BUFFER_ARB = 0x2011,
  34. WGL_COLOR_BITS_ARB = 0x2014,
  35. WGL_RED_BITS_ARB = 0x2015,
  36. WGL_GREEN_BITS_ARB = 0x2017,
  37. WGL_BLUE_BITS_ARB = 0x2019,
  38. WGL_ALPHA_BITS_ARB = 0x201B,
  39. WGL_DEPTH_BITS_ARB = 0x2022,
  40. WGL_STENCIL_BITS_ARB = 0x2023,
  41. WGL_FULL_ACCELERATION_ARB = 0x2027,
  42. WGL_ACCUM_RED_BITS_ARB = 0x201E,
  43. WGL_ACCUM_GREEN_BITS_ARB = 0x201F,
  44. WGL_ACCUM_BLUE_BITS_ARB = 0x2020,
  45. WGL_ACCUM_ALPHA_BITS_ARB = 0x2021,
  46. WGL_STEREO_ARB = 0x2012,
  47. WGL_SAMPLE_BUFFERS_ARB = 0x2041,
  48. WGL_SAMPLES_ARB = 0x2042,
  49. WGL_TYPE_RGBA_ARB = 0x202B
  50. };
  51. static void getWglExtensions (HDC dc, StringArray& result) noexcept
  52. {
  53. PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = 0;
  54. if (WGL_EXT_FUNCTION_INIT (PFNWGLGETEXTENSIONSSTRINGARBPROC, wglGetExtensionsStringARB))
  55. result.addTokens (String (wglGetExtensionsStringARB (dc)), false);
  56. else
  57. jassertfalse; // If this fails, it may be because you didn't activate the openGL context
  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. // Use a default pixel format that should be supported everywhere
  74. PIXELFORMATDESCRIPTOR pfd = { 0 };
  75. pfd.nSize = sizeof (pfd);
  76. pfd.nVersion = 1;
  77. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  78. pfd.iPixelType = PFD_TYPE_RGBA;
  79. pfd.cColorBits = 24;
  80. pfd.cDepthBits = 16;
  81. const int format = ChoosePixelFormat (dc, &pfd);
  82. if (format != 0)
  83. SetPixelFormat (dc, format, &pfd);
  84. renderContext = wglCreateContext (dc);
  85. makeActive();
  86. setPixelFormat (pixelFormat);
  87. if (contextToShareWith != 0 && renderContext != 0)
  88. wglShareLists (contextToShareWith, renderContext);
  89. }
  90. ~WindowedGLContext()
  91. {
  92. deleteContext();
  93. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  94. nativeWindow = nullptr;
  95. }
  96. void deleteContext()
  97. {
  98. makeInactive();
  99. if (renderContext != 0)
  100. {
  101. wglDeleteContext (renderContext);
  102. renderContext = 0;
  103. }
  104. }
  105. bool makeActive() const noexcept
  106. {
  107. jassert (renderContext != 0);
  108. return wglMakeCurrent (dc, renderContext) != 0;
  109. }
  110. bool makeInactive() const noexcept
  111. {
  112. return (! isActive()) || (wglMakeCurrent (0, 0) != 0);
  113. }
  114. bool isActive() const noexcept
  115. {
  116. return wglGetCurrentContext() == renderContext;
  117. }
  118. OpenGLPixelFormat getPixelFormat() const
  119. {
  120. OpenGLPixelFormat pf;
  121. makeActive();
  122. StringArray availableExtensions;
  123. getWglExtensions (dc, availableExtensions);
  124. fillInPixelFormatDetails (GetPixelFormat (dc), pf, availableExtensions);
  125. return pf;
  126. }
  127. void* getRawContext() const noexcept
  128. {
  129. return renderContext;
  130. }
  131. bool setPixelFormat (const OpenGLPixelFormat& pixelFormat)
  132. {
  133. makeActive();
  134. PIXELFORMATDESCRIPTOR pfd = { 0 };
  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. nativeWindow = nullptr;
  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 (const Rectangle<int>& bounds)
  235. {
  236. SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
  237. bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
  238. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  239. }
  240. void repaint()
  241. {
  242. nativeWindow->repaint (nativeWindow->getBounds().withPosition (Point<int>()));
  243. }
  244. void swapBuffers()
  245. {
  246. SwapBuffers (dc);
  247. }
  248. bool setSwapInterval (int numFramesPerSwap)
  249. {
  250. makeActive();
  251. StringArray availableExtensions;
  252. getWglExtensions (dc, availableExtensions);
  253. PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = 0;
  254. return availableExtensions.contains ("WGL_EXT_swap_control")
  255. && WGL_EXT_FUNCTION_INIT (PFNWGLSWAPINTERVALEXTPROC, wglSwapIntervalEXT)
  256. && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  257. }
  258. int getSwapInterval() const
  259. {
  260. makeActive();
  261. StringArray availableExtensions;
  262. getWglExtensions (dc, availableExtensions);
  263. PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT = 0;
  264. if (availableExtensions.contains ("WGL_EXT_swap_control")
  265. && WGL_EXT_FUNCTION_INIT (PFNWGLGETSWAPINTERVALEXTPROC, wglGetSwapIntervalEXT))
  266. return wglGetSwapIntervalEXT();
  267. return 0;
  268. }
  269. void findAlternativeOpenGLPixelFormats (OwnedArray <OpenGLPixelFormat>& results)
  270. {
  271. jassert (isActive());
  272. StringArray availableExtensions;
  273. getWglExtensions (dc, availableExtensions);
  274. PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;
  275. int numTypes = 0;
  276. if (availableExtensions.contains("WGL_ARB_pixel_format")
  277. && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
  278. {
  279. int attributes = WGL_NUMBER_PIXEL_FORMATS_ARB;
  280. if (! wglGetPixelFormatAttribivARB (dc, 1, 0, 1, &attributes, &numTypes))
  281. jassertfalse;
  282. }
  283. else
  284. {
  285. numTypes = DescribePixelFormat (dc, 0, 0, 0);
  286. }
  287. OpenGLPixelFormat pf;
  288. for (int i = 0; i < numTypes; ++i)
  289. {
  290. if (fillInPixelFormatDetails (i + 1, pf, availableExtensions))
  291. {
  292. bool alreadyListed = false;
  293. for (int j = results.size(); --j >= 0;)
  294. if (pf == *results.getUnchecked(j))
  295. alreadyListed = true;
  296. if (! alreadyListed)
  297. results.add (new OpenGLPixelFormat (pf));
  298. }
  299. }
  300. }
  301. void* getNativeWindowHandle() const
  302. {
  303. return nativeWindow != nullptr ? nativeWindow->getNativeHandle() : nullptr;
  304. }
  305. //==============================================================================
  306. HGLRC renderContext;
  307. private:
  308. ScopedPointer<ComponentPeer> nativeWindow;
  309. Component* const component;
  310. HDC dc;
  311. //==============================================================================
  312. void createNativeWindow()
  313. {
  314. nativeWindow = createNonRepaintingEmbeddedWindowsPeer (component, component->getTopLevelComponent()->getWindowHandle());
  315. nativeWindow->setVisible (true);
  316. dc = GetDC ((HWND) nativeWindow->getNativeHandle());
  317. }
  318. bool fillInPixelFormatDetails (const int pixelFormatIndex,
  319. OpenGLPixelFormat& result,
  320. const StringArray& availableExtensions) const noexcept
  321. {
  322. PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB = 0;
  323. if (availableExtensions.contains ("WGL_ARB_pixel_format")
  324. && WGL_EXT_FUNCTION_INIT (PFNWGLGETPIXELFORMATATTRIBIVARBPROC, wglGetPixelFormatAttribivARB))
  325. {
  326. int attributes[32];
  327. UINT numAttributes = 0;
  328. attributes[numAttributes++] = WGL_DRAW_TO_WINDOW_ARB;
  329. attributes[numAttributes++] = WGL_SUPPORT_OPENGL_ARB;
  330. attributes[numAttributes++] = WGL_ACCELERATION_ARB;
  331. attributes[numAttributes++] = WGL_DOUBLE_BUFFER_ARB;
  332. attributes[numAttributes++] = WGL_PIXEL_TYPE_ARB;
  333. attributes[numAttributes++] = WGL_RED_BITS_ARB;
  334. attributes[numAttributes++] = WGL_GREEN_BITS_ARB;
  335. attributes[numAttributes++] = WGL_BLUE_BITS_ARB;
  336. attributes[numAttributes++] = WGL_ALPHA_BITS_ARB;
  337. attributes[numAttributes++] = WGL_DEPTH_BITS_ARB;
  338. attributes[numAttributes++] = WGL_STENCIL_BITS_ARB;
  339. attributes[numAttributes++] = WGL_ACCUM_RED_BITS_ARB;
  340. attributes[numAttributes++] = WGL_ACCUM_GREEN_BITS_ARB;
  341. attributes[numAttributes++] = WGL_ACCUM_BLUE_BITS_ARB;
  342. attributes[numAttributes++] = WGL_ACCUM_ALPHA_BITS_ARB;
  343. if (availableExtensions.contains ("WGL_ARB_multisample"))
  344. attributes[numAttributes++] = WGL_SAMPLES_ARB;
  345. int values[32] = { 0 };
  346. if (wglGetPixelFormatAttribivARB (dc, pixelFormatIndex, 0, numAttributes, attributes, values))
  347. {
  348. int n = 0;
  349. bool isValidFormat = (values[n++] == GL_TRUE); // WGL_DRAW_TO_WINDOW_ARB
  350. isValidFormat = (values[n++] == GL_TRUE) && isValidFormat; // WGL_SUPPORT_OPENGL_ARB
  351. isValidFormat = (values[n++] == WGL_FULL_ACCELERATION_ARB) && isValidFormat; // WGL_ACCELERATION_ARB
  352. isValidFormat = (values[n++] == GL_TRUE) && isValidFormat; // WGL_DOUBLE_BUFFER_ARB:
  353. isValidFormat = (values[n++] == WGL_TYPE_RGBA_ARB) && isValidFormat; // WGL_PIXEL_TYPE_ARB
  354. result.redBits = values[n++]; // WGL_RED_BITS_ARB
  355. result.greenBits = values[n++]; // WGL_GREEN_BITS_ARB
  356. result.blueBits = values[n++]; // WGL_BLUE_BITS_ARB
  357. result.alphaBits = values[n++]; // WGL_ALPHA_BITS_ARB
  358. result.depthBufferBits = values[n++]; // WGL_DEPTH_BITS_ARB
  359. result.stencilBufferBits = values[n++]; // WGL_STENCIL_BITS_ARB
  360. result.accumulationBufferRedBits = values[n++]; // WGL_ACCUM_RED_BITS_ARB
  361. result.accumulationBufferGreenBits = values[n++]; // WGL_ACCUM_GREEN_BITS_ARB
  362. result.accumulationBufferBlueBits = values[n++]; // WGL_ACCUM_BLUE_BITS_ARB
  363. result.accumulationBufferAlphaBits = values[n++]; // WGL_ACCUM_ALPHA_BITS_ARB
  364. result.fullSceneAntiAliasingNumSamples = (uint8) values[n++]; // WGL_SAMPLES_ARB
  365. return isValidFormat;
  366. }
  367. else
  368. {
  369. jassertfalse;
  370. }
  371. }
  372. else
  373. {
  374. PIXELFORMATDESCRIPTOR pfd;
  375. if (DescribePixelFormat (dc, pixelFormatIndex, sizeof (pfd), &pfd))
  376. {
  377. result.redBits = pfd.cRedBits;
  378. result.greenBits = pfd.cGreenBits;
  379. result.blueBits = pfd.cBlueBits;
  380. result.alphaBits = pfd.cAlphaBits;
  381. result.depthBufferBits = pfd.cDepthBits;
  382. result.stencilBufferBits = pfd.cStencilBits;
  383. result.accumulationBufferRedBits = pfd.cAccumRedBits;
  384. result.accumulationBufferGreenBits = pfd.cAccumGreenBits;
  385. result.accumulationBufferBlueBits = pfd.cAccumBlueBits;
  386. result.accumulationBufferAlphaBits = pfd.cAccumAlphaBits;
  387. result.fullSceneAntiAliasingNumSamples = 0;
  388. return true;
  389. }
  390. else
  391. {
  392. jassertfalse;
  393. }
  394. }
  395. return false;
  396. }
  397. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  398. };
  399. //==============================================================================
  400. OpenGLContext* OpenGLComponent::createContext()
  401. {
  402. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this,
  403. contextToShareListsWith != nullptr ? (HGLRC) contextToShareListsWith->getRawContext() : 0,
  404. preferredPixelFormat));
  405. return (c->renderContext != 0) ? c.release() : nullptr;
  406. }
  407. void* OpenGLComponent::getNativeWindowHandle() const
  408. {
  409. return context != nullptr ? static_cast<WindowedGLContext*> (static_cast<OpenGLContext*> (context))->getNativeWindowHandle() : nullptr;
  410. }
  411. void OpenGLPixelFormat::getAvailablePixelFormats (Component* component,
  412. OwnedArray <OpenGLPixelFormat>& results)
  413. {
  414. Component tempComp;
  415. {
  416. WindowedGLContext wc (component, 0, OpenGLPixelFormat (8, 8, 16, 0));
  417. wc.makeActive();
  418. wc.findAlternativeOpenGLPixelFormats (results);
  419. }
  420. }