blob: f16f01db371b1f808fe5d048d19c0dfc974755f1 [file] [log] [blame]
wutao0dc1f4f2017-06-26 15:03:55 -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
8#include "gm.h"
9#include "sk_tool_utils.h"
10#include "SkSurface.h"
11#include "SkBlurImageFilter.h"
12
wutao0dc1f4f2017-06-26 15:03:55 -070013static sk_sp<SkImage> make_image(SkCanvas* canvas) {
14 SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200);
Cary Clarka24712e2018-09-05 18:41:40 +000015 auto surface = sk_tool_utils::makeSurface(canvas, info);
wutao0dc1f4f2017-06-26 15:03:55 -070016 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
30static 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
40namespace skiagm {
41
42// This GM draws one rectangle, one green inscribed circle, and one red square
43// with different blur settings.
44class ImageBlurClampModeGM : public GM {
45public:
46 ImageBlurClampModeGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040047 this->setBGColor(0xFFCCCCCC);
wutao0dc1f4f2017-06-26 15:03:55 -070048 }
49
50protected:
51
52 SkString onShortName() override {
53 return SkString("imageblurclampmode");
54 }
55
56 SkISize onISize() override {
57 return SkISize::Make(850, 920);
58 }
59
wutao039a7c72017-06-30 10:44:45 -070060 bool runAsBench() const override { return true; }
61
wutao0dc1f4f2017-06-26 15:03:55 -070062 void onDraw(SkCanvas* canvas) override {
63 sk_sp<SkImage> image(make_image(canvas));
Robert Phillips213ce182018-04-25 09:13:28 -040064 sk_sp<SkImageFilter> filter;
wutao0dc1f4f2017-06-26 15:03:55 -070065
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 Phillips213ce182018-04-25 09:13:28 -040071
72 // x-only blur
73 filter = SkBlurImageFilter::Make(sigma, 0.0f, nullptr, nullptr,
74 SkBlurImageFilter::kClamp_TileMode);
wutao0dc1f4f2017-06-26 15:03:55 -070075 draw_image(canvas, image, std::move(filter));
76 canvas->translate(image->width() + 20, 0);
77
Robert Phillips213ce182018-04-25 09:13:28 -040078 // y-only blur
wutao039a7c72017-06-30 10:44:45 -070079 filter = SkBlurImageFilter::Make(0.0f, sigma, nullptr, nullptr,
80 SkBlurImageFilter::kClamp_TileMode);
wutao0dc1f4f2017-06-26 15:03:55 -070081 draw_image(canvas, image, std::move(filter));
82 canvas->translate(image->width() + 20, 0);
83
Robert Phillips213ce182018-04-25 09:13:28 -040084 // both directions
wutao039a7c72017-06-30 10:44:45 -070085 filter = SkBlurImageFilter::Make(sigma, sigma, nullptr, nullptr,
86 SkBlurImageFilter::kClamp_TileMode);
wutao0dc1f4f2017-06-26 15:03:55 -070087 draw_image(canvas, image, std::move(filter));
88 canvas->translate(image->width() + 20, 0);
89
90 canvas->restore();
Robert Phillips213ce182018-04-25 09:13:28 -040091
wutao0dc1f4f2017-06-26 15:03:55 -070092 canvas->translate(0, image->height() + 20);
93 }
94 }
95
96private:
97 typedef GM INHERITED;
98};
99
100//////////////////////////////////////////////////////////////////////////////
101
102DEF_GM(return new ImageBlurClampModeGM;)
103}