Narrow fix for creating BGRA backend texture from pixmaps on desktop GL.

GrTestingBackendTextureUploadTest creates backend textures from BGRA
pixmaps. There is a bug on desktop GL. We choose a RGBA format for the
texture. We then infer the color type of the data from the format (via
a hop through GrPixelConfig) as RGBA and upload it as such.

Currently we don't fail the test because the test fails to read the data
back (because we don't support direct reading of BGRA data from textures
in GL).

Fixing the read back causes the test to fail.

This change removes the inference of color type from the format and
instead uses the pixmaps' color type.

This doesn't fix several larger issues, including:

1) The intended color type is not specified when creating a texture of a
solid color so we still infer it from the format in that case.

2) We may not support direct uploading of data in the pixmaps' color type
to the chosen texture format but must first convert it to some other
color type on the CPU.


Change-Id: I3b1022c40201fc107cb39bbb5cca775795c828e8
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/250336
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/gl/GrGLGpu.cpp b/src/gpu/gl/GrGLGpu.cpp
index 45f1111..d9f6a96 100644
--- a/src/gpu/gl/GrGLGpu.cpp
+++ b/src/gpu/gl/GrGLGpu.cpp
@@ -3626,13 +3626,6 @@
         return GrBackendTexture();  // invalid
     }
 
-    auto textureColorType = GrPixelConfigToColorType(config);
-
-    // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
-    if (!this->caps()->isFormatTexturableAndUploadable(textureColorType, format)) {
-        return GrBackendTexture();  // invalid
-    }
-
     GrGLTextureInfo info;
     GrGLTextureParameters::SamplerOverriddenState initialState;
 
@@ -3640,13 +3633,19 @@
     SkAutoMalloc pixelStorage;
 
     int mipLevelCount = 1;
+    GrColorType textureAndDataColorType = GrColorType::kUnknown;
     if (srcData) {
         mipLevelCount = numMipLevels;
+        textureAndDataColorType = SkColorTypeToGrColorType(srcData[0].colorType());
         texels.append(mipLevelCount);
         for (int i = 0; i < mipLevelCount; ++i) {
             texels[i] = { srcData[i].addr(), srcData[i].rowBytes() };
         }
     } else if (color) {
+        // We don't currently communicate the intended color type in this case so reverse engineer
+        // it from the config. Note this is lossy.
+        textureAndDataColorType = GrPixelConfigToColorType(config);
+
         if (GrMipMapped::kYes == mipMapped) {
             mipLevelCount = SkMipMap::ComputeLevelCount(w, h) + 1;
         }
@@ -3661,7 +3660,7 @@
 
         char* tmpPixels = (char*)pixelStorage.reset(totalSize);
 
-        GrFillInData(textureColorType, w, h, individualMipOffsets, tmpPixels, *color);
+        GrFillInData(textureAndDataColorType, w, h, individualMipOffsets, tmpPixels, *color);
         for (int i = 0; i < mipLevelCount; ++i) {
             size_t offset = individualMipOffsets[i];
 
@@ -3684,13 +3683,17 @@
     if (!info.fID) {
         return GrBackendTexture();  // invalid
     }
-    auto srcColorType = GrPixelConfigToColorType(desc.fConfig);
-    if (!texels.empty() &&
-        !this->uploadTexData(glFormat, textureColorType, desc.fWidth, desc.fHeight,
-                             GR_GL_TEXTURE_2D, 0, 0, desc.fWidth, desc.fHeight, srcColorType,
-                             texels.begin(), texels.count())) {
-        GL_CALL(DeleteTextures(1, &info.fID));
-        return GrBackendTexture();
+    if (!texels.empty()) {
+        // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
+        if (!this->caps()->isFormatTexturableAndUploadable(textureAndDataColorType, format)) {
+            return GrBackendTexture();  // invalid
+        }
+        if (!this->uploadTexData(glFormat, textureAndDataColorType, desc.fWidth, desc.fHeight,
+                                 GR_GL_TEXTURE_2D, 0, 0, desc.fWidth, desc.fHeight,
+                                 textureAndDataColorType, texels.begin(), texels.count())) {
+            GL_CALL(DeleteTextures(1, &info.fID));
+            return GrBackendTexture();
+        }
     }
 
     // unbind the texture from the texture unit to avoid asserts