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 
9 #include "SkDiscretePathEffect.h"
10 #include "SkFixed.h"
11 #include "SkPathMeasure.h"
12 #include "SkPointPriv.h"
13 #include "SkReadBuffer.h"
14 #include "SkStrokeRec.h"
15 #include "SkWriteBuffer.h"
16 
Make(SkScalar segLength,SkScalar deviation,uint32_t seedAssist)17 sk_sp<SkPathEffect> SkDiscretePathEffect::Make(SkScalar segLength, SkScalar deviation,
18                                                uint32_t seedAssist) {
19     if (!SkScalarIsFinite(segLength) || !SkScalarIsFinite(deviation)) {
20         return nullptr;
21     }
22     if (segLength <= SK_ScalarNearlyZero) {
23         return nullptr;
24     }
25     return sk_sp<SkPathEffect>(new SkDiscretePathEffect(segLength, deviation, seedAssist));
26 }
27 
Perterb(SkPoint * p,const SkVector & tangent,SkScalar scale)28 static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) {
29     SkVector normal = tangent;
30     SkPointPriv::RotateCCW(&normal);
31     normal.setLength(scale);
32     *p += normal;
33 }
34 
SkDiscretePathEffect(SkScalar segLength,SkScalar deviation,uint32_t seedAssist)35 SkDiscretePathEffect::SkDiscretePathEffect(SkScalar segLength,
36                                            SkScalar deviation,
37                                            uint32_t seedAssist)
38     : fSegLength(segLength), fPerterb(deviation), fSeedAssist(seedAssist)
39 {
40 }
41 
42 /** \class LCGRandom
43 
44     Utility class that implements pseudo random 32bit numbers using a fast
45     linear equation. Unlike rand(), this class holds its own seed (initially
46     set to 0), so that multiple instances can be used with no side-effects.
47 
48     Copied from the original implementation of SkRandom. Only contains the
49     methods used by SkDiscretePathEffect::filterPath, with methods that were
50     not called directly moved to private.
51 */
52 
53 class LCGRandom {
54 public:
LCGRandom(uint32_t seed)55     LCGRandom(uint32_t seed) : fSeed(seed) {}
56 
57     /** Return the next pseudo random number expressed as a SkScalar
58         in the range [-SK_Scalar1..SK_Scalar1).
59     */
nextSScalar1()60     SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); }
61 
62 private:
63     /** Return the next pseudo random number as an unsigned 32bit value.
64     */
nextU()65     uint32_t nextU() { uint32_t r = fSeed * kMul + kAdd; fSeed = r; return r; }
66 
67     /** Return the next pseudo random number as a signed 32bit value.
68      */
nextS()69     int32_t nextS() { return (int32_t)this->nextU(); }
70 
71     /** Return the next pseudo random number expressed as a signed SkFixed
72      in the range [-SK_Fixed1..SK_Fixed1).
73      */
nextSFixed1()74     SkFixed nextSFixed1() { return this->nextS() >> 15; }
75 
76     //  See "Numerical Recipes in C", 1992 page 284 for these constants
77     enum {
78         kMul = 1664525,
79         kAdd = 1013904223
80     };
81     uint32_t fSeed;
82 };
83 
filterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect *) const84 bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src,
85                                       SkStrokeRec* rec, const SkRect*) const {
86     bool doFill = rec->isFillStyle();
87 
88     SkPathMeasure   meas(src, doFill);
89 
90     /* Caller may supply their own seed assist, which by default is 0 */
91     uint32_t seed = fSeedAssist ^ SkScalarRoundToInt(meas.getLength());
92 
93     LCGRandom   rand(seed ^ ((seed << 16) | (seed >> 16)));
94     SkScalar    scale = fPerterb;
95     SkPoint     p;
96     SkVector    v;
97 
98     do {
99         SkScalar    length = meas.getLength();
100 
101         if (fSegLength * (2 + doFill) > length) {
102             meas.getSegment(0, length, dst, true);  // to short for us to mangle
103         } else {
104             int         n = SkScalarRoundToInt(length / fSegLength);
105             SkScalar    delta = length / n;
106             SkScalar    distance = 0;
107 
108             if (meas.isClosed()) {
109                 n -= 1;
110                 distance += delta/2;
111             }
112 
113             if (meas.getPosTan(distance, &p, &v)) {
114                 Perterb(&p, v, rand.nextSScalar1() * scale);
115                 dst->moveTo(p);
116             }
117             while (--n >= 0) {
118                 distance += delta;
119                 if (meas.getPosTan(distance, &p, &v)) {
120                     Perterb(&p, v, rand.nextSScalar1() * scale);
121                     dst->lineTo(p);
122                 }
123             }
124             if (meas.isClosed()) {
125                 dst->close();
126             }
127         }
128     } while (meas.nextContour());
129     return true;
130 }
131 
CreateProc(SkReadBuffer & buffer)132 sk_sp<SkFlattenable> SkDiscretePathEffect::CreateProc(SkReadBuffer& buffer) {
133     SkScalar segLength = buffer.readScalar();
134     SkScalar perterb = buffer.readScalar();
135     uint32_t seed = buffer.readUInt();
136     return Make(segLength, perterb, seed);
137 }
138 
flatten(SkWriteBuffer & buffer) const139 void SkDiscretePathEffect::flatten(SkWriteBuffer& buffer) const {
140     buffer.writeScalar(fSegLength);
141     buffer.writeScalar(fPerterb);
142     buffer.writeUInt(fSeedAssist);
143 }
144 
145 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const146 void SkDiscretePathEffect::toString(SkString* str) const {
147     str->appendf("SkDiscretePathEffect: (");
148     str->appendf("segLength: %.2f deviation: %.2f seed %d", fSegLength, fPerterb, fSeedAssist);
149     str->append(")");
150 }
151 #endif
152