• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
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 "include/effects/Sk1DPathEffect.h"
9 
10 #include "include/core/SkFlattenable.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkPathEffect.h"
14 #include "include/core/SkPathMeasure.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkStrokeRec.h"
19 #include "include/core/SkTypes.h"
20 #include "include/private/base/SkFloatingPoint.h"
21 #include "src/core/SkPathEffectBase.h"
22 #include "src/core/SkReadBuffer.h"
23 #include "src/core/SkWriteBuffer.h"
24 
25 struct SkRect;
26 
27 // Since we are stepping by a float, the do/while loop might go on forever (or nearly so).
28 // Put in a governor to limit crash values from looping too long (and allocating too much ram).
29 #define MAX_REASONABLE_ITERATIONS   100000
30 
31 class Sk1DPathEffect : public SkPathEffectBase {
32 public:
33 protected:
onFilterPath(SkPath * dst,const SkPath & src,SkStrokeRec *,const SkRect *,const SkMatrix &) const34     bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec*, const SkRect*,
35                       const SkMatrix&) const override {
36         SkPathMeasure   meas(src, false);
37         do {
38             int governor = MAX_REASONABLE_ITERATIONS;
39             SkScalar    length = meas.getLength();
40             SkScalar    distance = this->begin(length);
41             while (distance < length && --governor >= 0) {
42                 SkScalar delta = this->next(dst, distance, meas);
43                 if (delta <= 0) {
44                     break;
45                 }
46                 distance += delta;
47             }
48 #ifdef OHOS_USED
49             if (governor < 0) {
50                 return false;
51             }
52 #endif
53         } while (meas.nextContour());
54         return true;
55     }
56 
57     /** Called at the start of each contour, returns the initial offset
58         into that contour.
59     */
60     virtual SkScalar begin(SkScalar contourLength) const = 0;
61     /** Called with the current distance along the path, with the current matrix
62         for the point/tangent at the specified distance.
63         Return the distance to travel for the next call. If return <= 0, then that
64         contour is done.
65     */
66     virtual SkScalar next(SkPath* dst, SkScalar dist, SkPathMeasure&) const = 0;
67 
68 private:
69     // For simplicity, assume fast bounds cannot be computed
computeFastBounds(SkRect *) const70     bool computeFastBounds(SkRect*) const override { return false; }
71 };
72 
73 ///////////////////////////////////////////////////////////////////////////////
74 
75 class SkPath1DPathEffectImpl : public Sk1DPathEffect {
76 public:
SkPath1DPathEffectImpl(const SkPath & path,SkScalar advance,SkScalar phase,SkPath1DPathEffect::Style style)77     SkPath1DPathEffectImpl(const SkPath& path, SkScalar advance, SkScalar phase,
78                            SkPath1DPathEffect::Style style) : fPath(path) {
79         SkASSERT(advance > 0 && !path.isEmpty());
80 
81         // Make the path thread-safe.
82         fPath.updateBoundsCache();
83         (void)fPath.getGenerationID();
84 
85         // cleanup their phase parameter, inverting it so that it becomes an
86         // offset along the path (to match the interpretation in PostScript)
87         if (phase < 0) {
88             phase = -phase;
89             if (phase > advance) {
90                 phase = SkScalarMod(phase, advance);
91             }
92         } else {
93             if (phase > advance) {
94                 phase = SkScalarMod(phase, advance);
95             }
96             phase = advance - phase;
97         }
98         // now catch the edge case where phase == advance (within epsilon)
99         if (phase >= advance) {
100             phase = 0;
101         }
102         SkASSERT(phase >= 0);
103 
104         fAdvance = advance;
105         fInitialOffset = phase;
106         fStyle = style;
107     }
108 
onFilterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * cullRect,const SkMatrix & ctm) const109     bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
110                       const SkRect* cullRect, const SkMatrix& ctm) const override {
111         rec->setFillStyle();
112         return this->INHERITED::onFilterPath(dst, src, rec, cullRect, ctm);
113     }
114 
begin(SkScalar contourLength) const115     SkScalar begin(SkScalar contourLength) const override {
116         return fInitialOffset;
117     }
118 
119     SkScalar next(SkPath*, SkScalar, SkPathMeasure&) const override;
120 
CreateProc(SkReadBuffer & buffer)121     static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) {
122         SkScalar advance = buffer.readScalar();
123         SkPath path;
124         buffer.readPath(&path);
125         SkScalar phase = buffer.readScalar();
126         SkPath1DPathEffect::Style style = buffer.read32LE(SkPath1DPathEffect::kLastEnum_Style);
127         return buffer.isValid() ? SkPath1DPathEffect::Make(path, advance, phase, style) : nullptr;
128     }
129 
flatten(SkWriteBuffer & buffer) const130     void flatten(SkWriteBuffer& buffer) const override {
131         buffer.writeScalar(fAdvance);
132         buffer.writePath(fPath);
133         buffer.writeScalar(fInitialOffset);
134         buffer.writeUInt(fStyle);
135     }
136 
getFactory() const137     Factory getFactory() const override { return CreateProc; }
getTypeName() const138     const char* getTypeName() const override { return "SkPath1DPathEffectImpl"; }
139 
140 private:
141     SkPath                      fPath;          // copied from constructor
142     SkScalar                    fAdvance;       // copied from constructor
143     SkScalar                    fInitialOffset; // computed from phase
144     SkPath1DPathEffect::Style   fStyle;         // copied from constructor
145 
146     using INHERITED = Sk1DPathEffect;
147 };
148 
morphpoints(SkPoint dst[],const SkPoint src[],int count,SkPathMeasure & meas,SkScalar dist)149 static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
150                         SkPathMeasure& meas, SkScalar dist) {
151     for (int i = 0; i < count; i++) {
152         SkPoint pos;
153         SkVector tangent;
154 
155         SkScalar sx = src[i].fX;
156         SkScalar sy = src[i].fY;
157 
158         if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
159             return false;
160         }
161 
162         SkMatrix    matrix;
163         SkPoint     pt;
164 
165         pt.set(sx, sy);
166         matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
167         matrix.preTranslate(-sx, 0);
168         matrix.postTranslate(pos.fX, pos.fY);
169         matrix.mapPoints(&dst[i], &pt, 1);
170     }
171     return true;
172 }
173 
174 /*  TODO
175 
176 Need differentially more subdivisions when the follow-path is curvy. Not sure how to
177 determine that, but we need it. I guess a cheap answer is let the caller tell us,
178 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
179 */
morphpath(SkPath * dst,const SkPath & src,SkPathMeasure & meas,SkScalar dist)180 static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
181                       SkScalar dist) {
182     SkPath::Iter    iter(src, false);
183     SkPoint         srcP[4], dstP[3];
184     SkPath::Verb    verb;
185 
186     while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
187         switch (verb) {
188             case SkPath::kMove_Verb:
189                 if (morphpoints(dstP, srcP, 1, meas, dist)) {
190                     dst->moveTo(dstP[0]);
191                 }
192                 break;
193             case SkPath::kLine_Verb:
194                 srcP[2] = srcP[1];
195                 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
196                             SkScalarAve(srcP[0].fY, srcP[2].fY));
197                 [[fallthrough]];
198             case SkPath::kQuad_Verb:
199                 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
200                     dst->quadTo(dstP[0], dstP[1]);
201                 }
202                 break;
203             case SkPath::kConic_Verb:
204                 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
205                     dst->conicTo(dstP[0], dstP[1], iter.conicWeight());
206                 }
207                 break;
208             case SkPath::kCubic_Verb:
209                 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
210                     dst->cubicTo(dstP[0], dstP[1], dstP[2]);
211                 }
212                 break;
213             case SkPath::kClose_Verb:
214                 dst->close();
215                 break;
216             default:
217                 SkDEBUGFAIL("unknown verb");
218                 break;
219         }
220     }
221 }
222 
next(SkPath * dst,SkScalar distance,SkPathMeasure & meas) const223 SkScalar SkPath1DPathEffectImpl::next(SkPath* dst, SkScalar distance,
224                                       SkPathMeasure& meas) const {
225 #if defined(SK_BUILD_FOR_FUZZER)
226     if (dst->countPoints() > 100000) {
227         return fAdvance;
228     }
229 #endif
230     switch (fStyle) {
231         case SkPath1DPathEffect::kTranslate_Style: {
232             SkPoint pos;
233             if (meas.getPosTan(distance, &pos, nullptr)) {
234                 dst->addPath(fPath, pos.fX, pos.fY);
235             }
236         } break;
237         case SkPath1DPathEffect::kRotate_Style: {
238             SkMatrix matrix;
239             if (meas.getMatrix(distance, &matrix)) {
240                 dst->addPath(fPath, matrix);
241             }
242         } break;
243         case SkPath1DPathEffect::kMorph_Style:
244             morphpath(dst, fPath, meas, distance);
245             break;
246     }
247     return fAdvance;
248 }
249 
250 ///////////////////////////////////////////////////////////////////////////////////////////////////
251 
Make(const SkPath & path,SkScalar advance,SkScalar phase,Style style)252 sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase,
253                                              Style style) {
254     if (advance <= 0 || !SkIsFinite(advance, phase) || path.isEmpty()) {
255         return nullptr;
256     }
257     return sk_sp<SkPathEffect>(new SkPath1DPathEffectImpl(path, advance, phase, style));
258 }
259 
RegisterFlattenables()260 void SkPath1DPathEffect::RegisterFlattenables() {
261     SK_REGISTER_FLATTENABLE(SkPath1DPathEffectImpl);
262 }
263