SkDiscretePathEffect.cpp revision a439334b6e758d38501e225e2e5d0ab73e2fb6eb
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 9 10#include "SkDiscretePathEffect.h" 11#include "SkReadBuffer.h" 12#include "SkWriteBuffer.h" 13#include "SkPathMeasure.h" 14#include "SkStrokeRec.h" 15 16sk_sp<SkPathEffect> SkDiscretePathEffect::Make(SkScalar segLength, SkScalar deviation, 17 uint32_t seedAssist) { 18 return sk_sp<SkPathEffect>(new SkDiscretePathEffect(segLength, deviation, seedAssist)); 19} 20 21static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) { 22 SkVector normal = tangent; 23 normal.rotateCCW(); 24 normal.setLength(scale); 25 *p += normal; 26} 27 28SkDiscretePathEffect::SkDiscretePathEffect(SkScalar segLength, 29 SkScalar deviation, 30 uint32_t seedAssist) 31 : fSegLength(segLength), fPerterb(deviation), fSeedAssist(seedAssist) 32{ 33} 34 35/** \class LCGRandom 36 37 Utility class that implements pseudo random 32bit numbers using a fast 38 linear equation. Unlike rand(), this class holds its own seed (initially 39 set to 0), so that multiple instances can be used with no side-effects. 40 41 Copied from the original implementation of SkRandom. Only contains the 42 methods used by SkDiscretePathEffect::filterPath, with methods that were 43 not called directly moved to private. 44*/ 45 46class LCGRandom { 47public: 48 LCGRandom(uint32_t seed) : fSeed(seed) {} 49 50 /** Return the next pseudo random number expressed as a SkScalar 51 in the range (-SK_Scalar1..SK_Scalar1). 52 */ 53 SkScalar nextSScalar1() { return SkFixedToScalar(this->nextSFixed1()); } 54 55private: 56 /** Return the next pseudo random number as an unsigned 32bit value. 57 */ 58 uint32_t nextU() { uint32_t r = fSeed * kMul + kAdd; fSeed = r; return r; } 59 60 /** Return the next pseudo random number as a signed 32bit value. 61 */ 62 int32_t nextS() { return (int32_t)this->nextU(); } 63 64 /** Return the next pseudo random number expressed as a signed SkFixed 65 in the range (-SK_Fixed1..SK_Fixed1). 66 */ 67 SkFixed nextSFixed1() { return this->nextS() >> 15; } 68 69 // See "Numerical Recipes in C", 1992 page 284 for these constants 70 enum { 71 kMul = 1664525, 72 kAdd = 1013904223 73 }; 74 uint32_t fSeed; 75}; 76 77bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src, 78 SkStrokeRec* rec, const SkRect*) const { 79 bool doFill = rec->isFillStyle(); 80 81 SkPathMeasure meas(src, doFill); 82 83 /* Caller may supply their own seed assist, which by default is 0 */ 84 uint32_t seed = fSeedAssist ^ SkScalarRoundToInt(meas.getLength()); 85 86 LCGRandom rand(seed ^ ((seed << 16) | (seed >> 16))); 87 SkScalar scale = fPerterb; 88 SkPoint p; 89 SkVector v; 90 91 do { 92 SkScalar length = meas.getLength(); 93 94 if (fSegLength * (2 + doFill) > length) { 95 meas.getSegment(0, length, dst, true); // to short for us to mangle 96 } else { 97 int n = SkScalarRoundToInt(length / fSegLength); 98 SkScalar delta = length / n; 99 SkScalar distance = 0; 100 101 if (meas.isClosed()) { 102 n -= 1; 103 distance += delta/2; 104 } 105 106 if (meas.getPosTan(distance, &p, &v)) { 107 Perterb(&p, v, SkScalarMul(rand.nextSScalar1(), scale)); 108 dst->moveTo(p); 109 } 110 while (--n >= 0) { 111 distance += delta; 112 if (meas.getPosTan(distance, &p, &v)) { 113 Perterb(&p, v, SkScalarMul(rand.nextSScalar1(), scale)); 114 dst->lineTo(p); 115 } 116 } 117 if (meas.isClosed()) { 118 dst->close(); 119 } 120 } 121 } while (meas.nextContour()); 122 return true; 123} 124 125SkFlattenable* SkDiscretePathEffect::CreateProc(SkReadBuffer& buffer) { 126 SkScalar segLength = buffer.readScalar(); 127 SkScalar perterb = buffer.readScalar(); 128 uint32_t seed = buffer.readUInt(); 129 return Make(segLength, perterb, seed).release(); 130} 131 132void SkDiscretePathEffect::flatten(SkWriteBuffer& buffer) const { 133 buffer.writeScalar(fSegLength); 134 buffer.writeScalar(fPerterb); 135 buffer.writeUInt(fSeedAssist); 136} 137 138#ifndef SK_IGNORE_TO_STRING 139void SkDiscretePathEffect::toString(SkString* str) const { 140 str->appendf("SkDiscretePathEffect: ("); 141 str->appendf("segLength: %.2f deviation: %.2f seed %d", fSegLength, fPerterb, fSeedAssist); 142 str->append(")"); 143} 144#endif 145