Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
imageblurrepeatmode.cpp
Go to the documentation of this file.
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"
16#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
23#include "tools/ToolUtils.h"
24
25#include <initializer_list>
26#include <utility>
27
28static sk_sp<SkImage> make_image(SkCanvas* canvas, int direction) {
30 auto surface = ToolUtils::makeSurface(canvas, info);
31 SkCanvas* c = surface->getCanvas();
33 paint.setAntiAlias(true);
34
35 const SkColor colors[] = {
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
64static void draw_image(SkCanvas* canvas, const sk_sp<SkImage> image, sk_sp<SkImageFilter> filter) {
65 SkAutoCanvasRestore acr(canvas, true);
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
74namespace skiagm {
75
76// This GM draws a colorful grids with different blur settings.
77class ImageBlurRepeatModeGM : public GM {
78public:
80 this->setBGColor(0xFFCCCCCC);
81 }
82
83protected:
84 SkString getName() const override { return SkString("imageblurrepeatmode"); }
85
86 SkISize getISize() override { return SkISize::Make(850, 920); }
87
88 bool runAsBench() const override { return true; }
89
90 void onDraw(SkCanvas* canvas) override {
92 { make_image(canvas, 1), make_image(canvas, 2), make_image(canvas, 3) };
94
95 canvas->translate(0, 30);
96 // Test different kernel size, including the one to launch 2d Gaussian
97 // blur.
98 for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) {
99 // FIXME crops
100 canvas->save();
101 filter = SkImageFilters::Blur(
102 sigma, 0.0f, SkTileMode::kRepeat, nullptr, image[0]->bounds());
103 draw_image(canvas, image[0], std::move(filter));
104 canvas->translate(image[0]->width() + 20, 0);
105
106 filter = SkImageFilters::Blur(
107 0.0f, sigma, SkTileMode::kRepeat, nullptr, image[1]->bounds());
108 draw_image(canvas, image[1], std::move(filter));
109 canvas->translate(image[1]->width() + 20, 0);
110
111 filter = SkImageFilters::Blur(
112 sigma, sigma, SkTileMode::kRepeat, nullptr, image[2]->bounds());
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
121private:
122 using INHERITED = GM;
123};
124
125//////////////////////////////////////////////////////////////////////////////
126
127DEF_GM(return new ImageBlurRepeatModeGM;)
128} // namespace skiagm
129
130// See skbug.com/10145 for more context, but if the blur doesn't have its own crop rect and
131// the canvas is not clipped, repeat can behave strangely (before fixes, this meant:
132// 1. The filtered results became semi-transparent when they should have remained opaque.
133// 2. The filtered results clip to 3xSigma, which makes sense for the decal tile mode, but not
134// the others.
135// 3. The repeat filter interacts non-intuitively when an expanded clip rect intersects the draw
136// geometry (it repeats across the edges of the intersection instead of repeating across the
137// draw and then clipping)).
138DEF_SIMPLE_GM(imageblurrepeatunclipped, canvas, 256, 128) {
139 // To show translucency
141 SK_ColorGRAY, 8);
142 canvas->drawImage(checkerboard, 0, 0);
143
144 // Make an image with one red and one blue band
145 SkBitmap bmp;
146 bmp.allocN32Pixels(100, 20);
148 bmp.eraseArea(SkIRect::MakeXYWH(0, 10, 100, 10), SK_ColorBLUE);
149
150 auto img = bmp.asImage();
151 // The blur filter uses a repeat crop applied to the image bounds to define the tiling geometry,
152 // but the crop IF is created directly since the tilemode factory for ::Blur also adds a kDecal
153 // post-crop that is undesired for this GM.
154 auto filter = SkImageFilters::Blur(0, 10,
155 SkImageFilters::Crop(SkRect::Make(img->bounds()), SkTileMode::kRepeat, nullptr));
157 paint.setImageFilter(std::move(filter));
158
159 // Draw the blurred image once with a clip that shows the repeat is tiled several times.
160 // 3xsigma is used to match the historic, but underspecified behavior for when kRepeat was used
161 // with no crop rect (which must now be provided or kRepeat is ignored).
162 canvas->translate(0, 50);
163 canvas->save();
164 canvas->clipIRect(img->bounds().makeOutset(0, 30));
165 canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
166 canvas->restore();
167
168 // Draw the blurred image with a clip positioned such that the draw would be excluded except
169 // that the image filter causes it to intersect with the clip. It should look like the
170 // left image, but clipped to the debug-black rectangle.
171 canvas->translate(110, 0);
172 canvas->save();
173 canvas->clipIRect(SkIRect::MakeXYWH(0, -30, 100, 10));
174 canvas->drawImage(img, 0, 0, SkSamplingOptions(), &paint);
175 canvas->restore();
176
177 // Visualize the clip
178 SkPaint line;
179 line.setStyle(SkPaint::kStroke_Style);
180 canvas->drawRect(SkRect::MakeXYWH(0, -30, 99, 9), line);
181}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
static void draw_image(SkCanvas *canvas, SkImage *img)
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
constexpr SkColor SK_ColorLTGRAY
Definition SkColor.h:118
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
#define SkIntToScalar(x)
Definition SkScalar.h:57
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
void eraseArea(const SkIRect &area, SkColor c) const
Definition SkBitmap.h:854
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void clipIRect(const SkIRect &irect, SkClipOp op=SkClipOp::kIntersect)
Definition SkCanvas.h:991
int save()
Definition SkCanvas.cpp:451
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Crop(const SkRect &rect, SkTileMode tileMode, sk_sp< SkImageFilter > input)
SkIRect bounds() const
Definition SkImage.h:303
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
SkScalar width()
Definition gm.h:159
SkScalar height()
Definition gm.h:162
void setBGColor(SkColor)
Definition gm.cpp:159
void onDraw(SkCanvas *canvas) override
SkString getName() const override
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
#define DEF_GM(CODE)
Definition gm.h:40
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
static void draw_image(SkCanvas *canvas, const sk_sp< SkImage > image, sk_sp< SkImageFilter > filter)
static sk_sp< SkImage > make_image()
Definition mipmap.cpp:21
double y
double x
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
sk_sp< SkImage > create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize)
int32_t width
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition SkRect.h:56
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static void checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)