Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
imagefilters.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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"
20#include "include/core/SkRect.h"
33#include "tools/DecodeUtils.h"
34#include "tools/Resources.h"
35#include "tools/ToolUtils.h"
36
37#include <utility>
38
39/**
40 * Test drawing a primitive w/ an imagefilter (in this case, just matrix w/ identity) to see
41 * that we apply the xfermode *after* the image has been created and filtered, and not during
42 * the creation step (i.e. before it is filtered).
43 *
44 * see https://bug.skia.org/3741
45 */
46static void do_draw(SkCanvas* canvas, SkBlendMode mode, sk_sp<SkImageFilter> imf) {
47 SkAutoCanvasRestore acr(canvas, true);
48 canvas->clipRect(SkRect::MakeWH(220, 220));
49
50 // want to force a layer, so modes like DstIn can combine meaningfully, but the final
51 // image can still be shown against our default (opaque) background. non-opaque GMs
52 // are a lot more trouble to compare/triage.
53 canvas->saveLayer(nullptr, nullptr);
54 canvas->drawColor(SK_ColorGREEN);
55
57 paint.setAntiAlias(true);
58
59 SkRect r0 = SkRect::MakeXYWH(10, 60, 200, 100);
60 SkRect r1 = SkRect::MakeXYWH(60, 10, 100, 200);
61
62 paint.setColor(SK_ColorRED);
63 canvas->drawOval(r0, paint);
64
65 paint.setColor(0x660000FF);
66 paint.setImageFilter(std::move(imf));
67 paint.setBlendMode(mode);
68 canvas->drawOval(r1, paint);
69}
70
71DEF_SIMPLE_GM(imagefilters_xfermodes, canvas, 480, 480) {
72 canvas->translate(10, 10);
73
74 // just need an imagefilter to trigger the code-path (which creates a tmp layer)
77 nullptr));
78
79 const SkBlendMode modes[] = {
81 };
82
83 for (size_t i = 0; i < std::size(modes); ++i) {
84 canvas->save();
85 do_draw(canvas, modes[i], nullptr);
86 canvas->translate(240, 0);
87 do_draw(canvas, modes[i], imf);
88 canvas->restore();
89
90 canvas->translate(0, 240);
91 }
92}
93
96 auto surface(ToolUtils::makeSurface(canvas, info));
97 surface->getCanvas()->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), SkPaint());
98 return surface->makeImageSnapshot();
99}
100
101// Compare blurs when we're tightly clipped (fast) and not as tightly (slower)
102//
103// Expect the two to draw the same (modulo the extra border of pixels when the clip is larger)
104//
105DEF_SIMPLE_GM(fast_slow_blurimagefilter, canvas, 620, 260) {
107 const SkRect r = SkRect::MakeIWH(image->width(), image->height());
108
109 canvas->translate(10, 10);
110 for (SkScalar sigma = 8; sigma <= 128; sigma *= 2) {
112 paint.setImageFilter(SkImageFilters::Blur(sigma, sigma, nullptr));
113
114 canvas->save();
115 // we outset the clip by 1, to fall out of the fast-case in drawImage
116 // i.e. the clip is larger than the image
117 for (SkScalar outset = 0; outset <= 1; ++outset) {
118 canvas->save();
119 canvas->clipRect(r.makeOutset(outset, outset));
120 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
121 canvas->restore();
122 canvas->translate(0, r.height() + 20);
123 }
124 canvas->restore();
125 canvas->translate(r.width() + 20, 0);
126 }
127}
128
129///////////////////////////////////////////////////////////////////////////////////////////////////
130
131static void draw_set(SkCanvas* canvas, sk_sp<SkImageFilter> filters[], int count) {
132 const SkRect r = SkRect::MakeXYWH(30, 30, 200, 200);
133 const SkScalar offset = 250;
134 SkScalar dx = 0, dy = 0;
135
136 for (int i = 0; i < count; ++i) {
137 canvas->save();
138 SkRRect rr = SkRRect::MakeRectXY(r.makeOffset(dx, dy), 20, 20);
139 canvas->clipRRect(rr, true);
140 canvas->saveLayer(SkCanvas::SaveLayerRec(&rr.getBounds(), nullptr, filters[i].get(), 0));
141 canvas->drawColor(0x40FFFFFF);
142 canvas->restore();
143 canvas->restore();
144
145 if (0 == dx) {
146 dx = offset;
147 } else {
148 dx = 0;
149 dy = offset;
150 }
151 }
152}
153
155protected:
156 bool runAsBench() const override { return true; }
157 SkString getName() const override { return SkString("savelayer_with_backdrop"); }
158 SkISize getISize() override { return SkISize::Make(830, 550); }
159
160 void onDraw(SkCanvas* canvas) override {
161 SkColorMatrix cm;
162 cm.setSaturation(10);
164 const SkScalar kernel[] = { 4, 0, 4, 0, -15, 0, 4, 0, 4 };
165 sk_sp<SkImageFilter> filters[] = {
166 SkImageFilters::Blur(10, 10, nullptr),
167 SkImageFilters::Dilate(8, 8, nullptr),
168 SkImageFilters::MatrixConvolution({ 3, 3 }, kernel, 1, 0, { 0, 0 },
169 SkTileMode::kDecal, true, nullptr),
170 SkImageFilters::ColorFilter(std::move(cf), nullptr),
171 };
172
173 const struct {
174 SkScalar fSx, fSy, fTx, fTy;
175 } xforms[] = {
176 { 1, 1, 0, 0 },
177 { 0.5f, 0.5f, 530, 0 },
178 { 0.25f, 0.25f, 530, 275 },
179 { 0.125f, 0.125f, 530, 420 },
180 };
181
184 sk_sp<SkImage> image(ToolUtils::GetResourceAsImage("images/mandrill_512.png"));
185
186 canvas->translate(20, 20);
187 for (const auto& xform : xforms) {
188 canvas->save();
189 canvas->translate(xform.fTx, xform.fTy);
190 canvas->scale(xform.fSx, xform.fSy);
191 canvas->drawImage(image, 0, 0, sampling, nullptr);
192 draw_set(canvas, filters, std::size(filters));
193 canvas->restore();
194 }
195 }
196};
197
198DEF_GM(return new SaveLayerWithBackdropGM();)
199
200///////////////////////////////////////////////////////////////////////////////////////////////////
201
202// Test that color filters and mask filters are applied before the image filter, even if it would
203// normally be a sprite draw that could avoid an auto-saveLayer.
204DEF_SIMPLE_GM(imagefilters_effect_order, canvas, 512, 512) {
205 sk_sp<SkImage> image(ToolUtils::GetResourceAsImage("images/mandrill_256.png"));
206 auto direct = GrAsDirectContext(canvas->recordingContext());
207 if (direct) {
208 if (sk_sp<SkImage> gpuImage = SkImages::TextureFromImage(direct, image)) {
209 image = std::move(gpuImage);
210 }
211 }
212
213 SkISize kernelSize = SkISize::Make(3, 3);
214 SkIPoint kernelOffset = SkIPoint::Make(1, 1);
215 // A Laplacian edge detector, ie https://en.wikipedia.org/wiki/Kernel_(image_processing)
216 SkScalar kernel[9] = {-1.f, -1.f, -1.f,
217 -1.f, 8.f, -1.f,
218 -1.f, -1.f, -1.f};
219 auto edgeDetector = SkImageFilters::MatrixConvolution(
220 kernelSize, kernel, 1.f, 0.f, kernelOffset, SkTileMode::kClamp, false, nullptr);
221 // This uses the high contrast filter because it resembles a pre-processing step you may perform
222 // prior to edge detection. The specifics of the high contrast algorithm don't matter for the GM
223 auto edgeAmplify = SkHighContrastFilter::Make(
224 {false, SkHighContrastConfig::InvertStyle::kNoInvert, 0.5f});
225
226 SkPaint testCFPaint;
227 testCFPaint.setColorFilter(edgeAmplify);
228 testCFPaint.setImageFilter(edgeDetector);
229
230 // The expected result is color filter then image filter, so represent this explicitly in the
231 // image filter graph.
232 SkPaint expectedCFPaint;
233 expectedCFPaint.setImageFilter(SkImageFilters::Compose(edgeDetector,
234 SkImageFilters::ColorFilter(edgeAmplify, nullptr)));
235
236 // Draw the image twice (expected on the left, test on the right that should match)
237 SkRect crop = SkRect::Make(image->bounds());
238 canvas->save();
239 canvas->clipRect(crop);
240 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &expectedCFPaint); // Filter applied by draw's SkPaint
241 canvas->restore();
242
243 canvas->save();
244 canvas->translate(image->width(), 0);
245 canvas->clipRect(crop);
246 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &testCFPaint);
247 canvas->restore();
248
249 // Now test mask filters. These should be run before the image filter, and thus have the same
250 // effect as multiplying by an alpha mask.
251
252 // This mask filter pokes a hole in the center of the image
253 static constexpr SkColor kAlphas[] = { SK_ColorBLACK, SK_ColorTRANSPARENT };
254 static constexpr SkScalar kPos[] = { 0.4f, 0.9f };
256 {128.f, 128.f}, 128.f, kAlphas, kPos, 2, SkTileMode::kClamp);
257 sk_sp<SkMaskFilter> maskFilter = SkShaderMaskFilter::Make(alphaMaskShader);
258
259 // If edge detector sees the mask filter, it'll have alpha and then blend with the original
260 // image; otherwise the mask filter will apply late (incorrectly) and none of the original
261 // image will be visible.
264
265 SkPaint testMaskPaint;
266 testMaskPaint.setMaskFilter(maskFilter);
267 testMaskPaint.setImageFilter(edgeBlend);
268
269 SkPaint expectedMaskPaint;
270 expectedMaskPaint.setImageFilter(SkImageFilters::Compose(edgeBlend,
272 SkImageFilters::Shader(alphaMaskShader))));
273
274 canvas->save();
275 canvas->translate(0, image->height());
276 canvas->clipRect(crop);
277 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &expectedMaskPaint);
278 canvas->restore();
279
280 canvas->save();
281 canvas->translate(image->width(), image->height());
282 canvas->clipRect(crop);
283 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &testMaskPaint);
284 canvas->restore();
285}
286
287DEF_SIMPLE_GM(multiple_filters, canvas, 415, 210) {
289 canvas->translate(5, 5);
290
291 auto drawFilteredLayer = [=](SkCanvas::FilterSpan filters) {
292 SkPaint restorePaint;
293 restorePaint.setAlphaf(0.5f);
295 /*bounds=*/nullptr,
296 &restorePaint,
297 /*backdrop=*/nullptr,
298 /*backdropScale=*/1,
299 /*saveLayerFlags=*/0,
300 filters);
301 canvas->save();
302 canvas->clipRect({0, 0, 200, 200});
303 canvas->saveLayer(rec);
304
307 paint.setStrokeWidth(20);
308 paint.setColor(SK_ColorGREEN);
309 canvas->drawCircle(100, 100, 70, paint);
310
311 canvas->restore();
312 canvas->restore();
313 canvas->translate(205, 0);
314 };
315
316 {
317 // Test with two non-null filters that each change bounds in a different way:
318 sk_sp<SkImageFilter> filters[2] = {SkImageFilters::Dilate(5, 5, nullptr),
319 SkImageFilters::Erode(5, 5, nullptr)};
320 drawFilteredLayer(filters);
321 }
322
323 {
324 // Test with one null filter, to more closely mimic the canvas2D layers use-case:
325 sk_sp<SkImageFilter> filters[2] = {
326 SkImageFilters::DropShadowOnly(7, 7, 5, 5, SK_ColorBLUE, nullptr), nullptr};
327 drawFilteredLayer(filters);
328 }
329}
static const int outset
Definition BlurTest.cpp:58
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
int count
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
SkBlendMode
Definition SkBlendMode.h:38
@ kDstIn
r = d * sa
@ kSrcOver
r = s + (1-sa)*d
@ kSrcATop
r = s*da + d*(1-sa)
@ kSrcIn
r = s * da
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
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
bool runAsBench() const override
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
SkString getName() const override
static SkCanvas::SaveLayerRec ScaledBackdropLayer(const SkRect *bounds, const SkPaint *paint, const SkImageFilter *backdrop, SkScalar backdropScale, SkCanvas::SaveLayerFlags saveLayerFlags, SkCanvas::FilterSpan filters={})
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
void drawOval(const SkRect &oval, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
int save()
Definition SkCanvas.cpp:451
void scale(SkScalar sx, SkScalar sy)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
void clipRRect(const SkRRect &rrect, SkClipOp op, bool doAntiAlias)
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
void setSaturation(float sat)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkImageFilter > MatrixConvolution(const SkISize &kernelSize, const SkScalar kernel[], SkScalar gain, SkScalar bias, const SkIPoint &kernelOffset, SkTileMode tileMode, bool convolveAlpha, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Erode(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > DropShadowOnly(SkScalar dx, SkScalar dy, SkScalar sigmaX, SkScalar sigmaY, SkColor color, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Compose(sk_sp< SkImageFilter > outer, sk_sp< SkImageFilter > inner)
static sk_sp< SkImageFilter > Image(sk_sp< SkImage > image, const SkRect &srcRect, const SkRect &dstRect, const SkSamplingOptions &sampling)
static sk_sp< SkImageFilter > MatrixTransform(const SkMatrix &matrix, const SkSamplingOptions &sampling, sk_sp< SkImageFilter > input)
static sk_sp< SkImageFilter > Blend(SkBlendMode mode, sk_sp< SkImageFilter > background, sk_sp< SkImageFilter > foreground=nullptr, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Shader(sk_sp< SkShader > shader, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Dilate(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
int width() const
Definition SkImage.h:285
int height() const
Definition SkImage.h:291
SkIRect bounds() const
Definition SkImage.h:303
static const SkMatrix & I()
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
void setMaskFilter(sk_sp< SkMaskFilter > maskFilter)
void setColorFilter(sk_sp< SkColorFilter > colorFilter)
void setAlphaf(float a)
Definition SkPaint.cpp:130
static SkRRect MakeRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Definition SkRRect.h:180
const SkRect & getBounds() const
Definition SkRRect.h:279
static sk_sp< SkMaskFilter > Make(sk_sp< SkShader > shader)
T * get() const
Definition SkRefCnt.h:303
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
static void do_draw(SkCanvas *canvas, SkBlendMode mode, sk_sp< SkImageFilter > imf)
static void draw_set(SkCanvas *canvas, sk_sp< SkImageFilter > filters[], int count)
static sk_sp< SkImage > make_image()
Definition mipmap.cpp:21
SK_API sk_sp< SkImage > TextureFromImage(GrDirectContext *, const SkImage *, skgpu::Mipmapped=skgpu::Mipmapped::kNo, skgpu::Budgeted=skgpu::Budgeted::kYes)
void draw_checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
Point offset
static sk_sp< SkColorFilter > Make(const SkHighContrastConfig &config)
static constexpr SkIPoint Make(int32_t x, int32_t y)
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo MakeS32(int width, int height, SkAlphaType at)
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
constexpr SkRect makeOffset(float dx, float dy) const
Definition SkRect.h:965
static SkRect MakeIWH(int w, int h)
Definition SkRect.h:623
SkRect makeOutset(float dx, float dy) const
Definition SkRect.h:1002
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
constexpr float height() const
Definition SkRect.h:769
constexpr float width() const
Definition SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609