blob: 342fe2e8469d48f12fb8d0c4cc51124cb9d36fb7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkPathMeasure.h"
10#include "include/core/SkStrokeRec.h"
11#include "include/effects/SkDiscretePathEffect.h"
12#include "include/private/SkFixed.h"
13#include "src/core/SkPointPriv.h"
14#include "src/core/SkReadBuffer.h"
15#include "src/core/SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
reeda4393342016-03-18 11:22:57 -070017sk_sp<SkPathEffect> SkDiscretePathEffect::Make(SkScalar segLength, SkScalar deviation,
18 uint32_t seedAssist) {
reed18c00972016-04-02 18:40:40 -070019 if (!SkScalarIsFinite(segLength) || !SkScalarIsFinite(deviation)) {
20 return nullptr;
21 }
22 if (segLength <= SK_ScalarNearlyZero) {
23 return nullptr;
24 }
reeda4393342016-03-18 11:22:57 -070025 return sk_sp<SkPathEffect>(new SkDiscretePathEffect(segLength, deviation, seedAssist));
26}
27
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000028static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 SkVector normal = tangent;
Cary Clarkdf429f32017-11-08 11:44:31 -050030 SkPointPriv::RotateCCW(&normal);
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 normal.setLength(scale);
32 *p += normal;
33}
34
rs.prinja39e58ad2014-06-12 22:55:08 -070035SkDiscretePathEffect::SkDiscretePathEffect(SkScalar segLength,
36 SkScalar deviation,
37 uint32_t seedAssist)
38 : fSegLength(segLength), fPerterb(deviation), fSeedAssist(seedAssist)
reed@android.com8a1c16f2008-12-17 15:59:43 +000039{
40}
41
scroggof9d61012014-12-15 12:54:51 -080042/** \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
53class LCGRandom {
54public:
55 LCGRandom(uint32_t seed) : fSeed(seed) {}
56
57 /** Return the next pseudo random number expressed as a SkScalar
benjaminwagner12634482016-03-31 06:13:22 -070058 in the range [-SK_Scalar1..SK_Scalar1).
scroggof9d61012014-12-15 12:54:51 -080059 */
60 SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); }
61
62private:
63 /** Return the next pseudo random number as an unsigned 32bit value.
64 */
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 */
69 int32_t nextS() { return (int32_t)this->nextU(); }
70
71 /** Return the next pseudo random number expressed as a signed SkFixed
benjaminwagner12634482016-03-31 06:13:22 -070072 in the range [-SK_Fixed1..SK_Fixed1).
scroggof9d61012014-12-15 12:54:51 -080073 */
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
Mike Reed6d10f8b2018-08-16 13:22:16 -040084bool SkDiscretePathEffect::onFilterPath(SkPath* dst, const SkPath& src,
85 SkStrokeRec* rec, const SkRect*) const {
reed@google.comfd4be262012-05-25 01:04:12 +000086 bool doFill = rec->isFillStyle();
reed@android.com8a1c16f2008-12-17 15:59:43 +000087
88 SkPathMeasure meas(src, doFill);
rs.prinja39e58ad2014-06-12 22:55:08 -070089
90 /* Caller may supply their own seed assist, which by default is 0 */
91 uint32_t seed = fSeedAssist ^ SkScalarRoundToInt(meas.getLength());
92
scroggof9d61012014-12-15 12:54:51 -080093 LCGRandom rand(seed ^ ((seed << 16) | (seed >> 16)));
94 SkScalar scale = fPerterb;
95 SkPoint p;
96 SkVector v;
reed@android.com8a1c16f2008-12-17 15:59:43 +000097
98 do {
99 SkScalar length = meas.getLength();
Kevin Lubick493f89e2020-09-14 08:37:35 -0400100#if defined(SK_BUILD_FOR_FUZZER)
101 if (length > 1000) {
102 return false;
103 }
104#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000106 if (fSegLength * (2 + doFill) > length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 meas.getSegment(0, length, dst, true); // to short for us to mangle
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000108 } else {
reed@google.come1ca7052013-12-17 19:22:07 +0000109 int n = SkScalarRoundToInt(length / fSegLength);
Cary Clarkcec40a92018-03-21 15:27:08 -0400110 constexpr int kMaxReasonableIterations = 100000;
Brian Osman788b9162020-02-07 10:36:46 -0500111 n = std::min(n, kMaxReasonableIterations);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 SkScalar delta = length / n;
113 SkScalar distance = 0;
114
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000115 if (meas.isClosed()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 n -= 1;
117 distance += delta/2;
118 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000119
reed@google.comf3edf9f2012-04-12 19:44:38 +0000120 if (meas.getPosTan(distance, &p, &v)) {
Mike Reed8be952a2017-02-13 20:44:33 -0500121 Perterb(&p, v, rand.nextSScalar1() * scale);
reed@google.comf3edf9f2012-04-12 19:44:38 +0000122 dst->moveTo(p);
123 }
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000124 while (--n >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 distance += delta;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000126 if (meas.getPosTan(distance, &p, &v)) {
Mike Reed8be952a2017-02-13 20:44:33 -0500127 Perterb(&p, v, rand.nextSScalar1() * scale);
reed@google.comf3edf9f2012-04-12 19:44:38 +0000128 dst->lineTo(p);
129 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 }
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000131 if (meas.isClosed()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 dst->close();
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000133 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 }
135 } while (meas.nextContour());
136 return true;
137}
138
Michael Ludwig4e1c1a72021-05-11 11:39:36 -0400139bool SkDiscretePathEffect::computeFastBounds(SkRect* bounds) const {
140 if (bounds) {
141 SkScalar maxOutset = SkScalarAbs(fPerterb);
142 bounds->outset(maxOutset, maxOutset);
143 }
144 return true;
145}
146
reed60c9b582016-04-03 09:11:13 -0700147sk_sp<SkFlattenable> SkDiscretePathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700148 SkScalar segLength = buffer.readScalar();
149 SkScalar perterb = buffer.readScalar();
150 uint32_t seed = buffer.readUInt();
reed60c9b582016-04-03 09:11:13 -0700151 return Make(segLength, perterb, seed);
reed9fa60da2014-08-21 07:59:51 -0700152}
153
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000154void SkDiscretePathEffect::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 buffer.writeScalar(fSegLength);
156 buffer.writeScalar(fPerterb);
rs.prinja39e58ad2014-06-12 22:55:08 -0700157 buffer.writeUInt(fSeedAssist);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158}