Combine CPDF_DeviceBuffer::Initialize() and GetBuffer()
Change CPDF_DeviceBuffer::Initialize() to return the initialized bitmap
on success, and declare this as part of the API contract. Then the
caller can just check if the returned bitmap pointer is null or not, and
does not have to second guess if the bitmap has a buffer.
Bug: pdfium:2047
Change-Id: I15300c7be1a0447ebcd9983249f8279f8dae1253
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/111992
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/render/cpdf_devicebuffer.cpp b/core/fpdfapi/render/cpdf_devicebuffer.cpp
index 61611b2..e67adef 100644
--- a/core/fpdfapi/render/cpdf_devicebuffer.cpp
+++ b/core/fpdfapi/render/cpdf_devicebuffer.cpp
@@ -64,11 +64,14 @@
CPDF_DeviceBuffer::~CPDF_DeviceBuffer() = default;
-bool CPDF_DeviceBuffer::Initialize() {
+RetainPtr<CFX_DIBitmap> CPDF_DeviceBuffer::Initialize() {
FX_RECT bitmap_rect =
m_Matrix.TransformRect(CFX_FloatRect(m_Rect)).GetOuterRect();
- return m_pBitmap->Create(bitmap_rect.Width(), bitmap_rect.Height(),
- FXDIB_Format::kArgb);
+ if (!m_pBitmap->Create(bitmap_rect.Width(), bitmap_rect.Height(),
+ FXDIB_Format::kArgb)) {
+ return nullptr;
+ }
+ return m_pBitmap;
}
void CPDF_DeviceBuffer::OutputToDevice() {
diff --git a/core/fpdfapi/render/cpdf_devicebuffer.h b/core/fpdfapi/render/cpdf_devicebuffer.h
index 1084465..98bf439 100644
--- a/core/fpdfapi/render/cpdf_devicebuffer.h
+++ b/core/fpdfapi/render/cpdf_devicebuffer.h
@@ -30,9 +30,10 @@
int max_dpi);
~CPDF_DeviceBuffer();
- bool Initialize();
+ // On success, the returned bitmap will already have its buffer allocated.
+ // On failure, the returned result is null.
+ [[nodiscard]] RetainPtr<CFX_DIBitmap> Initialize();
void OutputToDevice();
- RetainPtr<CFX_DIBitmap> GetBitmap() const { return m_pBitmap; }
const CFX_Matrix& GetMatrix() const { return m_Matrix; }
private:
diff --git a/core/fpdfapi/render/cpdf_rendershading.cpp b/core/fpdfapi/render/cpdf_rendershading.cpp
index bce967f..a0965aa 100644
--- a/core/fpdfapi/render/cpdf_rendershading.cpp
+++ b/core/fpdfapi/render/cpdf_rendershading.cpp
@@ -914,12 +914,10 @@
return;
}
CPDF_DeviceBuffer buffer(pContext, pDevice, clip_rect_bbox, pCurObj, 150);
- if (!buffer.Initialize())
+ RetainPtr<CFX_DIBitmap> pBitmap = buffer.Initialize();
+ if (!pBitmap) {
return;
-
- RetainPtr<CFX_DIBitmap> pBitmap = buffer.GetBitmap();
- if (pBitmap->GetBuffer().empty())
- return;
+ }
if (background != 0) {
pBitmap->Clear(background);