2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/utils/SkPaintFilterCanvas.h"
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPixmap.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkSurface.h" // IWYU pragma: keep
30 struct SkDrawShadowRec;
32 class SkPaintFilterCanvas::AutoPaintFilter {
34 AutoPaintFilter(const SkPaintFilterCanvas* canvas, const SkPaint* paint)
35 : fPaint(paint ? *paint : SkPaint()) {
36 fShouldDraw = canvas->onFilter(fPaint);
39 AutoPaintFilter(const SkPaintFilterCanvas* canvas, const SkPaint& paint)
40 : AutoPaintFilter(canvas, &paint) { }
42 const SkPaint& paint() const { return fPaint; }
44 bool shouldDraw() const { return fShouldDraw; }
51 SkPaintFilterCanvas::SkPaintFilterCanvas(SkCanvas *canvas)
52 : SkCanvasVirtualEnforcer<SkNWayCanvas>(canvas->imageInfo().width(),
53 canvas->imageInfo().height()) {
55 // Transfer matrix & clip state before adding the target canvas.
56 this->clipRect(SkRect::Make(canvas->getDeviceClipBounds()));
57 this->setMatrix(canvas->getLocalToDevice());
59 this->addCanvas(canvas);
62 void SkPaintFilterCanvas::onDrawPaint(const SkPaint& paint) {
63 AutoPaintFilter apf(this, paint);
64 if (apf.shouldDraw()) {
65 this->SkNWayCanvas::onDrawPaint(apf.paint());
69 void SkPaintFilterCanvas::onDrawBehind(const SkPaint& paint) {
70 AutoPaintFilter apf(this, paint);
71 if (apf.shouldDraw()) {
72 this->SkNWayCanvas::onDrawBehind(apf.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());
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());
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());
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());
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());
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());
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());
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());
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());
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);
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());
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());
169 void SkPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColor colors[],
170 const SkPoint texCoords[], 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());
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();
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
196 this->SkNWayCanvas::onDrawPicture(picture, m, newPaint);
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);
210 void SkPaintFilterCanvas::onDrawGlyphRunList(const SkGlyphRunList& list, const SkPaint& paint) {
211 AutoPaintFilter apf(this, paint);
212 if (apf.shouldDraw()) {
213 this->SkNWayCanvas::onDrawGlyphRunList(list, apf.paint());
217 void SkPaintFilterCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
218 const SkPaint& paint) {
219 AutoPaintFilter apf(this, paint);
220 if (apf.shouldDraw()) {
221 this->SkNWayCanvas::onDrawTextBlob(blob, x, y, apf.paint());
225 void SkPaintFilterCanvas::onDrawAtlas2(const SkImage* image, const SkRSXform xform[],
226 const SkRect tex[], const SkColor colors[], int count,
227 SkBlendMode bmode, const SkSamplingOptions& sampling,
228 const SkRect* cull, const SkPaint* paint) {
229 AutoPaintFilter apf(this, paint);
230 if (apf.shouldDraw()) {
231 this->SkNWayCanvas::onDrawAtlas2(image, xform, tex, colors, count, bmode, sampling, cull,
236 void SkPaintFilterCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
237 this->SkNWayCanvas::onDrawAnnotation(rect, key, value);
240 void SkPaintFilterCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
241 this->SkNWayCanvas::onDrawShadowRec(path, rec);
244 void SkPaintFilterCanvas::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
245 QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
247 paint.setColor(color);
248 paint.setBlendMode(mode);
249 AutoPaintFilter apf(this, paint);
250 if (apf.shouldDraw()) {
251 this->SkNWayCanvas::onDrawEdgeAAQuad(rect, clip, aa, apf.paint().getColor4f(),
252 apf.paint().getBlendMode_or(SkBlendMode::kSrcOver));
256 void SkPaintFilterCanvas::onDrawEdgeAAImageSet2(const ImageSetEntry set[], int count,
257 const SkPoint dstClips[],
258 const SkMatrix preViewMatrices[],
259 const SkSamplingOptions& sampling,
260 const SkPaint* paint,
261 SrcRectConstraint constraint) {
262 AutoPaintFilter apf(this, paint);
263 if (apf.shouldDraw()) {
264 this->SkNWayCanvas::onDrawEdgeAAImageSet2(
265 set, count, dstClips, preViewMatrices, sampling, &apf.paint(), constraint);
269 sk_sp<SkSurface> SkPaintFilterCanvas::onNewSurface(const SkImageInfo& info,
270 const SkSurfaceProps& props) {
271 return this->proxy()->makeSurface(info, &props);
274 bool SkPaintFilterCanvas::onPeekPixels(SkPixmap* pixmap) {
275 return this->proxy()->peekPixels(pixmap);
278 bool SkPaintFilterCanvas::onAccessTopLayerPixels(SkPixmap* pixmap) {
282 void* addr = this->proxy()->accessTopLayerPixels(&info, &rowBytes);
287 pixmap->reset(info, addr, rowBytes);
291 SkImageInfo SkPaintFilterCanvas::onImageInfo() const {
292 return this->proxy()->imageInfo();
295 bool SkPaintFilterCanvas::onGetProps(SkSurfaceProps* props) const {
296 return this->proxy()->getProps(props);