Improve handling of GrPixelConfig in GrBackendTex/RT ctors

Make sure that no client facing code was relying on what we set as the
default value for fConfig by making in kUnkown.

Bug: skia:
Change-Id: Ie52ff08ba8deeacc16fe06eb0dd0c7292b2edf91
Reviewed-on: https://skia-review.googlesource.com/114261
Reviewed-by: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
diff --git a/src/gpu/GrBackendSurface.cpp b/src/gpu/GrBackendSurface.cpp
index fa2582a..49a8c5f 100644
--- a/src/gpu/GrBackendSurface.cpp
+++ b/src/gpu/GrBackendSurface.cpp
@@ -69,7 +69,7 @@
                                    const GrVkImageInfo& vkInfo)
         : fWidth(width)
         , fHeight(height)
-        , fConfig(GrVkFormatToPixelConfig(vkInfo.fFormat))
+        , fConfig(kUnknown_GrPixelConfig)
         , fMipMapped(GrMipMapped(vkInfo.fLevelCount > 1))
         , fBackend(kVulkan_GrBackend)
         , fVkInfo(vkInfo) {}
@@ -99,7 +99,7 @@
                                    const GrGLTextureInfo& glInfo)
         : fWidth(width)
         , fHeight(height)
-        , fConfig(GrGLSizedFormatToPixelConfig(glInfo.fFormat))
+        , fConfig(kUnknown_GrPixelConfig)
         , fMipMapped(mipMapped)
         , fBackend(kOpenGL_GrBackend)
         , fGLInfo(glInfo) {}
@@ -159,7 +159,7 @@
         , fHeight(height)
         , fSampleCnt(SkTMax(1, sampleCnt))
         , fStencilBits(0)  // We always create stencil buffers internally for vulkan
-        , fConfig(GrVkFormatToPixelConfig(vkInfo.fFormat))
+        , fConfig(kUnknown_GrPixelConfig)
         , fBackend(kVulkan_GrBackend)
         , fVkInfo(vkInfo) {}
 #endif
@@ -187,7 +187,7 @@
         , fHeight(height)
         , fSampleCnt(SkTMax(1, sampleCnt))
         , fStencilBits(stencilBits)
-        , fConfig(GrGLSizedFormatToPixelConfig(glInfo.fFormat))
+        , fConfig(kUnknown_GrPixelConfig)
         , fBackend(kOpenGL_GrBackend)
         , fGLInfo(glInfo) {}
 
diff --git a/src/gpu/gl/GrGLGpu.cpp b/src/gpu/gl/GrGLGpu.cpp
index 9023554..bcea516 100644
--- a/src/gpu/gl/GrGLGpu.cpp
+++ b/src/gpu/gl/GrGLGpu.cpp
@@ -4410,7 +4410,9 @@
     // unbind the texture from the texture unit to avoid asserts
     GL_CALL(BindTexture(info.fTarget, 0));
 
-    return GrBackendTexture(w, h, mipMapped, info);
+    GrBackendTexture beTex = GrBackendTexture(w, h, mipMapped, info);
+    beTex.setPixelConfig(config);
+    return beTex;
 }
 
 bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
@@ -4499,7 +4501,9 @@
         return {};
     }
     auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
-    return {w, h, 1, stencilBits, config, info};
+    GrBackendRenderTarget beRT = {w, h, 1, stencilBits, info};
+    beRT.setPixelConfig(config);
+    return beRT;
 }
 
 void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
diff --git a/src/gpu/gl/GrGLRenderTarget.cpp b/src/gpu/gl/GrGLRenderTarget.cpp
index de7399b..73c5fdf 100644
--- a/src/gpu/gl/GrGLRenderTarget.cpp
+++ b/src/gpu/gl/GrGLRenderTarget.cpp
@@ -91,8 +91,16 @@
         numStencilBits = stencil->bits();
     }
 
-    return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(),
-                                 numStencilBits, fbi);
+    GrBackendRenderTarget beRT = GrBackendRenderTarget(this->width(), this->height(),
+                                                       this->numColorSamples(), numStencilBits,
+                                                       fbi);
+#if GR_TEST_UTILS
+    // We shouldn't have to set this since the client can't access it and we will handle the config
+    // correctly if we go through our public SkSurface APIs. However, some of our tests bypass the
+    // public APIs so we need to set this manually here.
+    beRT.setPixelConfig(this->config());
+#endif
+    return beRT;
 }
 
 size_t GrGLRenderTarget::onGpuMemorySize() const {
diff --git a/src/gpu/gl/GrGLTexture.cpp b/src/gpu/gl/GrGLTexture.cpp
index 0f36dd0..7710069 100644
--- a/src/gpu/gl/GrGLTexture.cpp
+++ b/src/gpu/gl/GrGLTexture.cpp
@@ -98,7 +98,15 @@
 }
 
 GrBackendTexture GrGLTexture::getBackendTexture() const {
-    return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), fInfo);
+    GrBackendTexture beTex = GrBackendTexture(this->width(), this->height(),
+                                              this->texturePriv().mipMapped(), fInfo);
+#if GR_TEST_UTILS
+    // We shouldn't have to set this since the client can't access it and we will handle the config
+    // correctly if we go through our public SkSurface and SkImage APIs. However, some of our tests
+    // bypass the public APIs so we need to set this manually here.
+    beTex.setPixelConfig(this->config());
+#endif
+    return beTex;
 }
 
 void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
diff --git a/src/gpu/gl/GrGLUtil.cpp b/src/gpu/gl/GrGLUtil.cpp
index 6b44402..a268827 100644
--- a/src/gpu/gl/GrGLUtil.cpp
+++ b/src/gpu/gl/GrGLUtil.cpp
@@ -486,38 +486,3 @@
     return gTable[(int)test];
 }
 
-GrPixelConfig GrGLSizedFormatToPixelConfig(GrGLenum sizedFormat) {
-    switch (sizedFormat) {
-        case GR_GL_R8:
-            return kAlpha_8_as_Red_GrPixelConfig;
-        case GR_GL_ALPHA8:
-            return kAlpha_8_as_Alpha_GrPixelConfig;
-        case GR_GL_RGBA8:
-            return kRGBA_8888_GrPixelConfig;
-        case GR_GL_BGRA8:
-            return kBGRA_8888_GrPixelConfig;
-        case GR_GL_SRGB8_ALPHA8:
-            return kSRGBA_8888_GrPixelConfig;
-        case GR_GL_RGB565:
-            return kRGB_565_GrPixelConfig;
-        case GR_GL_RGB5:
-            return kRGB_565_GrPixelConfig;
-        case GR_GL_RGBA4:
-            return kRGBA_4444_GrPixelConfig;
-        case GR_GL_RGB10_A2:
-            return kRGBA_1010102_GrPixelConfig;
-        case GR_GL_LUMINANCE8:
-            return kGray_8_GrPixelConfig;
-        case GR_GL_RGBA32F:
-            return kRGBA_float_GrPixelConfig;
-        case GR_GL_RG32F:
-            return kRG_float_GrPixelConfig;
-        case GR_GL_R16F:
-            return kAlpha_half_as_Red_GrPixelConfig;
-        case GR_GL_RGBA16F:
-            return kRGBA_half_GrPixelConfig;
-        default:
-            return kUnknown_GrPixelConfig;
-    }
-}
-
diff --git a/src/gpu/gl/GrGLUtil.h b/src/gpu/gl/GrGLUtil.h
index 9905d75..88e8901 100644
--- a/src/gpu/gl/GrGLUtil.h
+++ b/src/gpu/gl/GrGLUtil.h
@@ -248,6 +248,4 @@
 
 GrGLenum GrToGLStencilFunc(GrStencilTest test);
 
-GrPixelConfig GrGLSizedFormatToPixelConfig(GrGLenum sizedFormat);
-
 #endif
diff --git a/src/gpu/vk/GrVkGpu.cpp b/src/gpu/vk/GrVkGpu.cpp
index 5e6473e..a7daf39 100644
--- a/src/gpu/vk/GrVkGpu.cpp
+++ b/src/gpu/vk/GrVkGpu.cpp
@@ -1422,7 +1422,6 @@
         barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
         barrier.image = image;
         barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
-        initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
         VK_CALL(CmdPipelineBarrier(cmdBuffer,
                                    GrVkMemory::LayoutToPipelineStageFlags(initialLayout),
                                    VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
@@ -1430,6 +1429,7 @@
                                    0, nullptr,
                                    0, nullptr,
                                    1, &barrier));
+        initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
     }
 
     // End CommandBuffer
@@ -1498,7 +1498,9 @@
                                         &info)) {
         return {};
     }
-    return GrBackendTexture(w, h, info);
+    GrBackendTexture beTex = GrBackendTexture(w, h, info);
+    beTex.setPixelConfig(config);
+    return beTex;
 }
 
 bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
@@ -1541,7 +1543,9 @@
                                         &info)) {
         return {};
     }
-    return {w, h, 1, 0, info};
+    GrBackendRenderTarget beRT = {w, h, 1, 0, info};
+    beRT.setPixelConfig(config);
+    return beRT;
 }
 
 void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
diff --git a/src/gpu/vk/GrVkRenderTarget.cpp b/src/gpu/vk/GrVkRenderTarget.cpp
index 99b6b76..9c57ec0 100644
--- a/src/gpu/vk/GrVkRenderTarget.cpp
+++ b/src/gpu/vk/GrVkRenderTarget.cpp
@@ -352,8 +352,16 @@
     if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
         numStencilBits = stencil->bits();
     }
-    return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(),
-                                 numStencilBits, fInfo);
+    GrBackendRenderTarget beRT = GrBackendRenderTarget(this->width(), this->height(),
+                                                       this->numColorSamples(), numStencilBits,
+                                                       fInfo);
+#if GR_TEST_UTILS
+    // We shouldn't have to set this since the client can't access it and we will handle the config
+    // correctly if we go through our public SkSurface APIs. However, some of our tests bypass the
+    // public APIs so we need to set this manually here.
+    beRT.setPixelConfig(this->config());
+#endif
+    return beRT;
 }
 
 const GrVkResource* GrVkRenderTarget::stencilImageResource() const {
diff --git a/src/gpu/vk/GrVkTexture.cpp b/src/gpu/vk/GrVkTexture.cpp
index 109b106..6668b8f 100644
--- a/src/gpu/vk/GrVkTexture.cpp
+++ b/src/gpu/vk/GrVkTexture.cpp
@@ -162,7 +162,14 @@
 }
 
 GrBackendTexture GrVkTexture::getBackendTexture() const {
-    return GrBackendTexture(this->width(), this->height(), fInfo);
+    GrBackendTexture beTex = GrBackendTexture(this->width(), this->height(), fInfo);
+#if GR_TEST_UTILS
+    // We shouldn't have to set this since the client can't access it and we will handle the config
+    // correctly if we go through our public SkSurface and SkImage APIs. However, some of our tests
+    // bypass the public APIs so we need to set this manually here.
+    beTex.setPixelConfig(this->config());
+#endif
+    return beTex;
 }
 
 GrVkGpu* GrVkTexture::getVkGpu() const {
diff --git a/src/gpu/vk/GrVkUtil.cpp b/src/gpu/vk/GrVkUtil.cpp
index 4f0acdb..5edb2e3 100644
--- a/src/gpu/vk/GrVkUtil.cpp
+++ b/src/gpu/vk/GrVkUtil.cpp
@@ -72,40 +72,6 @@
     return false;
 }
 
-GrPixelConfig GrVkFormatToPixelConfig(VkFormat format) {
-    switch (format) {
-        case VK_FORMAT_R8G8B8A8_UNORM:
-            return kRGBA_8888_GrPixelConfig;
-        case VK_FORMAT_B8G8R8A8_UNORM:
-            return kBGRA_8888_GrPixelConfig;
-        case VK_FORMAT_R8G8B8A8_SRGB:
-            return kSRGBA_8888_GrPixelConfig;
-        case VK_FORMAT_B8G8R8A8_SRGB:
-            return kSBGRA_8888_GrPixelConfig;
-        case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
-            return kRGBA_1010102_GrPixelConfig;
-        case VK_FORMAT_R5G6B5_UNORM_PACK16:
-            return kRGB_565_GrPixelConfig;
-            break;
-        case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
-            // R4G4B4A4 is not required to be supported so we actually
-            // store RGBA_4444 data as B4G4R4A4.
-            return kRGBA_4444_GrPixelConfig;
-        case VK_FORMAT_R8_UNORM:
-            return kAlpha_8_GrPixelConfig;
-        case VK_FORMAT_R32G32B32A32_SFLOAT:
-            return kRGBA_float_GrPixelConfig;
-        case VK_FORMAT_R32G32_SFLOAT:
-            return kRG_float_GrPixelConfig;
-        case VK_FORMAT_R16G16B16A16_SFLOAT:
-            return kRGBA_half_GrPixelConfig;
-        case VK_FORMAT_R16_SFLOAT:
-            return kAlpha_half_GrPixelConfig;
-        default:
-            return kUnknown_GrPixelConfig;
-    }
-}
-
 bool GrVkFormatPixelConfigPairIsValid(VkFormat format, GrPixelConfig config) {
     switch (format) {
         case VK_FORMAT_R8G8B8A8_UNORM:
diff --git a/src/gpu/vk/GrVkUtil.h b/src/gpu/vk/GrVkUtil.h
index 01688c8..b0d118a 100644
--- a/src/gpu/vk/GrVkUtil.h
+++ b/src/gpu/vk/GrVkUtil.h
@@ -32,11 +32,6 @@
  */
 bool GrPixelConfigToVkFormat(GrPixelConfig config, VkFormat* format);
 
-/**
-* Returns the GrPixelConfig for the given vulkan texture format
-*/
-GrPixelConfig GrVkFormatToPixelConfig(VkFormat format);
-
 bool GrVkFormatIsSupported(VkFormat);
 
 /**