Remove `CFX_DIBBase::m_pAlphaMask` and some related code.

After these CLs [1] [2] [3] [4] [5] removed many unused FXDIB_Format
values, `FXDIB_Format::kArgb` is the only remaining format which has an
alpha channel. Thus CFX_DIBBase::BuildAlphaMask() is dead code because
all of its call sites are unreachable. With BuildAlphaMask() gone,
`CFX_DIBBase::m_pAlphaMask` will always to null.

Remove all `m_pAlphaMask` use in CFX_DIBBase and CFX_DIBitmap. This
leaves several CFX_DIBBase methods as stubs. Add TODOs to evaluate
callers to these stubs.

[1] https://pdfium-review.googlesource.com/75510
[2] https://pdfium-review.googlesource.com/75531
[3] https://pdfium-review.googlesource.com/75532
[4] https://pdfium-review.googlesource.com/75533
[5] https://pdfium-review.googlesource.com/75550

Change-Id: I81a5be5aae166048efbceecc5533caf20d68872d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/101850
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxge/dib/cfx_dibbase.cpp b/core/fxge/dib/cfx_dibbase.cpp
index 464dda3..92e3674 100644
--- a/core/fxge/dib/cfx_dibbase.cpp
+++ b/core/fxge/dib/cfx_dibbase.cpp
@@ -634,7 +634,6 @@
     return nullptr;
 
   pNewBitmap->SetPalette(GetPaletteSpan());
-  pNewBitmap->SetAlphaMask(m_pAlphaMask, pClip);
   if (GetBPP() == 1 && rect.left % 8 != 0) {
     int left_shift = rect.left % 32;
     int right_shift = 32 - left_shift;
@@ -690,21 +689,6 @@
   }
 }
 
-bool CFX_DIBBase::BuildAlphaMask() {
-  if (m_pAlphaMask)
-    return true;
-
-  m_pAlphaMask = pdfium::MakeRetain<CFX_DIBitmap>();
-  if (!m_pAlphaMask->Create(m_Width, m_Height, FXDIB_Format::k8bppMask)) {
-    m_pAlphaMask = nullptr;
-    return false;
-  }
-  for (int i = 0; i < m_pAlphaMask->GetHeight(); ++i) {
-    fxcrt::spanset(m_pAlphaMask->GetWritableScanline(i), 0xff);
-  }
-  return true;
-}
-
 size_t CFX_DIBBase::GetRequiredPaletteSize() const {
   if (IsMaskFormat())
     return 0;
@@ -861,25 +845,23 @@
 }
 
 uint32_t CFX_DIBBase::GetAlphaMaskPitch() const {
-  return m_pAlphaMask ? m_pAlphaMask->GetPitch() : 0;
+  return 0;
 }
 
 pdfium::span<const uint8_t> CFX_DIBBase::GetAlphaMaskScanline(int line) const {
-  return m_pAlphaMask ? m_pAlphaMask->GetScanline(line)
-                      : pdfium::span<const uint8_t>();
+  return pdfium::span<const uint8_t>();
 }
 
 pdfium::span<uint8_t> CFX_DIBBase::GetWritableAlphaMaskScanline(int line) {
-  return m_pAlphaMask ? m_pAlphaMask->GetWritableScanline(line)
-                      : pdfium::span<uint8_t>();
+  return pdfium::span<uint8_t>();
 }
 
 pdfium::span<uint8_t> CFX_DIBBase::GetAlphaMaskBuffer() {
-  return m_pAlphaMask ? m_pAlphaMask->GetBuffer() : pdfium::span<uint8_t>();
+  return pdfium::span<uint8_t>();
 }
 
 RetainPtr<CFX_DIBitmap> CFX_DIBBase::GetAlphaMask() {
-  return m_pAlphaMask;
+  return nullptr;
 }
 
 RetainPtr<CFX_DIBitmap> CFX_DIBBase::CloneAlphaMask() const {
@@ -901,34 +883,6 @@
   return pMask;
 }
 
-bool CFX_DIBBase::SetAlphaMask(const RetainPtr<CFX_DIBBase>& pAlphaMask,
-                               const FX_RECT* pClip) {
-  if (!IsAlphaFormat() || GetFormat() == FXDIB_Format::kArgb)
-    return false;
-
-  if (!pAlphaMask) {
-    m_pAlphaMask->Clear(0xff000000);
-    return true;
-  }
-  FX_RECT rect(0, 0, pAlphaMask->m_Width, pAlphaMask->m_Height);
-  if (pClip) {
-    rect.Intersect(*pClip);
-    if (rect.IsEmpty() || rect.Width() != m_Width ||
-        rect.Height() != m_Height) {
-      return false;
-    }
-  } else {
-    if (pAlphaMask->m_Width != m_Width || pAlphaMask->m_Height != m_Height)
-      return false;
-  }
-  for (int row = 0; row < m_Height; ++row) {
-    memcpy(m_pAlphaMask->GetWritableScanline(row).data(),
-           pAlphaMask->GetScanline(row + rect.top).subspan(rect.left).data(),
-           m_pAlphaMask->m_Pitch);
-  }
-  return true;
-}
-
 RetainPtr<CFX_DIBitmap> CFX_DIBBase::FlipImage(bool bXFlip, bool bYFlip) const {
   auto pFlipped = pdfium::MakeRetain<CFX_DIBitmap>();
   if (!pFlipped->Create(m_Width, m_Height, GetFormat()))
@@ -979,26 +933,6 @@
       }
     }
   }
-  if (m_pAlphaMask) {
-    uint32_t dest_pitch = pFlipped->m_pAlphaMask->GetPitch();
-    for (int row = 0; row < m_Height; ++row) {
-      const uint8_t* src_scan = m_pAlphaMask->GetScanline(row).data();
-      uint8_t* dest_scan =
-          pFlipped->m_pAlphaMask
-              ->GetWritableScanline(bYFlip ? m_Height - row - 1 : row)
-              .data();
-      if (!bXFlip) {
-        memcpy(dest_scan, src_scan, dest_pitch);
-        continue;
-      }
-      dest_scan += (m_Width - 1);
-      for (int col = 0; col < m_Width; ++col) {
-        *dest_scan = *src_scan;
-        --dest_scan;
-        ++src_scan;
-      }
-    }
-  }
   return pFlipped;
 }
 
@@ -1012,19 +946,13 @@
 
   RetainPtr<CFX_DIBitmap> pSrcAlpha;
   if (IsAlphaFormat()) {
-    pSrcAlpha =
-        (GetFormat() == FXDIB_Format::kArgb) ? CloneAlphaMask() : m_pAlphaMask;
+    pSrcAlpha = CloneAlphaMask();
     if (!pSrcAlpha)
       return nullptr;
   }
   if (GetIsAlphaFromFormat(dest_format)) {
-    bool ret;
-    if (dest_format == FXDIB_Format::kArgb) {
-      ret = pSrcAlpha ? pClone->SetAlphaFromBitmap(pSrcAlpha)
-                      : pClone->SetUniformOpaqueAlpha();
-    } else {
-      ret = pClone->SetAlphaMask(pSrcAlpha, nullptr);
-    }
+    bool ret = pSrcAlpha ? pClone->SetAlphaFromBitmap(pSrcAlpha)
+                         : pClone->SetUniformOpaqueAlpha();
     if (!ret)
       return nullptr;
   }
@@ -1117,26 +1045,6 @@
       }
     }
   }
-  if (m_pAlphaMask) {
-    uint8_t* desk_mask_buf = pTransBitmap->m_pAlphaMask->GetBuffer().data();
-    const int dest_mask_pitch = pTransBitmap->m_pAlphaMask->GetPitch();
-    const int dest_mask_step = bYFlip ? -dest_mask_pitch : dest_mask_pitch;
-    const size_t dest_mask_last_row_offset =
-        Fx2DSizeOrDie(dest_mask_pitch, result_height - 1);
-    for (int row = row_start; row < row_end; ++row) {
-      int dest_col = (bXFlip ? dest_clip.right - (row - row_start) - 1 : row) -
-                     dest_clip.left;
-      uint8_t* desk_mask_scan = desk_mask_buf + dest_col;
-      if (bYFlip)
-        desk_mask_scan += dest_mask_last_row_offset;
-      const uint8_t* src_scan =
-          m_pAlphaMask->GetScanline(row).subspan(col_start).data();
-      for (int col = col_start; col < col_end; ++col) {
-        *desk_mask_scan = *src_scan++;
-        desk_mask_scan += dest_mask_step;
-      }
-    }
-  }
   return pTransBitmap;
 }
 
diff --git a/core/fxge/dib/cfx_dibbase.h b/core/fxge/dib/cfx_dibbase.h
index 52a56ac..d0b067e 100644
--- a/core/fxge/dib/cfx_dibbase.h
+++ b/core/fxge/dib/cfx_dibbase.h
@@ -75,17 +75,15 @@
   RetainPtr<CFX_DIBitmap> SwapXY(bool bXFlip, bool bYFlip) const;
   RetainPtr<CFX_DIBitmap> FlipImage(bool bXFlip, bool bYFlip) const;
 
-  bool HasAlphaMask() const { return !!m_pAlphaMask; }
+  // TODO(thestig): Remove these dummy methods.
+  bool HasAlphaMask() const { return false; }
   uint32_t GetAlphaMaskPitch() const;
   pdfium::span<const uint8_t> GetAlphaMaskScanline(int line) const;
   pdfium::span<uint8_t> GetWritableAlphaMaskScanline(int line);
   pdfium::span<uint8_t> GetAlphaMaskBuffer();
   RetainPtr<CFX_DIBitmap> GetAlphaMask();
-  RetainPtr<CFX_DIBitmap> CloneAlphaMask() const;
 
-  // Copies into internally-owned mask.
-  bool SetAlphaMask(const RetainPtr<CFX_DIBBase>& pAlphaMask,
-                    const FX_RECT* pClip);
+  RetainPtr<CFX_DIBitmap> CloneAlphaMask() const;
 
   bool GetOverlapRect(int& dest_left,
                       int& dest_top,
@@ -116,14 +114,12 @@
 
   RetainPtr<CFX_DIBitmap> ClipToInternal(const FX_RECT* pClip) const;
   void BuildPalette();
-  bool BuildAlphaMask();
   int FindPalette(uint32_t color) const;
 
   FXDIB_Format m_Format = FXDIB_Format::kInvalid;
   int m_Width = 0;
   int m_Height = 0;
   uint32_t m_Pitch = 0;
-  RetainPtr<CFX_DIBitmap> m_pAlphaMask;
   DataVector<uint32_t> m_palette;
 };
 
diff --git a/core/fxge/dib/cfx_dibextractor.cpp b/core/fxge/dib/cfx_dibextractor.cpp
index 582d933..1d2d920 100644
--- a/core/fxge/dib/cfx_dibextractor.cpp
+++ b/core/fxge/dib/cfx_dibextractor.cpp
@@ -23,7 +23,6 @@
     return;
   }
   m_pBitmap->SetPalette(pOldSrc->GetPaletteSpan());
-  m_pBitmap->SetAlphaMask(pOldSrc->GetAlphaMask(), nullptr);
 }
 
 CFX_DIBExtractor::~CFX_DIBExtractor() = default;
diff --git a/core/fxge/dib/cfx_dibitmap.cpp b/core/fxge/dib/cfx_dibitmap.cpp
index 375fd6c..03ca143 100644
--- a/core/fxge/dib/cfx_dibitmap.cpp
+++ b/core/fxge/dib/cfx_dibitmap.cpp
@@ -38,6 +38,8 @@
                           FXDIB_Format format,
                           uint8_t* pBuffer,
                           uint32_t pitch) {
+  DCHECK(!GetIsAlphaFromFormat(format) || format == FXDIB_Format::kArgb);
+
   m_pBuffer = nullptr;
   m_Format = format;
   m_Width = 0;
@@ -65,20 +67,7 @@
   m_Width = width;
   m_Height = height;
   m_Pitch = pitch_size.value().pitch;
-  if (!IsAlphaFormat() || format == FXDIB_Format::kArgb)
-    return true;
-
-  if (BuildAlphaMask())
-    return true;
-
-  if (pBuffer)
-    return true;
-
-  m_pBuffer = nullptr;
-  m_Width = 0;
-  m_Height = 0;
-  m_Pitch = 0;
-  return false;
+  return true;
 }
 
 bool CFX_DIBitmap::Copy(const RetainPtr<CFX_DIBBase>& pSrc) {
@@ -89,7 +78,6 @@
     return false;
 
   SetPalette(pSrc->GetPaletteSpan());
-  SetAlphaMask(pSrc->GetAlphaMask(), nullptr);
   for (int row = 0; row < pSrc->GetHeight(); row++) {
     memcpy(m_pBuffer.Get() + row * m_Pitch, pSrc->GetScanline(row).data(),
            m_Pitch);
@@ -127,9 +115,7 @@
 void CFX_DIBitmap::TakeOver(RetainPtr<CFX_DIBitmap>&& pSrcBitmap) {
   m_pBuffer = std::move(pSrcBitmap->m_pBuffer);
   m_palette = std::move(pSrcBitmap->m_palette);
-  m_pAlphaMask = pSrcBitmap->m_pAlphaMask;
   pSrcBitmap->m_pBuffer = nullptr;
-  pSrcBitmap->m_pAlphaMask = nullptr;
   m_Format = pSrcBitmap->m_Format;
   m_Width = pSrcBitmap->m_Width;
   m_Height = pSrcBitmap->m_Height;
@@ -364,10 +350,6 @@
     pSrcClone = pSrcMatched;
   }
   RetainPtr<CFX_DIBitmap> pDst(this);
-  if (destChannel == Channel::kAlpha && m_pAlphaMask) {
-    pDst = m_pAlphaMask;
-    destOffset = 0;
-  }
   int srcBytes = pSrcClone->GetBPP() / 8;
   int destBytes = pDst->GetBPP() / 8;
   for (int row = 0; row < m_Height; row++) {
@@ -409,10 +391,6 @@
     memset(m_pBuffer.Get(), 0xff, m_Height * m_Pitch);
     return true;
   }
-  if (m_pAlphaMask) {
-    fxcrt::spanset(m_pAlphaMask->GetBuffer(), 0xff);
-    return true;
-  }
   const int destOffset = GetFormat() == FXDIB_Format::kArgb ? 3 : 0;
   for (int row = 0; row < m_Height; row++) {
     uint8_t* scan_line = m_pBuffer.Get() + row * m_Pitch + destOffset;
@@ -463,22 +441,19 @@
         }
       }
     }
-  } else {
-    if (GetFormat() == FXDIB_Format::kArgb) {
-      if (pSrcClone->GetBPP() == 1)
-        return false;
+    return true;
+  }
 
-      for (int row = 0; row < m_Height; row++) {
-        uint8_t* dest_scan = m_pBuffer.Get() + m_Pitch * row + 3;
-        uint8_t* src_scan =
-            pSrcClone->m_pBuffer.Get() + pSrcClone->m_Pitch * row;
-        for (int col = 0; col < m_Width; col++) {
-          *dest_scan = (*dest_scan) * src_scan[col] / 255;
-          dest_scan += 4;
-        }
-      }
-    } else {
-      m_pAlphaMask->MultiplyAlpha(pSrcClone);
+  DCHECK_EQ(GetFormat(), FXDIB_Format::kArgb);
+  if (pSrcClone->GetBPP() == 1)
+    return false;
+
+  for (int row = 0; row < m_Height; row++) {
+    uint8_t* dest_scan = m_pBuffer.Get() + m_Pitch * row + 3;
+    uint8_t* src_scan = pSrcClone->m_pBuffer.Get() + pSrcClone->m_Pitch * row;
+    for (int col = 0; col < m_Width; col++) {
+      *dest_scan = (*dest_scan) * src_scan[col] / 255;
+      dest_scan += 4;
     }
   }
   return true;
@@ -515,14 +490,11 @@
       break;
     }
     default:
-      if (IsAlphaFormat()) {
-        m_pAlphaMask->MultiplyAlpha(alpha);
-      } else {
-        if (!ConvertFormat(FXDIB_Format::kArgb)) {
-          return false;
-        }
-        MultiplyAlpha(alpha);
+      DCHECK(!IsAlphaFormat());
+      if (!ConvertFormat(FXDIB_Format::kArgb)) {
+        return false;
       }
+      MultiplyAlpha(alpha);
       break;
   }
   return true;
@@ -792,10 +764,8 @@
         pSrcAlphaMask
             ? pSrcAlphaMask->GetScanline(src_top + row).subspan(src_left)
             : pdfium::span<const uint8_t>();
-    pdfium::span<uint8_t> dst_scan_extra_alpha =
-        m_pAlphaMask ? m_pAlphaMask->GetWritableScanline(dest_top + row)
-                           .subspan(dest_left)
-                     : pdfium::span<uint8_t>();
+    // TODO(thestig): This span is always empty. Check for more dead code.
+    pdfium::span<uint8_t> dst_scan_extra_alpha;
     pdfium::span<const uint8_t> clip_scan;
     if (pClipMask) {
       clip_scan = pClipMask->GetWritableScanline(dest_top + row - clip_box.top)
@@ -863,10 +833,8 @@
     pdfium::span<uint8_t> dest_scan =
         GetWritableScanline(dest_top + row).subspan(dest_left * Bpp);
     pdfium::span<const uint8_t> src_scan = pMask->GetScanline(src_top + row);
-    pdfium::span<uint8_t> dst_scan_extra_alpha =
-        m_pAlphaMask ? m_pAlphaMask->GetWritableScanline(dest_top + row)
-                           .subspan(dest_left)
-                     : pdfium::span<uint8_t>();
+    // TODO(thestig): This span is always empty. Check for more dead code.
+    pdfium::span<uint8_t> dst_scan_extra_alpha;
     pdfium::span<const uint8_t> clip_scan;
     if (pClipMask) {
       clip_scan = pClipMask->GetScanline(dest_top + row - clip_box.top)
@@ -967,18 +935,14 @@
 
   color_p[3] = static_cast<uint8_t>(src_alpha);
   int Bpp = GetBppFromFormat(m_Format) / 8;
-  bool bAlpha = IsAlphaFormat();
-  bool bArgb = GetFormat() == FXDIB_Format::kArgb;
+  const bool bAlpha = IsAlphaFormat();
+  if (bAlpha) {
+    // Other formats with alpha have already been handled above.
+    DCHECK_EQ(GetFormat(), FXDIB_Format::kArgb);
+  }
   if (src_alpha == 255) {
     for (int row = rect.top; row < rect.bottom; row++) {
       uint8_t* dest_scan = m_pBuffer.Get() + row * m_Pitch + rect.left * Bpp;
-      uint8_t* dest_scan_alpha =
-          m_pAlphaMask
-              ? m_pAlphaMask->GetWritableScanline(row).subspan(rect.left).data()
-              : nullptr;
-      if (dest_scan_alpha)
-        memset(dest_scan_alpha, 0xff, width);
-
       if (Bpp == 4) {
         uint32_t* scan = reinterpret_cast<uint32_t*>(dest_scan);
         for (int col = 0; col < width; col++)
@@ -996,47 +960,24 @@
   for (int row = rect.top; row < rect.bottom; row++) {
     uint8_t* dest_scan = m_pBuffer.Get() + row * m_Pitch + rect.left * Bpp;
     if (bAlpha) {
-      if (bArgb) {
-        for (int col = 0; col < width; col++) {
-          uint8_t back_alpha = dest_scan[3];
-          if (back_alpha == 0) {
-            FXARGB_SETDIB(dest_scan, ArgbEncode(src_alpha, color_p[2],
-                                                color_p[1], color_p[0]));
-            dest_scan += 4;
-            continue;
-          }
-          uint8_t dest_alpha =
-              back_alpha + src_alpha - back_alpha * src_alpha / 255;
-          int alpha_ratio = src_alpha * 255 / dest_alpha;
-          *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, color_p[0], alpha_ratio);
-          dest_scan++;
-          *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, color_p[1], alpha_ratio);
-          dest_scan++;
-          *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, color_p[2], alpha_ratio);
-          dest_scan++;
-          *dest_scan++ = dest_alpha;
+      for (int col = 0; col < width; col++) {
+        uint8_t back_alpha = dest_scan[3];
+        if (back_alpha == 0) {
+          FXARGB_SETDIB(dest_scan, ArgbEncode(src_alpha, color_p[2], color_p[1],
+                                              color_p[0]));
+          dest_scan += 4;
+          continue;
         }
-      } else {
-        uint8_t* dest_scan_alpha =
-            m_pAlphaMask->GetWritableScanline(row).subspan(rect.left).data();
-        for (int col = 0; col < width; col++) {
-          uint8_t back_alpha = *dest_scan_alpha;
-          if (back_alpha == 0) {
-            *dest_scan_alpha++ = src_alpha;
-            memcpy(dest_scan, color_p, Bpp);
-            dest_scan += Bpp;
-            continue;
-          }
-          uint8_t dest_alpha =
-              back_alpha + src_alpha - back_alpha * src_alpha / 255;
-          *dest_scan_alpha++ = dest_alpha;
-          int alpha_ratio = src_alpha * 255 / dest_alpha;
-          for (int comps = 0; comps < Bpp; comps++) {
-            *dest_scan =
-                FXDIB_ALPHA_MERGE(*dest_scan, color_p[comps], alpha_ratio);
-            dest_scan++;
-          }
-        }
+        uint8_t dest_alpha =
+            back_alpha + src_alpha - back_alpha * src_alpha / 255;
+        int alpha_ratio = src_alpha * 255 / dest_alpha;
+        *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, color_p[0], alpha_ratio);
+        dest_scan++;
+        *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, color_p[1], alpha_ratio);
+        dest_scan++;
+        *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, color_p[2], alpha_ratio);
+        dest_scan++;
+        *dest_scan++ = dest_alpha;
       }
     } else {
       for (int col = 0; col < width; col++) {
@@ -1055,6 +996,10 @@
 }
 
 bool CFX_DIBitmap::ConvertFormat(FXDIB_Format dest_format) {
+  DCHECK(dest_format == FXDIB_Format::k8bppMask ||
+         dest_format == FXDIB_Format::kArgb ||
+         dest_format == kPlatformRGBFormat);
+
   if (dest_format == m_Format)
     return true;
 
@@ -1082,43 +1027,16 @@
   if (!dest_buf)
     return false;
 
-  RetainPtr<CFX_DIBitmap> pAlphaMask;
   if (dest_format == FXDIB_Format::kArgb) {
     memset(dest_buf.get(), 0xff, dest_buf_size);
-    if (m_pAlphaMask) {
-      for (int row = 0; row < m_Height; row++) {
-        uint8_t* pDstScanline = dest_buf.get() + row * dest_pitch + 3;
-        const uint8_t* pSrcScanline = m_pAlphaMask->GetScanline(row).data();
-        for (int col = 0; col < m_Width; col++) {
-          *pDstScanline = *pSrcScanline++;
-          pDstScanline += 4;
-        }
-      }
-    }
-  } else if (GetIsAlphaFromFormat(dest_format)) {
-    if (m_Format == FXDIB_Format::kArgb) {
-      pAlphaMask = CloneAlphaMask();
-      if (!pAlphaMask)
-        return false;
-    } else {
-      if (!m_pAlphaMask) {
-        if (!BuildAlphaMask())
-          return false;
-        pAlphaMask = std::move(m_pAlphaMask);
-      } else {
-        pAlphaMask = m_pAlphaMask;
-      }
-    }
   }
-  bool ret = false;
   RetainPtr<CFX_DIBBase> holder(this);
   DataVector<uint32_t> pal_8bpp;
-  ret = ConvertBuffer(dest_format, {dest_buf.get(), dest_buf_size}, dest_pitch,
-                      m_Width, m_Height, holder, 0, 0, &pal_8bpp);
-  if (!ret)
+  if (!ConvertBuffer(dest_format, {dest_buf.get(), dest_buf_size}, dest_pitch,
+                     m_Width, m_Height, holder, 0, 0, &pal_8bpp)) {
     return false;
+  }
 
-  m_pAlphaMask = pAlphaMask;
   m_palette = std::move(pal_8bpp);
   m_pBuffer = std::move(dest_buf);
   m_Format = dest_format;