wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/core/SkSurface.h" |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 22 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tools/ToolUtils.h" |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 24 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 25 | #include <initializer_list> |
| 26 | #include <utility> |
| 27 | |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 28 | static sk_sp<SkImage> make_image(SkCanvas* canvas, int direction) { |
| 29 | SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 30 | auto surface = ToolUtils::makeSurface(canvas, info); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 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) { |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 46 | paint.setAlphaf(0.5f); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 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) { |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 56 | paint.setAlphaf(0.5f); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 57 | } |
| 58 | c->drawRect(SkRect::MakeXYWH(0, y, info.width(), width), paint); |
| 59 | } |
| 60 | } |
| 61 | return surface->makeImageSnapshot(); |
| 62 | } |
| 63 | |
| 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); |
Mike Reed | 568f0ae | 2021-01-24 08:57:23 -0500 | [diff] [blame] | 70 | canvas->clipIRect(image->bounds()); |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 71 | canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | namespace skiagm { |
| 75 | |
| 76 | // This GM draws a colorful grids with different blur settings. |
| 77 | class ImageBlurRepeatModeGM : public GM { |
| 78 | public: |
| 79 | ImageBlurRepeatModeGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 80 | this->setBGColor(0xFFCCCCCC); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | protected: |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 84 | SkString getName() const override { return SkString("imageblurrepeatmode"); } |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 85 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 86 | SkISize getISize() override { return SkISize::Make(850, 920); } |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 87 | |
| 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 Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 93 | sk_sp<SkImageFilter> filter; |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 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 }) { |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 99 | // FIXME crops |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 100 | canvas->save(); |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 101 | filter = SkImageFilters::Blur( |
| 102 | sigma, 0.0f, SkTileMode::kRepeat, nullptr, image[0]->bounds()); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 103 | draw_image(canvas, image[0], std::move(filter)); |
| 104 | canvas->translate(image[0]->width() + 20, 0); |
| 105 | |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 106 | filter = SkImageFilters::Blur( |
| 107 | 0.0f, sigma, SkTileMode::kRepeat, nullptr, image[1]->bounds()); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 108 | draw_image(canvas, image[1], std::move(filter)); |
| 109 | canvas->translate(image[1]->width() + 20, 0); |
| 110 | |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 111 | filter = SkImageFilters::Blur( |
| 112 | sigma, sigma, SkTileMode::kRepeat, nullptr, image[2]->bounds()); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 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: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 122 | using INHERITED = GM; |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | ////////////////////////////////////////////////////////////////////////////// |
| 126 | |
| 127 | DEF_GM(return new ImageBlurRepeatModeGM;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 128 | } // namespace skiagm |
Michael Ludwig | c80026c | 2020-04-16 10:25:55 -0400 | [diff] [blame] | 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 |
Mike Reed | 607a382 | 2021-01-24 19:49:21 -0500 | [diff] [blame] | 140 | auto checkerboard = ToolUtils::create_checkerboard_image(256, 128, SK_ColorLTGRAY, |
| 141 | SK_ColorGRAY, 8); |
| 142 | canvas->drawImage(checkerboard, 0, 0); |
Michael Ludwig | c80026c | 2020-04-16 10:25:55 -0400 | [diff] [blame] | 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 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 150 | auto img = bmp.asImage(); |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 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, |
Michael Ludwig | d78aba2 | 2023-09-25 12:18:14 -0400 | [diff] [blame] | 155 | SkImageFilters::Crop(SkRect::Make(img->bounds()), SkTileMode::kRepeat, nullptr)); |
Michael Ludwig | c80026c | 2020-04-16 10:25:55 -0400 | [diff] [blame] | 156 | SkPaint paint; |
| 157 | paint.setImageFilter(std::move(filter)); |
| 158 | |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 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). |
Michael Ludwig | c80026c | 2020-04-16 10:25:55 -0400 | [diff] [blame] | 162 | canvas->translate(0, 50); |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 163 | canvas->save(); |
| 164 | canvas->clipIRect(img->bounds().makeOutset(0, 30)); |
| 165 | canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint); |
| 166 | canvas->restore(); |
Michael Ludwig | c80026c | 2020-04-16 10:25:55 -0400 | [diff] [blame] | 167 | |
| 168 | // Draw the blurred image with a clip positioned such that the draw would be excluded except |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 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. |
Michael Ludwig | c80026c | 2020-04-16 10:25:55 -0400 | [diff] [blame] | 171 | canvas->translate(110, 0); |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 172 | canvas->save(); |
| 173 | canvas->clipIRect(SkIRect::MakeXYWH(0, -30, 100, 10)); |
| 174 | canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint); |
| 175 | canvas->restore(); |
Michael Ludwig | c80026c | 2020-04-16 10:25:55 -0400 | [diff] [blame] | 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 | } |