GL: Move draw functions from Renderer to Context.

This avoids one more function call + register pushing/popping. Slight
improvement in the command_buffer_perftests.

Bug: angleproject:2877
Change-Id: Ia50a330e62f0bd32266c9f0999810243cbaa41e0
Reviewed-on: https://chromium-review.googlesource.com/c/1293630
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/renderer/gl/ContextGL.cpp b/src/libANGLE/renderer/gl/ContextGL.cpp
index 98b9c23..39402cf 100644
--- a/src/libANGLE/renderer/gl/ContextGL.cpp
+++ b/src/libANGLE/renderer/gl/ContextGL.cpp
@@ -170,12 +170,90 @@
     return mRenderer->finish();
 }
 
+ANGLE_INLINE angle::Result ContextGL::setDrawArraysState(const gl::Context *context,
+                                                         GLint first,
+                                                         GLsizei count,
+                                                         GLsizei instanceCount)
+{
+    if (context->getStateCache().hasAnyActiveClientAttrib())
+    {
+        const gl::State &glState   = context->getGLState();
+        const gl::Program *program = glState.getProgram();
+        const gl::VertexArray *vao = glState.getVertexArray();
+        const VertexArrayGL *vaoGL = GetImplAs<VertexArrayGL>(vao);
+
+        ANGLE_TRY(vaoGL->syncClientSideData(context, program->getActiveAttribLocationsMask(), first,
+                                            count, instanceCount));
+    }
+
+    if (context->getExtensions().webglCompatibility)
+    {
+        const gl::State &glState     = context->getGLState();
+        FramebufferGL *framebufferGL = GetImplAs<FramebufferGL>(glState.getDrawFramebuffer());
+        framebufferGL->maskOutInactiveOutputDrawBuffers(context);
+    }
+
+    return angle::Result::Continue();
+}
+
+ANGLE_INLINE angle::Result ContextGL::setDrawElementsState(const gl::Context *context,
+                                                           GLsizei count,
+                                                           GLenum type,
+                                                           const void *indices,
+                                                           GLsizei instanceCount,
+                                                           const void **outIndices)
+{
+    const gl::State &glState = context->getGLState();
+
+    const gl::Program *program = glState.getProgram();
+
+    const gl::VertexArray *vao = glState.getVertexArray();
+    const VertexArrayGL *vaoGL = GetImplAs<VertexArrayGL>(vao);
+
+    ANGLE_TRY(vaoGL->syncDrawElementsState(context, program->getActiveAttribLocationsMask(), count,
+                                           type, indices, instanceCount,
+                                           glState.isPrimitiveRestartEnabled(), outIndices));
+
+    if (context->getExtensions().webglCompatibility)
+    {
+        FramebufferGL *framebufferGL = GetImplAs<FramebufferGL>(glState.getDrawFramebuffer());
+        framebufferGL->maskOutInactiveOutputDrawBuffers(context);
+    }
+
+    return angle::Result::Continue();
+}
+
+ANGLE_INLINE angle::Result ContextGL::setDrawIndirectState(const gl::Context *context)
+{
+    if (context->getExtensions().webglCompatibility)
+    {
+        const gl::State &glState     = context->getGLState();
+        FramebufferGL *framebufferGL = GetImplAs<FramebufferGL>(glState.getDrawFramebuffer());
+        framebufferGL->maskOutInactiveOutputDrawBuffers(context);
+    }
+
+    return angle::Result::Continue();
+}
+
 angle::Result ContextGL::drawArrays(const gl::Context *context,
                                     gl::PrimitiveMode mode,
                                     GLint first,
                                     GLsizei count)
 {
-    return mRenderer->drawArrays(context, mode, first, count);
+    const gl::Program *program  = context->getGLState().getProgram();
+    const bool usesMultiview    = program->usesMultiview();
+    const GLsizei instanceCount = usesMultiview ? program->getNumViews() : 0;
+
+    ANGLE_TRY(setDrawArraysState(context, first, count, instanceCount));
+    if (!usesMultiview)
+    {
+        getFunctions()->drawArrays(ToGLenum(mode), first, count);
+    }
+    else
+    {
+        getFunctions()->drawArraysInstanced(ToGLenum(mode), first, count, instanceCount);
+    }
+    return angle::Result::Continue();
 }
 
 angle::Result ContextGL::drawArraysInstanced(const gl::Context *context,
@@ -184,7 +262,16 @@
                                              GLsizei count,
                                              GLsizei instanceCount)
 {
-    return mRenderer->drawArraysInstanced(context, mode, first, count, instanceCount);
+    GLsizei adjustedInstanceCount = instanceCount;
+    const gl::Program *program    = context->getGLState().getProgram();
+    if (program->usesMultiview())
+    {
+        adjustedInstanceCount *= program->getNumViews();
+    }
+
+    ANGLE_TRY(setDrawArraysState(context, first, count, adjustedInstanceCount));
+    getFunctions()->drawArraysInstanced(ToGLenum(mode), first, count, adjustedInstanceCount);
+    return angle::Result::Continue();
 }
 
 angle::Result ContextGL::drawElements(const gl::Context *context,
@@ -193,7 +280,22 @@
                                       GLenum type,
                                       const void *indices)
 {
-    return mRenderer->drawElements(context, mode, count, type, indices);
+    const gl::Program *program  = context->getGLState().getProgram();
+    const bool usesMultiview    = program->usesMultiview();
+    const GLsizei instanceCount = usesMultiview ? program->getNumViews() : 0;
+    const void *drawIndexPtr    = nullptr;
+
+    ANGLE_TRY(setDrawElementsState(context, count, type, indices, instanceCount, &drawIndexPtr));
+    if (!usesMultiview)
+    {
+        getFunctions()->drawElements(ToGLenum(mode), count, type, drawIndexPtr);
+    }
+    else
+    {
+        getFunctions()->drawElementsInstanced(ToGLenum(mode), count, type, drawIndexPtr,
+                                              instanceCount);
+    }
+    return angle::Result::Continue();
 }
 
 angle::Result ContextGL::drawElementsInstanced(const gl::Context *context,
@@ -203,7 +305,19 @@
                                                const void *indices,
                                                GLsizei instances)
 {
-    return mRenderer->drawElementsInstanced(context, mode, count, type, indices, instances);
+    GLsizei adjustedInstanceCount = instances;
+    const gl::Program *program    = context->getGLState().getProgram();
+    if (program->usesMultiview())
+    {
+        adjustedInstanceCount *= program->getNumViews();
+    }
+    const void *drawIndexPointer = nullptr;
+
+    ANGLE_TRY(setDrawElementsState(context, count, type, indices, adjustedInstanceCount,
+                                   &drawIndexPointer));
+    getFunctions()->drawElementsInstanced(ToGLenum(mode), count, type, drawIndexPointer,
+                                          adjustedInstanceCount);
+    return angle::Result::Continue();
 }
 
 angle::Result ContextGL::drawRangeElements(const gl::Context *context,
@@ -214,14 +328,33 @@
                                            GLenum type,
                                            const void *indices)
 {
-    return mRenderer->drawRangeElements(context, mode, start, end, count, type, indices);
+    const gl::Program *program   = context->getGLState().getProgram();
+    const bool usesMultiview     = program->usesMultiview();
+    const GLsizei instanceCount  = usesMultiview ? program->getNumViews() : 0;
+    const void *drawIndexPointer = nullptr;
+
+    ANGLE_TRY(
+        setDrawElementsState(context, count, type, indices, instanceCount, &drawIndexPointer));
+    if (!usesMultiview)
+    {
+        getFunctions()->drawRangeElements(ToGLenum(mode), start, end, count, type,
+                                          drawIndexPointer);
+    }
+    else
+    {
+        getFunctions()->drawElementsInstanced(ToGLenum(mode), count, type, drawIndexPointer,
+                                              instanceCount);
+    }
+    return angle::Result::Continue();
 }
 
 angle::Result ContextGL::drawArraysIndirect(const gl::Context *context,
                                             gl::PrimitiveMode mode,
                                             const void *indirect)
 {
-    return mRenderer->drawArraysIndirect(context, mode, indirect);
+    ANGLE_TRY(setDrawIndirectState(context));
+    getFunctions()->drawArraysIndirect(ToGLenum(mode), indirect);
+    return angle::Result::Continue();
 }
 
 angle::Result ContextGL::drawElementsIndirect(const gl::Context *context,
@@ -229,7 +362,9 @@
                                               GLenum type,
                                               const void *indirect)
 {
-    return mRenderer->drawElementsIndirect(context, mode, type, indirect);
+    ANGLE_TRY(setDrawIndirectState(context));
+    getFunctions()->drawElementsIndirect(ToGLenum(mode), type, indirect);
+    return angle::Result::Continue();
 }
 
 void ContextGL::stencilFillPath(const gl::Path *path, GLenum fillMode, GLuint mask)
@@ -415,11 +550,6 @@
     return mRenderer->applyNativeWorkarounds(workarounds);
 }
 
-const FunctionsGL *ContextGL::getFunctions() const
-{
-    return mRenderer->getFunctions();
-}
-
 StateManagerGL *ContextGL::getStateManager()
 {
     return mRenderer->getStateManager();
diff --git a/src/libANGLE/renderer/gl/ContextGL.h b/src/libANGLE/renderer/gl/ContextGL.h
index e0a073c..f28ce1d 100644
--- a/src/libANGLE/renderer/gl/ContextGL.h
+++ b/src/libANGLE/renderer/gl/ContextGL.h
@@ -11,6 +11,7 @@
 #define LIBANGLE_RENDERER_GL_CONTEXTGL_H_
 
 #include "libANGLE/renderer/ContextImpl.h"
+#include "libANGLE/renderer/gl/RendererGL.h"
 
 namespace sh
 {
@@ -194,7 +195,8 @@
     void applyNativeWorkarounds(gl::Workarounds *workarounds) const override;
 
     // Handle helpers
-    const FunctionsGL *getFunctions() const;
+    ANGLE_INLINE const FunctionsGL *getFunctions() const { return mRenderer->getFunctions(); }
+
     StateManagerGL *getStateManager();
     const WorkaroundsGL &getWorkaroundsGL() const;
     BlitGL *getBlitter() const;
@@ -216,6 +218,20 @@
                      unsigned int line);
 
   private:
+    angle::Result setDrawArraysState(const gl::Context *context,
+                                     GLint first,
+                                     GLsizei count,
+                                     GLsizei instanceCount);
+
+    angle::Result setDrawElementsState(const gl::Context *context,
+                                       GLsizei count,
+                                       GLenum type,
+                                       const void *indices,
+                                       GLsizei instanceCount,
+                                       const void **outIndices);
+
+    angle::Result setDrawIndirectState(const gl::Context *context);
+
     std::shared_ptr<RendererGL> mRenderer;
 };
 
diff --git a/src/libANGLE/renderer/gl/RendererGL.cpp b/src/libANGLE/renderer/gl/RendererGL.cpp
index 6c8b76f..62d1c08 100644
--- a/src/libANGLE/renderer/gl/RendererGL.cpp
+++ b/src/libANGLE/renderer/gl/RendererGL.cpp
@@ -247,201 +247,6 @@
     return angle::Result::Continue();
 }
 
-ANGLE_INLINE angle::Result RendererGL::setDrawArraysState(const gl::Context *context,
-                                                          GLint first,
-                                                          GLsizei count,
-                                                          GLsizei instanceCount)
-{
-    if (context->getStateCache().hasAnyActiveClientAttrib())
-    {
-        const gl::State &glState   = context->getGLState();
-        const gl::Program *program = glState.getProgram();
-        const gl::VertexArray *vao = glState.getVertexArray();
-        const VertexArrayGL *vaoGL = GetImplAs<VertexArrayGL>(vao);
-
-        ANGLE_TRY(vaoGL->syncClientSideData(context, program->getActiveAttribLocationsMask(), first,
-                                            count, instanceCount));
-    }
-
-    if (context->getExtensions().webglCompatibility)
-    {
-        const gl::State &glState     = context->getGLState();
-        FramebufferGL *framebufferGL = GetImplAs<FramebufferGL>(glState.getDrawFramebuffer());
-        framebufferGL->maskOutInactiveOutputDrawBuffers(context);
-    }
-
-    return angle::Result::Continue();
-}
-
-ANGLE_INLINE angle::Result RendererGL::setDrawElementsState(const gl::Context *context,
-                                                            GLsizei count,
-                                                            GLenum type,
-                                                            const void *indices,
-                                                            GLsizei instanceCount,
-                                                            const void **outIndices)
-{
-    const gl::State &glState = context->getGLState();
-
-    const gl::Program *program = glState.getProgram();
-
-    const gl::VertexArray *vao = glState.getVertexArray();
-    const VertexArrayGL *vaoGL = GetImplAs<VertexArrayGL>(vao);
-
-    ANGLE_TRY(vaoGL->syncDrawElementsState(context, program->getActiveAttribLocationsMask(), count,
-                                           type, indices, instanceCount,
-                                           glState.isPrimitiveRestartEnabled(), outIndices));
-
-    if (context->getExtensions().webglCompatibility)
-    {
-        FramebufferGL *framebufferGL = GetImplAs<FramebufferGL>(glState.getDrawFramebuffer());
-        framebufferGL->maskOutInactiveOutputDrawBuffers(context);
-    }
-
-    return angle::Result::Continue();
-}
-
-ANGLE_INLINE angle::Result RendererGL::setDrawIndirectState(const gl::Context *context)
-{
-    if (context->getExtensions().webglCompatibility)
-    {
-        const gl::State &glState     = context->getGLState();
-        FramebufferGL *framebufferGL = GetImplAs<FramebufferGL>(glState.getDrawFramebuffer());
-        framebufferGL->maskOutInactiveOutputDrawBuffers(context);
-    }
-
-    return angle::Result::Continue();
-}
-
-angle::Result RendererGL::drawArrays(const gl::Context *context,
-                                     gl::PrimitiveMode mode,
-                                     GLint first,
-                                     GLsizei count)
-{
-    const gl::Program *program  = context->getGLState().getProgram();
-    const bool usesMultiview    = program->usesMultiview();
-    const GLsizei instanceCount = usesMultiview ? program->getNumViews() : 0;
-
-    ANGLE_TRY(setDrawArraysState(context, first, count, instanceCount));
-    if (!usesMultiview)
-    {
-        mFunctions->drawArrays(ToGLenum(mode), first, count);
-    }
-    else
-    {
-        mFunctions->drawArraysInstanced(ToGLenum(mode), first, count, instanceCount);
-    }
-    return angle::Result::Continue();
-}
-
-angle::Result RendererGL::drawArraysInstanced(const gl::Context *context,
-                                              gl::PrimitiveMode mode,
-                                              GLint first,
-                                              GLsizei count,
-                                              GLsizei instanceCount)
-{
-    GLsizei adjustedInstanceCount = instanceCount;
-    const gl::Program *program    = context->getGLState().getProgram();
-    if (program->usesMultiview())
-    {
-        adjustedInstanceCount *= program->getNumViews();
-    }
-
-    ANGLE_TRY(setDrawArraysState(context, first, count, adjustedInstanceCount));
-    mFunctions->drawArraysInstanced(ToGLenum(mode), first, count, adjustedInstanceCount);
-    return angle::Result::Continue();
-}
-
-angle::Result RendererGL::drawElements(const gl::Context *context,
-                                       gl::PrimitiveMode mode,
-                                       GLsizei count,
-                                       GLenum type,
-                                       const void *indices)
-{
-    const gl::Program *program  = context->getGLState().getProgram();
-    const bool usesMultiview    = program->usesMultiview();
-    const GLsizei instanceCount = usesMultiview ? program->getNumViews() : 0;
-    const void *drawIndexPtr    = nullptr;
-
-    ANGLE_TRY(setDrawElementsState(context, count, type, indices, instanceCount, &drawIndexPtr));
-    if (!usesMultiview)
-    {
-        mFunctions->drawElements(ToGLenum(mode), count, type, drawIndexPtr);
-    }
-    else
-    {
-        mFunctions->drawElementsInstanced(ToGLenum(mode), count, type, drawIndexPtr, instanceCount);
-    }
-    return angle::Result::Continue();
-}
-
-angle::Result RendererGL::drawElementsInstanced(const gl::Context *context,
-                                                gl::PrimitiveMode mode,
-                                                GLsizei count,
-                                                GLenum type,
-                                                const void *indices,
-                                                GLsizei instances)
-{
-    GLsizei adjustedInstanceCount = instances;
-    const gl::Program *program    = context->getGLState().getProgram();
-    if (program->usesMultiview())
-    {
-        adjustedInstanceCount *= program->getNumViews();
-    }
-    const void *drawIndexPointer = nullptr;
-
-    ANGLE_TRY(setDrawElementsState(context, count, type, indices, adjustedInstanceCount,
-                                   &drawIndexPointer));
-    mFunctions->drawElementsInstanced(ToGLenum(mode), count, type, drawIndexPointer,
-                                      adjustedInstanceCount);
-    return angle::Result::Continue();
-}
-
-angle::Result RendererGL::drawRangeElements(const gl::Context *context,
-                                            gl::PrimitiveMode mode,
-                                            GLuint start,
-                                            GLuint end,
-                                            GLsizei count,
-                                            GLenum type,
-                                            const void *indices)
-{
-    const gl::Program *program   = context->getGLState().getProgram();
-    const bool usesMultiview     = program->usesMultiview();
-    const GLsizei instanceCount  = usesMultiview ? program->getNumViews() : 0;
-    const void *drawIndexPointer = nullptr;
-
-    ANGLE_TRY(
-        setDrawElementsState(context, count, type, indices, instanceCount, &drawIndexPointer));
-    if (!usesMultiview)
-    {
-        mFunctions->drawRangeElements(ToGLenum(mode), start, end, count, type, drawIndexPointer);
-    }
-    else
-    {
-        mFunctions->drawElementsInstanced(ToGLenum(mode), count, type, drawIndexPointer,
-                                          instanceCount);
-    }
-    return angle::Result::Continue();
-}
-
-angle::Result RendererGL::drawArraysIndirect(const gl::Context *context,
-                                             gl::PrimitiveMode mode,
-                                             const void *indirect)
-{
-    ANGLE_TRY(setDrawIndirectState(context));
-    mFunctions->drawArraysIndirect(ToGLenum(mode), indirect);
-    return angle::Result::Continue();
-}
-
-angle::Result RendererGL::drawElementsIndirect(const gl::Context *context,
-                                               gl::PrimitiveMode mode,
-                                               GLenum type,
-                                               const void *indirect)
-{
-    ANGLE_TRY(setDrawIndirectState(context));
-    mFunctions->drawElementsIndirect(ToGLenum(mode), type, indirect);
-    return angle::Result::Continue();
-}
-
 void RendererGL::stencilFillPath(const gl::ContextState &state,
                                  const gl::Path *path,
                                  GLenum fillMode,
diff --git a/src/libANGLE/renderer/gl/RendererGL.h b/src/libANGLE/renderer/gl/RendererGL.h
index 7d4b035..acce79e 100644
--- a/src/libANGLE/renderer/gl/RendererGL.h
+++ b/src/libANGLE/renderer/gl/RendererGL.h
@@ -50,42 +50,6 @@
     angle::Result flush();
     angle::Result finish();
 
-    angle::Result drawArrays(const gl::Context *context,
-                             gl::PrimitiveMode mode,
-                             GLint first,
-                             GLsizei count);
-    angle::Result drawArraysInstanced(const gl::Context *context,
-                                      gl::PrimitiveMode mode,
-                                      GLint first,
-                                      GLsizei count,
-                                      GLsizei instanceCount);
-
-    angle::Result drawElements(const gl::Context *context,
-                               gl::PrimitiveMode mode,
-                               GLsizei count,
-                               GLenum type,
-                               const void *indices);
-    angle::Result drawElementsInstanced(const gl::Context *context,
-                                        gl::PrimitiveMode mode,
-                                        GLsizei count,
-                                        GLenum type,
-                                        const void *indices,
-                                        GLsizei instances);
-    angle::Result drawRangeElements(const gl::Context *context,
-                                    gl::PrimitiveMode mode,
-                                    GLuint start,
-                                    GLuint end,
-                                    GLsizei count,
-                                    GLenum type,
-                                    const void *indices);
-    angle::Result drawArraysIndirect(const gl::Context *context,
-                                     gl::PrimitiveMode mode,
-                                     const void *indirect);
-    angle::Result drawElementsIndirect(const gl::Context *context,
-                                       gl::PrimitiveMode mode,
-                                       GLenum type,
-                                       const void *indirect);
-
     // CHROMIUM_path_rendering implementation
     void stencilFillPath(const gl::ContextState &state,
                          const gl::Path *path,
@@ -192,20 +156,6 @@
                       gl::Extensions *outExtensions,
                       gl::Limitations *outLimitations) const;
 
-    angle::Result setDrawArraysState(const gl::Context *context,
-                                     GLint first,
-                                     GLsizei count,
-                                     GLsizei instanceCount);
-
-    angle::Result setDrawElementsState(const gl::Context *context,
-                                       GLsizei count,
-                                       GLenum type,
-                                       const void *indices,
-                                       GLsizei instanceCount,
-                                       const void **outIndices);
-
-    angle::Result setDrawIndirectState(const gl::Context *context);
-
     mutable gl::Version mMaxSupportedESVersion;
 
     std::unique_ptr<FunctionsGL> mFunctions;