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