blob: 57d8f9639e34c33170bef3cd9c0dc2c4b5af395b [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"
Cary Clarkdf429f32017-11-08 11:44:31 -050012#include "SkPointPriv.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070013#include "SkReadBuffer.h"
halcanary435657f2015-09-15 12:53:07 -070014#include "SkStrokeRec.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070015#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
reeda4393342016-03-18 11:22:57 -070017sk_sp<SkPathEffect> SkDiscretePathEffect::Make(SkScalar segLength, SkScalar deviation,
18 uint32_t seedAssist) {
reed18c00972016-04-02 18:40:40 -070019 if (!SkScalarIsFinite(segLength) || !SkScalarIsFinite(deviation)) {
20 return nullptr;
21 }
22 if (segLength <= SK_ScalarNearlyZero) {
23 return nullptr;
24 }
reeda4393342016-03-18 11:22:57 -070025 return sk_sp<SkPathEffect>(new SkDiscretePathEffect(segLength, deviation, seedAssist));
26}
27
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000028static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 SkVector normal = tangent;
Cary Clarkdf429f32017-11-08 11:44:31 -050030 SkPointPriv::RotateCCW(&normal);
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 normal.setLength(scale);
32 *p += normal;
33}
34
rs.prinja39e58ad2014-06-12 22:55:08 -070035SkDiscretePathEffect::SkDiscretePathEffect(SkScalar segLength,
36 SkScalar deviation,
37 uint32_t seedAssist)
38 : fSegLength(segLength), fPerterb(deviation), fSeedAssist(seedAssist)
reed@android.com8a1c16f2008-12-17 15:59:43 +000039{
40}
41
scroggof9d61012014-12-15 12:54:51 -080042/** \class LCGRandom
43
44 Utility class that implements pseudo random 32bit numbers using a fast
45 linear equation. Unlike rand(), this class holds its own seed (initially
46 set to 0), so that multiple instances can be used with no side-effects.
47
48 Copied from the original implementation of SkRandom. Only contains the
49 methods used by SkDiscretePathEffect::filterPath, with methods that were
50 not called directly moved to private.
51*/
52
53class LCGRandom {
54public:
55 LCGRandom(uint32_t seed) : fSeed(seed) {}
56
57 /** Return the next pseudo random number expressed as a SkScalar
benjaminwagner12634482016-03-31 06:13:22 -070058 in the range [-SK_Scalar1..SK_Scalar1).
scroggof9d61012014-12-15 12:54:51 -080059 */
60 SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); }
61
62private:
63 /** Return the next pseudo random number as an unsigned 32bit value.
64 */
65 uint32_t nextU() { uint32_t r = fSeed * kMul + kAdd; fSeed = r; return r; }
66
67 /** Return the next pseudo random number as a signed 32bit value.
68 */
69 int32_t nextS() { return (int32_t)this->nextU(); }
70
71 /** Return the next pseudo random number expressed as a signed SkFixed
benjaminwagner12634482016-03-31 06:13:22 -070072 in the range [-SK_Fixed1..SK_Fixed1).
scroggof9d61012014-12-15 12:54:51 -080073 */
74 SkFixed nextSFixed1() { return this->nextS() >> 15; }
75
76 // See "Numerical Recipes in C", 1992 page 284 for these constants
77 enum {
78 kMul = 1664525,
79 kAdd = 1013904223
80 };
81 uint32_t fSeed;
82};
83
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000084bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +000085 SkStrokeRec* rec, const SkRect*) const {
reed@google.comfd4be262012-05-25 01:04:12 +000086 bool doFill = rec->isFillStyle();
reed@android.com8a1c16f2008-12-17 15:59:43 +000087
88 SkPathMeasure meas(src, doFill);
rs.prinja39e58ad2014-06-12 22:55:08 -070089
90 /* Caller may supply their own seed assist, which by default is 0 */
91 uint32_t seed = fSeedAssist ^ SkScalarRoundToInt(meas.getLength());
92
scroggof9d61012014-12-15 12:54:51 -080093 LCGRandom rand(seed ^ ((seed << 16) | (seed >> 16)));
94 SkScalar scale = fPerterb;
95 SkPoint p;
96 SkVector v;
reed@android.com8a1c16f2008-12-17 15:59:43 +000097
98 do {
99 SkScalar length = meas.getLength();
100
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000101 if (fSegLength * (2 + doFill) > length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 meas.getSegment(0, length, dst, true); // to short for us to mangle
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000103 } else {
reed@google.come1ca7052013-12-17 19:22:07 +0000104 int n = SkScalarRoundToInt(length / fSegLength);
Cary Clarkcec40a92018-03-21 15:27:08 -0400105 constexpr int kMaxReasonableIterations = 100000;
106 n = SkTMin(n, kMaxReasonableIterations);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 SkScalar delta = length / n;
108 SkScalar distance = 0;
109
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000110 if (meas.isClosed()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 n -= 1;
112 distance += delta/2;
113 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000114
reed@google.comf3edf9f2012-04-12 19:44:38 +0000115 if (meas.getPosTan(distance, &p, &v)) {
Mike Reed8be952a2017-02-13 20:44:33 -0500116 Perterb(&p, v, rand.nextSScalar1() * scale);
reed@google.comf3edf9f2012-04-12 19:44:38 +0000117 dst->moveTo(p);
118 }
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000119 while (--n >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 distance += delta;
reed@google.comf3edf9f2012-04-12 19:44:38 +0000121 if (meas.getPosTan(distance, &p, &v)) {
Mike Reed8be952a2017-02-13 20:44:33 -0500122 Perterb(&p, v, rand.nextSScalar1() * scale);
reed@google.comf3edf9f2012-04-12 19:44:38 +0000123 dst->lineTo(p);
124 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 }
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000126 if (meas.isClosed()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 dst->close();
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000128 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 }
130 } while (meas.nextContour());
131 return true;
132}
133
reed60c9b582016-04-03 09:11:13 -0700134sk_sp<SkFlattenable> SkDiscretePathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700135 SkScalar segLength = buffer.readScalar();
136 SkScalar perterb = buffer.readScalar();
137 uint32_t seed = buffer.readUInt();
reed60c9b582016-04-03 09:11:13 -0700138 return Make(segLength, perterb, seed);
reed9fa60da2014-08-21 07:59:51 -0700139}
140
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000141void SkDiscretePathEffect::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 buffer.writeScalar(fSegLength);
143 buffer.writeScalar(fPerterb);
rs.prinja39e58ad2014-06-12 22:55:08 -0700144 buffer.writeUInt(fSeedAssist);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145}
robertphillips42dbfa82015-01-26 06:08:52 -0800146
robertphillips42dbfa82015-01-26 06:08:52 -0800147void SkDiscretePathEffect::toString(SkString* str) const {
148 str->appendf("SkDiscretePathEffect: (");
149 str->appendf("segLength: %.2f deviation: %.2f seed %d", fSegLength, fPerterb, fSeedAssist);
150 str->append(")");
151}