Debian Codegraph @

src/effects/SkDiscretePathEffect.cpp

raw
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/core/SkFlattenable.h"
9 #include "include/core/SkPathBuilder.h"
10 #include "include/core/SkPathEffect.h"
11 #include "include/core/SkPathMeasure.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkStrokeRec.h"
17 #include "include/core/SkTypes.h"
18 #include "include/effects/SkDiscretePathEffect.h"
19 #include "include/private/base/SkFixed.h"
20 #include "include/private/base/SkFloatingPoint.h"
21 #include "src/core/SkPathEffectBase.h"
22 #include "src/core/SkPointPriv.h"
23 #include "src/core/SkReadBuffer.h"
24 #include "src/core/SkWriteBuffer.h"
25
26 #include <algorithm>
27 #include <cstdint>
28
29 class SkMatrix;
30 class SkPath;
31
32 /** \class LCGRandom
33
34 Utility class that implements pseudo random 32bit numbers using a fast
35 linear equation. Unlike rand(), this class holds its own seed (initially
36 set to 0), so that multiple instances can be used with no side-effects.
37
38 Copied from the original implementation of SkRandom. Only contains the
39 methods used by SkDiscretePathEffect::filterPath, with methods that were
40 not called directly moved to private.
41 */
42 class LCGRandom {
43 public:
44 LCGRandom(uint32_t seed) : fSeed(seed) {}
45
46 /** Return the next pseudo random number expressed as a SkScalar
47 in the range [-SK_Scalar1..SK_Scalar1).
48 */
49 SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); }
50
51 private:
52 /** Return the next pseudo random number as an unsigned 32bit value.
53 */
54 uint32_t nextU() { uint32_t r = fSeed * kMul + kAdd; fSeed = r; return r; }
55
56 /** Return the next pseudo random number as a signed 32bit value.
57 */
58 int32_t nextS() { return (int32_t)this->nextU(); }
59
60 /** Return the next pseudo random number expressed as a signed SkFixed
61 in the range [-SK_Fixed1..SK_Fixed1).
62 */
63 SkFixed nextSFixed1() { return this->nextS() >> 15; }
64
65 // See "Numerical Recipes in C", 1992 page 284 for these constants
66 enum {
67 kMul = 1664525,
68 kAdd = 1013904223
69 };
70 uint32_t fSeed;
71 };
72
73 static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) {
74 SkVector normal = tangent;
75 SkPointPriv::RotateCCW(&normal);
76 normal.setLength(scale);
77 *p += normal;
78 }
79
80 class SkDiscretePathEffectImpl : public SkPathEffectBase {
81 public:
82 SkDiscretePathEffectImpl(SkScalar segLength, SkScalar deviation, uint32_t seedAssist)
83 : fSegLength(segLength), fPerterb(deviation), fSeedAssist(seedAssist)
84 {
85 SkASSERT(SkIsFinite(segLength));
86 SkASSERT(SkIsFinite(deviation));
87 SkASSERT(segLength > SK_ScalarNearlyZero);
88 }
89
90 bool onFilterPath(SkPathBuilder* dst, const SkPath& src, SkStrokeRec* rec,
91 const SkRect*, const SkMatrix&) const override {
92 bool doFill = rec->isFillStyle();
93
94 SkPathMeasure meas(src, doFill);
95
96 /* Caller may supply their own seed assist, which by default is 0 */
97 uint32_t seed = fSeedAssist ^ SkScalarRoundToInt(meas.getLength());
98
99 LCGRandom rand(seed ^ ((seed << 16) | (seed >> 16)));
100 SkScalar scale = fPerterb;
101 SkPoint p;
102 SkVector v;
103
104 do {
105 SkScalar length = meas.getLength();
106 #if defined(SK_BUILD_FOR_FUZZER)
107 if (length > 1000) {
108 return false;
109 }
110 #endif
111
112 if (fSegLength * (2 + doFill) > length) {
113 meas.getSegment(0, length, dst, true); // to short for us to mangle
114 } else {
115 int n = SkScalarRoundToInt(length / fSegLength);
116 constexpr int kMaxReasonableIterations = 100000;
117 n = std::min(n, kMaxReasonableIterations);
118 SkScalar delta = length / n;
119 SkScalar distance = 0;
120
121 if (meas.isClosed()) {
122 n -= 1;
123 distance += delta/2;
124 }
125
126 if (meas.getPosTan(distance, &p, &v)) {
127 Perterb(&p, v, rand.nextSScalar1() * scale);
128 dst->moveTo(p);
129 }
130 while (--n >= 0) {
131 distance += delta;
132 if (meas.getPosTan(distance, &p, &v)) {
133 Perterb(&p, v, rand.nextSScalar1() * scale);
134 dst->lineTo(p);
135 }
136 }
137 if (meas.isClosed()) {
138 dst->close();
139 }
140 }
141 } while (meas.nextContour());
142 return true;
143 }
144
145 bool computeFastBounds(SkRect* bounds) const override {
146 if (bounds) {
147 SkScalar maxOutset = SkScalarAbs(fPerterb);
148 bounds->outset(maxOutset, maxOutset);
149 }
150 return true;
151 }
152
153 static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) {
154 SkScalar segLength = buffer.readScalar();
155 SkScalar perterb = buffer.readScalar();
156 uint32_t seed = buffer.readUInt();
157 return SkDiscretePathEffect::Make(segLength, perterb, seed);
158 }
159
160 void flatten(SkWriteBuffer& buffer) const override {
161 buffer.writeScalar(fSegLength);
162 buffer.writeScalar(fPerterb);
163 buffer.writeUInt(fSeedAssist);
164 }
165
166 Factory getFactory() const override { return CreateProc; }
167 const char* getTypeName() const override { return "SkDiscretePathEffect"; }
168
169 private:
170 const SkScalar fSegLength,
171 fPerterb;
172 /* Caller-supplied 32 bit seed assist */
173 const uint32_t fSeedAssist;
174
175 using INHERITED = SkPathEffectBase;
176 };
177
178 //////////////////////////////////////////////////////////////////////////////////////////////////
179
180 sk_sp<SkPathEffect> SkDiscretePathEffect::Make(SkScalar segLength, SkScalar deviation,
181 uint32_t seedAssist) {
182 if (!SkIsFinite(segLength, deviation)) {
183 return nullptr;
184 }
185 if (segLength <= SK_ScalarNearlyZero) {
186 return nullptr;
187 }
188 return sk_sp<SkPathEffect>(new SkDiscretePathEffectImpl(segLength, deviation, seedAssist));
189 }
190
191 void SkDiscretePathEffect::RegisterFlattenables() {
192 SkFlattenable::Register("SkDiscretePathEffect", SkDiscretePathEffectImpl::CreateProc);
193 }