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/SkPaint.h"
11 #include "include/core/SkPixmap.h"
12 #include "include/core/SkSurface.h"
13 #include "src/core/SkCanvasPriv.h"
14 #include "src/core/SkTLazy.h"
15
16 class SkPaintFilterCanvas::AutoPaintFilter {
17 public:
AutoPaintFilter(const SkPaintFilterCanvas * canvas,const SkPaint * paint)18 AutoPaintFilter(const SkPaintFilterCanvas* canvas, const SkPaint* paint)
19 : fPaint(paint ? *paint : SkPaint()) {
20 fShouldDraw = canvas->onFilter(fPaint);
21 }
22
AutoPaintFilter(const SkPaintFilterCanvas * canvas,const SkPaint & paint)23 AutoPaintFilter(const SkPaintFilterCanvas* canvas, const SkPaint& paint)
24 : AutoPaintFilter(canvas, &paint) { }
25
paint() const26 const SkPaint& paint() const { return fPaint; }
27
shouldDraw() const28 bool shouldDraw() const { return fShouldDraw; }
29
30 private:
31 SkPaint fPaint;
32 bool fShouldDraw;
33 };
34
SkPaintFilterCanvas(SkCanvas * canvas)35 SkPaintFilterCanvas::SkPaintFilterCanvas(SkCanvas *canvas)
36 : SkCanvasVirtualEnforcer<SkNWayCanvas>(canvas->imageInfo().width(),
37 canvas->imageInfo().height()) {
38
39 // Transfer matrix & clip state before adding the target canvas.
40 this->clipRect(SkRect::Make(canvas->getDeviceClipBounds()));
41 this->setMatrix(canvas->getLocalToDevice());
42
43 this->addCanvas(canvas);
44 }
45
onDrawPaint(const SkPaint & paint)46 void SkPaintFilterCanvas::onDrawPaint(const SkPaint& paint) {
47 AutoPaintFilter apf(this, paint);
48 if (apf.shouldDraw()) {
49 this->SkNWayCanvas::onDrawPaint(apf.paint());
50 }
51 }
52
onDrawBehind(const SkPaint & paint)53 void SkPaintFilterCanvas::onDrawBehind(const SkPaint& paint) {
54 AutoPaintFilter apf(this, paint);
55 if (apf.shouldDraw()) {
56 this->SkNWayCanvas::onDrawBehind(apf.paint());
57 }
58 }
59
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)60 void SkPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
61 const SkPaint& paint) {
62 AutoPaintFilter apf(this, paint);
63 if (apf.shouldDraw()) {
64 this->SkNWayCanvas::onDrawPoints(mode, count, pts, apf.paint());
65 }
66 }
67
onDrawBlurImage(const SkImage * image,const SkBlurArg & blurArg)68 bool SkPaintFilterCanvas::onDrawBlurImage(const SkImage* image, const SkBlurArg& blurArg)
69 {
70 return this->SkNWayCanvas::onDrawBlurImage(image, blurArg);
71 }
72
onDrawRect(const SkRect & rect,const SkPaint & paint)73 void SkPaintFilterCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
74 AutoPaintFilter apf(this, paint);
75 if (apf.shouldDraw()) {
76 this->SkNWayCanvas::onDrawRect(rect, apf.paint());
77 }
78 }
79
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)80 void SkPaintFilterCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
81 AutoPaintFilter apf(this, paint);
82 if (apf.shouldDraw()) {
83 this->SkNWayCanvas::onDrawRRect(rrect, apf.paint());
84 }
85 }
86
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)87 void SkPaintFilterCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
88 const SkPaint& paint) {
89 AutoPaintFilter apf(this, paint);
90 if (apf.shouldDraw()) {
91 this->SkNWayCanvas::onDrawDRRect(outer, inner, apf.paint());
92 }
93 }
94
onDrawRegion(const SkRegion & region,const SkPaint & paint)95 void SkPaintFilterCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
96 AutoPaintFilter apf(this, paint);
97 if (apf.shouldDraw()) {
98 this->SkNWayCanvas::onDrawRegion(region, apf.paint());
99 }
100 }
101
onDrawOval(const SkRect & rect,const SkPaint & paint)102 void SkPaintFilterCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
103 AutoPaintFilter apf(this, paint);
104 if (apf.shouldDraw()) {
105 this->SkNWayCanvas::onDrawOval(rect, apf.paint());
106 }
107 }
108
onDrawArc(const SkRect & rect,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)109 void SkPaintFilterCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
110 bool useCenter, const SkPaint& paint) {
111 AutoPaintFilter apf(this, paint);
112 if (apf.shouldDraw()) {
113 this->SkNWayCanvas::onDrawArc(rect, startAngle, sweepAngle, useCenter, apf.paint());
114 }
115 }
116
onDrawPath(const SkPath & path,const SkPaint & paint)117 void SkPaintFilterCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
118 AutoPaintFilter apf(this, paint);
119 if (apf.shouldDraw()) {
120 this->SkNWayCanvas::onDrawPath(path, apf.paint());
121 }
122 }
123
onDrawImage2(const SkImage * image,SkScalar left,SkScalar top,const SkSamplingOptions & sampling,const SkPaint * paint)124 void SkPaintFilterCanvas::onDrawImage2(const SkImage* image, SkScalar left, SkScalar top,
125 const SkSamplingOptions& sampling, const SkPaint* paint) {
126 AutoPaintFilter apf(this, paint);
127 if (apf.shouldDraw()) {
128 this->SkNWayCanvas::onDrawImage2(image, left, top, sampling, &apf.paint());
129 }
130 }
131
onDrawImageRect2(const SkImage * image,const SkRect & src,const SkRect & dst,const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)132 void SkPaintFilterCanvas::onDrawImageRect2(const SkImage* image, const SkRect& src,
133 const SkRect& dst, const SkSamplingOptions& sampling,
134 const SkPaint* paint, SrcRectConstraint constraint) {
135 AutoPaintFilter apf(this, paint);
136 if (apf.shouldDraw()) {
137 this->SkNWayCanvas::onDrawImageRect2(image, src, dst, sampling, &apf.paint(), constraint);
138 }
139 }
140
onDrawImageLattice2(const SkImage * image,const Lattice & lattice,const SkRect & dst,SkFilterMode filter,const SkPaint * paint)141 void SkPaintFilterCanvas::onDrawImageLattice2(const SkImage* image, const Lattice& lattice,
142 const SkRect& dst, SkFilterMode filter,
143 const SkPaint* paint) {
144 AutoPaintFilter apf(this, paint);
145 if (apf.shouldDraw()) {
146 this->SkNWayCanvas::onDrawImageLattice2(image, lattice, dst, filter, &apf.paint());
147 }
148 }
149
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode bmode,const SkPaint & paint)150 void SkPaintFilterCanvas::onDrawVerticesObject(const SkVertices* vertices,
151 SkBlendMode bmode, const SkPaint& paint) {
152 AutoPaintFilter apf(this, paint);
153 if (apf.shouldDraw()) {
154 this->SkNWayCanvas::onDrawVerticesObject(vertices, bmode, apf.paint());
155 }
156 }
157
onDrawPatch(const SkPoint cubics[],const SkColor colors[],const SkPoint texCoords[],SkBlendMode bmode,const SkPaint & paint)158 void SkPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColor colors[],
159 const SkPoint texCoords[], SkBlendMode bmode,
160 const SkPaint& paint) {
161 AutoPaintFilter apf(this, paint);
162 if (apf.shouldDraw()) {
163 this->SkNWayCanvas::onDrawPatch(cubics, colors, texCoords, bmode, apf.paint());
164 }
165 }
166
onDrawPicture(const SkPicture * picture,const SkMatrix * m,const SkPaint * originalPaint)167 void SkPaintFilterCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* m,
168 const SkPaint* originalPaint) {
169 AutoPaintFilter apf(this, originalPaint);
170 if (apf.shouldDraw()) {
171 const SkPaint* newPaint = &apf.paint();
172
173 // Passing a paint (-vs- passing null) makes drawPicture draw into a layer...
174 // much slower, and can produce different blending. Thus we should only do this
175 // if the filter's effect actually impacts the picture.
176 if (originalPaint == nullptr) {
177 if ( newPaint->getAlphaf() == 1.0f
178 && newPaint->getColorFilter() == nullptr
179 && newPaint->getImageFilter() == nullptr
180 && newPaint->asBlendMode() == SkBlendMode::kSrcOver) {
181 // restore the original nullptr
182 newPaint = nullptr;
183 }
184 }
185 this->SkNWayCanvas::onDrawPicture(picture, m, newPaint);
186 }
187 }
188
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)189 void SkPaintFilterCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
190 // There is no paint to filter in this case, but we can still filter on type.
191 // Subclasses need to unroll the drawable explicity (by overriding this method) in
192 // order to actually filter nested content.
193 AutoPaintFilter apf(this, nullptr);
194 if (apf.shouldDraw()) {
195 this->SkNWayCanvas::onDrawDrawable(drawable, matrix);
196 }
197 }
198
onDrawGlyphRunList(const SkGlyphRunList & list,const SkPaint & paint)199 void SkPaintFilterCanvas::onDrawGlyphRunList(const SkGlyphRunList& list, const SkPaint& paint) {
200 AutoPaintFilter apf(this, paint);
201 if (apf.shouldDraw()) {
202 this->SkNWayCanvas::onDrawGlyphRunList(list, apf.paint());
203 }
204 }
205
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)206 void SkPaintFilterCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
207 const SkPaint& paint) {
208 AutoPaintFilter apf(this, paint);
209 if (apf.shouldDraw()) {
210 this->SkNWayCanvas::onDrawTextBlob(blob, x, y, apf.paint());
211 }
212 }
213
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)214 void SkPaintFilterCanvas::onDrawAtlas2(const SkImage* image, const SkRSXform xform[],
215 const SkRect tex[], const SkColor colors[], int count,
216 SkBlendMode bmode, const SkSamplingOptions& sampling,
217 const SkRect* cull, const SkPaint* paint) {
218 AutoPaintFilter apf(this, paint);
219 if (apf.shouldDraw()) {
220 this->SkNWayCanvas::onDrawAtlas2(image, xform, tex, colors, count, bmode, sampling, cull,
221 &apf.paint());
222 }
223 }
224
onDrawAnnotation(const SkRect & rect,const char key[],SkData * value)225 void SkPaintFilterCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
226 this->SkNWayCanvas::onDrawAnnotation(rect, key, value);
227 }
228
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)229 void SkPaintFilterCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
230 this->SkNWayCanvas::onDrawShadowRec(path, rec);
231 }
232
onDrawEdgeAAQuad(const SkRect & rect,const SkPoint clip[4],QuadAAFlags aa,const SkColor4f & color,SkBlendMode mode)233 void SkPaintFilterCanvas::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
234 QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
235 SkPaint paint;
236 paint.setColor(color);
237 paint.setBlendMode(mode);
238 AutoPaintFilter apf(this, paint);
239 if (apf.shouldDraw()) {
240 this->SkNWayCanvas::onDrawEdgeAAQuad(rect, clip, aa, apf.paint().getColor4f(),
241 apf.paint().getBlendMode_or(SkBlendMode::kSrcOver));
242 }
243 }
244
onDrawEdgeAAImageSet2(const ImageSetEntry set[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)245 void SkPaintFilterCanvas::onDrawEdgeAAImageSet2(const ImageSetEntry set[], int count,
246 const SkPoint dstClips[],
247 const SkMatrix preViewMatrices[],
248 const SkSamplingOptions& sampling,
249 const SkPaint* paint,
250 SrcRectConstraint constraint) {
251 AutoPaintFilter apf(this, paint);
252 if (apf.shouldDraw()) {
253 this->SkNWayCanvas::onDrawEdgeAAImageSet2(
254 set, count, dstClips, preViewMatrices, sampling, &apf.paint(), constraint);
255 }
256 }
257
onNewSurface(const SkImageInfo & info,const SkSurfaceProps & props)258 sk_sp<SkSurface> SkPaintFilterCanvas::onNewSurface(const SkImageInfo& info,
259 const SkSurfaceProps& props) {
260 return this->proxy()->makeSurface(info, &props);
261 }
262
onPeekPixels(SkPixmap * pixmap)263 bool SkPaintFilterCanvas::onPeekPixels(SkPixmap* pixmap) {
264 return this->proxy()->peekPixels(pixmap);
265 }
266
onAccessTopLayerPixels(SkPixmap * pixmap)267 bool SkPaintFilterCanvas::onAccessTopLayerPixels(SkPixmap* pixmap) {
268 SkImageInfo info;
269 size_t rowBytes;
270
271 void* addr = this->proxy()->accessTopLayerPixels(&info, &rowBytes);
272 if (!addr) {
273 return false;
274 }
275
276 pixmap->reset(info, addr, rowBytes);
277 return true;
278 }
279
onImageInfo() const280 SkImageInfo SkPaintFilterCanvas::onImageInfo() const {
281 return this->proxy()->imageInfo();
282 }
283
onGetProps(SkSurfaceProps * props) const284 bool SkPaintFilterCanvas::onGetProps(SkSurfaceProps* props) const {
285 return this->proxy()->getProps(props);
286 }
287