Flutter Engine
canvas.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
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 FLUTTER_LIB_UI_PAINTING_CANVAS_H_
6 #define FLUTTER_LIB_UI_PAINTING_CANVAS_H_
7 
8 #include "flutter/lib/ui/dart_wrapper.h"
9 #include "flutter/lib/ui/painting/paint.h"
10 #include "flutter/lib/ui/painting/path.h"
11 #include "flutter/lib/ui/painting/picture.h"
12 #include "flutter/lib/ui/painting/picture_recorder.h"
13 #include "flutter/lib/ui/painting/rrect.h"
14 #include "flutter/lib/ui/painting/vertices.h"
15 #include "third_party/skia/include/core/SkCanvas.h"
16 #include "third_party/skia/include/utils/SkShadowUtils.h"
18 
19 namespace tonic {
20 class DartLibraryNatives;
21 } // namespace tonic
22 
23 namespace flutter {
24 class CanvasImage;
25 
26 class Canvas : public RefCountedDartWrappable<Canvas> {
29 
30  public:
31  static fml::RefPtr<Canvas> Create(PictureRecorder* recorder,
32  double left,
33  double top,
34  double right,
35  double bottom);
36 
37  ~Canvas() override;
38 
39  void save();
40  void saveLayerWithoutBounds(const Paint& paint, const PaintData& paint_data);
41  void saveLayer(double left,
42  double top,
43  double right,
44  double bottom,
45  const Paint& paint,
46  const PaintData& paint_data);
47  void restore();
48  int getSaveCount();
49 
50  void translate(double dx, double dy);
51  void scale(double sx, double sy);
52  void rotate(double radians);
53  void skew(double sx, double sy);
54  void transform(const tonic::Float64List& matrix4);
55 
56  void clipRect(double left,
57  double top,
58  double right,
59  double bottom,
60  SkClipOp clipOp,
61  bool doAntiAlias = true);
62  void clipRRect(const RRect& rrect, bool doAntiAlias = true);
63  void clipPath(const CanvasPath* path, bool doAntiAlias = true);
64 
65  void drawColor(SkColor color, SkBlendMode blend_mode);
66  void drawLine(double x1,
67  double y1,
68  double x2,
69  double y2,
70  const Paint& paint,
71  const PaintData& paint_data);
72  void drawPaint(const Paint& paint, const PaintData& paint_data);
73  void drawRect(double left,
74  double top,
75  double right,
76  double bottom,
77  const Paint& paint,
78  const PaintData& paint_data);
79  void drawRRect(const RRect& rrect,
80  const Paint& paint,
81  const PaintData& paint_data);
82  void drawDRRect(const RRect& outer,
83  const RRect& inner,
84  const Paint& paint,
85  const PaintData& paint_data);
86  void drawOval(double left,
87  double top,
88  double right,
89  double bottom,
90  const Paint& paint,
91  const PaintData& paint_data);
92  void drawCircle(double x,
93  double y,
94  double radius,
95  const Paint& paint,
96  const PaintData& paint_data);
97  void drawArc(double left,
98  double top,
99  double right,
100  double bottom,
101  double startAngle,
102  double sweepAngle,
103  bool useCenter,
104  const Paint& paint,
105  const PaintData& paint_data);
106  void drawPath(const CanvasPath* path,
107  const Paint& paint,
108  const PaintData& paint_data);
109  void drawImage(const CanvasImage* image,
110  double x,
111  double y,
112  const Paint& paint,
113  const PaintData& paint_data,
114  int filterQualityIndex);
115  void drawImageRect(const CanvasImage* image,
116  double src_left,
117  double src_top,
118  double src_right,
119  double src_bottom,
120  double dst_left,
121  double dst_top,
122  double dst_right,
123  double dst_bottom,
124  const Paint& paint,
125  const PaintData& paint_data,
126  int filterQualityIndex);
127  void drawImageNine(const CanvasImage* image,
128  double center_left,
129  double center_top,
130  double center_right,
131  double center_bottom,
132  double dst_left,
133  double dst_top,
134  double dst_right,
135  double dst_bottom,
136  const Paint& paint,
137  const PaintData& paint_data,
138  int bitmapSamplingIndex);
139  void drawPicture(Picture* picture);
140 
141  // The paint argument is first for the following functions because Paint
142  // unwraps a number of C++ objects. Once we create a view unto a
143  // Float32List, we cannot re-enter the VM to unwrap objects. That means we
144  // either need to process the paint argument first.
145 
146  void drawPoints(const Paint& paint,
147  const PaintData& paint_data,
148  SkCanvas::PointMode point_mode,
149  const tonic::Float32List& points);
150 
151  void drawVertices(const Vertices* vertices,
152  SkBlendMode blend_mode,
153  const Paint& paint,
154  const PaintData& paint_data);
155 
156  void drawAtlas(const Paint& paint,
157  const PaintData& paint_data,
158  int filterQualityIndex,
159  CanvasImage* atlas,
160  const tonic::Float32List& transforms,
161  const tonic::Float32List& rects,
162  const tonic::Int32List& colors,
163  SkBlendMode blend_mode,
164  const tonic::Float32List& cull_rect);
165 
166  void drawShadow(const CanvasPath* path,
167  SkColor color,
168  double elevation,
169  bool transparentOccluder);
170 
171  SkCanvas* canvas() const { return canvas_; }
172  void Invalidate();
173 
174  static void RegisterNatives(tonic::DartLibraryNatives* natives);
175 
176  private:
177  explicit Canvas(SkCanvas* canvas);
178 
179  // The SkCanvas is supplied by a call to SkPictureRecorder::beginRecording,
180  // which does not transfer ownership. For this reason, we hold a raw
181  // pointer and manually set to null in Clear.
182  SkCanvas* canvas_;
183 
184  // A copy of the recorder used by the SkCanvas->DisplayList adapter for cases
185  // where we cannot record the SkCanvas method call through the various OnOp()
186  // virtual methods or where we can be more efficient by talking directly in
187  // the DisplayList operation lexicon. The recorder has a method for recording
188  // paint attributes from an SkPaint and an operation type as well as access
189  // to the raw DisplayListBuilder for emitting custom rendering operations.
190  sk_sp<DisplayListCanvasRecorder> display_list_recorder_;
191  sk_sp<DisplayListBuilder> builder() {
192  return display_list_recorder_->builder();
193  }
194 };
195 
196 } // namespace flutter
197 
198 #endif // FLUTTER_LIB_UI_PAINTING_CANVAS_H_
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
#define DEFINE_WRAPPERTYPEINFO()
SkCanvas * canvas() const
Definition: canvas.h:171
#define FML_FRIEND_MAKE_REF_COUNTED(T)
Definition: ref_counted.h:131
constexpr SkColor colors[]