blob: ac0aef441d350c828b653fb30168ce61b3a9fb52 [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:
84
85 SkString onShortName() override {
86 return SkString("imageblurrepeatmode");
87 }
88
89 SkISize onISize() override {
90 return SkISize::Make(850, 920);
91 }
92
93 bool runAsBench() const override { return true; }
94
95 void onDraw(SkCanvas* canvas) override {
96 sk_sp<SkImage> image[] =
97 { make_image(canvas, 1), make_image(canvas, 2), make_image(canvas, 3) };
98
99 canvas->translate(0, 30);
100 // Test different kernel size, including the one to launch 2d Gaussian
101 // blur.
102 for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) {
103 canvas->save();
104 sk_sp<SkImageFilter> filter(
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400105 SkImageFilters::Blur(sigma, 0.0f, SkTileMode::kRepeat, nullptr));
wutao039a7c72017-06-30 10:44:45 -0700106 draw_image(canvas, image[0], std::move(filter));
107 canvas->translate(image[0]->width() + 20, 0);
108
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400109 filter = SkImageFilters::Blur(0.0f, sigma, SkTileMode::kRepeat, nullptr);
wutao039a7c72017-06-30 10:44:45 -0700110 draw_image(canvas, image[1], std::move(filter));
111 canvas->translate(image[1]->width() + 20, 0);
112
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400113 filter = SkImageFilters::Blur(sigma, sigma, SkTileMode::kRepeat, nullptr);
wutao039a7c72017-06-30 10:44:45 -0700114 draw_image(canvas, image[2], std::move(filter));
115 canvas->translate(image[2]->width() + 20, 0);
116
117 canvas->restore();
118 canvas->translate(0, image[0]->height() + 20);
119 }
120 }
121
122private:
John Stiles7571f9e2020-09-02 22:42:33 -0400123 using INHERITED = GM;
wutao039a7c72017-06-30 10:44:45 -0700124};
125
126//////////////////////////////////////////////////////////////////////////////
127
128DEF_GM(return new ImageBlurRepeatModeGM;)
John Stilesa6841be2020-08-06 14:11:56 -0400129} // namespace skiagm
Michael Ludwigc80026c2020-04-16 10:25:55 -0400130
131// See skbug.com/10145 for more context, but if the blur doesn't have its own crop rect and
132// the canvas is not clipped, repeat can behave strangely (before fixes, this meant:
133// 1. The filtered results became semi-transparent when they should have remained opaque.
134// 2. The filtered results clip to 3xSigma, which makes sense for the decal tile mode, but not
135// the others.
136// 3. The repeat filter interacts non-intuitively when an expanded clip rect intersects the draw
137// geometry (it repeats across the edges of the intersection instead of repeating across the
138// draw and then clipping)).
139DEF_SIMPLE_GM(imageblurrepeatunclipped, canvas, 256, 128) {
140 // To show translucency
Mike Reed607a3822021-01-24 19:49:21 -0500141 auto checkerboard = ToolUtils::create_checkerboard_image(256, 128, SK_ColorLTGRAY,
142 SK_ColorGRAY, 8);
143 canvas->drawImage(checkerboard, 0, 0);
Michael Ludwigc80026c2020-04-16 10:25:55 -0400144
145 // Make an image with one red and one blue band
146 SkBitmap bmp;
147 bmp.allocN32Pixels(100, 20);
148 bmp.eraseArea(SkIRect::MakeWH(100, 10), SK_ColorRED);
149 bmp.eraseArea(SkIRect::MakeXYWH(0, 10, 100, 10), SK_ColorBLUE);
150
Mike Reedac9f0c92020-12-23 10:11:33 -0500151 auto img = bmp.asImage();
Michael Ludwigc80026c2020-04-16 10:25:55 -0400152 auto filter = SkImageFilters::Blur(0, 10, SkTileMode::kRepeat, nullptr);
153 SkPaint paint;
154 paint.setImageFilter(std::move(filter));
155
156 // Draw the blurred image once
157 canvas->translate(0, 50);
Mike Reed8d29ab62021-01-23 18:10:39 -0500158 canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
Michael Ludwigc80026c2020-04-16 10:25:55 -0400159
160 // Draw the blurred image with a clip positioned such that the draw would be excluded except
161 // that the image filter causes it to intersect with the clip. Ideally should look like the
162 // left image, but clipped to the debug-black rectangle (Narrator: it does not look like that).
163 canvas->translate(110, 0);
164 canvas->clipRect(SkRect::MakeXYWH(0, -30, 100, 10));
Mike Reed8d29ab62021-01-23 18:10:39 -0500165 canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
Michael Ludwigc80026c2020-04-16 10:25:55 -0400166
167 // Visualize the clip
168 SkPaint line;
169 line.setStyle(SkPaint::kStroke_Style);
170 canvas->drawRect(SkRect::MakeXYWH(0, -30, 99, 9), line);
171}