• 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/check_op.h"
13 #include "core/fxcrt/data_vector.h"
14 #include "core/fxcrt/fx_memory_wrappers.h"
15 #include "core/fxcrt/retain_ptr.h"
16 #include "core/fxcrt/span.h"
17 #include "core/fxcrt/unowned_ptr.h"
18 #include "core/fxge/cfx_fillrenderoptions.h"
19 #include "core/fxge/cfx_path.h"
20 #include "core/fxge/renderdevicedriver_iface.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 SkPaint;
29 class SkPath;
30 class SkSurface;
31 class TextCharPos;
32 struct CFX_TextRenderOptions;
33 
34 // Assumes Skia is not going to add non-data members to its fundamental types.
35 FX_DATA_PARTITION_EXCEPTION(SkPoint);
36 FX_DATA_PARTITION_EXCEPTION(SkRSXform);
37 
38 class CFX_SkiaDeviceDriver final : public RenderDeviceDriverIface {
39  public:
40   static std::unique_ptr<CFX_SkiaDeviceDriver> Create(
41       RetainPtr<CFX_DIBitmap> pBitmap,
42       bool bRgbByteOrder,
43       RetainPtr<CFX_DIBitmap> pBackdropBitmap,
44       bool bGroupKnockout);
45   static std::unique_ptr<CFX_SkiaDeviceDriver> Create(SkCanvas& canvas);
46 
47   ~CFX_SkiaDeviceDriver() override;
48 
49   // RenderDeviceDriverIface:
50   DeviceType GetDeviceType() const override;
51   int GetDeviceCaps(int caps_id) const override;
52   void SaveState() override;
53   void RestoreState(bool bKeepSaved) override;
54   bool SetClip_PathFill(const CFX_Path& path,
55                         const CFX_Matrix* pObject2Device,
56                         const CFX_FillRenderOptions& fill_options) override;
57   bool SetClip_PathStroke(const CFX_Path& path,
58                           const CFX_Matrix* pObject2Device,
59                           const CFX_GraphStateData* pGraphState) override;
60   bool DrawPath(const CFX_Path& path,
61                 const CFX_Matrix* pObject2Device,
62                 const CFX_GraphStateData* pGraphState,
63                 uint32_t fill_color,
64                 uint32_t stroke_color,
65                 const CFX_FillRenderOptions& fill_options) override;
66   bool FillRect(const FX_RECT& rect, uint32_t fill_color) override;
67   FX_RECT GetClipBox() const override;
68   bool GetDIBits(RetainPtr<CFX_DIBitmap> bitmap,
69                  int left,
70                  int top) const override;
71   RetainPtr<const CFX_DIBitmap> GetBackDrop() const override;
72   bool SetDIBits(RetainPtr<const CFX_DIBBase> bitmap,
73                  uint32_t color,
74                  const FX_RECT& src_rect,
75                  int dest_left,
76                  int dest_top,
77                  BlendMode blend_type) override;
78   bool SetBitsWithMask(RetainPtr<const CFX_DIBBase> bitmap,
79                        RetainPtr<const CFX_DIBBase> mask,
80                        int dest_left,
81                        int dest_top,
82                        float alpha,
83                        BlendMode blend_type) override;
84   void SetGroupKnockout(bool group_knockout) override;
85   void SyncInternalBitmaps() override;
86   bool StretchDIBits(RetainPtr<const CFX_DIBBase> bitmap,
87                      uint32_t color,
88                      int dest_left,
89                      int dest_top,
90                      int dest_width,
91                      int dest_height,
92                      const FX_RECT* pClipRect,
93                      const FXDIB_ResampleOptions& options,
94                      BlendMode blend_type) override;
95   StartResult StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
96                           float alpha,
97                           uint32_t color,
98                           const CFX_Matrix& matrix,
99                           const FXDIB_ResampleOptions& options,
100                           BlendMode blend_type) override;
101   bool DrawBitsWithMask(RetainPtr<const CFX_DIBBase> bitmap,
102                         RetainPtr<const CFX_DIBBase> mask,
103                         float alpha,
104                         const CFX_Matrix& matrix,
105                         BlendMode blend_type);
106   bool DrawDeviceText(pdfium::span<const TextCharPos> pCharPos,
107                       CFX_Font* pFont,
108                       const CFX_Matrix& mtObject2Device,
109                       float font_size,
110                       uint32_t color,
111                       const CFX_TextRenderOptions& options) override;
112   int GetDriverType() const override;
113   bool DrawShading(const CPDF_ShadingPattern& pattern,
114                    const CFX_Matrix& matrix,
115                    const FX_RECT& clip_rect,
116                    int alpha) override;
117 
118   bool MultiplyAlpha(float alpha) override;
119   bool MultiplyAlphaMask(RetainPtr<const CFX_DIBitmap> mask) override;
120 
121   void Clear(uint32_t color);
122   void Dump() const;
123 
124  private:
125   class CharDetail {
126    public:
127     CharDetail();
128     ~CharDetail();
129 
GetPositions()130     const DataVector<SkPoint>& GetPositions() const { return m_positions; }
SetPositionAt(size_t index,const SkPoint & position)131     void SetPositionAt(size_t index, const SkPoint& position) {
132       m_positions[index] = position;
133     }
GetGlyphs()134     const DataVector<uint16_t>& GetGlyphs() const { return m_glyphs; }
SetGlyphAt(size_t index,uint16_t glyph)135     void SetGlyphAt(size_t index, uint16_t glyph) { m_glyphs[index] = glyph; }
GetFontCharWidths()136     const DataVector<uint32_t>& GetFontCharWidths() const {
137       return m_fontCharWidths;
138     }
SetFontCharWidthAt(size_t index,uint32_t width)139     void SetFontCharWidthAt(size_t index, uint32_t width) {
140       m_fontCharWidths[index] = width;
141     }
Count()142     size_t Count() const {
143       DCHECK_EQ(m_positions.size(), m_glyphs.size());
144       return m_glyphs.size();
145     }
SetCount(size_t count)146     void SetCount(size_t count) {
147       m_positions.resize(count);
148       m_glyphs.resize(count);
149       m_fontCharWidths.resize(count);
150     }
151 
152    private:
153     DataVector<SkPoint> m_positions;  // accumulator for text positions
154     DataVector<uint16_t> m_glyphs;    // accumulator for text glyphs
155     // accumulator for glyphs' width defined in pdf
156     DataVector<uint32_t> m_fontCharWidths;
157   };
158 
159   // Use the public creation methods instead.
160   CFX_SkiaDeviceDriver(RetainPtr<CFX_DIBitmap> pBitmap,
161                        bool bRgbByteOrder,
162                        RetainPtr<CFX_DIBitmap> pBackdropBitmap,
163                        bool bGroupKnockout);
164   explicit CFX_SkiaDeviceDriver(SkCanvas& canvas);
165 
166   bool TryDrawText(pdfium::span<const TextCharPos> char_pos,
167                    const CFX_Font* pFont,
168                    const CFX_Matrix& matrix,
169                    float font_size,
170                    uint32_t color,
171                    const CFX_TextRenderOptions& options);
172 
173   bool StartDIBitsSkia(RetainPtr<const CFX_DIBBase> bitmap,
174                        const FX_RECT& src_rect,
175                        float alpha,
176                        uint32_t color,
177                        const CFX_Matrix& matrix,
178                        const FXDIB_ResampleOptions& options,
179                        BlendMode blend_type);
180 
181   // A wrapper around SkCanvas::drawPath() that optionally can show debug data.
182   void DrawPathImpl(const SkPath& path, const SkPaint& paint);
183 
184   RetainPtr<CFX_DIBitmap> m_pBitmap;
185   RetainPtr<CFX_DIBitmap> m_pBackdropBitmap;
186 
187   // The input bitmap passed by the render device. Only used when the input
188   // bitmap is 24 bpp and cannot be directly used as the back of a SkCanvas.
189   RetainPtr<CFX_DIBitmap> m_pOriginalBitmap;
190 
191   sk_sp<SkSurface> surface_;
192   UnownedPtr<SkCanvas> m_pCanvas;
193   CFX_FillRenderOptions m_FillOptions;
194   bool m_bRgbByteOrder;
195   bool m_bGroupKnockout;
196 
197   CharDetail m_charDetails;
198   // accumulator for txt rotate/scale/translate
199   DataVector<SkRSXform> m_rsxform;
200 };
201 
202 #endif  // CORE_FXGE_SKIA_FX_SKIA_DEVICE_H_
203