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.

350 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. class OpenGLFrameBuffer::Pimpl
  20. {
  21. public:
  22. Pimpl (OpenGLContext& c, const int w, const int h,
  23. const bool wantsDepthBuffer, const bool wantsStencilBuffer)
  24. : context (c), width (w), height (h),
  25. textureID (0), frameBufferID (0), depthOrStencilBuffer (0),
  26. hasDepthBuffer (false), hasStencilBuffer (false)
  27. {
  28. // Framebuffer objects can only be created when the current thread has an active OpenGL
  29. // context. You'll need to create this object in one of the OpenGLContext's callbacks.
  30. jassert (OpenGLHelpers::isContextActive());
  31. #if JUCE_WINDOWS || JUCE_LINUX
  32. if (context.extensions.glGenFramebuffers == nullptr)
  33. return;
  34. #endif
  35. context.extensions.glGenFramebuffers (1, &frameBufferID);
  36. bind();
  37. glGenTextures (1, &textureID);
  38. glBindTexture (GL_TEXTURE_2D, textureID);
  39. JUCE_CHECK_OPENGL_ERROR
  40. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  41. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  42. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  43. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  44. JUCE_CHECK_OPENGL_ERROR
  45. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  46. JUCE_CHECK_OPENGL_ERROR
  47. context.extensions.glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
  48. if (wantsDepthBuffer || wantsStencilBuffer)
  49. {
  50. context.extensions.glGenRenderbuffers (1, &depthOrStencilBuffer);
  51. context.extensions.glBindRenderbuffer (GL_RENDERBUFFER, depthOrStencilBuffer);
  52. jassert (context.extensions.glIsRenderbuffer (depthOrStencilBuffer));
  53. context.extensions.glRenderbufferStorage (GL_RENDERBUFFER,
  54. (wantsDepthBuffer && wantsStencilBuffer) ? GL_DEPTH24_STENCIL8
  55. #if JUCE_OPENGL_ES
  56. : GL_DEPTH_COMPONENT16,
  57. #else
  58. : GL_DEPTH_COMPONENT,
  59. #endif
  60. width, height);
  61. GLint params = 0;
  62. context.extensions.glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_DEPTH_SIZE, &params);
  63. context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
  64. if (wantsStencilBuffer)
  65. context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
  66. hasDepthBuffer = wantsDepthBuffer;
  67. hasStencilBuffer = wantsStencilBuffer;
  68. }
  69. unbind();
  70. }
  71. ~Pimpl()
  72. {
  73. if (OpenGLHelpers::isContextActive())
  74. {
  75. if (textureID != 0)
  76. glDeleteTextures (1, &textureID);
  77. if (depthOrStencilBuffer != 0)
  78. context.extensions.glDeleteRenderbuffers (1, &depthOrStencilBuffer);
  79. if (frameBufferID != 0)
  80. context.extensions.glDeleteFramebuffers (1, &frameBufferID);
  81. JUCE_CHECK_OPENGL_ERROR
  82. }
  83. }
  84. bool createdOk() const
  85. {
  86. return frameBufferID != 0 && textureID != 0;
  87. }
  88. void bind()
  89. {
  90. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, frameBufferID);
  91. JUCE_CHECK_OPENGL_ERROR
  92. }
  93. void unbind()
  94. {
  95. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, context.getFrameBufferID());
  96. JUCE_CHECK_OPENGL_ERROR
  97. }
  98. OpenGLContext& context;
  99. const int width, height;
  100. GLuint textureID, frameBufferID, depthOrStencilBuffer;
  101. bool hasDepthBuffer, hasStencilBuffer;
  102. private:
  103. bool checkStatus() noexcept
  104. {
  105. const GLenum status = context.extensions.glCheckFramebufferStatus (GL_FRAMEBUFFER);
  106. return status == GL_NO_ERROR
  107. || status == GL_FRAMEBUFFER_COMPLETE;
  108. }
  109. JUCE_DECLARE_NON_COPYABLE (Pimpl)
  110. };
  111. //==============================================================================
  112. class OpenGLFrameBuffer::SavedState
  113. {
  114. public:
  115. SavedState (OpenGLFrameBuffer& buffer, const int w, const int h)
  116. : width (w), height (h),
  117. data ((size_t) (w * h))
  118. {
  119. buffer.readPixels (data, Rectangle<int> (w, h));
  120. }
  121. bool restore (OpenGLContext& context, OpenGLFrameBuffer& buffer)
  122. {
  123. if (buffer.initialise (context, width, height))
  124. {
  125. buffer.writePixels (data, Rectangle<int> (width, height));
  126. return true;
  127. }
  128. return false;
  129. }
  130. private:
  131. const int width, height;
  132. HeapBlock<PixelARGB> data;
  133. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState)
  134. };
  135. //==============================================================================
  136. OpenGLFrameBuffer::OpenGLFrameBuffer() {}
  137. OpenGLFrameBuffer::~OpenGLFrameBuffer() {}
  138. bool OpenGLFrameBuffer::initialise (OpenGLContext& context, int width, int height)
  139. {
  140. jassert (context.isActive()); // The context must be active when creating a framebuffer!
  141. pimpl = nullptr;
  142. pimpl = new Pimpl (context, width, height, false, false);
  143. if (! pimpl->createdOk())
  144. pimpl = nullptr;
  145. return pimpl != nullptr;
  146. }
  147. bool OpenGLFrameBuffer::initialise (OpenGLContext& context, const Image& image)
  148. {
  149. if (! image.isARGB())
  150. return initialise (context, image.convertedToFormat (Image::ARGB));
  151. Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
  152. return initialise (context, bitmap.width, bitmap.height)
  153. && writePixels ((const PixelARGB*) bitmap.data, image.getBounds());
  154. }
  155. bool OpenGLFrameBuffer::initialise (OpenGLFrameBuffer& other)
  156. {
  157. const Pimpl* const p = other.pimpl;
  158. if (p == nullptr)
  159. {
  160. pimpl = nullptr;
  161. return true;
  162. }
  163. const Rectangle<int> area (pimpl->width, pimpl->height);
  164. if (initialise (p->context, area.getWidth(), area.getHeight()))
  165. {
  166. pimpl->bind();
  167. #if ! JUCE_ANDROID
  168. glEnable (GL_TEXTURE_2D);
  169. clearGLError();
  170. #endif
  171. glBindTexture (GL_TEXTURE_2D, p->textureID);
  172. pimpl->context.copyTexture (area, area, area.getWidth(), area.getHeight(), false);
  173. glBindTexture (GL_TEXTURE_2D, 0);
  174. JUCE_CHECK_OPENGL_ERROR
  175. pimpl->unbind();
  176. return true;
  177. }
  178. return false;
  179. }
  180. void OpenGLFrameBuffer::release()
  181. {
  182. pimpl = nullptr;
  183. savedState = nullptr;
  184. }
  185. void OpenGLFrameBuffer::saveAndRelease()
  186. {
  187. if (pimpl != nullptr)
  188. {
  189. savedState = new SavedState (*this, pimpl->width, pimpl->height);
  190. pimpl = nullptr;
  191. }
  192. }
  193. bool OpenGLFrameBuffer::reloadSavedCopy (OpenGLContext& context)
  194. {
  195. if (savedState != nullptr)
  196. {
  197. ScopedPointer<SavedState> state (savedState);
  198. if (state->restore (context, *this))
  199. return true;
  200. savedState = state;
  201. }
  202. return false;
  203. }
  204. int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
  205. int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
  206. GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
  207. bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
  208. {
  209. // trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
  210. // reloadSavedCopy() to put it back into GPU memory before using it..
  211. jassert (savedState == nullptr);
  212. if (pimpl == nullptr)
  213. return false;
  214. pimpl->bind();
  215. return true;
  216. }
  217. GLuint OpenGLFrameBuffer::getFrameBufferID() const noexcept
  218. {
  219. return pimpl != nullptr ? pimpl->frameBufferID : 0;
  220. }
  221. GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget() noexcept
  222. {
  223. GLint fb;
  224. glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fb);
  225. return (GLuint) fb;
  226. }
  227. void OpenGLFrameBuffer::releaseAsRenderingTarget()
  228. {
  229. if (pimpl != nullptr)
  230. pimpl->unbind();
  231. }
  232. void OpenGLFrameBuffer::clear (Colour colour)
  233. {
  234. if (makeCurrentRenderingTarget())
  235. {
  236. OpenGLHelpers::clear (colour);
  237. releaseAsRenderingTarget();
  238. }
  239. }
  240. void OpenGLFrameBuffer::makeCurrentAndClear()
  241. {
  242. if (makeCurrentRenderingTarget())
  243. {
  244. glClearColor (0, 0, 0, 0);
  245. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  246. }
  247. }
  248. bool OpenGLFrameBuffer::readPixels (PixelARGB* target, const Rectangle<int>& area)
  249. {
  250. if (! makeCurrentRenderingTarget())
  251. return false;
  252. glPixelStorei (GL_PACK_ALIGNMENT, 4);
  253. glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  254. JUCE_RGBA_FORMAT, GL_UNSIGNED_BYTE, target);
  255. pimpl->unbind();
  256. return true;
  257. }
  258. bool OpenGLFrameBuffer::writePixels (const PixelARGB* data, const Rectangle<int>& area)
  259. {
  260. OpenGLTargetSaver ts (pimpl->context);
  261. if (! makeCurrentRenderingTarget())
  262. return false;
  263. glDisable (GL_DEPTH_TEST);
  264. glDisable (GL_BLEND);
  265. JUCE_CHECK_OPENGL_ERROR
  266. OpenGLTexture tex;
  267. tex.loadARGB (data, area.getWidth(), area.getHeight());
  268. glViewport (0, 0, pimpl->width, pimpl->height);
  269. pimpl->context.copyTexture (area, Rectangle<int> (area.getX(), area.getY(),
  270. tex.getWidth(), tex.getHeight()),
  271. pimpl->width, pimpl->height, true);
  272. JUCE_CHECK_OPENGL_ERROR
  273. return true;
  274. }