xref: /aosp_15_r20/external/pdfium/core/fpdfapi/render/cpdf_scaledrenderbuffer.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2016 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fpdfapi/render/cpdf_scaledrenderbuffer.h"
8 
9 #include "core/fpdfapi/parser/cpdf_dictionary.h"
10 #include "core/fpdfapi/render/cpdf_devicebuffer.h"
11 #include "core/fpdfapi/render/cpdf_rendercontext.h"
12 #include "core/fxge/cfx_defaultrenderdevice.h"
13 #include "core/fxge/dib/cfx_dibitmap.h"
14 
15 namespace {
16 
17 constexpr size_t kImageSizeLimitBytes = 30 * 1024 * 1024;
18 
19 }  // namespace
20 
21 CPDF_ScaledRenderBuffer::CPDF_ScaledRenderBuffer() = default;
22 
23 CPDF_ScaledRenderBuffer::~CPDF_ScaledRenderBuffer() = default;
24 
Initialize(CPDF_RenderContext * pContext,CFX_RenderDevice * pDevice,const FX_RECT & rect,const CPDF_PageObject * pObj,const CPDF_RenderOptions * pOptions,int max_dpi)25 bool CPDF_ScaledRenderBuffer::Initialize(CPDF_RenderContext* pContext,
26                                          CFX_RenderDevice* pDevice,
27                                          const FX_RECT& rect,
28                                          const CPDF_PageObject* pObj,
29                                          const CPDF_RenderOptions* pOptions,
30                                          int max_dpi) {
31   m_pDevice = pDevice;
32   if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_GET_BITS)
33     return true;
34 
35   m_Rect = rect;
36   m_Matrix = CPDF_DeviceBuffer::CalculateMatrix(pDevice, rect, max_dpi,
37                                                 /*scale=*/true);
38   m_pBitmapDevice = std::make_unique<CFX_DefaultRenderDevice>();
39   bool bIsAlpha =
40       !!(m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_ALPHA_OUTPUT);
41   FXDIB_Format dibFormat = bIsAlpha ? FXDIB_Format::kArgb : FXDIB_Format::kRgb;
42   while (true) {
43     FX_RECT bitmap_rect =
44         m_Matrix.TransformRect(CFX_FloatRect(rect)).GetOuterRect();
45     int32_t width = bitmap_rect.Width();
46     int32_t height = bitmap_rect.Height();
47     // Set to 0 to make CalculatePitchAndSize() calculate it.
48     constexpr uint32_t kNoPitch = 0;
49     absl::optional<CFX_DIBitmap::PitchAndSize> pitch_size =
50         CFX_DIBitmap::CalculatePitchAndSize(width, height, dibFormat, kNoPitch);
51     if (!pitch_size.has_value())
52       return false;
53 
54     if (pitch_size.value().size <= kImageSizeLimitBytes &&
55         m_pBitmapDevice->Create(width, height, dibFormat, nullptr)) {
56       break;
57     }
58     m_Matrix.Scale(0.5f, 0.5f);
59   }
60   pContext->GetBackground(m_pBitmapDevice->GetBitmap(), pObj, pOptions,
61                           m_Matrix);
62   return true;
63 }
64 
GetDevice() const65 CFX_RenderDevice* CPDF_ScaledRenderBuffer::GetDevice() const {
66   return m_pBitmapDevice ? static_cast<CFX_RenderDevice*>(m_pBitmapDevice.get())
67                          : m_pDevice.get();
68 }
69 
OutputToDevice()70 void CPDF_ScaledRenderBuffer::OutputToDevice() {
71   if (m_pBitmapDevice) {
72     m_pDevice->StretchDIBits(m_pBitmapDevice->GetBitmap(), m_Rect.left,
73                              m_Rect.top, m_Rect.Width(), m_Rect.Height());
74   }
75 }
76