Reland x2 "Assert that all GrCaps colortype/format -> swizzle queries are legal."

This reverts commit e8d2080a34ed3edfee6b979c8b07ad6e96b240d8.

Change-Id: I3e1d586939fbc2763aa7b7eb0b35cd6e34f2922d
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/280962
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/GrRenderTargetContext.cpp b/src/gpu/GrRenderTargetContext.cpp
index 2eadfcc..f143003 100644
--- a/src/gpu/GrRenderTargetContext.cpp
+++ b/src/gpu/GrRenderTargetContext.cpp
@@ -10,6 +10,7 @@
 #include "include/core/SkDrawable.h"
 #include "include/core/SkVertices.h"
 #include "include/gpu/GrBackendSemaphore.h"
+#include "include/private/GrImageContext.h"
 #include "include/private/GrRecordingContext.h"
 #include "include/private/SkShadowFlags.h"
 #include "include/utils/SkShadowUtils.h"
@@ -33,6 +34,7 @@
 #include "src/gpu/GrDrawingManager.h"
 #include "src/gpu/GrFixedClip.h"
 #include "src/gpu/GrGpuResourcePriv.h"
+#include "src/gpu/GrImageContextPriv.h"
 #include "src/gpu/GrImageInfo.h"
 #include "src/gpu/GrMemoryPool.h"
 #include "src/gpu/GrPathRenderer.h"
@@ -241,6 +243,19 @@
     }
 }
 
+std::tuple<GrColorType, GrBackendFormat> GrRenderTargetContext::GetFallbackColorTypeAndFormat(
+        GrImageContext* context, GrColorType colorType) {
+    do {
+        auto format =
+                context->priv().caps()->getDefaultBackendFormat(colorType, GrRenderable::kYes);
+        if (format.isValid()) {
+            return {colorType, format};
+        }
+        colorType = color_type_fallback(colorType);
+    } while (colorType != GrColorType::kUnknown);
+    return {GrColorType::kUnknown, {}};
+}
+
 std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeWithFallback(
         GrRecordingContext* context,
         GrColorType colorType,
@@ -253,14 +268,12 @@
         GrSurfaceOrigin origin,
         SkBudgeted budgeted,
         const SkSurfaceProps* surfaceProps) {
-    std::unique_ptr<GrRenderTargetContext> rtc;
-    do {
-        rtc = GrRenderTargetContext::Make(context, colorType, colorSpace, fit, dimensions,
-                                          sampleCnt, mipMapped, isProtected, origin, budgeted,
-                                          surfaceProps);
-        colorType = color_type_fallback(colorType);
-    } while (!rtc && colorType != GrColorType::kUnknown);
-    return rtc;
+    auto [ct, format] = GetFallbackColorTypeAndFormat(context, colorType);
+    if (ct == GrColorType::kUnknown) {
+        return nullptr;
+    }
+    return GrRenderTargetContext::Make(context, ct, colorSpace, fit, dimensions, sampleCnt,
+                                       mipMapped, isProtected, origin, budgeted, surfaceProps);
 }
 
 std::unique_ptr<GrRenderTargetContext> GrRenderTargetContext::MakeFromBackendTexture(