Micro-optimize some validation checks.

Prepares for caching hasMappedBuffer. Also inclines several checks for
faster speed.

Bug: angleproject:2746
Change-Id: I74f9408d7b41e245c3f58d367dd2cc8fbace4a7a
Reviewed-on: https://chromium-review.googlesource.com/1150762
Commit-Queue: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Frank Henigman <fjhenigman@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/validationES.cpp b/src/libANGLE/validationES.cpp
index ca6dedf..d9e0dad 100644
--- a/src/libANGLE/validationES.cpp
+++ b/src/libANGLE/validationES.cpp
@@ -2628,15 +2628,10 @@
     // WebGL buffers cannot be mapped/unmapped because the MapBufferRange, FlushMappedBufferRange,
     // and UnmapBuffer entry points are removed from the WebGL 2.0 API.
     // https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.14
-    if (!extensions.webglCompatibility)
+    if (!extensions.webglCompatibility && state.getVertexArray()->hasMappedEnabledArrayBuffer())
     {
-        // Check for mapped buffers
-        // TODO(jmadill): Optimize this check for non - WebGL contexts.
-        if (state.hasMappedBuffer(BufferBinding::Array))
-        {
-            context->handleError(InvalidOperation());
-            return false;
-        }
+        context->handleError(InvalidOperation());
+        return false;
     }
 
     // Note: these separate values are not supported in WebGL, due to D3D's limitations. See
@@ -2692,7 +2687,6 @@
     // If we are running GLES1, there is no current program.
     if (context->getClientVersion() >= Version(2, 0))
     {
-
         gl::Program *program = state.getProgram();
         if (!program)
         {
@@ -2996,20 +2990,6 @@
         return false;
     }
 
-    // WebGL buffers cannot be mapped/unmapped because the MapBufferRange, FlushMappedBufferRange,
-    // and UnmapBuffer entry points are removed from the WebGL 2.0 API.
-    // https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.14
-    if (!context->getExtensions().webglCompatibility)
-    {
-        // Check for mapped buffers
-        // TODO(jmadill): Optimize this check for non - WebGL contexts.
-        if (state.hasMappedBuffer(gl::BufferBinding::ElementArray))
-        {
-            context->handleError(InvalidOperation() << "Index buffer is mapped.");
-            return false;
-        }
-    }
-
     const gl::VertexArray *vao     = state.getVertexArray();
     gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer().get();
 
@@ -3036,6 +3016,14 @@
             return false;
         }
     }
+    else if (elementArrayBuffer && elementArrayBuffer->isMapped())
+    {
+        // WebGL buffers cannot be mapped/unmapped because the MapBufferRange,
+        // FlushMappedBufferRange, and UnmapBuffer entry points are removed from the WebGL 2.0 API.
+        // https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.14
+        context->handleError(InvalidOperation() << "Index buffer is mapped.");
+        return false;
+    }
 
     if (context->getExtensions().webglCompatibility ||
         !context->getGLState().areClientArraysEnabled())