Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
backdrop_imagefilter_croprect.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 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"
13
15
16static void draw_backdrop_filter_gm(SkCanvas* canvas, float outsetX, float outsetY,
17 FilterFactory factory) {
18 // CTM translates to (150, 150)
19 SkPoint origin = SkPoint::Make(150.f, 150.f);
20 // The save layer specified after the CTM has negative coordinates, but
21 // means that (100, 100) to (500, 250) in device-space will be saved
22 SkRect clip = SkRect::MakeXYWH(-50.f, -50.f, 400.f, 150.f);
23 // The image-filter crop relative to the CTM, which will map to
24 // (200, 160) to (400, 190) in device space, or (100, 600) to (300, 90) in
25 // the layer's image space.
26 SkRect cropInLocal = SkRect::MakeLTRB(50.f, 10.f, 250.f, 40.f);
27
28 SkIRect cropRect = cropInLocal.makeOutset(outsetX, outsetY).roundOut();
29 sk_sp<SkImageFilter> imageFilter = factory(&cropRect);
30
31 SkPaint p;
32 for (int i = 0; i < 2; ++i) {
33 canvas->save();
34 canvas->translate(origin.fX, origin.fY);
35
36 canvas->clipRect(clip);
37
38 if (i == 0) {
39 // Primary save layer mode, so save layer before drawing the content
40 SkPaint imfPaint;
41 imfPaint.setImageFilter(imageFilter);
42 canvas->saveLayer(nullptr, &imfPaint);
43 } // else backdrop mode, so the content is drawn first
44
45 // Fill the clip with one color (cyan for i == 0 (inverse = red), and
46 // magenta for i == 1 (inverse = green))
47 p.setColor(i == 0 ? SK_ColorCYAN : SK_ColorMAGENTA);
48 canvas->drawPaint(p);
49
50 // Then an inner rectangle with a color meant to be inverted by the image filter
51 p.setColor(i == 0 ? SK_ColorRED : SK_ColorGREEN);
52 canvas->drawRect(cropInLocal, p);
53
54 if (i == 1) {
55 // Backdrop mode, so save a layer using the image filter as the backdrop to filter
56 // content on initialization.
57 canvas->saveLayer(SkCanvas::SaveLayerRec(nullptr, nullptr, imageFilter.get(),
59 }
60
61 // Restore the saved layer (either a main layer that was just drawn into and needs to be
62 // filtered, or an "empty" layer initialized with the previously filtered backdrop)
63 canvas->restore();
64
65 // Move down
66 canvas->restore();
67 origin.fY += 150.f;
68 }
69}
70
72 static const float matrix[20] = {-1.f, 0.f, 0.f, 0.f, 1.f,
73 0.f, -1.f, 0.f, 0.f, 1.f,
74 0.f, 0.f, -1.f, 0.f, 1.f,
75 0.f, 0.f, 0.f, 1.f, 0.f};
76 return SkImageFilters::ColorFilter(SkColorFilters::Matrix(matrix), nullptr, crop);
77}
78
80 // Use different sigmas for x and y so rotated CTM is apparent
81 return SkImageFilters::Blur(16.f, 4.f, nullptr, crop);
82}
83
84// This draws correctly if there's a small cyan rectangle above a much larger magenta rectangle.
85// There should be no red around the cyan rectangle and no green within the magenta rectangle.
86DEF_SIMPLE_GM(backdrop_imagefilter_croprect, canvas, 600, 500) {
88}
89
90// This draws correctly if there's a blurred red rectangle inside a cyan rectangle, above a blurred
91// green rectangle inside a larger magenta rectangle. All rectangles and the blur direction are
92// consistently rotated.
93DEF_SIMPLE_GM(backdrop_imagefilter_croprect_rotated, canvas, 600, 500) {
94 canvas->translate(140.f, -180.f);
95 canvas->rotate(30.f);
96 draw_backdrop_filter_gm(canvas, 32.f, 32.f, make_blur_filter);
97}
98
99// This draws correctly if there's a blurred red rectangle inside a cyan rectangle, above a blurred
100// green rectangle inside a larger magenta rectangle. All rectangles and the blur direction are
101// under consistent perspective.
102// NOTE: Currently renders incorrectly, see skbug.com/9074
103DEF_SIMPLE_GM(backdrop_imagefilter_croprect_persp, canvas, 600, 500) {
104 SkMatrix persp = SkMatrix::I();
105 persp.setPerspY(0.001f);
106 persp.setSkewX(8.f / 25.f);
107 canvas->concat(persp);
108 draw_backdrop_filter_gm(canvas, 32.f, 32.f, make_blur_filter);
109}
110
111// This draws correctly if there's a small cyan rectangle above a much larger magenta rectangle.
112// There should be no red around the cyan rectangle and no green within the magenta rectangle, and
113// everything should be 50% transparent.
114DEF_SIMPLE_GM(backdrop_imagefilter_croprect_nested, canvas, 600, 500) {
115 SkPaint p;
116 p.setAlphaf(0.5f);
117 // This ensures there is a non-root device on the stack with a non-zero origin.
118 canvas->translate(15.f, 10.f);
119 canvas->clipRect(SkRect::MakeWH(600.f, 500.f));
120
121 canvas->saveLayer(nullptr, &p);
123 canvas->restore();
124}
constexpr SkColor SK_ColorMAGENTA
Definition SkColor.h:147
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
sk_sp< SkImageFilter >(* FilterFactory)(const SkIRect *crop)
static sk_sp< SkImageFilter > make_invert_filter(const SkIRect *crop)
static sk_sp< SkImageFilter > make_blur_filter(const SkIRect *crop)
static void draw_backdrop_filter_gm(SkCanvas *canvas, float outsetX, float outsetY, FilterFactory factory)
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
void drawRect(const SkRect &rect, 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 drawPaint(const SkPaint &paint)
@ kInitWithPrevious_SaveLayerFlag
initializes with previous contents
Definition SkCanvas.h:672
int save()
Definition SkCanvas.cpp:451
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, 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={})
SkMatrix & setSkewX(SkScalar v)
Definition SkMatrix.h:518
SkMatrix & setPerspY(SkScalar v)
Definition SkMatrix.h:544
static const SkMatrix & I()
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
T * get() const
Definition SkRefCnt.h:303
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
float fX
x-axis value
static constexpr SkPoint Make(float x, float y)
float fY
y-axis value
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
void roundOut(SkIRect *dst) const
Definition SkRect.h:1241
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646