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