Always attach stencil buffers with exact sample count matches

After this CL we will always attach a stencil buffer for rendering
that matches the stencil sample count of the proxy. We will even
downgrade a stencil attachment to one with less samples if necessary.
(In the past we would only guarantee that the attached stencil buffer
had a sample count >= to that of the proxy.)

Change-Id: I358e13ffdf286695257b1b5b672a84d16c417b80
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/253547
Commit-Queue: Chris Dalton <csmartdalton@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/GrOpsTask.cpp b/src/gpu/GrOpsTask.cpp
index 06d3733..6181995 100644
--- a/src/gpu/GrOpsTask.cpp
+++ b/src/gpu/GrOpsTask.cpp
@@ -507,7 +507,7 @@
         stencil = renderTarget->renderTargetPriv().getStencilAttachment();
     }
 
-    SkASSERT(!stencil || stencil->numSamples() >= proxy->numStencilSamples());
+    SkASSERT(!stencil || stencil->numSamples() == proxy->numStencilSamples());
 
     GrLoadOp stencilLoadOp;
     switch (fInitialStencilContent) {
diff --git a/src/gpu/GrRenderTargetProxy.h b/src/gpu/GrRenderTargetProxy.h
index a234a4d..b67554c 100644
--- a/src/gpu/GrRenderTargetProxy.h
+++ b/src/gpu/GrRenderTargetProxy.h
@@ -45,10 +45,7 @@
     }
 
     /**
-     * Returns the number of stencil samples required by this proxy.
-     * NOTE: Once instantiated, the actual render target may have more samples, but it is guaranteed
-     * to have at least this many. (After a multisample stencil buffer has been attached to a render
-     * target, we never "downgrade" it to one with fewer samples.)
+     * Returns the number of stencil samples this proxy will use, or 0 if it does not use stencil.
      */
     int numStencilSamples() const { return fNumStencilSamples; }
 
diff --git a/src/gpu/GrResourceProvider.cpp b/src/gpu/GrResourceProvider.cpp
index 4139d0f..5434c5d 100644
--- a/src/gpu/GrResourceProvider.cpp
+++ b/src/gpu/GrResourceProvider.cpp
@@ -481,10 +481,10 @@
     return buffer;
 }
 
-bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int minStencilSampleCount) {
+bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt, int numStencilSamples) {
     SkASSERT(rt);
     GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment();
-    if (stencil && stencil->numSamples() >= minStencilSampleCount) {
+    if (stencil && stencil->numSamples() == numStencilSamples) {
         return true;
     }
 
@@ -500,12 +500,12 @@
         }
 #endif
         GrStencilAttachment::ComputeSharedStencilAttachmentKey(
-                width, height, minStencilSampleCount, &sbKey);
+                width, height, numStencilSamples, &sbKey);
         auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
         if (!stencil) {
             // Need to try and create a new stencil
             stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(
-                    rt, width, height, minStencilSampleCount));
+                    rt, width, height, numStencilSamples));
             if (!stencil) {
                 return false;
             }
@@ -515,7 +515,7 @@
     }
 
     if (GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment()) {
-        return stencil->numSamples() >= minStencilSampleCount;
+        return stencil->numSamples() == numStencilSamples;
     }
     return false;
 }