Make RenderDeviceDriverIface::StartDIBits() take RetainPtrs by value

More consistently pass bitmaps around by value, with move semantics,
rather than by const-ref. Change related code in the call stack to do
the same.

Change-Id: Ic3f7bc947addba284496c095361ceba8ef024d06
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/115530
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp
index 7ae0f1c..652afe0 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.cpp
+++ b/core/fpdfapi/render/cpdf_renderstatus.cpp
@@ -1215,8 +1215,8 @@
           std::unique_ptr<CFX_ImageRenderer> dummy;
           CFX_Matrix matrix = CFX_RenderDevice::GetFlipMatrix(
               pDIBitmap->GetWidth(), pDIBitmap->GetHeight(), left, top);
-          m_pDevice->StartDIBits(pDIBitmap, alpha, /*argb=*/0, matrix,
-                                 FXDIB_ResampleOptions(), &dummy);
+          m_pDevice->StartDIBits(std::move(pDIBitmap), alpha, /*argb=*/0,
+                                 matrix, FXDIB_ResampleOptions(), &dummy);
           return;
         }
         pDIBitmap->MultiplyAlpha(alpha);
diff --git a/core/fxge/agg/fx_agg_driver.cpp b/core/fxge/agg/fx_agg_driver.cpp
index 5fe268c..d7b1c64 100644
--- a/core/fxge/agg/fx_agg_driver.cpp
+++ b/core/fxge/agg/fx_agg_driver.cpp
@@ -1354,15 +1354,15 @@
                    /*bVertical=*/false, /*bFlipX=*/false, /*bFlipY=*/false,
                    m_bRgbByteOrder, blend_type);
   dest_clip.Offset(-dest_rect.left, -dest_rect.top);
-  CFX_ImageStretcher stretcher(&composer, pSource, dest_width, dest_height,
-                               dest_clip, options);
+  CFX_ImageStretcher stretcher(&composer, std::move(pSource), dest_width,
+                               dest_height, dest_clip, options);
   if (stretcher.Start())
     stretcher.Continue(nullptr);
   return true;
 }
 
 bool CFX_AggDeviceDriver::StartDIBits(
-    const RetainPtr<const CFX_DIBBase>& pSource,
+    RetainPtr<const CFX_DIBBase> bitmap,
     float alpha,
     uint32_t argb,
     const CFX_Matrix& matrix,
@@ -1372,9 +1372,9 @@
   if (m_pBitmap->GetBuffer().empty())
     return true;
 
-  *handle = std::make_unique<CFX_ImageRenderer>(m_pBitmap, m_pClipRgn.get(),
-                                                pSource, alpha, argb, matrix,
-                                                options, m_bRgbByteOrder);
+  *handle = std::make_unique<CFX_ImageRenderer>(
+      m_pBitmap, m_pClipRgn.get(), std::move(bitmap), alpha, argb, matrix,
+      options, m_bRgbByteOrder);
   return true;
 }
 
diff --git a/core/fxge/agg/fx_agg_driver.h b/core/fxge/agg/fx_agg_driver.h
index c82f0a4..2c15e42 100644
--- a/core/fxge/agg/fx_agg_driver.h
+++ b/core/fxge/agg/fx_agg_driver.h
@@ -82,7 +82,7 @@
                      const FX_RECT* pClipRect,
                      const FXDIB_ResampleOptions& options,
                      BlendMode blend_type) override;
-  bool StartDIBits(const RetainPtr<const CFX_DIBBase>& pSource,
+  bool StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                    float alpha,
                    uint32_t argb,
                    const CFX_Matrix& matrix,
diff --git a/core/fxge/cfx_renderdevice.cpp b/core/fxge/cfx_renderdevice.cpp
index 6e4994c..64a7a7f 100644
--- a/core/fxge/cfx_renderdevice.cpp
+++ b/core/fxge/cfx_renderdevice.cpp
@@ -983,16 +983,26 @@
                                         BlendMode::kNormal);
 }
 
+bool CFX_RenderDevice::StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
+                                   float alpha,
+                                   uint32_t argb,
+                                   const CFX_Matrix& matrix,
+                                   const FXDIB_ResampleOptions& options,
+                                   std::unique_ptr<CFX_ImageRenderer>* handle) {
+  return StartDIBitsWithBlend(std::move(bitmap), alpha, argb, matrix, options,
+                              handle, BlendMode::kNormal);
+}
+
 bool CFX_RenderDevice::StartDIBitsWithBlend(
-    const RetainPtr<CFX_DIBBase>& pBitmap,
+    RetainPtr<const CFX_DIBBase> bitmap,
     float alpha,
     uint32_t argb,
     const CFX_Matrix& matrix,
     const FXDIB_ResampleOptions& options,
     std::unique_ptr<CFX_ImageRenderer>* handle,
     BlendMode blend_mode) {
-  return m_pDeviceDriver->StartDIBits(pBitmap, alpha, argb, matrix, options,
-                                      handle, blend_mode);
+  return m_pDeviceDriver->StartDIBits(std::move(bitmap), alpha, argb, matrix,
+                                      options, handle, blend_mode);
 }
 
 bool CFX_RenderDevice::ContinueDIBits(CFX_ImageRenderer* handle,
diff --git a/core/fxge/cfx_renderdevice.h b/core/fxge/cfx_renderdevice.h
index 14ae479..8a45a84 100644
--- a/core/fxge/cfx_renderdevice.h
+++ b/core/fxge/cfx_renderdevice.h
@@ -138,16 +138,13 @@
                                int dest_height,
                                uint32_t argb,
                                const FXDIB_ResampleOptions& options);
-  bool StartDIBits(const RetainPtr<CFX_DIBBase>& pBitmap,
+  bool StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                    float alpha,
                    uint32_t argb,
                    const CFX_Matrix& matrix,
                    const FXDIB_ResampleOptions& options,
-                   std::unique_ptr<CFX_ImageRenderer>* handle) {
-    return StartDIBitsWithBlend(pBitmap, alpha, argb, matrix, options, handle,
-                                BlendMode::kNormal);
-  }
-  bool StartDIBitsWithBlend(const RetainPtr<CFX_DIBBase>& pBitmap,
+                   std::unique_ptr<CFX_ImageRenderer>* handle);
+  bool StartDIBitsWithBlend(RetainPtr<const CFX_DIBBase> bitmap,
                             float alpha,
                             uint32_t argb,
                             const CFX_Matrix& matrix,
diff --git a/core/fxge/dib/cfx_imagerenderer.cpp b/core/fxge/dib/cfx_imagerenderer.cpp
index 3dc2e27..3d125c7 100644
--- a/core/fxge/dib/cfx_imagerenderer.cpp
+++ b/core/fxge/dib/cfx_imagerenderer.cpp
@@ -9,6 +9,7 @@
 #include <math.h>
 
 #include <memory>
+#include <utility>
 
 #include "core/fxcrt/fx_system.h"
 #include "core/fxge/cfx_cliprgn.h"
@@ -16,15 +17,14 @@
 #include "core/fxge/dib/cfx_imagestretcher.h"
 #include "core/fxge/dib/cfx_imagetransformer.h"
 
-CFX_ImageRenderer::CFX_ImageRenderer(
-    const RetainPtr<CFX_DIBitmap>& pDevice,
-    const CFX_ClipRgn* pClipRgn,
-    const RetainPtr<const CFX_DIBBase>& pSource,
-    float alpha,
-    uint32_t mask_color,
-    const CFX_Matrix& matrix,
-    const FXDIB_ResampleOptions& options,
-    bool bRgbByteOrder)
+CFX_ImageRenderer::CFX_ImageRenderer(const RetainPtr<CFX_DIBitmap>& pDevice,
+                                     const CFX_ClipRgn* pClipRgn,
+                                     RetainPtr<const CFX_DIBBase> source,
+                                     float alpha,
+                                     uint32_t mask_color,
+                                     const CFX_Matrix& matrix,
+                                     const FXDIB_ResampleOptions& options,
+                                     bool bRgbByteOrder)
     : m_pDevice(pDevice),
       m_pClipRgn(pClipRgn),
       m_Matrix(matrix),
@@ -56,14 +56,15 @@
                          /*bVertical=*/true, flip_x, flip_y, m_bRgbByteOrder,
                          BlendMode::kNormal);
       m_Stretcher = std::make_unique<CFX_ImageStretcher>(
-          &m_Composer, pSource, dest_height, dest_width, bitmap_clip, options);
+          &m_Composer, std::move(source), dest_height, dest_width, bitmap_clip,
+          options);
       if (m_Stretcher->Start())
         m_State = State::kStretching;
       return;
     }
     m_State = State::kTransforming;
     m_pTransformer = std::make_unique<CFX_ImageTransformer>(
-        pSource, m_Matrix, options, &m_ClipBox);
+        std::move(source), m_Matrix, options, &m_ClipBox);
     return;
   }
 
@@ -85,7 +86,8 @@
                      m_bRgbByteOrder, BlendMode::kNormal);
   m_State = State::kStretching;
   m_Stretcher = std::make_unique<CFX_ImageStretcher>(
-      &m_Composer, pSource, dest_width, dest_height, bitmap_clip, options);
+      &m_Composer, std::move(source), dest_width, dest_height, bitmap_clip,
+      options);
   m_Stretcher->Start();
 }
 
diff --git a/core/fxge/dib/cfx_imagerenderer.h b/core/fxge/dib/cfx_imagerenderer.h
index 6a29c9d..6ceeaf4 100644
--- a/core/fxge/dib/cfx_imagerenderer.h
+++ b/core/fxge/dib/cfx_imagerenderer.h
@@ -24,7 +24,7 @@
  public:
   CFX_ImageRenderer(const RetainPtr<CFX_DIBitmap>& pDevice,
                     const CFX_ClipRgn* pClipRgn,
-                    const RetainPtr<const CFX_DIBBase>& pSource,
+                    RetainPtr<const CFX_DIBBase> source,
                     float alpha,
                     uint32_t mask_color,
                     const CFX_Matrix& matrix,
diff --git a/core/fxge/dib/cfx_imagestretcher.cpp b/core/fxge/dib/cfx_imagestretcher.cpp
index 1eafa40..f3fdf93 100644
--- a/core/fxge/dib/cfx_imagestretcher.cpp
+++ b/core/fxge/dib/cfx_imagestretcher.cpp
@@ -6,6 +6,8 @@
 
 #include "core/fxge/dib/cfx_imagestretcher.h"
 
+#include <utility>
+
 #include "core/fxcrt/fx_safe_types.h"
 #include "core/fxge/dib/cfx_dibbase.h"
 #include "core/fxge/dib/cfx_dibitmap.h"
@@ -71,20 +73,19 @@
 
 }  // namespace
 
-CFX_ImageStretcher::CFX_ImageStretcher(
-    ScanlineComposerIface* pDest,
-    const RetainPtr<const CFX_DIBBase>& pSource,
-    int dest_width,
-    int dest_height,
-    const FX_RECT& bitmap_rect,
-    const FXDIB_ResampleOptions& options)
+CFX_ImageStretcher::CFX_ImageStretcher(ScanlineComposerIface* pDest,
+                                       RetainPtr<const CFX_DIBBase> source,
+                                       int dest_width,
+                                       int dest_height,
+                                       const FX_RECT& bitmap_rect,
+                                       const FXDIB_ResampleOptions& options)
     : m_pDest(pDest),
-      m_pSource(pSource),
+      m_pSource(std::move(source)),
       m_ResampleOptions(options),
       m_DestWidth(dest_width),
       m_DestHeight(dest_height),
       m_ClipRect(bitmap_rect),
-      m_DestFormat(GetStretchedFormat(*pSource)) {
+      m_DestFormat(GetStretchedFormat(*m_pSource)) {
   DCHECK(m_ClipRect.Valid());
 }
 
diff --git a/core/fxge/dib/cfx_imagestretcher.h b/core/fxge/dib/cfx_imagestretcher.h
index 7e51dc7..561bc3a 100644
--- a/core/fxge/dib/cfx_imagestretcher.h
+++ b/core/fxge/dib/cfx_imagestretcher.h
@@ -22,7 +22,7 @@
 class CFX_ImageStretcher {
  public:
   CFX_ImageStretcher(ScanlineComposerIface* pDest,
-                     const RetainPtr<const CFX_DIBBase>& pSource,
+                     RetainPtr<const CFX_DIBBase> source,
                      int dest_width,
                      int dest_height,
                      const FX_RECT& bitmap_rect,
diff --git a/core/fxge/dib/cfx_imagetransformer.cpp b/core/fxge/dib/cfx_imagetransformer.cpp
index 12bc14d..b37d1f9 100644
--- a/core/fxge/dib/cfx_imagetransformer.cpp
+++ b/core/fxge/dib/cfx_imagetransformer.cpp
@@ -130,12 +130,11 @@
 
 }  // namespace
 
-CFX_ImageTransformer::CFX_ImageTransformer(
-    const RetainPtr<const CFX_DIBBase>& pSrc,
-    const CFX_Matrix& matrix,
-    const FXDIB_ResampleOptions& options,
-    const FX_RECT* pClip)
-    : m_pSrc(pSrc), m_matrix(matrix), m_ResampleOptions(options) {
+CFX_ImageTransformer::CFX_ImageTransformer(RetainPtr<const CFX_DIBBase> source,
+                                           const CFX_Matrix& matrix,
+                                           const FXDIB_ResampleOptions& options,
+                                           const FX_RECT* pClip)
+    : m_pSrc(std::move(source)), m_matrix(matrix), m_ResampleOptions(options) {
   FX_RECT result_rect = m_matrix.GetUnitRect().GetClosestRect();
   FX_RECT result_clip = result_rect;
   if (pClip)
diff --git a/core/fxge/dib/cfx_imagetransformer.h b/core/fxge/dib/cfx_imagetransformer.h
index 2329e9b..cfc03c4 100644
--- a/core/fxge/dib/cfx_imagetransformer.h
+++ b/core/fxge/dib/cfx_imagetransformer.h
@@ -39,7 +39,7 @@
     uint32_t pitch;
   };
 
-  CFX_ImageTransformer(const RetainPtr<const CFX_DIBBase>& pSrc,
+  CFX_ImageTransformer(RetainPtr<const CFX_DIBBase> source,
                        const CFX_Matrix& matrix,
                        const FXDIB_ResampleOptions& options,
                        const FX_RECT* pClip);
diff --git a/core/fxge/renderdevicedriver_iface.h b/core/fxge/renderdevicedriver_iface.h
index 206ad43..8d90f97 100644
--- a/core/fxge/renderdevicedriver_iface.h
+++ b/core/fxge/renderdevicedriver_iface.h
@@ -87,7 +87,7 @@
                              const FX_RECT* pClipRect,
                              const FXDIB_ResampleOptions& options,
                              BlendMode blend_type) = 0;
-  virtual bool StartDIBits(const RetainPtr<const CFX_DIBBase>& pBitmap,
+  virtual bool StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                            float alpha,
                            uint32_t color,
                            const CFX_Matrix& matrix,
diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp
index 0009c22..4c61823 100644
--- a/core/fxge/skia/fx_skia_device.cpp
+++ b/core/fxge/skia/fx_skia_device.cpp
@@ -1408,8 +1408,8 @@
   FXDIB_ResampleOptions sampling_options;
   sampling_options.bNoSmoothing = true;
 
-  return StartDIBitsSkia(pBitmap, src_rect, /*alpha=*/1.0f, color, matrix,
-                         sampling_options, blend_type);
+  return StartDIBitsSkia(std::move(pBitmap), src_rect, /*alpha=*/1.0f, color,
+                         matrix, sampling_options, blend_type);
 }
 
 bool CFX_SkiaDeviceDriver::StretchDIBits(
@@ -1436,22 +1436,22 @@
   FXDIB_ResampleOptions sampling_options;
   sampling_options.bNoSmoothing = true;
 
-  return StartDIBitsSkia(
-      pSource, FX_RECT(0, 0, pSource->GetWidth(), pSource->GetHeight()),
-      /*alpha=*/1.0f, color, matrix, sampling_options, blend_type);
+  FX_RECT rect(0, 0, pSource->GetWidth(), pSource->GetHeight());
+  return StartDIBitsSkia(std::move(pSource), rect, /*alpha=*/1.0f, color,
+                         matrix, sampling_options, blend_type);
 }
 
 bool CFX_SkiaDeviceDriver::StartDIBits(
-    const RetainPtr<const CFX_DIBBase>& pSource,
+    RetainPtr<const CFX_DIBBase> bitmap,
     float alpha,
     uint32_t color,
     const CFX_Matrix& matrix,
     const FXDIB_ResampleOptions& options,
     std::unique_ptr<CFX_ImageRenderer>* handle,
     BlendMode blend_type) {
-  return StartDIBitsSkia(
-      pSource, FX_RECT(0, 0, pSource->GetWidth(), pSource->GetHeight()), alpha,
-      color, matrix, options, blend_type);
+  FX_RECT rect(0, 0, bitmap->GetWidth(), bitmap->GetHeight());
+  return StartDIBitsSkia(std::move(bitmap), rect, alpha, color, matrix, options,
+                         blend_type);
 }
 
 bool CFX_SkiaDeviceDriver::ContinueDIBits(CFX_ImageRenderer* handle,
@@ -1575,17 +1575,16 @@
   m_pCanvas->clear(color);
 }
 
-bool CFX_SkiaDeviceDriver::StartDIBitsSkia(
-    const RetainPtr<const CFX_DIBBase>& pSource,
-    const FX_RECT& src_rect,
-    float alpha,
-    uint32_t color,
-    const CFX_Matrix& matrix,
-    const FXDIB_ResampleOptions& options,
-    BlendMode blend_type) {
+bool CFX_SkiaDeviceDriver::StartDIBitsSkia(RetainPtr<const CFX_DIBBase> bitmap,
+                                           const FX_RECT& src_rect,
+                                           float alpha,
+                                           uint32_t color,
+                                           const CFX_Matrix& matrix,
+                                           const FXDIB_ResampleOptions& options,
+                                           BlendMode blend_type) {
   DebugValidate(m_pBitmap);
 
-  sk_sp<SkImage> skia_source = pSource->RealizeSkImage();
+  sk_sp<SkImage> skia_source = bitmap->RealizeSkImage();
   if (!skia_source) {
     return false;
   }
@@ -1593,13 +1592,13 @@
   {
     SkAutoCanvasRestore scoped_save_restore(m_pCanvas, /*doSave=*/true);
 
-    const int width = pSource->GetWidth();
-    const int height = pSource->GetHeight();
+    const int width = bitmap->GetWidth();
+    const int height = bitmap->GetHeight();
     SkMatrix skMatrix;
     SetBitmapMatrix(matrix, width, height, &skMatrix);
     m_pCanvas->concat(skMatrix);
     SkPaint paint;
-    SetBitmapPaint(pSource->IsMaskFormat(), !m_FillOptions.aliased_path, alpha,
+    SetBitmapPaint(bitmap->IsMaskFormat(), !m_FillOptions.aliased_path, alpha,
                    color, blend_type, &paint);
 
     bool use_interpolate_bilinear = options.bInterpolateBilinear;
diff --git a/core/fxge/skia/fx_skia_device.h b/core/fxge/skia/fx_skia_device.h
index fbecb16..fb36981 100644
--- a/core/fxge/skia/fx_skia_device.h
+++ b/core/fxge/skia/fx_skia_device.h
@@ -120,7 +120,7 @@
                      const FXDIB_ResampleOptions& options,
                      BlendMode blend_type) override;
 
-  bool StartDIBits(const RetainPtr<const CFX_DIBBase>& pBitmap,
+  bool StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                    float alpha,
                    uint32_t color,
                    const CFX_Matrix& matrix,
@@ -207,7 +207,7 @@
                    uint32_t color,
                    const CFX_TextRenderOptions& options);
 
-  bool StartDIBitsSkia(const RetainPtr<const CFX_DIBBase>& pSource,
+  bool StartDIBitsSkia(RetainPtr<const CFX_DIBBase> bitmap,
                        const FX_RECT& src_rect,
                        float alpha,
                        uint32_t color,
diff --git a/core/fxge/win32/cfx_psrenderer.cpp b/core/fxge/win32/cfx_psrenderer.cpp
index e8068bd..fd9fcbc 100644
--- a/core/fxge/win32/cfx_psrenderer.cpp
+++ b/core/fxge/win32/cfx_psrenderer.cpp
@@ -485,7 +485,7 @@
   StartRendering();
   CFX_Matrix matrix = CFX_RenderDevice::GetFlipMatrix(
       pSource->GetWidth(), pSource->GetHeight(), left, top);
-  return DrawDIBits(pSource, color, matrix, FXDIB_ResampleOptions());
+  return DrawDIBits(std::move(pSource), color, matrix, FXDIB_ResampleOptions());
 }
 
 bool CFX_PSRenderer::StretchDIBits(const RetainPtr<const CFX_DIBBase>& pSource,
@@ -498,10 +498,10 @@
   StartRendering();
   CFX_Matrix matrix = CFX_RenderDevice::GetFlipMatrix(dest_width, dest_height,
                                                       dest_left, dest_top);
-  return DrawDIBits(pSource, color, matrix, options);
+  return DrawDIBits(std::move(pSource), color, matrix, options);
 }
 
-bool CFX_PSRenderer::DrawDIBits(const RetainPtr<const CFX_DIBBase>& pSource,
+bool CFX_PSRenderer::DrawDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                                 uint32_t color,
                                 const CFX_Matrix& matrix,
                                 const FXDIB_ResampleOptions& options) {
@@ -509,12 +509,14 @@
   if ((matrix.a == 0 && matrix.b == 0) || (matrix.c == 0 && matrix.d == 0))
     return true;
 
-  if (pSource->IsAlphaFormat())
+  if (bitmap->IsAlphaFormat()) {
     return false;
+  }
 
   int alpha = FXARGB_A(color);
-  if (pSource->IsMaskFormat() && (alpha < 255 || pSource->GetBPP() != 1))
+  if (bitmap->IsMaskFormat() && (alpha < 255 || bitmap->GetBPP() != 1)) {
     return false;
+  }
 
   WriteString("q\n");
 
@@ -522,16 +524,16 @@
   buf << "[" << matrix.a << " " << matrix.b << " " << matrix.c << " "
       << matrix.d << " " << matrix.e << " " << matrix.f << "]cm ";
 
-  const int width = pSource->GetWidth();
-  const int height = pSource->GetHeight();
+  const int width = bitmap->GetWidth();
+  const int height = bitmap->GetHeight();
   buf << width << " " << height;
 
-  if (pSource->GetBPP() == 1 && !pSource->HasPalette()) {
-    FaxCompressResult compress_result = FaxCompressData(pSource);
+  if (bitmap->GetBPP() == 1 && !bitmap->HasPalette()) {
+    FaxCompressResult compress_result = FaxCompressData(bitmap);
     if (compress_result.data.empty())
       return false;
 
-    if (pSource->IsMaskFormat()) {
+    if (bitmap->IsMaskFormat()) {
       SetColor(color);
       m_bColorSet = false;
       buf << " true[";
@@ -545,48 +547,48 @@
       buf << "<</K -1/EndOfBlock false/Columns " << width << "/Rows " << height
           << ">>/CCITTFaxDecode filter ";
     }
-    if (pSource->IsMaskFormat())
+    if (bitmap->IsMaskFormat()) {
       buf << "iM\n";
-    else
+    } else {
       buf << "false 1 colorimage\n";
+    }
 
     WriteStream(buf);
     WritePSBinary(compress_result.data);
   } else {
-    RetainPtr<const CFX_DIBBase> pConverted = pSource;
-    switch (pSource->GetFormat()) {
+    switch (bitmap->GetFormat()) {
       case FXDIB_Format::k1bppRgb:
       case FXDIB_Format::kRgb32:
-        pConverted = pConverted->ConvertTo(FXDIB_Format::kRgb);
+        bitmap = bitmap->ConvertTo(FXDIB_Format::kRgb);
         break;
       case FXDIB_Format::k8bppRgb:
-        if (pSource->HasPalette())
-          pConverted = pConverted->ConvertTo(FXDIB_Format::kRgb);
+        if (bitmap->HasPalette()) {
+          bitmap = bitmap->ConvertTo(FXDIB_Format::kRgb);
+        }
         break;
       default:
         break;
     }
-    if (!pConverted) {
+    if (!bitmap) {
       WriteString("\nQ\n");
       return false;
     }
 
-    int bpp = pConverted->GetBPP() / 8;
+    int bpp = bitmap->GetBPP() / 8;
     uint8_t* output_buf = nullptr;
     size_t output_size = 0;
     bool output_buf_is_owned = true;
     absl::optional<PSCompressResult> compress_result;
     ByteString filter;
     if ((m_Level.value() == RenderingLevel::kLevel2 || options.bLossy) &&
-        m_pEncoderIface->pJpegEncodeFunc(pConverted, &output_buf,
-                                         &output_size)) {
+        m_pEncoderIface->pJpegEncodeFunc(bitmap, &output_buf, &output_size)) {
       filter = "/DCTDecode filter ";
     } else {
       int src_pitch = width * bpp;
       output_size = height * src_pitch;
       output_buf = FX_Alloc(uint8_t, output_size);
       for (int row = 0; row < height; row++) {
-        const uint8_t* src_scan = pConverted->GetScanline(row).data();
+        const uint8_t* src_scan = bitmap->GetScanline(row).data();
         uint8_t* dest_scan = output_buf + row * src_pitch;
         if (bpp == 3) {
           for (int col = 0; col < width; col++) {
diff --git a/core/fxge/win32/cfx_psrenderer.h b/core/fxge/win32/cfx_psrenderer.h
index aa73abb..108ee03 100644
--- a/core/fxge/win32/cfx_psrenderer.h
+++ b/core/fxge/win32/cfx_psrenderer.h
@@ -88,7 +88,7 @@
                      int dest_width,
                      int dest_height,
                      const FXDIB_ResampleOptions& options);
-  bool DrawDIBits(const RetainPtr<const CFX_DIBBase>& pBitmap,
+  bool DrawDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                   uint32_t color,
                   const CFX_Matrix& matrix,
                   const FXDIB_ResampleOptions& options);
diff --git a/core/fxge/win32/cfx_psrenderer_unittest.cpp b/core/fxge/win32/cfx_psrenderer_unittest.cpp
index 9927692..2112a26 100644
--- a/core/fxge/win32/cfx_psrenderer_unittest.cpp
+++ b/core/fxge/win32/cfx_psrenderer_unittest.cpp
@@ -4,6 +4,8 @@
 
 #include "core/fxge/win32/cfx_psrenderer.h"
 
+#include <utility>
+
 #include "core/fxcrt/bytestring.h"
 #include "core/fxcrt/data_vector.h"
 #include "core/fxcrt/fx_coordinates.h"
@@ -204,8 +206,8 @@
     auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
     bool result = bitmap->Create(kWidth, kHeight, FXDIB_Format::k1bppRgb);
     ASSERT_TRUE(result);
-    ASSERT_TRUE(renderer.DrawDIBits(bitmap, /*color=*/0, CFX_Matrix(),
-                                    FXDIB_ResampleOptions()));
+    ASSERT_TRUE(renderer.DrawDIBits(std::move(bitmap), /*color=*/0,
+                                    CFX_Matrix(), FXDIB_ResampleOptions()));
   }
 
   ByteString output(output_stream->GetSpan());
diff --git a/core/fxge/win32/cgdi_display_driver.cpp b/core/fxge/win32/cgdi_display_driver.cpp
index fa9157b..ec705ed 100644
--- a/core/fxge/win32/cgdi_display_driver.cpp
+++ b/core/fxge/win32/cgdi_display_driver.cpp
@@ -213,7 +213,7 @@
                            dest_height, FXDIB_ResampleOptions());
 }
 
-bool CGdiDisplayDriver::StartDIBits(const RetainPtr<const CFX_DIBBase>& pBitmap,
+bool CGdiDisplayDriver::StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                                     float alpha,
                                     uint32_t color,
                                     const CFX_Matrix& matrix,
diff --git a/core/fxge/win32/cgdi_display_driver.h b/core/fxge/win32/cgdi_display_driver.h
index 4b71328..864c941 100644
--- a/core/fxge/win32/cgdi_display_driver.h
+++ b/core/fxge/win32/cgdi_display_driver.h
@@ -45,7 +45,7 @@
                      const FX_RECT* pClipRect,
                      const FXDIB_ResampleOptions& options,
                      BlendMode blend_type) override;
-  bool StartDIBits(const RetainPtr<const CFX_DIBBase>& pBitmap,
+  bool StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                    float alpha,
                    uint32_t color,
                    const CFX_Matrix& matrix,
diff --git a/core/fxge/win32/cgdi_printer_driver.cpp b/core/fxge/win32/cgdi_printer_driver.cpp
index 706aa5a..d4c16f3 100644
--- a/core/fxge/win32/cgdi_printer_driver.cpp
+++ b/core/fxge/win32/cgdi_printer_driver.cpp
@@ -11,6 +11,7 @@
 
 #include <algorithm>
 #include <memory>
+#include <utility>
 
 #include "core/fxcrt/fx_memory.h"
 #include "core/fxcrt/fx_system.h"
@@ -118,15 +119,15 @@
                            dest_height, options);
 }
 
-bool CGdiPrinterDriver::StartDIBits(const RetainPtr<const CFX_DIBBase>& pSource,
+bool CGdiPrinterDriver::StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                                     float alpha,
                                     uint32_t color,
                                     const CFX_Matrix& matrix,
                                     const FXDIB_ResampleOptions& options,
                                     std::unique_ptr<CFX_ImageRenderer>* handle,
                                     BlendMode blend_type) {
-  if (alpha != 1.0f || pSource->IsAlphaFormat() ||
-      (pSource->IsMaskFormat() && (pSource->GetBPP() != 1))) {
+  if (alpha != 1.0f || bitmap->IsAlphaFormat() ||
+      (bitmap->IsMaskFormat() && (bitmap->GetBPP() != 1))) {
     return false;
   }
   CFX_FloatRect unit_rect = matrix.GetUnitRect();
@@ -135,7 +136,7 @@
       matrix.d != 0) {
     bool bFlipX = matrix.a < 0;
     bool bFlipY = matrix.d > 0;
-    return StretchDIBits(pSource, color,
+    return StretchDIBits(std::move(bitmap), color,
                          bFlipX ? full_rect.right : full_rect.left,
                          bFlipY ? full_rect.bottom : full_rect.top,
                          bFlipX ? -full_rect.Width() : full_rect.Width(),
@@ -145,12 +146,14 @@
   if (fabs(matrix.a) >= 0.5f || fabs(matrix.d) >= 0.5f)
     return false;
 
-  RetainPtr<CFX_DIBBase> pTransformed =
-      pSource->SwapXY(matrix.c > 0, matrix.b < 0);
-  if (!pTransformed)
+  const bool flip_x = matrix.c > 0;
+  const bool flip_y = matrix.b < 0;
+  bitmap = bitmap->SwapXY(flip_x, flip_y);
+  if (!bitmap) {
     return false;
+  }
 
-  return StretchDIBits(pTransformed, color, full_rect.left, full_rect.top,
+  return StretchDIBits(std::move(bitmap), color, full_rect.left, full_rect.top,
                        full_rect.Width(), full_rect.Height(), nullptr,
                        FXDIB_ResampleOptions(), blend_type);
 }
diff --git a/core/fxge/win32/cgdi_printer_driver.h b/core/fxge/win32/cgdi_printer_driver.h
index bf8f82c..c8e115d 100644
--- a/core/fxge/win32/cgdi_printer_driver.h
+++ b/core/fxge/win32/cgdi_printer_driver.h
@@ -36,7 +36,7 @@
                      const FX_RECT* pClipRect,
                      const FXDIB_ResampleOptions& options,
                      BlendMode blend_type) override;
-  bool StartDIBits(const RetainPtr<const CFX_DIBBase>& pBitmap,
+  bool StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                    float alpha,
                    uint32_t color,
                    const CFX_Matrix& matrix,
diff --git a/core/fxge/win32/cps_printer_driver.cpp b/core/fxge/win32/cps_printer_driver.cpp
index 3957520..79b523b 100644
--- a/core/fxge/win32/cps_printer_driver.cpp
+++ b/core/fxge/win32/cps_printer_driver.cpp
@@ -9,12 +9,14 @@
 #include <stdint.h>
 
 #include <sstream>
+#include <utility>
 
 #include "core/fxcrt/data_vector.h"
 #include "core/fxcrt/fx_system.h"
 #include "core/fxcrt/retain_ptr.h"
 #include "core/fxge/cfx_fillrenderoptions.h"
 #include "core/fxge/cfx_path.h"
+#include "core/fxge/dib/cfx_dibbase.h"
 #include "core/fxge/dib/cfx_imagerenderer.h"
 #include "core/fxge/win32/cpsoutput.h"
 #include "third_party/base/check.h"
@@ -182,7 +184,7 @@
                                     dest_width, dest_height, options);
 }
 
-bool CPSPrinterDriver::StartDIBits(const RetainPtr<const CFX_DIBBase>& pBitmap,
+bool CPSPrinterDriver::StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                                    float alpha,
                                    uint32_t color,
                                    const CFX_Matrix& matrix,
@@ -194,7 +196,7 @@
   }
 
   *handle = nullptr;
-  return m_PSRenderer.DrawDIBits(pBitmap, color, matrix, options);
+  return m_PSRenderer.DrawDIBits(std::move(bitmap), color, matrix, options);
 }
 
 bool CPSPrinterDriver::DrawDeviceText(
diff --git a/core/fxge/win32/cps_printer_driver.h b/core/fxge/win32/cps_printer_driver.h
index 5de81b9..3899e72 100644
--- a/core/fxge/win32/cps_printer_driver.h
+++ b/core/fxge/win32/cps_printer_driver.h
@@ -60,7 +60,7 @@
                      const FX_RECT* pClipRect,
                      const FXDIB_ResampleOptions& options,
                      BlendMode blend_type) override;
-  bool StartDIBits(const RetainPtr<const CFX_DIBBase>& pBitmap,
+  bool StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                    float alpha,
                    uint32_t color,
                    const CFX_Matrix& matrix,
diff --git a/core/fxge/win32/ctext_only_printer_driver.cpp b/core/fxge/win32/ctext_only_printer_driver.cpp
index 8696b88..b2c8b2e 100644
--- a/core/fxge/win32/ctext_only_printer_driver.cpp
+++ b/core/fxge/win32/ctext_only_printer_driver.cpp
@@ -14,6 +14,7 @@
 #include "core/fxcrt/fx_string.h"
 #include "core/fxcrt/fx_system.h"
 #include "core/fxge/cfx_font.h"
+#include "core/fxge/dib/cfx_dibbase.h"
 #include "core/fxge/text_char_pos.h"
 #include "third_party/base/check_op.h"
 #include "third_party/base/notreached.h"
@@ -114,7 +115,7 @@
 }
 
 bool CTextOnlyPrinterDriver::StartDIBits(
-    const RetainPtr<const CFX_DIBBase>& pBitmap,
+    RetainPtr<const CFX_DIBBase> bitmap,
     float alpha,
     uint32_t color,
     const CFX_Matrix& matrix,
diff --git a/core/fxge/win32/ctext_only_printer_driver.h b/core/fxge/win32/ctext_only_printer_driver.h
index a5c81d7..1cbec5d 100644
--- a/core/fxge/win32/ctext_only_printer_driver.h
+++ b/core/fxge/win32/ctext_only_printer_driver.h
@@ -51,7 +51,7 @@
                      const FX_RECT* pClipRect,
                      const FXDIB_ResampleOptions& options,
                      BlendMode blend_type) override;
-  bool StartDIBits(const RetainPtr<const CFX_DIBBase>& pBitmap,
+  bool StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                    float alpha,
                    uint32_t color,
                    const CFX_Matrix& matrix,