Remove GrPixelConfig from SkSurfaceCharacterization

TBR=bsalomon@google.com
Change-Id: I3127e672512a398867f39029c97eb807ea77e217
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/225136
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
diff --git a/src/core/SkDeferredDisplayListRecorder.cpp b/src/core/SkDeferredDisplayListRecorder.cpp
index 3fc07fa..4ccf387 100644
--- a/src/core/SkDeferredDisplayListRecorder.cpp
+++ b/src/core/SkDeferredDisplayListRecorder.cpp
@@ -108,6 +108,7 @@
                                                     new SkDeferredDisplayList::LazyProxyData);
 
     auto proxyProvider = fContext->priv().proxyProvider();
+    const GrCaps* caps = fContext->priv().caps();
 
     bool usesGLFBO0 = fCharacterization.usesGLFBO0();
     if (usesGLFBO0) {
@@ -129,11 +130,17 @@
         }
     }
 
+    GrPixelConfig config = caps->getConfigFromBackendFormat(fCharacterization.backendFormat(),
+                                                            fCharacterization.colorType());
+    if (config == kUnknown_GrPixelConfig) {
+        return false;
+    }
+
     GrSurfaceDesc desc;
     desc.fFlags = kRenderTarget_GrSurfaceFlag;
     desc.fWidth = fCharacterization.width();
     desc.fHeight = fCharacterization.height();
-    desc.fConfig = fCharacterization.config();
+    desc.fConfig = config;
     desc.fSampleCnt = fCharacterization.sampleCount();
 
     sk_sp<SkDeferredDisplayList::LazyProxyData> lazyProxyData = fLazyProxyData;
diff --git a/src/core/SkSurfaceCharacterization.cpp b/src/core/SkSurfaceCharacterization.cpp
index a051547..7ca29ce 100644
--- a/src/core/SkSurfaceCharacterization.cpp
+++ b/src/core/SkSurfaceCharacterization.cpp
@@ -11,6 +11,18 @@
 #include "src/gpu/GrCaps.h"
 #include "src/gpu/GrContextThreadSafeProxyPriv.h"
 
+#ifdef SK_DEBUG
+void SkSurfaceCharacterization::validate() const {
+    const GrCaps* caps = fContextInfo->priv().caps();
+
+    int maxColorSamples = caps->maxRenderTargetSampleCount(this->colorType(), fBackendFormat);
+    SkASSERT(maxColorSamples && fSampleCnt && fSampleCnt <= maxColorSamples);
+
+    SkASSERT(caps->areColorTypeAndFormatCompatible(this->colorType(), fBackendFormat));
+}
+#endif
+
+
 bool SkSurfaceCharacterization::operator==(const SkSurfaceCharacterization& other) const {
     if (!this->isValid() || !other.isValid()) {
         return false;
@@ -23,7 +35,7 @@
     return fCacheMaxResourceBytes == other.fCacheMaxResourceBytes &&
            fOrigin == other.fOrigin &&
            fImageInfo == other.fImageInfo &&
-           fConfig == other.fConfig &&
+           fBackendFormat == other.fBackendFormat &&
            fSampleCnt == other.fSampleCnt &&
            fIsTextureable == other.fIsTextureable &&
            fIsMipMapped == other.fIsMipMapped &&
@@ -44,7 +56,7 @@
     }
 
     return SkSurfaceCharacterization(fContextInfo, fCacheMaxResourceBytes,
-                                     fImageInfo.makeWH(width, height), fOrigin, fConfig,
+                                     fImageInfo.makeWH(width, height), fBackendFormat, fOrigin,
                                      fSampleCnt, fIsTextureable, fIsMipMapped, fUsesGLFBO0,
                                      fVulkanSecondaryCBCompatible, fSurfaceProps);
 }
@@ -54,19 +66,8 @@
         return false;
     }
 
-    const GrCaps* caps = fContextInfo->priv().caps();
-
-    // TODO: remove this block involving the pixel config
-    {
-        GrPixelConfig config = caps->getConfigFromBackendFormat(backendTex.getBackendFormat(),
-                                                                this->colorType());
-        if (GrPixelConfig::kUnknown_GrPixelConfig == config) {
-            return false;
-        }
-
-        if (this->config() != config) {
-            return false;
-        }
+    if (fBackendFormat != backendTex.getBackendFormat()) {
+        return false;
     }
 
     if (this->usesGLFBO0()) {
@@ -78,22 +79,12 @@
         return false;
     }
 
-    int maxColorSamples = caps->maxRenderTargetSampleCount(this->colorType(),
-                                                           backendTex.getBackendFormat());
-    if (0 == maxColorSamples) {
-        return false;  // backendTex isn't renderable
-    }
-
     if (this->isMipMapped() && !backendTex.hasMipMaps()) {
         // backend texture is allowed to have mipmaps even if the characterization doesn't require
         // them.
         return false;
     }
 
-    if (!caps->areColorTypeAndFormatCompatible(this->colorType(), backendTex.getBackendFormat())) {
-        return false;
-    }
-
     if (this->width() != backendTex.width() || this->height() != backendTex.height()) {
         return false;
     }
diff --git a/src/gpu/GrBackendSurface.cpp b/src/gpu/GrBackendSurface.cpp
index ba88035..9cb2320 100644
--- a/src/gpu/GrBackendSurface.cpp
+++ b/src/gpu/GrBackendSurface.cpp
@@ -20,6 +20,38 @@
 #include "src/gpu/mtl/GrMtlCppUtil.h"
 #endif
 
+GrBackendFormat::GrBackendFormat(const GrBackendFormat& that)
+        : fBackend(that.fBackend)
+        , fValid(that.fValid)
+        , fTextureType(that.fTextureType) {
+    if (!fValid) {
+        return;
+    }
+
+    switch (fBackend) {
+#ifdef SK_GL
+        case GrBackendApi::kOpenGL:
+            fGLFormat = that.fGLFormat;
+            break;
+#endif
+#ifdef SK_VULKAN
+        case GrBackendApi::kVulkan:
+            fVk = that.fVk;
+            break;
+#endif
+#ifdef SK_METAL
+        case GrBackendApi::kMetal:
+            fMtlFormat = that.fMtlFormat;
+            break;
+#endif
+        case GrBackendApi::kMock:
+            fMockFormat = that.fMockFormat;
+            break;
+        default:
+            SK_ABORT("Unknown GrBackend");
+    }
+}
+
 GrBackendFormat::GrBackendFormat(GrGLenum format, GrGLenum target)
         : fBackend(GrBackendApi::kOpenGL)
         , fValid(true)
diff --git a/src/gpu/GrContextThreadSafeProxy.cpp b/src/gpu/GrContextThreadSafeProxy.cpp
index 8bf5afb..e8ac761 100644
--- a/src/gpu/GrContextThreadSafeProxy.cpp
+++ b/src/gpu/GrContextThreadSafeProxy.cpp
@@ -53,6 +53,11 @@
         return SkSurfaceCharacterization(); // return an invalid characterization
     }
 
+
+    if (!this->caps()->areColorTypeAndFormatCompatible(ii.colorType(), backendFormat)) {
+        return SkSurfaceCharacterization(); // return an invalid characterization
+    }
+
     sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, ii.colorType(), backendFormat);
     if (!sampleCnt) {
         return SkSurfaceCharacterization(); // return an invalid characterization
@@ -67,14 +72,9 @@
         return SkSurfaceCharacterization(); // return an invalid characterization
     }
 
-    GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat, ii.colorType());
-    if (kUnknown_GrPixelConfig == config) {
-        return SkSurfaceCharacterization(); // return an invalid characterization
-    }
-
     return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
-                                     cacheMaxResourceBytes, ii,
-                                     origin, config, sampleCnt,
+                                     cacheMaxResourceBytes, ii, backendFormat,
+                                     origin, sampleCnt,
                                      SkSurfaceCharacterization::Textureable(isTextureable),
                                      SkSurfaceCharacterization::MipMapped(isMipMapped),
                                      SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
diff --git a/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp b/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
index 3772292..f1eff5f 100644
--- a/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
+++ b/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
@@ -32,6 +32,7 @@
 
     sk_sp<GrRenderTargetContext> rtc(
             ctx->priv().makeVulkanSecondaryCBRenderTargetContext(imageInfo, vkInfo, props));
+    SkASSERT(rtc->asSurfaceProxy()->isInstantiated());
 
     int width = rtc->width();
     int height = rtc->height();
@@ -96,9 +97,11 @@
     SkImageInfo ii = SkImageInfo::Make(rtc->width(), rtc->height(), ct, kPremul_SkAlphaType,
                                        rtc->colorSpaceInfo().refColorSpace());
 
-    GrPixelConfig config = rtc->asSurfaceProxy()->config();
-    characterization->set(ctx->threadSafeProxy(), maxResourceBytes, ii, rtc->origin(), config,
-                          rtc->numSamples(), SkSurfaceCharacterization::Textureable(false),
+    GrBackendFormat format = rtc->asRenderTargetProxy()->backendFormat();
+
+    characterization->set(ctx->threadSafeProxy(), maxResourceBytes, ii, format,
+                          rtc->origin(), rtc->numSamples(),
+                          SkSurfaceCharacterization::Textureable(false),
                           SkSurfaceCharacterization::MipMapped(false),
                           SkSurfaceCharacterization::UsesGLFBO0(false),
                           SkSurfaceCharacterization::VulkanSecondaryCBCompatible(true),
@@ -141,11 +144,12 @@
         return false;
     }
 
-    GrPixelConfig config = rtc->asSurfaceProxy()->config();
+    GrBackendFormat rtcFormat = rtc->asRenderTargetProxy()->backendFormat();
+
     return characterization.contextInfo() && characterization.contextInfo()->priv().matches(ctx) &&
            characterization.cacheMaxResourceBytes() <= maxResourceBytes &&
            characterization.origin() == rtc->origin() &&
-           characterization.config() == config &&
+           characterization.backendFormat() == rtcFormat &&
            characterization.width() == rtc->width() &&
            characterization.height() == rtc->height() &&
            characterization.colorType() == rtColorType &&
diff --git a/src/image/SkSurface_Gpu.cpp b/src/image/SkSurface_Gpu.cpp
index a1e62e5..81653d5 100644
--- a/src/image/SkSurface_Gpu.cpp
+++ b/src/image/SkSurface_Gpu.cpp
@@ -208,14 +208,15 @@
     SkImageInfo ii = SkImageInfo::Make(rtc->width(), rtc->height(), ct, kPremul_SkAlphaType,
                                        rtc->colorSpaceInfo().refColorSpace());
 
-    GrPixelConfig config = rtc->asSurfaceProxy()->config();
-    characterization->set(
-            ctx->threadSafeProxy(), maxResourceBytes, ii, rtc->origin(), config, rtc->numSamples(),
-            SkSurfaceCharacterization::Textureable(SkToBool(rtc->asTextureProxy())),
-            SkSurfaceCharacterization::MipMapped(mipmapped),
-            SkSurfaceCharacterization::UsesGLFBO0(usesGLFBO0),
-            SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false), this->props());
+    GrBackendFormat format = rtc->asSurfaceProxy()->backendFormat();
 
+    characterization->set(ctx->threadSafeProxy(), maxResourceBytes, ii, format,
+                          rtc->origin(), rtc->numSamples(),
+                          SkSurfaceCharacterization::Textureable(SkToBool(rtc->asTextureProxy())),
+                          SkSurfaceCharacterization::MipMapped(mipmapped),
+                          SkSurfaceCharacterization::UsesGLFBO0(usesGLFBO0),
+                          SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false),
+                          this->props());
     return true;
 }
 
@@ -299,11 +300,10 @@
         return false;
     }
 
-    GrPixelConfig config = rtc->asSurfaceProxy()->config();
     return characterization.contextInfo() && characterization.contextInfo()->priv().matches(ctx) &&
            characterization.cacheMaxResourceBytes() <= maxResourceBytes &&
            characterization.origin() == rtc->origin() &&
-           characterization.config() == config &&
+           characterization.backendFormat() == rtc->asSurfaceProxy()->backendFormat() &&
            characterization.width() == rtc->width() &&
            characterization.height() == rtc->height() &&
            characterization.colorType() == rtcColorType &&
@@ -354,26 +354,24 @@
         return nullptr;
     }
 
-    const GrBackendFormat format = caps->getBackendFormatFromColorType(c.colorType());
-    if (!format.isValid()) {
+    if (!SkSurface_Gpu::Valid(caps, c.backendFormat())) {
         return nullptr;
     }
 
-    if (!SkSurface_Gpu::Valid(caps, format)) {
+    GrPixelConfig config = caps->getConfigFromBackendFormat(c.backendFormat(), c.colorType());
+    if (config == kUnknown_GrPixelConfig) {
         return nullptr;
     }
 
-    // In order to ensure compatibility we have to match the backend format (i.e. the GrPixelConfig
-    // of the characterization)
     GrSurfaceDesc desc;
     desc.fFlags = kRenderTarget_GrSurfaceFlag;
     desc.fWidth = c.width();
     desc.fHeight = c.height();
-    desc.fConfig = c.config();
+    desc.fConfig = config;
     desc.fSampleCnt = c.sampleCount();
 
     sk_sp<GrSurfaceContext> sc(
-            context->priv().makeDeferredSurfaceContext(format,
+            context->priv().makeDeferredSurfaceContext(c.backendFormat(),
                                                        desc,
                                                        c.origin(),
                                                        GrMipMapped(c.isMipMapped()),