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