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" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/core/SkSurface.h" |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 21 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "tools/ToolUtils.h" |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 23 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #include <initializer_list> |
| 25 | #include <utility> |
| 26 | |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 27 | static sk_sp<SkImage> make_image(SkCanvas* canvas, int direction) { |
| 28 | SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 29 | auto surface = ToolUtils::makeSurface(canvas, info); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 30 | 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 Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 45 | paint.setAlphaf(0.5f); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 46 | } |
| 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 Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 55 | paint.setAlphaf(0.5f); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 56 | } |
| 57 | c->drawRect(SkRect::MakeXYWH(0, y, info.width(), width), paint); |
| 58 | } |
| 59 | } |
| 60 | return surface->makeImageSnapshot(); |
| 61 | } |
| 62 | |
| 63 | static 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 | |
| 73 | namespace skiagm { |
| 74 | |
| 75 | // This GM draws a colorful grids with different blur settings. |
| 76 | class ImageBlurRepeatModeGM : public GM { |
| 77 | public: |
| 78 | ImageBlurRepeatModeGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 79 | this->setBGColor(0xFFCCCCCC); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | protected: |
| 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 Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 104 | SkImageFilters::Blur(sigma, 0.0f, SkTileMode::kRepeat, nullptr)); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 105 | draw_image(canvas, image[0], std::move(filter)); |
| 106 | canvas->translate(image[0]->width() + 20, 0); |
| 107 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 108 | filter = SkImageFilters::Blur(0.0f, sigma, SkTileMode::kRepeat, nullptr); |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 109 | draw_image(canvas, image[1], std::move(filter)); |
| 110 | canvas->translate(image[1]->width() + 20, 0); |
| 111 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 112 | filter = SkImageFilters::Blur(sigma, sigma, SkTileMode::kRepeat, nullptr); |
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: |
| 122 | typedef GM INHERITED; |
| 123 | }; |
| 124 | |
| 125 | ////////////////////////////////////////////////////////////////////////////// |
| 126 | |
| 127 | DEF_GM(return new ImageBlurRepeatModeGM;) |
| 128 | } |
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 |
| 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 | } |