[graphite] Fix A->RGBx GPU to CPU transfers

Because GPU->CPU transfers that detect kIgnoreSrc rewrite the
texture format *after* FormatXferOps were queried, the transfer
function was not detecting that the case where we'd normally be
padding a 3-channel RGB texture up to a 4-channel CPU type.

Before this fix, an A->RGBx GPU to CPU transfer would detect it as
kIgnoreSrc, and then switch the texture format to the default format
for the kRGB_888x color type, which is TF::kRGB8 (3 bytes). Since this
switch happened after the extended transfer ops were set, the actual
transfer function assumed the bpp was 12 and would not properly
iterate across each row.

The fix is to lift the dstCT from RGBx to RGBA and add the kForceOpaque
flag. This then means the switched texture format is kRGBA8 and all the
pixels' bits are written to. An alternative fix would have been to keep
the dstCT as RGBx but add in the kPadAlpha. This would produce the
identical output. I chose not to do this because there is already the
kIgnoreSrc + kForceOpaque xfer op combination, but not currently any
use for kIgnoreSrc + kPadAlpha.

This scenario should have been caught by the existing bulk transfer
tests in TextureFormatTest, except that the dst output buffer was
being zero-initialized alongside the expected 1px dst output. This
meant the comparison was unable to differentiate the problematic
case writing `00 00 00, 00 00 00, ...` vs. the correct
`00 00 00 xx, 00 00 00 xx`.

I confirmed that by initializing the dst output with a sentinel
value that the TextureFormatTest detected this bpp problem. Because
the values that were problematic happened to fall into a padding
channel, I adjusted transfer_data to report success in order to
dump the pixel comparison, instead of assuming that compare_pixels
would also report a failure.

Bug: 573263421
Fixed: 573263421
Change-Id: I6f535822c91aaddaa66bbc6d2457ff0c54f471fc
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1414539
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
Auto-Submit: Michael Ludwig <michaelludwig@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/graphite/TextureFormatXferFn.cpp b/src/gpu/graphite/TextureFormatXferFn.cpp
index cbaa809..387d522 100644
--- a/src/gpu/graphite/TextureFormatXferFn.cpp
+++ b/src/gpu/graphite/TextureFormatXferFn.cpp
@@ -574,7 +574,12 @@
                     // so instead it can just use kForceOpaque
                     finalOps |= kForceOpaque;
                 }
-            } else if (!SkColorTypeIsAlwaysOpaque(*dstCT)) {
+            } else if (!SkColorTypeIsAlwaysOpaque(*dstCT) ||
+                       (!TextureIsDst && rgbx_to_rgba(dstCT))) {
+                // An RGBx CPU dst color type needs to be lifted to RGBA or the "src" texture format
+                // detection will pick a 3-channel format that would have normally included the
+                // FormatXferOp::kDropAlpha flag to handle the missing channel for CPU conversion.
+                // Since kIgnoreSrc is playing texture format games, it has to be handled manually.
                 finalOps |= kForceOpaque;
             }
 
diff --git a/tests/graphite/TextureFormatTest.cpp b/tests/graphite/TextureFormatTest.cpp
index e52b0ba..f103cee 100644
--- a/tests/graphite/TextureFormatTest.cpp
+++ b/tests/graphite/TextureFormatTest.cpp
@@ -521,10 +521,10 @@
     return true;
 }
 
-PixelData transfer_data(const TextureFormatXferFn& xferFn,
-                        const PixelData& inputData,
-                        size_t srcPixelBits,
-                        size_t dstPixelBits) {
+std::pair<PixelData, bool> transfer_data(const TextureFormatXferFn& xferFn,
+                                         const PixelData& inputData,
+                                         size_t srcPixelBits,
+                                         size_t dstPixelBits) {
     // First do a conversion on a 1x1 image, which will represent the expected output when
     // testing various width/height/rowbyte combinations for a solid input.
     PixelData outputData{}; // zero-initialize for comparison stability on unwritten values.
@@ -577,13 +577,17 @@
         fill_image(kWidth, kHeight, srcRowBytes);
         for (bool outputDense : {true, false}) {
             size_t dstRowBytes = (outputDense ? kWidth : kMaxDim) * dstPixelBytes;
+            // Filling `dst` with non-zero values detects if any bytes are not written to by
+            // `xferFn` since the comparison value (`outputData` for the 1x1) will have skipped
+            // those same parts of the pixel and *was* zero-initialized.
+            dst->fill(1);
             xferFn.run(kWidth, kHeight, src->data(), srcRowBytes, dst->data(), dstRowBytes);
             if (!check_image(kWidth, kHeight, dstRowBytes)) {
-                return outputData;
+                return {outputData, false};
             }
         }
     }
-    return outputData;
+    return {outputData, true};
 }
 
 // Define the channel layout for every SkColorType for use in generating and validating the
@@ -1156,7 +1160,8 @@
                                                                    src.fChannels,
                                                                    loadSrc,
                                                                    validSrcAT);
-            PixelData actualGpuPixel = transfer_data(*xferFn, cpuPixel, cpuPixelBits, gpuPixelBits);
+            auto [actualGpuPixel, success] =
+                    transfer_data(*xferFn, cpuPixel, cpuPixelBits, gpuPixelBits);
 
             // The data transfer can skip alpha handling if we know that all samples/reads are going
             // to override it with 1.0 anyways. Testing the final readSwizzle includes both color
@@ -1174,7 +1179,8 @@
 
             const int tol = channel_tolerance(src.fChannels, validSrcAT,
                                               expectedTextureChannels, dstAT);
-            if (!compare_pixels(expectedTextureChannels, expectedGpuPixel, actualGpuPixel, tol,
+            if (!success ||
+                !compare_pixels(expectedTextureChannels, expectedGpuPixel, actualGpuPixel, tol,
                                 ignoreChannel)) {
                 dump_pixel_comparison(ctLabel,
                                       src.fChannels,
@@ -1236,11 +1242,12 @@
                                      gpuLabel, expectedTextureChannels, gpuPixel,
                                      ctLabel, dst.fChannels, expectedCpuPixel);
 
-            PixelData actualCpuPixel = transfer_data(*xferFn, gpuPixel, gpuPixelBits, cpuPixelBits);
-
+            auto [actualCpuPixel, success] =
+                    transfer_data(*xferFn, gpuPixel, gpuPixelBits, cpuPixelBits);
             const int tol = channel_tolerance(expectedTextureChannels, srcAT,
                                               dst.fChannels, dstAT);
-            if (!compare_pixels(dst.fChannels, expectedCpuPixel, actualCpuPixel, tol)) {
+            if (!success ||
+                !compare_pixels(dst.fChannels, expectedCpuPixel, actualCpuPixel, tol)) {
                 dump_pixel_comparison(gpuLabel,
                                       textureFormat.fChannels,
                                       gpuPixel,