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 "gm/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorFilter.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageFilter.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkMaskFilter.h"
17 #include "include/core/SkMatrix.h"
18 #include "include/core/SkPaint.h"
19 #include "include/core/SkRRect.h"
20 #include "include/core/SkRect.h"
21 #include "include/core/SkRefCnt.h"
22 #include "include/core/SkScalar.h"
23 #include "include/core/SkSurface.h"
24 #include "include/core/SkTypes.h"
25 #include "include/effects/SkColorMatrix.h"
26 #include "include/effects/SkGradientShader.h"
27 #include "include/effects/SkHighContrastFilter.h"
28 #include "include/effects/SkImageFilters.h"
29 #include "include/effects/SkShaderMaskFilter.h"
30 #include "include/gpu/ganesh/GrDirectContext.h"
31 #include "include/gpu/ganesh/SkImageGanesh.h"
32 #include "src/core/SkCanvasPriv.h"
33 #include "tools/DecodeUtils.h"
34 #include "tools/Resources.h"
35 #include "tools/ToolUtils.h"
36
37 #include <utility>
38
39 /**
40 * Test drawing a primitive w/ an imagefilter (in this case, just matrix w/ identity) to see
41 * that we apply the xfermode *after* the image has been created and filtered, and not during
42 * the creation step (i.e. before it is filtered).
43 *
44 * see https://bug.skia.org/3741
45 */
do_draw(SkCanvas * canvas,SkBlendMode mode,sk_sp<SkImageFilter> imf)46 static void do_draw(SkCanvas* canvas, SkBlendMode mode, sk_sp<SkImageFilter> imf) {
47 SkAutoCanvasRestore acr(canvas, true);
48 canvas->clipRect(SkRect::MakeWH(220, 220));
49
50 // want to force a layer, so modes like DstIn can combine meaningfully, but the final
51 // image can still be shown against our default (opaque) background. non-opaque GMs
52 // are a lot more trouble to compare/triage.
53 canvas->saveLayer(nullptr, nullptr);
54 canvas->drawColor(SK_ColorGREEN);
55
56 SkPaint paint;
57 paint.setAntiAlias(true);
58
59 SkRect r0 = SkRect::MakeXYWH(10, 60, 200, 100);
60 SkRect r1 = SkRect::MakeXYWH(60, 10, 100, 200);
61
62 paint.setColor(SK_ColorRED);
63 canvas->drawOval(r0, paint);
64
65 paint.setColor(0x660000FF);
66 paint.setImageFilter(std::move(imf));
67 paint.setBlendMode(mode);
68 canvas->drawOval(r1, paint);
69 }
70
71 DEF_SIMPLE_GM(imagefilters_xfermodes, canvas, 480, 480) {
72 canvas->translate(10, 10);
73
74 // just need an imagefilter to trigger the code-path (which creates a tmp layer)
75 sk_sp<SkImageFilter> imf(SkImageFilters::MatrixTransform(SkMatrix::I(),
76 SkSamplingOptions(),
77 nullptr));
78
79 const SkBlendMode modes[] = {
80 SkBlendMode::kSrcATop, SkBlendMode::kDstIn
81 };
82
83 for (size_t i = 0; i < std::size(modes); ++i) {
84 canvas->save();
85 do_draw(canvas, modes[i], nullptr);
86 canvas->translate(240, 0);
87 do_draw(canvas, modes[i], imf);
88 canvas->restore();
89
90 canvas->translate(0, 240);
91 }
92 }
93
make_image(SkCanvas * canvas)94 static sk_sp<SkImage> make_image(SkCanvas* canvas) {
95 const SkImageInfo info = SkImageInfo::MakeS32(100, 100, kPremul_SkAlphaType);
96 auto surface(ToolUtils::makeSurface(canvas, info));
97 surface->getCanvas()->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), SkPaint());
98 return surface->makeImageSnapshot();
99 }
100
101 // Compare blurs when we're tightly clipped (fast) and not as tightly (slower)
102 //
103 // Expect the two to draw the same (modulo the extra border of pixels when the clip is larger)
104 //
105 DEF_SIMPLE_GM(fast_slow_blurimagefilter, canvas, 620, 260) {
106 sk_sp<SkImage> image(make_image(canvas));
107 const SkRect r = SkRect::MakeIWH(image->width(), image->height());
108
109 canvas->translate(10, 10);
110 for (SkScalar sigma = 8; sigma <= 128; sigma *= 2) {
111 SkPaint paint;
112 paint.setImageFilter(SkImageFilters::Blur(sigma, sigma, nullptr));
113
114 canvas->save();
115 // we outset the clip by 1, to fall out of the fast-case in drawImage
116 // i.e. the clip is larger than the image
117 for (SkScalar outset = 0; outset <= 1; ++outset) {
118 canvas->save();
119 canvas->clipRect(r.makeOutset(outset, outset));
120 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
121 canvas->restore();
122 canvas->translate(0, r.height() + 20);
123 }
124 canvas->restore();
125 canvas->translate(r.width() + 20, 0);
126 }
127 }
128
129 ///////////////////////////////////////////////////////////////////////////////////////////////////
130
draw_set(SkCanvas * canvas,sk_sp<SkImageFilter> filters[],int count)131 static void draw_set(SkCanvas* canvas, sk_sp<SkImageFilter> filters[], int count) {
132 const SkRect r = SkRect::MakeXYWH(30, 30, 200, 200);
133 const SkScalar offset = 250;
134 SkScalar dx = 0, dy = 0;
135
136 for (int i = 0; i < count; ++i) {
137 canvas->save();
138 SkRRect rr = SkRRect::MakeRectXY(r.makeOffset(dx, dy), 20, 20);
139 canvas->clipRRect(rr, true);
140 canvas->saveLayer(SkCanvas::SaveLayerRec(&rr.getBounds(), nullptr, filters[i].get(), 0));
141 canvas->drawColor(0x40FFFFFF);
142 canvas->restore();
143 canvas->restore();
144
145 if (0 == dx) {
146 dx = offset;
147 } else {
148 dx = 0;
149 dy = offset;
150 }
151 }
152 }
153
154 class SaveLayerWithBackdropGM : public skiagm::GM {
155 protected:
runAsBench() const156 bool runAsBench() const override { return true; }
getName() const157 SkString getName() const override { return SkString("savelayer_with_backdrop"); }
getISize()158 SkISize getISize() override { return SkISize::Make(830, 550); }
159
onDraw(SkCanvas * canvas)160 void onDraw(SkCanvas* canvas) override {
161 SkColorMatrix cm;
162 cm.setSaturation(10);
163 sk_sp<SkColorFilter> cf(SkColorFilters::Matrix(cm));
164 const SkScalar kernel[] = { 4, 0, 4, 0, -15, 0, 4, 0, 4 };
165 sk_sp<SkImageFilter> filters[] = {
166 SkImageFilters::Blur(10, 10, nullptr),
167 SkImageFilters::Dilate(8, 8, nullptr),
168 SkImageFilters::MatrixConvolution({ 3, 3 }, kernel, 1, 0, { 0, 0 },
169 SkTileMode::kDecal, true, nullptr),
170 SkImageFilters::ColorFilter(std::move(cf), nullptr),
171 };
172
173 const struct {
174 SkScalar fSx, fSy, fTx, fTy;
175 } xforms[] = {
176 { 1, 1, 0, 0 },
177 { 0.5f, 0.5f, 530, 0 },
178 { 0.25f, 0.25f, 530, 275 },
179 { 0.125f, 0.125f, 530, 420 },
180 };
181
182 SkSamplingOptions sampling(SkFilterMode::kLinear,
183 SkMipmapMode::kLinear);
184 sk_sp<SkImage> image(ToolUtils::GetResourceAsImage("images/mandrill_512.png"));
185
186 canvas->translate(20, 20);
187 for (const auto& xform : xforms) {
188 canvas->save();
189 canvas->translate(xform.fTx, xform.fTy);
190 canvas->scale(xform.fSx, xform.fSy);
191 canvas->drawImage(image, 0, 0, sampling, nullptr);
192 draw_set(canvas, filters, std::size(filters));
193 canvas->restore();
194 }
195 }
196 };
197
198 DEF_GM(return new SaveLayerWithBackdropGM();)
199
200 ///////////////////////////////////////////////////////////////////////////////////////////////////
201
202 // Test that color filters and mask filters are applied before the image filter, even if it would
203 // normally be a sprite draw that could avoid an auto-saveLayer.
204 DEF_SIMPLE_GM(imagefilters_effect_order, canvas, 512, 512) {
205 sk_sp<SkImage> image(ToolUtils::GetResourceAsImage("images/mandrill_256.png"));
206 auto direct = GrAsDirectContext(canvas->recordingContext());
207 if (direct) {
208 if (sk_sp<SkImage> gpuImage = SkImages::TextureFromImage(direct, image)) {
209 image = std::move(gpuImage);
210 }
211 }
212
213 SkISize kernelSize = SkISize::Make(3, 3);
214 SkIPoint kernelOffset = SkIPoint::Make(1, 1);
215 // A Laplacian edge detector, ie https://en.wikipedia.org/wiki/Kernel_(image_processing)
216 SkScalar kernel[9] = {-1.f, -1.f, -1.f,
217 -1.f, 8.f, -1.f,
218 -1.f, -1.f, -1.f};
219 auto edgeDetector = SkImageFilters::MatrixConvolution(
220 kernelSize, kernel, 1.f, 0.f, kernelOffset, SkTileMode::kClamp, false, nullptr);
221 // This uses the high contrast filter because it resembles a pre-processing step you may perform
222 // prior to edge detection. The specifics of the high contrast algorithm don't matter for the GM
223 auto edgeAmplify = SkHighContrastFilter::Make(
224 {false, SkHighContrastConfig::InvertStyle::kNoInvert, 0.5f});
225
226 SkPaint testCFPaint;
227 testCFPaint.setColorFilter(edgeAmplify);
228 testCFPaint.setImageFilter(edgeDetector);
229
230 // The expected result is color filter then image filter, so represent this explicitly in the
231 // image filter graph.
232 SkPaint expectedCFPaint;
233 expectedCFPaint.setImageFilter(SkImageFilters::Compose(edgeDetector,
234 SkImageFilters::ColorFilter(edgeAmplify, nullptr)));
235
236 // Draw the image twice (expected on the left, test on the right that should match)
237 SkRect crop = SkRect::Make(image->bounds());
238 canvas->save();
239 canvas->clipRect(crop);
240 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &expectedCFPaint); // Filter applied by draw's SkPaint
241 canvas->restore();
242
243 canvas->save();
244 canvas->translate(image->width(), 0);
245 canvas->clipRect(crop);
246 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &testCFPaint);
247 canvas->restore();
248
249 // Now test mask filters. These should be run before the image filter, and thus have the same
250 // effect as multiplying by an alpha mask.
251
252 // This mask filter pokes a hole in the center of the image
253 static constexpr SkColor kAlphas[] = { SK_ColorBLACK, SK_ColorTRANSPARENT };
254 static constexpr SkScalar kPos[] = { 0.4f, 0.9f };
255 sk_sp<SkShader> alphaMaskShader = SkGradientShader::MakeRadial(
256 {128.f, 128.f}, 128.f, kAlphas, kPos, 2, SkTileMode::kClamp);
257 sk_sp<SkMaskFilter> maskFilter = SkShaderMaskFilter::Make(alphaMaskShader);
258
259 // If edge detector sees the mask filter, it'll have alpha and then blend with the original
260 // image; otherwise the mask filter will apply late (incorrectly) and none of the original
261 // image will be visible.
262 sk_sp<SkImageFilter> edgeBlend = SkImageFilters::Blend(SkBlendMode::kSrcOver,
263 SkImageFilters::Image(image, SkFilterMode::kNearest), edgeDetector);
264
265 SkPaint testMaskPaint;
266 testMaskPaint.setMaskFilter(maskFilter);
267 testMaskPaint.setImageFilter(edgeBlend);
268
269 SkPaint expectedMaskPaint;
270 expectedMaskPaint.setImageFilter(SkImageFilters::Compose(edgeBlend,
271 SkImageFilters::Blend(SkBlendMode::kSrcIn,
272 SkImageFilters::Shader(alphaMaskShader))));
273
274 canvas->save();
275 canvas->translate(0, image->height());
276 canvas->clipRect(crop);
277 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &expectedMaskPaint);
278 canvas->restore();
279
280 canvas->save();
281 canvas->translate(image->width(), image->height());
282 canvas->clipRect(crop);
283 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &testMaskPaint);
284 canvas->restore();
285 }
286
287 DEF_SIMPLE_GM(multiple_filters, canvas, 415, 210) {
288 ToolUtils::draw_checkerboard(canvas);
289 canvas->translate(5, 5);
290
__anon38ec2bce0202(SkCanvas::FilterSpan filters) 291 auto drawFilteredLayer = [=](SkCanvas::FilterSpan filters) {
292 SkPaint restorePaint;
293 restorePaint.setAlphaf(0.5f);
294 SkCanvas::SaveLayerRec rec = SkCanvasPriv::ScaledBackdropLayer(
295 /*bounds=*/nullptr,
296 &restorePaint,
297 /*backdrop=*/nullptr,
298 /*backdropScale=*/1,
299 /*saveLayerFlags=*/0,
300 filters);
301 canvas->save();
302 canvas->clipRect({0, 0, 200, 200});
303 canvas->saveLayer(rec);
304
305 SkPaint paint;
306 paint.setStyle(SkPaint::kStroke_Style);
307 paint.setStrokeWidth(20);
308 paint.setColor(SK_ColorGREEN);
309 canvas->drawCircle(100, 100, 70, paint);
310
311 canvas->restore();
312 canvas->restore();
313 canvas->translate(205, 0);
314 };
315
316 {
317 // Test with two non-null filters that each change bounds in a different way:
318 sk_sp<SkImageFilter> filters[2] = {SkImageFilters::Dilate(5, 5, nullptr),
319 SkImageFilters::Erode(5, 5, nullptr)};
320 drawFilteredLayer(filters);
321 }
322
323 {
324 // Test with one null filter, to more closely mimic the canvas2D layers use-case:
325 sk_sp<SkImageFilter> filters[2] = {
326 SkImageFilters::DropShadowOnly(7, 7, 5, 5, SK_ColorBLUE, nullptr), nullptr};
327 drawFilteredLayer(filters);
328 }
329 }
330