epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | |
| 9 | #include "SkDiscretePathEffect.h" |
benjaminwagner | 6c71e0a | 2016-04-07 08:49:31 -0700 | [diff] [blame] | 10 | #include "SkFixed.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkPathMeasure.h" |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 12 | #include "SkPointPriv.h" |
benjaminwagner | 6c71e0a | 2016-04-07 08:49:31 -0700 | [diff] [blame] | 13 | #include "SkReadBuffer.h" |
halcanary | 435657f | 2015-09-15 12:53:07 -0700 | [diff] [blame] | 14 | #include "SkStrokeRec.h" |
benjaminwagner | 6c71e0a | 2016-04-07 08:49:31 -0700 | [diff] [blame] | 15 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 17 | sk_sp<SkPathEffect> SkDiscretePathEffect::Make(SkScalar segLength, SkScalar deviation, |
| 18 | uint32_t seedAssist) { |
reed | 18c0097 | 2016-04-02 18:40:40 -0700 | [diff] [blame] | 19 | if (!SkScalarIsFinite(segLength) || !SkScalarIsFinite(deviation)) { |
| 20 | return nullptr; |
| 21 | } |
| 22 | if (segLength <= SK_ScalarNearlyZero) { |
| 23 | return nullptr; |
| 24 | } |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 25 | return sk_sp<SkPathEffect>(new SkDiscretePathEffect(segLength, deviation, seedAssist)); |
| 26 | } |
| 27 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 28 | static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | SkVector normal = tangent; |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 30 | SkPointPriv::RotateCCW(&normal); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | normal.setLength(scale); |
| 32 | *p += normal; |
| 33 | } |
| 34 | |
rs.prinja | 39e58ad | 2014-06-12 22:55:08 -0700 | [diff] [blame] | 35 | SkDiscretePathEffect::SkDiscretePathEffect(SkScalar segLength, |
| 36 | SkScalar deviation, |
| 37 | uint32_t seedAssist) |
| 38 | : fSegLength(segLength), fPerterb(deviation), fSeedAssist(seedAssist) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 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: |
| 55 | LCGRandom(uint32_t seed) : fSeed(seed) {} |
| 56 | |
| 57 | /** Return the next pseudo random number expressed as a SkScalar |
benjaminwagner | 1263448 | 2016-03-31 06:13:22 -0700 | [diff] [blame] | 58 | in the range [-SK_Scalar1..SK_Scalar1). |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 59 | */ |
| 60 | SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); } |
| 61 | |
| 62 | private: |
| 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 |
benjaminwagner | 1263448 | 2016-03-31 06:13:22 -0700 | [diff] [blame] | 72 | in the range [-SK_Fixed1..SK_Fixed1). |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 73 | */ |
| 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@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 84 | bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src, |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 85 | SkStrokeRec* rec, const SkRect*) const { |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 86 | bool doFill = rec->isFillStyle(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | |
| 88 | SkPathMeasure meas(src, doFill); |
rs.prinja | 39e58ad | 2014-06-12 22:55:08 -0700 | [diff] [blame] | 89 | |
| 90 | /* Caller may supply their own seed assist, which by default is 0 */ |
| 91 | uint32_t seed = fSeedAssist ^ SkScalarRoundToInt(meas.getLength()); |
| 92 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 93 | LCGRandom rand(seed ^ ((seed << 16) | (seed >> 16))); |
| 94 | SkScalar scale = fPerterb; |
| 95 | SkPoint p; |
| 96 | SkVector v; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | |
| 98 | do { |
| 99 | SkScalar length = meas.getLength(); |
| 100 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 101 | if (fSegLength * (2 + doFill) > length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | meas.getSegment(0, length, dst, true); // to short for us to mangle |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 103 | } else { |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 104 | int n = SkScalarRoundToInt(length / fSegLength); |
Cary Clark | cec40a9 | 2018-03-21 15:27:08 -0400 | [diff] [blame] | 105 | constexpr int kMaxReasonableIterations = 100000; |
| 106 | n = SkTMin(n, kMaxReasonableIterations); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | SkScalar delta = length / n; |
| 108 | SkScalar distance = 0; |
| 109 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 110 | if (meas.isClosed()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | n -= 1; |
| 112 | distance += delta/2; |
| 113 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 114 | |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 115 | if (meas.getPosTan(distance, &p, &v)) { |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 116 | Perterb(&p, v, rand.nextSScalar1() * scale); |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 117 | dst->moveTo(p); |
| 118 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 119 | while (--n >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | distance += delta; |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 121 | if (meas.getPosTan(distance, &p, &v)) { |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 122 | Perterb(&p, v, rand.nextSScalar1() * scale); |
reed@google.com | f3edf9f | 2012-04-12 19:44:38 +0000 | [diff] [blame] | 123 | dst->lineTo(p); |
| 124 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 126 | if (meas.isClosed()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | dst->close(); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 128 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | } |
| 130 | } while (meas.nextContour()); |
| 131 | return true; |
| 132 | } |
| 133 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 134 | sk_sp<SkFlattenable> SkDiscretePathEffect::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 135 | SkScalar segLength = buffer.readScalar(); |
| 136 | SkScalar perterb = buffer.readScalar(); |
| 137 | uint32_t seed = buffer.readUInt(); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 138 | return Make(segLength, perterb, seed); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 139 | } |
| 140 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 141 | void SkDiscretePathEffect::flatten(SkWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | buffer.writeScalar(fSegLength); |
| 143 | buffer.writeScalar(fPerterb); |
rs.prinja | 39e58ad | 2014-06-12 22:55:08 -0700 | [diff] [blame] | 144 | buffer.writeUInt(fSeedAssist); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | } |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 146 | |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 147 | void SkDiscretePathEffect::toString(SkString* str) const { |
| 148 | str->appendf("SkDiscretePathEffect: ("); |
| 149 | str->appendf("segLength: %.2f deviation: %.2f seed %d", fSegLength, fPerterb, fSeedAssist); |
| 150 | str->append(")"); |
| 151 | } |