1 /*
2 * Copyright 2013 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/SkBlurTypes.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkMaskFilter.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPathEffect.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkTypes.h"
19 #include "include/effects/SkDashPathEffect.h"
20 #include "include/effects/SkImageFilters.h"
21
22 #include <utility>
23
generate_square(SkScalar cx,SkScalar cy,SkScalar w)24 static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) {
25 return SkPath::Rect(SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w));
26 }
27
generate_rect_line(SkScalar cx,SkScalar cy,SkScalar l)28 static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) {
29 return SkPath::Rect(SkRect::MakeXYWH(cx - l / 2, cy, l, 0));
30 }
31
generate_circle(SkScalar cx,SkScalar cy,SkScalar d)32 static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) {
33 return SkPath::Circle(cx, cy, d/2, SkPathDirection::kCW);
34 }
35
generate_line(SkScalar cx,SkScalar cy,SkScalar l)36 static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) {
37 return SkPath::Line({cx - l / 2, cy}, {cx + l / 2, cy});
38 }
39
40 namespace {
41 struct Style {
Style__anon4e0d744e0111::Style42 Style(SkPaint::Style paintStyle, sk_sp<SkPathEffect> pe = sk_sp<SkPathEffect>())
43 : fPaintStyle(paintStyle)
44 , fPathEffect(std::move(pe)) {}
45 SkPaint::Style fPaintStyle;
46 sk_sp<SkPathEffect> fPathEffect;
47 };
48
make_dash()49 sk_sp<SkPathEffect> make_dash() {
50 constexpr SkScalar kIntervals[] = { 4.f, 3.f };
51 return SkDashPathEffect::Make(kIntervals, std::size(kIntervals), 0);
52 }
53
54 Style styles[] {
55 {SkPaint::kStroke_Style},
56 {SkPaint::kStrokeAndFill_Style},
57 {SkPaint::kFill_Style},
58 {SkPaint::kStroke_Style, make_dash()},
59 };
60
61 SkScalar pathSizes[] = {
62 40,
63 10,
64 0
65 };
66 SkScalar strokeWidths[] = {
67 10,
68 0
69 };
70 SkPath (*paths[])(SkScalar, SkScalar, SkScalar) = {
71 generate_square,
72 generate_rect_line,
73 generate_circle,
74 generate_line
75 };
76
77 const SkScalar slideWidth = 90, slideHeight = 90;
78 const SkScalar slideBoundary = 5;
79
80 } // namespace
81
82 DEF_SIMPLE_GM(inverse_paths, canvas, 800, 1200) {
83 SkScalar cx = slideWidth / 2 + slideBoundary;
84 SkScalar cy = slideHeight / 2 + slideBoundary;
85 SkScalar dx = slideWidth + 2 * slideBoundary;
86 SkScalar dy = slideHeight + 2 * slideBoundary;
87
88 SkRect clipRect = SkRect::MakeLTRB(slideBoundary, slideBoundary,
89 slideBoundary + slideWidth,
90 slideBoundary + slideHeight);
91 SkPaint clipPaint;
92 clipPaint.setStyle(SkPaint::kStroke_Style);
93 clipPaint.setStrokeWidth(SkIntToScalar(2));
94
95 SkPaint outlinePaint;
96 outlinePaint.setColor(0x40000000);
97 outlinePaint.setStyle(SkPaint::kStroke_Style);
98 outlinePaint.setStrokeWidth(SkIntToScalar(0));
99
100 for (size_t styleIndex = 0; styleIndex < std::size(styles);
101 styleIndex++) {
102 for (size_t sizeIndex = 0; sizeIndex < std::size(pathSizes);
103 sizeIndex++) {
104 SkScalar size = pathSizes[sizeIndex];
105
106 canvas->save();
107
108 for (size_t widthIndex = 0;
109 widthIndex < std::size(strokeWidths);
110 widthIndex++) {
111 SkPaint paint;
112 paint.setColor(0xff007000);
113 paint.setStrokeWidth(strokeWidths[widthIndex]);
114 paint.setStyle(styles[styleIndex].fPaintStyle);
115 paint.setPathEffect(styles[styleIndex].fPathEffect);
116
117 for (size_t pathIndex = 0;
118 pathIndex < std::size(paths);
119 pathIndex++) {
120 canvas->drawRect(clipRect, clipPaint);
121
122 canvas->save();
123 canvas->clipRect(clipRect);
124
125 SkPath path = paths[pathIndex](cx, cy, size);
126 path.setFillType(SkPathFillType::kInverseWinding);
127 canvas->drawPath(path, paint);
128
129 path.setFillType(SkPathFillType::kWinding);
130 canvas->drawPath(path, outlinePaint);
131
132 canvas->restore();
133 canvas->translate(dx, 0);
134 }
135 }
136 canvas->restore();
137 canvas->translate(0, dy);
138 }
139 }
140 }
141
142 DEF_SIMPLE_GM(inverse_fill_filters, canvas, 384, 128) {
__anon4e0d744e0202(const SkPaint& paint) 143 auto draw = [canvas](const SkPaint& paint) {
144 SkPath path = SkPath::Circle(65.f, 65.f, 30.f);
145 path.setFillType(SkPathFillType::kInverseWinding);
146
147 canvas->save();
148 canvas->clipRect({0, 0, 128, 128});
149 canvas->drawPath(path, paint);
150 canvas->restore();
151
152 SkPaint stroke;
153 stroke.setStyle(SkPaint::kStroke_Style);
154 stroke.setColor(SK_ColorWHITE);
155 canvas->drawRect({0, 0, 128, 128}, stroke);
156 };
157
158 SkPaint paint;
159 paint.setAntiAlias(true);
160
161 draw(paint);
162
163 canvas->translate(128, 0);
164 paint.setImageFilter(SkImageFilters::Blur(5.f, 5.f, nullptr));
165 draw(paint);
166
167 canvas->translate(128, 0);
168 paint.setImageFilter(nullptr);
169 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 5));
170 draw(paint);
171 }
172
173 DEF_SIMPLE_GM(inverse_windingmode_filters, canvas, 256, 100) {
174 SkPath path;
175 path.addRect({10, 10, 30, 30}, SkPathDirection::kCW);
176 path.addRect({20, 20, 40, 40}, SkPathDirection::kCW);
177 path.addRect({10, 60, 30, 80}, SkPathDirection::kCW);
178 path.addRect({20, 70, 40, 90}, SkPathDirection::kCCW);
179 SkPaint strokePaint;
180 strokePaint.setStyle(SkPaint::kStroke_Style);
181 SkRect clipRect = {0, 0, 51, 99};
182 canvas->drawPath(path, strokePaint);
183 SkPaint fillPaint;
184 fillPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 1.0f));
185 for (auto fillType : { SkPathFillType::kWinding,
186 SkPathFillType::kEvenOdd,
187 SkPathFillType::kInverseWinding,
188 SkPathFillType::kInverseEvenOdd } ) {
189 canvas->translate(51, 0);
190 canvas->save();
191 canvas->clipRect(clipRect);
192 path.setFillType(fillType);
193 canvas->drawPath(path, fillPaint);
194 canvas->restore();
195 SkPaint clipPaint;
196 clipPaint.setColor(SK_ColorRED);
197 clipPaint.setStyle(SkPaint::kStroke_Style);
198 clipPaint.setStrokeWidth(1.f);
199 canvas->drawRect(clipRect, clipPaint);
200 }
201 }
202