Debian Codegraph @

gm/backdrop_imagefilter_croprect.cpp

raw
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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColorFilter.h"
11 #include "include/core/SkPaint.h"
12 #include "include/effects/SkImageFilters.h"
13
14 typedef sk_sp<SkImageFilter> (*FilterFactory)(const SkIRect* crop);
15
16 static 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(),
58 SkCanvas::kInitWithPrevious_SaveLayerFlag));
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
71 static sk_sp<SkImageFilter> make_invert_filter(const SkIRect* crop) {
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
79 static sk_sp<SkImageFilter> make_blur_filter(const SkIRect* crop) {
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.
86 DEF_SIMPLE_GM(backdrop_imagefilter_croprect, canvas, 600, 500) {
87 draw_backdrop_filter_gm(canvas, 0.f, 0.f, make_invert_filter);
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.
93 DEF_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/40040358
103 DEF_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.
114 DEF_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);
122 draw_backdrop_filter_gm(canvas, 0.f, 0.f, make_invert_filter);
123 canvas->restore();
124 }
125
126 DEF_SIMPLE_GM(backdrop_layer_tilemode, canvas, 512, 128) {
127 auto drawBackdropTileMode = [canvas](SkTileMode backdropTileMode) {
128 canvas->save();
129 // Restrict the canvas before starting a new layer to control its size.
130 canvas->clipRect(SkRect::MakeIWH(128, 128));
131 // This layer will be the backdrop content, but without any additional effects, its size
132 // will match the clip (128x128).
133 canvas->saveLayer(nullptr, nullptr);
134 // Fill the layer with high frequency content (stripes of red and white)
135 for (int y = 0; y < 128; y += 8) {
136 SkPaint fill;
137 fill.setColor(y % 16 ? SK_ColorRED : SK_ColorWHITE);
138 canvas->drawRect(SkRect::MakeXYWH(0, y, 128, 8), fill);
139 }
140 // Perform a backdrop blur layer with the specified backdrop tile mode
141 sk_sp<SkImageFilter> blur = SkImageFilters::Blur(32.f, 32.f, nullptr);
142 canvas->saveLayer({nullptr, nullptr, blur.get(), backdropTileMode, nullptr, 0});
143 canvas->restore();
144 canvas->restore();
145 canvas->restore();
146
147 canvas->translate(128, 0);
148 };
149
150 drawBackdropTileMode(SkTileMode::kClamp);
151 drawBackdropTileMode(SkTileMode::kDecal);
152 drawBackdropTileMode(SkTileMode::kRepeat);
153 drawBackdropTileMode(SkTileMode::kMirror);
154 }