Remove redundant FBO query methods.

Several query methods simply wrapped a NULL check with a default
return value. Most of these safety checks were unnecessary.

BUG=angle:660

Change-Id: I0ac6897f06be082c8efab8721920d1b51ba999ee
Reviewed-on: https://chromium-review.googlesource.com/205606
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Tested-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libGLESv2/Context.cpp b/src/libGLESv2/Context.cpp
index ac09e0e..fe7b361 100644
--- a/src/libGLESv2/Context.cpp
+++ b/src/libGLESv2/Context.cpp
@@ -2594,7 +2594,7 @@
 
     if (mask & GL_DEPTH_BUFFER_BIT)
     {
-        if (mState.depthStencil.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
+        if (mState.depthStencil.depthMask && framebufferObject->getDepthbuffer() != NULL)
         {
             clearParams.clearDepth = true;
         }
@@ -2602,7 +2602,7 @@
 
     if (mask & GL_STENCIL_BUFFER_BIT)
     {
-        if (framebufferObject->getStencilbufferType() != GL_NONE)
+        if (framebufferObject->getStencilbuffer() != NULL)
         {
             rx::RenderTarget *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
             if (!depthStencil)
diff --git a/src/libGLESv2/Framebuffer.cpp b/src/libGLESv2/Framebuffer.cpp
index 4d5e8d4..b40279e 100644
--- a/src/libGLESv2/Framebuffer.cpp
+++ b/src/libGLESv2/Framebuffer.cpp
@@ -271,88 +271,27 @@
     return NULL;
 }
 
-GLenum Framebuffer::getColorbufferType(unsigned int colorAttachment) const
+FramebufferAttachment *Framebuffer::getAttachment(GLenum attachment) const
 {
-    ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
-    return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->type() : GL_NONE);
-}
-
-GLenum Framebuffer::getDepthbufferType() const
-{
-    return (mDepthbuffer ? mDepthbuffer->type() : GL_NONE);
-}
-
-GLenum Framebuffer::getStencilbufferType() const
-{
-    return (mStencilbuffer ? mStencilbuffer->type() : GL_NONE);
-}
-
-GLenum Framebuffer::getDepthStencilbufferType() const
-{
-    return (hasValidDepthStencil() ? mDepthbuffer->type() : GL_NONE);
-}
-
-GLuint Framebuffer::getColorbufferHandle(unsigned int colorAttachment) const
-{
-    ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
-    return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->id() : 0);
-}
-
-GLuint Framebuffer::getDepthbufferHandle() const
-{
-    return (mDepthbuffer ? mDepthbuffer->id() : 0);
-}
-
-GLuint Framebuffer::getStencilbufferHandle() const
-{
-    return (mStencilbuffer ? mStencilbuffer->id() : 0);
-}
-
-GLuint Framebuffer::getDepthStencilbufferHandle() const
-{
-    return (hasValidDepthStencil() ? mDepthbuffer->id() : 0);
-}
-
-GLint Framebuffer::getColorbufferMipLevel(unsigned int colorAttachment) const
-{
-    ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
-    return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->mipLevel() : 0);
-}
-
-GLint Framebuffer::getDepthbufferMipLevel() const
-{
-    return (mDepthbuffer ? mDepthbuffer->mipLevel() : 0);
-}
-
-GLint Framebuffer::getStencilbufferMipLevel() const
-{
-    return (mStencilbuffer ? mStencilbuffer->mipLevel() : 0);
-}
-
-GLint Framebuffer::getDepthStencilbufferMipLevel() const
-{
-    return (hasValidDepthStencil() ? mDepthbuffer->mipLevel() : 0);
-}
-
-GLint Framebuffer::getColorbufferLayer(unsigned int colorAttachment) const
-{
-    ASSERT(colorAttachment < IMPLEMENTATION_MAX_DRAW_BUFFERS);
-    return (mColorbuffers[colorAttachment] ? mColorbuffers[colorAttachment]->layer() : 0);
-}
-
-GLint Framebuffer::getDepthbufferLayer() const
-{
-    return (mDepthbuffer ? mDepthbuffer->layer() : 0);
-}
-
-GLint Framebuffer::getStencilbufferLayer() const
-{
-    return (mStencilbuffer ? mStencilbuffer->layer() : 0);
-}
-
-GLint Framebuffer::getDepthStencilbufferLayer() const
-{
-    return (hasValidDepthStencil() ? mDepthbuffer->layer() : 0);
+    if (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15)
+    {
+        return getColorbuffer(attachment - GL_COLOR_ATTACHMENT0);
+    }
+    else
+    {
+        switch (attachment)
+        {
+          case GL_DEPTH_ATTACHMENT:
+            return getDepthbuffer();
+          case GL_STENCIL_ATTACHMENT:
+            return getStencilbuffer();
+          case GL_DEPTH_STENCIL_ATTACHMENT:
+            return getDepthStencilBuffer();
+          default:
+            UNREACHABLE();
+            return NULL;
+        }
+    }
 }
 
 GLenum Framebuffer::getDrawBufferState(unsigned int colorAttachment) const
@@ -472,8 +411,11 @@
                 // D3D11 does not allow for overlapping RenderTargetViews, so ensure uniqueness
                 for (unsigned int previousColorAttachment = 0; previousColorAttachment < colorAttachment; previousColorAttachment++)
                 {
-                    if (colorbuffer->id() == getColorbufferHandle(previousColorAttachment) &&
-                        colorbuffer->type() == getColorbufferType(previousColorAttachment))
+                    const FramebufferAttachment *previousAttachment = mColorbuffers[previousColorAttachment];
+
+                    if (previousAttachment &&
+                        (colorbuffer->id() == previousAttachment->id() &&
+                         colorbuffer->type() == previousAttachment->type()))
                     {
                         return GL_FRAMEBUFFER_UNSUPPORTED;
                     }
@@ -666,4 +608,22 @@
     return GL_FRAMEBUFFER_COMPLETE;
 }
 
+FramebufferAttachment *DefaultFramebuffer::getAttachment(GLenum attachment) const
+{
+    switch (attachment)
+    {
+      case GL_BACK:
+        return getColorbuffer(0);
+      case GL_DEPTH:
+        return getDepthbuffer();
+      case GL_STENCIL:
+        return getStencilbuffer();
+      case GL_DEPTH_STENCIL:
+        return getDepthStencilBuffer();
+      default:
+        UNREACHABLE();
+        return NULL;
+    }
+}
+
 }
diff --git a/src/libGLESv2/Framebuffer.h b/src/libGLESv2/Framebuffer.h
index 56eb6d9..cd1a311 100644
--- a/src/libGLESv2/Framebuffer.h
+++ b/src/libGLESv2/Framebuffer.h
@@ -51,25 +51,7 @@
     GLenum getReadColorbufferType() const;
     FramebufferAttachment *getFirstColorbuffer() const;
 
-    GLenum getColorbufferType(unsigned int colorAttachment) const;
-    GLenum getDepthbufferType() const;
-    GLenum getStencilbufferType() const;
-    GLenum getDepthStencilbufferType() const;
-
-    GLuint getColorbufferHandle(unsigned int colorAttachment) const;
-    GLuint getDepthbufferHandle() const;
-    GLuint getStencilbufferHandle() const;
-    GLuint getDepthStencilbufferHandle() const;
-
-    GLint getColorbufferMipLevel(unsigned int colorAttachment) const;
-    GLint getDepthbufferMipLevel() const;
-    GLint getStencilbufferMipLevel() const;
-    GLint getDepthStencilbufferMipLevel() const;
-
-    GLint getColorbufferLayer(unsigned int colorAttachment) const;
-    GLint getDepthbufferLayer() const;
-    GLint getStencilbufferLayer() const;
-    GLint getDepthStencilbufferLayer() const;
+    virtual FramebufferAttachment *getAttachment(GLenum attachment) const;
 
     GLenum getDrawBufferState(unsigned int colorAttachment) const;
     void setDrawBufferState(unsigned int colorAttachment, GLenum drawBuffer);
@@ -81,6 +63,7 @@
     bool usingExtendedDrawBuffers() const;
 
     virtual GLenum completeness() const;
+    bool hasValidDepthStencil() const;
 
   protected:
     rx::Renderer *mRenderer;
@@ -92,8 +75,6 @@
     FramebufferAttachment *mDepthbuffer;
     FramebufferAttachment *mStencilbuffer;
 
-    bool hasValidDepthStencil() const;
-
 private:
     DISALLOW_COPY_AND_ASSIGN(Framebuffer);
 
@@ -106,6 +87,7 @@
     DefaultFramebuffer(rx::Renderer *Renderer, Colorbuffer *colorbuffer, DepthStencilbuffer *depthStencil);
 
     virtual GLenum completeness() const;
+    virtual FramebufferAttachment *getAttachment(GLenum attachment) const;
 
   private:
     DISALLOW_COPY_AND_ASSIGN(DefaultFramebuffer);
diff --git a/src/libGLESv2/ProgramBinary.cpp b/src/libGLESv2/ProgramBinary.cpp
index 9635b8e..2efe8b7 100644
--- a/src/libGLESv2/ProgramBinary.cpp
+++ b/src/libGLESv2/ProgramBinary.cpp
@@ -199,7 +199,7 @@
     std::vector<GLenum> outputs(IMPLEMENTATION_MAX_DRAW_BUFFERS);
     for (size_t outputIndex = 0; outputIndex < IMPLEMENTATION_MAX_DRAW_BUFFERS; outputIndex++)
     {
-        if (fbo->getColorbufferType(outputIndex) != GL_NONE)
+        if (fbo->getColorbuffer(outputIndex) != NULL)
         {
             // Always output floats for now
             outputs[outputIndex] = GL_FLOAT;
diff --git a/src/libGLESv2/libGLESv2.cpp b/src/libGLESv2/libGLESv2.cpp
index 012a0d9..f5cf7c3 100644
--- a/src/libGLESv2/libGLESv2.cpp
+++ b/src/libGLESv2/libGLESv2.cpp
@@ -2606,12 +2606,6 @@
             ASSERT(framebufferHandle != GL_INVALID_INDEX);
             gl::Framebuffer *framebuffer = context->getFramebuffer(framebufferHandle);
 
-            GLenum attachmentType;
-            GLuint attachmentHandle;
-            GLuint attachmentLevel;
-            GLuint attachmentLayer;
-            const gl::FramebufferAttachment *attachmentObject;
-
             if (framebufferHandle == 0)
             {
                 if (clientVersion < 3)
@@ -2622,25 +2616,8 @@
                 switch (attachment)
                 {
                   case GL_BACK:
-                    attachmentType = framebuffer->getColorbufferType(0);
-                    attachmentHandle = framebuffer->getColorbufferHandle(0);
-                    attachmentLevel = framebuffer->getColorbufferMipLevel(0);
-                    attachmentLayer = framebuffer->getColorbufferLayer(0);
-                    attachmentObject = framebuffer->getColorbuffer(0);
-                    break;
                   case GL_DEPTH:
-                    attachmentType = framebuffer->getDepthbufferType();
-                    attachmentHandle = framebuffer->getDepthbufferHandle();
-                    attachmentLevel = framebuffer->getDepthbufferMipLevel();
-                    attachmentLayer = framebuffer->getDepthbufferLayer();
-                    attachmentObject = framebuffer->getDepthbuffer();
-                    break;
                   case GL_STENCIL:
-                    attachmentType = framebuffer->getStencilbufferType();
-                    attachmentHandle = framebuffer->getStencilbufferHandle();
-                    attachmentLevel = framebuffer->getStencilbufferMipLevel();
-                    attachmentLayer = framebuffer->getStencilbufferLayer();
-                    attachmentObject = framebuffer->getStencilbuffer();
                     break;
                   default:
                     return gl::error(GL_INVALID_OPERATION);
@@ -2650,41 +2627,20 @@
             {
                 if (attachment >= GL_COLOR_ATTACHMENT0_EXT && attachment <= GL_COLOR_ATTACHMENT15_EXT)
                 {
-                    const unsigned int colorAttachment = (attachment - GL_COLOR_ATTACHMENT0_EXT);
-                    attachmentType = framebuffer->getColorbufferType(colorAttachment);
-                    attachmentHandle = framebuffer->getColorbufferHandle(colorAttachment);
-                    attachmentLevel = framebuffer->getColorbufferMipLevel(colorAttachment);
-                    attachmentLayer = framebuffer->getColorbufferLayer(colorAttachment);
-                    attachmentObject = framebuffer->getColorbuffer(colorAttachment);
+                    // Valid attachment query
                 }
                 else
                 {
                     switch (attachment)
                     {
                       case GL_DEPTH_ATTACHMENT:
-                        attachmentType = framebuffer->getDepthbufferType();
-                        attachmentHandle = framebuffer->getDepthbufferHandle();
-                        attachmentLevel = framebuffer->getDepthbufferMipLevel();
-                        attachmentLayer = framebuffer->getDepthbufferLayer();
-                        attachmentObject = framebuffer->getDepthbuffer();
-                        break;
                       case GL_STENCIL_ATTACHMENT:
-                        attachmentType = framebuffer->getStencilbufferType();
-                        attachmentHandle = framebuffer->getStencilbufferHandle();
-                        attachmentLevel = framebuffer->getStencilbufferMipLevel();
-                        attachmentLayer = framebuffer->getStencilbufferLayer();
-                        attachmentObject = framebuffer->getStencilbuffer();
                         break;
                       case GL_DEPTH_STENCIL_ATTACHMENT:
-                        if (framebuffer->getDepthbufferHandle() != framebuffer->getStencilbufferHandle())
+                        if (framebuffer->hasValidDepthStencil())
                         {
                             return gl::error(GL_INVALID_OPERATION);
                         }
-                        attachmentType = framebuffer->getDepthStencilbufferType();
-                        attachmentHandle = framebuffer->getDepthStencilbufferHandle();
-                        attachmentLevel = framebuffer->getDepthStencilbufferMipLevel();
-                        attachmentLayer = framebuffer->getDepthStencilbufferLayer();
-                        attachmentObject = framebuffer->getDepthStencilBuffer();
                         break;
                       default:
                         return gl::error(GL_INVALID_OPERATION);
@@ -2692,6 +2648,21 @@
                 }
             }
 
+            GLenum attachmentType = GL_NONE;
+            GLuint attachmentHandle = 0;
+            GLuint attachmentLevel = 0;
+            GLuint attachmentLayer = 0;
+
+            const gl::FramebufferAttachment *attachmentObject = framebuffer->getAttachment(attachment);
+
+            if (attachmentObject)
+            {
+                attachmentType = attachmentObject->type();
+                attachmentHandle = attachmentObject->id();
+                attachmentLevel = attachmentObject->mipLevel();
+                attachmentLayer = attachmentObject->layer();
+            }
+
             GLenum attachmentObjectType;   // Type category
             if (framebufferHandle == 0)
             {
diff --git a/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp b/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp
index b9e7404..646bd9b 100644
--- a/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp
+++ b/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp
@@ -816,20 +816,13 @@
     for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
     {
         const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
+        gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
 
-        if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
+        if (colorbuffer && drawBufferState != GL_NONE)
         {
             // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
             ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
 
-            gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
-
-            if (!colorbuffer)
-            {
-                ERR("render target pointer unexpectedly null.");
-                return false;
-            }
-
             // check for zero-sized default framebuffer, which is a special case.
             // in this case we do not wish to modify any state and just silently return false.
             // this will not report any gl error but will cause the calling method to return.
@@ -869,31 +862,16 @@
     }
 
     // Get the depth stencil render buffer and serials
-    gl::FramebufferAttachment *depthStencil = NULL;
+    gl::FramebufferAttachment *depthStencil = framebuffer->getDepthbuffer();
     unsigned int depthbufferSerial = 0;
     unsigned int stencilbufferSerial = 0;
-    if (framebuffer->getDepthbufferType() != GL_NONE)
+    if (depthStencil)
     {
-        depthStencil = framebuffer->getDepthbuffer();
-        if (!depthStencil)
-        {
-            ERR("Depth stencil pointer unexpectedly null.");
-            SafeRelease(framebufferRTVs);
-            return false;
-        }
-
         depthbufferSerial = depthStencil->getSerial();
     }
-    else if (framebuffer->getStencilbufferType() != GL_NONE)
+    else if (framebuffer->getStencilbuffer())
     {
         depthStencil = framebuffer->getStencilbuffer();
-        if (!depthStencil)
-        {
-            ERR("Depth stencil pointer unexpectedly null.");
-            SafeRelease(framebufferRTVs);
-            return false;
-        }
-
         stencilbufferSerial = depthStencil->getSerial();
     }
 
@@ -3349,20 +3327,20 @@
         gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(colorAttachment);
         if (attachment && attachment->isTexture())
         {
-            invalidateFBOAttachmentSwizzles(attachment, framebuffer->getColorbufferMipLevel(colorAttachment));
+            invalidateFBOAttachmentSwizzles(attachment, attachment->mipLevel());
         }
     }
 
     gl::FramebufferAttachment *depthAttachment = framebuffer->getDepthbuffer();
     if (depthAttachment && depthAttachment->isTexture())
     {
-        invalidateFBOAttachmentSwizzles(depthAttachment, framebuffer->getDepthbufferMipLevel());
+        invalidateFBOAttachmentSwizzles(depthAttachment, depthAttachment->mipLevel());
     }
 
     gl::FramebufferAttachment *stencilAttachment = framebuffer->getStencilbuffer();
     if (stencilAttachment && stencilAttachment->isTexture())
     {
-        invalidateFBOAttachmentSwizzles(stencilAttachment, framebuffer->getStencilbufferMipLevel());
+        invalidateFBOAttachmentSwizzles(stencilAttachment, stencilAttachment->mipLevel());
     }
 }
 
diff --git a/src/libGLESv2/renderer/d3d/d3d9/Renderer9.cpp b/src/libGLESv2/renderer/d3d/d3d9/Renderer9.cpp
index c8dffe0..c7d66c5 100644
--- a/src/libGLESv2/renderer/d3d/d3d9/Renderer9.cpp
+++ b/src/libGLESv2/renderer/d3d/d3d9/Renderer9.cpp
@@ -1151,29 +1151,25 @@
 {
     // if there is no color attachment we must synthesize a NULL colorattachment
     // to keep the D3D runtime happy.  This should only be possible if depth texturing.
-    gl::FramebufferAttachment *renderbufferObject = NULL;
-    if (framebuffer->getColorbufferType(0) != GL_NONE)
+    gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(0);
+    if (!attachment)
     {
-        renderbufferObject = framebuffer->getColorbuffer(0);
+        attachment = getNullColorbuffer(framebuffer->getDepthbuffer());
     }
-    else
-    {
-        renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
-    }
-    if (!renderbufferObject)
+    if (!attachment)
     {
         ERR("unable to locate renderbuffer for FBO.");
         return false;
     }
 
     bool renderTargetChanged = false;
-    unsigned int renderTargetSerial = renderbufferObject->getSerial();
+    unsigned int renderTargetSerial = attachment->getSerial();
     if (renderTargetSerial != mAppliedRenderTargetSerial)
     {
         // Apply the render target on the device
         IDirect3DSurface9 *renderTargetSurface = NULL;
 
-        RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
+        RenderTarget *renderTarget = attachment->getRenderTarget();
         if (renderTarget)
         {
             renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
@@ -1192,29 +1188,16 @@
         renderTargetChanged = true;
     }
 
-    gl::FramebufferAttachment *depthStencil = NULL;
+    gl::FramebufferAttachment *depthStencil = framebuffer->getDepthbuffer();
     unsigned int depthbufferSerial = 0;
     unsigned int stencilbufferSerial = 0;
-    if (framebuffer->getDepthbufferType() != GL_NONE)
+    if (depthStencil)
     {
-        depthStencil = framebuffer->getDepthbuffer();
-        if (!depthStencil)
-        {
-            ERR("Depth stencil pointer unexpectedly null.");
-            return false;
-        }
-
         depthbufferSerial = depthStencil->getSerial();
     }
-    else if (framebuffer->getStencilbufferType() != GL_NONE)
+    else if (framebuffer->getStencilbuffer())
     {
         depthStencil = framebuffer->getStencilbuffer();
-        if (!depthStencil)
-        {
-            ERR("Depth stencil pointer unexpectedly null.");
-            return false;
-        }
-
         stencilbufferSerial = depthStencil->getSerial();
     }
 
@@ -1276,9 +1259,9 @@
         mForceSetViewport = true;
         mForceSetBlendState = true;
 
-        mRenderTargetDesc.width = renderbufferObject->getWidth();
-        mRenderTargetDesc.height = renderbufferObject->getHeight();
-        mRenderTargetDesc.format = renderbufferObject->getActualFormat();
+        mRenderTargetDesc.width = attachment->getWidth();
+        mRenderTargetDesc.height = attachment->getHeight();
+        mRenderTargetDesc.format = attachment->getActualFormat();
         mRenderTargetDescInitialized = true;
     }
 
diff --git a/src/libGLESv2/validationES.cpp b/src/libGLESv2/validationES.cpp
index d940b9e..bf0cfca 100644
--- a/src/libGLESv2/validationES.cpp
+++ b/src/libGLESv2/validationES.cpp
@@ -542,13 +542,15 @@
                 {
                     if (drawFramebuffer->isEnabledColorAttachment(colorAttachment))
                     {
-                        if (drawFramebuffer->getColorbufferType(colorAttachment) != GL_TEXTURE_2D &&
-                            drawFramebuffer->getColorbufferType(colorAttachment) != GL_RENDERBUFFER)
+                        FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(colorAttachment);
+                        ASSERT(attachment);
+
+                        if (attachment->type() != GL_TEXTURE_2D && attachment->type() != GL_RENDERBUFFER)
                         {
                             return gl::error(GL_INVALID_OPERATION, false);
                         }
 
-                        if (drawFramebuffer->getColorbuffer(colorAttachment)->getActualFormat() != readColorBuffer->getActualFormat())
+                        if (attachment->getActualFormat() != readColorBuffer->getActualFormat())
                         {
                             return gl::error(GL_INVALID_OPERATION, false);
                         }