bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | */ |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkMatrix.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkPaint.h" |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 12 | #include "include/core/SkPathBuilder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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/SkSize.h" |
| 18 | #include "include/core/SkString.h" |
| 19 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/effects/Sk1DPathEffect.h" |
| 21 | #include "include/effects/Sk2DPathEffect.h" |
| 22 | #include "include/effects/SkCornerPathEffect.h" |
| 23 | #include "include/effects/SkDashPathEffect.h" |
| 24 | #include "include/effects/SkDiscretePathEffect.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 25 | #include "include/effects/SkOpPathEffect.h" |
| 26 | #include "include/pathops/SkPathOps.h" |
| 27 | |
| 28 | #include <initializer_list> |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 29 | |
| 30 | namespace skiagm { |
| 31 | |
| 32 | static void compose_pe(SkPaint* paint) { |
| 33 | SkPathEffect* pe = paint->getPathEffect(); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 34 | sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25); |
| 35 | sk_sp<SkPathEffect> compose; |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 36 | if (pe) { |
Mike Reed | a07741a | 2017-02-25 22:34:32 -0500 | [diff] [blame] | 37 | compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 38 | } else { |
| 39 | compose = corner; |
| 40 | } |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 41 | paint->setPathEffect(compose); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static void hair_pe(SkPaint* paint) { |
| 45 | paint->setStrokeWidth(0); |
| 46 | } |
| 47 | |
| 48 | static void hair2_pe(SkPaint* paint) { |
| 49 | paint->setStrokeWidth(0); |
| 50 | compose_pe(paint); |
| 51 | } |
| 52 | |
| 53 | static void stroke_pe(SkPaint* paint) { |
| 54 | paint->setStrokeWidth(12); |
| 55 | compose_pe(paint); |
| 56 | } |
| 57 | |
| 58 | static void dash_pe(SkPaint* paint) { |
| 59 | SkScalar inter[] = { 20, 10, 10, 10 }; |
| 60 | paint->setStrokeWidth(12); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 61 | paint->setPathEffect(SkDashPathEffect::Make(inter, SK_ARRAY_COUNT(inter), 0)); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 62 | compose_pe(paint); |
| 63 | } |
| 64 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 65 | constexpr int gXY[] = { |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 66 | 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 67 | }; |
| 68 | |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 69 | static SkPath scale(const SkPath& path, SkScalar scale) { |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 70 | SkMatrix m; |
| 71 | m.setScale(scale, scale); |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 72 | return path.makeTransform(m); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static void one_d_pe(SkPaint* paint) { |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 76 | SkPathBuilder b; |
| 77 | b.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 78 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) { |
| 79 | b.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 80 | } |
| 81 | b.close().offset(SkIntToScalar(-6), 0); |
| 82 | SkPath path = scale(b.detach(), 1.5f); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 83 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 84 | paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0, |
| 85 | SkPath1DPathEffect::kRotate_Style)); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 86 | compose_pe(paint); |
| 87 | } |
| 88 | |
| 89 | typedef void (*PE_Proc)(SkPaint*); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 90 | constexpr PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe }; |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 91 | |
| 92 | static void fill_pe(SkPaint* paint) { |
| 93 | paint->setStyle(SkPaint::kFill_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 94 | paint->setPathEffect(nullptr); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static void discrete_pe(SkPaint* paint) { |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 98 | paint->setPathEffect(SkDiscretePathEffect::Make(10, 4)); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 99 | } |
| 100 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 101 | static sk_sp<SkPathEffect> MakeTileEffect() { |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 102 | SkMatrix m; |
| 103 | m.setScale(SkIntToScalar(12), SkIntToScalar(12)); |
| 104 | |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 105 | return SkPath2DPathEffect::Make(m, SkPath::Circle(0,0,5)); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void tile_pe(SkPaint* paint) { |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 109 | paint->setPathEffect(MakeTileEffect()); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 112 | constexpr PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe }; |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 113 | |
| 114 | class PathEffectGM : public GM { |
| 115 | public: |
| 116 | PathEffectGM() {} |
| 117 | |
| 118 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 119 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 120 | SkString onShortName() override { |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 121 | return SkString("patheffect"); |
| 122 | } |
| 123 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 124 | SkISize onISize() override { return SkISize::Make(800, 600); } |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 125 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 126 | void onDraw(SkCanvas* canvas) override { |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 127 | SkPaint paint; |
| 128 | paint.setAntiAlias(true); |
| 129 | paint.setStyle(SkPaint::kStroke_Style); |
| 130 | |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 131 | SkPath path = SkPath::Polygon({ |
| 132 | {20, 20}, |
| 133 | {70, 120}, |
| 134 | {120, 30}, |
| 135 | {170, 80}, |
| 136 | {240, 50}, |
| 137 | }, false); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 138 | |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 139 | canvas->save(); |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 140 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPE); i++) { |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 141 | gPE[i](&paint); |
| 142 | canvas->drawPath(path, paint); |
| 143 | canvas->translate(0, 75); |
| 144 | } |
| 145 | canvas->restore(); |
| 146 | |
| 147 | path.reset(); |
| 148 | SkRect r = { 0, 0, 250, 120 }; |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 149 | path = SkPathBuilder().addOval(r, SkPathDirection::kCW) |
| 150 | .addRect(r.makeInset(50, 50), SkPathDirection::kCCW) |
| 151 | .detach(); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 152 | |
| 153 | canvas->translate(320, 20); |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 154 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPE2); i++) { |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 155 | gPE2[i](&paint); |
| 156 | canvas->drawPath(path, paint); |
| 157 | canvas->translate(0, 160); |
| 158 | } |
bsalomon@google.com | 22f42b7 | 2012-03-26 14:36:55 +0000 | [diff] [blame] | 159 | |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 160 | const SkIRect rect = SkIRect::MakeXYWH(20, 20, 60, 60); |
| 161 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPE); i++) { |
bsalomon@google.com | 22f42b7 | 2012-03-26 14:36:55 +0000 | [diff] [blame] | 162 | SkPaint p; |
| 163 | p.setAntiAlias(true); |
| 164 | p.setStyle(SkPaint::kFill_Style); |
| 165 | gPE[i](&p); |
| 166 | canvas->drawIRect(rect, p); |
| 167 | canvas->translate(75, 0); |
| 168 | } |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 172 | using INHERITED = GM; |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 175 | DEF_GM( return new PathEffectGM; ) |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 176 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 177 | } // namespace skiagm |
Mike Reed | 0ef539a | 2018-07-18 13:28:42 -0400 | [diff] [blame] | 178 | |
| 179 | ////////////////////////////////////////////////////////////////////////////// |
Mike Reed | 0ef539a | 2018-07-18 13:28:42 -0400 | [diff] [blame] | 180 | |
| 181 | class ComboPathEfectsGM : public skiagm::GM { |
| 182 | public: |
| 183 | ComboPathEfectsGM() {} |
| 184 | |
| 185 | protected: |
| 186 | |
| 187 | SkString onShortName() override { |
| 188 | return SkString("combo-patheffects"); |
| 189 | } |
| 190 | |
| 191 | SkISize onISize() override { return SkISize::Make(360, 630); } |
| 192 | |
| 193 | void onDraw(SkCanvas* canvas) override { |
Mike Reed | 06d7c9d | 2020-08-26 12:56:51 -0400 | [diff] [blame] | 194 | SkPath path0 = SkPath::Circle(100, 100, 60), |
| 195 | path1 = SkPathBuilder().moveTo(20, 20) |
| 196 | .cubicTo(20, 180, 140, 0, 140, 140) |
| 197 | .detach(); |
Mike Reed | 0ef539a | 2018-07-18 13:28:42 -0400 | [diff] [blame] | 198 | |
| 199 | sk_sp<SkPathEffect> effects[] = { |
| 200 | nullptr, |
| 201 | SkStrokePathEffect::Make(20, SkPaint::kRound_Join, SkPaint::kRound_Cap, 0), |
Mike Reed | 310f44d | 2018-07-19 13:47:44 -0400 | [diff] [blame] | 202 | SkMergePathEffect::Make(nullptr, |
| 203 | SkStrokePathEffect::Make(20, SkPaint::kRound_Join, |
| 204 | SkPaint::kRound_Cap, 0), |
| 205 | kDifference_SkPathOp), |
| 206 | SkMergePathEffect::Make(SkMatrixPathEffect::MakeTranslate(50, 30), |
| 207 | SkStrokePathEffect::Make(20, SkPaint::kRound_Join, |
| 208 | SkPaint::kRound_Cap, 0), |
| 209 | kReverseDifference_SkPathOp), |
Mike Reed | 0ef539a | 2018-07-18 13:28:42 -0400 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | SkPaint wireframe; |
| 213 | wireframe.setStyle(SkPaint::kStroke_Style); |
| 214 | wireframe.setAntiAlias(true); |
| 215 | |
| 216 | SkPaint paint; |
| 217 | paint.setColor(0xFF8888FF); |
| 218 | paint.setAntiAlias(true); |
| 219 | |
John Stiles | bd3ffa4 | 2020-07-30 20:24:57 -0400 | [diff] [blame] | 220 | for (const SkPath& path : { path0, path1 }) { |
Mike Reed | 0ef539a | 2018-07-18 13:28:42 -0400 | [diff] [blame] | 221 | canvas->save(); |
John Stiles | bd3ffa4 | 2020-07-30 20:24:57 -0400 | [diff] [blame] | 222 | for (const sk_sp<SkPathEffect>& pe : effects) { |
Mike Reed | 0ef539a | 2018-07-18 13:28:42 -0400 | [diff] [blame] | 223 | paint.setPathEffect(pe); |
| 224 | canvas->drawPath(path, paint); |
| 225 | canvas->drawPath(path, wireframe); |
| 226 | |
| 227 | canvas->translate(0, 150); |
| 228 | } |
| 229 | canvas->restore(); |
| 230 | canvas->translate(180, 0); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 235 | using INHERITED = GM; |
Mike Reed | 0ef539a | 2018-07-18 13:28:42 -0400 | [diff] [blame] | 236 | }; |
| 237 | DEF_GM(return new ComboPathEfectsGM;) |
| 238 | |
Mike Reed | 3e84312 | 2020-05-20 09:55:58 -0400 | [diff] [blame] | 239 | #include "include/effects/SkStrokeAndFillPathEffect.h" |
| 240 | |
| 241 | // Test that we can replicate SkPaint::kStrokeAndFill_Style |
| 242 | // with a patheffect. We expect the 2nd and 3rd columns to draw the same. |
| 243 | DEF_SIMPLE_GM(stroke_and_fill_patheffect, canvas, 900, 450) { |
| 244 | const float kStrokeWidth = 20; |
| 245 | |
Mike Reed | 74a7a81 | 2020-08-02 22:14:43 -0400 | [diff] [blame] | 246 | typedef SkPath (*Maker)(); |
Mike Reed | 3e84312 | 2020-05-20 09:55:58 -0400 | [diff] [blame] | 247 | const Maker makers[] = { |
Mike Reed | 74a7a81 | 2020-08-02 22:14:43 -0400 | [diff] [blame] | 248 | []() { return SkPath::Oval({0, 0, 100, 100}, SkPathDirection::kCW); }, |
| 249 | []() { return SkPath::Oval({0, 0, 100, 100}, SkPathDirection::kCCW); }, |
| 250 | []() { |
| 251 | const SkPoint pts[] = { |
| 252 | {0, 0}, {100, 100}, {0, 100}, {100, 0}, |
| 253 | }; |
| 254 | return SkPath::Polygon(pts, SK_ARRAY_COUNT(pts), true); |
Mike Reed | 3e84312 | 2020-05-20 09:55:58 -0400 | [diff] [blame] | 255 | }, |
| 256 | }; |
| 257 | |
| 258 | const struct { |
| 259 | SkPaint::Style fStyle; |
| 260 | float fWidth; |
| 261 | bool fUsePE; |
| 262 | bool fExpectStrokeAndFill; |
| 263 | } rec[] = { |
| 264 | { SkPaint::kStroke_Style, 0, false, false }, |
| 265 | { SkPaint::kFill_Style, 0, true, false }, |
| 266 | { SkPaint::kStroke_Style, 0, true, false }, |
| 267 | { SkPaint::kStrokeAndFill_Style, kStrokeWidth, false, true }, |
| 268 | { SkPaint::kStroke_Style, kStrokeWidth, true, true }, |
| 269 | { SkPaint::kStrokeAndFill_Style, kStrokeWidth, true, true }, |
| 270 | }; |
| 271 | |
| 272 | SkPaint paint; |
| 273 | canvas->translate(20, 20); |
| 274 | for (auto maker : makers) { |
Mike Reed | 74a7a81 | 2020-08-02 22:14:43 -0400 | [diff] [blame] | 275 | const SkPath path = maker(); |
Mike Reed | 3e84312 | 2020-05-20 09:55:58 -0400 | [diff] [blame] | 276 | canvas->save(); |
| 277 | for (const auto& r : rec) { |
| 278 | paint.setStyle(r.fStyle); |
| 279 | paint.setStrokeWidth(r.fWidth); |
| 280 | paint.setPathEffect(r.fUsePE ? SkStrokeAndFillPathEffect::Make() : nullptr); |
| 281 | paint.setColor(r.fExpectStrokeAndFill ? SK_ColorGRAY : SK_ColorBLACK); |
| 282 | |
| 283 | canvas->drawPath(path, paint); |
| 284 | canvas->translate(150, 0); |
| 285 | } |
| 286 | canvas->restore(); |
| 287 | |
| 288 | canvas->translate(0, 150); |
| 289 | } |
| 290 | } |