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
8 #include "include/utils/SkPaintFilterCanvas.h"
9
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPixmap.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkSurface.h" // IWYU pragma: keep
17 #include "include/core/SkSurfaceProps.h"
18
19 #include <optional>
20
21 class SkData;
22 class SkDrawable;
23 class SkImage;
24 class SkPath;
25 class SkPicture;
26 class SkRRect;
27 class SkRegion;
28 class SkTextBlob;
29 class SkVertices;
30 struct SkDrawShadowRec;
31
32 class SkPaintFilterCanvas::AutoPaintFilter {
33 public:
AutoPaintFilter(const SkPaintFilterCanvas * canvas,const SkPaint * paint)34 AutoPaintFilter(const SkPaintFilterCanvas* canvas, const SkPaint* paint)
35 : fPaint(paint ? *paint : SkPaint()) {
36 fShouldDraw = canvas->onFilter(fPaint);
37 }
38
AutoPaintFilter(const SkPaintFilterCanvas * canvas,const SkPaint & paint)39 AutoPaintFilter(const SkPaintFilterCanvas* canvas, const SkPaint& paint)
40 : AutoPaintFilter(canvas, &paint) { }
41
paint() const42 const SkPaint& paint() const { return fPaint; }
43
shouldDraw() const44 bool shouldDraw() const { return fShouldDraw; }
45
46 private:
47 SkPaint fPaint;
48 bool fShouldDraw;
49 };
50
SkPaintFilterCanvas(SkCanvas * canvas)51 SkPaintFilterCanvas::SkPaintFilterCanvas(SkCanvas *canvas)
52 : SkCanvasVirtualEnforcer<SkNWayCanvas>(canvas->imageInfo().width(),
53 canvas->imageInfo().height()) {
54
55 // Transfer matrix & clip state before adding the target canvas.
56 this->clipRect(SkRect::Make(canvas->getDeviceClipBounds()));
57 this->setMatrix(canvas->getLocalToDevice());
58
59 this->addCanvas(canvas);
60 }
61
onDrawPaint(const SkPaint & paint)62 void SkPaintFilterCanvas::onDrawPaint(const SkPaint& paint) {
63 AutoPaintFilter apf(this, paint);
64 if (apf.shouldDraw()) {
65 this->SkNWayCanvas::onDrawPaint(apf.paint());
66 }
67 }
68
onDrawBehind(const SkPaint & paint)69 void SkPaintFilterCanvas::onDrawBehind(const SkPaint& paint) {
70 AutoPaintFilter apf(this, paint);
71 if (apf.shouldDraw()) {
72 this->SkNWayCanvas::onDrawBehind(apf.paint());
73 }
74 }
75
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)76 void SkPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
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
onDrawRect(const SkRect & rect,const SkPaint & paint)84 void SkPaintFilterCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
85 AutoPaintFilter apf(this, paint);
86 if (apf.shouldDraw()) {
87 this->SkNWayCanvas::onDrawRect(rect, apf.paint());
88 }
89 }
90
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)91 void SkPaintFilterCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
92 AutoPaintFilter apf(this, paint);
93 if (apf.shouldDraw()) {
94 this->SkNWayCanvas::onDrawRRect(rrect, apf.paint());
95 }
96 }
97
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)98 void 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
onDrawRegion(const SkRegion & region,const SkPaint & paint)106 void SkPaintFilterCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
107 AutoPaintFilter apf(this, paint);
108 if (apf.shouldDraw()) {
109 this->SkNWayCanvas::onDrawRegion(region, apf.paint());
110 }
111 }
112
onDrawOval(const SkRect & rect,const SkPaint & paint)113 void SkPaintFilterCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
114 AutoPaintFilter apf(this, paint);
115 if (apf.shouldDraw()) {
116 this->SkNWayCanvas::onDrawOval(rect, apf.paint());
117 }
118 }
119
onDrawArc(const SkRect & rect,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)120 void 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
onDrawPath(const SkPath & path,const SkPaint & paint)128 void SkPaintFilterCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
129 AutoPaintFilter apf(this, paint);
130 if (apf.shouldDraw()) {
131 this->SkNWayCanvas::onDrawPath(path, apf.paint());
132 }
133 }
134
onDrawImage2(const SkImage * image,SkScalar left,SkScalar top,const SkSamplingOptions & sampling,const SkPaint * paint)135 void SkPaintFilterCanvas::onDrawImage2(const SkImage* image, SkScalar left, SkScalar top,
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
onDrawImageRect2(const SkImage * image,const SkRect & src,const SkRect & dst,const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)143 void SkPaintFilterCanvas::onDrawImageRect2(const SkImage* image, const SkRect& src,
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
onDrawImageLattice2(const SkImage * image,const Lattice & lattice,const SkRect & dst,SkFilterMode filter,const SkPaint * paint)152 void SkPaintFilterCanvas::onDrawImageLattice2(const SkImage* image, const Lattice& lattice,
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
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode bmode,const SkPaint & paint)161 void SkPaintFilterCanvas::onDrawVerticesObject(const SkVertices* vertices,
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
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode bmode,const SkPaint & paint)169 void 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
onDrawPicture(const SkPicture * picture,const SkMatrix * m,const SkPaint * originalPaint)178 void SkPaintFilterCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* m,
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
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)200 void SkPaintFilterCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
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
onDrawGlyphRunList(const sktext::GlyphRunList & list,const SkPaint & paint)210 void SkPaintFilterCanvas::onDrawGlyphRunList(
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
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)218 void SkPaintFilterCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
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
onDrawAtlas2(const SkImage * image,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,SkBlendMode bmode,const SkSamplingOptions & sampling,const SkRect * cull,const SkPaint * paint)226 void SkPaintFilterCanvas::onDrawAtlas2(const SkImage* image, const SkRSXform xform[],
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
onDrawAnnotation(const SkRect & rect,const char key[],SkData * value)237 void SkPaintFilterCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
238 this->SkNWayCanvas::onDrawAnnotation(rect, key, value);
239 }
240
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)241 void SkPaintFilterCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
242 this->SkNWayCanvas::onDrawShadowRec(path, rec);
243 }
244
onDrawEdgeAAQuad(const SkRect & rect,const SkPoint clip[4],QuadAAFlags aa,const SkColor4f & color,SkBlendMode mode)245 void SkPaintFilterCanvas::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
246 QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
247 SkPaint paint;
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(),
253 apf.paint().getBlendMode_or(SkBlendMode::kSrcOver));
254 }
255 }
256
onDrawEdgeAAImageSet2(const ImageSetEntry set[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)257 void SkPaintFilterCanvas::onDrawEdgeAAImageSet2(const ImageSetEntry set[], int count,
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()) {
265 this->SkNWayCanvas::onDrawEdgeAAImageSet2(
266 set, count, dstClips, preViewMatrices, sampling, &apf.paint(), constraint);
267 }
268 }
269
onNewSurface(const SkImageInfo & info,const SkSurfaceProps & props)270 sk_sp<SkSurface> SkPaintFilterCanvas::onNewSurface(const SkImageInfo& info,
271 const SkSurfaceProps& props) {
272 return this->proxy()->makeSurface(info, &props);
273 }
274
onPeekPixels(SkPixmap * pixmap)275 bool SkPaintFilterCanvas::onPeekPixels(SkPixmap* pixmap) {
276 return this->proxy()->peekPixels(pixmap);
277 }
278
onAccessTopLayerPixels(SkPixmap * pixmap)279 bool SkPaintFilterCanvas::onAccessTopLayerPixels(SkPixmap* pixmap) {
280 SkImageInfo info;
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
onImageInfo() const292 SkImageInfo SkPaintFilterCanvas::onImageInfo() const {
293 return this->proxy()->imageInfo();
294 }
295
onGetProps(SkSurfaceProps * props,bool top) const296 bool SkPaintFilterCanvas::onGetProps(SkSurfaceProps* props, bool top) const {
297 if (props) {
298 *props = top ? this->proxy()->getTopProps() : this->proxy()->getBaseProps();
299 }
300 return true;
301 }
302