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.

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