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 
13 static sk_sp<SkImage> make_image(SkCanvas* canvas) {
14     SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200);
15     auto surface = sk_tool_utils::makeSurface(canvas, info);
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() {
47         this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
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 
60     bool runAsBench() const override { return true; }
61 
62     void onDraw(SkCanvas* canvas) override {
63         sk_sp<SkImage> image(make_image(canvas));
64 
65         canvas->translate(0, 30);
66         // Test different kernel size, including the one to launch 2d Gaussian
67         // blur.
68         for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) {
69             canvas->save();
70             sk_sp<SkImageFilter> filter(
71                   SkBlurImageFilter::Make(sigma, 0.0f, nullptr, nullptr,
72                                           SkBlurImageFilter::kClamp_TileMode));
73             draw_image(canvas, image, std::move(filter));
74             canvas->translate(image->width() + 20, 0);
75 
76             filter = SkBlurImageFilter::Make(0.0f, sigma, nullptr, nullptr,
77                                              SkBlurImageFilter::kClamp_TileMode);
78             draw_image(canvas, image, std::move(filter));
79             canvas->translate(image->width() + 20, 0);
80 
81             filter = SkBlurImageFilter::Make(sigma, sigma, nullptr, nullptr,
82                                              SkBlurImageFilter::kClamp_TileMode);
83             draw_image(canvas, image, std::move(filter));
84             canvas->translate(image->width() + 20, 0);
85 
86             canvas->restore();
87             canvas->translate(0, image->height() + 20);
88         }
89     }
90 
91 private:
92     typedef GM INHERITED;
93 };
94 
95 //////////////////////////////////////////////////////////////////////////////
96 
97 DEF_GM(return new ImageBlurClampModeGM;)
98 }
99