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