Make GetEstimatedImageSize() a method of CFX_DIBBase

Rename to GetEstimatedImageMemoryBurden() since the result is
used in cache limit calculations. Allow the base class to return
the size of the palette for better calculations when there is no
buffer. This is a second step closer to moving to a world where
GetBuffer() is only called on CFX_DIBitmap, not the base class.

Change-Id: Id01c148fbfe204753eba0ecd0566df101e606043
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/85691
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_dib.cpp b/core/fpdfapi/page/cpdf_dib.cpp
index 95a09f8..c87e196 100644
--- a/core/fpdfapi/page/cpdf_dib.cpp
+++ b/core/fpdfapi/page/cpdf_dib.cpp
@@ -1196,6 +1196,10 @@
   return m_pDecoder && m_pDecoder->SkipToScanline(line, pPause);
 }
 
+uint32_t CPDF_DIB::GetEstimatedImageMemoryBurden() const {
+  return m_pCachedBitmap ? m_pCachedBitmap->GetEstimatedImageMemoryBurden() : 0;
+}
+
 bool CPDF_DIB::TransMask() const {
   return m_bLoadMask && m_GroupFamily == CPDF_ColorSpace::Family::kDeviceCMYK &&
          m_Family == CPDF_ColorSpace::Family::kDeviceCMYK;
diff --git a/core/fpdfapi/page/cpdf_dib.h b/core/fpdfapi/page/cpdf_dib.h
index a13d571..ae9e8c3 100644
--- a/core/fpdfapi/page/cpdf_dib.h
+++ b/core/fpdfapi/page/cpdf_dib.h
@@ -45,9 +45,10 @@
   bool Load(CPDF_Document* pDoc, const CPDF_Stream* pStream);
 
   // CFX_DIBBase:
-  bool SkipToScanline(int line, PauseIndicatorIface* pPause) const override;
   uint8_t* GetBuffer() const override;
   pdfium::span<const uint8_t> GetScanline(int line) const override;
+  bool SkipToScanline(int line, PauseIndicatorIface* pPause) const override;
+  uint32_t GetEstimatedImageMemoryBurden() const override;
 
   RetainPtr<CPDF_ColorSpace> GetColorSpace() const { return m_pColorSpace; }
   uint32_t GetMatteColor() const { return m_MatteColor; }
diff --git a/core/fpdfapi/render/cpdf_imagecacheentry.cpp b/core/fpdfapi/render/cpdf_imagecacheentry.cpp
index b170f29..840505c 100644
--- a/core/fpdfapi/render/cpdf_imagecacheentry.cpp
+++ b/core/fpdfapi/render/cpdf_imagecacheentry.cpp
@@ -18,22 +18,6 @@
 #include "core/fpdfapi/render/cpdf_rendercontext.h"
 #include "core/fpdfapi/render/cpdf_renderstatus.h"
 #include "core/fxge/dib/cfx_dibitmap.h"
-#include "third_party/base/check.h"
-#include "third_party/base/numerics/safe_conversions.h"
-
-namespace {
-
-uint32_t GetEstimatedImageSize(const RetainPtr<CFX_DIBBase>& pDIB) {
-  if (!pDIB || !pDIB->GetBuffer())
-    return 0;
-
-  int height = pDIB->GetHeight();
-  DCHECK(pdfium::base::IsValueInRangeForNumericType<uint32_t>(height));
-  return static_cast<uint32_t>(height) * pDIB->GetPitch() +
-         pDIB->GetRequiredPaletteSize() * 4;
-}
-
-}  // namespace
 
 CPDF_ImageCacheEntry::CPDF_ImageCacheEntry(CPDF_Document* pDoc,
                                            const RetainPtr<CPDF_Image>& pImage)
@@ -116,6 +100,9 @@
 }
 
 void CPDF_ImageCacheEntry::CalcSize() {
-  m_dwCacheSize = GetEstimatedImageSize(m_pCachedBitmap) +
-                  GetEstimatedImageSize(m_pCachedMask);
+  m_dwCacheSize = 0;
+  if (m_pCachedBitmap)
+    m_dwCacheSize += m_pCachedBitmap->GetEstimatedImageMemoryBurden();
+  if (m_pCachedMask)
+    m_dwCacheSize += m_pCachedMask->GetEstimatedImageMemoryBurden();
 }
diff --git a/core/fxge/dib/cfx_dibbase.cpp b/core/fxge/dib/cfx_dibbase.cpp
index 4eb0075..2bfae7a 100644
--- a/core/fxge/dib/cfx_dibbase.cpp
+++ b/core/fxge/dib/cfx_dibbase.cpp
@@ -612,6 +612,10 @@
   return false;
 }
 
+uint32_t CFX_DIBBase::GetEstimatedImageMemoryBurden() const {
+  return GetRequiredPaletteSize() * sizeof(uint32_t);
+}
+
 RetainPtr<CFX_DIBitmap> CFX_DIBBase::Clone(const FX_RECT* pClip) const {
   FX_RECT rect(0, 0, m_Width, m_Height);
   if (pClip) {
diff --git a/core/fxge/dib/cfx_dibbase.h b/core/fxge/dib/cfx_dibbase.h
index 705c3bf..8aee458 100644
--- a/core/fxge/dib/cfx_dibbase.h
+++ b/core/fxge/dib/cfx_dibbase.h
@@ -35,6 +35,7 @@
   virtual uint8_t* GetBuffer() const;
   virtual pdfium::span<const uint8_t> GetScanline(int line) const = 0;
   virtual bool SkipToScanline(int line, PauseIndicatorIface* pPause) const;
+  virtual uint32_t GetEstimatedImageMemoryBurden() const;
 
   pdfium::span<uint8_t> GetWritableScanline(int line) {
     pdfium::span<const uint8_t> src = GetScanline(line);
diff --git a/core/fxge/dib/cfx_dibitmap.cpp b/core/fxge/dib/cfx_dibitmap.cpp
index 96e3d49..d39f7f9 100644
--- a/core/fxge/dib/cfx_dibitmap.cpp
+++ b/core/fxge/dib/cfx_dibitmap.cpp
@@ -18,8 +18,10 @@
 #include "core/fxcrt/fx_safe_types.h"
 #include "core/fxge/cfx_cliprgn.h"
 #include "core/fxge/dib/cfx_scanlinecompositor.h"
+#include "third_party/base/check.h"
 #include "third_party/base/check_op.h"
 #include "third_party/base/notreached.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 CFX_DIBitmap::CFX_DIBitmap() = default;
 
@@ -104,6 +106,16 @@
   return {m_pBuffer.Get() + line * m_Pitch, m_Pitch};
 }
 
+uint32_t CFX_DIBitmap::GetEstimatedImageMemoryBurden() const {
+  uint32_t result = CFX_DIBBase::GetEstimatedImageMemoryBurden();
+  if (GetBuffer()) {
+    int height = GetHeight();
+    DCHECK(pdfium::base::IsValueInRangeForNumericType<uint32_t>(height));
+    result += static_cast<uint32_t>(height) * GetPitch();
+  }
+  return result;
+}
+
 void CFX_DIBitmap::TakeOver(RetainPtr<CFX_DIBitmap>&& pSrcBitmap) {
   m_pBuffer = std::move(pSrcBitmap->m_pBuffer);
   m_palette = std::move(pSrcBitmap->m_palette);
diff --git a/core/fxge/dib/cfx_dibitmap.h b/core/fxge/dib/cfx_dibitmap.h
index c3d9eb0..07c84c8 100644
--- a/core/fxge/dib/cfx_dibitmap.h
+++ b/core/fxge/dib/cfx_dibitmap.h
@@ -35,6 +35,7 @@
   // CFX_DIBBase
   uint8_t* GetBuffer() const override;
   pdfium::span<const uint8_t> GetScanline(int line) const override;
+  uint32_t GetEstimatedImageMemoryBurden() const override;
 
   void TakeOver(RetainPtr<CFX_DIBitmap>&& pSrcBitmap);
   bool ConvertFormat(FXDIB_Format format);