blob: ac84a600e3ddb0726916a8bf16dafeac82dd6599 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkDiscretePathEffect.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
12#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkPathMeasure.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000015static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000016 SkVector normal = tangent;
17 normal.rotateCCW();
18 normal.setLength(scale);
19 *p += normal;
20}
21
rs.prinja39e58ad2014-06-12 22:55:08 -070022SkDiscretePathEffect::SkDiscretePathEffect(SkScalar segLength,
23 SkScalar deviation,
24 uint32_t seedAssist)
25 : fSegLength(segLength), fPerterb(deviation), fSeedAssist(seedAssist)
reed@android.com8a1c16f2008-12-17 15:59:43 +000026{
27}
28
scroggof9d61012014-12-15 12:54:51 -080029/** \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
40class LCGRandom {
41public:
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
49private:
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.org3334c3a2011-04-20 11:39:28 +000071bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000072 SkStrokeRec* rec, const SkRect*) const {
reed@google.comfd4be262012-05-25 01:04:12 +000073 bool doFill = rec->isFillStyle();
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
75 SkPathMeasure meas(src, doFill);
rs.prinja39e58ad2014-06-12 22:55:08 -070076
77 /* Caller may supply their own seed assist, which by default is 0 */
78 uint32_t seed = fSeedAssist ^ SkScalarRoundToInt(meas.getLength());
79
scroggof9d61012014-12-15 12:54:51 -080080 LCGRandom rand(seed ^ ((seed << 16) | (seed >> 16)));
81 SkScalar scale = fPerterb;
82 SkPoint p;
83 SkVector v;
reed@android.com8a1c16f2008-12-17 15:59:43 +000084
85 do {
86 SkScalar length = meas.getLength();
87
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000088 if (fSegLength * (2 + doFill) > length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 meas.getSegment(0, length, dst, true); // to short for us to mangle
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000090 } else {
reed@google.come1ca7052013-12-17 19:22:07 +000091 int n = SkScalarRoundToInt(length / fSegLength);
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 SkScalar delta = length / n;
93 SkScalar distance = 0;
94
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000095 if (meas.isClosed()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 n -= 1;
97 distance += delta/2;
98 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000099
reed@google.comf3edf9f2012-04-12 19:44:38 +0000100 if (meas.getPosTan(distance, &p, &v)) {
101 Perterb(&p, v, SkScalarMul(rand.nextSScalar1(), scale));
102 dst->moveTo(p);
103 }
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000104 while (--n >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 distance += delta;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000106 if (meas.getPosTan(distance, &p, &v)) {
107 Perterb(&p, v, SkScalarMul(rand.nextSScalar1(), scale));
108 dst->lineTo(p);
109 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 }
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000111 if (meas.isClosed()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 dst->close();
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000113 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 }
115 } while (meas.nextContour());
116 return true;
117}
118
reed9fa60da2014-08-21 07:59:51 -0700119SkFlattenable* 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.org8b0e8ac2014-01-30 18:58:24 +0000126void SkDiscretePathEffect::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 buffer.writeScalar(fSegLength);
128 buffer.writeScalar(fPerterb);
rs.prinja39e58ad2014-06-12 22:55:08 -0700129 buffer.writeUInt(fSeedAssist);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130}
robertphillips42dbfa82015-01-26 06:08:52 -0800131
132#ifndef SK_IGNORE_TO_STRING
133void 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