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