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