blob: 8b226def1754d871e8c9690f18c050078a35c112 [file] [log] [blame]
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Kevin Lubick1ef156b2022-11-29 12:07:14 -05009#include "include/core/SkBlurTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Michael Ludwig0b124c42022-02-15 12:29:58 -050011#include "include/core/SkMaskFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/effects/SkDashPathEffect.h"
Michael Ludwig0b124c42022-02-15 12:29:58 -050020#include "include/effects/SkImageFilters.h"
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000021
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include <utility>
23
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000024static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) {
Mike Reedb746d5c2020-08-02 18:56:34 -040025 return SkPath::Rect(SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w));
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000026}
27
28static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) {
Mike Reedb746d5c2020-08-02 18:56:34 -040029 return SkPath::Rect(SkRect::MakeXYWH(cx - l / 2, cy, l, 0));
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000030}
31
32static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) {
Mike Reedb746d5c2020-08-02 18:56:34 -040033 return SkPath::Circle(cx, cy, d/2, SkPathDirection::kCW);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000034}
35
36static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) {
Mike Reedb746d5c2020-08-02 18:56:34 -040037 return SkPath::Line({cx - l / 2, cy}, {cx + l / 2, cy});
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000038}
39
halcanary2a243382015-09-09 08:16:41 -070040namespace {
bsalomon687d9d22016-06-10 12:09:59 -070041struct Style {
42 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;
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000047};
bsalomon687d9d22016-06-10 12:09:59 -070048
49sk_sp<SkPathEffect> make_dash() {
mtkleindbfd7ab2016-09-01 11:24:54 -070050 constexpr SkScalar kIntervals[] = { 4.f, 3.f };
Herb Derbyc37b3862022-06-21 09:49:17 -040051 return SkDashPathEffect::Make(kIntervals, std::size(kIntervals), 0);
bsalomon687d9d22016-06-10 12:09:59 -070052}
53
54Style styles[] {
55 {SkPaint::kStroke_Style},
56 {SkPaint::kStrokeAndFill_Style},
57 {SkPaint::kFill_Style},
58 {SkPaint::kStroke_Style, make_dash()},
59};
60
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000061SkScalar pathSizes[] = {
62 40,
63 10,
64 0
65};
66SkScalar strokeWidths[] = {
67 10,
68 0
69};
Ben Wagner1a1d2412017-10-17 17:33:44 -040070SkPath (*paths[])(SkScalar, SkScalar, SkScalar) = {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000071 generate_square,
72 generate_rect_line,
73 generate_circle,
74 generate_line
75};
76
77const SkScalar slideWidth = 90, slideHeight = 90;
78const SkScalar slideBoundary = 5;
79
halcanary2a243382015-09-09 08:16:41 -070080} // namespace
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000081
bsalomon687d9d22016-06-10 12:09:59 -070082DEF_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;
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000087
bsalomon687d9d22016-06-10 12:09:59 -070088 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));
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000094
bsalomon687d9d22016-06-10 12:09:59 -070095 SkPaint outlinePaint;
96 outlinePaint.setColor(0x40000000);
97 outlinePaint.setStyle(SkPaint::kStroke_Style);
98 outlinePaint.setStrokeWidth(SkIntToScalar(0));
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000099
Herb Derbyc37b3862022-06-21 09:49:17 -0400100 for (size_t styleIndex = 0; styleIndex < std::size(styles);
bsalomon687d9d22016-06-10 12:09:59 -0700101 styleIndex++) {
Herb Derbyc37b3862022-06-21 09:49:17 -0400102 for (size_t sizeIndex = 0; sizeIndex < std::size(pathSizes);
bsalomon687d9d22016-06-10 12:09:59 -0700103 sizeIndex++) {
104 SkScalar size = pathSizes[sizeIndex];
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000105
bsalomon687d9d22016-06-10 12:09:59 -0700106 canvas->save();
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000107
bsalomon687d9d22016-06-10 12:09:59 -0700108 for (size_t widthIndex = 0;
Herb Derbyc37b3862022-06-21 09:49:17 -0400109 widthIndex < std::size(strokeWidths);
bsalomon687d9d22016-06-10 12:09:59 -0700110 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);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000116
bsalomon687d9d22016-06-10 12:09:59 -0700117 for (size_t pathIndex = 0;
Herb Derbyc37b3862022-06-21 09:49:17 -0400118 pathIndex < std::size(paths);
bsalomon687d9d22016-06-10 12:09:59 -0700119 pathIndex++) {
120 canvas->drawRect(clipRect, clipPaint);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000121
bsalomon687d9d22016-06-10 12:09:59 -0700122 canvas->save();
123 canvas->clipRect(clipRect);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000124
bsalomon687d9d22016-06-10 12:09:59 -0700125 SkPath path = paths[pathIndex](cx, cy, size);
Mike Reed7d34dc72019-11-26 12:17:17 -0500126 path.setFillType(SkPathFillType::kInverseWinding);
bsalomon687d9d22016-06-10 12:09:59 -0700127 canvas->drawPath(path, paint);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000128
Mike Reed7d34dc72019-11-26 12:17:17 -0500129 path.setFillType(SkPathFillType::kWinding);
bsalomon687d9d22016-06-10 12:09:59 -0700130 canvas->drawPath(path, outlinePaint);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000131
bsalomon687d9d22016-06-10 12:09:59 -0700132 canvas->restore();
133 canvas->translate(dx, 0);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000134 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000135 }
bsalomon687d9d22016-06-10 12:09:59 -0700136 canvas->restore();
137 canvas->translate(0, dy);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000138 }
bsalomon687d9d22016-06-10 12:09:59 -0700139 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000140}
Michael Ludwig0b124c42022-02-15 12:29:58 -0500141
142DEF_SIMPLE_GM(inverse_fill_filters, canvas, 384, 128) {
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
173DEF_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}