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