Incorporate sample count into RTC fallback color type.

Formats can be renderable but not support MSAA.

Change-Id: I33fd295cce0b4b11a0c132c40a85fc7a7d4973b5
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/281477
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/GrBlurUtils.cpp b/src/gpu/GrBlurUtils.cpp
index b6cbdf8..72c5d55 100644
--- a/src/gpu/GrBlurUtils.cpp
+++ b/src/gpu/GrBlurUtils.cpp
@@ -34,7 +34,8 @@
 static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
 
 static GrSurfaceProxyView find_filtered_mask(GrProxyProvider* provider, const GrUniqueKey& key) {
-    return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8);
+    return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8,
+                                                          1);
 }
 
 // Draw a mask using the supplied paint. Since the coverage/geometry
diff --git a/src/gpu/GrClipStackClip.cpp b/src/gpu/GrClipStackClip.cpp
index ec6c648..63409c2 100644
--- a/src/gpu/GrClipStackClip.cpp
+++ b/src/gpu/GrClipStackClip.cpp
@@ -355,7 +355,8 @@
 static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
 
 static GrSurfaceProxyView find_mask(GrProxyProvider* provider, const GrUniqueKey& key) {
-    return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8);
+    return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8,
+                                                          1);
 }
 
 GrSurfaceProxyView GrClipStackClip::createAlphaClipMask(GrRecordingContext* context,
diff --git a/src/gpu/GrProxyProvider.cpp b/src/gpu/GrProxyProvider.cpp
index 1fa39c4..60ada6c 100644
--- a/src/gpu/GrProxyProvider.cpp
+++ b/src/gpu/GrProxyProvider.cpp
@@ -232,7 +232,8 @@
 
 GrSurfaceProxyView GrProxyProvider::findCachedProxyWithColorTypeFallback(const GrUniqueKey& key,
                                                                          GrSurfaceOrigin origin,
-                                                                         GrColorType ct) {
+                                                                         GrColorType ct,
+                                                                         int sampleCnt) {
     auto proxy = this->findOrCreateProxyByUniqueKey(key);
     if (!proxy) {
         return {};
@@ -241,7 +242,7 @@
     if (proxy->asRenderTargetProxy()) {
         GrBackendFormat expectedFormat;
         std::tie(ct, expectedFormat) =
-                GrRenderTargetContext::GetFallbackColorTypeAndFormat(fImageContext, ct);
+                GrRenderTargetContext::GetFallbackColorTypeAndFormat(fImageContext, ct, sampleCnt);
         SkASSERT(expectedFormat == proxy->backendFormat());
     }
     GrSwizzle swizzle = fImageContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), ct);
diff --git a/src/gpu/GrProxyProvider.h b/src/gpu/GrProxyProvider.h
index 4fbceb6..27cbf6a 100644
--- a/src/gpu/GrProxyProvider.h
+++ b/src/gpu/GrProxyProvider.h
@@ -67,7 +67,8 @@
      */
     GrSurfaceProxyView findCachedProxyWithColorTypeFallback(const GrUniqueKey&,
                                                             GrSurfaceOrigin,
-                                                            GrColorType);
+                                                            GrColorType,
+                                                            int sampleCnt);
 
     /*
      * Creates a new texture proxy for the bitmap, optionally with mip levels generated by the cpu.
diff --git a/src/gpu/GrRenderTargetContext.cpp b/src/gpu/GrRenderTargetContext.cpp
index f143003..a3b9367 100644
--- a/src/gpu/GrRenderTargetContext.cpp
+++ b/src/gpu/GrRenderTargetContext.cpp
@@ -244,11 +244,13 @@
 }
 
 std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
-        GrImageContext* context, GrColorType colorType) {
+        GrImageContext* context, GrColorType colorType, int sampleCnt) {
+    auto caps = context->priv().caps();
     do {
-        auto format =
-                context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
-        if (format.isValid()) {
+        auto format = caps->getDefaultBackendFormat(colorType, GrRenderable::kYes);
+        // We continue to the fallback color type if there no default renderable format or we
+        // requested msaa and the format doesn't support msaa.
+        if (format.isValid() && caps->isFormatRenderable(format, sampleCnt)) {
             return {colorType, format};
         }
         colorType = color_type_fallback(colorType);
@@ -268,7 +270,7 @@
         GrSurfaceOrigin origin,
         SkBudgeted budgeted,
         const SkSurfaceProps* surfaceProps) {
-    auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType);
+    auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType, sampleCnt);
     if (ct == GrColorType::kUnknown) {
         return nullptr;
     }
diff --git a/src/gpu/GrRenderTargetContext.h b/src/gpu/GrRenderTargetContext.h
index c230d35..1fdd14d 100644
--- a/src/gpu/GrRenderTargetContext.h
+++ b/src/gpu/GrRenderTargetContext.h
@@ -90,7 +90,8 @@
             const SkSurfaceProps* = nullptr);
 
     static std::tuple<GrColorType, GrBackendFormat> GetFallbackColorTypeAndFormat(GrImageContext*,
-                                                                                  GrColorType);
+                                                                                  GrColorType,
+                                                                                  int sampleCnt);
 
     // Same as previous factory but will try to use fallback GrColorTypes if the one passed in
     // fails. The fallback GrColorType will have at least the number of channels and precision per
diff --git a/src/gpu/effects/GrRRectBlurEffect.fp b/src/gpu/effects/GrRRectBlurEffect.fp
index 643a848..02a8743 100644
--- a/src/gpu/effects/GrRRectBlurEffect.fp
+++ b/src/gpu/effects/GrRRectBlurEffect.fp
@@ -51,7 +51,7 @@
         GrProxyProvider* proxyProvider = context->priv().proxyProvider();
 
         if (auto view = proxyProvider->findCachedProxyWithColorTypeFallback(
-                key, kMaskOrigin, GrColorType::kAlpha_8)) {
+                key, kMaskOrigin, GrColorType::kAlpha_8, 1)) {
             return view;
         }
 
diff --git a/src/gpu/effects/generated/GrRRectBlurEffect.h b/src/gpu/effects/generated/GrRRectBlurEffect.h
index a1a2ea4..84eb69d 100644
--- a/src/gpu/effects/generated/GrRRectBlurEffect.h
+++ b/src/gpu/effects/generated/GrRRectBlurEffect.h
@@ -53,7 +53,7 @@
         GrProxyProvider* proxyProvider = context->priv().proxyProvider();
 
         if (auto view = proxyProvider->findCachedProxyWithColorTypeFallback(
-                    key, kMaskOrigin, GrColorType::kAlpha_8)) {
+                    key, kMaskOrigin, GrColorType::kAlpha_8, 1)) {
             return view;
         }