• 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_surface(SkCanvas * canvas,const SkImageInfo & info)13 static sk_sp<SkSurface> make_surface(SkCanvas* canvas, const SkImageInfo& info) {
14     auto surface = canvas->makeSurface(info);
15     if (!surface) {
16         surface = SkSurface::MakeRaster(info);
17     }
18     return surface;
19 }
20 
make_image(SkCanvas * canvas,int direction)21 static sk_sp<SkImage> make_image(SkCanvas* canvas, int direction) {
22     SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200);
23     auto surface = make_surface(canvas, info);
24     SkCanvas* c = surface->getCanvas();
25     SkPaint paint;
26     paint.setAntiAlias(true);
27 
28     const SkColor colors[] = {
29         SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorYELLOW, SK_ColorBLACK
30     };
31 
32     int width = 25;
33     bool xDirection = (direction & 0x1) == 1;
34     bool yDirection = (direction & 0x2) == 2;
35     if (xDirection) {
36         for (int x = 0; x < info.width(); x += width) {
37             paint.setColor(colors[x/width % 5]);
38             if (yDirection) {
39                 paint.setAlpha(127);
40             }
41             c->drawRect(SkRect::MakeXYWH(x, 0, width, info.height()), paint);
42         }
43     }
44 
45     if (yDirection) {
46         for (int y = 0; y < info.height(); y += width) {
47             paint.setColor(colors[y/width % 5]);
48             if (xDirection) {
49                 paint.setAlpha(127);
50             }
51             c->drawRect(SkRect::MakeXYWH(0, y, info.width(), width), paint);
52         }
53     }
54     return surface->makeImageSnapshot();
55 }
56 
draw_image(SkCanvas * canvas,const sk_sp<SkImage> image,sk_sp<SkImageFilter> filter)57 static void draw_image(SkCanvas* canvas, const sk_sp<SkImage> image, sk_sp<SkImageFilter> filter) {
58     SkAutoCanvasRestore acr(canvas, true);
59     SkPaint paint;
60     paint.setImageFilter(std::move(filter));
61 
62     canvas->translate(SkIntToScalar(30), 0);
63     canvas->clipRect(SkRect::MakeIWH(image->width(),image->height()));
64     canvas->drawImage(image, 0, 0, &paint);
65 }
66 
67 namespace skiagm {
68 
69 // This GM draws a colorful grids with different blur settings.
70 class ImageBlurRepeatModeGM : public GM {
71 public:
ImageBlurRepeatModeGM()72     ImageBlurRepeatModeGM() {
73         this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
74     }
75 
76 protected:
77 
onShortName()78     SkString onShortName() override {
79         return SkString("imageblurrepeatmode");
80     }
81 
onISize()82     SkISize onISize() override {
83         return SkISize::Make(850, 920);
84     }
85 
runAsBench() const86     bool runAsBench() const override { return true; }
87 
onDraw(SkCanvas * canvas)88     void onDraw(SkCanvas* canvas) override {
89         sk_sp<SkImage> image[] =
90                 { make_image(canvas, 1), make_image(canvas, 2), make_image(canvas, 3) };
91 
92         canvas->translate(0, 30);
93         // Test different kernel size, including the one to launch 2d Gaussian
94         // blur.
95         for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) {
96             canvas->save();
97             sk_sp<SkImageFilter> filter(
98                   SkBlurImageFilter::Make(sigma, 0.0f, nullptr, nullptr,
99                                           SkBlurImageFilter::kRepeat_TileMode));
100             draw_image(canvas, image[0], std::move(filter));
101             canvas->translate(image[0]->width() + 20, 0);
102 
103             filter = SkBlurImageFilter::Make(0.0f, sigma, nullptr, nullptr,
104                                              SkBlurImageFilter::kRepeat_TileMode);
105             draw_image(canvas, image[1], std::move(filter));
106             canvas->translate(image[1]->width() + 20, 0);
107 
108             filter = SkBlurImageFilter::Make(sigma, sigma, nullptr, nullptr,
109                                              SkBlurImageFilter::kRepeat_TileMode);
110             draw_image(canvas, image[2], std::move(filter));
111             canvas->translate(image[2]->width() + 20, 0);
112 
113             canvas->restore();
114             canvas->translate(0, image[0]->height() + 20);
115         }
116     }
117 
118 private:
119     typedef GM INHERITED;
120 };
121 
122 //////////////////////////////////////////////////////////////////////////////
123 
124 DEF_GM(return new ImageBlurRepeatModeGM;)
125 }
126