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.

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