Share DMSAA attachments in GL between render targets.

Change-Id: Iaad578d43e2b686079b68b1d43337e3fc7c5b4ff
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/408176
Reviewed-by: Brian Salomon <bsalomon@google.com>
Reviewed-by: Chris Dalton <csmartdalton@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
diff --git a/src/gpu/GrResourceProvider.cpp b/src/gpu/GrResourceProvider.cpp
index 92b5fca..38738a5 100644
--- a/src/gpu/GrResourceProvider.cpp
+++ b/src/gpu/GrResourceProvider.cpp
@@ -548,6 +548,42 @@
              stencil->numSamples() == num_stencil_samples(rt, useMSAASurface, *this->caps()));
     return stencil != nullptr;
 }
+sk_sp<GrAttachment> GrResourceProvider::getDiscardableMSAAAttachment(SkISize dimensions,
+                                                                     const GrBackendFormat& format,
+                                                                     int sampleCnt,
+                                                                     GrProtected isProtected) {
+    ASSERT_SINGLE_OWNER
+
+    SkASSERT(sampleCnt > 1);
+
+    if (this->isAbandoned()) {
+        return nullptr;
+    }
+
+    if (!fCaps->validateSurfaceParams(
+                dimensions, format, GrRenderable::kYes, sampleCnt, GrMipmapped::kNo)) {
+        return nullptr;
+    }
+
+    GrUniqueKey key;
+    GrAttachment::ComputeSharedAttachmentUniqueKey(*this->caps(),
+                                                   format,
+                                                   dimensions,
+                                                   GrAttachment::UsageFlags::kColorAttachment,
+                                                   sampleCnt,
+                                                   GrMipmapped::kNo,
+                                                   isProtected,
+                                                   &key);
+    auto msaaAttachment = this->findByUniqueKey<GrAttachment>(key);
+    if (msaaAttachment) {
+        return msaaAttachment;
+    }
+    msaaAttachment = this->makeMSAAAttachment(dimensions, format, sampleCnt, isProtected);
+    if (msaaAttachment) {
+        this->assignUniqueKeyToResource(key, msaaAttachment.get());
+    }
+    return msaaAttachment;
+}
 
 sk_sp<GrAttachment> GrResourceProvider::makeMSAAAttachment(SkISize dimensions,
                                                            const GrBackendFormat& format,
diff --git a/src/gpu/GrResourceProvider.h b/src/gpu/GrResourceProvider.h
index ff88dfe..aa79dd6 100644
--- a/src/gpu/GrResourceProvider.h
+++ b/src/gpu/GrResourceProvider.h
@@ -278,6 +278,17 @@
                                            GrProtected isProtected);
 
     /**
+     * Gets a GrAttachment that can be used for MSAA rendering. This attachment may be shared by
+     * other users. Thus any renderpass that uses the attachment should not assume any specific
+     * data at the start and should not try to save written data at the end. Ideally the render pass
+     * should discard the data at the end.
+     */
+    sk_sp<GrAttachment> getDiscardableMSAAAttachment(SkISize dimensions,
+                                                     const GrBackendFormat& format,
+                                                     int sampleCnt,
+                                                     GrProtected isProtected);
+
+    /**
      * Assigns a unique key to a resource. If the key is associated with another resource that
      * association is removed and replaced by this resource.
      */
diff --git a/src/gpu/gl/GrGLRenderTarget.cpp b/src/gpu/gl/GrGLRenderTarget.cpp
index 98657b4..c5d6a3e 100644
--- a/src/gpu/gl/GrGLRenderTarget.cpp
+++ b/src/gpu/gl/GrGLRenderTarget.cpp
@@ -206,9 +206,10 @@
         }
     }
 
-    fDynamicMSAAAttachment.reset(static_cast<GrGLAttachment*>(resourceProvider->makeMSAAAttachment(
-            this->dimensions(), this->backendFormat(), internalSampleCount,
-            GrProtected(this->isProtected())).release()));
+    fDynamicMSAAAttachment.reset(
+            static_cast<GrGLAttachment*>(resourceProvider->getDiscardableMSAAAttachment(
+                    this->dimensions(), this->backendFormat(), internalSampleCount,
+                    GrProtected(this->isProtected())).release()));
     if (!fDynamicMSAAAttachment) {
         return false;
     }
diff --git a/src/gpu/vk/GrVkRenderTarget.cpp b/src/gpu/vk/GrVkRenderTarget.cpp
index cf09035..c5f9e97 100644
--- a/src/gpu/vk/GrVkRenderTarget.cpp
+++ b/src/gpu/vk/GrVkRenderTarget.cpp
@@ -210,10 +210,10 @@
     const GrBackendFormat& format = nonMSAAColorAttachment->backendFormat();
 
     sk_sp<GrAttachment> msaaAttachment =
-            rp->makeMSAAAttachment(nonMSAAColorAttachment->dimensions(),
-                                   format,
-                                   gpu->caps()->internalMultisampleCount(format),
-                                   GrProtected(nonMSAAColorAttachment->isProtected()));
+            rp->getDiscardableMSAAAttachment(nonMSAAColorAttachment->dimensions(),
+                                             format,
+                                             gpu->caps()->internalMultisampleCount(format),
+                                             GrProtected(nonMSAAColorAttachment->isProtected()));
     if (!msaaAttachment) {
         return nullptr;
     }