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.

524 lines
19KB

  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. BEGIN_JUCE_NAMESPACE
  19. #if JUCE_WINDOWS
  20. enum
  21. {
  22. GL_FRAMEBUFFER_EXT = 0x8D40,
  23. GL_RENDERBUFFER_EXT = 0x8D41,
  24. GL_COLOR_ATTACHMENT0_EXT = 0x8CE0,
  25. GL_DEPTH_ATTACHMENT_EXT = 0x8D00,
  26. GL_STENCIL_ATTACHMENT_EXT = 0x8D20,
  27. GL_FRAMEBUFFER_COMPLETE_EXT = 0x8CD5,
  28. GL_DEPTH24_STENCIL8_EXT = 0x88F0,
  29. GL_RENDERBUFFER_DEPTH_SIZE_EXT = 0x8D54
  30. };
  31. #endif
  32. #if JUCE_WINDOWS || JUCE_LINUX
  33. #define FRAMEBUFFER_FUNCTION_LIST(USE_FUNCTION) \
  34. USE_FUNCTION (glIsRenderbufferEXT, GLboolean, (GLuint renderbuffer))\
  35. USE_FUNCTION (glBindRenderbufferEXT, void, (GLenum target, GLuint renderbuffer))\
  36. USE_FUNCTION (glDeleteRenderbuffersEXT, void, (GLsizei n, const GLuint *renderbuffers))\
  37. USE_FUNCTION (glGenRenderbuffersEXT, void, (GLsizei n, GLuint *renderbuffers))\
  38. USE_FUNCTION (glRenderbufferStorageEXT, void, (GLenum target, GLenum internalformat, GLsizei width, GLsizei height))\
  39. USE_FUNCTION (glGetRenderbufferParameterivEXT, void, (GLenum target, GLenum pname, GLint* params))\
  40. USE_FUNCTION (glIsFramebufferEXT, GLboolean, (GLuint framebuffer))\
  41. USE_FUNCTION (glBindFramebufferEXT, void, (GLenum target, GLuint framebuffer))\
  42. USE_FUNCTION (glDeleteFramebuffersEXT, void, (GLsizei n, const GLuint *framebuffers))\
  43. USE_FUNCTION (glGenFramebuffersEXT, void, (GLsizei n, GLuint *framebuffers))\
  44. USE_FUNCTION (glCheckFramebufferStatusEXT, GLenum, (GLenum target))\
  45. USE_FUNCTION (glFramebufferTexture1DEXT, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level))\
  46. USE_FUNCTION (glFramebufferTexture2DEXT, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level))\
  47. USE_FUNCTION (glFramebufferTexture3DEXT, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset))\
  48. USE_FUNCTION (glFramebufferRenderbufferEXT, void, (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer))\
  49. USE_FUNCTION (glGetFramebufferAttachmentParameterivEXT, void, (GLenum target, GLenum attachment, GLenum pname, GLint *params))\
  50. USE_FUNCTION (glGenerateMipmapEXT, void, (GLenum target))\
  51. #if JUCE_WINDOWS
  52. #define APICALLTYPE __stdcall
  53. #else
  54. #define APICALLTYPE
  55. #endif
  56. #define DECLARE_FUNCTION(name, returnType, params) \
  57. typedef returnType (APICALLTYPE * type_ ## name) params; static type_ ## name name;
  58. FRAMEBUFFER_FUNCTION_LIST (DECLARE_FUNCTION)
  59. #undef DECLARE_FUNCTION
  60. static bool framebufferFunctionsInitialised = false;
  61. static void initialiseFrameBufferFunctions()
  62. {
  63. if (! framebufferFunctionsInitialised)
  64. {
  65. framebufferFunctionsInitialised = true;
  66. #if JUCE_LINUX
  67. #define JUCE_LOOKUP_FUNCTION(name) glXGetProcAddress ((const GLubyte*) name)
  68. #else
  69. #define JUCE_LOOKUP_FUNCTION(name) wglGetProcAddress (name)
  70. #endif
  71. #define FIND_FUNCTION(name, returnType, params) name = (type_ ## name) JUCE_LOOKUP_FUNCTION (#name);
  72. FRAMEBUFFER_FUNCTION_LIST (FIND_FUNCTION)
  73. #undef FIND_FUNCTION
  74. #undef JUCE_LOOKUP_FUNCTION
  75. }
  76. }
  77. #undef FRAMEBUFFER_FUNCTION_LIST
  78. //==============================================================================
  79. #elif JUCE_OPENGL_ES
  80. #define glIsRenderbufferEXT glIsRenderbufferOES
  81. #define glBindRenderbufferEXT glBindRenderbufferOES
  82. #define glDeleteRenderbuffersEXT glDeleteRenderbuffersOES
  83. #define glGenRenderbuffersEXT glGenRenderbuffersOES
  84. #define glRenderbufferStorageEXT glRenderbufferStorageOES
  85. #define glGetRenderbufferParameterivEXT glGetRenderbufferParameterivOES
  86. #define glIsFramebufferEXT glIsFramebufferOES
  87. #define glBindFramebufferEXT glBindFramebufferOES
  88. #define glDeleteFramebuffersEXT glDeleteFramebuffersOES
  89. #define glGenFramebuffersEXT glGenFramebuffersOES
  90. #define glCheckFramebufferStatusEXT glCheckFramebufferStatusOES
  91. #define glFramebufferTexture1DEXT glFramebufferTexture1DOES
  92. #define glFramebufferTexture2DEXT glFramebufferTexture2DOES
  93. #define glFramebufferTexture3DEXT glFramebufferTexture3DOES
  94. #define glFramebufferRenderbufferEXT glFramebufferRenderbufferOES
  95. #define glGetFramebufferAttachmentParameterivEXT glGetFramebufferAttachmentParameterivOES
  96. #define glGenerateMipmapEXT glGenerateMipmapOES
  97. #define GL_FRAMEBUFFER_EXT GL_FRAMEBUFFER_OES
  98. #define GL_RGBA8 GL_RGBA
  99. #define GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT0_OES
  100. #define GL_RENDERBUFFER_EXT GL_RENDERBUFFER_OES
  101. #define GL_DEPTH24_STENCIL8_EXT GL_DEPTH24_STENCIL8_OES
  102. #define GL_RENDERBUFFER_DEPTH_SIZE_EXT GL_RENDERBUFFER_DEPTH_SIZE_OES
  103. #define GL_DEPTH_ATTACHMENT_EXT GL_DEPTH_ATTACHMENT_OES
  104. #define GL_STENCIL_ATTACHMENT_EXT GL_STENCIL_ATTACHMENT_OES
  105. #define GL_FRAMEBUFFER_COMPLETE_EXT GL_FRAMEBUFFER_COMPLETE_OES
  106. #define GL_FRAMEBUFFER_UNSUPPORTED_EXT GL_FRAMEBUFFER_UNSUPPORTED_OES
  107. #define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_OES
  108. #endif
  109. //==============================================================================
  110. class OpenGLFrameBuffer::Pimpl
  111. {
  112. public:
  113. Pimpl (const int width_, const int height_,
  114. const bool wantsDepthBuffer, const bool wantsStencilBuffer,
  115. const GLenum textureType = GL_TEXTURE_2D)
  116. : width (width_),
  117. height (height_),
  118. textureID (0),
  119. frameBufferHandle (0),
  120. depthOrStencilBuffer (0),
  121. hasDepthBuffer (false),
  122. hasStencilBuffer (false),
  123. ok (false)
  124. {
  125. #if JUCE_WINDOWS || JUCE_LINUX
  126. initialiseFrameBufferFunctions();
  127. if (glGenFramebuffersEXT == nullptr)
  128. return;
  129. #endif
  130. OpenGLHelpers::resetErrorState();
  131. glGenFramebuffersEXT (1, &frameBufferHandle);
  132. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, frameBufferHandle);
  133. glGenTextures (1, &textureID);
  134. glBindTexture (textureType, textureID);
  135. glTexImage2D (textureType, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  136. glTexParameterf (textureType, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  137. glTexParameterf (textureType, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  138. glTexParameterf (textureType, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  139. glTexParameterf (textureType, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  140. glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, textureType, textureID, 0);
  141. if (wantsDepthBuffer || wantsStencilBuffer)
  142. {
  143. glGenRenderbuffersEXT (1, &depthOrStencilBuffer);
  144. glBindRenderbufferEXT (GL_RENDERBUFFER_EXT, depthOrStencilBuffer);
  145. jassert (glIsRenderbufferEXT (depthOrStencilBuffer));
  146. if (wantsDepthBuffer && wantsStencilBuffer)
  147. {
  148. glRenderbufferStorageEXT (GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT, width, height);
  149. GLint params = 0;
  150. glGetRenderbufferParameterivEXT (GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_DEPTH_SIZE_EXT, &params);
  151. glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthOrStencilBuffer);
  152. glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthOrStencilBuffer);
  153. hasDepthBuffer = true;
  154. hasStencilBuffer = true;
  155. }
  156. else
  157. {
  158. #if JUCE_OPENGL_ES
  159. glRenderbufferStorageEXT (GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT16, width, height);
  160. #else
  161. glRenderbufferStorageEXT (GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height);
  162. #endif
  163. GLint params = 0;
  164. glGetRenderbufferParameterivEXT (GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_DEPTH_SIZE_EXT, &params);
  165. glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthOrStencilBuffer);
  166. hasDepthBuffer = true;
  167. }
  168. }
  169. ok = checkStatus();
  170. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
  171. }
  172. ~Pimpl()
  173. {
  174. if (textureID != 0)
  175. glDeleteTextures (1, &textureID);
  176. if (depthOrStencilBuffer != 0)
  177. glDeleteRenderbuffersEXT (1, &depthOrStencilBuffer);
  178. if (frameBufferHandle != 0)
  179. glDeleteFramebuffersEXT (1, &frameBufferHandle);
  180. }
  181. bool bind() { return bind (frameBufferHandle); }
  182. bool unbind() { return bind (0); }
  183. const int width, height;
  184. GLuint textureID, frameBufferHandle, depthOrStencilBuffer;
  185. bool hasDepthBuffer, hasStencilBuffer, ok;
  186. private:
  187. bool bind (GLuint buffer)
  188. {
  189. if (ok)
  190. {
  191. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, buffer);
  192. ok = checkStatus();
  193. }
  194. return ok;
  195. }
  196. static bool checkStatus() noexcept
  197. {
  198. const GLenum status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
  199. return status == GL_NO_ERROR
  200. || status == GL_FRAMEBUFFER_COMPLETE_EXT;
  201. }
  202. JUCE_DECLARE_NON_COPYABLE (Pimpl);
  203. };
  204. //==============================================================================
  205. class OpenGLFrameBuffer::SavedState
  206. {
  207. public:
  208. SavedState (OpenGLFrameBuffer& buffer, const int w, const int h)
  209. : width (w), height (h),
  210. data (w * h)
  211. {
  212. buffer.readPixels (data, Rectangle<int> (0, 0, w, h));
  213. }
  214. bool restore (OpenGLFrameBuffer& buffer)
  215. {
  216. if (buffer.initialise (width, height))
  217. {
  218. buffer.writePixels (data, 4, Rectangle<int> (0, 0, width, height));
  219. return true;
  220. }
  221. return false;
  222. }
  223. private:
  224. const int width, height;
  225. HeapBlock <int32> data;
  226. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState);
  227. };
  228. //==============================================================================
  229. OpenGLFrameBuffer::OpenGLFrameBuffer() {}
  230. OpenGLFrameBuffer::~OpenGLFrameBuffer() {}
  231. bool OpenGLFrameBuffer::initialise (int width, int height)
  232. {
  233. pimpl = nullptr;
  234. pimpl = new Pimpl (width, height, false, false);
  235. if (! pimpl->ok)
  236. pimpl = nullptr;
  237. return pimpl != nullptr;
  238. }
  239. bool OpenGLFrameBuffer::initialise (const Image& image)
  240. {
  241. if (initialise (image.getWidth(), image.getHeight()))
  242. {
  243. {
  244. Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
  245. if (bitmap.lineStride == image.getWidth() * bitmap.pixelStride)
  246. return writePixels (bitmap.data, bitmap.pixelStride, image.getBounds());
  247. }
  248. return initialise (Image (image.getSharedImage()->clone()));
  249. }
  250. return false;
  251. }
  252. void OpenGLFrameBuffer::release()
  253. {
  254. pimpl = nullptr;
  255. savedState = nullptr;
  256. }
  257. void OpenGLFrameBuffer::saveAndRelease()
  258. {
  259. if (pimpl != nullptr)
  260. {
  261. savedState = new SavedState (*this, pimpl->width, pimpl->height);
  262. pimpl = nullptr;
  263. }
  264. }
  265. bool OpenGLFrameBuffer::reloadSavedCopy()
  266. {
  267. if (savedState != nullptr)
  268. {
  269. ScopedPointer<SavedState> state (savedState);
  270. if (state->restore (*this))
  271. return true;
  272. savedState = state;
  273. }
  274. return false;
  275. }
  276. int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
  277. int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
  278. GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
  279. bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
  280. {
  281. // trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
  282. // reloadSavedCopy() to put it back into GPU memory before using it..
  283. jassert (savedState == nullptr);
  284. return pimpl != nullptr && pimpl->bind();
  285. }
  286. void OpenGLFrameBuffer::releaseAsRenderingTarget()
  287. {
  288. if (pimpl != nullptr)
  289. pimpl->unbind();
  290. }
  291. void OpenGLFrameBuffer::clear (const Colour& colour)
  292. {
  293. if (makeCurrentRenderingTarget())
  294. {
  295. OpenGLHelpers::clear (colour);
  296. releaseAsRenderingTarget();
  297. }
  298. }
  299. void OpenGLFrameBuffer::makeCurrentAndClear()
  300. {
  301. if (makeCurrentRenderingTarget())
  302. {
  303. glClearColor (0, 0, 0, 0);
  304. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  305. }
  306. }
  307. bool OpenGLFrameBuffer::readPixels (void* target, const Rectangle<int>& area)
  308. {
  309. if (! makeCurrentRenderingTarget())
  310. return false;
  311. glPixelStorei (GL_PACK_ALIGNMENT, 4);
  312. glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, target);
  313. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
  314. return true;
  315. }
  316. bool OpenGLFrameBuffer::writePixels (const void* data, int pixelStride, const Rectangle<int>& area)
  317. {
  318. if (! makeCurrentRenderingTarget())
  319. return false;
  320. jassert (pixelStride == 3 || pixelStride == 4); // can only handle RGB or ARGB
  321. const int format = pixelStride == 3 ? GL_RGB : GL_BGRA_EXT;
  322. const int invertedY = pimpl->height - area.getBottom();
  323. OpenGLHelpers::prepareFor2D (pimpl->width, pimpl->height);
  324. glDisable (GL_DEPTH_TEST);
  325. glDisable (GL_BLEND);
  326. #if JUCE_OPENGL_ES
  327. // GLES has no glDrawPixels function, so we have to create a texture and draw it..
  328. GLuint temporaryTexture = 0;
  329. glGenTextures (1, &temporaryTexture);
  330. jassert (temporaryTexture != 0); // can't create a texture!
  331. if (temporaryTexture != 0)
  332. {
  333. glEnable (GL_TEXTURE_2D);
  334. glBindTexture (GL_TEXTURE_2D, temporaryTexture);
  335. glPixelStorei (GL_UNPACK_ALIGNMENT, pixelStride);
  336. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, area.getWidth(), area.getHeight(), 0,
  337. format, GL_UNSIGNED_BYTE, data);
  338. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  339. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  340. const int cropRect[4] = { 0, 0, area.getWidth(), area.getHeight() };
  341. glTexParameteriv (GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
  342. glDrawTexiOES (area.getX(), invertedY, 1, area.getWidth(), area.getHeight());
  343. glBindTexture (GL_TEXTURE_2D, 0);
  344. glDeleteTextures (1, &temporaryTexture);
  345. }
  346. #else
  347. glRasterPos2i (area.getX(), invertedY);
  348. glBindTexture (GL_TEXTURE_2D, 0);
  349. glPixelStorei (GL_UNPACK_ALIGNMENT, pixelStride);
  350. glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
  351. glDrawPixels (area.getWidth(), area.getHeight(), format, GL_UNSIGNED_BYTE, data);
  352. #endif
  353. glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
  354. return true;
  355. }
  356. void OpenGLFrameBuffer::draw2D (float x1, float y1,
  357. float x2, float y2,
  358. float x3, float y3,
  359. float x4, float y4,
  360. const Colour& colour) const
  361. {
  362. if (pimpl != nullptr)
  363. {
  364. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  365. OpenGLHelpers::drawQuad2D (x1, y1, x2, y2, x3, y3, x4, y4, colour);
  366. glBindTexture (GL_TEXTURE_2D, 0);
  367. }
  368. }
  369. void OpenGLFrameBuffer::draw3D (float x1, float y1, float z1,
  370. float x2, float y2, float z2,
  371. float x3, float y3, float z3,
  372. float x4, float y4, float z4,
  373. const Colour& colour) const
  374. {
  375. if (pimpl != nullptr)
  376. {
  377. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  378. OpenGLHelpers::drawQuad3D (x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, colour);
  379. glBindTexture (GL_TEXTURE_2D, 0);
  380. }
  381. }
  382. void OpenGLFrameBuffer::drawAt (float x1, float y1) const
  383. {
  384. if (pimpl != nullptr)
  385. {
  386. glEnable (GL_TEXTURE_2D);
  387. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  388. glDisableClientState (GL_COLOR_ARRAY);
  389. glDisableClientState (GL_NORMAL_ARRAY);
  390. const GLfloat vertices[] = { x1, y1,
  391. x1 + pimpl->width, y1,
  392. x1, y1 + pimpl->height,
  393. x1 + pimpl->width, y1 + pimpl->height };
  394. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  395. glEnableClientState (GL_VERTEX_ARRAY);
  396. glVertexPointer (2, GL_FLOAT, 0, vertices);
  397. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  398. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  399. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  400. glBindTexture (GL_TEXTURE_2D, 0);
  401. }
  402. }
  403. //==============================================================================
  404. void OpenGLFrameBuffer::createAlphaChannelFromPath (const Path& path, const AffineTransform& transform,
  405. const int oversamplingLevel)
  406. {
  407. makeCurrentRenderingTarget();
  408. glEnableClientState (GL_VERTEX_ARRAY);
  409. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  410. glDisableClientState (GL_COLOR_ARRAY);
  411. glDisableClientState (GL_NORMAL_ARRAY);
  412. glDisable (GL_TEXTURE_2D);
  413. glDisable (GL_DEPTH_TEST);
  414. glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  415. glEnable (GL_BLEND);
  416. glBlendFunc (GL_ONE, GL_ONE);
  417. prepareFor2D();
  418. TriangulatedPath (path, transform).draw (oversamplingLevel);
  419. }
  420. END_JUCE_NAMESPACE