wutao | 0dc1f4f | 2017-06-26 15:03:55 -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 | 0dc1f4f | 2017-06-26 15:03:55 -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 | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 27 | static sk_sp<SkImage> make_image(SkCanvas* canvas) { |
| 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 | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 30 | SkCanvas* c = surface->getCanvas(); |
| 31 | SkPaint paint; |
| 32 | paint.setAntiAlias(true); |
| 33 | |
| 34 | paint.setColor(SK_ColorBLUE); |
| 35 | c->drawRect(SkRect::MakeIWH(info.width(), info.height()), paint); |
| 36 | paint.setColor(SK_ColorGREEN); |
| 37 | c->drawCircle(125, 100, 100, paint); |
| 38 | paint.setColor(SK_ColorRED); |
| 39 | c->drawRect(SkRect::MakeIWH(80, 80), paint); |
| 40 | |
| 41 | return surface->makeImageSnapshot(); |
| 42 | } |
| 43 | |
| 44 | static void draw_image(SkCanvas* canvas, const sk_sp<SkImage> image, sk_sp<SkImageFilter> filter) { |
| 45 | SkAutoCanvasRestore acr(canvas, true); |
| 46 | SkPaint paint; |
| 47 | paint.setImageFilter(std::move(filter)); |
| 48 | |
| 49 | canvas->translate(SkIntToScalar(30), 0); |
Mike Reed | 568f0ae | 2021-01-24 08:57:23 -0500 | [diff] [blame] | 50 | canvas->clipIRect(image->bounds()); |
Mike Reed | 07c5f52 | 2021-01-23 12:23:23 -0500 | [diff] [blame] | 51 | canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint); |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | namespace skiagm { |
| 55 | |
| 56 | // This GM draws one rectangle, one green inscribed circle, and one red square |
| 57 | // with different blur settings. |
| 58 | class ImageBlurClampModeGM : public GM { |
| 59 | public: |
| 60 | ImageBlurClampModeGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 61 | this->setBGColor(0xFFCCCCCC); |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | protected: |
| 65 | |
| 66 | SkString onShortName() override { |
| 67 | return SkString("imageblurclampmode"); |
| 68 | } |
| 69 | |
| 70 | SkISize onISize() override { |
| 71 | return SkISize::Make(850, 920); |
| 72 | } |
| 73 | |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 74 | bool runAsBench() const override { return true; } |
| 75 | |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 76 | void onDraw(SkCanvas* canvas) override { |
| 77 | sk_sp<SkImage> image(make_image(canvas)); |
Robert Phillips | 213ce18 | 2018-04-25 09:13:28 -0400 | [diff] [blame] | 78 | sk_sp<SkImageFilter> filter; |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 79 | |
| 80 | canvas->translate(0, 30); |
| 81 | // Test different kernel size, including the one to launch 2d Gaussian |
| 82 | // blur. |
| 83 | for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) { |
| 84 | canvas->save(); |
Robert Phillips | 213ce18 | 2018-04-25 09:13:28 -0400 | [diff] [blame] | 85 | |
| 86 | // x-only blur |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 87 | filter = SkImageFilters::Blur(sigma, 0.0f, SkTileMode::kClamp, nullptr); |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 88 | draw_image(canvas, image, std::move(filter)); |
| 89 | canvas->translate(image->width() + 20, 0); |
| 90 | |
Robert Phillips | 213ce18 | 2018-04-25 09:13:28 -0400 | [diff] [blame] | 91 | // y-only blur |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 92 | filter = SkImageFilters::Blur(0.0f, sigma, SkTileMode::kClamp, nullptr); |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 93 | draw_image(canvas, image, std::move(filter)); |
| 94 | canvas->translate(image->width() + 20, 0); |
| 95 | |
Robert Phillips | 213ce18 | 2018-04-25 09:13:28 -0400 | [diff] [blame] | 96 | // both directions |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 97 | filter = SkImageFilters::Blur(sigma, sigma, SkTileMode::kClamp, nullptr); |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 98 | draw_image(canvas, image, std::move(filter)); |
| 99 | canvas->translate(image->width() + 20, 0); |
| 100 | |
| 101 | canvas->restore(); |
Robert Phillips | 213ce18 | 2018-04-25 09:13:28 -0400 | [diff] [blame] | 102 | |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 103 | canvas->translate(0, image->height() + 20); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 108 | using INHERITED = GM; |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | ////////////////////////////////////////////////////////////////////////////// |
| 112 | |
| 113 | DEF_GM(return new ImageBlurClampModeGM;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 114 | } // namespace skiagm |