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