blob: b9d76f5b138e2a51b12de0528fba8954f7c4b19f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com8a1c16f2008-12-17 15:59:43 +00008
9#include "SkDiscretePathEffect.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070010#include "SkFixed.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkPathMeasure.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070012#include "SkReadBuffer.h"
halcanary435657f2015-09-15 12:53:07 -070013#include "SkStrokeRec.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070014#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
reeda4393342016-03-18 11:22:57 -070016sk_sp<SkPathEffect> SkDiscretePathEffect::Make(SkScalar segLength, SkScalar deviation,
17 uint32_t seedAssist) {
reed18c00972016-04-02 18:40:40 -070018 if (!SkScalarIsFinite(segLength) || !SkScalarIsFinite(deviation)) {
19 return nullptr;
20 }
21 if (segLength <= SK_ScalarNearlyZero) {
22 return nullptr;
23 }
reeda4393342016-03-18 11:22:57 -070024 return sk_sp<SkPathEffect>(new SkDiscretePathEffect(segLength, deviation, seedAssist));
25}
26
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000027static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 SkVector normal = tangent;
29 normal.rotateCCW();
30 normal.setLength(scale);
31 *p += normal;
32}
33
rs.prinja39e58ad2014-06-12 22:55:08 -070034SkDiscretePathEffect::SkDiscretePathEffect(SkScalar segLength,
35 SkScalar deviation,
36 uint32_t seedAssist)
37 : fSegLength(segLength), fPerterb(deviation), fSeedAssist(seedAssist)
reed@android.com8a1c16f2008-12-17 15:59:43 +000038{
39}
40
scroggof9d61012014-12-15 12:54:51 -080041/** \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
52class LCGRandom {
53public:
54 LCGRandom(uint32_t seed) : fSeed(seed) {}
55
56 /** Return the next pseudo random number expressed as a SkScalar
benjaminwagner12634482016-03-31 06:13:22 -070057 in the range [-SK_Scalar1..SK_Scalar1).
scroggof9d61012014-12-15 12:54:51 -080058 */
59 SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); }
60
61private:
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
benjaminwagner12634482016-03-31 06:13:22 -070071 in the range [-SK_Fixed1..SK_Fixed1).
scroggof9d61012014-12-15 12:54:51 -080072 */
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.org3334c3a2011-04-20 11:39:28 +000083bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000084 SkStrokeRec* rec, const SkRect*) const {
reed@google.comfd4be262012-05-25 01:04:12 +000085 bool doFill = rec->isFillStyle();
reed@android.com8a1c16f2008-12-17 15:59:43 +000086
87 SkPathMeasure meas(src, doFill);
rs.prinja39e58ad2014-06-12 22:55:08 -070088
89 /* Caller may supply their own seed assist, which by default is 0 */
90 uint32_t seed = fSeedAssist ^ SkScalarRoundToInt(meas.getLength());
91
scroggof9d61012014-12-15 12:54:51 -080092 LCGRandom rand(seed ^ ((seed << 16) | (seed >> 16)));
93 SkScalar scale = fPerterb;
94 SkPoint p;
95 SkVector v;
reed@android.com8a1c16f2008-12-17 15:59:43 +000096
97 do {
98 SkScalar length = meas.getLength();
99
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000100 if (fSegLength * (2 + doFill) > length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 meas.getSegment(0, length, dst, true); // to short for us to mangle
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000102 } else {
reed@google.come1ca7052013-12-17 19:22:07 +0000103 int n = SkScalarRoundToInt(length / fSegLength);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 SkScalar delta = length / n;
105 SkScalar distance = 0;
106
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000107 if (meas.isClosed()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 n -= 1;
109 distance += delta/2;
110 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000111
reed@google.comf3edf9f2012-04-12 19:44:38 +0000112 if (meas.getPosTan(distance, &p, &v)) {
Mike Reed8be952a2017-02-13 20:44:33 -0500113 Perterb(&p, v, rand.nextSScalar1() * scale);
reed@google.comf3edf9f2012-04-12 19:44:38 +0000114 dst->moveTo(p);
115 }
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000116 while (--n >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 distance += delta;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000118 if (meas.getPosTan(distance, &p, &v)) {
Mike Reed8be952a2017-02-13 20:44:33 -0500119 Perterb(&p, v, rand.nextSScalar1() * scale);
reed@google.comf3edf9f2012-04-12 19:44:38 +0000120 dst->lineTo(p);
121 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 }
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000123 if (meas.isClosed()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 dst->close();
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000125 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 }
127 } while (meas.nextContour());
128 return true;
129}
130
reed60c9b582016-04-03 09:11:13 -0700131sk_sp<SkFlattenable> SkDiscretePathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700132 SkScalar segLength = buffer.readScalar();
133 SkScalar perterb = buffer.readScalar();
134 uint32_t seed = buffer.readUInt();
reed60c9b582016-04-03 09:11:13 -0700135 return Make(segLength, perterb, seed);
reed9fa60da2014-08-21 07:59:51 -0700136}
137
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000138void SkDiscretePathEffect::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 buffer.writeScalar(fSegLength);
140 buffer.writeScalar(fPerterb);
rs.prinja39e58ad2014-06-12 22:55:08 -0700141 buffer.writeUInt(fSeedAssist);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142}
robertphillips42dbfa82015-01-26 06:08:52 -0800143
144#ifndef SK_IGNORE_TO_STRING
145void 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