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.

397 lines
13KB

  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. class OpenGLFrameBuffer::Pimpl
  19. {
  20. public:
  21. Pimpl (const int width_, const int height_,
  22. const bool wantsDepthBuffer, const bool wantsStencilBuffer)
  23. : width (width_),
  24. height (height_),
  25. textureID (0),
  26. frameBufferHandle (0),
  27. depthOrStencilBuffer (0),
  28. hasDepthBuffer (false),
  29. hasStencilBuffer (false),
  30. ok (true)
  31. {
  32. // Framebuffer objects can only be created when the current thread has an active OpenGL
  33. // context. You'll need to make an OpenGLComponent active before calling this.
  34. jassert (OpenGLHelpers::isContextActive());
  35. #if JUCE_WINDOWS || JUCE_LINUX
  36. if (glGenFramebuffers == nullptr)
  37. return;
  38. #endif
  39. glGenFramebuffers (1, &frameBufferHandle);
  40. glBindFramebuffer (GL_FRAMEBUFFER, frameBufferHandle);
  41. glGenTextures (1, &textureID);
  42. glBindTexture (GL_TEXTURE_2D, textureID);
  43. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  44. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  45. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  46. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  47. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  48. glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
  49. if (wantsDepthBuffer || wantsStencilBuffer)
  50. {
  51. glGenRenderbuffers (1, &depthOrStencilBuffer);
  52. glBindRenderbuffer (GL_RENDERBUFFER, depthOrStencilBuffer);
  53. jassert (glIsRenderbuffer (depthOrStencilBuffer));
  54. glRenderbufferStorage (GL_RENDERBUFFER,
  55. (wantsDepthBuffer && wantsStencilBuffer) ? GL_DEPTH24_STENCIL8
  56. #if JUCE_OPENGL_ES
  57. : GL_DEPTH_COMPONENT16,
  58. #else
  59. : GL_DEPTH_COMPONENT,
  60. #endif
  61. width, height);
  62. GLint params = 0;
  63. glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_DEPTH_SIZE, &params);
  64. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
  65. if (wantsStencilBuffer)
  66. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
  67. hasDepthBuffer = wantsDepthBuffer;
  68. hasStencilBuffer = wantsStencilBuffer;
  69. }
  70. glBindFramebuffer (GL_FRAMEBUFFER, 0);
  71. }
  72. ~Pimpl()
  73. {
  74. if (textureID != 0)
  75. glDeleteTextures (1, &textureID);
  76. if (depthOrStencilBuffer != 0)
  77. glDeleteRenderbuffers (1, &depthOrStencilBuffer);
  78. if (frameBufferHandle != 0)
  79. glDeleteFramebuffers (1, &frameBufferHandle);
  80. }
  81. void bind() { glBindFramebuffer (GL_FRAMEBUFFER, frameBufferHandle); }
  82. void unbind() { glBindFramebuffer (GL_FRAMEBUFFER, 0); }
  83. const int width, height;
  84. GLuint textureID, frameBufferHandle, depthOrStencilBuffer;
  85. bool hasDepthBuffer, hasStencilBuffer, ok;
  86. private:
  87. static bool checkStatus() noexcept
  88. {
  89. const GLenum status = glCheckFramebufferStatus (GL_FRAMEBUFFER);
  90. return status == GL_NO_ERROR
  91. || status == GL_FRAMEBUFFER_COMPLETE;
  92. }
  93. JUCE_DECLARE_NON_COPYABLE (Pimpl);
  94. };
  95. //==============================================================================
  96. class OpenGLFrameBuffer::SavedState
  97. {
  98. public:
  99. SavedState (OpenGLFrameBuffer& buffer, const int w, const int h)
  100. : width (w), height (h),
  101. data (w * h)
  102. {
  103. buffer.readPixels (data, Rectangle<int> (w, h));
  104. }
  105. bool restore (OpenGLFrameBuffer& buffer)
  106. {
  107. if (buffer.initialise (width, height))
  108. {
  109. buffer.writePixels (data, Rectangle<int> (width, height));
  110. return true;
  111. }
  112. return false;
  113. }
  114. private:
  115. const int width, height;
  116. HeapBlock <PixelARGB> data;
  117. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState);
  118. };
  119. //==============================================================================
  120. OpenGLFrameBuffer::OpenGLFrameBuffer() {}
  121. OpenGLFrameBuffer::~OpenGLFrameBuffer() {}
  122. bool OpenGLFrameBuffer::initialise (int width, int height)
  123. {
  124. pimpl = nullptr;
  125. pimpl = new Pimpl (width, height, false, false);
  126. if (! pimpl->ok)
  127. pimpl = nullptr;
  128. return pimpl != nullptr;
  129. }
  130. bool OpenGLFrameBuffer::initialise (const Image& image)
  131. {
  132. if (! image.isARGB())
  133. return initialise (image.convertedToFormat (Image::ARGB));
  134. Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
  135. return initialise (bitmap.width, bitmap.height)
  136. && writePixels ((const PixelARGB*) bitmap.data, image.getBounds());
  137. }
  138. bool OpenGLFrameBuffer::initialise (const OpenGLFrameBuffer& other)
  139. {
  140. const Pimpl* const p = other.pimpl;
  141. if (p == nullptr)
  142. {
  143. pimpl = nullptr;
  144. return true;
  145. }
  146. if (initialise (p->width, p->height))
  147. {
  148. pimpl->bind();
  149. OpenGLHelpers::prepareFor2D (p->width, p->height);
  150. glDisable (GL_BLEND);
  151. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  152. other.drawAt (0, 0);
  153. pimpl->unbind();
  154. return true;
  155. }
  156. return false;
  157. }
  158. void OpenGLFrameBuffer::release()
  159. {
  160. pimpl = nullptr;
  161. savedState = nullptr;
  162. }
  163. void OpenGLFrameBuffer::saveAndRelease()
  164. {
  165. if (pimpl != nullptr)
  166. {
  167. savedState = new SavedState (*this, pimpl->width, pimpl->height);
  168. pimpl = nullptr;
  169. }
  170. }
  171. bool OpenGLFrameBuffer::reloadSavedCopy()
  172. {
  173. if (savedState != nullptr)
  174. {
  175. ScopedPointer<SavedState> state (savedState);
  176. if (state->restore (*this))
  177. return true;
  178. savedState = state;
  179. }
  180. return false;
  181. }
  182. int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
  183. int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
  184. GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
  185. bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
  186. {
  187. // trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
  188. // reloadSavedCopy() to put it back into GPU memory before using it..
  189. jassert (savedState == nullptr);
  190. if (pimpl == nullptr)
  191. return false;
  192. pimpl->bind();
  193. return true;
  194. }
  195. void OpenGLFrameBuffer::setCurrentFrameBufferTarget (GLuint frameBufferID)
  196. {
  197. glBindFramebuffer (GL_FRAMEBUFFER, frameBufferID);
  198. }
  199. GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget()
  200. {
  201. GLint fb;
  202. glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fb);
  203. return (GLuint) fb;
  204. }
  205. void OpenGLFrameBuffer::releaseAsRenderingTarget()
  206. {
  207. setCurrentFrameBufferTarget (0);
  208. }
  209. void OpenGLFrameBuffer::clear (const Colour& colour)
  210. {
  211. if (makeCurrentRenderingTarget())
  212. {
  213. OpenGLHelpers::clear (colour);
  214. releaseAsRenderingTarget();
  215. }
  216. }
  217. void OpenGLFrameBuffer::makeCurrentAndClear()
  218. {
  219. if (makeCurrentRenderingTarget())
  220. {
  221. glClearColor (0, 0, 0, 0);
  222. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  223. }
  224. }
  225. bool OpenGLFrameBuffer::readPixels (PixelARGB* target, const Rectangle<int>& area)
  226. {
  227. if (! makeCurrentRenderingTarget())
  228. return false;
  229. glPixelStorei (GL_PACK_ALIGNMENT, 4);
  230. glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(), GL_BGRA_EXT, GL_UNSIGNED_BYTE, target);
  231. glBindFramebuffer (GL_FRAMEBUFFER, 0);
  232. glPixelStorei (GL_PACK_ALIGNMENT, 0);
  233. return true;
  234. }
  235. bool OpenGLFrameBuffer::writePixels (const PixelARGB* data, const Rectangle<int>& area)
  236. {
  237. if (! makeCurrentRenderingTarget())
  238. return false;
  239. OpenGLHelpers::prepareFor2D (pimpl->width, pimpl->height);
  240. glDisable (GL_DEPTH_TEST);
  241. glDisable (GL_BLEND);
  242. OpenGLTexture tex;
  243. tex.loadARGBFlipped (data, area.getWidth(), area.getHeight());
  244. const int texH = tex.getHeight();
  245. #if JUCE_OPENGL_ES
  246. tex.bind();
  247. const GLint cropRect[4] = { 0, texH - area.getHeight(), area.getWidth(), area.getHeight() };
  248. glTexParameteriv (GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
  249. glEnable (GL_TEXTURE_2D);
  250. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  251. glDrawTexiOES (area.getX(), area.getY(), 1, area.getWidth(), area.getHeight());
  252. glBindTexture (GL_TEXTURE_2D, 0);
  253. #else
  254. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  255. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  256. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  257. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  258. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  259. const int x = area.getX();
  260. const int y = area.getY() - (texH - area.getHeight());
  261. const GLfloat x1 = (GLfloat) x;
  262. const GLfloat y1 = (GLfloat) y;
  263. const GLfloat x2 = (GLfloat) (x + tex.getWidth());
  264. const GLfloat y2 = (GLfloat) (y + texH);
  265. const GLfloat vertices[] = { x1, y1, x2, y1, x1, y2, x2, y2 };
  266. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  267. OpenGLHelpers::drawTriangleStrip (vertices, textureCoords, 4, tex.getTextureID());
  268. #endif
  269. glBindFramebuffer (GL_FRAMEBUFFER, 0);
  270. return true;
  271. }
  272. void OpenGLFrameBuffer::draw2D (float x1, float y1,
  273. float x2, float y2,
  274. float x3, float y3,
  275. float x4, float y4,
  276. const Colour& colour) const
  277. {
  278. if (pimpl != nullptr)
  279. {
  280. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  281. OpenGLHelpers::drawQuad2D (x1, y1, x2, y2, x3, y3, x4, y4, colour);
  282. glBindTexture (GL_TEXTURE_2D, 0);
  283. }
  284. }
  285. void OpenGLFrameBuffer::draw3D (float x1, float y1, float z1,
  286. float x2, float y2, float z2,
  287. float x3, float y3, float z3,
  288. float x4, float y4, float z4,
  289. const Colour& colour) const
  290. {
  291. if (pimpl != nullptr)
  292. {
  293. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  294. OpenGLHelpers::drawQuad3D (x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, colour);
  295. glBindTexture (GL_TEXTURE_2D, 0);
  296. }
  297. }
  298. void OpenGLFrameBuffer::drawAt (float x1, float y1) const
  299. {
  300. if (pimpl != nullptr)
  301. {
  302. glEnable (GL_TEXTURE_2D);
  303. glBindTexture (GL_TEXTURE_2D, pimpl->textureID);
  304. glDisableClientState (GL_COLOR_ARRAY);
  305. glDisableClientState (GL_NORMAL_ARRAY);
  306. const GLfloat vertices[] = { x1, y1,
  307. x1 + pimpl->width, y1,
  308. x1, y1 + pimpl->height,
  309. x1 + pimpl->width, y1 + pimpl->height };
  310. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  311. glEnableClientState (GL_VERTEX_ARRAY);
  312. glVertexPointer (2, GL_FLOAT, 0, vertices);
  313. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  314. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  315. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  316. glBindTexture (GL_TEXTURE_2D, 0);
  317. }
  318. }