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.h"
9 #include "SkBlurImageFilter.h"
10 #include "SkColorMatrixFilter.h"
11 #include "SkImage.h"
12 #include "SkImageFilter.h"
13 #include "SkSurface.h"
14
15 /**
16 * Test drawing a primitive w/ an imagefilter (in this case, just matrix w/ identity) to see
17 * that we apply the xfermode *after* the image has been created and filtered, and not during
18 * the creation step (i.e. before it is filtered).
19 *
20 * see https://bug.skia.org/3741
21 */
do_draw(SkCanvas * canvas,SkBlendMode mode,sk_sp<SkImageFilter> imf)22 static void do_draw(SkCanvas* canvas, SkBlendMode mode, sk_sp<SkImageFilter> imf) {
23 SkAutoCanvasRestore acr(canvas, true);
24 canvas->clipRect(SkRect::MakeWH(220, 220));
25
26 // want to force a layer, so modes like DstIn can combine meaningfully, but the final
27 // image can still be shown against our default (opaque) background. non-opaque GMs
28 // are a lot more trouble to compare/triage.
29 canvas->saveLayer(nullptr, nullptr);
30 canvas->drawColor(SK_ColorGREEN);
31
32 SkPaint paint;
33 paint.setAntiAlias(true);
34
35 SkRect r0 = SkRect::MakeXYWH(10, 60, 200, 100);
36 SkRect r1 = SkRect::MakeXYWH(60, 10, 100, 200);
37
38 paint.setColor(SK_ColorRED);
39 canvas->drawOval(r0, paint);
40
41 paint.setColor(0x660000FF);
42 paint.setImageFilter(std::move(imf));
43 paint.setBlendMode(mode);
44 canvas->drawOval(r1, paint);
45 }
46
47 DEF_SIMPLE_GM(imagefilters_xfermodes, canvas, 480, 480) {
48 canvas->translate(10, 10);
49
50 // just need an imagefilter to trigger the code-path (which creates a tmp layer)
51 sk_sp<SkImageFilter> imf(SkImageFilter::MakeMatrixFilter(SkMatrix::I(),
52 kNone_SkFilterQuality,
53 nullptr));
54
55 const SkBlendMode modes[] = {
56 SkBlendMode::kSrcATop, SkBlendMode::kDstIn
57 };
58
59 for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
60 canvas->save();
61 do_draw(canvas, modes[i], nullptr);
62 canvas->translate(240, 0);
63 do_draw(canvas, modes[i], imf);
64 canvas->restore();
65
66 canvas->translate(0, 240);
67 }
68 }
69
make_image(SkCanvas * canvas)70 static sk_sp<SkImage> make_image(SkCanvas* canvas) {
71 const SkImageInfo info = SkImageInfo::MakeS32(100, 100, kPremul_SkAlphaType);
72 auto surface(canvas->makeSurface(info));
73 if (!surface) {
74 surface = SkSurface::MakeRaster(info);
75 }
76 surface->getCanvas()->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), SkPaint());
77 return surface->makeImageSnapshot();
78 }
79
80 // Compare blurs when we're tightly clipped (fast) and not as tightly (slower)
81 //
82 // Expect the two to draw the same (modulo the extra border of pixels when the clip is larger)
83 //
84 DEF_SIMPLE_GM(fast_slow_blurimagefilter, canvas, 620, 260) {
85 sk_sp<SkImage> image(make_image(canvas));
86 const SkRect r = SkRect::MakeIWH(image->width(), image->height());
87
88 canvas->translate(10, 10);
89 for (SkScalar sigma = 8; sigma <= 128; sigma *= 2) {
90 SkPaint paint;
91 paint.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr));
92
93 canvas->save();
94 // we outset the clip by 1, to fall out of the fast-case in drawImage
95 // i.e. the clip is larger than the image
96 for (SkScalar outset = 0; outset <= 1; ++outset) {
97 canvas->save();
98 canvas->clipRect(r.makeOutset(outset, outset));
99 canvas->drawImage(image, 0, 0, &paint);
100 canvas->restore();
101 canvas->translate(0, r.height() + 20);
102 }
103 canvas->restore();
104 canvas->translate(r.width() + 20, 0);
105 }
106 }
107
108 ///////////////////////////////////////////////////////////////////////////////////////////////////
109 #include "Resources.h"
110 #include "SkBlurImageFilter.h"
111 #include "SkMatrixConvolutionImageFilter.h"
112 #include "SkMorphologyImageFilter.h"
113 #include "SkColorMatrixFilter.h"
114 #include "SkColorFilterImageFilter.h"
115 #include "SkRRect.h"
116
draw_set(SkCanvas * canvas,sk_sp<SkImageFilter> filters[],int count)117 static void draw_set(SkCanvas* canvas, sk_sp<SkImageFilter> filters[], int count) {
118 const SkRect r = SkRect::MakeXYWH(30, 30, 200, 200);
119 const SkScalar offset = 250;
120 SkScalar dx = 0, dy = 0;
121
122 for (int i = 0; i < count; ++i) {
123 canvas->save();
124 SkRRect rr = SkRRect::MakeRectXY(r.makeOffset(dx, dy), 20, 20);
125 canvas->clipRRect(rr, true);
126 canvas->saveLayer({ &rr.getBounds(), nullptr, filters[i].get(), nullptr, nullptr, 0 });
127 canvas->drawColor(0x40FFFFFF);
128 canvas->restore();
129 canvas->restore();
130
131 if (0 == dx) {
132 dx = offset;
133 } else {
134 dx = 0;
135 dy = offset;
136 }
137 }
138 }
139
140 DEF_SIMPLE_GM(savelayer_with_backdrop, canvas, 830, 550) {
141 SkColorMatrix cm;
142 cm.setSaturation(10);
143 sk_sp<SkColorFilter> cf(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
144 const SkScalar kernel[] = { 4, 0, 4, 0, -15, 0, 4, 0, 4 };
145 sk_sp<SkImageFilter> filters[] = {
146 SkBlurImageFilter::Make(10, 10, nullptr),
147 SkDilateImageFilter::Make(8, 8, nullptr),
148 SkMatrixConvolutionImageFilter::Make(
149 { 3, 3 }, kernel, 1, 0, { 0, 0 },
150 SkMatrixConvolutionImageFilter::kClampToBlack_TileMode,
151 true, nullptr),
152 SkColorFilterImageFilter::Make(std::move(cf), nullptr),
153 };
154
155 const struct {
156 SkScalar fSx, fSy, fTx, fTy;
157 } xforms[] = {
158 { 1, 1, 0, 0 },
159 { 0.5f, 0.5f, 530, 0 },
160 { 0.25f, 0.25f, 530, 275 },
161 { 0.125f, 0.125f, 530, 420 },
162 };
163
164 SkPaint paint;
165 paint.setFilterQuality(kMedium_SkFilterQuality);
166 sk_sp<SkImage> image(GetResourceAsImage("mandrill_512.png"));
167
168 canvas->translate(20, 20);
169 for (const auto& xform : xforms) {
170 canvas->save();
171 canvas->translate(xform.fTx, xform.fTy);
172 canvas->scale(xform.fSx, xform.fSy);
173 canvas->drawImage(image, 0, 0, &paint);
174 draw_set(canvas, filters, SK_ARRAY_COUNT(filters));
175 canvas->restore();
176 }
177 }
178