xref: /aosp_15_r20/external/pdfium/core/fxge/cfx_renderdevice.h (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 #ifndef CORE_FXGE_CFX_RENDERDEVICE_H_
8 #define CORE_FXGE_CFX_RENDERDEVICE_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "build/build_config.h"
14 #include "core/fxcrt/fx_coordinates.h"
15 #include "core/fxcrt/retain_ptr.h"
16 #include "core/fxcrt/unowned_ptr.h"
17 #include "core/fxge/cfx_path.h"
18 #include "core/fxge/dib/fx_dib.h"
19 #include "core/fxge/render_defines.h"
20 #include "core/fxge/renderdevicedriver_iface.h"
21 #include "third_party/base/containers/span.h"
22 
23 class CFX_DIBBase;
24 class CFX_DIBitmap;
25 class CFX_Font;
26 class CFX_GraphStateData;
27 class CFX_ImageRenderer;
28 class PauseIndicatorIface;
29 class TextCharPos;
30 struct CFX_Color;
31 struct CFX_FillRenderOptions;
32 struct CFX_TextRenderOptions;
33 
34 enum class BorderStyle { kSolid, kDash, kBeveled, kInset, kUnderline };
35 
36 // Base class for all render devices. Derived classes must call
37 // SetDeviceDriver() to fully initialize the class. Until then, class methods
38 // are not safe to call, or may return invalid results.
39 class CFX_RenderDevice {
40  public:
41   class StateRestorer {
42    public:
43     explicit StateRestorer(CFX_RenderDevice* pDevice);
44     ~StateRestorer();
45 
46    private:
47     UnownedPtr<CFX_RenderDevice> m_pDevice;
48   };
49 
50   virtual ~CFX_RenderDevice();
51 
52   static CFX_Matrix GetFlipMatrix(float width,
53                                   float height,
54                                   float left,
55                                   float top);
56 
57   void SaveState();
58   void RestoreState(bool bKeepSaved);
59 
GetWidth()60   int GetWidth() const { return m_Width; }
GetHeight()61   int GetHeight() const { return m_Height; }
GetDeviceType()62   DeviceType GetDeviceType() const { return m_DeviceType; }
GetRenderCaps()63   int GetRenderCaps() const { return m_RenderCaps; }
64   int GetDeviceCaps(int id) const;
65   RetainPtr<CFX_DIBitmap> GetBitmap() const;
66   void SetBitmap(const RetainPtr<CFX_DIBitmap>& pBitmap);
67   bool CreateCompatibleBitmap(const RetainPtr<CFX_DIBitmap>& pDIB,
68                               int width,
69                               int height) const;
GetClipBox()70   const FX_RECT& GetClipBox() const { return m_ClipBox; }
71   void SetBaseClip(const FX_RECT& rect);
72   bool SetClip_PathFill(const CFX_Path& path,
73                         const CFX_Matrix* pObject2Device,
74                         const CFX_FillRenderOptions& fill_options);
75   bool SetClip_PathStroke(const CFX_Path& path,
76                           const CFX_Matrix* pObject2Device,
77                           const CFX_GraphStateData* pGraphState);
78   bool SetClip_Rect(const FX_RECT& pRect);
79   bool DrawPath(const CFX_Path& path,
80                 const CFX_Matrix* pObject2Device,
81                 const CFX_GraphStateData* pGraphState,
82                 uint32_t fill_color,
83                 uint32_t stroke_color,
84                 const CFX_FillRenderOptions& fill_options);
85   bool DrawPathWithBlend(const CFX_Path& path,
86                          const CFX_Matrix* pObject2Device,
87                          const CFX_GraphStateData* pGraphState,
88                          uint32_t fill_color,
89                          uint32_t stroke_color,
90                          const CFX_FillRenderOptions& fill_options,
91                          BlendMode blend_type);
FillRect(const FX_RECT & rect,uint32_t color)92   bool FillRect(const FX_RECT& rect, uint32_t color) {
93     return FillRectWithBlend(rect, color, BlendMode::kNormal);
94   }
95 
96   RetainPtr<CFX_DIBitmap> GetBackDrop();
97   bool GetDIBits(const RetainPtr<CFX_DIBitmap>& pBitmap, int left, int top);
SetDIBits(const RetainPtr<CFX_DIBBase> & pBitmap,int left,int top)98   bool SetDIBits(const RetainPtr<CFX_DIBBase>& pBitmap, int left, int top) {
99     return SetDIBitsWithBlend(pBitmap, left, top, BlendMode::kNormal);
100   }
101   bool SetDIBitsWithBlend(const RetainPtr<CFX_DIBBase>& pBitmap,
102                           int left,
103                           int top,
104                           BlendMode blend_mode);
StretchDIBits(const RetainPtr<CFX_DIBBase> & pBitmap,int left,int top,int dest_width,int dest_height)105   bool StretchDIBits(const RetainPtr<CFX_DIBBase>& pBitmap,
106                      int left,
107                      int top,
108                      int dest_width,
109                      int dest_height) {
110     return StretchDIBitsWithFlagsAndBlend(pBitmap, left, top, dest_width,
111                                           dest_height, FXDIB_ResampleOptions(),
112                                           BlendMode::kNormal);
113   }
114   bool StretchDIBitsWithFlagsAndBlend(const RetainPtr<CFX_DIBBase>& pBitmap,
115                                       int left,
116                                       int top,
117                                       int dest_width,
118                                       int dest_height,
119                                       const FXDIB_ResampleOptions& options,
120                                       BlendMode blend_mode);
121   bool SetBitMask(const RetainPtr<CFX_DIBBase>& pBitmap,
122                   int left,
123                   int top,
124                   uint32_t argb);
125   bool StretchBitMask(const RetainPtr<CFX_DIBBase>& pBitmap,
126                       int left,
127                       int top,
128                       int dest_width,
129                       int dest_height,
130                       uint32_t color);
131   bool StretchBitMaskWithFlags(const RetainPtr<CFX_DIBBase>& pBitmap,
132                                int left,
133                                int top,
134                                int dest_width,
135                                int dest_height,
136                                uint32_t argb,
137                                const FXDIB_ResampleOptions& options);
StartDIBits(const RetainPtr<CFX_DIBBase> & pBitmap,int bitmap_alpha,uint32_t color,const CFX_Matrix & matrix,const FXDIB_ResampleOptions & options,std::unique_ptr<CFX_ImageRenderer> * handle)138   bool StartDIBits(const RetainPtr<CFX_DIBBase>& pBitmap,
139                    int bitmap_alpha,
140                    uint32_t color,
141                    const CFX_Matrix& matrix,
142                    const FXDIB_ResampleOptions& options,
143                    std::unique_ptr<CFX_ImageRenderer>* handle) {
144     return StartDIBitsWithBlend(pBitmap, bitmap_alpha, color, matrix, options,
145                                 handle, BlendMode::kNormal);
146   }
147   bool StartDIBitsWithBlend(const RetainPtr<CFX_DIBBase>& pBitmap,
148                             int bitmap_alpha,
149                             uint32_t argb,
150                             const CFX_Matrix& matrix,
151                             const FXDIB_ResampleOptions& options,
152                             std::unique_ptr<CFX_ImageRenderer>* handle,
153                             BlendMode blend_mode);
154   bool ContinueDIBits(CFX_ImageRenderer* handle, PauseIndicatorIface* pPause);
155 
156   bool DrawNormalText(pdfium::span<const TextCharPos> pCharPos,
157                       CFX_Font* pFont,
158                       float font_size,
159                       const CFX_Matrix& mtText2Device,
160                       uint32_t fill_color,
161                       const CFX_TextRenderOptions& options);
162   bool DrawTextPath(pdfium::span<const TextCharPos> pCharPos,
163                     CFX_Font* pFont,
164                     float font_size,
165                     const CFX_Matrix& mtText2User,
166                     const CFX_Matrix* pUser2Device,
167                     const CFX_GraphStateData* pGraphState,
168                     uint32_t fill_color,
169                     uint32_t stroke_color,
170                     CFX_Path* pClippingPath,
171                     const CFX_FillRenderOptions& fill_options);
172 
173   void DrawFillRect(const CFX_Matrix* pUser2Device,
174                     const CFX_FloatRect& rect,
175                     const CFX_Color& color,
176                     int32_t nTransparency);
177   void DrawFillRect(const CFX_Matrix* pUser2Device,
178                     const CFX_FloatRect& rect,
179                     const FX_COLORREF& color);
180   void DrawStrokeRect(const CFX_Matrix& mtUser2Device,
181                       const CFX_FloatRect& rect,
182                       const FX_COLORREF& color,
183                       float fWidth);
184   void DrawStrokeLine(const CFX_Matrix* pUser2Device,
185                       const CFX_PointF& ptMoveTo,
186                       const CFX_PointF& ptLineTo,
187                       const FX_COLORREF& color,
188                       float fWidth);
189   void DrawBorder(const CFX_Matrix* pUser2Device,
190                   const CFX_FloatRect& rect,
191                   float fWidth,
192                   const CFX_Color& color,
193                   const CFX_Color& crLeftTop,
194                   const CFX_Color& crRightBottom,
195                   BorderStyle nStyle,
196                   int32_t nTransparency);
197   void DrawFillArea(const CFX_Matrix& mtUser2Device,
198                     const std::vector<CFX_PointF>& points,
199                     const FX_COLORREF& color);
200   void DrawShadow(const CFX_Matrix& mtUser2Device,
201                   const CFX_FloatRect& rect,
202                   int32_t nTransparency,
203                   int32_t nStartGray,
204                   int32_t nEndGray);
205   bool DrawShading(const CPDF_ShadingPattern* pPattern,
206                    const CFX_Matrix* pMatrix,
207                    const FX_RECT& clip_rect,
208                    int alpha,
209                    bool bAlphaMode);
210 
211   // Multiplies the device by a constant alpha, returning `true` on success.
212   bool MultiplyAlpha(float alpha);
213 
214   // Multiplies the device by an alpha mask, returning `true` on success.
215   bool MultiplyAlpha(const RetainPtr<CFX_DIBBase>& mask);
216 
217 #if defined(_SKIA_SUPPORT_)
218   bool SetBitsWithMask(const RetainPtr<CFX_DIBBase>& pBitmap,
219                        const RetainPtr<CFX_DIBBase>& pMask,
220                        int left,
221                        int top,
222                        int bitmap_alpha,
223                        BlendMode blend_type);
224 #endif
225 
226  protected:
227   CFX_RenderDevice();
228 
229   void SetDeviceDriver(std::unique_ptr<RenderDeviceDriverIface> pDriver);
GetDeviceDriver()230   RenderDeviceDriverIface* GetDeviceDriver() const {
231     return m_pDeviceDriver.get();
232   }
233 
234  private:
235   void InitDeviceInfo();
236   void UpdateClipBox();
237   bool DrawFillStrokePath(const CFX_Path& path,
238                           const CFX_Matrix* pObject2Device,
239                           const CFX_GraphStateData* pGraphState,
240                           uint32_t fill_color,
241                           uint32_t stroke_color,
242                           const CFX_FillRenderOptions& fill_options,
243                           BlendMode blend_type);
244   bool DrawCosmeticLine(const CFX_PointF& ptMoveTo,
245                         const CFX_PointF& ptLineTo,
246                         uint32_t color,
247                         const CFX_FillRenderOptions& fill_options,
248                         BlendMode blend_type);
249   void DrawZeroAreaPath(const std::vector<CFX_Path::Point>& path,
250                         const CFX_Matrix* matrix,
251                         bool adjust,
252                         bool aliased_path,
253                         uint32_t fill_color,
254                         uint8_t fill_alpha,
255                         BlendMode blend_type);
256   bool FillRectWithBlend(const FX_RECT& rect,
257                          uint32_t color,
258                          BlendMode blend_type);
259 
260   RetainPtr<CFX_DIBitmap> m_pBitmap;
261   int m_Width = 0;
262   int m_Height = 0;
263   int m_bpp = 0;
264   int m_RenderCaps = 0;
265   DeviceType m_DeviceType = DeviceType::kDisplay;
266   FX_RECT m_ClipBox;
267   std::unique_ptr<RenderDeviceDriverIface> m_pDeviceDriver;
268 };
269 
270 #endif  // CORE_FXGE_CFX_RENDERDEVICE_H_
271