• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
make_image(SkCanvas * canvas)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 
draw_image(SkCanvas * canvas,const sk_sp<SkImage> image,sk_sp<SkImageFilter> filter)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:
ImageBlurClampModeGM()46     ImageBlurClampModeGM() {
47         this->setBGColor(0xFFCCCCCC);
48     }
49 
50 protected:
51 
onShortName()52     SkString onShortName() override {
53         return SkString("imageblurclampmode");
54     }
55 
onISize()56     SkISize onISize() override {
57         return SkISize::Make(850, 920);
58     }
59 
runAsBench() const60     bool runAsBench() const override { return true; }
61 
onDraw(SkCanvas * canvas)62     void onDraw(SkCanvas* canvas) override {
63         sk_sp<SkImage> image(make_image(canvas));
64         sk_sp<SkImageFilter> filter;
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();
71 
72             // x-only blur
73             filter =  SkBlurImageFilter::Make(sigma, 0.0f, nullptr, nullptr,
74                                               SkBlurImageFilter::kClamp_TileMode);
75             draw_image(canvas, image, std::move(filter));
76             canvas->translate(image->width() + 20, 0);
77 
78             // y-only blur
79             filter = SkBlurImageFilter::Make(0.0f, sigma, nullptr, nullptr,
80                                              SkBlurImageFilter::kClamp_TileMode);
81             draw_image(canvas, image, std::move(filter));
82             canvas->translate(image->width() + 20, 0);
83 
84             // both directions
85             filter = SkBlurImageFilter::Make(sigma, sigma, nullptr, nullptr,
86                                              SkBlurImageFilter::kClamp_TileMode);
87             draw_image(canvas, image, std::move(filter));
88             canvas->translate(image->width() + 20, 0);
89 
90             canvas->restore();
91 
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 }
104