Fix memory leak with default Framebuffers.

When the depth-stencil attachment had no depth or stencil bits, we
would like a gl::Renderbuffer object. This was causing unbounded
memory growth in Chrome, and could happen when you watched a lot
of streaming video.

The bug was originally reported and fixed by Austin Kinross in
commit 44f4d74c0f07fa23.

BUG=403471

Change-Id: I022f01a6d3159f766e65f65f023e6ce9baacbc8e
Reviewed-on: https://chromium-review.googlesource.com/231583
Tested-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Hendrik Wagenaar <hendrikw@chromium.org>
Tested-by: Hendrik Wagenaar <hendrikw@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libGLESv2/Framebuffer.cpp b/src/libGLESv2/Framebuffer.cpp
index 5b21433..64617a5 100644
--- a/src/libGLESv2/Framebuffer.cpp
+++ b/src/libGLESv2/Framebuffer.cpp
@@ -637,12 +637,23 @@
     Renderbuffer *colorRenderbuffer = new Renderbuffer(0, colorbuffer);
     mColorbuffers[0] = new RenderbufferAttachment(GL_BACK, colorRenderbuffer);
 
-    Renderbuffer *depthStencilBuffer = new Renderbuffer(0, depthStencil);
+    GLenum depthStencilActualFormat = depthStencil->getActualFormat();
+    const gl::InternalFormat &depthStencilFormatInfo = GetInternalFormatInfo(depthStencilActualFormat);
 
-    // Make a new attachment objects to ensure we do not double-delete
-    // See angle issue 686
-    mDepthbuffer = (depthStencilBuffer->getDepthSize() != 0 ? new RenderbufferAttachment(GL_DEPTH_ATTACHMENT, depthStencilBuffer) : NULL);
-    mStencilbuffer = (depthStencilBuffer->getStencilSize() != 0 ? new RenderbufferAttachment(GL_STENCIL_ATTACHMENT, depthStencilBuffer) : NULL);
+    if (depthStencilFormatInfo.depthBits != 0 || depthStencilFormatInfo.stencilBits != 0)
+    {
+        Renderbuffer *depthStencilBuffer = new Renderbuffer(0, depthStencil);
+
+        // Make a new attachment objects to ensure we do not double-delete
+        // See angle issue 686
+        mDepthbuffer = (depthStencilFormatInfo.depthBits != 0 ? new RenderbufferAttachment(GL_DEPTH_ATTACHMENT, depthStencilBuffer) : NULL);
+        mStencilbuffer = (depthStencilFormatInfo.stencilBits != 0 ? new RenderbufferAttachment(GL_STENCIL_ATTACHMENT, depthStencilBuffer) : NULL);
+    }
+    else
+    {
+        // This method transfers ownership, so delete the unused storage if we don't keep it.
+        SafeDelete(depthStencil);
+    }
 
     mDrawBufferStates[0] = GL_BACK;
     mReadBufferState = GL_BACK;