Make SkPixmap-variant of createBackendTexture public

Change-Id: I51d916d2410428113e29d2fc374e5f1f7f4010b9
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/244676
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt
index 227db76..6ea0bde 100644
--- a/RELEASE_NOTES.txt
+++ b/RELEASE_NOTES.txt
@@ -113,3 +113,6 @@
     This is intended to help support HDR YUV uses case (e.g., P010 and P016). As such,
     the addition is focused on allowing creation of SkPixmaps and SkImages and not
     SkSurfaces (i.e., who wants to render to render to these?)
+
+  * Added SkPixmap-based createBackendTexture method to GrContext. This allows clients to create
+    backend resources (initialized with texture data) that Skia/Ganesh doesn't know about/track.
diff --git a/gm/asyncrescaleandread.cpp b/gm/asyncrescaleandread.cpp
index a796854..386cb33 100644
--- a/gm/asyncrescaleandread.cpp
+++ b/gm/asyncrescaleandread.cpp
@@ -17,7 +17,6 @@
 #include "src/core/SkConvertPixels.h"
 #include "src/core/SkScopeExit.h"
 #include "src/gpu/GrContextPriv.h"
-#include "src/gpu/GrGpu.h"
 #include "tools/Resources.h"
 #include "tools/ToolUtils.h"
 
@@ -107,12 +106,12 @@
     auto* gr = surface->getCanvas()->getGrContext();
     GrBackendTexture backendTextures[3];
 
-    backendTextures[0] = gr->priv().createBackendTexture(&context.fYData, 1,
-                                                         GrRenderable::kNo, GrProtected::kNo);
-    backendTextures[1] = gr->priv().createBackendTexture(&context.fUData, 1,
-                                                         GrRenderable::kNo, GrProtected::kNo);
-    backendTextures[2] = gr->priv().createBackendTexture(&context.fVData, 1,
-                                                         GrRenderable::kNo, GrProtected::kNo);
+    backendTextures[0] = gr->createBackendTexture(&context.fYData, 1,
+                                                  GrRenderable::kNo, GrProtected::kNo);
+    backendTextures[1] = gr->createBackendTexture(&context.fUData, 1,
+                                                  GrRenderable::kNo, GrProtected::kNo);
+    backendTextures[2] = gr->createBackendTexture(&context.fVData, 1,
+                                                  GrRenderable::kNo, GrProtected::kNo);
     SkYUVAIndex indices[4] = {
         { 0, SkColorChannel::kR},
         { 1, SkColorChannel::kR},
diff --git a/gm/imagefromyuvtextures.cpp b/gm/imagefromyuvtextures.cpp
index 6b98912..68dc7dc 100644
--- a/gm/imagefromyuvtextures.cpp
+++ b/gm/imagefromyuvtextures.cpp
@@ -31,8 +31,6 @@
 #include "include/gpu/GrTypes.h"
 #include "include/private/GrTypesPriv.h"
 #include "include/private/SkTo.h"
-#include "src/gpu/GrContextPriv.h"
-#include "src/gpu/GrGpu.h"
 
 class GrRenderTargetContext;
 
@@ -128,9 +126,8 @@
     void createYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) {
         for (int i = 0; i < 3; ++i) {
             SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes()));
-            yuvTextures[i] = context->priv().createBackendTexture(&fYUVBmps[i].pixmap(), 1,
-                                                                  GrRenderable::kNo,
-                                                                  GrProtected::kNo);
+            yuvTextures[i] = context->createBackendTexture(&fYUVBmps[i].pixmap(), 1,
+                                                           GrRenderable::kNo, GrProtected::kNo);
         }
     }
 
diff --git a/gm/wacky_yuv_formats.cpp b/gm/wacky_yuv_formats.cpp
index 119143e..23c868d 100644
--- a/gm/wacky_yuv_formats.cpp
+++ b/gm/wacky_yuv_formats.cpp
@@ -1081,8 +1081,7 @@
     }
 #endif
 
-    return context->priv().createBackendTexture(&bm.pixmap(), 1,
-                                                GrRenderable::kNo, GrProtected::kNo);
+    return context->createBackendTexture(&bm.pixmap(), 1, GrRenderable::kNo, GrProtected::kNo);
 }
 
 static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h
index 0a1b0a4..2615961 100644
--- a/include/gpu/GrContext.h
+++ b/include/gpu/GrContext.h
@@ -455,6 +455,22 @@
     GrBackendTexture createBackendTexture(const SkSurfaceCharacterization& characterization,
                                           const SkColor4f& color);
 
+    // If possible, create a backend texture initialized with the provided pixmap data. The client
+    // should ensure that the returned backend texture is valid.
+    // If successful, the created backend texture will be compatible with the provided
+    // pixmap(s). Compatible, in this case, means that the backend format will be the result
+    // of calling defaultBackendFormat on the base pixmap's colortype.
+    // If numLevels is 1 a non-mipMapped texture will result. If a mipMapped texture is desired
+    // the data for all the mipmap levels must be provided. In the mipmapped case all the
+    // colortypes of the provided pixmaps must be the same. Additionally, all the miplevels
+    // must be sized correctly (please see SkMipMap::ComputeLevelSize and ComputeLevelCount).
+    // Note: the pixmap's alphatypes and colorspaces are ignored.
+    // For the Vulkan backend the layout of the created VkImage will be:
+    //      VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
+    // regardless of the renderability setting
+    GrBackendTexture createBackendTexture(const SkPixmap srcData[], int numLevels,
+                                          GrRenderable, GrProtected);
+
     void deleteBackendTexture(GrBackendTexture);
 
     // This interface allows clients to pre-compile shaders and populate the runtime program cache.
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index cd9db04..a41922d 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -478,6 +478,33 @@
                                       isProtected);
 }
 
+GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numLevels,
+                                                 GrRenderable renderable, GrProtected isProtected) {
+    TRACE_EVENT0("skia.gpu", TRACE_FUNC);
+
+    if (!this->asDirectContext()) {
+        return {};
+    }
+
+    if (this->abandoned()) {
+        return {};
+    }
+
+    if (!srcData || !numLevels) {
+        return {};
+    }
+
+    int baseWidth = srcData[0].width();
+    int baseHeight = srcData[0].height();
+    SkColorType colorType = srcData[0].colorType();
+
+    GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
+
+    return fGpu->createBackendTexture(baseWidth, baseHeight, backendFormat,
+                                      numLevels > 1 ? GrMipMapped::kYes : GrMipMapped::kNo,
+                                      renderable, srcData, numLevels, nullptr, isProtected);
+}
+
 void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
     if (this->abandoned() || !backendTex.isValid()) {
diff --git a/src/gpu/GrContextPriv.cpp b/src/gpu/GrContextPriv.cpp
index 5507b43..a84ace6 100644
--- a/src/gpu/GrContextPriv.cpp
+++ b/src/gpu/GrContextPriv.cpp
@@ -338,35 +338,3 @@
 
     return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
 }
-
-//////////////////////////////////////////////////////////////////////////////
-
-#include "src/core/SkMipMap.h"
-
-GrBackendTexture GrContextPriv::createBackendTexture(const SkPixmap srcData[], int numLevels,
-                                                     GrRenderable renderable,
-                                                     GrProtected isProtected) {
-    if (!fContext->asDirectContext()) {
-        return {};
-    }
-
-    if (this->abandoned()) {
-        return {};
-    }
-
-    if (!srcData || !numLevels) {
-        return {};
-    }
-
-    int baseWidth = srcData[0].width();
-    int baseHeight = srcData[0].height();
-    SkColorType colorType = srcData[0].colorType();
-
-    GrBackendFormat backendFormat = fContext->defaultBackendFormat(colorType, renderable);
-
-    GrGpu* gpu = fContext->fGpu.get();
-
-    return gpu->createBackendTexture(baseWidth, baseHeight, backendFormat,
-                                     numLevels > 1 ? GrMipMapped::kYes : GrMipMapped::kNo,
-                                     renderable, srcData, numLevels, nullptr, isProtected);
-}
diff --git a/src/gpu/GrContextPriv.h b/src/gpu/GrContextPriv.h
index 0fbec0f..399ac57 100644
--- a/src/gpu/GrContextPriv.h
+++ b/src/gpu/GrContextPriv.h
@@ -261,18 +261,6 @@
     void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
 #endif
 
-    // If possible, create a backend texture initialized with the provided pixmap data. The client
-    // should ensure that the returned backend texture is valid.
-    // If successful, the created backend texture will be compatible with the provided
-    // pixmap(s).
-    // If numLevels is 1 a non-mipMapped texture will result. If a mipMapped texture is desired
-    // the data for all the mipmap levels must be provided.
-    // For the Vulkan backend the layout of the created VkImage will be:
-    //      VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
-    // regardless of the renderability setting
-    GrBackendTexture createBackendTexture(const SkPixmap srcData[], int numLevels,
-                                          GrRenderable, GrProtected);
-
 private:
     explicit GrContextPriv(GrContext* context) : fContext(context) {}
     GrContextPriv(const GrContextPriv&); // unimpl
diff --git a/tests/BackendAllocationTest.cpp b/tests/BackendAllocationTest.cpp
index 732097d..ada040d 100644
--- a/tests/BackendAllocationTest.cpp
+++ b/tests/BackendAllocationTest.cpp
@@ -707,9 +707,8 @@
                                                    int numLevels,
                                                    GrRenderable renderable) {
                         SkASSERT(srcData && numLevels);
-                        auto result = context->priv().createBackendTexture(srcData, numLevels,
-                                                                           renderable,
-                                                                           GrProtected::kNo);
+                        auto result = context->createBackendTexture(srcData, numLevels,
+                                                                    renderable, GrProtected::kNo);
                         check_vk_layout(result, VkLayout::kReadOnlyOptimal);
 #ifdef SK_DEBUG
                         {
diff --git a/tests/ExtendedSkColorTypeTests.cpp b/tests/ExtendedSkColorTypeTests.cpp
index 5de5bd5..ce06ec2 100644
--- a/tests/ExtendedSkColorTypeTests.cpp
+++ b/tests/ExtendedSkColorTypeTests.cpp
@@ -8,7 +8,6 @@
 #include "include/core/SkImage.h"
 #include "include/core/SkSurface.h"
 #include "src/core/SkAutoPixmapStorage.h"
-#include "src/gpu/GrContextPriv.h"
 
 #include "tests/Test.h"
 #include "tests/TestUtils.h"
@@ -195,9 +194,8 @@
         GrBackendTexture backendTex;
 
         if (fullInit) {
-            backendTex = context->priv().createBackendTexture(&nativeExpected, 1,
-                                                              GrRenderable::kNo,
-                                                              GrProtected::kNo);
+            backendTex = context->createBackendTexture(&nativeExpected, 1,
+                                                       GrRenderable::kNo, GrProtected::kNo);
         } else {
             backendTex = context->createBackendTexture(kSize, kSize, test.fColorType,
                                                        SkColors::kWhite, GrMipMapped::kNo,
diff --git a/tests/GrSurfaceTest.cpp b/tests/GrSurfaceTest.cpp
index 0b5fdba..363d2f6 100644
--- a/tests/GrSurfaceTest.cpp
+++ b/tests/GrSurfaceTest.cpp
@@ -10,10 +10,8 @@
 #include "include/gpu/GrContext.h"
 #include "include/gpu/GrTexture.h"
 #include "src/core/SkAutoPixmapStorage.h"
-#include "src/core/SkMipMap.h"
 #include "src/gpu/GrClip.h"
 #include "src/gpu/GrContextPriv.h"
-#include "src/gpu/GrDataUtils.h"
 #include "src/gpu/GrGpu.h"
 #include "src/gpu/GrImageInfo.h"
 #include "src/gpu/GrProxyProvider.h"
@@ -397,9 +395,8 @@
     // that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with
     // kRead for the right reason.
     for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) {
-        auto backendTex = context->priv().createBackendTexture(&srcPixmap, 1,
-                                                               GrRenderable::kYes,
-                                                               GrProtected::kNo);
+        auto backendTex = context->createBackendTexture(&srcPixmap, 1,
+                                                        GrRenderable::kYes, GrProtected::kNo);
 
         auto proxy = proxyProvider->wrapBackendTexture(backendTex, GrColorType::kRGBA_8888,
                                                        kTopLeft_GrSurfaceOrigin,
diff --git a/tests/GrTestingBackendTextureUploadTest.cpp b/tests/GrTestingBackendTextureUploadTest.cpp
index 7428274..8cd1008 100644
--- a/tests/GrTestingBackendTextureUploadTest.cpp
+++ b/tests/GrTestingBackendTextureUploadTest.cpp
@@ -7,10 +7,7 @@
 
 #include "src/core/SkAutoPixmapStorage.h"
 #include "src/gpu/GrContextPriv.h"
-#include "src/gpu/GrGpu.h"
 #include "src/gpu/GrImageInfo.h"
-#include "src/gpu/GrSurfaceContext.h"
-#include "src/gpu/SkGr.h"
 #include "tests/Test.h"
 #include "tests/TestUtils.h"
 
@@ -42,8 +39,8 @@
 
         fill_pixel_data(kWidth, kHeight, expectedPixels.writable_addr32(0, 0));
 
-        backendTex = context->priv().createBackendTexture(&expectedPixels, 1,
-                                                          renderable, GrProtected::kNo);
+        backendTex = context->createBackendTexture(&expectedPixels, 1,
+                                                   renderable, GrProtected::kNo);
     } else {
         backendTex = context->createBackendTexture(kWidth, kHeight, ct, SkColors::kTransparent,
                                                    mipMapped, renderable, GrProtected::kNo);
diff --git a/tests/SurfaceTest.cpp b/tests/SurfaceTest.cpp
index 9fa0a06..7c0758d 100644
--- a/tests/SurfaceTest.cpp
+++ b/tests/SurfaceTest.cpp
@@ -830,7 +830,7 @@
 }
 
 static void test_surface_draw_partially(
-    skiatest::Reporter* reporter, sk_sp<SkSurface> surface, uint32_t origColor) {
+    skiatest::Reporter* reporter, sk_sp<SkSurface> surface, SkColor origColor) {
     const int kW = surface->width();
     const int kH = surface->height();
     SkPaint paint;
@@ -849,14 +849,14 @@
                                        readback.rowBytes(), 0, 0));
     bool stop = false;
 
-    SkPMColor origColorPM = SkPackARGB_as_RGBA((origColor >> 24 & 0xFF),
-                                               (origColor >> 0 & 0xFF),
-                                               (origColor >> 8 & 0xFF),
-                                               (origColor >> 16 & 0xFF));
-    SkPMColor rectColorPM = SkPackARGB_as_RGBA((kRectColor >> 24 & 0xFF),
-                                               (kRectColor >> 16 & 0xFF),
-                                               (kRectColor >> 8 & 0xFF),
-                                               (kRectColor >> 0 & 0xFF));
+    SkPMColor origColorPM = SkPackARGB_as_RGBA(SkColorGetA(origColor),
+                                               SkColorGetR(origColor),
+                                               SkColorGetG(origColor),
+                                               SkColorGetB(origColor));
+    SkPMColor rectColorPM = SkPackARGB_as_RGBA(SkColorGetA(kRectColor),
+                                               SkColorGetR(kRectColor),
+                                               SkColorGetG(kRectColor),
+                                               SkColorGetB(kRectColor));
 
     for (int y = 0; y < kH/2 && !stop; ++y) {
        for (int x = 0; x < kW && !stop; ++x) {
diff --git a/tests/TestUtils.cpp b/tests/TestUtils.cpp
index d8f6d67..2e6120a 100644
--- a/tests/TestUtils.cpp
+++ b/tests/TestUtils.cpp
@@ -101,20 +101,8 @@
 bool create_backend_texture(GrContext* context, GrBackendTexture* backendTex,
                             const SkImageInfo& ii, const SkColor4f& color,
                             GrMipMapped mipMapped, GrRenderable renderable) {
-    // TODO: use the color-init version of createBackendTexture once Metal supports it.
-#if 0
     *backendTex = context->createBackendTexture(ii.width(), ii.height(), ii.colorType(),
                                                 color, mipMapped, renderable);
-#else
-    SkBitmap bm;
-    bm.allocPixels(ii);
-    sk_memset32(bm.getAddr32(0, 0), color.toSkColor(), ii.width() * ii.height());
-
-    SkASSERT(GrMipMapped::kNo == mipMapped);
-    *backendTex = context->priv().createBackendTexture(&bm.pixmap(), 1, renderable,
-                                                       GrProtected::kNo);
-#endif
-
     return backendTex->isValid();
 }
 
diff --git a/tools/DDLPromiseImageHelper.cpp b/tools/DDLPromiseImageHelper.cpp
index 63901b1..5cfbb6e 100644
--- a/tools/DDLPromiseImageHelper.cpp
+++ b/tools/DDLPromiseImageHelper.cpp
@@ -8,12 +8,12 @@
 #include "tools/DDLPromiseImageHelper.h"
 
 #include "include/core/SkDeferredDisplayListRecorder.h"
+#include "include/core/SkPicture.h"
+#include "include/core/SkSerialProcs.h"
 #include "include/core/SkYUVAIndex.h"
 #include "include/core/SkYUVASizeInfo.h"
 #include "include/gpu/GrContext.h"
 #include "src/core/SkCachedData.h"
-#include "src/gpu/GrContextPriv.h"
-#include "src/gpu/GrGpu.h"
 #include "src/image/SkImage_Base.h"
 #include "src/image/SkImage_GpuYUVA.h"
 
@@ -71,7 +71,7 @@
     }
 #endif
 
-    return context->priv().createBackendTexture(&pm, 1, GrRenderable::kNo, GrProtected::kNo);
+    return context->createBackendTexture(&pm, 1, GrRenderable::kNo, GrProtected::kNo);
 }
 
 void DDLPromiseImageHelper::uploadAllToGPU(GrContext* context) {
@@ -100,7 +100,7 @@
 
             const SkBitmap& bm = info.normalBitmap();
 
-            GrBackendTexture backendTex = context->priv().createBackendTexture(
+            GrBackendTexture backendTex = context->createBackendTexture(
                                                         &bm.pixmap(), 1, GrRenderable::kNo,
                                                         GrProtected::kNo);