Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
120void SkPaintFilterCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
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
169void SkPaintFilterCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
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,
228 SkBlendMode bmode, const SkSamplingOptions& sampling,
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
237void SkPaintFilterCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
239}
240
242 this->SkNWayCanvas::onDrawShadowRec(path, rec);
243}
244
246 QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
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[],
259 const SkMatrix preViewMatrices[],
260 const SkSamplingOptions& sampling,
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
SkColor4f color
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:3824
static bool left(const SkPoint &p0, const SkPoint &p1)
SkFilterMode
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
bool peekPixels(SkPixmap *pixmap)
SkSurfaceProps getTopProps() const
sk_sp< SkSurface > makeSurface(const SkImageInfo &info, const SkSurfaceProps *props=nullptr)
SrcRectConstraint
Definition SkCanvas.h:1541
SkM44 getLocalToDevice() const
SkIRect getDeviceClipBounds() const
void setMatrix(const SkM44 &matrix)
SkSurfaceProps getBaseProps() const
SkImageInfo imageInfo() const
void * accessTopLayerPixels(SkImageInfo *info, size_t *rowBytes, SkIPoint *origin=nullptr)
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
void reset()
Definition SkPixmap.cpp:32
const Paint & paint
sk_sp< SkImage > image
Definition examples.cpp:29
float SkScalar
Definition extension.cpp:12
uint8_t value
double y
double x
int32_t height
int32_t width
static SkRect Make(const SkISize &size)
Definition SkRect.h:669