Respect initial stencil contents when merging opsTasks

Bug: skia:11952
Change-Id: I0eb6f578dcc95fa6e92ff959a6483c7583d0ad9b
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/404396
Reviewed-by: Adlai Holler <adlai@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
diff --git a/src/gpu/GrOpsTask.cpp b/src/gpu/GrOpsTask.cpp
index 6d36e48..1cb5ff5 100644
--- a/src/gpu/GrOpsTask.cpp
+++ b/src/gpu/GrOpsTask.cpp
@@ -727,6 +727,18 @@
         fClippedContentBounds.join(toMerge->fClippedContentBounds);
         fTotalBounds.join(toMerge->fTotalBounds);
         fRenderPassXferBarriers |= toMerge->fRenderPassXferBarriers;
+        if (fInitialStencilContent == StencilContent::kDontCare) {
+            // Propogate the first stencil content that isn't kDontCare.
+            //
+            // Once the stencil has any kind of initial content that isn't kDontCare, then the
+            // inital contents of subsequent opsTasks that get merged in don't matter.
+            //
+            // (This works because the opsTask all target the same render target and are in
+            // painter's order. kPreserved obviously happens automatically with a merge, and kClear
+            // is also automatic because the contract is for ops to leave the stencil buffer in a
+            // cleared state when finished.)
+            fInitialStencilContent = toMerge->fInitialStencilContent;
+        }
         fUsesMSAASurface |= toMerge->fUsesMSAASurface;
         SkDEBUGCODE(fNumClips += toMerge->fNumClips);
     }