Flutter Engine
The Flutter Engine
dl_sk_canvas.cc
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#if !SLIMPELLER
6
7#include "flutter/display_list/skia/dl_sk_canvas.h"
8
9#include "flutter/display_list/skia/dl_sk_conversions.h"
10#include "flutter/display_list/skia/dl_sk_dispatcher.h"
11#include "flutter/fml/trace_event.h"
12
17
18namespace flutter {
19
21 public:
22 // SkOptionalPaint is only valid for ops that do not use the ColorSource
23 explicit SkOptionalPaint(const DlPaint* dl_paint) {
24 if (dl_paint && !dl_paint->isDefault()) {
25 sk_paint_ = ToNonShaderSk(*dl_paint);
26 ptr_ = &sk_paint_;
27 } else {
28 ptr_ = nullptr;
29 }
30 }
31
32 SkPaint* operator()() { return ptr_; }
33
34 private:
35 SkPaint sk_paint_;
36 SkPaint* ptr_;
37};
38
40 delegate_ = canvas;
41}
42
44 return delegate_->getBaseLayerSize();
45}
46
48 return delegate_->imageInfo();
49}
50
52 delegate_->save();
53}
54
56 const DlPaint* paint,
57 const DlImageFilter* backdrop) {
58 sk_sp<SkImageFilter> sk_backdrop = ToSk(backdrop);
59 SkOptionalPaint sk_paint(paint);
60 TRACE_EVENT0("flutter", "Canvas::saveLayer");
61 delegate_->saveLayer(
62 SkCanvas::SaveLayerRec{bounds, sk_paint(), sk_backdrop.get(), 0});
63}
64
66 delegate_->restore();
67}
68
70 return delegate_->getSaveCount();
71}
72
73void DlSkCanvasAdapter::RestoreToCount(int restore_count) {
74 delegate_->restoreToCount(restore_count);
75}
76
78 delegate_->translate(tx, ty);
79}
80
82 delegate_->scale(sx, sy);
83}
84
86 delegate_->rotate(degrees);
87}
88
90 delegate_->skew(sx, sy);
91}
92
93// clang-format off
94
95// 2x3 2D affine subset of a 4x4 transform in row major order
97 SkScalar mxx, SkScalar mxy, SkScalar mxt,
98 SkScalar myx, SkScalar myy, SkScalar myt) {
99 delegate_->concat(SkMatrix::MakeAll(mxx, mxy, mxt, myx, myy, myt, 0, 0, 1));
100}
101
102// full 4x4 transform in row major order
104 SkScalar mxx, SkScalar mxy, SkScalar mxz, SkScalar mxt,
105 SkScalar myx, SkScalar myy, SkScalar myz, SkScalar myt,
106 SkScalar mzx, SkScalar mzy, SkScalar mzz, SkScalar mzt,
107 SkScalar mwx, SkScalar mwy, SkScalar mwz, SkScalar mwt) {
108 delegate_->concat(SkM44(mxx, mxy, mxz, mxt,
109 myx, myy, myz, myt,
110 mzx, mzy, mzz, mzt,
111 mwx, mwy, mwz, mwt));
112}
113
114// clang-format on
115
117 delegate_->resetMatrix();
118}
119
121 delegate_->concat(*matrix);
122}
123
124void DlSkCanvasAdapter::Transform(const SkM44* matrix44) {
125 delegate_->concat(*matrix44);
126}
127
129 delegate_->setMatrix(*matrix);
130}
131
133 delegate_->setMatrix(*matrix44);
134}
135
136/// Returns the 4x4 full perspective transform representing all transform
137/// operations executed so far in this DisplayList within the enclosing
138/// save stack.
140 return delegate_->getLocalToDevice();
141}
142
143/// Returns the 3x3 partial perspective transform representing all transform
144/// operations executed so far in this DisplayList within the enclosing
145/// save stack.
147 return delegate_->getTotalMatrix();
148}
149
151 ClipOp clip_op,
152 bool is_aa) {
153 delegate_->clipRect(rect, ToSk(clip_op), is_aa);
154}
155
157 ClipOp clip_op,
158 bool is_aa) {
159 delegate_->clipRRect(rrect, ToSk(clip_op), is_aa);
160}
161
163 ClipOp clip_op,
164 bool is_aa) {
165 delegate_->clipPath(path, ToSk(clip_op), is_aa);
166}
167
168/// Conservative estimate of the bounds of all outstanding clip operations
169/// measured in the coordinate space within which this DisplayList will
170/// be rendered.
172 return SkRect::Make(delegate_->getDeviceClipBounds());
173}
174
175/// Conservative estimate of the bounds of all outstanding clip operations
176/// transformed into the local coordinate space in which currently
177/// recorded rendering operations are interpreted.
179 return delegate_->getLocalClipBounds();
180}
181
182/// Return true iff the supplied bounds are easily shown to be outside
183/// of the current clip bounds. This method may conservatively return
184/// false if it cannot make the determination.
186 return delegate_->quickReject(bounds);
187}
188
190 delegate_->drawPaint(ToSk(paint));
191}
192
194 delegate_->drawColor(ToSk(color), ToSk(mode));
195}
196
198 const SkPoint& p1,
199 const DlPaint& paint) {
200 delegate_->drawLine(p0, p1, ToStrokedSk(paint));
201}
202
204 const DlPoint& p1,
205 DlScalar on_length,
206 DlScalar off_length,
207 const DlPaint& paint) {
208 SkPaint dashed_paint = ToStrokedSk(paint);
209 SkScalar intervals[2] = {on_length, off_length};
210 dashed_paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0.0f));
211 delegate_->drawLine(ToSkPoint(p0), ToSkPoint(p1), dashed_paint);
212}
213
215 delegate_->drawRect(rect, ToSk(paint));
216}
217
219 delegate_->drawOval(bounds, ToSk(paint));
220}
221
223 SkScalar radius,
224 const DlPaint& paint) {
225 delegate_->drawCircle(center, radius, ToSk(paint));
226}
227
229 delegate_->drawRRect(rrect, ToSk(paint));
230}
231
233 const SkRRect& inner,
234 const DlPaint& paint) {
235 delegate_->drawDRRect(outer, inner, ToSk(paint));
236}
237
239 delegate_->drawPath(path, ToSk(paint));
240}
241
243 SkScalar start,
244 SkScalar sweep,
245 bool useCenter,
246 const DlPaint& paint) {
247 delegate_->drawArc(bounds, start, sweep, useCenter, ToSk(paint));
248}
249
251 uint32_t count,
252 const SkPoint pts[],
253 const DlPaint& paint) {
254 delegate_->drawPoints(ToSk(mode), count, pts, ToStrokedSk(paint));
255}
256
259 const DlPaint& paint) {
260 delegate_->drawVertices(ToSk(vertices), ToSk(mode), ToSk(paint));
261}
262
264 const SkPoint point,
266 const DlPaint* paint) {
267 SkOptionalPaint sk_paint(paint);
268 sk_sp<SkImage> sk_image = image->skia_image();
269 delegate_->drawImage(sk_image.get(), point.fX, point.fY, ToSk(sampling),
270 sk_paint());
271}
272
274 const SkRect& src,
275 const SkRect& dst,
277 const DlPaint* paint,
278 SrcRectConstraint constraint) {
279 SkOptionalPaint sk_paint(paint);
280 sk_sp<SkImage> sk_image = image->skia_image();
281 delegate_->drawImageRect(sk_image.get(), src, dst, ToSk(sampling), sk_paint(),
282 ToSk(constraint));
283}
284
286 const SkIRect& center,
287 const SkRect& dst,
288 DlFilterMode filter,
289 const DlPaint* paint) {
290 SkOptionalPaint sk_paint(paint);
291 sk_sp<SkImage> sk_image = image->skia_image();
292 delegate_->drawImageNine(sk_image.get(), center, dst, ToSk(filter),
293 sk_paint());
294}
295
297 const SkRSXform xform[],
298 const SkRect tex[],
299 const DlColor colors[],
300 int count,
303 const SkRect* cullRect,
304 const DlPaint* paint) {
305 SkOptionalPaint sk_paint(paint);
306 sk_sp<SkImage> sk_image = atlas->skia_image();
307 const SkColor* sk_colors = reinterpret_cast<const SkColor*>(colors);
308 delegate_->drawAtlas(sk_image.get(), xform, tex, sk_colors, count, ToSk(mode),
309 ToSk(sampling), cullRect, sk_paint());
310}
311
313 SkScalar opacity) {
314 const int restore_count = delegate_->getSaveCount();
315
316 // Figure out whether we can apply the opacity during dispatch or
317 // if we need a saveLayer.
318 if (opacity < SK_Scalar1 && !display_list->can_apply_group_opacity()) {
319 TRACE_EVENT0("flutter", "Canvas::saveLayer");
320 delegate_->saveLayerAlphaf(&display_list->bounds(), opacity);
321 opacity = SK_Scalar1;
322 } else {
323 delegate_->save();
324 }
325
326 DlSkCanvasDispatcher dispatcher(delegate_, opacity);
327 if (display_list->has_rtree()) {
328 display_list->Dispatch(dispatcher, delegate_->getLocalClipBounds());
329 } else {
330 display_list->Dispatch(dispatcher);
331 }
332
333 delegate_->restoreToCount(restore_count);
334}
335
337 SkScalar x,
338 SkScalar y,
339 const DlPaint& paint) {
340 delegate_->drawTextBlob(blob, x, y, ToSk(paint));
341}
342
344 const std::shared_ptr<impeller::TextFrame>& text_frame,
345 SkScalar x,
346 SkScalar y,
347 const DlPaint& paint) {
348 FML_CHECK(false);
349}
350
352 const DlColor color,
353 const SkScalar elevation,
354 bool transparent_occluder,
355 SkScalar dpr) {
356 DlSkCanvasDispatcher::DrawShadow(delegate_, path, color, elevation,
357 transparent_occluder, dpr);
358}
359
361 auto dContext = GrAsDirectContext(delegate_->recordingContext());
362
363 if (dContext) {
364 dContext->flushAndSubmit();
365 }
366}
367
368} // namespace flutter
369
370#endif // !SLIMPELLER
int count
Definition: FontMgrTest.cpp:50
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
uint32_t SkColor
Definition: SkColor.h:37
#define SK_Scalar1
Definition: SkScalar.h:18
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawOval(const SkRect &oval, const SkPaint &paint)
Definition: SkCanvas.cpp:1698
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint &paint)
Definition: SkCanvas.cpp:1710
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawImageNine(const SkImage *image, const SkIRect &center, const SkRect &dst, SkFilterMode filter, const SkPaint *paint=nullptr)
Definition: SkCanvas.cpp:1769
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
SkRect getLocalClipBounds() const
Definition: SkCanvas.cpp:1586
virtual GrRecordingContext * recordingContext() const
Definition: SkCanvas.cpp:1637
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
virtual SkISize getBaseLayerSize() const
Definition: SkCanvas.cpp:369
SkM44 getLocalToDevice() const
Definition: SkCanvas.cpp:1633
int getSaveCount() const
Definition: SkCanvas.cpp:431
void rotate(SkScalar degrees)
Definition: SkCanvas.cpp:1300
void restoreToCount(int saveCount)
Definition: SkCanvas.cpp:478
void drawRRect(const SkRRect &rrect, const SkPaint &paint)
Definition: SkCanvas.cpp:1705
SkMatrix getTotalMatrix() const
Definition: SkCanvas.cpp:1629
void drawArc(const SkRect &oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter, const SkPaint &paint)
Definition: SkCanvas.cpp:2728
void resetMatrix()
Definition: SkCanvas.cpp:1355
void clipPath(const SkPath &path, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1456
SkIRect getDeviceClipBounds() const
Definition: SkCanvas.cpp:1607
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
int save()
Definition: SkCanvas.cpp:447
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void drawAtlas(const SkImage *atlas, const SkRSXform xform[], const SkRect tex[], const SkColor colors[], int count, SkBlendMode mode, const SkSamplingOptions &sampling, const SkRect *cullRect, const SkPaint *paint)
Definition: SkCanvas.cpp:1810
void setMatrix(const SkM44 &matrix)
Definition: SkCanvas.cpp:1349
void drawDRRect(const SkRRect &outer, const SkRRect &inner, const SkPaint &paint)
Definition: SkCanvas.cpp:1645
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
void drawVertices(const SkVertices *vertices, SkBlendMode mode, const SkPaint &paint)
Definition: SkCanvas.cpp:1720
SkImageInfo imageInfo() const
Definition: SkCanvas.cpp:1206
void skew(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1312
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2484
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
bool quickReject(const SkRect &rect) const
Definition: SkCanvas.cpp:1557
void clipRRect(const SkRRect &rrect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1439
int saveLayerAlphaf(const SkRect *bounds, float alpha)
Definition: SkCanvas.cpp:1077
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
static sk_sp< SkPathEffect > Make(const SkScalar intervals[], int count, SkScalar phase)
Definition: SkM44.h:150
static SkMatrix MakeAll(SkScalar scaleX, SkScalar skewX, SkScalar transX, SkScalar skewY, SkScalar scaleY, SkScalar transY, SkScalar pers0, SkScalar pers1, SkScalar pers2)
Definition: SkMatrix.h:179
void setPathEffect(sk_sp< SkPathEffect > pathEffect)
Definition: SkPath.h:59
bool isDefault() const
Definition: dl_paint.h:179
void DrawPoints(PointMode mode, uint32_t count, const SkPoint pts[], const DlPaint &paint) override
void Skew(SkScalar sx, SkScalar sy) override
Definition: dl_sk_canvas.cc:89
SkRect GetDestinationClipBounds() const override
int GetSaveCount() const override
Definition: dl_sk_canvas.cc:69
void TransformReset() override
void DrawArc(const SkRect &bounds, SkScalar start, SkScalar sweep, bool useCenter, const DlPaint &paint) override
SkRect GetLocalClipBounds() const override
void DrawCircle(const SkPoint &center, SkScalar radius, const DlPaint &paint) override
void DrawColor(DlColor color, DlBlendMode mode) override
void DrawTextBlob(const sk_sp< SkTextBlob > &blob, SkScalar x, SkScalar y, const DlPaint &paint) override
void SetTransform(const SkMatrix *matrix) override
void ClipPath(const SkPath &path, ClipOp clip_op, bool is_aa) override
void Transform(const SkMatrix *matrix) override
void DrawRRect(const SkRRect &rrect, const DlPaint &paint) override
void DrawAtlas(const sk_sp< DlImage > &atlas, const SkRSXform xform[], const SkRect tex[], const DlColor colors[], int count, DlBlendMode mode, DlImageSampling sampling, const SkRect *cullRect, const DlPaint *paint=nullptr) override
void ClipRect(const SkRect &rect, ClipOp clip_op, bool is_aa) override
void Translate(SkScalar tx, SkScalar ty) override
Definition: dl_sk_canvas.cc:77
void ClipRRect(const SkRRect &rrect, ClipOp clip_op, bool is_aa) override
void DrawLine(const SkPoint &p0, const SkPoint &p1, const DlPaint &paint) override
void Rotate(SkScalar degrees) override
Definition: dl_sk_canvas.cc:85
void TransformFullPerspective(SkScalar mxx, SkScalar mxy, SkScalar mxz, SkScalar mxt, SkScalar myx, SkScalar myy, SkScalar myz, SkScalar myt, SkScalar mzx, SkScalar mzy, SkScalar mzz, SkScalar mzt, SkScalar mwx, SkScalar mwy, SkScalar mwz, SkScalar mwt) override
void DrawVertices(const DlVertices *vertices, DlBlendMode mode, const DlPaint &paint) override
void DrawImageNine(const sk_sp< DlImage > &image, const SkIRect &center, const SkRect &dst, DlFilterMode filter, const DlPaint *paint=nullptr) override
SkMatrix GetTransform() const override
void DrawRect(const SkRect &rect, const DlPaint &paint) override
void DrawShadow(const SkPath &path, const DlColor color, const SkScalar elevation, bool transparent_occluder, SkScalar dpr) override
void Transform2DAffine(SkScalar mxx, SkScalar mxy, SkScalar mxt, SkScalar myx, SkScalar myy, SkScalar myt) override
Definition: dl_sk_canvas.cc:96
void DrawOval(const SkRect &bounds, const DlPaint &paint) override
void DrawImageRect(const sk_sp< DlImage > &image, const SkRect &src, const SkRect &dst, DlImageSampling sampling, const DlPaint *paint=nullptr, SrcRectConstraint constraint=SrcRectConstraint::kFast) override
SkISize GetBaseLayerSize() const override
Definition: dl_sk_canvas.cc:43
void DrawImage(const sk_sp< DlImage > &image, const SkPoint point, DlImageSampling sampling, const DlPaint *paint=nullptr) override
void DrawTextFrame(const std::shared_ptr< impeller::TextFrame > &text_frame, SkScalar x, SkScalar y, const DlPaint &paint) override
void Scale(SkScalar sx, SkScalar sy) override
Definition: dl_sk_canvas.cc:81
SkM44 GetTransformFullPerspective() const override
void set_canvas(SkCanvas *canvas)
Definition: dl_sk_canvas.cc:39
void DrawDisplayList(const sk_sp< DisplayList > display_list, SkScalar opacity=SK_Scalar1) override
void DrawDashedLine(const DlPoint &p0, const DlPoint &p1, DlScalar on_length, DlScalar off_length, const DlPaint &paint) override
void DrawDRRect(const SkRRect &outer, const SkRRect &inner, const DlPaint &paint) override
void SaveLayer(const SkRect *bounds, const DlPaint *paint=nullptr, const DlImageFilter *backdrop=nullptr) override
Definition: dl_sk_canvas.cc:55
void RestoreToCount(int restore_count) override
Definition: dl_sk_canvas.cc:73
SkImageInfo GetImageInfo() const override
Definition: dl_sk_canvas.cc:47
bool QuickReject(const SkRect &bounds) const override
void DrawPaint(const DlPaint &paint) override
void DrawPath(const SkPath &path, const DlPaint &paint) override
Backend implementation of |DlOpReceiver| for |SkCanvas|.
static void DrawShadow(SkCanvas *canvas, const SkPath &path, DlColor color, float elevation, bool transparentOccluder, SkScalar dpr)
Holds all of the data (both required and optional) for a DisplayList drawVertices call.
Definition: dl_vertices.h:71
SkOptionalPaint(const DlPaint *dl_paint)
Definition: dl_sk_canvas.cc:23
T * get() const
Definition: SkRefCnt.h:303
const Paint & paint
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
#define FML_CHECK(condition)
Definition: logging.h:85
double y
double x
sk_sp< const SkImage > atlas
Definition: SkRecords.h:331
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< const SkImageFilter > backdrop
Definition: SkRecords.h:191
SkRRect rrect
Definition: SkRecords.h:232
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkSamplingOptions sampling
Definition: SkRecords.h:337
impeller::Scalar DlScalar
SkPaint ToSk(const DlPaint &paint)
const SkPoint & ToSkPoint(const DlPoint &point)
SkPaint ToStrokedSk(const DlPaint &paint)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
SkPaint ToNonShaderSk(const DlPaint &paint)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
dst
Definition: cp.py:12
Definition: SkRect.h:32
Definition: SkSize.h:16
float fX
x-axis value
Definition: SkPoint_impl.h:164
float fY
y-axis value
Definition: SkPoint_impl.h:165
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131