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 | |
| 8 | #include "gm.h" |
| 9 | #include "sk_tool_utils.h" |
| 10 | #include "SkSurface.h" |
| 11 | #include "SkBlurImageFilter.h" |
| 12 | |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 13 | static sk_sp<SkImage> make_image(SkCanvas* canvas) { |
| 14 | SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200); |
Cary Clark | a24712e | 2018-09-05 18:41:40 +0000 | [diff] [blame] | 15 | auto surface = sk_tool_utils::makeSurface(canvas, info); |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 16 | SkCanvas* c = surface->getCanvas(); |
| 17 | SkPaint paint; |
| 18 | paint.setAntiAlias(true); |
| 19 | |
| 20 | paint.setColor(SK_ColorBLUE); |
| 21 | c->drawRect(SkRect::MakeIWH(info.width(), info.height()), paint); |
| 22 | paint.setColor(SK_ColorGREEN); |
| 23 | c->drawCircle(125, 100, 100, paint); |
| 24 | paint.setColor(SK_ColorRED); |
| 25 | c->drawRect(SkRect::MakeIWH(80, 80), paint); |
| 26 | |
| 27 | return surface->makeImageSnapshot(); |
| 28 | } |
| 29 | |
| 30 | static void draw_image(SkCanvas* canvas, const sk_sp<SkImage> image, sk_sp<SkImageFilter> filter) { |
| 31 | SkAutoCanvasRestore acr(canvas, true); |
| 32 | SkPaint paint; |
| 33 | paint.setImageFilter(std::move(filter)); |
| 34 | |
| 35 | canvas->translate(SkIntToScalar(30), 0); |
| 36 | canvas->clipRect(SkRect::MakeIWH(image->width(),image->height())); |
| 37 | canvas->drawImage(image, 0, 0, &paint); |
| 38 | } |
| 39 | |
| 40 | namespace skiagm { |
| 41 | |
| 42 | // This GM draws one rectangle, one green inscribed circle, and one red square |
| 43 | // with different blur settings. |
| 44 | class ImageBlurClampModeGM : public GM { |
| 45 | public: |
| 46 | ImageBlurClampModeGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 47 | this->setBGColor(0xFFCCCCCC); |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | protected: |
| 51 | |
| 52 | SkString onShortName() override { |
| 53 | return SkString("imageblurclampmode"); |
| 54 | } |
| 55 | |
| 56 | SkISize onISize() override { |
| 57 | return SkISize::Make(850, 920); |
| 58 | } |
| 59 | |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 60 | bool runAsBench() const override { return true; } |
| 61 | |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 62 | void onDraw(SkCanvas* canvas) override { |
| 63 | sk_sp<SkImage> image(make_image(canvas)); |
Robert Phillips | 213ce18 | 2018-04-25 09:13:28 -0400 | [diff] [blame] | 64 | sk_sp<SkImageFilter> filter; |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 65 | |
| 66 | canvas->translate(0, 30); |
| 67 | // Test different kernel size, including the one to launch 2d Gaussian |
| 68 | // blur. |
| 69 | for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) { |
| 70 | canvas->save(); |
Robert Phillips | 213ce18 | 2018-04-25 09:13:28 -0400 | [diff] [blame] | 71 | |
| 72 | // x-only blur |
| 73 | filter = SkBlurImageFilter::Make(sigma, 0.0f, nullptr, nullptr, |
| 74 | SkBlurImageFilter::kClamp_TileMode); |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 75 | draw_image(canvas, image, std::move(filter)); |
| 76 | canvas->translate(image->width() + 20, 0); |
| 77 | |
Robert Phillips | 213ce18 | 2018-04-25 09:13:28 -0400 | [diff] [blame] | 78 | // y-only blur |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 79 | filter = SkBlurImageFilter::Make(0.0f, sigma, nullptr, nullptr, |
| 80 | SkBlurImageFilter::kClamp_TileMode); |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 81 | draw_image(canvas, image, std::move(filter)); |
| 82 | canvas->translate(image->width() + 20, 0); |
| 83 | |
Robert Phillips | 213ce18 | 2018-04-25 09:13:28 -0400 | [diff] [blame] | 84 | // both directions |
wutao | 039a7c7 | 2017-06-30 10:44:45 -0700 | [diff] [blame] | 85 | filter = SkBlurImageFilter::Make(sigma, sigma, nullptr, nullptr, |
| 86 | SkBlurImageFilter::kClamp_TileMode); |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 87 | draw_image(canvas, image, std::move(filter)); |
| 88 | canvas->translate(image->width() + 20, 0); |
| 89 | |
| 90 | canvas->restore(); |
Robert Phillips | 213ce18 | 2018-04-25 09:13:28 -0400 | [diff] [blame] | 91 | |
wutao | 0dc1f4f | 2017-06-26 15:03:55 -0700 | [diff] [blame] | 92 | canvas->translate(0, image->height() + 20); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | typedef GM INHERITED; |
| 98 | }; |
| 99 | |
| 100 | ////////////////////////////////////////////////////////////////////////////// |
| 101 | |
| 102 | DEF_GM(return new ImageBlurClampModeGM;) |
| 103 | } |