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