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