xref: /aosp_15_r20/external/skia/gm/imageblurrepeatmode.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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 
8 #include "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #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"
21 #include "include/core/SkSurface.h"
22 #include "include/effects/SkImageFilters.h"
23 #include "tools/ToolUtils.h"
24 
25 #include <initializer_list>
26 #include <utility>
27 
make_image(SkCanvas * canvas,int direction)28 static sk_sp<SkImage> make_image(SkCanvas* canvas, int direction) {
29     SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200);
30     auto        surface = ToolUtils::makeSurface(canvas, info);
31     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) {
46                 paint.setAlphaf(0.5f);
47             }
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) {
56                 paint.setAlphaf(0.5f);
57             }
58             c->drawRect(SkRect::MakeXYWH(0, y, info.width(), width), paint);
59         }
60     }
61     return surface->makeImageSnapshot();
62 }
63 
draw_image(SkCanvas * canvas,const sk_sp<SkImage> image,sk_sp<SkImageFilter> filter)64 static 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);
70     canvas->clipIRect(image->bounds());
71     canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
72 }
73 
74 namespace skiagm {
75 
76 // This GM draws a colorful grids with different blur settings.
77 class ImageBlurRepeatModeGM : public GM {
78 public:
ImageBlurRepeatModeGM()79     ImageBlurRepeatModeGM() {
80         this->setBGColor(0xFFCCCCCC);
81     }
82 
83 protected:
getName() const84     SkString getName() const override { return SkString("imageblurrepeatmode"); }
85 
getISize()86     SkISize getISize() override { return SkISize::Make(850, 920); }
87 
runAsBench() const88     bool runAsBench() const override { return true; }
89 
onDraw(SkCanvas * canvas)90     void onDraw(SkCanvas* canvas) override {
91         sk_sp<SkImage> image[] =
92                 { make_image(canvas, 1), make_image(canvas, 2), make_image(canvas, 3) };
93         sk_sp<SkImageFilter> filter;
94 
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 }) {
99             // FIXME crops
100             canvas->save();
101             filter = SkImageFilters::Blur(
102                     sigma, 0.0f, SkTileMode::kRepeat, nullptr, image[0]->bounds());
103             draw_image(canvas, image[0], std::move(filter));
104             canvas->translate(image[0]->width() + 20, 0);
105 
106             filter = SkImageFilters::Blur(
107                     0.0f, sigma, SkTileMode::kRepeat, nullptr, image[1]->bounds());
108             draw_image(canvas, image[1], std::move(filter));
109             canvas->translate(image[1]->width() + 20, 0);
110 
111             filter = SkImageFilters::Blur(
112                     sigma, sigma, SkTileMode::kRepeat, nullptr, image[2]->bounds());
113             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 
121 private:
122     using INHERITED = GM;
123 };
124 
125 //////////////////////////////////////////////////////////////////////////////
126 
127 DEF_GM(return new ImageBlurRepeatModeGM;)
128 }  // namespace skiagm
129 
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)).
138 DEF_SIMPLE_GM(imageblurrepeatunclipped, canvas, 256, 128) {
139     // To show translucency
140     auto checkerboard = ToolUtils::create_checkerboard_image(256, 128, SK_ColorLTGRAY,
141                                                              SK_ColorGRAY, 8);
142     canvas->drawImage(checkerboard, 0, 0);
143 
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 
150     auto img = bmp.asImage();
151     // 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,
155             SkImageFilters::Crop(SkRect::Make(img->bounds()), SkTileMode::kRepeat, nullptr));
156     SkPaint paint;
157     paint.setImageFilter(std::move(filter));
158 
159     // 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).
162     canvas->translate(0, 50);
163     canvas->save();
164         canvas->clipIRect(img->bounds().makeOutset(0, 30));
165         canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
166     canvas->restore();
167 
168     // Draw the blurred image with a clip positioned such that the draw would be excluded except
169     // 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.
171     canvas->translate(110, 0);
172     canvas->save();
173         canvas->clipIRect(SkIRect::MakeXYWH(0, -30, 100, 10));
174         canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
175     canvas->restore();
176 
177     // Visualize the clip
178     SkPaint line;
179     line.setStyle(SkPaint::kStroke_Style);
180     canvas->drawRect(SkRect::MakeXYWH(0, -30, 99, 9), line);
181 }
182