xref: /aosp_15_r20/external/pdfium/core/fxge/skia/fx_skia_device.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2014 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 #ifndef CORE_FXGE_SKIA_FX_SKIA_DEVICE_H_
6 #define CORE_FXGE_SKIA_FX_SKIA_DEVICE_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 
12 #include "core/fxcrt/data_vector.h"
13 #include "core/fxcrt/fx_memory_wrappers.h"
14 #include "core/fxcrt/retain_ptr.h"
15 #include "core/fxcrt/unowned_ptr.h"
16 #include "core/fxge/cfx_fillrenderoptions.h"
17 #include "core/fxge/cfx_path.h"
18 #include "core/fxge/renderdevicedriver_iface.h"
19 #include "third_party/base/check_op.h"
20 #include "third_party/base/containers/span.h"
21 #include "third_party/skia/include/core/SkPoint.h"
22 #include "third_party/skia/include/core/SkRSXform.h"
23 #include "third_party/skia/include/core/SkRefCnt.h"
24 
25 class CFX_Font;
26 class CFX_Matrix;
27 class SkCanvas;
28 class SkSurface;
29 class TextCharPos;
30 struct CFX_TextRenderOptions;
31 
32 // Assumes Skia is not going to add non-data members to its fundamental types.
33 FX_DATA_PARTITION_EXCEPTION(SkPoint);
34 FX_DATA_PARTITION_EXCEPTION(SkRSXform);
35 
36 class CFX_SkiaDeviceDriver final : public RenderDeviceDriverIface {
37  public:
38   static std::unique_ptr<CFX_SkiaDeviceDriver> Create(
39       RetainPtr<CFX_DIBitmap> pBitmap,
40       bool bRgbByteOrder,
41       RetainPtr<CFX_DIBitmap> pBackdropBitmap,
42       bool bGroupKnockout);
43 
44   explicit CFX_SkiaDeviceDriver(SkCanvas* canvas);
45   ~CFX_SkiaDeviceDriver() override;
46 
47   /** Options */
48   DeviceType GetDeviceType() const override;
49   int GetDeviceCaps(int caps_id) const override;
50 
51   /** Save and restore all graphic states */
52   void SaveState() override;
53   void RestoreState(bool bKeepSaved) override;
54 
55   /** Set clipping path using filled region */
56   bool SetClip_PathFill(
57       const CFX_Path& path,                       // path info
58       const CFX_Matrix* pObject2Device,           // optional transformation
59       const CFX_FillRenderOptions& fill_options)  // fill options
60       override;
61 
62   /** Set clipping path using stroked region */
63   bool SetClip_PathStroke(
64       const CFX_Path& path,              // path info
65       const CFX_Matrix* pObject2Device,  // required transformation
66       const CFX_GraphStateData*
67           pGraphState)  // graphic state, for pen attributes
68       override;
69 
70   /** Draw a path */
71   bool DrawPath(const CFX_Path& path,
72                 const CFX_Matrix* pObject2Device,
73                 const CFX_GraphStateData* pGraphState,
74                 uint32_t fill_color,
75                 uint32_t stroke_color,
76                 const CFX_FillRenderOptions& fill_options,
77                 BlendMode blend_type) override;
78 
79   bool FillRectWithBlend(const FX_RECT& rect,
80                          uint32_t fill_color,
81                          BlendMode blend_type) override;
82 
83   /** Draw a single pixel (device dependant) line */
84   bool DrawCosmeticLine(const CFX_PointF& ptMoveTo,
85                         const CFX_PointF& ptLineTo,
86                         uint32_t color,
87                         BlendMode blend_type) override;
88 
89   bool GetClipBox(FX_RECT* pRect) override;
90 
91   /** Load device buffer into a DIB */
92   bool GetDIBits(const RetainPtr<CFX_DIBitmap>& pBitmap,
93                  int left,
94                  int top) override;
95 
96   RetainPtr<CFX_DIBitmap> GetBackDrop() override;
97 
98   bool SetDIBits(const RetainPtr<CFX_DIBBase>& pBitmap,
99                  uint32_t color,
100                  const FX_RECT& src_rect,
101                  int dest_left,
102                  int dest_top,
103                  BlendMode blend_type) override;
104   bool SetBitsWithMask(const RetainPtr<CFX_DIBBase>& pBitmap,
105                        const RetainPtr<CFX_DIBBase>& pMask,
106                        int dest_left,
107                        int dest_top,
108                        int bitmap_alpha,
109                        BlendMode blend_type) override;
110   void SetGroupKnockout(bool group_knockout) override;
111 
112   bool StretchDIBits(const RetainPtr<CFX_DIBBase>& pBitmap,
113                      uint32_t color,
114                      int dest_left,
115                      int dest_top,
116                      int dest_width,
117                      int dest_height,
118                      const FX_RECT* pClipRect,
119                      const FXDIB_ResampleOptions& options,
120                      BlendMode blend_type) override;
121 
122   bool StartDIBits(const RetainPtr<CFX_DIBBase>& pBitmap,
123                    int bitmap_alpha,
124                    uint32_t color,
125                    const CFX_Matrix& matrix,
126                    const FXDIB_ResampleOptions& options,
127                    std::unique_ptr<CFX_ImageRenderer>* handle,
128                    BlendMode blend_type) override;
129 
130   bool ContinueDIBits(CFX_ImageRenderer* handle,
131                       PauseIndicatorIface* pPause) override;
132 
133   bool DrawBitsWithMask(const RetainPtr<CFX_DIBBase>& pBitmap,
134                         const RetainPtr<CFX_DIBBase>& pMask,
135                         int bitmap_alpha,
136                         const CFX_Matrix& matrix,
137                         BlendMode blend_type);
138 
139   bool DrawDeviceText(pdfium::span<const TextCharPos> pCharPos,
140                       CFX_Font* pFont,
141                       const CFX_Matrix& mtObject2Device,
142                       float font_size,
143                       uint32_t color,
144                       const CFX_TextRenderOptions& options) override;
145 
146   int GetDriverType() const override;
147 
148   bool DrawShading(const CPDF_ShadingPattern* pPattern,
149                    const CFX_Matrix* pMatrix,
150                    const FX_RECT& clip_rect,
151                    int alpha,
152                    bool bAlphaMode) override;
153 
154   bool MultiplyAlpha(float alpha) override;
155   bool MultiplyAlpha(const RetainPtr<CFX_DIBBase>& mask) override;
156 
157   void Clear(uint32_t color);
158   void Dump() const;
159 
160  private:
161   class CharDetail {
162    public:
163     CharDetail();
164     ~CharDetail();
165 
GetPositions()166     const DataVector<SkPoint>& GetPositions() const { return m_positions; }
SetPositionAt(size_t index,const SkPoint & position)167     void SetPositionAt(size_t index, const SkPoint& position) {
168       m_positions[index] = position;
169     }
GetGlyphs()170     const DataVector<uint16_t>& GetGlyphs() const { return m_glyphs; }
SetGlyphAt(size_t index,uint16_t glyph)171     void SetGlyphAt(size_t index, uint16_t glyph) { m_glyphs[index] = glyph; }
GetFontCharWidths()172     const DataVector<uint32_t>& GetFontCharWidths() const {
173       return m_fontCharWidths;
174     }
SetFontCharWidthAt(size_t index,uint32_t width)175     void SetFontCharWidthAt(size_t index, uint32_t width) {
176       m_fontCharWidths[index] = width;
177     }
Count()178     size_t Count() const {
179       DCHECK_EQ(m_positions.size(), m_glyphs.size());
180       return m_glyphs.size();
181     }
SetCount(size_t count)182     void SetCount(size_t count) {
183       m_positions.resize(count);
184       m_glyphs.resize(count);
185       m_fontCharWidths.resize(count);
186     }
187 
188    private:
189     DataVector<SkPoint> m_positions;  // accumulator for text positions
190     DataVector<uint16_t> m_glyphs;    // accumulator for text glyphs
191     // accumulator for glyphs' width defined in pdf
192     DataVector<uint32_t> m_fontCharWidths;
193   };
194 
195   CFX_SkiaDeviceDriver(RetainPtr<CFX_DIBitmap> pBitmap,
196                        bool bRgbByteOrder,
197                        RetainPtr<CFX_DIBitmap> pBackdropBitmap,
198                        bool bGroupKnockout);
199 
200   bool TryDrawText(pdfium::span<const TextCharPos> char_pos,
201                    const CFX_Font* pFont,
202                    const CFX_Matrix& matrix,
203                    float font_size,
204                    uint32_t color,
205                    const CFX_TextRenderOptions& options);
206 
207   bool StartDIBitsSkia(const RetainPtr<CFX_DIBBase>& pBitmap,
208                        const FX_RECT& src_rect,
209                        int bitmap_alpha,
210                        uint32_t color,
211                        const CFX_Matrix& matrix,
212                        const FXDIB_ResampleOptions& options,
213                        BlendMode blend_type);
214 
215   RetainPtr<CFX_DIBitmap> m_pBitmap;
216   RetainPtr<CFX_DIBitmap> m_pBackdropBitmap;
217 
218   // The input bitmap passed by the render device. Only used when the input
219   // bitmap is 24 bpp and cannot be directly used as the back of a SkCanvas.
220   RetainPtr<CFX_DIBitmap> m_pOriginalBitmap;
221 
222   sk_sp<SkSurface> surface_;
223   UnownedPtr<SkCanvas> m_pCanvas;
224   CFX_FillRenderOptions m_FillOptions;
225   bool m_bRgbByteOrder;
226   bool m_bGroupKnockout;
227 
228   CharDetail m_charDetails;
229   // accumulator for txt rotate/scale/translate
230   DataVector<SkRSXform> m_rsxform;
231 };
232 
233 #endif  // CORE_FXGE_SKIA_FX_SKIA_DEVICE_H_
234