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