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 | */ |
| 7 | #include "gm.h" |
| 8 | #include "SkCanvas.h" |
| 9 | #include "SkPaint.h" |
| 10 | #include "Sk1DPathEffect.h" |
| 11 | #include "Sk2DPathEffect.h" |
| 12 | #include "SkCornerPathEffect.h" |
| 13 | #include "SkDashPathEffect.h" |
| 14 | #include "SkDiscretePathEffect.h" |
| 15 | |
| 16 | namespace skiagm { |
| 17 | |
| 18 | static void compose_pe(SkPaint* paint) { |
| 19 | SkPathEffect* pe = paint->getPathEffect(); |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 20 | SkPathEffect* corner = SkCornerPathEffect::Create(25); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 21 | SkPathEffect* compose; |
| 22 | if (pe) { |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 23 | compose = SkComposePathEffect::Create(pe, corner); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 24 | corner->unref(); |
| 25 | } else { |
| 26 | compose = corner; |
| 27 | } |
| 28 | paint->setPathEffect(compose)->unref(); |
| 29 | } |
| 30 | |
| 31 | static void hair_pe(SkPaint* paint) { |
| 32 | paint->setStrokeWidth(0); |
| 33 | } |
| 34 | |
| 35 | static void hair2_pe(SkPaint* paint) { |
| 36 | paint->setStrokeWidth(0); |
| 37 | compose_pe(paint); |
| 38 | } |
| 39 | |
| 40 | static void stroke_pe(SkPaint* paint) { |
| 41 | paint->setStrokeWidth(12); |
| 42 | compose_pe(paint); |
| 43 | } |
| 44 | |
| 45 | static void dash_pe(SkPaint* paint) { |
| 46 | SkScalar inter[] = { 20, 10, 10, 10 }; |
| 47 | paint->setStrokeWidth(12); |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 48 | paint->setPathEffect(SkDashPathEffect::Create(inter, SK_ARRAY_COUNT(inter), |
| 49 | 0))->unref(); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 50 | compose_pe(paint); |
| 51 | } |
| 52 | |
| 53 | static const int gXY[] = { |
| 54 | 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 55 | }; |
| 56 | |
| 57 | static void scale(SkPath* path, SkScalar scale) { |
| 58 | SkMatrix m; |
| 59 | m.setScale(scale, scale); |
| 60 | path->transform(m); |
| 61 | } |
| 62 | |
| 63 | static void one_d_pe(SkPaint* paint) { |
| 64 | SkPath path; |
| 65 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 66 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 67 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 68 | path.close(); |
| 69 | path.offset(SkIntToScalar(-6), 0); |
caryclark@google.com | 1313086 | 2012-06-06 12:10:45 +0000 | [diff] [blame] | 70 | scale(&path, 1.5f); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 71 | |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 72 | paint->setPathEffect(SkPath1DPathEffect::Create(path, SkIntToScalar(21), 0, |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 73 | SkPath1DPathEffect::kRotate_Style))->unref(); |
| 74 | compose_pe(paint); |
| 75 | } |
| 76 | |
| 77 | typedef void (*PE_Proc)(SkPaint*); |
| 78 | static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe }; |
| 79 | |
| 80 | static void fill_pe(SkPaint* paint) { |
| 81 | paint->setStyle(SkPaint::kFill_Style); |
| 82 | paint->setPathEffect(NULL); |
| 83 | } |
| 84 | |
| 85 | static void discrete_pe(SkPaint* paint) { |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 86 | paint->setPathEffect(SkDiscretePathEffect::Create(10, 4))->unref(); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static SkPathEffect* MakeTileEffect() { |
| 90 | SkMatrix m; |
| 91 | m.setScale(SkIntToScalar(12), SkIntToScalar(12)); |
| 92 | |
| 93 | SkPath path; |
| 94 | path.addCircle(0, 0, SkIntToScalar(5)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 95 | |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 96 | return SkPath2DPathEffect::Create(m, path); |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void tile_pe(SkPaint* paint) { |
| 100 | paint->setPathEffect(MakeTileEffect())->unref(); |
| 101 | } |
| 102 | |
| 103 | static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe }; |
| 104 | |
| 105 | class PathEffectGM : public GM { |
| 106 | public: |
| 107 | PathEffectGM() {} |
| 108 | |
| 109 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 110 | |
tfarina | 752e7eb | 2014-12-20 06:53:43 -0800 | [diff] [blame] | 111 | SkString onShortName() SK_OVERRIDE { |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 112 | return SkString("patheffect"); |
| 113 | } |
| 114 | |
tfarina | 752e7eb | 2014-12-20 06:53:43 -0800 | [diff] [blame] | 115 | SkISize onISize() SK_OVERRIDE { return SkISize::Make(800, 600); } |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 116 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 117 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 118 | SkPaint paint; |
| 119 | paint.setAntiAlias(true); |
| 120 | paint.setStyle(SkPaint::kStroke_Style); |
| 121 | |
| 122 | SkPath path; |
| 123 | path.moveTo(20, 20); |
| 124 | path.lineTo(70, 120); |
| 125 | path.lineTo(120, 30); |
| 126 | path.lineTo(170, 80); |
| 127 | path.lineTo(240, 50); |
| 128 | |
| 129 | size_t i; |
| 130 | canvas->save(); |
| 131 | for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) { |
| 132 | gPE[i](&paint); |
| 133 | canvas->drawPath(path, paint); |
| 134 | canvas->translate(0, 75); |
| 135 | } |
| 136 | canvas->restore(); |
| 137 | |
| 138 | path.reset(); |
| 139 | SkRect r = { 0, 0, 250, 120 }; |
| 140 | path.addOval(r, SkPath::kCW_Direction); |
| 141 | r.inset(50, 50); |
| 142 | path.addRect(r, SkPath::kCCW_Direction); |
| 143 | |
| 144 | canvas->translate(320, 20); |
| 145 | for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) { |
| 146 | gPE2[i](&paint); |
| 147 | canvas->drawPath(path, paint); |
| 148 | canvas->translate(0, 160); |
| 149 | } |
bsalomon@google.com | 22f42b7 | 2012-03-26 14:36:55 +0000 | [diff] [blame] | 150 | |
| 151 | SkIRect rect = SkIRect::MakeXYWH(20, 20, 60, 60); |
| 152 | for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) { |
| 153 | SkPaint p; |
| 154 | p.setAntiAlias(true); |
| 155 | p.setStyle(SkPaint::kFill_Style); |
| 156 | gPE[i](&p); |
| 157 | canvas->drawIRect(rect, p); |
| 158 | canvas->translate(75, 0); |
| 159 | } |
bsalomon@google.com | 632151b | 2012-02-13 15:18:34 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | private: |
| 163 | typedef GM INHERITED; |
| 164 | }; |
| 165 | |
| 166 | ////////////////////////////////////////////////////////////////////////////// |
| 167 | |
| 168 | static GM* PathEffectFactory(void*) { return new PathEffectGM; } |
| 169 | static GMRegistry regPathEffect(PathEffectFactory); |
| 170 | |
| 171 | } |