Minor fixup to D3D readPixels and writePixels.

The trimbytes were being set to values based on the texture rather than
the given inputs.

Bug: skia:9935
Change-Id: Ic3227236e4c3920ca4586c35791b15f5a596c069
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/284798
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
diff --git a/src/gpu/d3d/GrD3DGpu.cpp b/src/gpu/d3d/GrD3DGpu.cpp
index 6e2f79e..5b3bf25 100644
--- a/src/gpu/d3d/GrD3DGpu.cpp
+++ b/src/gpu/d3d/GrD3DGpu.cpp
@@ -291,14 +291,17 @@
     // Set up dst location and create transfer buffer
     D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
     dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
-    UINT transferNumRows;
-    UINT64 transferRowBytes;
     UINT64 transferTotalBytes;
     const UINT64 baseOffset = 0;
     D3D12_RESOURCE_DESC desc = srcLocation.pResource->GetDesc();
     fDevice->GetCopyableFootprints(&desc, 0, 1, baseOffset, &dstLocation.PlacedFootprint,
-                                   &transferNumRows, &transferRowBytes, &transferTotalBytes);
+                                   nullptr, nullptr, &transferTotalBytes);
     SkASSERT(transferTotalBytes);
+    size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
+    if (this->d3dCaps().bytesPerPixel(d3dTex->dxgiFormat()) != bpp) {
+        return false;
+    }
+    size_t tightRowBytes = bpp * width;
 
     // TODO: implement some way of reusing buffers instead of making a new one every time.
     sk_sp<GrGpuBuffer> transferBuffer = this->createBuffer(transferTotalBytes,
@@ -317,7 +320,7 @@
     const void* mappedMemory = transferBuffer->map();
 
     SkRectMemcpy(buffer, rowBytes, mappedMemory, dstLocation.PlacedFootprint.Footprint.RowPitch,
-                 transferRowBytes, transferNumRows);
+                 tightRowBytes, height);
 
     transferBuffer->unmap();
 
@@ -391,15 +394,14 @@
     }
 
     SkAutoTMalloc<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedFootprints(mipLevelCount);
-    SkAutoTMalloc<UINT> numRows(mipLevelCount);
-    SkAutoTMalloc<UINT64> rowBytes(mipLevelCount);
     UINT64 combinedBufferSize;
     // We reset the width and height in the description to match our subrectangle size
     // so we don't end up allocating more space than we need.
     desc.Width = width;
     desc.Height = height;
     fDevice->GetCopyableFootprints(&desc, 0, mipLevelCount, 0, placedFootprints.get(),
-                                   numRows.get(), rowBytes.get(), &combinedBufferSize);
+                                   nullptr, nullptr, &combinedBufferSize);
+    size_t bpp = GrColorTypeBytesPerPixel(colorType);
     SkASSERT(combinedBufferSize);
 
     // TODO: do this until we have slices of buttery buffers
@@ -419,15 +421,13 @@
         if (texels[currentMipLevel].fPixels) {
             SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
 
-            const size_t trimRowBytes = rowBytes[currentMipLevel];
+            const size_t trimRowBytes = currentWidth * bpp;
             const size_t srcRowBytes = texels[currentMipLevel].fRowBytes;
 
             char* dst = bufferData + placedFootprints[currentMipLevel].Offset;
 
             // copy data into the buffer, skipping any trailing bytes
-
             const char* src = (const char*)texels[currentMipLevel].fPixels;
-            SkASSERT(currentHeight == (int)numRows[currentMipLevel]);
             SkRectMemcpy(dst, placedFootprints[currentMipLevel].Footprint.RowPitch,
                          src, srcRowBytes, trimRowBytes, currentHeight);
         }