Don't use glFrontFace

This is an exploratory commit to see if glFrontFace is the cause of
a recent perf regression. Instead we invert gl_FrontFacing in SkSL.

Bug: skia:
Change-Id: Ida6f6435785e814c089ad34ba2f32793c4ac23c4
Reviewed-on: https://skia-review.googlesource.com/144410
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
diff --git a/src/gpu/gl/GrGLGpu.cpp b/src/gpu/gl/GrGLGpu.cpp
index ea1bfcd..bcc0e08 100644
--- a/src/gpu/gl/GrGLGpu.cpp
+++ b/src/gpu/gl/GrGLGpu.cpp
@@ -359,9 +359,9 @@
 
         // We don't use face culling.
         GL_CALL(Disable(GR_GL_CULL_FACE));
-
-        // Setting the front face keeps gl_FrontFacing consistent in device space.
-        fHWFrontFace = GR_GL_NONE;
+        // We do use separate stencil. Our algorithms don't care which face is front vs. back so
+        // just set this to the default for self-consistency.
+        GL_CALL(FrontFace(GR_GL_CCW));
 
         fHWBufferState[kTexel_GrBufferType].invalidate();
         fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
@@ -1704,7 +1704,6 @@
     fHWProgram->setData(primProc, pipeline);
 
     GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
-    GrSurfaceOrigin origin = pipeline.proxy()->origin();
     GrStencilSettings stencil;
     if (pipeline.isStencilEnabled()) {
         // TODO: attach stencil and create settings during render target flush.
@@ -1716,16 +1715,16 @@
     if (pipeline.isScissorEnabled()) {
         static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
         GrScissorState state(fixedDynamicState ? fixedDynamicState->fScissorRect : kBogusScissor);
-        this->flushScissor(state, glRT->getViewport(), origin);
+        this->flushScissor(state, glRT->getViewport(), pipeline.proxy()->origin());
     } else {
         this->disableScissor();
     }
-    this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, origin);
+    this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, pipeline.proxy()->origin());
     this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled());
 
     // This must come after textures are flushed because a texture may need
     // to be msaa-resolved (which will modify bound FBO state).
-    this->flushRenderTarget(glRT, origin);
+    this->flushRenderTarget(glRT);
 
     return true;
 }
@@ -1857,9 +1856,9 @@
     GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
 
     if (clip.scissorEnabled()) {
-        this->flushRenderTarget(glRT, origin, &clip.scissorRect());
+        this->flushRenderTarget(glRT, origin, clip.scissorRect());
     } else {
-        this->flushRenderTarget(glRT, origin);
+        this->flushRenderTarget(glRT);
     }
     this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
     this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
@@ -2122,17 +2121,14 @@
 }
 
 void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
-                                const SkIRect* bounds) {
+                                const SkIRect& bounds) {
     this->flushRenderTargetNoColorWrites(target);
+    this->didWriteToSurface(target, origin, &bounds);
+}
 
-    // A triangle is front-facing if it winds clockwise in device space.
-    GrGLenum frontFace = (kBottomLeft_GrSurfaceOrigin == origin) ? GR_GL_CW : GR_GL_CCW;
-    if (frontFace != fHWFrontFace) {
-        GL_CALL(FrontFace(frontFace));
-        fHWFrontFace = frontFace;
-    }
-
-    this->didWriteToSurface(target, origin, bounds);
+void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
+    this->flushRenderTargetNoColorWrites(target);
+    this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
 }
 
 void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
@@ -3365,7 +3361,7 @@
     }
 
     GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
-    this->flushRenderTarget(glRT, origin);
+    this->flushRenderTarget(glRT);
 
     this->flushProgram(fStencilClipClearProgram);
 
diff --git a/src/gpu/gl/GrGLGpu.h b/src/gpu/gl/GrGLGpu.h
index 3e6dbaa..78ae024 100644
--- a/src/gpu/gl/GrGLGpu.h
+++ b/src/gpu/gl/GrGLGpu.h
@@ -341,7 +341,9 @@
 
     // The passed bounds contains the render target's color values that will subsequently be
     // written.
-    void flushRenderTarget(GrGLRenderTarget*, GrSurfaceOrigin, const SkIRect* bounds = nullptr);
+    void flushRenderTarget(GrGLRenderTarget*, GrSurfaceOrigin, const SkIRect& bounds);
+    // This version has an implicit bounds of the entire render target.
+    void flushRenderTarget(GrGLRenderTarget*);
     // This version can be used when the render target's colors will not be written.
     void flushRenderTargetNoColorWrites(GrGLRenderTarget*);
 
@@ -558,7 +560,6 @@
     GrStencilSettings                       fHWStencilSettings;
     TriState                                fHWStencilTestEnabled;
 
-    GrGLenum                                fHWFrontFace;
 
     TriState                                fHWWriteToColor;
     GrGpuResource::UniqueID                 fHWBoundRenderTargetUniqueID;
diff --git a/src/gpu/gl/GrGLPathRendering.cpp b/src/gpu/gl/GrGLPathRendering.cpp
index 8a53f88..85c129f 100644
--- a/src/gpu/gl/GrGLPathRendering.cpp
+++ b/src/gpu/gl/GrGLPathRendering.cpp
@@ -88,12 +88,11 @@
     gpu->flushColorWrite(false);
 
     GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(args.fProxy->priv().peekRenderTarget());
-    GrSurfaceOrigin origin = args.fProxy->origin();
     SkISize size = SkISize::Make(rt->width(), rt->height());
-    this->setProjectionMatrix(*args.fViewMatrix, size, origin);
-    gpu->flushScissor(*args.fScissor, rt->getViewport(), origin);
+    this->setProjectionMatrix(*args.fViewMatrix, size, args.fProxy->origin());
+    gpu->flushScissor(*args.fScissor, rt->getViewport(), args.fProxy->origin());
     gpu->flushHWAAState(rt, args.fUseHWAA, true);
-    gpu->flushRenderTarget(rt, origin);
+    gpu->flushRenderTarget(rt);
 
     const GrGLPath* glPath = static_cast<const GrGLPath*>(path);
 
diff --git a/src/sksl/SkSLGLSLCodeGenerator.cpp b/src/sksl/SkSLGLSLCodeGenerator.cpp
index f0c97a3..62cfbae 100644
--- a/src/sksl/SkSLGLSLCodeGenerator.cpp
+++ b/src/sksl/SkSLGLSLCodeGenerator.cpp
@@ -690,7 +690,7 @@
             this->writeFragCoord();
             break;
         case SK_CLOCKWISE_BUILTIN:
-            this->write("gl_FrontFacing");
+            this->write(fProgram.fSettings.fFlipY ? "(!gl_FrontFacing)" : "gl_FrontFacing");
             break;
         case SK_VERTEXID_BUILTIN:
             this->write("gl_VertexID");