Use a single data storage in Upsample() in fx_skia_device.cpp.

Stop passing 2 vectors into Upsample() when only 1 will be used. Get rid
of the vector of 8-bit elements, and adapt Upsample() to always use the
vector of 32-bit elements.

Change-Id: I4b4df90245367f182ea14a38159b573461f8fc65
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/103221
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp
index 412bcae..532c157 100644
--- a/core/fxge/skia/fx_skia_device.cpp
+++ b/core/fxge/skia/fx_skia_device.cpp
@@ -30,6 +30,7 @@
 #include "core/fxcrt/fx_memory_wrappers.h"
 #include "core/fxcrt/fx_system.h"
 #include "core/fxcrt/stl_util.h"
+#include "core/fxge/calculate_pitch.h"
 #include "core/fxge/cfx_defaultrenderdevice.h"
 #include "core/fxge/cfx_font.h"
 #include "core/fxge/cfx_graphstatedata.h"
@@ -595,7 +596,6 @@
 }
 
 bool Upsample(const RetainPtr<CFX_DIBBase>& pSource,
-              DataVector<uint8_t>& dst8_storage,
               DataVector<uint32_t>& dst32_storage,
               SkBitmap* skBitmap,
               bool forceAlpha) {
@@ -624,7 +624,6 @@
                                IsRGBColorGrayScale(palette_color1);
         if (!use_gray_colors) {
           dst32_storage = Fill32BppDestStorageWith1BppSource(pSource);
-          buffer = dst32_storage.data();
           rowBytes = width * sizeof(uint32_t);
           colorType = kBGRA_8888_SkColorType;
           break;
@@ -634,17 +633,18 @@
         color1 = FXARGB_R(palette_color1);
       }
 
-      dst8_storage = DataVector<uint8_t>(Fx2DSizeOrDie(width, height));
-      pdfium::span<uint8_t> dst8_pixels(dst8_storage);
+      const int src_row_bytes = rowBytes;  // Save original value.
+      rowBytes = fxge::CalculatePitch32OrDie(/*bpp=*/8, width);
+      dst32_storage = DataVector<uint32_t>(Fx2DSizeOrDie(rowBytes / 4, height));
+      pdfium::span<uint8_t> dst8_pixels =
+          pdfium::as_writable_bytes(pdfium::make_span(dst32_storage));
       for (int y = 0; y < height; ++y) {
-        const uint8_t* srcRow =
-            static_cast<const uint8_t*>(buffer) + y * rowBytes;
-        pdfium::span<uint8_t> dst_row = dst8_pixels.subspan(y * width);
+        const uint8_t* src_row =
+            static_cast<const uint8_t*>(buffer) + y * src_row_bytes;
+        pdfium::span<uint8_t> dst_row = dst8_pixels.subspan(y * rowBytes);
         for (int x = 0; x < width; ++x)
-          dst_row[x] = srcRow[x >> 3] & (1 << (~x & 0x07)) ? color1 : color0;
+          dst_row[x] = src_row[x >> 3] & (1 << (~x & 0x07)) ? color1 : color0;
       }
-      buffer = dst8_storage.data();
-      rowBytes = width;
       break;
     }
     case 8:
@@ -657,7 +657,6 @@
           src_palette = src_palette.first(src_palette_size);
 
         dst32_storage = Fill32BppDestStorageWithPalette(pSource, src_palette);
-        buffer = dst32_storage.data();
         rowBytes = width * sizeof(uint32_t);
         colorType = kBGRA_8888_SkColorType;
       }
@@ -674,7 +673,6 @@
                                     srcRow[x * 3 + 0]);
         }
       }
-      buffer = dst32_storage.data();
       rowBytes = width * sizeof(uint32_t);
       colorType = kBGRA_8888_SkColorType;
       alphaType = kOpaque_SkAlphaType;
@@ -687,6 +685,9 @@
     default:
       NOTREACHED();
   }
+  if (!dst32_storage.empty()) {
+    buffer = dst32_storage.data();
+  }
   SkImageInfo imageInfo =
       SkImageInfo::Make(width, height, colorType, alphaType);
   skBitmap->installPixels(imageInfo, buffer, rowBytes);
@@ -2062,18 +2063,14 @@
     BlendMode blend_type) {
   DebugValidate(m_pBitmap, m_pBackdropBitmap);
   // Storage vectors must outlive `skBitmap` and `skMask`.
-  DataVector<uint8_t> src8_storage;
-  DataVector<uint8_t> mask8_storage;
   DataVector<uint32_t> src32_storage;
   DataVector<uint32_t> mask32_storage;
   SkBitmap skBitmap;
   SkBitmap skMask;
-  if (!Upsample(pSource, src8_storage, src32_storage, &skBitmap,
-                /*forceAlpha=*/false)) {
+  if (!Upsample(pSource, src32_storage, &skBitmap, /*forceAlpha=*/false)) {
     return false;
   }
-  if (!Upsample(pMask, mask8_storage, mask32_storage, &skMask,
-                /*forceAlpha=*/true)) {
+  if (!Upsample(pMask, mask32_storage, &skMask, /*forceAlpha=*/true)) {
     return false;
   }
   {
@@ -2146,12 +2143,10 @@
     BlendMode blend_type) {
   m_pCache->FlushForDraw();
   DebugValidate(m_pBitmap, m_pBackdropBitmap);
-  // Storage vectors must outlive `skBitmap`.
-  DataVector<uint8_t> dst8_storage;
+  // Storage vector must outlive `skBitmap`.
   DataVector<uint32_t> dst32_storage;
   SkBitmap skBitmap;
-  if (!Upsample(pSource, dst8_storage, dst32_storage, &skBitmap,
-                /*forceAlpha=*/false)) {
+  if (!Upsample(pSource, dst32_storage, &skBitmap, /*forceAlpha=*/false)) {
     return false;
   }
   {