Flutter Engine
The Flutter Engine
canvas.cpp
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#include "export.h"
6#include "helpers.h"
7#include "wrappers.h"
8
13
14using namespace skia::textlayout;
15
16using namespace Skwasm;
17
18namespace {
19// These numbers have been chosen empirically to give a result closest to the
20// material spec.
21// These values are also used by the CanvasKit renderer and the native engine.
22// See:
23// flutter/display_list/skia/dl_sk_dispatcher.cc
24// flutter/lib/web_ui/lib/src/engine/canvaskit/util.dart
25constexpr SkScalar kShadowAmbientAlpha = 0.039;
26constexpr SkScalar kShadowSpotAlpha = 0.25;
27constexpr SkScalar kShadowLightRadius = 1.1;
28constexpr SkScalar kShadowLightHeight = 600.0;
29constexpr SkScalar kShadowLightXOffset = 0;
30constexpr SkScalar kShadowLightYOffset = -450;
31} // namespace
32
34 SkRect* rect,
38}
39
41 canvas->save();
42}
43
45 canvas->restore();
46}
47
49 canvas->restoreToCount(count);
50}
51
53 return canvas->getSaveCount();
54}
55
58 SkScalar dy) {
59 canvas->translate(dx, dy);
60}
61
63 canvas->scale(sx, sy);
64}
65
67 canvas->rotate(degrees);
68}
69
71 canvas->skew(sx, sy);
72}
73
74SKWASM_EXPORT void canvas_transform(SkCanvas* canvas, const SkM44* matrix44) {
75 canvas->concat(*matrix44);
76}
77
79 const SkRect* rect,
80 SkClipOp op,
81 bool antialias) {
82 canvas->clipRect(*rect, op, antialias);
83}
84
86 const SkScalar* rrectValues,
87 bool antialias) {
88 canvas->clipRRect(createRRect(rrectValues), antialias);
89}
90
92 SkPath* path,
93 bool antialias) {
94 canvas->clipPath(*path, antialias);
95}
96
99 SkBlendMode blendMode) {
100 canvas->drawColor(color, blendMode);
101}
102
104 SkScalar x1,
105 SkScalar y1,
106 SkScalar x2,
107 SkScalar y2,
108 SkPaint* paint) {
109 canvas->drawLine(x1, y1, x2, y2, *paint);
110}
111
113 canvas->drawPaint(*paint);
114}
115
117 SkRect* rect,
118 SkPaint* paint) {
119 canvas->drawRect(*rect, *paint);
120}
121
123 const SkScalar* rrectValues,
124 SkPaint* paint) {
125 canvas->drawRRect(createRRect(rrectValues), *paint);
126}
127
129 const SkScalar* outerRrectValues,
130 const SkScalar* innerRrectValues,
131 SkPaint* paint) {
132 canvas->drawDRRect(createRRect(outerRrectValues),
133 createRRect(innerRrectValues), *paint);
134}
135
137 const SkRect* rect,
138 SkPaint* paint) {
139 canvas->drawOval(*rect, *paint);
140}
141
143 SkScalar x,
144 SkScalar y,
145 SkScalar radius,
146 SkPaint* paint) {
147 canvas->drawCircle(x, y, radius, *paint);
148}
149
151 const SkRect* rect,
152 SkScalar startAngleDegrees,
153 SkScalar sweepAngleDegrees,
154 bool useCenter,
155 SkPaint* paint) {
156 canvas->drawArc(*rect, startAngleDegrees, sweepAngleDegrees, useCenter,
157 *paint);
158}
159
161 SkPath* path,
162 SkPaint* paint) {
163 canvas->drawPath(*path, *paint);
164}
165
167 SkPath* path,
168 SkScalar elevation,
169 SkScalar devicePixelRatio,
171 bool transparentOccluder) {
172 SkColor inAmbient =
173 SkColorSetA(color, kShadowAmbientAlpha * SkColorGetA(color));
174 SkColor inSpot = SkColorSetA(color, kShadowSpotAlpha * SkColorGetA(color));
175 SkColor outAmbient;
176 SkColor outSpot;
177 SkShadowUtils::ComputeTonalColors(inAmbient, inSpot, &outAmbient, &outSpot);
178 uint32_t flags = transparentOccluder
183 canvas, *path, SkPoint3::Make(0.0f, 0.0f, elevation * devicePixelRatio),
184 SkPoint3::Make(kShadowLightXOffset, kShadowLightYOffset,
185 kShadowLightHeight * devicePixelRatio),
186 devicePixelRatio * kShadowLightRadius, outAmbient, outSpot, flags);
187}
188
190 Paragraph* paragraph,
191 SkScalar x,
192 SkScalar y) {
193 paragraph->paint(canvas, x, y);
194}
195
197 canvas->drawPicture(picture);
198}
199
201 SkImage* image,
204 SkPaint* paint,
205 FilterQuality quality) {
207 paint);
208}
209
211 SkImage* image,
212 SkRect* sourceRect,
213 SkRect* destRect,
214 SkPaint* paint,
215 FilterQuality quality) {
216 canvas->drawImageRect(image, *sourceRect, *destRect,
219}
220
222 SkImage* image,
223 SkIRect* centerRect,
224 SkRect* destinationRect,
225 SkPaint* paint,
226 FilterQuality quality) {
227 canvas->drawImageNine(image, *centerRect, *destinationRect,
228 filterModeForQuality(quality), paint);
229}
230
232 SkVertices* vertices,
234 SkPaint* paint) {
235 canvas->drawVertices(sk_ref_sp<SkVertices>(vertices), mode, *paint);
236}
237
241 int pointCount,
242 SkPaint* paint) {
243 canvas->drawPoints(mode, pointCount, points, *paint);
244}
245
247 SkImage* atlas,
248 SkRSXform* transforms,
249 SkRect* rects,
251 int spriteCount,
253 SkRect* cullRect,
254 SkPaint* paint) {
255 canvas->drawAtlas(
256 atlas, transforms, rects, colors, spriteCount, mode,
258 paint);
259}
260
261SKWASM_EXPORT void canvas_getTransform(SkCanvas* canvas, SkM44* outTransform) {
262 *outTransform = canvas->getLocalToDevice();
263}
264
266 SkRect* outRect) {
267 *outRect = canvas->getLocalClipBounds();
268}
269
271 SkIRect* outRect) {
272 *outRect = canvas->getDeviceClipBounds();
273}
int count
Definition: FontMgrTest.cpp:50
static const int points[]
SkBlendMode
Definition: SkBlendMode.h:38
SkClipOp
Definition: SkClipOp.h:13
uint32_t SkColor
Definition: SkColor.h:37
static constexpr SkColor SkColorSetA(SkColor c, U8CPU a)
Definition: SkColor.h:82
#define SkColorGetA(color)
Definition: SkColor.h:61
@ kDirectionalLight_ShadowFlag
Definition: SkShadowUtils.h:31
@ kTransparentOccluder_ShadowFlag
Definition: SkShadowUtils.h:27
@ kNone_ShadowFlag
Definition: SkShadowUtils.h:24
SKWASM_EXPORT void canvas_drawPicture(SkCanvas *canvas, SkPicture *picture)
Definition: canvas.cpp:196
SKWASM_EXPORT void canvas_translate(SkCanvas *canvas, SkScalar dx, SkScalar dy)
Definition: canvas.cpp:56
SKWASM_EXPORT void canvas_saveLayer(SkCanvas *canvas, SkRect *rect, SkPaint *paint, SkImageFilter *backdrop)
Definition: canvas.cpp:33
SKWASM_EXPORT void canvas_drawArc(SkCanvas *canvas, const SkRect *rect, SkScalar startAngleDegrees, SkScalar sweepAngleDegrees, bool useCenter, SkPaint *paint)
Definition: canvas.cpp:150
SKWASM_EXPORT void canvas_transform(SkCanvas *canvas, const SkM44 *matrix44)
Definition: canvas.cpp:74
SKWASM_EXPORT void canvas_skew(SkCanvas *canvas, SkScalar sx, SkScalar sy)
Definition: canvas.cpp:70
SKWASM_EXPORT void canvas_clipRRect(SkCanvas *canvas, const SkScalar *rrectValues, bool antialias)
Definition: canvas.cpp:85
SKWASM_EXPORT void canvas_drawLine(SkCanvas *canvas, SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkPaint *paint)
Definition: canvas.cpp:103
SKWASM_EXPORT void canvas_drawImageNine(SkCanvas *canvas, SkImage *image, SkIRect *centerRect, SkRect *destinationRect, SkPaint *paint, FilterQuality quality)
Definition: canvas.cpp:221
SKWASM_EXPORT int canvas_getSaveCount(SkCanvas *canvas)
Definition: canvas.cpp:52
SKWASM_EXPORT void canvas_drawImageRect(SkCanvas *canvas, SkImage *image, SkRect *sourceRect, SkRect *destRect, SkPaint *paint, FilterQuality quality)
Definition: canvas.cpp:210
SKWASM_EXPORT void canvas_restore(SkCanvas *canvas)
Definition: canvas.cpp:44
SKWASM_EXPORT void canvas_drawPaint(SkCanvas *canvas, SkPaint *paint)
Definition: canvas.cpp:112
SKWASM_EXPORT void canvas_drawOval(SkCanvas *canvas, const SkRect *rect, SkPaint *paint)
Definition: canvas.cpp:136
SKWASM_EXPORT void canvas_drawAtlas(SkCanvas *canvas, SkImage *atlas, SkRSXform *transforms, SkRect *rects, SkColor *colors, int spriteCount, SkBlendMode mode, SkRect *cullRect, SkPaint *paint)
Definition: canvas.cpp:246
SKWASM_EXPORT void canvas_drawRRect(SkCanvas *canvas, const SkScalar *rrectValues, SkPaint *paint)
Definition: canvas.cpp:122
SKWASM_EXPORT void canvas_rotate(SkCanvas *canvas, SkScalar degrees)
Definition: canvas.cpp:66
SKWASM_EXPORT void canvas_getTransform(SkCanvas *canvas, SkM44 *outTransform)
Definition: canvas.cpp:261
SKWASM_EXPORT void canvas_drawPath(SkCanvas *canvas, SkPath *path, SkPaint *paint)
Definition: canvas.cpp:160
SKWASM_EXPORT void canvas_scale(SkCanvas *canvas, SkScalar sx, SkScalar sy)
Definition: canvas.cpp:62
SKWASM_EXPORT void canvas_drawDRRect(SkCanvas *canvas, const SkScalar *outerRrectValues, const SkScalar *innerRrectValues, SkPaint *paint)
Definition: canvas.cpp:128
SKWASM_EXPORT void canvas_drawColor(SkCanvas *canvas, SkColor color, SkBlendMode blendMode)
Definition: canvas.cpp:97
SKWASM_EXPORT void canvas_drawShadow(SkCanvas *canvas, SkPath *path, SkScalar elevation, SkScalar devicePixelRatio, SkColor color, bool transparentOccluder)
Definition: canvas.cpp:166
SKWASM_EXPORT void canvas_drawVertices(SkCanvas *canvas, SkVertices *vertices, SkBlendMode mode, SkPaint *paint)
Definition: canvas.cpp:231
SKWASM_EXPORT void canvas_drawParagraph(SkCanvas *canvas, Paragraph *paragraph, SkScalar x, SkScalar y)
Definition: canvas.cpp:189
SKWASM_EXPORT void canvas_getDeviceClipBounds(SkCanvas *canvas, SkIRect *outRect)
Definition: canvas.cpp:270
SKWASM_EXPORT void canvas_drawRect(SkCanvas *canvas, SkRect *rect, SkPaint *paint)
Definition: canvas.cpp:116
SKWASM_EXPORT void canvas_drawImage(SkCanvas *canvas, SkImage *image, SkScalar offsetX, SkScalar offsetY, SkPaint *paint, FilterQuality quality)
Definition: canvas.cpp:200
SKWASM_EXPORT void canvas_drawPoints(SkCanvas *canvas, SkCanvas::PointMode mode, SkPoint *points, int pointCount, SkPaint *paint)
Definition: canvas.cpp:238
SKWASM_EXPORT void canvas_save(SkCanvas *canvas)
Definition: canvas.cpp:40
SKWASM_EXPORT void canvas_clipPath(SkCanvas *canvas, SkPath *path, bool antialias)
Definition: canvas.cpp:91
SKWASM_EXPORT void canvas_drawCircle(SkCanvas *canvas, SkScalar x, SkScalar y, SkScalar radius, SkPaint *paint)
Definition: canvas.cpp:142
SKWASM_EXPORT void canvas_clipRect(SkCanvas *canvas, const SkRect *rect, SkClipOp op, bool antialias)
Definition: canvas.cpp:78
SKWASM_EXPORT void canvas_getLocalClipBounds(SkCanvas *canvas, SkRect *outRect)
Definition: canvas.cpp:265
SKWASM_EXPORT void canvas_restoreToCount(SkCanvas *canvas, int count)
Definition: canvas.cpp:48
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
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
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition: SkCanvas.h:1542
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
void drawArc(const SkRect &oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter, const SkPaint &paint)
Definition: SkCanvas.cpp:2728
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 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 drawPicture(const SkPicture *picture)
Definition: SkCanvas.h:1961
void drawVertices(const SkVertices *vertices, SkBlendMode mode, const SkPaint &paint)
Definition: SkCanvas.cpp:1720
void skew(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1312
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
void clipRRect(const SkRRect &rrect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1439
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
Definition: SkM44.h:150
Definition: SkPath.h:59
static void DrawShadow(SkCanvas *canvas, const SkPath &path, const SkPoint3 &zPlaneParams, const SkPoint3 &lightPos, SkScalar lightRadius, SkColor ambientColor, SkColor spotColor, uint32_t flags=SkShadowFlags::kNone_ShadowFlag)
static void ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor, SkColor *outAmbientColor, SkColor *outSpotColor)
virtual void paint(SkCanvas *canvas, SkScalar x, SkScalar y)=0
const Paint & paint
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
FlutterSemanticsFlag flags
double y
double x
sk_sp< const SkImage > atlas
Definition: SkRecords.h:331
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
sk_sp< const SkImageFilter > backdrop
Definition: SkRecords.h:191
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkColor > colors
Definition: SkRecords.h:276
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
Definition: helpers.h:12
SkRRect createRRect(const SkScalar *f)
Definition: helpers.h:19
SkSamplingOptions samplingOptionsForQuality(FilterQuality quality)
Definition: helpers.h:47
FilterQuality
Definition: helpers.h:29
SkFilterMode filterModeForQuality(FilterQuality quality)
Definition: helpers.h:36
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
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
SkScalar offsetX
SkScalar offsetY
Definition: SkRect.h:32
static SkPoint3 Make(SkScalar x, SkScalar y, SkScalar z)
Definition: SkPoint3.h:18
#define SKWASM_EXPORT
Definition: export.h:10