Context: Remove mutable gl::State getter.

This will preserve layering - the API layer doesn't mutate the state
directly, it passes the API call through to the Context. Is also
removes the possiblity of any shenanigans of the Validation layer
changing the GL state.

Also, this CL refactors a few validation entry points to take
ValidationContext instead of Context. ValidationContext will be the
correct way to interact with the gl::Context in the Validation code.

Finally, additional refactorings make ContextState a proper class with
private data. This allows the ContextState itself to keep a mutable
pointer to the gl::State, so ValidationContext can modify it if
necessary (and it will be necessary for Framebuffer completeness
caching).

BUG=angleproject:1388

Change-Id: I86ab3561573caa9535c8d1b8aad4ab3d0e7cd470
Reviewed-on: https://chromium-review.googlesource.com/348954
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/validationES.cpp b/src/libANGLE/validationES.cpp
index 0f73448..52c5e5b 100644
--- a/src/libANGLE/validationES.cpp
+++ b/src/libANGLE/validationES.cpp
@@ -36,7 +36,7 @@
 {
 bool ValidateDrawAttribs(ValidationContext *context, GLint primcount, GLint maxVertex)
 {
-    const gl::State &state     = context->getState();
+    const gl::State &state     = context->getGLState();
     const gl::Program *program = state.getProgram();
 
     const VertexArray *vao     = state.getVertexArray();
@@ -537,7 +537,7 @@
         return false;
     }
 
-    GLuint handle = context->getState().getRenderbufferId();
+    GLuint handle = context->getGLState().getRenderbufferId();
     if (handle == 0)
     {
         context->handleError(Error(GL_INVALID_OPERATION));
@@ -587,7 +587,7 @@
         return false;
     }
 
-    gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
+    gl::Framebuffer *framebuffer = context->getGLState().getTargetFramebuffer(target);
 
     ASSERT(framebuffer);
     if (framebuffer->id() == 0)
@@ -618,7 +618,7 @@
     return true;
 }
 
-bool ValidateBlitFramebufferParameters(gl::Context *context,
+bool ValidateBlitFramebufferParameters(ValidationContext *context,
                                        GLint srcX0,
                                        GLint srcY0,
                                        GLint srcX1,
@@ -662,14 +662,9 @@
         return false;
     }
 
-    if (context->getState().getReadFramebuffer()->id() == context->getState().getDrawFramebuffer()->id())
-    {
-        context->handleError(Error(GL_INVALID_OPERATION));
-        return false;
-    }
-
-    const gl::Framebuffer *readFramebuffer = context->getState().getReadFramebuffer();
-    const gl::Framebuffer *drawFramebuffer = context->getState().getDrawFramebuffer();
+    const auto &glState                    = context->getGLState();
+    const gl::Framebuffer *readFramebuffer = glState.getReadFramebuffer();
+    const gl::Framebuffer *drawFramebuffer = glState.getDrawFramebuffer();
 
     if (!readFramebuffer || !drawFramebuffer)
     {
@@ -677,19 +672,25 @@
         return false;
     }
 
-    if (!readFramebuffer->checkStatus(context->getData()))
+    if (readFramebuffer->id() == drawFramebuffer->id())
+    {
+        context->handleError(Error(GL_INVALID_OPERATION));
+        return false;
+    }
+
+    if (!readFramebuffer->checkStatus(context->getContextState()))
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;
     }
 
-    if (!drawFramebuffer->checkStatus(context->getData()))
+    if (!drawFramebuffer->checkStatus(context->getContextState()))
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;
     }
 
-    if (drawFramebuffer->getSamples(context->getData()) != 0)
+    if (drawFramebuffer->getSamples(context->getContextState()) != 0)
     {
         context->handleError(Error(GL_INVALID_OPERATION));
         return false;
@@ -1069,7 +1070,7 @@
     }
 }
 
-bool ValidateReadPixels(Context *context,
+bool ValidateReadPixels(ValidationContext *context,
                         GLint x,
                         GLint y,
                         GLsizei width,
@@ -1084,17 +1085,17 @@
         return false;
     }
 
-    Framebuffer *framebuffer = context->getState().getReadFramebuffer();
+    const Framebuffer *framebuffer = context->getGLState().getReadFramebuffer();
     ASSERT(framebuffer);
 
-    if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
+    if (framebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;
     }
 
-    if (context->getState().getReadFramebuffer()->id() != 0 &&
-        framebuffer->getSamples(context->getData()) != 0)
+    if (context->getGLState().getReadFramebuffer()->id() != 0 &&
+        framebuffer->getSamples(context->getContextState()) != 0)
     {
         context->handleError(Error(GL_INVALID_OPERATION));
         return false;
@@ -1144,8 +1145,8 @@
     const InternalFormat &sizedFormatInfo = GetInternalFormatInfo(sizedInternalFormat);
 
     auto outputPitchOrErr =
-        sizedFormatInfo.computeRowPitch(type, width, context->getState().getPackAlignment(),
-                                        context->getState().getPackRowLength());
+        sizedFormatInfo.computeRowPitch(type, width, context->getGLState().getPackAlignment(),
+                                        context->getGLState().getPackRowLength());
 
     if (outputPitchOrErr.isError())
     {
@@ -1225,7 +1226,7 @@
     //       of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
     //       no query may be active for either if glBeginQuery targets either.
 
-    if (context->getState().isQueryActive(target))
+    if (context->getGLState().isQueryActive(target))
     {
         context->handleError(Error(GL_INVALID_OPERATION, "Other query is active"));
         return false;
@@ -1270,7 +1271,7 @@
         return false;
     }
 
-    const Query *queryObject = context->getState().getActiveQuery(target);
+    const Query *queryObject = context->getGLState().getActiveQuery(target);
 
     if (queryObject == nullptr)
     {
@@ -1314,7 +1315,7 @@
         return false;
     }
 
-    if (context->getState().isQueryActive(queryObject))
+    if (context->getGLState().isQueryActive(queryObject))
     {
         context->handleError(Error(GL_INVALID_OPERATION, "Query is active"));
         return false;
@@ -1379,7 +1380,7 @@
         return false;
     }
 
-    if (context->getState().isQueryActive(queryObject))
+    if (context->getGLState().isQueryActive(queryObject))
     {
         context->handleError(Error(GL_INVALID_OPERATION, "Query currently active"));
         return false;
@@ -1452,7 +1453,7 @@
         return false;
     }
 
-    gl::Program *program = context->getState().getProgram();
+    gl::Program *program = context->getGLState().getProgram();
     if (!program)
     {
         context->handleError(Error(GL_INVALID_OPERATION));
@@ -1543,7 +1544,10 @@
     return true;
 }
 
-bool ValidateStateQuery(gl::Context *context, GLenum pname, GLenum *nativeType, unsigned int *numParams)
+bool ValidateStateQuery(ValidationContext *context,
+                        GLenum pname,
+                        GLenum *nativeType,
+                        unsigned int *numParams)
 {
     if (!context->getQueryParameterInfo(pname, nativeType, numParams))
     {
@@ -1583,9 +1587,9 @@
       case GL_IMPLEMENTATION_COLOR_READ_TYPE:
       case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
         {
-            Framebuffer *framebuffer = context->getState().getReadFramebuffer();
+            const Framebuffer *framebuffer = context->getGLState().getReadFramebuffer();
             ASSERT(framebuffer);
-            if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
+            if (framebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
             {
                 context->handleError(Error(GL_INVALID_OPERATION));
                 return false;
@@ -1652,15 +1656,16 @@
         return false;
     }
 
-    const gl::Framebuffer *framebuffer = context->getState().getReadFramebuffer();
-    if (framebuffer->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
+    const auto &state                  = context->getGLState();
+    const gl::Framebuffer *framebuffer = state.getReadFramebuffer();
+    if (framebuffer->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;
     }
 
-    const auto &state = context->getState();
-    if (state.getReadFramebuffer()->id() != 0 && framebuffer->getSamples(context->getData()) != 0)
+    if (state.getReadFramebuffer()->id() != 0 &&
+        framebuffer->getSamples(context->getContextState()) != 0)
     {
         context->handleError(Error(GL_INVALID_OPERATION));
         return false;
@@ -1787,7 +1792,7 @@
         return false;
     }
 
-    const State &state = context->getState();
+    const State &state = context->getGLState();
 
     // Check for mapped buffers
     if (state.hasMappedBuffer(GL_ARRAY_BUFFER))
@@ -1798,7 +1803,7 @@
 
     if (context->getLimitations().noSeparateStencilRefsAndMasks)
     {
-        const Framebuffer *framebuffer             = context->getState().getDrawFramebuffer();
+        const Framebuffer *framebuffer             = state.getDrawFramebuffer();
         const FramebufferAttachment *stencilBuffer = framebuffer->getStencilbuffer();
         GLuint stencilBits                         = stencilBuffer ? stencilBuffer->getStencilSize() : 0;
         GLuint minimumRequiredStencilMask          = (1 << stencilBits) - 1;
@@ -1820,7 +1825,7 @@
     }
 
     const gl::Framebuffer *fbo = state.getDrawFramebuffer();
-    if (!fbo || fbo->checkStatus(context->getData()) != GL_FRAMEBUFFER_COMPLETE)
+    if (!fbo || fbo->checkStatus(context->getContextState()) != GL_FRAMEBUFFER_COMPLETE)
     {
         context->handleError(Error(GL_INVALID_FRAMEBUFFER_OPERATION));
         return false;
@@ -1877,7 +1882,11 @@
     return (count > 0);
 }
 
-bool ValidateDrawArrays(Context *context, GLenum mode, GLint first, GLsizei count, GLsizei primcount)
+bool ValidateDrawArrays(ValidationContext *context,
+                        GLenum mode,
+                        GLint first,
+                        GLsizei count,
+                        GLsizei primcount)
 {
     if (first < 0)
     {
@@ -1885,7 +1894,7 @@
         return false;
     }
 
-    const State &state = context->getState();
+    const State &state                          = context->getGLState();
     gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
     if (curTransformFeedback && curTransformFeedback->isActive() && !curTransformFeedback->isPaused() &&
         curTransformFeedback->getPrimitiveMode() != mode)
@@ -1930,7 +1939,7 @@
 static bool ValidateDrawInstancedANGLE(Context *context)
 {
     // Verify there is at least one active attribute with a divisor of zero
-    const gl::State& state = context->getState();
+    const gl::State &state = context->getGLState();
 
     gl::Program *program = state.getProgram();
 
@@ -1985,7 +1994,7 @@
         return false;
     }
 
-    const State &state = context->getState();
+    const State &state = context->getGLState();
 
     gl::TransformFeedback *curTransformFeedback = state.getCurrentTransformFeedback();
     if (curTransformFeedback && curTransformFeedback->isActive() && !curTransformFeedback->isPaused())
@@ -2153,7 +2162,7 @@
         }
     }
 
-    const gl::Framebuffer *framebuffer = context->getState().getTargetFramebuffer(target);
+    const gl::Framebuffer *framebuffer = context->getGLState().getTargetFramebuffer(target);
     ASSERT(framebuffer);
 
     if (framebuffer->id() == 0)
@@ -2614,7 +2623,7 @@
             return false;
         }
     }
-    if (context->getState().isTransformFeedbackActiveUnpaused())
+    if (context->getGLState().isTransformFeedbackActiveUnpaused())
     {
         // ES 3.0.4 section 2.15 page 91
         context->handleError(
@@ -2674,8 +2683,8 @@
         return false;
     }
 
-    ASSERT(context->getState().getDrawFramebuffer());
-    GLuint frameBufferId      = context->getState().getDrawFramebuffer()->id();
+    ASSERT(context->getGLState().getDrawFramebuffer());
+    GLuint frameBufferId      = context->getGLState().getDrawFramebuffer()->id();
     GLuint maxColorAttachment = GL_COLOR_ATTACHMENT0_EXT + context->getCaps().maxColorAttachments;
 
     // This should come first before the check for the default frame buffer
@@ -2771,7 +2780,7 @@
         return false;
     }
 
-    Buffer *buffer = context->getState().getTargetBuffer(target);
+    Buffer *buffer = context->getGLState().getTargetBuffer(target);
 
     // GLES 3.0 section 2.10.1: "Attempts to attempts to modify or query buffer object state for a
     // target bound to zero generate an INVALID_OPERATION error."
@@ -2794,7 +2803,7 @@
         return false;
     }
 
-    Buffer *buffer = context->getState().getTargetBuffer(target);
+    Buffer *buffer = context->getGLState().getTargetBuffer(target);
 
     if (buffer == nullptr || !buffer->isMapped())
     {
@@ -2823,7 +2832,7 @@
         return false;
     }
 
-    Buffer *buffer = context->getState().getTargetBuffer(target);
+    Buffer *buffer = context->getGLState().getTargetBuffer(target);
 
     if (!buffer)
     {
@@ -2911,7 +2920,7 @@
         return false;
     }
 
-    Buffer *buffer = context->getState().getTargetBuffer(target);
+    Buffer *buffer = context->getGLState().getTargetBuffer(target);
 
     if (buffer == nullptr)
     {