reed@google.com | 8d850be | 2012-07-13 15:55:15 +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 | */ |
| 7 | |
| 8 | #include "gm.h" |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame^] | 9 | #include "SkAnimTimer.h" |
reed@google.com | 8d850be | 2012-07-13 15:55:15 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame^] | 11 | #include "SkDashPathEffect.h" |
reed@google.com | 8d850be | 2012-07-13 15:55:15 +0000 | [diff] [blame] | 12 | #include "SkPath.h" |
| 13 | #include "SkParsePath.h" |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame^] | 14 | #include "SkTArray.h" |
| 15 | #include "SkTrimPathEffect.h" |
reed@google.com | 8d850be | 2012-07-13 15:55:15 +0000 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * Inspired by http://code.google.com/p/chromium/issues/detail?id=112145 |
| 19 | */ |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 20 | static void flower(SkCanvas* canvas, const SkPath& path, SkScalar intervals[2], |
| 21 | SkPaint::Join join) { |
| 22 | SkPaint paint; |
| 23 | paint.setAntiAlias(true); |
| 24 | paint.setStyle(SkPaint::kStroke_Style); |
| 25 | paint.setStrokeJoin(join); |
| 26 | paint.setStrokeWidth(42); |
| 27 | canvas->drawPath(path, paint); |
caryclark | 6df8e34 | 2015-02-23 12:47:03 -0800 | [diff] [blame] | 28 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 29 | paint.setColor(SK_ColorRED); |
| 30 | paint.setStrokeWidth(21); |
| 31 | paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0)); |
| 32 | canvas->drawPath(path, paint); |
caryclark | 6df8e34 | 2015-02-23 12:47:03 -0800 | [diff] [blame] | 33 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 34 | paint.setColor(SK_ColorGREEN); |
| 35 | paint.setPathEffect(nullptr); |
| 36 | paint.setStrokeWidth(0); |
| 37 | canvas->drawPath(path, paint); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 38 | } |
reed@google.com | 8d850be | 2012-07-13 15:55:15 +0000 | [diff] [blame] | 39 | |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 40 | DEF_SIMPLE_GM(dashcubics, canvas, 865, 750) { |
reed@google.com | 8d850be | 2012-07-13 15:55:15 +0000 | [diff] [blame] | 41 | SkPath path; |
| 42 | const char* d = "M 337,98 C 250,141 250,212 250,212 C 250,212 250,212 250,212" |
| 43 | "C 250,212 250,212 250,212 C 250,212 250,141 163,98 C 156,195 217,231 217,231" |
| 44 | "C 217,231 217,231 217,231 C 217,231 217,231 217,231 C 217,231 156,195 75,250" |
| 45 | "C 156,305 217,269 217,269 C 217,269 217,269 217,269 C 217,269 217,269 217,269" |
| 46 | "C 217,269 156,305 163,402 C 250,359 250,288 250,288 C 250,288 250,288 250,288" |
| 47 | "C 250,288 250,288 250,288 C 250,288 250,359 338,402 C 345,305 283,269 283,269" |
| 48 | "C 283,269 283,269 283,269 C 283,269 283,269 283,269 C 283,269 345,305 425,250" |
| 49 | "C 344,195 283,231 283,231 C 283,231 283,231 283,231 C 283,231 283,231 283,231" |
| 50 | "C 283,231 344,195 338,98"; |
| 51 | |
| 52 | SkParsePath::FromSVGString(d, &path); |
caryclark | 6df8e34 | 2015-02-23 12:47:03 -0800 | [diff] [blame] | 53 | canvas->translate(-35.f, -55.f); |
| 54 | for (int x = 0; x < 2; ++x) { |
| 55 | for (int y = 0; y < 2; ++y) { |
| 56 | canvas->save(); |
| 57 | canvas->translate(x * 430.f, y * 355.f); |
| 58 | SkScalar intervals[] = { 5 + (x ? 0 : 0.0001f + 0.0001f), 10 }; |
| 59 | flower(canvas, path, intervals, y ? SkPaint::kDefault_Join : SkPaint::kRound_Join); |
| 60 | canvas->restore(); |
| 61 | } |
| 62 | } |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 63 | } |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 64 | |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 65 | class TrimGM : public skiagm::GM { |
| 66 | public: |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame^] | 67 | TrimGM() { |
| 68 | SkAssertResult(SkParsePath::FromSVGString( |
| 69 | "M 0,100 C 10, 50 190, 50 200,100" |
| 70 | "M 200,100 C 210,150 390,150 400,100" |
| 71 | "M 400,100 C 390, 50 210, 50 200,100" |
| 72 | "M 200,100 C 190,150 10,150 0,100", |
| 73 | &fPaths.push_back())); |
| 74 | |
| 75 | SkAssertResult(SkParsePath::FromSVGString( |
| 76 | "M 0, 75 L 200, 75" |
| 77 | "M 200, 91 L 200, 91" |
| 78 | "M 200,108 L 200,108" |
| 79 | "M 200,125 L 400,125", |
| 80 | &fPaths.push_back())); |
| 81 | |
| 82 | SkAssertResult(SkParsePath::FromSVGString( |
| 83 | "M 0,100 L 50, 50" |
| 84 | "M 50, 50 L 150,150" |
| 85 | "M 150,150 L 250, 50" |
| 86 | "M 250, 50 L 350,150" |
| 87 | "M 350,150 L 400,100", |
| 88 | &fPaths.push_back())); |
| 89 | |
| 90 | } |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 91 | |
| 92 | protected: |
| 93 | SkString onShortName() override { return SkString("trimpatheffect"); } |
| 94 | |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame^] | 95 | SkISize onISize() override { |
| 96 | return SkISize::Make(1400, 1000); |
| 97 | } |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 98 | |
| 99 | void onDraw(SkCanvas* canvas) override { |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame^] | 100 | static constexpr SkSize kCellSize = { 440, 150 }; |
| 101 | static constexpr SkScalar kOffsets[][2] = { |
| 102 | { -0.33f, -0.66f }, |
| 103 | { 0 , 1 }, |
| 104 | { 0 , 0.25f}, |
| 105 | { 0.25f, 0.75f}, |
| 106 | { 0.75f, 1 }, |
| 107 | { 1 , 0.75f}, |
| 108 | }; |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 109 | |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame^] | 110 | SkPaint hairlinePaint; |
| 111 | hairlinePaint.setAntiAlias(true); |
| 112 | hairlinePaint.setStyle(SkPaint::kStroke_Style); |
| 113 | hairlinePaint.setStrokeCap(SkPaint::kRound_Cap); |
| 114 | hairlinePaint.setStrokeWidth(2); |
| 115 | SkPaint normalPaint = hairlinePaint; |
| 116 | normalPaint.setStrokeWidth(10); |
| 117 | normalPaint.setColor(0x8000ff00); |
| 118 | SkPaint invertedPaint = normalPaint; |
| 119 | invertedPaint.setColor(0x80ff0000); |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 120 | |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame^] | 121 | for (const auto& offset : kOffsets) { |
| 122 | auto start = offset[0] + fOffset, |
| 123 | stop = offset[1] + fOffset; |
| 124 | |
| 125 | auto normalMode = SkTrimPathEffect::Mode::kNormal, |
| 126 | invertedMode = SkTrimPathEffect::Mode::kInverted; |
| 127 | if (fOffset) { |
| 128 | start -= SkScalarFloorToScalar(start); |
| 129 | stop -= SkScalarFloorToScalar(stop); |
| 130 | if (start > stop) { |
| 131 | SkTSwap(start, stop); |
| 132 | SkTSwap(normalMode, invertedMode); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | normalPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, normalMode)); |
| 137 | invertedPaint.setPathEffect(SkTrimPathEffect::Make(start, stop, invertedMode)); |
| 138 | |
| 139 | { |
| 140 | SkAutoCanvasRestore acr(canvas, true); |
| 141 | for (const auto& path : fPaths) { |
| 142 | canvas->drawPath(path, normalPaint); |
| 143 | canvas->drawPath(path, invertedPaint); |
| 144 | canvas->drawPath(path, hairlinePaint); |
| 145 | canvas->translate(kCellSize.width(), 0); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | canvas->translate(0, kCellSize.height()); |
| 150 | } |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame^] | 153 | bool onAnimate(const SkAnimTimer& t) override { |
| 154 | fOffset = t.msec() / 2000.0f; |
| 155 | fOffset -= floorf(fOffset); |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 156 | return true; |
| 157 | } |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame^] | 158 | |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 159 | private: |
Florin Malita | 827af66 | 2018-03-09 16:08:58 -0500 | [diff] [blame^] | 160 | SkTArray<SkPath> fPaths; |
| 161 | SkScalar fOffset = 0; |
| 162 | |
Mike Reed | 4123223 | 2018-03-07 17:02:47 -0500 | [diff] [blame] | 163 | typedef skiagm::GM INHERITED; |
| 164 | }; |
| 165 | DEF_GM(return new TrimGM;) |
| 166 | |