Fix for Android batching bug

On Android it looks like we have:
   stencilClip1
   draw1
   stencilClip2
   draw2

where draw1 is being forward combined with draw2 b.c. they are both stencil clipped but it shouldn't b.c. they are different stencil clips.

Change-Id: Ia704d7ab869022a055eed0726e2b7fab8eaaf817
Reviewed-on: https://skia-review.googlesource.com/20977
Reviewed-by: Chris Dalton <csmartdalton@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrAppliedClip.h b/src/gpu/GrAppliedClip.h
index 8488dc4..4181f43 100644
--- a/src/gpu/GrAppliedClip.h
+++ b/src/gpu/GrAppliedClip.h
@@ -12,6 +12,8 @@
 #include "GrScissorState.h"
 #include "GrWindowRectsState.h"
 
+#include "SkClipStack.h"
+
 /**
  * Produced by GrClip. It provides a set of modifications to the drawing state that are used to
  * create the final GrPipeline for a GrOp.
@@ -25,7 +27,7 @@
     const GrScissorState& scissorState() const { return fScissorState; }
     const GrWindowRectsState& windowRectsState() const { return fWindowRectsState; }
     GrFragmentProcessor* clipCoverageFragmentProcessor() const { return fClipCoverageFP.get(); }
-    bool hasStencilClip() const { return fHasStencilClip; }
+    bool hasStencilClip() const { return SkClipStack::kInvalidGenID != fClipStackID; }
 
     /**
      * Intersects the applied clip with the provided rect. Returns false if the draw became empty.
@@ -51,18 +53,18 @@
         fClipCoverageFP = fp;
     }
 
-    void addStencilClip() {
-        SkASSERT(!fHasStencilClip);
-        fHasStencilClip = true;
+    void addStencilClip(int32_t clipStackID) {
+        SkASSERT(SkClipStack::kInvalidGenID == fClipStackID);
+        fClipStackID = clipStackID;
     }
 
     bool doesClip() const {
-        return fScissorState.enabled() || fClipCoverageFP || fHasStencilClip ||
+        return fScissorState.enabled() || fClipCoverageFP || this->hasStencilClip() ||
                fWindowRectsState.enabled();
     }
 
     bool operator==(const GrAppliedClip& that) const {
-        if (fScissorState != that.fScissorState || fHasStencilClip != that.fHasStencilClip) {
+        if (fScissorState != that.fScissorState || fClipStackID != that.fClipStackID) {
             return false;
         }
         if (SkToBool(fClipCoverageFP)) {
@@ -81,7 +83,7 @@
     GrScissorState             fScissorState;
     GrWindowRectsState         fWindowRectsState;
     sk_sp<GrFragmentProcessor> fClipCoverageFP;
-    bool                       fHasStencilClip = false;
+    int32_t                    fClipStackID = SkClipStack::kInvalidGenID;
 };
 
 #endif
diff --git a/src/gpu/GrClipStackClip.cpp b/src/gpu/GrClipStackClip.cpp
index 2ab3c46..ecc6252 100644
--- a/src/gpu/GrClipStackClip.cpp
+++ b/src/gpu/GrClipStackClip.cpp
@@ -357,7 +357,7 @@
         reducedClip.drawStencilClipMask(context, renderTargetContext);
         renderTargetContext->priv().setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds());
     }
-    out->addStencilClip();
+    out->addStencilClip(reducedClip.elementsGenID());
     return true;
 }
 
diff --git a/src/gpu/GrReducedClip.cpp b/src/gpu/GrReducedClip.cpp
index 8c98ac1..dfa4166 100644
--- a/src/gpu/GrReducedClip.cpp
+++ b/src/gpu/GrReducedClip.cpp
@@ -662,7 +662,11 @@
 
 class StencilClip final : public GrClip {
 public:
-    StencilClip(const SkIRect& scissorRect) : fFixedClip(scissorRect) {}
+    StencilClip(const SkIRect& scissorRect, int32_t clipStackID)
+        : fFixedClip(scissorRect)
+        , fClipStackID(clipStackID) {
+    }
+
     const GrFixedClip& fixedClip() const { return fFixedClip; }
 
     void setWindowRectangles(const GrWindowRectangles& windows, GrWindowRectsState::Mode mode) {
@@ -685,11 +689,12 @@
                               bounds)) {
             return false;
         }
-        out->addStencilClip();
+        out->addStencilClip(fClipStackID);
         return true;
     }
 
     GrFixedClip fFixedClip;
+    int32_t     fClipStackID;
 
     typedef GrClip INHERITED;
 };
@@ -697,7 +702,7 @@
 bool GrReducedClip::drawStencilClipMask(GrContext* context,
                                         GrRenderTargetContext* renderTargetContext) const {
     // We set the current clip to the bounds so that our recursive draws are scissored to them.
-    StencilClip stencilClip(fIBounds);
+    StencilClip stencilClip(fIBounds, this->elementsGenID());
 
     if (!fWindowRects.empty()) {
         stencilClip.setWindowRectangles(fWindowRects, GrWindowRectsState::Mode::kExclusive);