Reland "Remove code to push pixmaps to backend textures from GrGpu classes"

This is a reland of 45889d12696f8e0af2796a6bbd3938b6d2614eca

Original change's description:
> Remove code to push pixmaps to backend textures from GrGpu classes
>
> Replace GrGpu::updateBackendTexture with narrower method that clears
> a backend texture.
>
> Creation of data for a solid color compressed texture is lifted up to
> GrDirectContext and goes through updateCompressedBackendTexture.
>
> Bug: skia:11786
> Change-Id: I1d617623df5e65686f30e57c361a64f78d77f7bd
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/392836
> Reviewed-by: Jim Van Verth <jvanverth@google.com>
> Reviewed-by: Greg Daniel <egdaniel@google.com>
> Commit-Queue: Brian Salomon <bsalomon@google.com>

Bug: skia:11786
Change-Id: Ibf85794a33e68acb17f0fb704f6815cd6460b3f2
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/396098
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/GrDirectContext.cpp b/src/gpu/GrDirectContext.cpp
index 69f60c5..4b0a867 100644
--- a/src/gpu/GrDirectContext.cpp
+++ b/src/gpu/GrDirectContext.cpp
@@ -10,7 +10,9 @@
 
 #include "include/core/SkTraceMemoryDump.h"
 #include "include/gpu/GrContextThreadSafeProxy.h"
+#include "src/core/SkAutoMalloc.h"
 #include "src/core/SkTaskGroup.h"
+#include "src/gpu/GrBackendUtils.h"
 #include "src/gpu/GrClientMappedBufferManager.h"
 #include "src/gpu/GrContextThreadSafeProxyPriv.h"
 #include "src/gpu/GrDirectContextPriv.h"
@@ -472,26 +474,24 @@
     return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
 }
 
-static GrBackendTexture create_and_update_backend_texture(
-        GrDirectContext* dContext,
-        SkISize dimensions,
-        const GrBackendFormat& backendFormat,
-        GrMipmapped mipMapped,
-        GrRenderable renderable,
-        GrProtected isProtected,
-        sk_sp<GrRefCntedCallback> finishedCallback,
-        const GrGpu::BackendTextureData* data) {
+static GrBackendTexture create_and_clear_backend_texture(GrDirectContext* dContext,
+                                                         SkISize dimensions,
+                                                         const GrBackendFormat& backendFormat,
+                                                         GrMipmapped mipMapped,
+                                                         GrRenderable renderable,
+                                                         GrProtected isProtected,
+                                                         sk_sp<GrRefCntedCallback> finishedCallback,
+                                                         std::array<float, 4> color) {
     GrGpu* gpu = dContext->priv().getGpu();
-    SkASSERT(data->type() == GrGpu::BackendTextureData::Type::kColor);
     GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
                                                        mipMapped, isProtected);
     if (!beTex.isValid()) {
         return {};
     }
 
-    if (!dContext->priv().getGpu()->updateBackendTexture(beTex,
-                                                         std::move(finishedCallback),
-                                                         data)) {
+    if (!dContext->priv().getGpu()->clearBackendTexture(beTex,
+                                                        std::move(finishedCallback),
+                                                        color)) {
         dContext->deleteBackendTexture(beTex);
         return {};
     }
@@ -555,10 +555,14 @@
         return {};
     }
 
-    GrGpu::BackendTextureData data(color);
-    return create_and_update_backend_texture(this, {width, height},
-                                             backendFormat, mipMapped, renderable, isProtected,
-                                             std::move(finishedCallback), &data);
+    return create_and_clear_backend_texture(this,
+                                            {width, height},
+                                            backendFormat,
+                                            mipMapped,
+                                            renderable,
+                                            isProtected,
+                                            std::move(finishedCallback),
+                                            color.array());
 }
 
 GrBackendTexture GrDirectContext::createBackendTexture(int width, int height,
@@ -583,10 +587,14 @@
     GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
     SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
 
-    GrGpu::BackendTextureData data(swizzledColor);
-    return create_and_update_backend_texture(this, {width, height}, format,
-                                             mipMapped, renderable, isProtected,
-                                             std::move(finishedCallback), &data);
+    return create_and_clear_backend_texture(this,
+                                            {width, height},
+                                            format,
+                                            mipMapped,
+                                            renderable,
+                                            isProtected,
+                                            std::move(finishedCallback),
+                                            swizzledColor.array());
 }
 
 GrBackendTexture GrDirectContext::createBackendTexture(const SkPixmap srcData[],
@@ -647,8 +655,7 @@
         return false;
     }
 
-    GrGpu::BackendTextureData data(color);
-    return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
+    return fGpu->clearBackendTexture(backendTexture, std::move(finishedCallback), color.array());
 }
 
 bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture,
@@ -670,9 +677,11 @@
     }
 
     GrSwizzle swizzle = this->caps()->getWriteSwizzle(format, grColorType);
-    GrGpu::BackendTextureData data(swizzle.applyTo(color));
+    SkColor4f swizzledColor = swizzle.applyTo(color);
 
-    return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
+    return fGpu->clearBackendTexture(backendTexture,
+                                     std::move(finishedCallback),
+                                     swizzledColor.array());
 }
 
 bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture,
@@ -717,7 +726,8 @@
         GrMipmapped mipMapped,
         GrProtected isProtected,
         sk_sp<GrRefCntedCallback> finishedCallback,
-        const GrGpu::BackendTextureData* data) {
+        const void* data,
+        size_t size) {
     GrGpu* gpu = dContext->priv().getGpu();
 
     GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat,
@@ -727,20 +737,21 @@
     }
 
     if (!dContext->priv().getGpu()->updateCompressedBackendTexture(
-                beTex, std::move(finishedCallback), data)) {
+                beTex, std::move(finishedCallback), data, size)) {
         dContext->deleteBackendTexture(beTex);
         return {};
     }
     return beTex;
 }
 
-GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
-                                                             const GrBackendFormat& backendFormat,
-                                                             const SkColor4f& color,
-                                                             GrMipmapped mipMapped,
-                                                             GrProtected isProtected,
-                                                             GrGpuFinishedProc finishedProc,
-                                                             GrGpuFinishedContext finishedContext) {
+GrBackendTexture GrDirectContext::createCompressedBackendTexture(
+        int width, int height,
+        const GrBackendFormat& backendFormat,
+        const SkColor4f& color,
+        GrMipmapped mipmapped,
+        GrProtected isProtected,
+        GrGpuFinishedProc finishedProc,
+        GrGpuFinishedContext finishedContext) {
     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
     auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
 
@@ -748,19 +759,35 @@
         return {};
     }
 
-    GrGpu::BackendTextureData data(color);
-    return create_and_update_compressed_backend_texture(this, {width, height},
-                                                        backendFormat, mipMapped, isProtected,
-                                                        std::move(finishedCallback), &data);
+    SkImage::CompressionType compression = GrBackendFormatToCompressionType(backendFormat);
+    if (compression == SkImage::CompressionType::kNone) {
+        return {};
+    }
+
+    size_t size = SkCompressedDataSize(compression,
+                                       {width, height},
+                                       nullptr,
+                                       mipmapped == GrMipmapped::kYes);
+    auto storage = std::make_unique<char[]>(size);
+    GrFillInCompressedData(compression, {width, height}, mipmapped, storage.get(), color);
+    return create_and_update_compressed_backend_texture(this,
+                                                        {width, height},
+                                                        backendFormat,
+                                                        mipmapped,
+                                                        isProtected,
+                                                        std::move(finishedCallback),
+                                                        storage.get(),
+                                                        size);
 }
 
-GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
-                                                             SkImage::CompressionType compression,
-                                                             const SkColor4f& color,
-                                                             GrMipmapped mipMapped,
-                                                             GrProtected isProtected,
-                                                             GrGpuFinishedProc finishedProc,
-                                                             GrGpuFinishedContext finishedContext) {
+GrBackendTexture GrDirectContext::createCompressedBackendTexture(
+        int width, int height,
+        SkImage::CompressionType compression,
+        const SkColor4f& color,
+        GrMipmapped mipMapped,
+        GrProtected isProtected,
+        GrGpuFinishedProc finishedProc,
+        GrGpuFinishedContext finishedContext) {
     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
     GrBackendFormat format = this->compressedBackendFormat(compression);
     return this->createCompressedBackendTexture(width, height, format, color,
@@ -768,14 +795,15 @@
                                                 finishedContext);
 }
 
-GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
-                                                             const GrBackendFormat& backendFormat,
-                                                             const void* compressedData,
-                                                             size_t dataSize,
-                                                             GrMipmapped mipMapped,
-                                                             GrProtected isProtected,
-                                                             GrGpuFinishedProc finishedProc,
-                                                             GrGpuFinishedContext finishedContext) {
+GrBackendTexture GrDirectContext::createCompressedBackendTexture(
+        int width, int height,
+        const GrBackendFormat& backendFormat,
+        const void* compressedData,
+        size_t dataSize,
+        GrMipmapped mipMapped,
+        GrProtected isProtected,
+        GrGpuFinishedProc finishedProc,
+        GrGpuFinishedContext finishedContext) {
     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
     auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
 
@@ -783,19 +811,24 @@
         return {};
     }
 
-    GrGpu::BackendTextureData data(compressedData, dataSize);
-    return create_and_update_compressed_backend_texture(this, {width, height},
-                                                        backendFormat, mipMapped, isProtected,
-                                                        std::move(finishedCallback), &data);
+    return create_and_update_compressed_backend_texture(this,
+                                                        {width, height},
+                                                        backendFormat,
+                                                        mipMapped,
+                                                        isProtected,
+                                                        std::move(finishedCallback),
+                                                        compressedData,
+                                                        dataSize);
 }
 
-GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
-                                                             SkImage::CompressionType compression,
-                                                             const void* data, size_t dataSize,
-                                                             GrMipmapped mipMapped,
-                                                             GrProtected isProtected,
-                                                             GrGpuFinishedProc finishedProc,
-                                                             GrGpuFinishedContext finishedContext) {
+GrBackendTexture GrDirectContext::createCompressedBackendTexture(
+        int width, int height,
+        SkImage::CompressionType compression,
+        const void* data, size_t dataSize,
+        GrMipmapped mipMapped,
+        GrProtected isProtected,
+        GrGpuFinishedProc finishedProc,
+        GrGpuFinishedContext finishedContext) {
     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
     GrBackendFormat format = this->compressedBackendFormat(compression);
     return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
@@ -812,8 +845,25 @@
         return false;
     }
 
-    GrGpu::BackendTextureData data(color);
-    return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
+    SkImage::CompressionType compression =
+            GrBackendFormatToCompressionType(backendTexture.getBackendFormat());
+    if (compression == SkImage::CompressionType::kNone) {
+        return {};
+    }
+    size_t size = SkCompressedDataSize(compression,
+                                       backendTexture.dimensions(),
+                                       nullptr,
+                                       backendTexture.hasMipmaps());
+    SkAutoMalloc storage(size);
+    GrFillInCompressedData(compression,
+                           backendTexture.dimensions(),
+                           backendTexture.mipmapped(),
+                           static_cast<char*>(storage.get()),
+                           color);
+    return fGpu->updateCompressedBackendTexture(backendTexture,
+                                                std::move(finishedCallback),
+                                                storage.get(),
+                                                size);
 }
 
 bool GrDirectContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
@@ -831,9 +881,10 @@
         return false;
     }
 
-    GrGpu::BackendTextureData data(compressedData, dataSize);
-
-    return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
+    return fGpu->updateCompressedBackendTexture(backendTexture,
+                                                std::move(finishedCallback),
+                                                compressedData,
+                                                dataSize);
 }
 
 //////////////////////////////////////////////////////////////////////////////