Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(377)

Side by Side Diff: src/effects/SkDiscretePathEffect.cpp

Issue 805963002: There can be only one (SkRandom)! (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/utils/SkRandom.h ('k') | tests/StrokerTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkDiscretePathEffect.h" 10 #include "SkDiscretePathEffect.h"
11 #include "SkReadBuffer.h" 11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h" 12 #include "SkWriteBuffer.h"
13 #include "SkPathMeasure.h" 13 #include "SkPathMeasure.h"
14 #include "SkRandom.h"
15 14
16 static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) { 15 static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) {
17 SkVector normal = tangent; 16 SkVector normal = tangent;
18 normal.rotateCCW(); 17 normal.rotateCCW();
19 normal.setLength(scale); 18 normal.setLength(scale);
20 *p += normal; 19 *p += normal;
21 } 20 }
22 21
23 SkDiscretePathEffect::SkDiscretePathEffect(SkScalar segLength, 22 SkDiscretePathEffect::SkDiscretePathEffect(SkScalar segLength,
24 SkScalar deviation, 23 SkScalar deviation,
25 uint32_t seedAssist) 24 uint32_t seedAssist)
26 : fSegLength(segLength), fPerterb(deviation), fSeedAssist(seedAssist) 25 : fSegLength(segLength), fPerterb(deviation), fSeedAssist(seedAssist)
27 { 26 {
28 } 27 }
29 28
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
30 bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src, 71 bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src,
31 SkStrokeRec* rec, const SkRect*) const { 72 SkStrokeRec* rec, const SkRect*) const {
32 bool doFill = rec->isFillStyle(); 73 bool doFill = rec->isFillStyle();
33 74
34 SkPathMeasure meas(src, doFill); 75 SkPathMeasure meas(src, doFill);
35 76
36 /* Caller may supply their own seed assist, which by default is 0 */ 77 /* Caller may supply their own seed assist, which by default is 0 */
37 uint32_t seed = fSeedAssist ^ SkScalarRoundToInt(meas.getLength()); 78 uint32_t seed = fSeedAssist ^ SkScalarRoundToInt(meas.getLength());
38 79
39 SkLCGRandom rand(seed ^ ((seed << 16) | (seed >> 16))); 80 LCGRandom rand(seed ^ ((seed << 16) | (seed >> 16)));
40 SkScalar scale = fPerterb; 81 SkScalar scale = fPerterb;
41 SkPoint p; 82 SkPoint p;
42 SkVector v; 83 SkVector v;
43 84
44 do { 85 do {
45 SkScalar length = meas.getLength(); 86 SkScalar length = meas.getLength();
46 87
47 if (fSegLength * (2 + doFill) > length) { 88 if (fSegLength * (2 + doFill) > length) {
48 meas.getSegment(0, length, dst, true); // to short for us to mangle 89 meas.getSegment(0, length, dst, true); // to short for us to mangle
49 } else { 90 } else {
50 int n = SkScalarRoundToInt(length / fSegLength); 91 int n = SkScalarRoundToInt(length / fSegLength);
51 SkScalar delta = length / n; 92 SkScalar delta = length / n;
52 SkScalar distance = 0; 93 SkScalar distance = 0;
(...skipping 27 matching lines...) Expand all
80 SkScalar perterb = buffer.readScalar(); 121 SkScalar perterb = buffer.readScalar();
81 uint32_t seed = buffer.readUInt(); 122 uint32_t seed = buffer.readUInt();
82 return Create(segLength, perterb, seed); 123 return Create(segLength, perterb, seed);
83 } 124 }
84 125
85 void SkDiscretePathEffect::flatten(SkWriteBuffer& buffer) const { 126 void SkDiscretePathEffect::flatten(SkWriteBuffer& buffer) const {
86 buffer.writeScalar(fSegLength); 127 buffer.writeScalar(fSegLength);
87 buffer.writeScalar(fPerterb); 128 buffer.writeScalar(fPerterb);
88 buffer.writeUInt(fSeedAssist); 129 buffer.writeUInt(fSeedAssist);
89 } 130 }
OLDNEW
« no previous file with comments | « include/utils/SkRandom.h ('k') | tests/StrokerTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698