blob: 8f8d25759cfce72e88df385b4993051ad81b9d4c [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"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkImage.h"
12#include "include/core/SkImageFilter.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkSurface.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040021#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "tools/ToolUtils.h"
wutao039a7c72017-06-30 10:44:45 -070023
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include <initializer_list>
25#include <utility>
26
wutao039a7c72017-06-30 10:44:45 -070027static sk_sp<SkImage> make_image(SkCanvas* canvas, int direction) {
28 SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200);
Mike Kleinea3f0142019-03-20 11:12:10 -050029 auto surface = ToolUtils::makeSurface(canvas, info);
wutao039a7c72017-06-30 10:44:45 -070030 SkCanvas* c = surface->getCanvas();
31 SkPaint paint;
32 paint.setAntiAlias(true);
33
34 const SkColor colors[] = {
35 SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorYELLOW, SK_ColorBLACK
36 };
37
38 int width = 25;
39 bool xDirection = (direction & 0x1) == 1;
40 bool yDirection = (direction & 0x2) == 2;
41 if (xDirection) {
42 for (int x = 0; x < info.width(); x += width) {
43 paint.setColor(colors[x/width % 5]);
44 if (yDirection) {
Mike Reed9407e242019-02-15 16:13:57 -050045 paint.setAlphaf(0.5f);
wutao039a7c72017-06-30 10:44:45 -070046 }
47 c->drawRect(SkRect::MakeXYWH(x, 0, width, info.height()), paint);
48 }
49 }
50
51 if (yDirection) {
52 for (int y = 0; y < info.height(); y += width) {
53 paint.setColor(colors[y/width % 5]);
54 if (xDirection) {
Mike Reed9407e242019-02-15 16:13:57 -050055 paint.setAlphaf(0.5f);
wutao039a7c72017-06-30 10:44:45 -070056 }
57 c->drawRect(SkRect::MakeXYWH(0, y, info.width(), width), paint);
58 }
59 }
60 return surface->makeImageSnapshot();
61}
62
63static void draw_image(SkCanvas* canvas, const sk_sp<SkImage> image, sk_sp<SkImageFilter> filter) {
64 SkAutoCanvasRestore acr(canvas, true);
65 SkPaint paint;
66 paint.setImageFilter(std::move(filter));
67
68 canvas->translate(SkIntToScalar(30), 0);
69 canvas->clipRect(SkRect::MakeIWH(image->width(),image->height()));
70 canvas->drawImage(image, 0, 0, &paint);
71}
72
73namespace skiagm {
74
75// This GM draws a colorful grids with different blur settings.
76class ImageBlurRepeatModeGM : public GM {
77public:
78 ImageBlurRepeatModeGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040079 this->setBGColor(0xFFCCCCCC);
wutao039a7c72017-06-30 10:44:45 -070080 }
81
82protected:
83
84 SkString onShortName() override {
85 return SkString("imageblurrepeatmode");
86 }
87
88 SkISize onISize() override {
89 return SkISize::Make(850, 920);
90 }
91
92 bool runAsBench() const override { return true; }
93
94 void onDraw(SkCanvas* canvas) override {
95 sk_sp<SkImage> image[] =
96 { make_image(canvas, 1), make_image(canvas, 2), make_image(canvas, 3) };
97
98 canvas->translate(0, 30);
99 // Test different kernel size, including the one to launch 2d Gaussian
100 // blur.
101 for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) {
102 canvas->save();
103 sk_sp<SkImageFilter> filter(
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400104 SkImageFilters::Blur(sigma, 0.0f, SkTileMode::kRepeat, nullptr));
wutao039a7c72017-06-30 10:44:45 -0700105 draw_image(canvas, image[0], std::move(filter));
106 canvas->translate(image[0]->width() + 20, 0);
107
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400108 filter = SkImageFilters::Blur(0.0f, sigma, SkTileMode::kRepeat, nullptr);
wutao039a7c72017-06-30 10:44:45 -0700109 draw_image(canvas, image[1], std::move(filter));
110 canvas->translate(image[1]->width() + 20, 0);
111
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400112 filter = SkImageFilters::Blur(sigma, sigma, SkTileMode::kRepeat, nullptr);
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:
122 typedef GM INHERITED;
123};
124
125//////////////////////////////////////////////////////////////////////////////
126
127DEF_GM(return new ImageBlurRepeatModeGM;)
128}
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
140 SkBitmap checkerboard = ToolUtils::create_checkerboard_bitmap(256, 128, SK_ColorLTGRAY,
141 SK_ColorGRAY, 8);
142 canvas->drawBitmap(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 = SkImage::MakeFromBitmap(bmp);
151 auto filter = SkImageFilters::Blur(0, 10, SkTileMode::kRepeat, nullptr);
152 SkPaint paint;
153 paint.setImageFilter(std::move(filter));
154
155 // Draw the blurred image once
156 canvas->translate(0, 50);
157 canvas->drawImage(img, 0, 0, &paint);
158
159 // Draw the blurred image with a clip positioned such that the draw would be excluded except
160 // that the image filter causes it to intersect with the clip. Ideally should look like the
161 // left image, but clipped to the debug-black rectangle (Narrator: it does not look like that).
162 canvas->translate(110, 0);
163 canvas->clipRect(SkRect::MakeXYWH(0, -30, 100, 10));
164 canvas->drawImage(img, 0, 0, &paint);
165
166 // Visualize the clip
167 SkPaint line;
168 line.setStyle(SkPaint::kStroke_Style);
169 canvas->drawRect(SkRect::MakeXYWH(0, -30, 99, 9), line);
170}