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.

348 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class OpenGLFrameBuffer::Pimpl
  18. {
  19. public:
  20. Pimpl (OpenGLContext& c, const int w, const int h,
  21. const bool wantsDepthBuffer, const bool wantsStencilBuffer)
  22. : context (c), width (w), height (h),
  23. textureID (0), frameBufferID (0), depthOrStencilBuffer (0),
  24. hasDepthBuffer (false), hasStencilBuffer (false)
  25. {
  26. // Framebuffer objects can only be created when the current thread has an active OpenGL
  27. // context. You'll need to create this object in one of the OpenGLContext's callbacks.
  28. jassert (OpenGLHelpers::isContextActive());
  29. #if JUCE_WINDOWS || JUCE_LINUX
  30. if (context.extensions.glGenFramebuffers == nullptr)
  31. return;
  32. #endif
  33. context.extensions.glGenFramebuffers (1, &frameBufferID);
  34. bind();
  35. glGenTextures (1, &textureID);
  36. glBindTexture (GL_TEXTURE_2D, textureID);
  37. JUCE_CHECK_OPENGL_ERROR
  38. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  39. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  40. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  41. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  42. JUCE_CHECK_OPENGL_ERROR
  43. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  44. JUCE_CHECK_OPENGL_ERROR
  45. context.extensions.glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
  46. if (wantsDepthBuffer || wantsStencilBuffer)
  47. {
  48. context.extensions.glGenRenderbuffers (1, &depthOrStencilBuffer);
  49. context.extensions.glBindRenderbuffer (GL_RENDERBUFFER, depthOrStencilBuffer);
  50. jassert (context.extensions.glIsRenderbuffer (depthOrStencilBuffer));
  51. context.extensions.glRenderbufferStorage (GL_RENDERBUFFER,
  52. (wantsDepthBuffer && wantsStencilBuffer) ? GL_DEPTH24_STENCIL8
  53. #if JUCE_OPENGL_ES
  54. : GL_DEPTH_COMPONENT16,
  55. #else
  56. : GL_DEPTH_COMPONENT,
  57. #endif
  58. width, height);
  59. GLint params = 0;
  60. context.extensions.glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_DEPTH_SIZE, &params);
  61. context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
  62. if (wantsStencilBuffer)
  63. context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
  64. hasDepthBuffer = wantsDepthBuffer;
  65. hasStencilBuffer = wantsStencilBuffer;
  66. }
  67. unbind();
  68. }
  69. ~Pimpl()
  70. {
  71. if (OpenGLHelpers::isContextActive())
  72. {
  73. if (textureID != 0)
  74. glDeleteTextures (1, &textureID);
  75. if (depthOrStencilBuffer != 0)
  76. context.extensions.glDeleteRenderbuffers (1, &depthOrStencilBuffer);
  77. if (frameBufferID != 0)
  78. context.extensions.glDeleteFramebuffers (1, &frameBufferID);
  79. JUCE_CHECK_OPENGL_ERROR
  80. }
  81. }
  82. bool createdOk() const
  83. {
  84. return frameBufferID != 0 && textureID != 0;
  85. }
  86. void bind()
  87. {
  88. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, frameBufferID);
  89. JUCE_CHECK_OPENGL_ERROR
  90. }
  91. void unbind()
  92. {
  93. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, context.getFrameBufferID());
  94. JUCE_CHECK_OPENGL_ERROR
  95. }
  96. OpenGLContext& context;
  97. const int width, height;
  98. GLuint textureID, frameBufferID, depthOrStencilBuffer;
  99. bool hasDepthBuffer, hasStencilBuffer;
  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 ((size_t) (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->createdOk())
  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. clearGLError();
  168. #endif
  169. glBindTexture (GL_TEXTURE_2D, p->textureID);
  170. pimpl->context.copyTexture (area, area, area.getWidth(), area.getHeight(), false);
  171. glBindTexture (GL_TEXTURE_2D, 0);
  172. JUCE_CHECK_OPENGL_ERROR
  173. pimpl->unbind();
  174. return true;
  175. }
  176. return false;
  177. }
  178. void OpenGLFrameBuffer::release()
  179. {
  180. pimpl = nullptr;
  181. savedState = nullptr;
  182. }
  183. void OpenGLFrameBuffer::saveAndRelease()
  184. {
  185. if (pimpl != nullptr)
  186. {
  187. savedState = new SavedState (*this, pimpl->width, pimpl->height);
  188. pimpl = nullptr;
  189. }
  190. }
  191. bool OpenGLFrameBuffer::reloadSavedCopy (OpenGLContext& context)
  192. {
  193. if (savedState != nullptr)
  194. {
  195. ScopedPointer<SavedState> state (savedState);
  196. if (state->restore (context, *this))
  197. return true;
  198. savedState = state;
  199. }
  200. return false;
  201. }
  202. int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
  203. int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
  204. GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
  205. bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
  206. {
  207. // trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
  208. // reloadSavedCopy() to put it back into GPU memory before using it..
  209. jassert (savedState == nullptr);
  210. if (pimpl == nullptr)
  211. return false;
  212. pimpl->bind();
  213. return true;
  214. }
  215. GLuint OpenGLFrameBuffer::getFrameBufferID() const noexcept
  216. {
  217. return pimpl != nullptr ? pimpl->frameBufferID : 0;
  218. }
  219. GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget() noexcept
  220. {
  221. GLint fb;
  222. glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fb);
  223. return (GLuint) fb;
  224. }
  225. void OpenGLFrameBuffer::releaseAsRenderingTarget()
  226. {
  227. if (pimpl != nullptr)
  228. pimpl->unbind();
  229. }
  230. void OpenGLFrameBuffer::clear (Colour colour)
  231. {
  232. if (makeCurrentRenderingTarget())
  233. {
  234. OpenGLHelpers::clear (colour);
  235. releaseAsRenderingTarget();
  236. }
  237. }
  238. void OpenGLFrameBuffer::makeCurrentAndClear()
  239. {
  240. if (makeCurrentRenderingTarget())
  241. {
  242. glClearColor (0, 0, 0, 0);
  243. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  244. }
  245. }
  246. bool OpenGLFrameBuffer::readPixels (PixelARGB* target, const Rectangle<int>& area)
  247. {
  248. if (! makeCurrentRenderingTarget())
  249. return false;
  250. glPixelStorei (GL_PACK_ALIGNMENT, 4);
  251. glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  252. JUCE_RGBA_FORMAT, GL_UNSIGNED_BYTE, target);
  253. pimpl->unbind();
  254. return true;
  255. }
  256. bool OpenGLFrameBuffer::writePixels (const PixelARGB* data, const Rectangle<int>& area)
  257. {
  258. OpenGLTargetSaver ts (pimpl->context);
  259. if (! makeCurrentRenderingTarget())
  260. return false;
  261. glDisable (GL_DEPTH_TEST);
  262. glDisable (GL_BLEND);
  263. JUCE_CHECK_OPENGL_ERROR
  264. OpenGLTexture tex;
  265. tex.loadARGB (data, area.getWidth(), area.getHeight());
  266. glViewport (0, 0, pimpl->width, pimpl->height);
  267. pimpl->context.copyTexture (area, Rectangle<int> (area.getX(), area.getY(),
  268. tex.getWidth(), tex.getHeight()),
  269. pimpl->width, pimpl->height, true);
  270. JUCE_CHECK_OPENGL_ERROR
  271. return true;
  272. }