Simplify RenderDeviceDriverIface::GetClipBox()

Return the rect direct instead of via an out-parameter. Avoid wrapping
the rect in std::optional because most implementations do not fail. For
the ones that can fail, return the entire size of the underlying device.
This is consistent with how the callers would handle the failure case.
Now the callers do not have to.

Also mark GetClipBox() as const.

Change-Id: Ia36fa94024c1529d00604e9fcb712802655892e3
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/121240
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Tom Sepez <tsepez@google.com>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxge/agg/fx_agg_driver.cpp b/core/fxge/agg/fx_agg_driver.cpp
index a578b90..92d93ac 100644
--- a/core/fxge/agg/fx_agg_driver.cpp
+++ b/core/fxge/agg/fx_agg_driver.cpp
@@ -1259,8 +1259,7 @@
   if (m_pBitmap->GetBuffer().empty())
     return true;
 
-  FX_RECT clip_rect;
-  GetClipBox(&clip_rect);
+  FX_RECT clip_rect = GetClipBox();
   FX_RECT draw_rect = clip_rect;
   draw_rect.Intersect(rect);
   if (draw_rect.IsEmpty())
@@ -1285,15 +1284,12 @@
   return true;
 }
 
-bool CFX_AggDeviceDriver::GetClipBox(FX_RECT* pRect) {
-  if (!m_pClipRgn) {
-    pRect->left = pRect->top = 0;
-    pRect->right = GetDeviceCaps(FXDC_PIXEL_WIDTH);
-    pRect->bottom = GetDeviceCaps(FXDC_PIXEL_HEIGHT);
-    return true;
+FX_RECT CFX_AggDeviceDriver::GetClipBox() const {
+  if (m_pClipRgn) {
+    return m_pClipRgn->GetBox();
   }
-  *pRect = m_pClipRgn->GetBox();
-  return true;
+  return FX_RECT(0, 0, GetDeviceCaps(FXDC_PIXEL_WIDTH),
+                 GetDeviceCaps(FXDC_PIXEL_HEIGHT));
 }
 
 bool CFX_AggDeviceDriver::GetDIBits(RetainPtr<CFX_DIBitmap> bitmap,
diff --git a/core/fxge/agg/fx_agg_driver.h b/core/fxge/agg/fx_agg_driver.h
index fb4ef84..fb7795a 100644
--- a/core/fxge/agg/fx_agg_driver.h
+++ b/core/fxge/agg/fx_agg_driver.h
@@ -62,7 +62,7 @@
   bool FillRectWithBlend(const FX_RECT& rect,
                          uint32_t fill_color,
                          BlendMode blend_type) override;
-  bool GetClipBox(FX_RECT* pRect) override;
+  FX_RECT GetClipBox() const override;
   bool GetDIBits(RetainPtr<CFX_DIBitmap> bitmap,
                  int left,
                  int top) const override;
diff --git a/core/fxge/cfx_renderdevice.cpp b/core/fxge/cfx_renderdevice.cpp
index 463e1e2..6b247b5 100644
--- a/core/fxge/cfx_renderdevice.cpp
+++ b/core/fxge/cfx_renderdevice.cpp
@@ -522,12 +522,7 @@
   m_bpp = m_pDeviceDriver->GetDeviceCaps(FXDC_BITS_PIXEL);
   m_RenderCaps = m_pDeviceDriver->GetDeviceCaps(FXDC_RENDER_CAPS);
   m_DeviceType = m_pDeviceDriver->GetDeviceType();
-  if (!m_pDeviceDriver->GetClipBox(&m_ClipBox)) {
-    m_ClipBox.left = 0;
-    m_ClipBox.top = 0;
-    m_ClipBox.right = m_Width;
-    m_ClipBox.bottom = m_Height;
-  }
+  m_ClipBox = m_pDeviceDriver->GetClipBox();
 }
 
 void CFX_RenderDevice::SaveState() {
@@ -604,12 +599,7 @@
 }
 
 void CFX_RenderDevice::UpdateClipBox() {
-  if (m_pDeviceDriver->GetClipBox(&m_ClipBox))
-    return;
-  m_ClipBox.left = 0;
-  m_ClipBox.top = 0;
-  m_ClipBox.right = m_Width;
-  m_ClipBox.bottom = m_Height;
+  m_ClipBox = m_pDeviceDriver->GetClipBox();
 }
 
 bool CFX_RenderDevice::DrawPath(const CFX_Path& path,
diff --git a/core/fxge/renderdevicedriver_iface.h b/core/fxge/renderdevicedriver_iface.h
index 9133a5f..3dd6db3 100644
--- a/core/fxge/renderdevicedriver_iface.h
+++ b/core/fxge/renderdevicedriver_iface.h
@@ -67,7 +67,7 @@
                                 uint32_t color,
                                 BlendMode blend_type);
 
-  virtual bool GetClipBox(FX_RECT* pRect) = 0;
+  virtual FX_RECT GetClipBox() const = 0;
   virtual bool GetDIBits(RetainPtr<CFX_DIBitmap> bitmap,
                          int left,
                          int top) const;
diff --git a/core/fxge/skia/fx_skia_device.cpp b/core/fxge/skia/fx_skia_device.cpp
index 77fd5e0..349cff7 100644
--- a/core/fxge/skia/fx_skia_device.cpp
+++ b/core/fxge/skia/fx_skia_device.cpp
@@ -125,10 +125,9 @@
          local.fBottom);
   printf("device bounds %d %d %d %d\n", device.fLeft, device.fTop,
          device.fRight, device.fBottom);
-  FX_RECT clipBox;
-  driver->GetClipBox(&clipBox);
-  printf("reported bounds %d %d %d %d\n", clipBox.left, clipBox.top,
-         clipBox.right, clipBox.bottom);
+  FX_RECT clip_box = driver->GetClipBox();
+  printf("reported bounds %d %d %d %d\n", clip_box.left, clip_box.top,
+         clip_box.right, clip_box.bottom);
 #endif  // SHOW_SKIA_PATH
 }
 
@@ -1360,13 +1359,9 @@
   return true;
 }
 
-bool CFX_SkiaDeviceDriver::GetClipBox(FX_RECT* pRect) {
+FX_RECT CFX_SkiaDeviceDriver::GetClipBox() const {
   SkIRect clip = m_pCanvas->getDeviceClipBounds();
-  pRect->left = clip.fLeft;
-  pRect->top = clip.fTop;
-  pRect->right = clip.fRight;
-  pRect->bottom = clip.fBottom;
-  return true;
+  return FX_RECT(clip.fLeft, clip.fTop, clip.fRight, clip.fBottom);
 }
 
 bool CFX_SkiaDeviceDriver::GetDIBits(RetainPtr<CFX_DIBitmap> bitmap,
diff --git a/core/fxge/skia/fx_skia_device.h b/core/fxge/skia/fx_skia_device.h
index 8050958..56db082 100644
--- a/core/fxge/skia/fx_skia_device.h
+++ b/core/fxge/skia/fx_skia_device.h
@@ -86,7 +86,7 @@
                         uint32_t color,
                         BlendMode blend_type) override;
 
-  bool GetClipBox(FX_RECT* pRect) override;
+  FX_RECT GetClipBox() const override;
 
   /** Load device buffer into a DIB */
   bool GetDIBits(RetainPtr<CFX_DIBitmap> bitmap,
diff --git a/core/fxge/win32/cfx_psrenderer.h b/core/fxge/win32/cfx_psrenderer.h
index 8b31994..143f01b 100644
--- a/core/fxge/win32/cfx_psrenderer.h
+++ b/core/fxge/win32/cfx_psrenderer.h
@@ -69,7 +69,7 @@
   void SetClip_PathStroke(const CFX_Path& path,
                           const CFX_Matrix* pObject2Device,
                           const CFX_GraphStateData* pGraphState);
-  FX_RECT GetClipBox() { return m_ClipBox; }
+  FX_RECT GetClipBox() const { return m_ClipBox; }
   bool DrawPath(const CFX_Path& path,
                 const CFX_Matrix* pObject2Device,
                 const CFX_GraphStateData* pGraphState,
diff --git a/core/fxge/win32/cgdi_device_driver.cpp b/core/fxge/win32/cgdi_device_driver.cpp
index 024b696..979392a 100644
--- a/core/fxge/win32/cgdi_device_driver.cpp
+++ b/core/fxge/win32/cgdi_device_driver.cpp
@@ -551,8 +551,12 @@
   return true;
 }
 
-bool CGdiDeviceDriver::GetClipBox(FX_RECT* pRect) {
-  return !!(::GetClipBox(m_hDC, (RECT*)pRect));
+FX_RECT CGdiDeviceDriver::GetClipBox() const {
+  FX_RECT rect;
+  if (::GetClipBox(m_hDC, reinterpret_cast<RECT*>(&rect))) {
+    return rect;
+  }
+  return FX_RECT(0, 0, m_Width, m_Height);
 }
 
 bool CGdiDeviceDriver::MultiplyAlpha(float alpha) {
diff --git a/core/fxge/win32/cgdi_device_driver.h b/core/fxge/win32/cgdi_device_driver.h
index ef837f4..94c3b23 100644
--- a/core/fxge/win32/cgdi_device_driver.h
+++ b/core/fxge/win32/cgdi_device_driver.h
@@ -47,7 +47,7 @@
                         const CFX_PointF& ptLineTo,
                         uint32_t color,
                         BlendMode blend_type) override;
-  bool GetClipBox(FX_RECT* pRect) override;
+  FX_RECT GetClipBox() const override;
   bool MultiplyAlpha(float alpha) override;
   bool MultiplyAlphaMask(RetainPtr<const CFX_DIBitmap> mask) override;
 
diff --git a/core/fxge/win32/cps_printer_driver.cpp b/core/fxge/win32/cps_printer_driver.cpp
index 709a98a..69d5cb2 100644
--- a/core/fxge/win32/cps_printer_driver.cpp
+++ b/core/fxge/win32/cps_printer_driver.cpp
@@ -154,9 +154,8 @@
                                stroke_color, fill_options);
 }
 
-bool CPSPrinterDriver::GetClipBox(FX_RECT* pRect) {
-  *pRect = m_PSRenderer.GetClipBox();
-  return true;
+FX_RECT CPSPrinterDriver::GetClipBox() const {
+  return m_PSRenderer.GetClipBox();
 }
 
 bool CPSPrinterDriver::SetDIBits(RetainPtr<const CFX_DIBBase> bitmap,
diff --git a/core/fxge/win32/cps_printer_driver.h b/core/fxge/win32/cps_printer_driver.h
index dee5d7b..f57cb24 100644
--- a/core/fxge/win32/cps_printer_driver.h
+++ b/core/fxge/win32/cps_printer_driver.h
@@ -44,7 +44,7 @@
                 uint32_t stroke_color,
                 const CFX_FillRenderOptions& fill_options,
                 BlendMode blend_type) override;
-  bool GetClipBox(FX_RECT* pRect) override;
+  FX_RECT GetClipBox() const override;
   bool SetDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                  uint32_t color,
                  const FX_RECT& src_rect,
diff --git a/core/fxge/win32/ctext_only_printer_driver.cpp b/core/fxge/win32/ctext_only_printer_driver.cpp
index 5e5e64b..0b2f171 100644
--- a/core/fxge/win32/ctext_only_printer_driver.cpp
+++ b/core/fxge/win32/ctext_only_printer_driver.cpp
@@ -93,12 +93,8 @@
   return false;
 }
 
-bool CTextOnlyPrinterDriver::GetClipBox(FX_RECT* pRect) {
-  pRect->left = 0;
-  pRect->right = m_Width;
-  pRect->top = 0;
-  pRect->bottom = m_Height;
-  return true;
+FX_RECT CTextOnlyPrinterDriver::GetClipBox() const {
+  return FX_RECT(0, 0, m_Width, m_Height);
 }
 
 bool CTextOnlyPrinterDriver::StretchDIBits(RetainPtr<const CFX_DIBBase> bitmap,
diff --git a/core/fxge/win32/ctext_only_printer_driver.h b/core/fxge/win32/ctext_only_printer_driver.h
index 3c2754d..edefd8e 100644
--- a/core/fxge/win32/ctext_only_printer_driver.h
+++ b/core/fxge/win32/ctext_only_printer_driver.h
@@ -36,7 +36,7 @@
                 uint32_t stroke_color,
                 const CFX_FillRenderOptions& fill_options,
                 BlendMode blend_type) override;
-  bool GetClipBox(FX_RECT* pRect) override;
+  FX_RECT GetClipBox() const override;
   bool SetDIBits(RetainPtr<const CFX_DIBBase> bitmap,
                  uint32_t color,
                  const FX_RECT& src_rect,