Make Framebuffer::checkStatus non-const.

BUG=angleproject:1388

Change-Id: Ia9befba0c915c087f2fe0557d91060256f6d4950
Reviewed-on: https://chromium-review.googlesource.com/348955
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/ContextState.cpp b/src/libANGLE/ContextState.cpp
index 8af2527..2b39b44 100644
--- a/src/libANGLE/ContextState.cpp
+++ b/src/libANGLE/ContextState.cpp
@@ -7,6 +7,8 @@
 // Data.cpp: Container class for all GL relevant state, caps and objects
 
 #include "libANGLE/ContextState.h"
+
+#include "libANGLE/Framebuffer.h"
 #include "libANGLE/ResourceManager.h"
 
 namespace gl
diff --git a/src/libANGLE/Framebuffer.cpp b/src/libANGLE/Framebuffer.cpp
index cb1f97d..6d5c998 100644
--- a/src/libANGLE/Framebuffer.cpp
+++ b/src/libANGLE/Framebuffer.cpp
@@ -388,7 +388,7 @@
     return false;
 }
 
-GLenum Framebuffer::checkStatus(const ContextState &state) const
+GLenum Framebuffer::checkStatus(const ContextState &state)
 {
     // The default framebuffer *must* always be complete, though it may not be
     // subject to the same rules as application FBOs. ie, it could have 0x0 size.
@@ -743,7 +743,7 @@
     return mImpl->blit(context, sourceArea, destArea, mask, filter);
 }
 
-int Framebuffer::getSamples(const ContextState &state) const
+int Framebuffer::getSamples(const ContextState &state)
 {
     if (checkStatus(state) == GL_FRAMEBUFFER_COMPLETE)
     {
@@ -836,4 +836,18 @@
     }
 }
 
+int Framebuffer::getCachedSamples(const ContextState &state) const
+{
+    // TODO(jmadill): Framebuffer samples caching.
+    ASSERT(mDirtyBits.none());
+    return const_cast<Framebuffer *>(this)->getSamples(state);
+}
+
+GLenum Framebuffer::getCachedStatus(const ContextState &state) const
+{
+    // TODO(jmadill): Framebuffer status caching.
+    ASSERT(mDirtyBits.none());
+    return const_cast<Framebuffer *>(this)->checkStatus(state);
+}
+
 }  // namespace gl
diff --git a/src/libANGLE/Framebuffer.h b/src/libANGLE/Framebuffer.h
index cd48ab4..19edb82 100644
--- a/src/libANGLE/Framebuffer.h
+++ b/src/libANGLE/Framebuffer.h
@@ -96,8 +96,7 @@
     void setLabel(const std::string &label) override;
     const std::string &getLabel() const override;
 
-    const rx::FramebufferImpl *getImplementation() const { return mImpl; }
-    rx::FramebufferImpl *getImplementation() { return mImpl; }
+    rx::FramebufferImpl *getImplementation() const { return mImpl; }
 
     GLuint id() const { return mId; }
 
@@ -134,10 +133,18 @@
     size_t getNumColorBuffers() const;
     bool hasDepth() const;
     bool hasStencil() const;
-    int getSamples(const ContextState &state) const;
+
     bool usingExtendedDrawBuffers() const;
 
-    GLenum checkStatus(const ContextState &state) const;
+    // This method calls checkStatus.
+    int getSamples(const ContextState &state);
+    GLenum checkStatus(const ContextState &state);
+
+    // These methods do not change any state.
+    // TODO(jmadill): Remove ContextState parameter when able.
+    int getCachedSamples(const ContextState &state) const;
+    GLenum getCachedStatus(const ContextState &state) const;
+
     bool hasValidDepthStencil() const;
 
     Error discard(size_t count, const GLenum *attachments);
diff --git a/src/libANGLE/State.cpp b/src/libANGLE/State.cpp
index fd2abe1..08a6b8c 100644
--- a/src/libANGLE/State.cpp
+++ b/src/libANGLE/State.cpp
@@ -889,22 +889,12 @@
     }
 }
 
-Framebuffer *State::getReadFramebuffer()
+Framebuffer *State::getReadFramebuffer() const
 {
     return mReadFramebuffer;
 }
 
-Framebuffer *State::getDrawFramebuffer()
-{
-    return mDrawFramebuffer;
-}
-
-const Framebuffer *State::getReadFramebuffer() const
-{
-    return mReadFramebuffer;
-}
-
-const Framebuffer *State::getDrawFramebuffer() const
+Framebuffer *State::getDrawFramebuffer() const
 {
     return mDrawFramebuffer;
 }
diff --git a/src/libANGLE/State.h b/src/libANGLE/State.h
index 5416ba9..5aa8b68 100644
--- a/src/libANGLE/State.h
+++ b/src/libANGLE/State.h
@@ -176,10 +176,8 @@
     void setReadFramebufferBinding(Framebuffer *framebuffer);
     void setDrawFramebufferBinding(Framebuffer *framebuffer);
     Framebuffer *getTargetFramebuffer(GLenum target) const;
-    Framebuffer *getReadFramebuffer();
-    Framebuffer *getDrawFramebuffer();
-    const Framebuffer *getReadFramebuffer() const;
-    const Framebuffer *getDrawFramebuffer() const;
+    Framebuffer *getReadFramebuffer() const;
+    Framebuffer *getDrawFramebuffer() const;
     bool removeReadFramebufferBinding(GLuint framebuffer);
     bool removeDrawFramebufferBinding(GLuint framebuffer);
 
diff --git a/src/libANGLE/renderer/d3d/d3d11/Framebuffer11.cpp b/src/libANGLE/renderer/d3d/d3d11/Framebuffer11.cpp
index 70ef4c8..da33e2c 100644
--- a/src/libANGLE/renderer/d3d/d3d11/Framebuffer11.cpp
+++ b/src/libANGLE/renderer/d3d/d3d11/Framebuffer11.cpp
@@ -531,10 +531,9 @@
     return mInternalDirtyBits.any();
 }
 
-void Framebuffer11::syncInternalState() const
+void Framebuffer11::syncInternalState()
 {
-    // TODO(jmadill): Clean up this hack.
-    const_cast<Framebuffer11 *>(this)->syncState(gl::Framebuffer::DirtyBits());
+    syncState(gl::Framebuffer::DirtyBits());
 }
 
 }  // namespace rx
diff --git a/src/libANGLE/renderer/d3d/d3d11/Framebuffer11.h b/src/libANGLE/renderer/d3d/d3d11/Framebuffer11.h
index 99fb46c..8b5ca27 100644
--- a/src/libANGLE/renderer/d3d/d3d11/Framebuffer11.h
+++ b/src/libANGLE/renderer/d3d/d3d11/Framebuffer11.h
@@ -42,8 +42,7 @@
     }
 
     bool hasAnyInternalDirtyBit() const;
-    // TODO(jmadill): make this non-const
-    void syncInternalState() const;
+    void syncInternalState();
 
     void signal(angle::SignalToken token) override;
 
diff --git a/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp b/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
index b49fb8f..4de130d 100644
--- a/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
+++ b/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
@@ -1498,8 +1498,9 @@
 
     // Applies the render target surface, depth stencil surface, viewport rectangle and
     // scissor rectangle to the renderer
-    const gl::Framebuffer *framebufferObject = glState.getDrawFramebuffer();
-    ASSERT(framebufferObject && framebufferObject->checkStatus(data) == GL_FRAMEBUFFER_COMPLETE);
+    gl::Framebuffer *framebufferObject = glState.getDrawFramebuffer();
+    ASSERT(framebufferObject &&
+           framebufferObject->getCachedStatus(data) == GL_FRAMEBUFFER_COMPLETE);
     ANGLE_TRY(applyRenderTarget(framebufferObject));
 
     // Set the present path state
@@ -1516,7 +1517,7 @@
     mStateManager.setScissorRectangle(glState.getScissor(), glState.isScissorTestEnabled());
 
     // Applying rasterizer state to D3D11 device
-    int samples                    = framebufferObject->getSamples(data);
+    int samples                    = framebufferObject->getCachedSamples(data);
     gl::RasterizerState rasterizer = glState.getRasterizerState();
     rasterizer.pointDrawMode       = (drawMode == GL_POINTS);
     rasterizer.multiSample         = (samples != 0);
@@ -1572,7 +1573,7 @@
     return count >= minCount;
 }
 
-gl::Error Renderer11::applyRenderTarget(const gl::Framebuffer *framebuffer)
+gl::Error Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
 {
     return mStateManager.syncFramebuffer(framebuffer);
 }
diff --git a/src/libANGLE/renderer/d3d/d3d11/Renderer11.h b/src/libANGLE/renderer/d3d/d3d11/Renderer11.h
index 43cb2bd..43f3f13 100644
--- a/src/libANGLE/renderer/d3d/d3d11/Renderer11.h
+++ b/src/libANGLE/renderer/d3d/d3d11/Renderer11.h
@@ -136,7 +136,7 @@
     gl::Error updateState(const gl::ContextState &data, GLenum drawMode);
 
     bool applyPrimitiveType(GLenum mode, GLsizei count, bool usesPointSize);
-    gl::Error applyRenderTarget(const gl::Framebuffer *frameBuffer);
+    gl::Error applyRenderTarget(gl::Framebuffer *frameBuffer);
     gl::Error applyUniforms(const ProgramD3D &programD3D,
                             GLenum drawMode,
                             const std::vector<D3DUniform *> &uniformArray) override;
diff --git a/src/libANGLE/renderer/d3d/d3d11/StateManager11.cpp b/src/libANGLE/renderer/d3d/d3d11/StateManager11.cpp
index 593e7b9..d00d831 100644
--- a/src/libANGLE/renderer/d3d/d3d11/StateManager11.cpp
+++ b/src/libANGLE/renderer/d3d/d3d11/StateManager11.cpp
@@ -981,9 +981,9 @@
     mCurrentValueAttribs.clear();
 }
 
-gl::Error StateManager11::syncFramebuffer(const gl::Framebuffer *framebuffer)
+gl::Error StateManager11::syncFramebuffer(gl::Framebuffer *framebuffer)
 {
-    const Framebuffer11 *framebuffer11 = GetImplAs<Framebuffer11>(framebuffer);
+    Framebuffer11 *framebuffer11 = GetImplAs<Framebuffer11>(framebuffer);
     gl::Error error = framebuffer11->invalidateSwizzles();
     if (error.isError())
     {
diff --git a/src/libANGLE/renderer/d3d/d3d11/StateManager11.h b/src/libANGLE/renderer/d3d/d3d11/StateManager11.h
index c98c2c5..771eb43 100644
--- a/src/libANGLE/renderer/d3d/d3d11/StateManager11.h
+++ b/src/libANGLE/renderer/d3d/d3d11/StateManager11.h
@@ -77,7 +77,7 @@
                            ID3D11ShaderResourceView *srv);
     gl::Error clearTextures(gl::SamplerType samplerType, size_t rangeStart, size_t rangeEnd);
 
-    gl::Error syncFramebuffer(const gl::Framebuffer *framebuffer);
+    gl::Error syncFramebuffer(gl::Framebuffer *framebuffer);
 
     void invalidateRenderTarget();
     void invalidateBoundViews();
diff --git a/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp b/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp
index 90ebbe7..eced3a6 100644
--- a/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp
+++ b/src/libANGLE/renderer/d3d/d3d9/Renderer9.cpp
@@ -894,7 +894,8 @@
     // Applies the render target surface, depth stencil surface, viewport rectangle and
     // scissor rectangle to the renderer
     const gl::Framebuffer *framebufferObject = glState.getDrawFramebuffer();
-    ASSERT(framebufferObject && framebufferObject->checkStatus(data) == GL_FRAMEBUFFER_COMPLETE);
+    ASSERT(framebufferObject &&
+           framebufferObject->getCachedStatus(data) == GL_FRAMEBUFFER_COMPLETE);
 
     ANGLE_TRY(applyRenderTarget(context, framebufferObject));
 
@@ -906,7 +907,7 @@
     setScissorRectangle(glState.getScissor(), glState.isScissorTestEnabled());
 
     // Setting blend, depth stencil, and rasterizer states
-    int samples                    = framebufferObject->getSamples(data);
+    int samples                    = framebufferObject->getCachedSamples(data);
     gl::RasterizerState rasterizer = glState.getRasterizerState();
     rasterizer.pointDrawMode       = (drawMode == GL_POINTS);
     rasterizer.multiSample         = (samples != 0);
@@ -927,7 +928,7 @@
 gl::Error Renderer9::setBlendDepthRasterStates(const gl::ContextState &glData, GLenum drawMode)
 {
     const auto &glState            = glData.getState();
-    int samples                    = glState.getDrawFramebuffer()->getSamples(glData);
+    int samples                    = glState.getDrawFramebuffer()->getCachedSamples(glData);
     gl::RasterizerState rasterizer = glState.getRasterizerState();
     rasterizer.pointDrawMode       = (drawMode == GL_POINTS);
     rasterizer.multiSample         = (samples != 0);
diff --git a/src/libANGLE/validationES.cpp b/src/libANGLE/validationES.cpp
index 52c5e5b..f96ad6d 100644
--- a/src/libANGLE/validationES.cpp
+++ b/src/libANGLE/validationES.cpp
@@ -662,9 +662,9 @@
         return false;
     }
 
-    const auto &glState                    = context->getGLState();
-    const gl::Framebuffer *readFramebuffer = glState.getReadFramebuffer();
-    const gl::Framebuffer *drawFramebuffer = glState.getDrawFramebuffer();
+    const auto &glState              = context->getGLState();
+    gl::Framebuffer *readFramebuffer = glState.getReadFramebuffer();
+    gl::Framebuffer *drawFramebuffer = glState.getDrawFramebuffer();
 
     if (!readFramebuffer || !drawFramebuffer)
     {
@@ -678,13 +678,13 @@
         return false;
     }
 
-    if (!readFramebuffer->checkStatus(context->getContextState()))
+    if (readFramebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;
     }
 
-    if (!drawFramebuffer->checkStatus(context->getContextState()))
+    if (drawFramebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;
@@ -1085,22 +1085,22 @@
         return false;
     }
 
-    const Framebuffer *framebuffer = context->getGLState().getReadFramebuffer();
-    ASSERT(framebuffer);
+    auto readFramebuffer = context->getGLState().getReadFramebuffer();
 
-    if (framebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
+    if (readFramebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;
     }
 
-    if (context->getGLState().getReadFramebuffer()->id() != 0 &&
-        framebuffer->getSamples(context->getContextState()) != 0)
+    if (readFramebuffer->id() != 0 && readFramebuffer->getSamples(context->getContextState()) != 0)
     {
         context->handleError(Error(GL_INVALID_OPERATION));
         return false;
     }
 
+    const Framebuffer *framebuffer = context->getGLState().getReadFramebuffer();
+    ASSERT(framebuffer);
     const FramebufferAttachment *readBuffer = framebuffer->getReadColorbuffer();
     if (!readBuffer)
     {
@@ -1587,14 +1587,15 @@
       case GL_IMPLEMENTATION_COLOR_READ_TYPE:
       case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
         {
-            const Framebuffer *framebuffer = context->getGLState().getReadFramebuffer();
-            ASSERT(framebuffer);
-            if (framebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
+            if (context->getGLState().getReadFramebuffer()->checkStatus(
+                    context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
             {
                 context->handleError(Error(GL_INVALID_OPERATION));
                 return false;
             }
 
+            const Framebuffer *framebuffer = context->getGLState().getReadFramebuffer();
+            ASSERT(framebuffer);
             const FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
             if (!attachment)
             {
@@ -1656,16 +1657,15 @@
         return false;
     }
 
-    const auto &state                  = context->getGLState();
-    const gl::Framebuffer *framebuffer = state.getReadFramebuffer();
-    if (framebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
+    const auto &state    = context->getGLState();
+    auto readFramebuffer = state.getReadFramebuffer();
+    if (readFramebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;
     }
 
-    if (state.getReadFramebuffer()->id() != 0 &&
-        framebuffer->getSamples(context->getContextState()) != 0)
+    if (readFramebuffer->id() != 0 && readFramebuffer->getSamples(context->getContextState()) != 0)
     {
         context->handleError(Error(GL_INVALID_OPERATION));
         return false;
@@ -1801,9 +1801,9 @@
         return false;
     }
 
+    Framebuffer *framebuffer = state.getDrawFramebuffer();
     if (context->getLimitations().noSeparateStencilRefsAndMasks)
     {
-        const Framebuffer *framebuffer             = state.getDrawFramebuffer();
         const FramebufferAttachment *stencilBuffer = framebuffer->getStencilbuffer();
         GLuint stencilBits                         = stencilBuffer ? stencilBuffer->getStencilSize() : 0;
         GLuint minimumRequiredStencilMask          = (1 << stencilBits) - 1;
@@ -1824,8 +1824,7 @@
         }
     }
 
-    const gl::Framebuffer *fbo = state.getDrawFramebuffer();
-    if (!fbo || fbo->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
+    if (framebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;
diff --git a/src/libANGLE/validationES2.cpp b/src/libANGLE/validationES2.cpp
index 4472249..9dd1eb0 100644
--- a/src/libANGLE/validationES2.cpp
+++ b/src/libANGLE/validationES2.cpp
@@ -1632,8 +1632,8 @@
         return false;
     }
 
-    const Framebuffer *readFramebuffer = context->getGLState().getReadFramebuffer();
-    const Framebuffer *drawFramebuffer = context->getGLState().getDrawFramebuffer();
+    Framebuffer *readFramebuffer = context->getGLState().getReadFramebuffer();
+    Framebuffer *drawFramebuffer = context->getGLState().getDrawFramebuffer();
 
     if (mask & GL_COLOR_BUFFER_BIT)
     {
@@ -1676,9 +1676,7 @@
                 }
             }
 
-            int readSamples = readFramebuffer->getSamples(context->getContextState());
-
-            if (readSamples != 0 &&
+            if (readFramebuffer->getSamples(context->getContextState()) != 0 &&
                 IsPartialBlit(context, readColorAttachment, drawColorAttachment, srcX0, srcY0,
                               srcX1, srcY1, dstX0, dstY0, dstX1, dstY1))
             {
@@ -1727,10 +1725,8 @@
 
 bool ValidateClear(ValidationContext *context, GLbitfield mask)
 {
-    const Framebuffer *framebufferObject = context->getGLState().getDrawFramebuffer();
-    ASSERT(framebufferObject);
-
-    if (framebufferObject->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
+    auto fbo = context->getGLState().getDrawFramebuffer();
+    if (fbo->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;
diff --git a/src/libANGLE/validationES3.cpp b/src/libANGLE/validationES3.cpp
index cb2d53d..cfc178e 100644
--- a/src/libANGLE/validationES3.cpp
+++ b/src/libANGLE/validationES3.cpp
@@ -891,9 +891,9 @@
         return false;
     }
 
-    const auto &state                  = context->getGLState();
-    const gl::Framebuffer *framebuffer = state.getReadFramebuffer();
-    GLuint readFramebufferID           = framebuffer->id();
+    const auto &state            = context->getGLState();
+    gl::Framebuffer *framebuffer = state.getReadFramebuffer();
+    GLuint readFramebufferID     = framebuffer->id();
 
     if (framebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
     {
@@ -1414,8 +1414,8 @@
         return false;
     }
 
-    const gl::Framebuffer *fbo = context->getGLState().getDrawFramebuffer();
-    if (!fbo || fbo->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
+    if (context->getGLState().getDrawFramebuffer()->checkStatus(context->getContextState()) !=
+        GL_FRAMEBUFFER_COMPLETE)
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;