Make GrColorSpaceInfo store GrColorType.

This is largely redundant with GrPixelConfig. However, we intend to
remove GrPixelConfig.

Bug: skia:7580

Change-Id: I03d92303be832711f7821f8a97d36387c9b04a9f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/222883
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/GrRecordingContext.cpp b/src/gpu/GrRecordingContext.cpp
index c4438ef..440e730 100644
--- a/src/gpu/GrRecordingContext.cpp
+++ b/src/gpu/GrRecordingContext.cpp
@@ -145,6 +145,7 @@
 
 sk_sp<GrSurfaceContext> GrRecordingContext::makeWrappedSurfaceContext(
         sk_sp<GrSurfaceProxy> proxy,
+        GrColorType colorType,
         SkAlphaType alphaType,
         sk_sp<SkColorSpace> colorSpace,
         const SkSurfaceProps* props) {
@@ -152,12 +153,12 @@
 
     if (proxy->asRenderTargetProxy()) {
         SkASSERT(kPremul_SkAlphaType == alphaType || kOpaque_SkAlphaType == alphaType);
-        return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
+        return this->drawingManager()->makeRenderTargetContext(std::move(proxy), colorType,
                                                                std::move(colorSpace), props);
     } else {
         SkASSERT(proxy->asTextureProxy());
         SkASSERT(!props);
-        return this->drawingManager()->makeTextureContext(std::move(proxy), alphaType,
+        return this->drawingManager()->makeTextureContext(std::move(proxy), colorType, alphaType,
                                                           std::move(colorSpace));
     }
 }
@@ -169,6 +170,7 @@
         GrMipMapped mipMapped,
         SkBackingFit fit,
         SkBudgeted isDstBudgeted,
+        GrColorType colorType,
         SkAlphaType alphaType,
         sk_sp<SkColorSpace> colorSpace,
         const SkSurfaceProps* props) {
@@ -184,6 +186,7 @@
     }
 
     sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
+                                                                       colorType,
                                                                        alphaType,
                                                                        std::move(colorSpace),
                                                                        props);
@@ -195,9 +198,18 @@
 }
 
 sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext(
-        const GrBackendFormat& format, SkBackingFit fit, int width, int height,
-        GrPixelConfig config, sk_sp<SkColorSpace> colorSpace, int sampleCnt, GrMipMapped mipMapped,
-        GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted,
+        const GrBackendFormat& format,
+        SkBackingFit fit,
+        int width,
+        int height,
+        GrPixelConfig config,
+        GrColorType colorType,
+        sk_sp<SkColorSpace> colorSpace,
+        int sampleCnt,
+        GrMipMapped mipMapped,
+        GrSurfaceOrigin origin,
+        const SkSurfaceProps* surfaceProps,
+        SkBudgeted budgeted,
         GrProtected isProtected) {
     SkASSERT(sampleCnt > 0);
     if (this->abandoned()) {
@@ -224,9 +236,8 @@
 
     auto drawingManager = this->drawingManager();
 
-    sk_sp<GrRenderTargetContext> renderTargetContext =
-        drawingManager->makeRenderTargetContext(std::move(rtp), std::move(colorSpace),
-                                                surfaceProps);
+    sk_sp<GrRenderTargetContext> renderTargetContext = drawingManager->makeRenderTargetContext(
+            std::move(rtp), colorType, std::move(colorSpace), surfaceProps);
     if (!renderTargetContext) {
         return nullptr;
     }
@@ -236,55 +247,109 @@
     return renderTargetContext;
 }
 
-static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
-    switch (config) {
-        case kAlpha_8_GrPixelConfig:
-        case kAlpha_8_as_Alpha_GrPixelConfig:
-        case kAlpha_8_as_Red_GrPixelConfig:
-        case kRGB_565_GrPixelConfig:
-        case kRGBA_4444_GrPixelConfig:
-        case kBGRA_8888_GrPixelConfig:
-        case kRGBA_1010102_GrPixelConfig:
-        case kRGBA_half_GrPixelConfig:
-        case kRGBA_half_Clamped_GrPixelConfig:
-            return kRGBA_8888_GrPixelConfig;
-        case kSBGRA_8888_GrPixelConfig:
-            return kSRGBA_8888_GrPixelConfig;
-        case kAlpha_half_GrPixelConfig:
-        case kAlpha_half_as_Red_GrPixelConfig:
-            return kRGBA_half_GrPixelConfig;
-        case kGray_8_GrPixelConfig:
-        case kGray_8_as_Lum_GrPixelConfig:
-        case kGray_8_as_Red_GrPixelConfig:
-            return kRGB_888_GrPixelConfig;
+static inline bool color_type_and_config_fallback(GrColorType* ct, GrPixelConfig* config) {
+    switch (*ct) {
+        case GrColorType::kAlpha_8:
+            if (*config != kAlpha_8_GrPixelConfig && *config != kAlpha_8_as_Red_GrPixelConfig &&
+                *config != kAlpha_8_as_Alpha_GrPixelConfig) {
+                return false;
+            }
+            *config = kRGBA_8888_GrPixelConfig;
+            *ct = GrColorType::kRGBA_8888;
+            return true;
+        case GrColorType::kBGR_565:
+            if (*config != kRGB_565_GrPixelConfig) {
+                return false;
+            }
+            *config = kRGBA_8888_GrPixelConfig;
+            *ct = GrColorType::kRGBA_8888;
+            return true;
+        case GrColorType::kABGR_4444:
+            if (*config != kRGBA_4444_GrPixelConfig) {
+                return false;
+            }
+            *config = kRGBA_8888_GrPixelConfig;
+            *ct = GrColorType::kRGBA_8888;
+            return true;
+        case GrColorType::kBGRA_8888:
+            if (*config != kBGRA_8888_GrPixelConfig && *config != kSBGRA_8888_GrPixelConfig) {
+                return false;
+            }
+            *config = (*config == kSBGRA_8888_GrPixelConfig) ? kSRGBA_8888_GrPixelConfig
+                                                             : kRGBA_8888_GrPixelConfig;
+            *ct = GrColorType::kRGBA_8888;
+            return true;
+        case GrColorType::kRGBA_1010102:
+            if (*config != kRGBA_1010102_GrPixelConfig) {
+                return false;
+            }
+            *config = kRGBA_8888_GrPixelConfig;
+            *ct = GrColorType::kRGBA_8888;
+            return true;
+        case GrColorType::kRGBA_F16:
+            if (*config != kRGBA_half_GrPixelConfig) {
+                return false;
+            }
+            *config = kRGBA_8888_GrPixelConfig;
+            *ct = GrColorType::kRGBA_8888;
+            return true;
+        case GrColorType::kRGBA_F16_Clamped:
+            if (*config != kRGBA_half_Clamped_GrPixelConfig) {
+                return false;
+            }
+            *config = kRGBA_8888_GrPixelConfig;
+            *ct = GrColorType::kRGBA_8888;
+            return true;
+        case GrColorType::kAlpha_F16:
+            if (*config != kAlpha_half_GrPixelConfig &&
+                *config != kAlpha_half_as_Red_GrPixelConfig) {
+                return false;
+            }
+            *config = kRGBA_half_GrPixelConfig;
+            *ct = GrColorType::kRGBA_F16;
+            return true;
+        case GrColorType::kGray_8:
+            if (*config != kGray_8_GrPixelConfig && *config != kGray_8_as_Red_GrPixelConfig &&
+                *config != kGray_8_as_Lum_GrPixelConfig) {
+                return false;
+            }
+            *config = kRGB_888_GrPixelConfig;
+            *ct = GrColorType::kRGB_888x;
+            return true;
         default:
-            return kUnknown_GrPixelConfig;
+            return false;
     }
 }
 
 sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContextWithFallback(
-        const GrBackendFormat& format, SkBackingFit fit, int width, int height,
-        GrPixelConfig config, sk_sp<SkColorSpace> colorSpace, int sampleCnt, GrMipMapped mipMapped,
-        GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted,
+        const GrBackendFormat& format,
+        SkBackingFit fit,
+        int width,
+        int height,
+        GrPixelConfig config,
+        GrColorType colorType,
+        sk_sp<SkColorSpace> colorSpace,
+        int sampleCnt,
+        GrMipMapped mipMapped,
+        GrSurfaceOrigin origin,
+        const SkSurfaceProps* surfaceProps,
+        SkBudgeted budgeted,
         GrProtected isProtected) {
     GrBackendFormat localFormat = format;
     SkASSERT(sampleCnt > 0);
     if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, config)) {
-        config = GrPixelConfigFallback(config);
-        // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat
-        // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but
-        // for now we just convert the fallback pixel config to an SkColorType and then get the
-        // GrBackendFormat from that.
-        SkColorType colorType;
-        if (!GrPixelConfigToColorType(config, &colorType)) {
+        // TODO: Make the fallback part of GrCaps?
+        if (!color_type_and_config_fallback(&colorType, &config)) {
             return nullptr;
         }
-        localFormat = this->caps()->getBackendFormatFromColorType(colorType);
+        // Figure out what the new backend format should be for the new color type.
+        auto srgb = GrPixelConfigIsSRGBEncoded(config);
+        localFormat = this->caps()->getBackendFormatFromGrColorType(colorType, srgb);
     }
 
-    return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config,
+    return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config, colorType,
                                                  std::move(colorSpace), sampleCnt, mipMapped,
-                                                 origin, surfaceProps, budgeted);
+                                                 origin, surfaceProps, budgeted, isProtected);
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -306,11 +371,12 @@
 
 sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeWrappedSurfaceContext(
         sk_sp<GrSurfaceProxy> proxy,
+        GrColorType colorType,
         SkAlphaType alphaType,
         sk_sp<SkColorSpace> colorSpace,
         const SkSurfaceProps* props) {
-    return fContext->makeWrappedSurfaceContext(std::move(proxy), alphaType, std::move(colorSpace),
-                                               props);
+    return fContext->makeWrappedSurfaceContext(std::move(proxy), colorType, alphaType,
+                                               std::move(colorSpace), props);
 }
 
 sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeDeferredSurfaceContext(
@@ -320,33 +386,61 @@
         GrMipMapped mipMapped,
         SkBackingFit fit,
         SkBudgeted isDstBudgeted,
+        GrColorType colorType,
         SkAlphaType alphaType,
         sk_sp<SkColorSpace> colorSpace,
         const SkSurfaceProps* props) {
     return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit,
-                                                isDstBudgeted, alphaType, std::move(colorSpace),
-                                                props);
+                                                isDstBudgeted, colorType, alphaType,
+                                                std::move(colorSpace), props);
 }
 
 sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext(
-        const GrBackendFormat& format, SkBackingFit fit, int width, int height,
-        GrPixelConfig config, sk_sp<SkColorSpace> colorSpace, int sampleCnt, GrMipMapped mipMapped,
-        GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted,
+        const GrBackendFormat& format,
+        SkBackingFit fit,
+        int width,
+        int height,
+        GrPixelConfig config,
+        GrColorType colorType,
+        sk_sp<SkColorSpace> colorSpace,
+        int sampleCnt,
+        GrMipMapped mipMapped,
+        GrSurfaceOrigin origin,
+        const SkSurfaceProps* surfaceProps,
+        SkBudgeted budgeted,
         GrProtected isProtected) {
-    return fContext->makeDeferredRenderTargetContext(format, fit, width, height, config,
+    return fContext->makeDeferredRenderTargetContext(format, fit, width, height, config, colorType,
                                                      std::move(colorSpace), sampleCnt, mipMapped,
                                                      origin, surfaceProps, budgeted, isProtected);
 }
 
 sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContextWithFallback(
-        const GrBackendFormat& format, SkBackingFit fit, int width, int height,
-        GrPixelConfig config, sk_sp<SkColorSpace> colorSpace, int sampleCnt, GrMipMapped mipMapped,
-        GrSurfaceOrigin origin, const SkSurfaceProps* surfaceProps, SkBudgeted budgeted,
+        const GrBackendFormat& format,
+        SkBackingFit fit,
+        int width,
+        int height,
+        GrPixelConfig config,
+        GrColorType colorType,
+        sk_sp<SkColorSpace> colorSpace,
+        int sampleCnt,
+        GrMipMapped mipMapped,
+        GrSurfaceOrigin origin,
+        const SkSurfaceProps* surfaceProps,
+        SkBudgeted budgeted,
         GrProtected isProtected) {
-    return fContext->makeDeferredRenderTargetContextWithFallback(format, fit, width, height, config,
-                                                                 std::move(colorSpace), sampleCnt,
-                                                                 mipMapped, origin, surfaceProps,
-                                                                 budgeted, isProtected);
+    return fContext->makeDeferredRenderTargetContextWithFallback(format,
+                                                                 fit,
+                                                                 width,
+                                                                 height,
+                                                                 config,
+                                                                 colorType,
+                                                                 std::move(colorSpace),
+                                                                 sampleCnt,
+                                                                 mipMapped,
+                                                                 origin,
+                                                                 surfaceProps,
+                                                                 budgeted,
+                                                                 isProtected);
 }
 
 GrContext* GrRecordingContextPriv::backdoor() {