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/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageFilter.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkShader.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkSurface.h"
24 #include "include/core/SkTileMode.h"
25 #include "include/core/SkTypes.h"
26 #include "include/effects/SkGradientShader.h"
27 #include "include/effects/SkImageFilters.h"
28 #include "tools/Resources.h"
29 #include "tools/ToolUtils.h"
30 #include "tools/timer/TimeUtils.h"
31
32 #include <utility>
33
34 namespace skiagm {
35
36 // This GM draws image filters with a CTM containing shearing / rotation.
37 // It checks that the scale portion of the CTM is correctly extracted
38 // and applied to the image inputs separately from the non-scale portion.
39
make_gradient_circle(int width,int height)40 static sk_sp<SkImage> make_gradient_circle(int width, int height) {
41 SkScalar x = SkIntToScalar(width / 2);
42 SkScalar y = SkIntToScalar(height / 2);
43 SkScalar radius = std::min(x, y) * 0.8f;
44
45 auto surface(SkSurface::MakeRasterN32Premul(width, height));
46 SkCanvas* canvas = surface->getCanvas();
47
48 canvas->clear(0x00000000);
49 SkColor colors[2];
50 colors[0] = SK_ColorWHITE;
51 colors[1] = SK_ColorBLACK;
52 SkPaint paint;
53 paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(x, y), radius, colors, nullptr, 2,
54 SkTileMode::kClamp));
55 canvas->drawCircle(x, y, radius, paint);
56
57 return surface->makeImageSnapshot();
58 }
59
60 class ImageFiltersTransformedGM : public GM {
61 public:
ImageFiltersTransformedGM()62 ImageFiltersTransformedGM() {
63 this->setBGColor(SK_ColorBLACK);
64 }
65
66 protected:
67
onShortName()68 SkString onShortName() override { return SkString("imagefilterstransformed"); }
69
onISize()70 SkISize onISize() override { return SkISize::Make(420, 240); }
71
onOnceBeforeDraw()72 void onOnceBeforeDraw() override {
73 fCheckerboard =
74 ToolUtils::create_checkerboard_image(64, 64, 0xFFA0A0A0, 0xFF404040, 8);
75 fGradientCircle = make_gradient_circle(64, 64);
76 }
77
onDraw(SkCanvas * canvas)78 void onDraw(SkCanvas* canvas) override {
79 sk_sp<SkImageFilter> gradient(SkImageFilters::Image(fGradientCircle));
80 sk_sp<SkImageFilter> checkerboard(SkImageFilters::Image(fCheckerboard));
81 sk_sp<SkImageFilter> filters[] = {
82 SkImageFilters::Blur(12, 0, nullptr),
83 SkImageFilters::DropShadow(0, 15, 8, 0, SK_ColorGREEN, nullptr),
84 SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kR, 12,
85 std::move(gradient), checkerboard),
86 SkImageFilters::Dilate(2, 2, checkerboard),
87 SkImageFilters::Erode(2, 2, checkerboard),
88 };
89
90 const SkScalar margin = SkIntToScalar(20);
91 const SkScalar size = SkIntToScalar(60);
92
93 for (size_t j = 0; j < 3; j++) {
94 canvas->save();
95 canvas->translate(margin, 0);
96 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
97 SkPaint paint;
98 paint.setColor(SK_ColorWHITE);
99 paint.setImageFilter(filters[i]);
100 paint.setAntiAlias(true);
101 canvas->save();
102 canvas->translate(size * SK_ScalarHalf, size * SK_ScalarHalf);
103 canvas->scale(SkDoubleToScalar(0.8), SkDoubleToScalar(0.8));
104 if (j == 1) {
105 canvas->rotate(SkIntToScalar(45));
106 } else if (j == 2) {
107 canvas->skew(SkDoubleToScalar(0.5), SkDoubleToScalar(0.2));
108 }
109 canvas->translate(-size * SK_ScalarHalf, -size * SK_ScalarHalf);
110 canvas->drawOval(SkRect::MakeXYWH(0, size * SkDoubleToScalar(0.1),
111 size, size * SkDoubleToScalar(0.6)), paint);
112 canvas->restore();
113 canvas->translate(size + margin, 0);
114 }
115 canvas->restore();
116 canvas->translate(0, size + margin);
117 }
118 }
119
120 private:
121 sk_sp<SkImage> fCheckerboard;
122 sk_sp<SkImage> fGradientCircle;
123 using INHERITED = GM;
124 };
125 DEF_GM( return new ImageFiltersTransformedGM; )
126 } // namespace skiagm
127
128 //////////////////////////////////////////////////////////////////////////////
129
130 DEF_SIMPLE_GM(rotate_imagefilter, canvas, 500, 500) {
131 SkPaint paint;
132
133 const SkRect r = SkRect::MakeXYWH(50, 50, 100, 100);
134
135 sk_sp<SkImageFilter> filters[] = {
136 nullptr,
137 SkImageFilters::Blur(6, 0, nullptr),
138 SkImageFilters::Blend(SkBlendMode::kSrcOver, nullptr),
139 };
140
141 for (auto& filter : filters) {
142 paint.setAntiAlias(false);
143 paint.setImageFilter(filter);
144
145 canvas->save();
146
147 canvas->drawRect(r, paint);
148
149 canvas->translate(150, 0);
150 canvas->save();
151 canvas->rotate(30, 100, 100);
152 canvas->drawRect(r, paint);
153 canvas->restore();
154
155 paint.setAntiAlias(true);
156 canvas->translate(150, 0);
157 canvas->save();
158 canvas->rotate(30, 100, 100);
159 canvas->drawRect(r, paint);
160 canvas->restore();
161
162 canvas->restore();
163 canvas->translate(0, 150);
164 }
165 }
166
167 class ImageFilterMatrixWLocalMatrix : public skiagm::GM {
168 public:
169
170 // Start at 132 degrees, since that resulted in a skipped draw before the fix to
171 // SkLocalMatrixImageFilter's computeFastBounds() function.
ImageFilterMatrixWLocalMatrix()172 ImageFilterMatrixWLocalMatrix() : fDegrees(132.f) {}
173
174 protected:
onShortName()175 SkString onShortName() override {
176 return SkString("imagefilter_matrix_localmatrix");
177 }
178
onISize()179 SkISize onISize() override {
180 return SkISize::Make(512, 512);
181 }
182
onAnimate(double nanos)183 bool onAnimate(double nanos) override {
184 // Animate the rotation angle to ensure the local matrix bounds modifications work
185 // for a variety of transformations.
186 fDegrees = TimeUtils::Scaled(1e-9f * nanos, 360.f);
187 return true;
188 }
189
onOnceBeforeDraw()190 void onOnceBeforeDraw() override {
191 fImage = GetResourceAsImage("images/mandrill_256.png");
192 }
193
onDraw(SkCanvas * canvas)194 void onDraw(SkCanvas* canvas) override {
195 SkMatrix localMatrix;
196 localMatrix.preTranslate(128, 128);
197 localMatrix.preScale(2.0f, 2.0f);
198
199 // This matrix applies a rotate around the center of the image (prior to the simulated
200 // hi-dpi 2x device scale).
201 SkMatrix filterMatrix;
202 filterMatrix.setRotate(fDegrees, 64, 64);
203
204 sk_sp<SkImageFilter> filter =
205 SkImageFilters::MatrixTransform(filterMatrix,
206 SkSamplingOptions(SkFilterMode::kLinear), nullptr)
207 ->makeWithLocalMatrix(localMatrix);
208
209 SkPaint p;
210 p.setImageFilter(filter);
211 canvas->drawImage(fImage.get(), 128, 128, SkSamplingOptions(), &p);
212 }
213
214 private:
215 SkScalar fDegrees;
216 sk_sp<SkImage> fImage;
217 };
218
219 DEF_GM(return new ImageFilterMatrixWLocalMatrix();)
220
221 class ImageFilterComposedTransform : public skiagm::GM {
222 public:
223
224 // Start at 70 degrees since that highlighted the issue in skbug.com/10888
ImageFilterComposedTransform()225 ImageFilterComposedTransform() : fDegrees(70.f) {}
226
227 protected:
onShortName()228 SkString onShortName() override {
229 return SkString("imagefilter_composed_transform");
230 }
231
onISize()232 SkISize onISize() override {
233 return SkISize::Make(512, 512);
234 }
235
onAnimate(double nanos)236 bool onAnimate(double nanos) override {
237 // Animate the rotation angle to test a variety of transformations
238 fDegrees = TimeUtils::Scaled(1e-9f * nanos, 360.f);
239 return true;
240 }
241
onOnceBeforeDraw()242 void onOnceBeforeDraw() override {
243 fImage = GetResourceAsImage("images/mandrill_256.png");
244 }
245
onDraw(SkCanvas * canvas)246 void onDraw(SkCanvas* canvas) override {
247 SkMatrix matrix = SkMatrix::RotateDeg(fDegrees);
248 // All four quadrants should render the same
249 this->drawFilter(canvas, 0.f, 0.f, this->makeDirectFilter(matrix));
250 this->drawFilter(canvas, 256.f, 0.f, this->makeEarlyComposeFilter(matrix));
251 this->drawFilter(canvas, 0.f, 256.f, this->makeLateComposeFilter(matrix));
252 this->drawFilter(canvas, 256.f, 256.f, this->makeFullComposeFilter(matrix));
253 }
254
255 private:
256 SkScalar fDegrees;
257 sk_sp<SkImage> fImage;
258
drawFilter(SkCanvas * canvas,SkScalar tx,SkScalar ty,sk_sp<SkImageFilter> filter) const259 void drawFilter(SkCanvas* canvas, SkScalar tx, SkScalar ty, sk_sp<SkImageFilter> filter) const {
260 SkPaint p;
261 p.setImageFilter(std::move(filter));
262
263 canvas->save();
264 canvas->translate(tx, ty);
265 canvas->clipRect(SkRect::MakeWH(256, 256));
266 canvas->scale(0.5f, 0.5f);
267 canvas->translate(128, 128);
268 canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &p);
269 canvas->restore();
270 }
271
272 // offset(matrix(offset))
makeDirectFilter(const SkMatrix & matrix) const273 sk_sp<SkImageFilter> makeDirectFilter(const SkMatrix& matrix) const {
274 SkPoint v = {fImage->width() / 2.f, fImage->height() / 2.f};
275 sk_sp<SkImageFilter> filter = SkImageFilters::Offset(-v.fX, -v.fY, nullptr);
276 filter = SkImageFilters::MatrixTransform(matrix, SkSamplingOptions(SkFilterMode::kLinear),
277 std::move(filter));
278 filter = SkImageFilters::Offset(v.fX, v.fY, std::move(filter));
279 return filter;
280 }
281
282 // offset(compose(matrix, offset))
makeEarlyComposeFilter(const SkMatrix & matrix) const283 sk_sp<SkImageFilter> makeEarlyComposeFilter(const SkMatrix& matrix) const {
284 SkPoint v = {fImage->width() / 2.f, fImage->height() / 2.f};
285 sk_sp<SkImageFilter> offset = SkImageFilters::Offset(-v.fX, -v.fY, nullptr);
286 sk_sp<SkImageFilter> filter = SkImageFilters::MatrixTransform(
287 matrix, SkSamplingOptions(SkFilterMode::kLinear), nullptr);
288 filter = SkImageFilters::Compose(std::move(filter), std::move(offset));
289 filter = SkImageFilters::Offset(v.fX, v.fY, std::move(filter));
290 return filter;
291 }
292
293 // compose(offset, matrix(offset))
makeLateComposeFilter(const SkMatrix & matrix) const294 sk_sp<SkImageFilter> makeLateComposeFilter(const SkMatrix& matrix) const {
295 SkPoint v = {fImage->width() / 2.f, fImage->height() / 2.f};
296 sk_sp<SkImageFilter> filter = SkImageFilters::Offset(-v.fX, -v.fY, nullptr);
297 filter = SkImageFilters::MatrixTransform(matrix, SkSamplingOptions(SkFilterMode::kLinear),
298 std::move(filter));
299 sk_sp<SkImageFilter> offset = SkImageFilters::Offset(v.fX, v.fY, nullptr);
300 filter = SkImageFilters::Compose(std::move(offset), std::move(filter));
301 return filter;
302 }
303
304 // compose(offset, compose(matrix, offset))
makeFullComposeFilter(const SkMatrix & matrix) const305 sk_sp<SkImageFilter> makeFullComposeFilter(const SkMatrix& matrix) const {
306 SkPoint v = {fImage->width() / 2.f, fImage->height() / 2.f};
307 sk_sp<SkImageFilter> offset = SkImageFilters::Offset(-v.fX, -v.fY, nullptr);
308 sk_sp<SkImageFilter> filter = SkImageFilters::MatrixTransform(
309 matrix, SkSamplingOptions(SkFilterMode::kLinear), nullptr);
310 filter = SkImageFilters::Compose(std::move(filter), std::move(offset));
311 offset = SkImageFilters::Offset(v.fX, v.fY, nullptr);
312 filter = SkImageFilters::Compose(std::move(offset), std::move(filter));
313 return filter;
314 }
315 };
316
317 DEF_GM(return new ImageFilterComposedTransform();)
318
319 // Tests SkImageFilters::Image under tricky matrices (mirrors and perspective)
320 DEF_SIMPLE_GM(imagefilter_transformed_image, canvas, 256, 256) {
321 sk_sp<SkImage> image = GetResourceAsImage("images/color_wheel.png");
322 sk_sp<SkImageFilter> imageFilter = SkImageFilters::Image(image);
323
324 const SkRect imageRect = SkRect::MakeIWH(image->width(), image->height());
325
326 SkM44 m1 = SkM44::Translate(0.9f * image->width(), 0.1f * image->height()) *
327 SkM44::Scale(-.8f, .8f);
328
329 SkM44 m2 = SkM44::RectToRect({-1.f, -1.f, 1.f, 1.f}, imageRect) *
330 SkM44::Perspective(0.01f, 100.f, SK_ScalarPI / 3.f) *
331 SkM44::Translate(0.f, 0.f, -2.f) *
332 SkM44::Rotate({0.f, 1.f, 0.f}, SK_ScalarPI / 6.f) *
333 SkM44::RectToRect(imageRect, {-1.f, -1.f, 1.f, 1.f});
334
335 SkFont font(ToolUtils::create_portable_typeface());
336 canvas->drawString("Columns should match", 5.f, 15.f, font, SkPaint());
337 canvas->translate(0.f, 10.f);
338
339 SkSamplingOptions sampling(SkFilterMode::kLinear);
340 for (auto m : {m1, m2}) {
341 canvas->save();
342 for (bool canvasTransform : {false, true}) {
343 canvas->save();
344 canvas->clipRect(imageRect);
345
346 sk_sp<SkImageFilter> finalFilter;
347 if (canvasTransform) {
348 canvas->concat(m);
349 finalFilter = imageFilter;
350 } else {
351 finalFilter = SkImageFilters::MatrixTransform(m.asM33(), sampling, imageFilter);
352 }
353
354 SkPaint paint;
355 paint.setImageFilter(std::move(finalFilter));
356 canvas->drawPaint(paint);
357
358 canvas->restore();
359 canvas->translate(image->width(), 0.f);
360 }
361 canvas->restore();
362
363 canvas->translate(0.f, image->height());
364 }
365 }
366