Debian Codegraph @

gm/inversepaths.cpp

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