Revert "Revert "Remove GrCaps::isFormatTexturableAndUploadable""

This reverts commit 8176508cc63ad05a173e6478b6a2aaf38d3f135d.

Replaces incorrect check for Read pixels support with Write check.

Change-Id: Idc80aaaa41ba35014339df450bb8b583fb6dab51
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/280399
Auto-Submit: Brian Salomon <bsalomon@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
diff --git a/src/gpu/GrCaps.cpp b/src/gpu/GrCaps.cpp
index 7e210e0..a994862 100644
--- a/src/gpu/GrCaps.cpp
+++ b/src/gpu/GrCaps.cpp
@@ -352,19 +352,25 @@
     return read;
 }
 
-GrBackendFormat GrCaps::getDefaultBackendFormat(GrColorType grColorType,
+GrBackendFormat GrCaps::getDefaultBackendFormat(GrColorType colorType,
                                                 GrRenderable renderable) const {
-    GrBackendFormat format = this->onGetDefaultBackendFormat(grColorType, renderable);
-    if (!this->isFormatTexturableAndUploadable(grColorType, format)) {
+    auto format = this->onGetDefaultBackendFormat(colorType);
+    if (!this->isFormatTexturable(format)) {
         return {};
     }
-
-    if (renderable == GrRenderable::kYes) {
-        if (!this->isFormatAsColorTypeRenderable(grColorType, format)) {
-            return {};
-        }
+    if (!this->areColorTypeAndFormatCompatible(colorType, format)) {
+        return {};
     }
-
+    // Currently we require that it be possible to write pixels into the "default" format. Perhaps,
+    // that could be a separate requirement from the caller. It seems less necessary if
+    // renderability was requested.
+    if (this->supportedWritePixelsColorType(colorType, format, colorType).fColorType ==
+        GrColorType::kUnknown) {
+        return {};
+    }
+    if (renderable == GrRenderable::kYes &&
+        !this->isFormatAsColorTypeRenderable(colorType, format)) {
+        return {};
+    }
     return format;
 }
-
diff --git a/src/gpu/GrCaps.h b/src/gpu/GrCaps.h
index d98890d..b22010a 100644
--- a/src/gpu/GrCaps.h
+++ b/src/gpu/GrCaps.h
@@ -186,9 +186,6 @@
         return this->compressionType(format) != SkImage::CompressionType::kNone;
     }
 
-    // TODO: Once we use the supportWritePixels call for uploads, we can remove this function and
-    // instead only have the version that takes a GrBackendFormat.
-    virtual bool isFormatTexturableAndUploadable(GrColorType, const GrBackendFormat&) const = 0;
     // Can a texture be made with the GrBackendFormat, and then be bound and sampled in a shader.
     virtual bool isFormatTexturable(const GrBackendFormat&) const = 0;
 
@@ -196,9 +193,7 @@
     virtual bool isFormatCopyable(const GrBackendFormat&) const = 0;
 
     // Returns the maximum supported sample count for a format. 0 means the format is not renderable
-    // 1 means the format is renderable but doesn't support MSAA. This call only refers to the
-    // format itself. A caller should also confirm if the format is renderable with a given
-    // GrColorType by calling isFormatRenderable.
+    // 1 means the format is renderable but doesn't support MSAA.
     virtual int maxRenderTargetSampleCount(const GrBackendFormat&) const = 0;
 
     // Returns the number of samples to use when performing internal draws to the given config with
@@ -540,7 +535,7 @@
     virtual bool onSurfaceSupportsWritePixels(const GrSurface*) const = 0;
     virtual bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
                                   const SkIRect& srcRect, const SkIPoint& dstPoint) const = 0;
-    virtual GrBackendFormat onGetDefaultBackendFormat(GrColorType, GrRenderable) const = 0;
+    virtual GrBackendFormat onGetDefaultBackendFormat(GrColorType) const = 0;
 
     // Backends should implement this if they have any extra requirements for use of window
     // rectangles for a specific GrBackendRenderTarget outside of basic support.
diff --git a/src/gpu/GrContext_Base.cpp b/src/gpu/GrContext_Base.cpp
index 5ac2b08..034d023 100644
--- a/src/gpu/GrContext_Base.cpp
+++ b/src/gpu/GrContext_Base.cpp
@@ -52,7 +52,6 @@
         return GrBackendFormat();
     }
 
-    SkASSERT(caps->isFormatTexturableAndUploadable(grColorType, format));
     SkASSERT(renderable == GrRenderable::kNo ||
              caps->isFormatAsColorTypeRenderable(grColorType, format));
 
diff --git a/src/gpu/GrGpu.cpp b/src/gpu/GrGpu.cpp
index 6ae5308..afef05a4 100644
--- a/src/gpu/GrGpu.cpp
+++ b/src/gpu/GrGpu.cpp
@@ -446,8 +446,6 @@
     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
     SkASSERT(surface);
     SkASSERT(!surface->framebufferOnly());
-    SkASSERT(this->caps()->isFormatTexturableAndUploadable(surfaceColorType,
-                                                           surface->backendFormat()));
 
     if (surface->readOnly()) {
         return false;
@@ -489,8 +487,6 @@
     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
     SkASSERT(texture);
     SkASSERT(transferBuffer);
-    SkASSERT(this->caps()->isFormatTexturableAndUploadable(textureColorType,
-                                                           texture->backendFormat()));
 
     if (texture->readOnly()) {
         return false;
diff --git a/src/gpu/d3d/GrD3DCaps.cpp b/src/gpu/d3d/GrD3DCaps.cpp
index bac1178..07dca85 100644
--- a/src/gpu/d3d/GrD3DCaps.cpp
+++ b/src/gpu/d3d/GrD3DCaps.cpp
@@ -719,18 +719,6 @@
     SkUNREACHABLE;
 }
 
-bool GrD3DCaps::isFormatTexturableAndUploadable(GrColorType ct,
-                                                const GrBackendFormat& format) const {
-    DXGI_FORMAT dxgiFormat;
-    if (!format.asDxgiFormat(&dxgiFormat)) {
-        return false;
-    }
-
-    uint32_t ctFlags = this->getFormatInfo(dxgiFormat).colorTypeFlags(ct);
-    return this->isFormatTexturable(dxgiFormat) &&
-        SkToBool(ctFlags & ColorTypeInfo::kUploadData_Flag);
-}
-
 bool GrD3DCaps::isFormatTexturable(const GrBackendFormat& format) const {
     DXGI_FORMAT dxgiFormat;
     if (!format.asDxgiFormat(&dxgiFormat)) {
@@ -923,11 +911,10 @@
     SkUNREACHABLE;
 }
 
-GrBackendFormat GrD3DCaps::onGetDefaultBackendFormat(GrColorType ct,
-                                                     GrRenderable renderable) const {
+GrBackendFormat GrD3DCaps::onGetDefaultBackendFormat(GrColorType ct) const {
     DXGI_FORMAT format = this->getFormatFromColorType(ct);
     if (format == DXGI_FORMAT_UNKNOWN) {
-        return GrBackendFormat();
+        return {};
     }
     return GrBackendFormat::MakeDxgi(format);
 }
diff --git a/src/gpu/d3d/GrD3DCaps.h b/src/gpu/d3d/GrD3DCaps.h
index 1f5aa10..cb3d1fd 100644
--- a/src/gpu/d3d/GrD3DCaps.h
+++ b/src/gpu/d3d/GrD3DCaps.h
@@ -27,7 +27,6 @@
     bool isFormatSRGB(const GrBackendFormat&) const override;
     SkImage::CompressionType compressionType(const GrBackendFormat&) const override;
 
-    bool isFormatTexturableAndUploadable(GrColorType, const GrBackendFormat&) const override;
     bool isFormatTexturable(const GrBackendFormat&) const override;
     bool isFormatTexturable(DXGI_FORMAT) const;
 
@@ -101,7 +100,7 @@
     bool onSurfaceSupportsWritePixels(const GrSurface*) const override;
     bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override;
-    GrBackendFormat onGetDefaultBackendFormat(GrColorType, GrRenderable) const override;
+    GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override;
 
     bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
 
diff --git a/src/gpu/dawn/GrDawnCaps.cpp b/src/gpu/dawn/GrDawnCaps.cpp
index c93f7eb..9a35437 100644
--- a/src/gpu/dawn/GrDawnCaps.cpp
+++ b/src/gpu/dawn/GrDawnCaps.cpp
@@ -73,25 +73,6 @@
     return GrSwizzle::RGBA();
 }
 
-bool GrDawnCaps::isFormatTexturableAndUploadable(GrColorType ct,
-                                                 const GrBackendFormat& format) const {
-    wgpu::TextureFormat dawnFormat;
-    if (!format.asDawnFormat(&dawnFormat)) {
-        return false;
-    }
-    switch (ct) {
-        case GrColorType::kAlpha_8:
-            return wgpu::TextureFormat::R8Unorm == dawnFormat;
-        case GrColorType::kRGBA_8888:
-        case GrColorType::kRGB_888x:
-        case GrColorType::kBGRA_8888:
-            return wgpu::TextureFormat::RGBA8Unorm == dawnFormat ||
-                   wgpu::TextureFormat::BGRA8Unorm == dawnFormat;
-        default:
-            return false;
-    }
-}
-
 bool GrDawnCaps::isFormatRenderable(const GrBackendFormat& format,
                                     int sampleCount) const {
     wgpu::TextureFormat dawnFormat;
@@ -128,11 +109,10 @@
     return format.isValid() ? 1 : 0;
 }
 
-GrBackendFormat GrDawnCaps::onGetDefaultBackendFormat(GrColorType ct,
-                                                      GrRenderable renderable) const {
+GrBackendFormat GrDawnCaps::onGetDefaultBackendFormat(GrColorType ct) const {
     wgpu::TextureFormat format;
     if (!GrColorTypeToDawnFormat(ct, &format)) {
-        return GrBackendFormat();
+        return {};
     }
     return GrBackendFormat::MakeDawn(format);
 }
diff --git a/src/gpu/dawn/GrDawnCaps.h b/src/gpu/dawn/GrDawnCaps.h
index 5ed6512..bd0e592 100644
--- a/src/gpu/dawn/GrDawnCaps.h
+++ b/src/gpu/dawn/GrDawnCaps.h
@@ -20,7 +20,6 @@
     bool isFormatSRGB(const GrBackendFormat&) const override;
     SkImage::CompressionType compressionType(const GrBackendFormat&) const override;
 
-    bool isFormatTexturableAndUploadable(GrColorType, const GrBackendFormat& format) const override;
     bool isFormatRenderable(const GrBackendFormat& format,
                             int sampleCount = 1) const override;
     bool isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
@@ -73,7 +72,7 @@
         const SkIRect& srcRect, const SkIPoint& dstPoint) const override {
         return true;
     }
-    GrBackendFormat onGetDefaultBackendFormat(GrColorType, GrRenderable) const override;
+    GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override;
 
     bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
 
diff --git a/src/gpu/gl/GrGLCaps.cpp b/src/gpu/gl/GrGLCaps.cpp
index 8305d75..cee6edc 100644
--- a/src/gpu/gl/GrGLCaps.cpp
+++ b/src/gpu/gl/GrGLCaps.cpp
@@ -4111,15 +4111,6 @@
     SkUNREACHABLE;
 }
 
-bool GrGLCaps::isFormatTexturableAndUploadable(GrColorType ct,
-                                               const GrBackendFormat& format) const {
-    auto glFormat = format.asGLFormat();
-    const FormatInfo& info = this->getFormatInfo(glFormat);
-
-    return this->isFormatTexturable(glFormat) &&
-           SkToBool(info.colorTypeFlags(ct) & ColorTypeInfo::kUploadData_Flag);
-}
-
 bool GrGLCaps::isFormatTexturable(const GrBackendFormat& format) const {
     return this->isFormatTexturable(format.asGLFormat());
 }
@@ -4293,12 +4284,10 @@
     SkUNREACHABLE;
 }
 
-GrBackendFormat GrGLCaps::onGetDefaultBackendFormat(GrColorType ct,
-                                                    GrRenderable renderable) const {
-    // TODO: make use of renderable.
+GrBackendFormat GrGLCaps::onGetDefaultBackendFormat(GrColorType ct) const {
     auto format = this->getFormatFromColorType(ct);
     if (format == GrGLFormat::kUnknown) {
-        return GrBackendFormat();
+        return {};
     }
     return GrBackendFormat::MakeGL(GrGLFormatToEnum(format), GR_GL_TEXTURE_2D);
 }
diff --git a/src/gpu/gl/GrGLCaps.h b/src/gpu/gl/GrGLCaps.h
index f5ee8ce..5f62b07 100644
--- a/src/gpu/gl/GrGLCaps.h
+++ b/src/gpu/gl/GrGLCaps.h
@@ -116,7 +116,6 @@
     bool isFormatSRGB(const GrBackendFormat&) const override;
     SkImage::CompressionType compressionType(const GrBackendFormat&) const override;
 
-    bool isFormatTexturableAndUploadable(GrColorType, const GrBackendFormat&) const override;
     bool isFormatTexturable(const GrBackendFormat&) const override;
     bool isFormatTexturable(GrGLFormat) const;
 
@@ -501,7 +500,7 @@
     bool onSurfaceSupportsWritePixels(const GrSurface*) const override;
     bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override;
-    GrBackendFormat onGetDefaultBackendFormat(GrColorType, GrRenderable) const override;
+    GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override;
     bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
 
     SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&,
diff --git a/src/gpu/mock/GrMockCaps.h b/src/gpu/mock/GrMockCaps.h
index d76d04d..6335757 100644
--- a/src/gpu/mock/GrMockCaps.h
+++ b/src/gpu/mock/GrMockCaps.h
@@ -53,11 +53,6 @@
         return format.asMockCompressionType();
     }
 
-    bool isFormatTexturableAndUploadable(GrColorType,
-                                         const GrBackendFormat& format) const override {
-        return this->isFormatTexturable(format);
-    }
-
     bool isFormatTexturable(const GrBackendFormat& format) const override {
         SkImage::CompressionType compression = format.asMockCompressionType();
         if (compression != SkImage::CompressionType::kNone) {
@@ -190,7 +185,7 @@
                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override {
         return true;
     }
-    GrBackendFormat onGetDefaultBackendFormat(GrColorType ct, GrRenderable) const override {
+    GrBackendFormat onGetDefaultBackendFormat(GrColorType ct) const override {
         return GrBackendFormat::MakeMock(ct, SkImage::CompressionType::kNone);
     }
 
diff --git a/src/gpu/mtl/GrMtlCaps.h b/src/gpu/mtl/GrMtlCaps.h
index 347328a..f4f6de9 100644
--- a/src/gpu/mtl/GrMtlCaps.h
+++ b/src/gpu/mtl/GrMtlCaps.h
@@ -29,7 +29,6 @@
     bool isFormatSRGB(const GrBackendFormat&) const override;
     SkImage::CompressionType compressionType(const GrBackendFormat&) const override;
 
-    bool isFormatTexturableAndUploadable(GrColorType, const GrBackendFormat&) const override;
     bool isFormatTexturable(const GrBackendFormat&) const override;
     bool isFormatTexturable(MTLPixelFormat) const;
 
@@ -111,7 +110,7 @@
     bool onSurfaceSupportsWritePixels(const GrSurface*) const override;
     bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override;
-    GrBackendFormat onGetDefaultBackendFormat(GrColorType, GrRenderable) const override;
+    GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override;
     bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
 
     SupportedRead onSupportedReadPixelsColorType(GrColorType, const GrBackendFormat&,
diff --git a/src/gpu/mtl/GrMtlCaps.mm b/src/gpu/mtl/GrMtlCaps.mm
index 99a3ff2..e146d7f 100644
--- a/src/gpu/mtl/GrMtlCaps.mm
+++ b/src/gpu/mtl/GrMtlCaps.mm
@@ -331,15 +331,6 @@
     SkUNREACHABLE;
 }
 
-bool GrMtlCaps::isFormatTexturableAndUploadable(GrColorType ct,
-                                                const GrBackendFormat& format) const {
-    MTLPixelFormat mtlFormat = GrBackendFormatAsMTLPixelFormat(format);
-
-    uint32_t ctFlags = this->getFormatInfo(mtlFormat).colorTypeFlags(ct);
-    return this->isFormatTexturable(mtlFormat) &&
-           SkToBool(ctFlags & ColorTypeInfo::kUploadData_Flag);
-}
-
 bool GrMtlCaps::isFormatTexturable(const GrBackendFormat& format) const {
     MTLPixelFormat mtlFormat = GrBackendFormatAsMTLPixelFormat(format);
     return this->isFormatTexturable(mtlFormat);
@@ -909,11 +900,10 @@
     }
 }
 
-GrBackendFormat GrMtlCaps::onGetDefaultBackendFormat(GrColorType ct,
-                                                     GrRenderable renderable) const {
+GrBackendFormat GrMtlCaps::onGetDefaultBackendFormat(GrColorType ct) const {
     MTLPixelFormat format = this->getFormatFromColorType(ct);
     if (!format) {
-        return GrBackendFormat();
+        return {};
     }
     return GrBackendFormat::MakeMtl(format);
 }
diff --git a/src/gpu/mtl/GrMtlGpu.mm b/src/gpu/mtl/GrMtlGpu.mm
index 913a3af..c7b5e5c 100644
--- a/src/gpu/mtl/GrMtlGpu.mm
+++ b/src/gpu/mtl/GrMtlGpu.mm
@@ -262,9 +262,9 @@
     if (width == 0 || height == 0) {
         return false;
     }
-    if (!this->mtlCaps().isFormatTexturableAndUploadable(dataColorType, tex->backendFormat())) {
-        return false;
-    }
+
+    SkASSERT(this->mtlCaps().surfaceSupportsWritePixels(tex));
+    SkASSERT(this->mtlCaps().areColorTypeAndFormatCompatible(dataColorType, tex->backendFormat()));
 
     id<MTLTexture> mtlTexture = tex->mtlTexture();
     SkASSERT(mtlTexture);
diff --git a/src/gpu/vk/GrVkCaps.cpp b/src/gpu/vk/GrVkCaps.cpp
index cdac50f..55ddbad 100644
--- a/src/gpu/vk/GrVkCaps.cpp
+++ b/src/gpu/vk/GrVkCaps.cpp
@@ -1312,18 +1312,6 @@
     SkUNREACHABLE;
 }
 
-bool GrVkCaps::isFormatTexturableAndUploadable(GrColorType ct,
-                                               const GrBackendFormat& format) const {
-    VkFormat vkFormat;
-    if (!format.asVkFormat(&vkFormat)) {
-        return false;
-    }
-
-    uint32_t ctFlags = this->getFormatInfo(vkFormat).colorTypeFlags(ct);
-    return this->isVkFormatTexturable(vkFormat) &&
-           SkToBool(ctFlags & ColorTypeInfo::kUploadData_Flag);
-}
-
 bool GrVkCaps::isFormatTexturable(const GrBackendFormat& format) const {
     VkFormat vkFormat;
     if (!format.asVkFormat(&vkFormat)) {
@@ -1562,11 +1550,10 @@
     SkUNREACHABLE;
 }
 
-GrBackendFormat GrVkCaps::onGetDefaultBackendFormat(GrColorType ct,
-                                                    GrRenderable renderable) const {
+GrBackendFormat GrVkCaps::onGetDefaultBackendFormat(GrColorType ct) const {
     VkFormat format = this->getFormatFromColorType(ct);
     if (format == VK_FORMAT_UNDEFINED) {
-        return GrBackendFormat();
+        return {};
     }
     return GrBackendFormat::MakeVk(format);
 }
diff --git a/src/gpu/vk/GrVkCaps.h b/src/gpu/vk/GrVkCaps.h
index ac7a764..cf009f5 100644
--- a/src/gpu/vk/GrVkCaps.h
+++ b/src/gpu/vk/GrVkCaps.h
@@ -35,7 +35,6 @@
     bool isFormatSRGB(const GrBackendFormat&) const override;
     SkImage::CompressionType compressionType(const GrBackendFormat&) const override;
 
-    bool isFormatTexturableAndUploadable(GrColorType, const GrBackendFormat&) const override;
     bool isFormatTexturable(const GrBackendFormat&) const override;
     bool isVkFormatTexturable(VkFormat) const;
 
@@ -224,7 +223,7 @@
     bool onSurfaceSupportsWritePixels(const GrSurface*) const override;
     bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
                           const SkIRect& srcRect, const SkIPoint& dstPoint) const override;
-    GrBackendFormat onGetDefaultBackendFormat(GrColorType, GrRenderable) const override;
+    GrBackendFormat onGetDefaultBackendFormat(GrColorType) const override;
 
     bool onAreColorTypeAndFormatCompatible(GrColorType, const GrBackendFormat&) const override;
 
diff --git a/src/gpu/vk/GrVkGpu.cpp b/src/gpu/vk/GrVkGpu.cpp
index c978589..4a4bf01 100644
--- a/src/gpu/vk/GrVkGpu.cpp
+++ b/src/gpu/vk/GrVkGpu.cpp
@@ -762,9 +762,8 @@
         return false;
     }
 
-    if (!this->vkCaps().isFormatTexturableAndUploadable(dataColorType, tex->backendFormat())) {
-        return false;
-    }
+    SkASSERT(this->vkCaps().surfaceSupportsWritePixels(tex));
+    SkASSERT(this->vkCaps().areColorTypeAndFormatCompatible(dataColorType, tex->backendFormat()));
 
     // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
     // dst RGB texture. Thus we do not upload mip levels for that.
diff --git a/tests/GrSurfaceTest.cpp b/tests/GrSurfaceTest.cpp
index fe8547d..42abc42 100644
--- a/tests/GrSurfaceTest.cpp
+++ b/tests/GrSurfaceTest.cpp
@@ -123,13 +123,7 @@
         // support check is working
         {
             bool isCompressed = caps->isFormatCompressed(combo.fFormat);
-            bool isTexturable;
-            if (isCompressed) {
-                isTexturable = caps->isFormatTexturable(combo.fFormat);
-            } else {
-                isTexturable =
-                        caps->isFormatTexturableAndUploadable(combo.fColorType, combo.fFormat);
-            }
+            bool isTexturable = caps->isFormatTexturable(combo.fFormat);
 
             sk_sp<GrSurface> tex = createTexture(kDims, combo.fColorType, combo.fFormat,
                                                  GrRenderable::kNo, resourceProvider);
@@ -222,7 +216,7 @@
             SkASSERT(combo.fColorType != GrColorType::kUnknown);
             SkASSERT(combo.fFormat.isValid());
 
-            if (!caps->isFormatTexturableAndUploadable(combo.fColorType, combo.fFormat)) {
+            if (!caps->isFormatTexturable(combo.fFormat)) {
                 continue;
             }