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/gm.h"
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"
20 #include "include/core/SkSurface.h"
21 #include "include/effects/SkImageFilters.h"
22 #include "tools/ToolUtils.h"
23
24 #include <initializer_list>
25 #include <utility>
26
make_image(SkCanvas * canvas)27 static sk_sp<SkImage> make_image(SkCanvas* canvas) {
28 SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200);
29 auto surface = ToolUtils::makeSurface(canvas, info);
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
draw_image(SkCanvas * canvas,const sk_sp<SkImage> image,sk_sp<SkImageFilter> filter)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);
50 canvas->clipIRect(image->bounds());
51 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
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:
ImageBlurClampModeGM()60 ImageBlurClampModeGM() {
61 this->setBGColor(0xFFCCCCCC);
62 }
63
64 protected:
65
onShortName()66 SkString onShortName() override {
67 return SkString("imageblurclampmode");
68 }
69
onISize()70 SkISize onISize() override {
71 return SkISize::Make(850, 920);
72 }
73
runAsBench() const74 bool runAsBench() const override { return true; }
75
onDraw(SkCanvas * canvas)76 void onDraw(SkCanvas* canvas) override {
77 sk_sp<SkImage> image(make_image(canvas));
78 sk_sp<SkImageFilter> filter;
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();
85
86 // x-only blur
87 filter = SkImageFilters::Blur(sigma, 0.0f, SkTileMode::kClamp, nullptr);
88 draw_image(canvas, image, std::move(filter));
89 canvas->translate(image->width() + 20, 0);
90
91 // y-only blur
92 filter = SkImageFilters::Blur(0.0f, sigma, SkTileMode::kClamp, nullptr);
93 draw_image(canvas, image, std::move(filter));
94 canvas->translate(image->width() + 20, 0);
95
96 // both directions
97 filter = SkImageFilters::Blur(sigma, sigma, SkTileMode::kClamp, nullptr);
98 draw_image(canvas, image, std::move(filter));
99 canvas->translate(image->width() + 20, 0);
100
101 canvas->restore();
102
103 canvas->translate(0, image->height() + 20);
104 }
105 }
106
107 private:
108 using INHERITED = GM;
109 };
110
111 //////////////////////////////////////////////////////////////////////////////
112
113 DEF_GM(return new ImageBlurClampModeGM;)
114 } // namespace skiagm
115