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.

424 lines
14KB

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