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"
17#include "third_party/tonic/typed_data/typed_list.h"
18
19namespace tonic {
20class DartLibraryNatives;
21} // namespace tonic
22
23namespace flutter {
24class CanvasImage;
25
26class Canvas : public RefCountedDartWrappable<Canvas> {
27 DEFINE_WRAPPERTYPEINFO();
28 FML_FRIEND_MAKE_REF_COUNTED(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 void drawImageRect(const CanvasImage* image,
115 double src_left,
116 double src_top,
117 double src_right,
118 double src_bottom,
119 double dst_left,
120 double dst_top,
121 double dst_right,
122 double dst_bottom,
123 const Paint& paint,
124 const PaintData& paint_data);
125 void drawImageNine(const CanvasImage* image,
126 double center_left,
127 double center_top,
128 double center_right,
129 double center_bottom,
130 double dst_left,
131 double dst_top,
132 double dst_right,
133 double dst_bottom,
134 const Paint& paint,
135 const PaintData& paint_data);
136 void drawPicture(Picture* picture);
137
138 // The paint argument is first for the following functions because Paint
139 // unwraps a number of C++ objects. Once we create a view unto a
140 // Float32List, we cannot re-enter the VM to unwrap objects. That means we
141 // either need to process the paint argument first.
142
143 void drawPoints(const Paint& paint,
144 const PaintData& paint_data,
145 SkCanvas::PointMode point_mode,
146 const tonic::Float32List& points);
147
148 void drawVertices(const Vertices* vertices,
149 SkBlendMode blend_mode,
150 const Paint& paint,
151 const PaintData& paint_data);
152
153 void drawAtlas(const Paint& paint,
154 const PaintData& paint_data,
155 CanvasImage* atlas,
156 const tonic::Float32List& transforms,
157 const tonic::Float32List& rects,
158 const tonic::Int32List& colors,
159 SkBlendMode blend_mode,
160 const tonic::Float32List& cull_rect);
161
162 void drawShadow(const CanvasPath* path,
163 SkColor color,
164 double elevation,
165 bool transparentOccluder);
166
167 SkCanvas* canvas() const { return canvas_; }
168 void Invalidate();
169
170 static void RegisterNatives(tonic::DartLibraryNatives* natives);
171
172 size_t external_allocation_size() const { return external_allocation_size_; }
173
174 private:
175 explicit Canvas(SkCanvas* canvas);
176
177 // The SkCanvas is supplied by a call to SkPictureRecorder::beginRecording,
178 // which does not transfer ownership. For this reason, we hold a raw
179 // pointer and manually set to null in Clear.
180 SkCanvas* canvas_;
181 size_t external_allocation_size_ = 0;
182};
183
184} // namespace flutter
185
186#endif // FLUTTER_LIB_UI_PAINTING_CANVAS_H_
187