Flutter Engine
The Flutter Engine
SkPaintFilterCanvas.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
15#include "include/core/SkRect.h"
16#include "include/core/SkSurface.h" // IWYU pragma: keep
18
19#include <optional>
20
21class SkData;
22class SkDrawable;
23class SkImage;
24class SkPath;
25class SkPicture;
26class SkRRect;
27class SkRegion;
28class SkTextBlob;
29class SkVertices;
30struct SkDrawShadowRec;
31
33public:
35 : fPaint(paint ? *paint : SkPaint()) {
36 fShouldDraw = canvas->onFilter(fPaint);
37 }
38
40 : AutoPaintFilter(canvas, &paint) { }
41
42 const SkPaint& paint() const { return fPaint; }
43
44 bool shouldDraw() const { return fShouldDraw; }
45
46private:
47 SkPaint fPaint;
48 bool fShouldDraw;
49};
50
52 : SkCanvasVirtualEnforcer<SkNWayCanvas>(canvas->imageInfo().width(),
53 canvas->imageInfo().height()) {
54
55 // Transfer matrix & clip state before adding the target canvas.
57 this->setMatrix(canvas->getLocalToDevice());
58
59 this->addCanvas(canvas);
60}
61
63 AutoPaintFilter apf(this, paint);
64 if (apf.shouldDraw()) {
66 }
67}
68
70 AutoPaintFilter apf(this, paint);
71 if (apf.shouldDraw()) {
73 }
74}
75
77 const SkPaint& paint) {
78 AutoPaintFilter apf(this, paint);
79 if (apf.shouldDraw()) {
80 this->SkNWayCanvas::onDrawPoints(mode, count, pts, apf.paint());
81 }
82}
83
85 AutoPaintFilter apf(this, paint);
86 if (apf.shouldDraw()) {
87 this->SkNWayCanvas::onDrawRect(rect, apf.paint());
88 }
89}
90
92 AutoPaintFilter apf(this, paint);
93 if (apf.shouldDraw()) {
94 this->SkNWayCanvas::onDrawRRect(rrect, apf.paint());
95 }
96}
97
98void SkPaintFilterCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
99 const SkPaint& paint) {
100 AutoPaintFilter apf(this, paint);
101 if (apf.shouldDraw()) {
102 this->SkNWayCanvas::onDrawDRRect(outer, inner, apf.paint());
103 }
104}
105
107 AutoPaintFilter apf(this, paint);
108 if (apf.shouldDraw()) {
109 this->SkNWayCanvas::onDrawRegion(region, apf.paint());
110 }
111}
112
114 AutoPaintFilter apf(this, paint);
115 if (apf.shouldDraw()) {
116 this->SkNWayCanvas::onDrawOval(rect, apf.paint());
117 }
118}
119
121 bool useCenter, const SkPaint& paint) {
122 AutoPaintFilter apf(this, paint);
123 if (apf.shouldDraw()) {
124 this->SkNWayCanvas::onDrawArc(rect, startAngle, sweepAngle, useCenter, apf.paint());
125 }
126}
127
129 AutoPaintFilter apf(this, paint);
130 if (apf.shouldDraw()) {
131 this->SkNWayCanvas::onDrawPath(path, apf.paint());
132 }
133}
134
136 const SkSamplingOptions& sampling, const SkPaint* paint) {
137 AutoPaintFilter apf(this, paint);
138 if (apf.shouldDraw()) {
139 this->SkNWayCanvas::onDrawImage2(image, left, top, sampling, &apf.paint());
140 }
141}
142
144 const SkRect& dst, const SkSamplingOptions& sampling,
145 const SkPaint* paint, SrcRectConstraint constraint) {
146 AutoPaintFilter apf(this, paint);
147 if (apf.shouldDraw()) {
148 this->SkNWayCanvas::onDrawImageRect2(image, src, dst, sampling, &apf.paint(), constraint);
149 }
150}
151
153 const SkRect& dst, SkFilterMode filter,
154 const SkPaint* paint) {
155 AutoPaintFilter apf(this, paint);
156 if (apf.shouldDraw()) {
157 this->SkNWayCanvas::onDrawImageLattice2(image, lattice, dst, filter, &apf.paint());
158 }
159}
160
162 SkBlendMode bmode, const SkPaint& paint) {
163 AutoPaintFilter apf(this, paint);
164 if (apf.shouldDraw()) {
165 this->SkNWayCanvas::onDrawVerticesObject(vertices, bmode, apf.paint());
166 }
167}
168
170 const SkPoint texCoords[4], SkBlendMode bmode,
171 const SkPaint& paint) {
172 AutoPaintFilter apf(this, paint);
173 if (apf.shouldDraw()) {
174 this->SkNWayCanvas::onDrawPatch(cubics, colors, texCoords, bmode, apf.paint());
175 }
176}
177
179 const SkPaint* originalPaint) {
180 AutoPaintFilter apf(this, originalPaint);
181 if (apf.shouldDraw()) {
182 const SkPaint* newPaint = &apf.paint();
183
184 // Passing a paint (-vs- passing null) makes drawPicture draw into a layer...
185 // much slower, and can produce different blending. Thus we should only do this
186 // if the filter's effect actually impacts the picture.
187 if (originalPaint == nullptr) {
188 if ( newPaint->getAlphaf() == 1.0f
189 && newPaint->getColorFilter() == nullptr
190 && newPaint->getImageFilter() == nullptr
191 && newPaint->asBlendMode() == SkBlendMode::kSrcOver) {
192 // restore the original nullptr
193 newPaint = nullptr;
194 }
195 }
196 this->SkNWayCanvas::onDrawPicture(picture, m, newPaint);
197 }
198}
199
201 // There is no paint to filter in this case, but we can still filter on type.
202 // Subclasses need to unroll the drawable explicity (by overriding this method) in
203 // order to actually filter nested content.
204 AutoPaintFilter apf(this, nullptr);
205 if (apf.shouldDraw()) {
206 this->SkNWayCanvas::onDrawDrawable(drawable, matrix);
207 }
208}
209
211 const sktext::GlyphRunList& list, const SkPaint& paint) {
212 AutoPaintFilter apf(this, paint);
213 if (apf.shouldDraw()) {
214 this->SkNWayCanvas::onDrawGlyphRunList(list, apf.paint());
215 }
216}
217
219 const SkPaint& paint) {
220 AutoPaintFilter apf(this, paint);
221 if (apf.shouldDraw()) {
222 this->SkNWayCanvas::onDrawTextBlob(blob, x, y, apf.paint());
223 }
224}
225
227 const SkRect tex[], const SkColor colors[], int count,
229 const SkRect* cull, const SkPaint* paint) {
230 AutoPaintFilter apf(this, paint);
231 if (apf.shouldDraw()) {
232 this->SkNWayCanvas::onDrawAtlas2(image, xform, tex, colors, count, bmode, sampling, cull,
233 &apf.paint());
234 }
235}
236
239}
240
242 this->SkNWayCanvas::onDrawShadowRec(path, rec);
243}
244
248 paint.setColor(color);
249 paint.setBlendMode(mode);
250 AutoPaintFilter apf(this, paint);
251 if (apf.shouldDraw()) {
252 this->SkNWayCanvas::onDrawEdgeAAQuad(rect, clip, aa, apf.paint().getColor4f(),
254 }
255}
256
258 const SkPoint dstClips[],
261 const SkPaint* paint,
262 SrcRectConstraint constraint) {
263 AutoPaintFilter apf(this, paint);
264 if (apf.shouldDraw()) {
266 set, count, dstClips, preViewMatrices, sampling, &apf.paint(), constraint);
267 }
268}
269
271 const SkSurfaceProps& props) {
272 return this->proxy()->makeSurface(info, &props);
273}
274
276 return this->proxy()->peekPixels(pixmap);
277}
278
281 size_t rowBytes;
282
283 void* addr = this->proxy()->accessTopLayerPixels(&info, &rowBytes);
284 if (!addr) {
285 return false;
286 }
287
288 pixmap->reset(info, addr, rowBytes);
289 return true;
290}
291
293 return this->proxy()->imageInfo();
294}
295
297 if (props) {
298 *props = top ? this->proxy()->getTopProps() : this->proxy()->getBaseProps();
299 }
300 return true;
301}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
int count
Definition: FontMgrTest.cpp:50
SkBlendMode
Definition: SkBlendMode.h:38
@ kSrcOver
r = s + (1-sa)*d
uint32_t SkColor
Definition: SkColor.h:37
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
static bool left(const SkPoint &p0, const SkPoint &p1)
SkFilterMode
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
bool peekPixels(SkPixmap *pixmap)
Definition: SkCanvas.cpp:1237
SkSurfaceProps getTopProps() const
Definition: SkCanvas.cpp:1224
sk_sp< SkSurface > makeSurface(const SkImageInfo &info, const SkSurfaceProps *props=nullptr)
Definition: SkCanvas.cpp:1195
SrcRectConstraint
Definition: SkCanvas.h:1541
SkM44 getLocalToDevice() const
Definition: SkCanvas.cpp:1633
SkIRect getDeviceClipBounds() const
Definition: SkCanvas.cpp:1607
void setMatrix(const SkM44 &matrix)
Definition: SkCanvas.cpp:1349
SkSurfaceProps getBaseProps() const
Definition: SkCanvas.cpp:1218
SkImageInfo imageInfo() const
Definition: SkCanvas.cpp:1206
void * accessTopLayerPixels(SkImageInfo *info, size_t *rowBytes, SkIPoint *origin=nullptr)
Definition: SkCanvas.cpp:1245
Definition: SkData.h:25
void onDrawDRRect(const SkRRect &, const SkRRect &, const SkPaint &) override
void onDrawRRect(const SkRRect &, const SkPaint &) override
virtual void addCanvas(SkCanvas *)
void onDrawRect(const SkRect &, const SkPaint &) override
void onDrawVerticesObject(const SkVertices *, SkBlendMode, const SkPaint &) override
void onDrawArc(const SkRect &, SkScalar, SkScalar, bool, const SkPaint &) override
void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode, const SkPaint &paint) override
void onDrawPath(const SkPath &, const SkPaint &) override
void onDrawPicture(const SkPicture *, const SkMatrix *, const SkPaint *) override
void onDrawImageLattice2(const SkImage *, const Lattice &, const SkRect &, SkFilterMode, const SkPaint *) override
void onDrawRegion(const SkRegion &, const SkPaint &) override
void onDrawAnnotation(const SkRect &, const char[], SkData *) override
void onDrawDrawable(SkDrawable *, const SkMatrix *) override
void onDrawBehind(const SkPaint &) override
void onDrawImage2(const SkImage *, SkScalar, SkScalar, const SkSamplingOptions &, const SkPaint *) override
void onDrawAtlas2(const SkImage *, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode, const SkSamplingOptions &, const SkRect *, const SkPaint *) override
void onDrawPaint(const SkPaint &) override
void onDrawShadowRec(const SkPath &, const SkDrawShadowRec &) override
void onDrawEdgeAAImageSet2(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[], const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
void onDrawImageRect2(const SkImage *, const SkRect &, const SkRect &, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
void onDrawEdgeAAQuad(const SkRect &, const SkPoint[4], QuadAAFlags, const SkColor4f &, SkBlendMode) override
void onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint) override
void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint &) override
void onDrawGlyphRunList(const sktext::GlyphRunList &, const SkPaint &) override
void onDrawOval(const SkRect &, const SkPaint &) override
AutoPaintFilter(const SkPaintFilterCanvas *canvas, const SkPaint &paint)
AutoPaintFilter(const SkPaintFilterCanvas *canvas, const SkPaint *paint)
sk_sp< SkSurface > onNewSurface(const SkImageInfo &, const SkSurfaceProps &) override
SkPaintFilterCanvas(SkCanvas *canvas)
bool onAccessTopLayerPixels(SkPixmap *pixmap) override
void onDrawDRRect(const SkRRect &, const SkRRect &, const SkPaint &) override
void onDrawBehind(const SkPaint &) override
void onDrawAnnotation(const SkRect &rect, const char key[], SkData *value) override
void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint &) override
void onDrawArc(const SkRect &, SkScalar, SkScalar, bool, const SkPaint &) override
void onDrawPath(const SkPath &, const SkPaint &) override
void onDrawImageRect2(const SkImage *, const SkRect &, const SkRect &, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
void onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint) override
virtual bool onFilter(SkPaint &paint) const =0
void onDrawVerticesObject(const SkVertices *, SkBlendMode, const SkPaint &) override
void onDrawRect(const SkRect &, const SkPaint &) override
SkImageInfo onImageInfo() const override
bool onGetProps(SkSurfaceProps *props, bool top) const override
void onDrawAtlas2(const SkImage *, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode, const SkSamplingOptions &, const SkRect *, const SkPaint *) override
void onDrawImageLattice2(const SkImage *, const Lattice &, const SkRect &, SkFilterMode, const SkPaint *) override
void onDrawRegion(const SkRegion &, const SkPaint &) override
void onDrawImage2(const SkImage *, SkScalar, SkScalar, const SkSamplingOptions &, const SkPaint *) override
void onDrawGlyphRunList(const sktext::GlyphRunList &, const SkPaint &) override
void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode, const SkPaint &paint) override
void onDrawShadowRec(const SkPath &path, const SkDrawShadowRec &rec) override
void onDrawEdgeAAImageSet2(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[], const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
void onDrawRRect(const SkRRect &, const SkPaint &) override
void onDrawOval(const SkRect &, const SkPaint &) override
void onDrawPaint(const SkPaint &) override
void onDrawEdgeAAQuad(const SkRect &, const SkPoint[4], QuadAAFlags, const SkColor4f &, SkBlendMode) override
void onDrawPicture(const SkPicture *, const SkMatrix *, const SkPaint *) override
void onDrawDrawable(SkDrawable *, const SkMatrix *) override
bool onPeekPixels(SkPixmap *pixmap) override
SkBlendMode getBlendMode_or(SkBlendMode defaultMode) const
Definition: SkPaint.cpp:143
SkColorFilter * getColorFilter() const
Definition: SkPaint.h:426
SkColor4f getColor4f() const
Definition: SkPaint.h:232
float getAlphaf() const
Definition: SkPaint.h:261
SkImageFilter * getImageFilter() const
Definition: SkPaint.h:564
std::optional< SkBlendMode > asBlendMode() const
Definition: SkPaint.cpp:138
Definition: SkPath.h:59
void reset()
Definition: SkPixmap.cpp:32
const Paint & paint
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
uint8_t value
double y
double x
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
PODArray< SkPoint > dstClips
Definition: SkRecords.h:364
sk_sp< const SkImage > image
Definition: SkRecords.h:269
ClipOpAndAA opAA SkRegion region
Definition: SkRecords.h:238
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
SkRRect rrect
Definition: SkRecords.h:232
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkMatrix > preViewMatrices
Definition: SkRecords.h:365
SkScalar startAngle
Definition: SkRecords.h:250
SkScalar sweepAngle
Definition: SkRecords.h:251
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkSamplingOptions sampling
Definition: SkRecords.h:337
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
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 to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not set
Definition: switches.h:76
dst
Definition: cp.py:12
int32_t height
int32_t width
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669