Debian Codegraph @

src/utils/SkPaintFilterCanvas.cpp

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