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.

356 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.reset (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. auto* p = other.pimpl.get();
  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.reset (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. std::unique_ptr<SavedState> state;
  200. std::swap (state, savedState);
  201. if (state->restore (context, *this))
  202. return true;
  203. std::swap (state, savedState);
  204. }
  205. return false;
  206. }
  207. int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
  208. int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
  209. GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
  210. bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
  211. {
  212. // trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
  213. // reloadSavedCopy() to put it back into GPU memory before using it..
  214. jassert (savedState == nullptr);
  215. if (pimpl == nullptr)
  216. return false;
  217. pimpl->bind();
  218. return true;
  219. }
  220. GLuint OpenGLFrameBuffer::getFrameBufferID() const noexcept
  221. {
  222. return pimpl != nullptr ? pimpl->frameBufferID : 0;
  223. }
  224. GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget() noexcept
  225. {
  226. GLint fb;
  227. glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fb);
  228. return (GLuint) fb;
  229. }
  230. void OpenGLFrameBuffer::releaseAsRenderingTarget()
  231. {
  232. if (pimpl != nullptr)
  233. pimpl->unbind();
  234. }
  235. void OpenGLFrameBuffer::clear (Colour colour)
  236. {
  237. if (makeCurrentRenderingTarget())
  238. {
  239. OpenGLHelpers::clear (colour);
  240. releaseAsRenderingTarget();
  241. }
  242. }
  243. void OpenGLFrameBuffer::makeCurrentAndClear()
  244. {
  245. if (makeCurrentRenderingTarget())
  246. {
  247. glClearColor (0, 0, 0, 0);
  248. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  249. }
  250. }
  251. bool OpenGLFrameBuffer::readPixels (PixelARGB* target, const Rectangle<int>& area)
  252. {
  253. if (! makeCurrentRenderingTarget())
  254. return false;
  255. glPixelStorei (GL_PACK_ALIGNMENT, 4);
  256. glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  257. JUCE_RGBA_FORMAT, GL_UNSIGNED_BYTE, target);
  258. pimpl->unbind();
  259. return true;
  260. }
  261. bool OpenGLFrameBuffer::writePixels (const PixelARGB* data, const Rectangle<int>& area)
  262. {
  263. OpenGLTargetSaver ts (pimpl->context);
  264. if (! makeCurrentRenderingTarget())
  265. return false;
  266. glDisable (GL_DEPTH_TEST);
  267. glDisable (GL_BLEND);
  268. JUCE_CHECK_OPENGL_ERROR
  269. OpenGLTexture tex;
  270. tex.loadARGB (data, area.getWidth(), area.getHeight());
  271. glViewport (0, 0, pimpl->width, pimpl->height);
  272. pimpl->context.copyTexture (area, Rectangle<int> (area.getX(), area.getY(),
  273. tex.getWidth(), tex.getHeight()),
  274. pimpl->width, pimpl->height, true);
  275. JUCE_CHECK_OPENGL_ERROR
  276. return true;
  277. }
  278. } // namespace juce