| 1 |
/* |
| 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 |
|
| 8 |
#include "include/effects/Sk1DPathEffect.h" |
| 9 |
|
| 10 |
#include "include/core/SkFlattenable.h" |
| 11 |
#include "include/core/SkMatrix.h" |
| 12 |
#include "include/core/SkPath.h" |
| 13 |
#include "include/core/SkPathBuilder.h" |
| 14 |
#include "include/core/SkPathEffect.h" |
| 15 |
#include "include/core/SkPathMeasure.h" |
| 16 |
#include "include/core/SkPoint.h" |
| 17 |
#include "include/core/SkRefCnt.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/core/SkStrokeRec.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "include/private/base/SkFloatingPoint.h" |
| 22 |
#include "src/core/SkPathEffectBase.h" |
| 23 |
#include "src/core/SkReadBuffer.h" |
| 24 |
#include "src/core/SkWriteBuffer.h" |
| 25 |
|
| 26 |
struct SkRect; |
| 27 |
|
| 28 |
// Since we are stepping by a float, the do/while loop might go on forever (or nearly so). |
| 29 |
// Put in a governor to limit crash values from looping too long (and allocating too much ram). |
| 30 |
#define MAX_REASONABLE_ITERATIONS 100000 |
| 31 |
|
| 32 |
class Sk1DPathEffect : public SkPathEffectBase { |
| 33 |
public: |
| 34 |
protected: |
| 35 |
bool onFilterPath(SkPathBuilder* builder, const SkPath& src, SkStrokeRec*, const SkRect*, |
| 36 |
const SkMatrix&) const override { |
| 37 |
SkPathMeasure meas(src, false); |
| 38 |
do { |
| 39 |
int governor = MAX_REASONABLE_ITERATIONS; |
| 40 |
SkScalar length = meas.getLength(); |
| 41 |
SkScalar distance = this->begin(length); |
| 42 |
while (distance < length && --governor >= 0) { |
| 43 |
SkScalar delta = this->next(builder, distance, meas); |
| 44 |
if (delta <= 0) { |
| 45 |
break; |
| 46 |
} |
| 47 |
distance += delta; |
| 48 |
} |
| 49 |
if (governor < 0) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
} while (meas.nextContour()); |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
/** Called at the start of each contour, returns the initial offset |
| 57 |
into that contour. |
| 58 |
*/ |
| 59 |
virtual SkScalar begin(SkScalar contourLength) const = 0; |
| 60 |
/** Called with the current distance along the path, with the current matrix |
| 61 |
for the point/tangent at the specified distance. |
| 62 |
Return the distance to travel for the next call. If return <= 0, then that |
| 63 |
contour is done. |
| 64 |
*/ |
| 65 |
virtual SkScalar next(SkPathBuilder* dst, SkScalar dist, SkPathMeasure&) const = 0; |
| 66 |
|
| 67 |
private: |
| 68 |
// For simplicity, assume fast bounds cannot be computed |
| 69 |
bool computeFastBounds(SkRect*) const override { return false; } |
| 70 |
}; |
| 71 |
|
| 72 |
/////////////////////////////////////////////////////////////////////////////// |
| 73 |
|
| 74 |
class SkPath1DPathEffectImpl : public Sk1DPathEffect { |
| 75 |
public: |
| 76 |
SkPath1DPathEffectImpl(const SkPath& path, SkScalar advance, SkScalar phase, |
| 77 |
SkPath1DPathEffect::Style style) : fPath(path) { |
| 78 |
SkASSERT(advance > 0 && !path.isEmpty()); |
| 79 |
|
| 80 |
// Make the path thread-safe. |
| 81 |
fPath.updateBoundsCache(); |
| 82 |
(void)fPath.getGenerationID(); |
| 83 |
|
| 84 |
// cleanup their phase parameter, inverting it so that it becomes an |
| 85 |
// offset along the path (to match the interpretation in PostScript) |
| 86 |
if (phase < 0) { |
| 87 |
phase = -phase; |
| 88 |
if (phase > advance) { |
| 89 |
phase = SkScalarMod(phase, advance); |
| 90 |
} |
| 91 |
} else { |
| 92 |
if (phase > advance) { |
| 93 |
phase = SkScalarMod(phase, advance); |
| 94 |
} |
| 95 |
phase = advance - phase; |
| 96 |
} |
| 97 |
// now catch the edge case where phase == advance (within epsilon) |
| 98 |
if (phase >= advance) { |
| 99 |
phase = 0; |
| 100 |
} |
| 101 |
SkASSERT(phase >= 0); |
| 102 |
|
| 103 |
fAdvance = advance; |
| 104 |
fInitialOffset = phase; |
| 105 |
fStyle = style; |
| 106 |
} |
| 107 |
|
| 108 |
bool onFilterPath(SkPathBuilder* builder, const SkPath& src, SkStrokeRec* rec, |
| 109 |
const SkRect* cullRect, const SkMatrix& ctm) const override { |
| 110 |
rec->setFillStyle(); |
| 111 |
return this->INHERITED::onFilterPath(builder, src, rec, cullRect, ctm); |
| 112 |
} |
| 113 |
|
| 114 |
SkScalar begin(SkScalar contourLength) const override { |
| 115 |
return fInitialOffset; |
| 116 |
} |
| 117 |
|
| 118 |
SkScalar next(SkPathBuilder*, SkScalar, SkPathMeasure&) const override; |
| 119 |
|
| 120 |
static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) { |
| 121 |
sk_sp<SkFlattenable> result; |
| 122 |
|
| 123 |
SkScalar advance = buffer.readScalar(); |
| 124 |
if (auto path = buffer.readPath()) { |
| 125 |
SkScalar phase = buffer.readScalar(); |
| 126 |
SkPath1DPathEffect::Style style = buffer.read32LE(SkPath1DPathEffect::kLastEnum_Style); |
| 127 |
if (buffer.isValid()) { |
| 128 |
result = SkPath1DPathEffect::Make(*path, advance, phase, style); |
| 129 |
} |
| 130 |
} |
| 131 |
return result; |
| 132 |
} |
| 133 |
|
| 134 |
void flatten(SkWriteBuffer& buffer) const override { |
| 135 |
buffer.writeScalar(fAdvance); |
| 136 |
buffer.writePath(fPath); |
| 137 |
buffer.writeScalar(fInitialOffset); |
| 138 |
buffer.writeUInt(fStyle); |
| 139 |
} |
| 140 |
|
| 141 |
Factory getFactory() const override { return CreateProc; } |
| 142 |
const char* getTypeName() const override { return "SkPath1DPathEffect"; } |
| 143 |
|
| 144 |
private: |
| 145 |
SkPath fPath; // copied from constructor |
| 146 |
SkScalar fAdvance; // copied from constructor |
| 147 |
SkScalar fInitialOffset; // computed from phase |
| 148 |
SkPath1DPathEffect::Style fStyle; // copied from constructor |
| 149 |
|
| 150 |
using INHERITED = Sk1DPathEffect; |
| 151 |
}; |
| 152 |
|
| 153 |
static bool morphpoints(SkSpan<SkPoint> dst, SkSpan<const SkPoint> src, |
| 154 |
SkPathMeasure& meas, SkScalar dist) { |
| 155 |
SkASSERT(dst.size() >= src.size()); |
| 156 |
for (size_t i = 0; i < src.size(); i++) { |
| 157 |
SkPoint pos; |
| 158 |
SkVector tangent; |
| 159 |
|
| 160 |
SkScalar sx = src[i].fX; |
| 161 |
SkScalar sy = src[i].fY; |
| 162 |
|
| 163 |
if (!meas.getPosTan(dist + sx, &pos, &tangent)) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
SkMatrix matrix; |
| 168 |
SkPoint pt; |
| 169 |
|
| 170 |
pt.set(sx, sy); |
| 171 |
matrix.setSinCos(tangent.fY, tangent.fX, 0, 0); |
| 172 |
matrix.preTranslate(-sx, 0); |
| 173 |
matrix.postTranslate(pos.fX, pos.fY); |
| 174 |
dst[i] = matrix.mapPoint(pt); |
| 175 |
} |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
/* TODO |
| 180 |
|
| 181 |
Need differentially more subdivisions when the follow-path is curvy. Not sure how to |
| 182 |
determine that, but we need it. I guess a cheap answer is let the caller tell us, |
| 183 |
but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out. |
| 184 |
*/ |
| 185 |
static void morphpath(SkPathBuilder* dst, const SkPath& src, SkPathMeasure& meas, |
| 186 |
SkScalar dist) { |
| 187 |
SkPath::Iter iter(src, false); |
| 188 |
SkPoint dstP[3], scratch[3]; |
| 189 |
|
| 190 |
while (auto rec = iter.next()) { |
| 191 |
SkSpan<const SkPoint> srcP = rec->fPoints; |
| 192 |
switch (rec->fVerb) { |
| 193 |
case SkPathVerb::kMove: |
| 194 |
if (morphpoints(dstP, srcP, meas, dist)) { |
| 195 |
dst->moveTo(dstP[0]); |
| 196 |
} |
| 197 |
break; |
| 198 |
case SkPathVerb::kLine: |
| 199 |
scratch[0] = srcP[0]; |
| 200 |
scratch[1].set(sk_float_midpoint(srcP[0].fX, srcP[1].fX), |
| 201 |
sk_float_midpoint(srcP[0].fY, srcP[1].fY)); |
| 202 |
scratch[2] = srcP[1]; |
| 203 |
srcP = scratch; // now we look like a quad |
| 204 |
[[fallthrough]]; |
| 205 |
case SkPathVerb::kQuad: |
| 206 |
if (morphpoints(dstP, srcP.subspan(1), meas, dist)) { |
| 207 |
dst->quadTo(dstP[0], dstP[1]); |
| 208 |
} |
| 209 |
break; |
| 210 |
case SkPathVerb::kConic: |
| 211 |
if (morphpoints(dstP, srcP.subspan(1), meas, dist)) { |
| 212 |
dst->conicTo(dstP[0], dstP[1], rec->conicWeight()); |
| 213 |
} |
| 214 |
break; |
| 215 |
case SkPathVerb::kCubic: |
| 216 |
if (morphpoints(dstP, srcP.subspan(1), meas, dist)) { |
| 217 |
dst->cubicTo(dstP[0], dstP[1], dstP[2]); |
| 218 |
} |
| 219 |
break; |
| 220 |
case SkPathVerb::kClose: |
| 221 |
dst->close(); |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
SkScalar SkPath1DPathEffectImpl::next(SkPathBuilder* builder, SkScalar distance, |
| 228 |
SkPathMeasure& meas) const { |
| 229 |
#if defined(SK_BUILD_FOR_FUZZER) |
| 230 |
if (builder->countPoints() > 100000) { |
| 231 |
return fAdvance; |
| 232 |
} |
| 233 |
#endif |
| 234 |
switch (fStyle) { |
| 235 |
case SkPath1DPathEffect::kTranslate_Style: { |
| 236 |
SkPoint pos; |
| 237 |
if (meas.getPosTan(distance, &pos, nullptr)) { |
| 238 |
builder->addPath(fPath, pos.fX, pos.fY); |
| 239 |
} |
| 240 |
} break; |
| 241 |
case SkPath1DPathEffect::kRotate_Style: { |
| 242 |
SkMatrix matrix; |
| 243 |
if (meas.getMatrix(distance, &matrix)) { |
| 244 |
builder->addPath(fPath, matrix); |
| 245 |
} |
| 246 |
} break; |
| 247 |
case SkPath1DPathEffect::kMorph_Style: |
| 248 |
morphpath(builder, fPath, meas, distance); |
| 249 |
break; |
| 250 |
} |
| 251 |
return fAdvance; |
| 252 |
} |
| 253 |
|
| 254 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 255 |
|
| 256 |
sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase, |
| 257 |
Style style) { |
| 258 |
if (advance <= 0 || !SkIsFinite(advance, phase) || path.isEmpty()) { |
| 259 |
return nullptr; |
| 260 |
} |
| 261 |
return sk_sp<SkPathEffect>(new SkPath1DPathEffectImpl(path, advance, phase, style)); |
| 262 |
} |
| 263 |
|
| 264 |
void SkPath1DPathEffect::RegisterFlattenables() { |
| 265 |
SK_REGISTER_FLATTENABLE(SkPath1DPathEffectImpl); |
| 266 |
} |