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