• 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/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkImageFilter.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkSurface.h"
22 #include "include/effects/SkImageFilters.h"
23 #include "tools/ToolUtils.h"
24 
25 #include <initializer_list>
26 #include <utility>
27 
make_image(SkCanvas * canvas,int direction)28 static sk_sp<SkImage> make_image(SkCanvas* canvas, int direction) {
29     SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200);
30     auto        surface = ToolUtils::makeSurface(canvas, info);
31     SkCanvas* c = surface->getCanvas();
32     SkPaint paint;
33     paint.setAntiAlias(true);
34 
35     const SkColor colors[] = {
36         SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorYELLOW, SK_ColorBLACK
37     };
38 
39     int width = 25;
40     bool xDirection = (direction & 0x1) == 1;
41     bool yDirection = (direction & 0x2) == 2;
42     if (xDirection) {
43         for (int x = 0; x < info.width(); x += width) {
44             paint.setColor(colors[x/width % 5]);
45             if (yDirection) {
46                 paint.setAlphaf(0.5f);
47             }
48             c->drawRect(SkRect::MakeXYWH(x, 0, width, info.height()), paint);
49         }
50     }
51 
52     if (yDirection) {
53         for (int y = 0; y < info.height(); y += width) {
54             paint.setColor(colors[y/width % 5]);
55             if (xDirection) {
56                 paint.setAlphaf(0.5f);
57             }
58             c->drawRect(SkRect::MakeXYWH(0, y, info.width(), width), paint);
59         }
60     }
61     return surface->makeImageSnapshot();
62 }
63 
draw_image(SkCanvas * canvas,const sk_sp<SkImage> image,sk_sp<SkImageFilter> filter)64 static void draw_image(SkCanvas* canvas, const sk_sp<SkImage> image, sk_sp<SkImageFilter> filter) {
65     SkAutoCanvasRestore acr(canvas, true);
66     SkPaint paint;
67     paint.setImageFilter(std::move(filter));
68 
69     canvas->translate(SkIntToScalar(30), 0);
70     canvas->clipIRect(image->bounds());
71     canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
72 }
73 
74 namespace skiagm {
75 
76 // This GM draws a colorful grids with different blur settings.
77 class ImageBlurRepeatModeGM : public GM {
78 public:
ImageBlurRepeatModeGM()79     ImageBlurRepeatModeGM() {
80         this->setBGColor(0xFFCCCCCC);
81     }
82 
83 protected:
84 
onShortName()85     SkString onShortName() override {
86         return SkString("imageblurrepeatmode");
87     }
88 
onISize()89     SkISize onISize() override {
90         return SkISize::Make(850, 920);
91     }
92 
runAsBench() const93     bool runAsBench() const override { return true; }
94 
onDraw(SkCanvas * canvas)95     void onDraw(SkCanvas* canvas) override {
96         sk_sp<SkImage> image[] =
97                 { make_image(canvas, 1), make_image(canvas, 2), make_image(canvas, 3) };
98 
99         canvas->translate(0, 30);
100         // Test different kernel size, including the one to launch 2d Gaussian
101         // blur.
102         for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) {
103             canvas->save();
104             sk_sp<SkImageFilter> filter(
105                   SkImageFilters::Blur(sigma, 0.0f, SkTileMode::kRepeat, nullptr));
106             draw_image(canvas, image[0], std::move(filter));
107             canvas->translate(image[0]->width() + 20, 0);
108 
109             filter = SkImageFilters::Blur(0.0f, sigma, SkTileMode::kRepeat, nullptr);
110             draw_image(canvas, image[1], std::move(filter));
111             canvas->translate(image[1]->width() + 20, 0);
112 
113             filter = SkImageFilters::Blur(sigma, sigma, SkTileMode::kRepeat, nullptr);
114             draw_image(canvas, image[2], std::move(filter));
115             canvas->translate(image[2]->width() + 20, 0);
116 
117             canvas->restore();
118             canvas->translate(0, image[0]->height() + 20);
119         }
120     }
121 
122 private:
123     using INHERITED = GM;
124 };
125 
126 //////////////////////////////////////////////////////////////////////////////
127 
128 DEF_GM(return new ImageBlurRepeatModeGM;)
129 }  // namespace skiagm
130 
131 // See skbug.com/10145 for more context, but if the blur doesn't have its own crop rect and
132 // the canvas is not clipped, repeat can behave strangely (before fixes, this meant:
133 //  1. The filtered results became semi-transparent when they should have remained opaque.
134 //  2. The filtered results clip to 3xSigma, which makes sense for the decal tile mode, but not
135 //     the others.
136 //  3. The repeat filter interacts non-intuitively when an expanded clip rect intersects the draw
137 //     geometry (it repeats across the edges of the intersection instead of repeating across the
138 //     draw and then clipping)).
139 DEF_SIMPLE_GM(imageblurrepeatunclipped, canvas, 256, 128) {
140     // To show translucency
141     auto checkerboard = ToolUtils::create_checkerboard_image(256, 128, SK_ColorLTGRAY,
142                                                              SK_ColorGRAY, 8);
143     canvas->drawImage(checkerboard, 0, 0);
144 
145     // Make an image with one red and one blue band
146     SkBitmap bmp;
147     bmp.allocN32Pixels(100, 20);
148     bmp.eraseArea(SkIRect::MakeWH(100, 10), SK_ColorRED);
149     bmp.eraseArea(SkIRect::MakeXYWH(0, 10, 100, 10), SK_ColorBLUE);
150 
151     auto img = bmp.asImage();
152     auto filter = SkImageFilters::Blur(0, 10, SkTileMode::kRepeat, nullptr);
153     SkPaint paint;
154     paint.setImageFilter(std::move(filter));
155 
156     // Draw the blurred image once
157     canvas->translate(0, 50);
158     canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
159 
160     // Draw the blurred image with a clip positioned such that the draw would be excluded except
161     // that the image filter causes it to intersect with the clip. Ideally should look like the
162     // left image, but clipped to the debug-black rectangle (Narrator: it does not look like that).
163     canvas->translate(110, 0);
164     canvas->clipRect(SkRect::MakeXYWH(0, -30, 100, 10));
165     canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
166 
167     // Visualize the clip
168     SkPaint line;
169     line.setStyle(SkPaint::kStroke_Style);
170     canvas->drawRect(SkRect::MakeXYWH(0, -30, 99, 9), line);
171 }
172