makeDeferredSurfaceContext -> makeDeferredTextureContext and makeDeferredRenderTargetContext.

Removes some usage of GrSurfaceDesc.

Bug: skia:6718
Change-Id: Icc4f93aba0e5c49a801b4c7bbfcba76a6e30c538
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/227776
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/include/private/GrRecordingContext.h b/include/private/GrRecordingContext.h
index 86e84f8..b671fd6 100644
--- a/include/private/GrRecordingContext.h
+++ b/include/private/GrRecordingContext.h
@@ -21,6 +21,7 @@
 class GrSurfaceContext;
 class GrSurfaceProxy;
 class GrTextBlobCache;
+class GrTextureContext;
 
 class SK_API GrRecordingContext : public GrImageContext {
 public:
@@ -62,16 +63,18 @@
                                                       sk_sp<SkColorSpace> = nullptr,
                                                       const SkSurfaceProps* = nullptr);
 
-    sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
-                                                       const GrSurfaceDesc&,
-                                                       GrSurfaceOrigin,
-                                                       GrMipMapped,
-                                                       SkBackingFit,
-                                                       SkBudgeted,
-                                                       GrColorType,
-                                                       SkAlphaType,
-                                                       sk_sp<SkColorSpace> colorSpace = nullptr,
-                                                       const SkSurfaceProps* = nullptr);
+    /** Create a new texture context backed by a deferred-style GrTextureProxy. */
+    sk_sp<GrTextureContext> makeDeferredTextureContext(
+            SkBackingFit,
+            int width,
+            int height,
+            GrColorType,
+            SkAlphaType,
+            sk_sp<SkColorSpace>,
+            GrMipMapped = GrMipMapped::kNo,
+            GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
+            SkBudgeted = SkBudgeted::kYes,
+            GrProtected = GrProtected::kNo);
 
     /*
      * Create a new render target context backed by a deferred-style
diff --git a/src/gpu/GrCaps.cpp b/src/gpu/GrCaps.cpp
index 145c3aa..50774c6 100644
--- a/src/gpu/GrCaps.cpp
+++ b/src/gpu/GrCaps.cpp
@@ -329,7 +329,8 @@
     // has the expected values stored in the right places taking the swizzle into account. For now
     // we can be more restrictive and just make sure the configs are the same and if we generalize
     // copies and swizzles more in the future this can be updated.
-    if (dst->config() != src->config()) {
+    if (this->makeConfigSpecific(dst->config(), dst->backendFormat()) !=
+        this->makeConfigSpecific(src->config(), src->backendFormat())) {
         return false;
     }
     return this->onCanCopySurface(dst, src, srcRect, dstPoint);
diff --git a/src/gpu/GrCaps.h b/src/gpu/GrCaps.h
index 5bd4234..617838a 100644
--- a/src/gpu/GrCaps.h
+++ b/src/gpu/GrCaps.h
@@ -432,6 +432,17 @@
 
     const GrDriverBugWorkarounds& workarounds() const { return fDriverBugWorkarounds; }
 
+    /**
+     * Given a possibly generic GrPixelConfig and a backend format return a specific
+     * GrPixelConfig.
+     */
+    GrPixelConfig makeConfigSpecific(GrPixelConfig config, const GrBackendFormat& format) const {
+        auto ct = GrPixelConfigToColorType(config);
+        auto result = this->getConfigFromBackendFormat(format, ct);
+        SkASSERT(config == result || AreConfigsCompatible(config, result));
+        return result;
+    }
+
 #ifdef SK_DEBUG
     // This is just a debugging entry point until we're weaned off of GrPixelConfig. It
     // should be used to verify that the pixel config from user-level code (the genericConfig)
diff --git a/src/gpu/GrContextPriv.cpp b/src/gpu/GrContextPriv.cpp
index 476bd08..ad6d8ab 100644
--- a/src/gpu/GrContextPriv.cpp
+++ b/src/gpu/GrContextPriv.cpp
@@ -57,19 +57,19 @@
                                                std::move(colorSpace), props);
 }
 
-sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrBackendFormat& format,
-                                                                  const GrSurfaceDesc& dstDesc,
-                                                                  GrSurfaceOrigin origin,
-                                                                  GrMipMapped mipMapped,
-                                                                  SkBackingFit fit,
-                                                                  SkBudgeted isDstBudgeted,
+sk_sp<GrTextureContext> GrContextPriv::makeDeferredTextureContext(SkBackingFit fit,
+                                                                  int width,
+                                                                  int height,
                                                                   GrColorType colorType,
                                                                   SkAlphaType alphaType,
                                                                   sk_sp<SkColorSpace> colorSpace,
-                                                                  const SkSurfaceProps* props) {
-    return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit,
-                                                isDstBudgeted, colorType, alphaType,
-                                                std::move(colorSpace), props);
+                                                                  GrMipMapped mipMapped,
+                                                                  GrSurfaceOrigin origin,
+                                                                  SkBudgeted budgeted,
+                                                                  GrProtected isProtected) {
+    return fContext->makeDeferredTextureContext(fit, width, height, colorType, alphaType,
+                                                std::move(colorSpace), mipMapped, origin, budgeted,
+                                                isProtected);
 }
 
 sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
diff --git a/src/gpu/GrContextPriv.h b/src/gpu/GrContextPriv.h
index adea6c8..0e129b0 100644
--- a/src/gpu/GrContextPriv.h
+++ b/src/gpu/GrContextPriv.h
@@ -79,16 +79,18 @@
                                                       sk_sp<SkColorSpace> = nullptr,
                                                       const SkSurfaceProps* = nullptr);
 
-    sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
-                                                       const GrSurfaceDesc&,
-                                                       GrSurfaceOrigin,
-                                                       GrMipMapped,
-                                                       SkBackingFit,
-                                                       SkBudgeted,
-                                                       GrColorType,
-                                                       SkAlphaType,
-                                                       sk_sp<SkColorSpace> colorSpace = nullptr,
-                                                       const SkSurfaceProps* = nullptr);
+    /** Create a new texture context backed by a deferred-style GrTextureProxy. */
+    sk_sp<GrTextureContext> makeDeferredTextureContext(
+            SkBackingFit,
+            int width,
+            int height,
+            GrColorType,
+            SkAlphaType,
+            sk_sp<SkColorSpace>,
+            GrMipMapped = GrMipMapped::kNo,
+            GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
+            SkBudgeted = SkBudgeted::kYes,
+            GrProtected = GrProtected::kNo);
 
     /*
      * Create a new render target context backed by a deferred-style
diff --git a/src/gpu/GrRecordingContext.cpp b/src/gpu/GrRecordingContext.cpp
index d720771..cac1492 100644
--- a/src/gpu/GrRecordingContext.cpp
+++ b/src/gpu/GrRecordingContext.cpp
@@ -163,38 +163,46 @@
     }
 }
 
-sk_sp<GrSurfaceContext> GrRecordingContext::makeDeferredSurfaceContext(
-        const GrBackendFormat& format,
-        const GrSurfaceDesc& dstDesc,
-        GrSurfaceOrigin origin,
-        GrMipMapped mipMapped,
+sk_sp<GrTextureContext> GrRecordingContext::makeDeferredTextureContext(
         SkBackingFit fit,
-        SkBudgeted isDstBudgeted,
+        int width,
+        int height,
         GrColorType colorType,
         SkAlphaType alphaType,
         sk_sp<SkColorSpace> colorSpace,
-        const SkSurfaceProps* props) {
-    sk_sp<GrTextureProxy> proxy;
-    if (GrMipMapped::kNo == mipMapped) {
-        proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
-    } else {
-        SkASSERT(SkBackingFit::kExact == fit);
-        proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
+        GrMipMapped mipMapped,
+        GrSurfaceOrigin origin,
+        SkBudgeted budgeted,
+        GrProtected isProtected) {
+    auto format = this->caps()->getBackendFormatFromColorType(colorType);
+    if (!format.isValid()) {
+        return nullptr;
     }
-    if (!proxy) {
+    auto config = this->caps()->getConfigFromBackendFormat(format, colorType);
+    if (config == kUnknown_GrPixelConfig) {
         return nullptr;
     }
 
-    sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
-                                                                       colorType,
-                                                                       alphaType,
-                                                                       std::move(colorSpace),
-                                                                       props);
-    if (sContext && sContext->asRenderTargetContext()) {
-        sContext->asRenderTargetContext()->discard();
+    GrSurfaceDesc desc;
+    desc.fWidth = width;
+    desc.fHeight = height;
+    desc.fIsProtected = isProtected;
+    desc.fConfig = config;
+
+    sk_sp<GrTextureProxy> texture;
+    if (GrMipMapped::kNo == mipMapped) {
+        texture = this->proxyProvider()->createProxy(format, desc, origin, fit, budgeted);
+    } else {
+        texture = this->proxyProvider()->createMipMapProxy(format, desc, origin, budgeted);
+    }
+    if (!texture) {
+        return nullptr;
     }
 
-    return sContext;
+    auto drawingManager = this->drawingManager();
+
+    return drawingManager->makeTextureContext(std::move(texture), colorType, alphaType,
+                                              std::move(colorSpace));
 }
 
 sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext(
@@ -325,20 +333,20 @@
                                                std::move(colorSpace), props);
 }
 
-sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeDeferredSurfaceContext(
-        const GrBackendFormat& format,
-        const GrSurfaceDesc& dstDesc,
-        GrSurfaceOrigin origin,
-        GrMipMapped mipMapped,
+sk_sp<GrTextureContext> GrRecordingContextPriv::makeDeferredTextureContext(
         SkBackingFit fit,
-        SkBudgeted isDstBudgeted,
+        int width,
+        int height,
         GrColorType colorType,
         SkAlphaType alphaType,
         sk_sp<SkColorSpace> colorSpace,
-        const SkSurfaceProps* props) {
-    return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit,
-                                                isDstBudgeted, colorType, alphaType,
-                                                std::move(colorSpace), props);
+        GrMipMapped mipMapped,
+        GrSurfaceOrigin origin,
+        SkBudgeted budgeted,
+        GrProtected isProtected) {
+    return fContext->makeDeferredTextureContext(fit, width, height, colorType, alphaType,
+                                                std::move(colorSpace), mipMapped, origin, budgeted,
+                                                isProtected);
 }
 
 sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext(
diff --git a/src/gpu/GrRecordingContextPriv.h b/src/gpu/GrRecordingContextPriv.h
index 0743ae7..62ec136 100644
--- a/src/gpu/GrRecordingContextPriv.h
+++ b/src/gpu/GrRecordingContextPriv.h
@@ -63,16 +63,18 @@
                                                       sk_sp<SkColorSpace> = nullptr,
                                                       const SkSurfaceProps* = nullptr);
 
-    sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
-                                                       const GrSurfaceDesc&,
-                                                       GrSurfaceOrigin,
-                                                       GrMipMapped,
-                                                       SkBackingFit,
-                                                       SkBudgeted,
-                                                       GrColorType,
-                                                       SkAlphaType,
-                                                       sk_sp<SkColorSpace> colorSpace = nullptr,
-                                                       const SkSurfaceProps* = nullptr);
+    /** Create a new texture context backed by a deferred-style GrTextureProxy. */
+    sk_sp<GrTextureContext> makeDeferredTextureContext(
+            SkBackingFit,
+            int width,
+            int height,
+            GrColorType,
+            SkAlphaType,
+            sk_sp<SkColorSpace>,
+            GrMipMapped = GrMipMapped::kNo,
+            GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
+            SkBudgeted = SkBudgeted::kYes,
+            GrProtected = GrProtected::kNo);
 
     /*
      * Create a new render target context backed by a deferred-style
diff --git a/src/gpu/GrSurfaceContext.cpp b/src/gpu/GrSurfaceContext.cpp
index 708002e..94a3abe 100644
--- a/src/gpu/GrSurfaceContext.cpp
+++ b/src/gpu/GrSurfaceContext.cpp
@@ -403,13 +403,17 @@
     SkDEBUGCODE(this->validate();)
     GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
 
+    const GrCaps* caps = fContext->priv().caps();
+
     SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
     SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
-    SkASSERT(src->config() == this->asSurfaceProxy()->config());
+    SkASSERT(caps->makeConfigSpecific(src->config(), src->backendFormat()) ==
+             caps->makeConfigSpecific(this->asSurfaceProxy()->config(),
+                                      this->asSurfaceProxy()->backendFormat()));
 
     GrSurfaceProxy* dst = this->asSurfaceProxy();
 
-    if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) {
+    if (!caps->canCopySurface(dst, src, srcRect, dstPoint)) {
         return false;
     }
 
diff --git a/src/gpu/GrSurfaceProxy.cpp b/src/gpu/GrSurfaceProxy.cpp
index b24dc45..db7e794 100644
--- a/src/gpu/GrSurfaceProxy.cpp
+++ b/src/gpu/GrSurfaceProxy.cpp
@@ -10,6 +10,8 @@
 
 #include "include/gpu/GrContext.h"
 #include "include/private/GrRecordingContext.h"
+#include "src/core/SkMathPriv.h"
+#include "src/core/SkMipMap.h"
 #include "src/gpu/GrCaps.h"
 #include "src/gpu/GrClip.h"
 #include "src/gpu/GrContextPriv.h"
@@ -18,15 +20,11 @@
 #include "src/gpu/GrProxyProvider.h"
 #include "src/gpu/GrRecordingContextPriv.h"
 #include "src/gpu/GrStencilAttachment.h"
-#include "src/gpu/GrSurfaceContext.h"
-#include "src/gpu/GrSurfaceContextPriv.h"
 #include "src/gpu/GrSurfacePriv.h"
+#include "src/gpu/GrTextureContext.h"
 #include "src/gpu/GrTexturePriv.h"
 #include "src/gpu/GrTextureRenderTargetProxy.h"
 
-#include "src/core/SkMathPriv.h"
-#include "src/core/SkMipMap.h"
-
 #ifdef SK_DEBUG
 #include "include/gpu/GrRenderTarget.h"
 #include "src/gpu/GrRenderTargetPriv.h"
@@ -342,18 +340,18 @@
                                            SkBudgeted budgeted,
                                            RectsMustMatch rectsMustMatch) {
     SkASSERT(LazyState::kFully != src->lazyInstantiationState());
-    GrSurfaceDesc dstDesc;
-    dstDesc.fIsProtected = src->isProtected() ? GrProtected::kYes : GrProtected::kNo;
-    dstDesc.fConfig = src->config();
+    GrProtected isProtected = src->isProtected() ? GrProtected::kYes : GrProtected::kNo;
+    int width;
+    int height;
 
     SkIPoint dstPoint;
     if (rectsMustMatch == RectsMustMatch::kYes) {
-        dstDesc.fWidth = src->width();
-        dstDesc.fHeight = src->height();
+        width = src->width();
+        height = src->height();
         dstPoint = {srcRect.fLeft, srcRect.fTop};
     } else {
-        dstDesc.fWidth = srcRect.width();
-        dstDesc.fHeight = srcRect.height();
+        width = srcRect.width();
+        height = srcRect.height();
         dstPoint = {0, 0};
     }
 
@@ -362,9 +360,9 @@
     }
     auto colorType = GrPixelConfigToColorType(src->config());
     if (src->backendFormat().textureType() != GrTextureType::kExternal) {
-        sk_sp<GrSurfaceContext> dstContext(context->priv().makeDeferredSurfaceContext(
-                src->backendFormat().makeTexture2D(), dstDesc, src->origin(), mipMapped, fit,
-                budgeted, colorType, kUnknown_SkAlphaType));
+        sk_sp<GrTextureContext> dstContext(context->priv().makeDeferredTextureContext(
+                fit, width, height, colorType, kUnknown_SkAlphaType, nullptr, mipMapped,
+                src->origin(), budgeted, isProtected));
         if (!dstContext) {
             return nullptr;
         }
@@ -374,8 +372,8 @@
     }
     if (src->asTextureProxy()) {
         sk_sp<GrRenderTargetContext> dstContext = context->priv().makeDeferredRenderTargetContext(
-                fit, dstDesc.fWidth, dstDesc.fHeight, colorType, nullptr, 1, mipMapped,
-                src->origin(), nullptr, budgeted);
+                fit, width, height, colorType, nullptr, 1, mipMapped, src->origin(), nullptr,
+                budgeted);
 
         if (dstContext && dstContext->blitTexture(src->asTextureProxy(), srcRect, dstPoint)) {
             return dstContext->asTextureProxyRef();
diff --git a/src/image/SkSurface_Gpu.cpp b/src/image/SkSurface_Gpu.cpp
index 0bf78cf..6bc6d3d 100644
--- a/src/image/SkSurface_Gpu.cpp
+++ b/src/image/SkSurface_Gpu.cpp
@@ -363,37 +363,25 @@
     }
 
     GrColorType grColorType = SkColorTypeToGrColorType(c.colorType());
-    GrPixelConfig config = caps->getConfigFromBackendFormat(c.backendFormat(), grColorType);
-    if (config == kUnknown_GrPixelConfig) {
-        return nullptr;
-    }
 
-    GrSurfaceDesc desc;
-    desc.fFlags = kRenderTarget_GrSurfaceFlag;
-    desc.fWidth = c.width();
-    desc.fHeight = c.height();
-    desc.fIsProtected = c.isProtected();
-    desc.fConfig = config;
-    desc.fSampleCnt = c.sampleCount();
-
-    sk_sp<GrSurfaceContext> sc(
-            context->priv().makeDeferredSurfaceContext(c.backendFormat(),
-                                                       desc,
-                                                       c.origin(),
-                                                       GrMipMapped(c.isMipMapped()),
-                                                       SkBackingFit::kExact,
-                                                       budgeted,
-                                                       grColorType,
-                                                       kPremul_SkAlphaType,
-                                                       c.refColorSpace(),
-                                                       &c.surfaceProps()));
-    if (!sc || !sc->asRenderTargetContext()) {
+    sk_sp<GrRenderTargetContext> rtc(context->priv().makeDeferredRenderTargetContext(
+            SkBackingFit::kExact,
+            c.width(),
+            c.height(),
+            grColorType,
+            c.refColorSpace(),
+            c.sampleCount(),
+            GrMipMapped(c.isMipMapped()),
+            c.origin(),
+            &c.surfaceProps(),
+            budgeted,
+            c.isProtected()));
+    if (!rtc) {
         return nullptr;
     }
 
     // CONTEXT TODO: remove this use of 'backdoor' to create an SkGpuDevice
-    sk_sp<SkGpuDevice> device(SkGpuDevice::Make(context->priv().backdoor(),
-                                                sk_ref_sp(sc->asRenderTargetContext()),
+    sk_sp<SkGpuDevice> device(SkGpuDevice::Make(context->priv().backdoor(), std::move(rtc),
                                                 c.width(), c.height(),
                                                 SkGpuDevice::kClear_InitContents));
     if (!device) {
diff --git a/tests/GrSurfaceTest.cpp b/tests/GrSurfaceTest.cpp
index e16eb89..869ed75 100644
--- a/tests/GrSurfaceTest.cpp
+++ b/tests/GrSurfaceTest.cpp
@@ -280,14 +280,19 @@
                         }
 
                         GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
-                        const GrBackendFormat format =
-                                caps->getBackendFormatFromColorType(colorType);
 
                         // Try creating the texture as a deferred proxy.
                         for (int i = 0; i < 2; ++i) {
-                            auto surfCtx = context->priv().makeDeferredSurfaceContext(
-                                    format, desc, origin, GrMipMapped::kNo, fit, SkBudgeted::kYes,
-                                    colorType, kPremul_SkAlphaType);
+                            sk_sp<GrSurfaceContext> surfCtx;
+                            if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
+                                surfCtx = context->priv().makeDeferredRenderTargetContext(
+                                        fit, desc.fWidth, desc.fHeight, colorType, nullptr,
+                                        desc.fSampleCnt, GrMipMapped::kNo, origin, nullptr);
+                            } else {
+                                surfCtx = context->priv().makeDeferredTextureContext(
+                                        fit, desc.fWidth, desc.fHeight, colorType,
+                                        kUnknown_SkAlphaType, nullptr, GrMipMapped::kNo, origin);
+                            }
                             if (!surfCtx) {
                                 continue;
                             }
diff --git a/tests/SRGBReadWritePixelsTest.cpp b/tests/SRGBReadWritePixelsTest.cpp
index 765805e..d78b79d 100644
--- a/tests/SRGBReadWritePixelsTest.cpp
+++ b/tests/SRGBReadWritePixelsTest.cpp
@@ -10,6 +10,7 @@
 #include "include/gpu/GrContext.h"
 #include "src/gpu/GrCaps.h"
 #include "src/gpu/GrContextPriv.h"
+#include "src/gpu/GrRenderTargetContext.h"
 #include "src/gpu/GrSurfaceContext.h"
 #include "src/gpu/SkGr.h"
 #include "tests/Test.h"
@@ -187,23 +188,14 @@
 
 static sk_sp<GrSurfaceContext> make_surface_context(Encoding contextEncoding, GrContext* context,
                                                     skiatest::Reporter* reporter) {
-    GrSurfaceDesc desc;
-    desc.fFlags = kRenderTarget_GrSurfaceFlag;
-    desc.fWidth = kW;
-    desc.fHeight = kH;
-    desc.fConfig = kRGBA_8888_GrPixelConfig;
-
-    GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
-    const GrBackendFormat format = context->priv().caps()->getBackendFormatFromColorType(colorType);
-
-    auto surfaceContext = context->priv().makeDeferredSurfaceContext(
-            format, desc, kBottomLeft_GrSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact,
-            SkBudgeted::kNo, colorType, kPremul_SkAlphaType,
-            encoding_as_color_space(contextEncoding));
+    auto surfaceContext = context->priv().makeDeferredRenderTargetContext(
+            SkBackingFit::kExact, kW, kH, GrColorType::kRGBA_8888,
+            encoding_as_color_space(contextEncoding), 1, GrMipMapped::kNo,
+            kBottomLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kNo, GrProtected::kNo);
     if (!surfaceContext) {
         ERRORF(reporter, "Could not create %s surface context.", encoding_as_str(contextEncoding));
     }
-    return surfaceContext;
+    return std::move(surfaceContext);
 }
 
 static void test_write_read(Encoding contextEncoding, Encoding writeEncoding, Encoding readEncoding,