blob: ec93b830a090470075dda07225b8823cdfeeac1e [file] [log] [blame]
wutao039a7c72017-06-30 10:44:45 -07001/*
2 * Copyright 2017 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Reedac9f0c92020-12-23 10:11:33 -05009#include "include/core/SkBitmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkImage.h"
13#include "include/core/SkImageFilter.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkScalar.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/core/SkSurface.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040022#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/ToolUtils.h"
wutao039a7c72017-06-30 10:44:45 -070024
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include <initializer_list>
26#include <utility>
27
wutao039a7c72017-06-30 10:44:45 -070028static sk_sp<SkImage> make_image(SkCanvas* canvas, int direction) {
29 SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200);
Mike Kleinea3f0142019-03-20 11:12:10 -050030 auto surface = ToolUtils::makeSurface(canvas, info);
wutao039a7c72017-06-30 10:44:45 -070031 SkCanvas* c = surface->getCanvas();
32 SkPaint paint;
33 paint.setAntiAlias(true);
34
35 const SkColor colors[] = {
36 SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorYELLOW, SK_ColorBLACK
37 };
38
39 int width = 25;
40 bool xDirection = (direction & 0x1) == 1;
41 bool yDirection = (direction & 0x2) == 2;
42 if (xDirection) {
43 for (int x = 0; x < info.width(); x += width) {
44 paint.setColor(colors[x/width % 5]);
45 if (yDirection) {
Mike Reed9407e242019-02-15 16:13:57 -050046 paint.setAlphaf(0.5f);
wutao039a7c72017-06-30 10:44:45 -070047 }
48 c->drawRect(SkRect::MakeXYWH(x, 0, width, info.height()), paint);
49 }
50 }
51
52 if (yDirection) {
53 for (int y = 0; y < info.height(); y += width) {
54 paint.setColor(colors[y/width % 5]);
55 if (xDirection) {
Mike Reed9407e242019-02-15 16:13:57 -050056 paint.setAlphaf(0.5f);
wutao039a7c72017-06-30 10:44:45 -070057 }
58 c->drawRect(SkRect::MakeXYWH(0, y, info.width(), width), paint);
59 }
60 }
61 return surface->makeImageSnapshot();
62}
63
64static void draw_image(SkCanvas* canvas, const sk_sp<SkImage> image, sk_sp<SkImageFilter> filter) {
65 SkAutoCanvasRestore acr(canvas, true);
66 SkPaint paint;
67 paint.setImageFilter(std::move(filter));
68
69 canvas->translate(SkIntToScalar(30), 0);
Mike Reed568f0ae2021-01-24 08:57:23 -050070 canvas->clipIRect(image->bounds());
Mike Reed07c5f522021-01-23 12:23:23 -050071 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
wutao039a7c72017-06-30 10:44:45 -070072}
73
74namespace skiagm {
75
76// This GM draws a colorful grids with different blur settings.
77class ImageBlurRepeatModeGM : public GM {
78public:
79 ImageBlurRepeatModeGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040080 this->setBGColor(0xFFCCCCCC);
wutao039a7c72017-06-30 10:44:45 -070081 }
82
83protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000084 SkString getName() const override { return SkString("imageblurrepeatmode"); }
wutao039a7c72017-06-30 10:44:45 -070085
Leandro Lovisolo8f023882023-08-15 21:13:52 +000086 SkISize getISize() override { return SkISize::Make(850, 920); }
wutao039a7c72017-06-30 10:44:45 -070087
88 bool runAsBench() const override { return true; }
89
90 void onDraw(SkCanvas* canvas) override {
91 sk_sp<SkImage> image[] =
92 { make_image(canvas, 1), make_image(canvas, 2), make_image(canvas, 3) };
Michael Ludwig02eecda2023-08-23 12:05:53 -040093 sk_sp<SkImageFilter> filter;
wutao039a7c72017-06-30 10:44:45 -070094
95 canvas->translate(0, 30);
96 // Test different kernel size, including the one to launch 2d Gaussian
97 // blur.
98 for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) {
Michael Ludwig02eecda2023-08-23 12:05:53 -040099 // FIXME crops
wutao039a7c72017-06-30 10:44:45 -0700100 canvas->save();
Michael Ludwig02eecda2023-08-23 12:05:53 -0400101 filter = SkImageFilters::Blur(
102 sigma, 0.0f, SkTileMode::kRepeat, nullptr, image[0]->bounds());
wutao039a7c72017-06-30 10:44:45 -0700103 draw_image(canvas, image[0], std::move(filter));
104 canvas->translate(image[0]->width() + 20, 0);
105
Michael Ludwig02eecda2023-08-23 12:05:53 -0400106 filter = SkImageFilters::Blur(
107 0.0f, sigma, SkTileMode::kRepeat, nullptr, image[1]->bounds());
wutao039a7c72017-06-30 10:44:45 -0700108 draw_image(canvas, image[1], std::move(filter));
109 canvas->translate(image[1]->width() + 20, 0);
110
Michael Ludwig02eecda2023-08-23 12:05:53 -0400111 filter = SkImageFilters::Blur(
112 sigma, sigma, SkTileMode::kRepeat, nullptr, image[2]->bounds());
wutao039a7c72017-06-30 10:44:45 -0700113 draw_image(canvas, image[2], std::move(filter));
114 canvas->translate(image[2]->width() + 20, 0);
115
116 canvas->restore();
117 canvas->translate(0, image[0]->height() + 20);
118 }
119 }
120
121private:
John Stiles7571f9e2020-09-02 22:42:33 -0400122 using INHERITED = GM;
wutao039a7c72017-06-30 10:44:45 -0700123};
124
125//////////////////////////////////////////////////////////////////////////////
126
127DEF_GM(return new ImageBlurRepeatModeGM;)
John Stilesa6841be2020-08-06 14:11:56 -0400128} // namespace skiagm
Michael Ludwigc80026c2020-04-16 10:25:55 -0400129
130// See skbug.com/10145 for more context, but if the blur doesn't have its own crop rect and
131// the canvas is not clipped, repeat can behave strangely (before fixes, this meant:
132// 1. The filtered results became semi-transparent when they should have remained opaque.
133// 2. The filtered results clip to 3xSigma, which makes sense for the decal tile mode, but not
134// the others.
135// 3. The repeat filter interacts non-intuitively when an expanded clip rect intersects the draw
136// geometry (it repeats across the edges of the intersection instead of repeating across the
137// draw and then clipping)).
138DEF_SIMPLE_GM(imageblurrepeatunclipped, canvas, 256, 128) {
139 // To show translucency
Mike Reed607a3822021-01-24 19:49:21 -0500140 auto checkerboard = ToolUtils::create_checkerboard_image(256, 128, SK_ColorLTGRAY,
141 SK_ColorGRAY, 8);
142 canvas->drawImage(checkerboard, 0, 0);
Michael Ludwigc80026c2020-04-16 10:25:55 -0400143
144 // Make an image with one red and one blue band
145 SkBitmap bmp;
146 bmp.allocN32Pixels(100, 20);
147 bmp.eraseArea(SkIRect::MakeWH(100, 10), SK_ColorRED);
148 bmp.eraseArea(SkIRect::MakeXYWH(0, 10, 100, 10), SK_ColorBLUE);
149
Mike Reedac9f0c92020-12-23 10:11:33 -0500150 auto img = bmp.asImage();
Michael Ludwig02eecda2023-08-23 12:05:53 -0400151 // The blur filter uses a repeat crop applied to the image bounds to define the tiling geometry,
152 // but the crop IF is created directly since the tilemode factory for ::Blur also adds a kDecal
153 // post-crop that is undesired for this GM.
154 auto filter = SkImageFilters::Blur(0, 10,
Michael Ludwigd78aba22023-09-25 12:18:14 -0400155 SkImageFilters::Crop(SkRect::Make(img->bounds()), SkTileMode::kRepeat, nullptr));
Michael Ludwigc80026c2020-04-16 10:25:55 -0400156 SkPaint paint;
157 paint.setImageFilter(std::move(filter));
158
Michael Ludwig02eecda2023-08-23 12:05:53 -0400159 // Draw the blurred image once with a clip that shows the repeat is tiled several times.
160 // 3xsigma is used to match the historic, but underspecified behavior for when kRepeat was used
161 // with no crop rect (which must now be provided or kRepeat is ignored).
Michael Ludwigc80026c2020-04-16 10:25:55 -0400162 canvas->translate(0, 50);
Michael Ludwig02eecda2023-08-23 12:05:53 -0400163 canvas->save();
164 canvas->clipIRect(img->bounds().makeOutset(0, 30));
165 canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
166 canvas->restore();
Michael Ludwigc80026c2020-04-16 10:25:55 -0400167
168 // Draw the blurred image with a clip positioned such that the draw would be excluded except
Michael Ludwig02eecda2023-08-23 12:05:53 -0400169 // that the image filter causes it to intersect with the clip. It should look like the
170 // left image, but clipped to the debug-black rectangle.
Michael Ludwigc80026c2020-04-16 10:25:55 -0400171 canvas->translate(110, 0);
Michael Ludwig02eecda2023-08-23 12:05:53 -0400172 canvas->save();
173 canvas->clipIRect(SkIRect::MakeXYWH(0, -30, 100, 10));
174 canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
175 canvas->restore();
Michael Ludwigc80026c2020-04-16 10:25:55 -0400176
177 // Visualize the clip
178 SkPaint line;
179 line.setStyle(SkPaint::kStroke_Style);
180 canvas->drawRect(SkRect::MakeXYWH(0, -30, 99, 9), line);
181}