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.

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