Just expose factories for patheffects
bug: skia:11957
Change-Id: If2983fcd1b520a7ae77650d7e5ab226af9db52e0
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/410782
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Tyler Denniston <tdenniston@google.com>
diff --git a/src/effects/Sk1DPathEffect.cpp b/src/effects/Sk1DPathEffect.cpp
index 24288c0..00fa542 100644
--- a/src/effects/Sk1DPathEffect.cpp
+++ b/src/effects/Sk1DPathEffect.cpp
@@ -16,68 +16,119 @@
// Put in a governor to limit crash values from looping too long (and allocating too much ram).
#define MAX_REASONABLE_ITERATIONS 100000
-bool Sk1DPathEffect::onFilterPath(SkPath* dst, const SkPath& src,
- SkStrokeRec*, const SkRect*) const {
- SkPathMeasure meas(src, false);
- do {
- int governor = MAX_REASONABLE_ITERATIONS;
- SkScalar length = meas.getLength();
- SkScalar distance = this->begin(length);
- while (distance < length && --governor >= 0) {
- SkScalar delta = this->next(dst, distance, meas);
- if (delta <= 0) {
- break;
+class Sk1DPathEffect : public SkPathEffect {
+public:
+protected:
+ bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec*, const SkRect*) const override {
+ SkPathMeasure meas(src, false);
+ do {
+ int governor = MAX_REASONABLE_ITERATIONS;
+ SkScalar length = meas.getLength();
+ SkScalar distance = this->begin(length);
+ while (distance < length && --governor >= 0) {
+ SkScalar delta = this->next(dst, distance, meas);
+ if (delta <= 0) {
+ break;
+ }
+ distance += delta;
}
- distance += delta;
- }
- } while (meas.nextContour());
- return true;
-}
+ } while (meas.nextContour());
+ return true;
+ }
+
+ /** Called at the start of each contour, returns the initial offset
+ into that contour.
+ */
+ virtual SkScalar begin(SkScalar contourLength) const = 0;
+ /** Called with the current distance along the path, with the current matrix
+ for the point/tangent at the specified distance.
+ Return the distance to travel for the next call. If return <= 0, then that
+ contour is done.
+ */
+ virtual SkScalar next(SkPath* dst, SkScalar dist, SkPathMeasure&) const = 0;
+
+private:
+ // For simplicity, assume fast bounds cannot be computed
+ bool computeFastBounds(SkRect*) const override { return false; }
+
+ using INHERITED = SkPathEffect;
+};
///////////////////////////////////////////////////////////////////////////////
-SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance, SkScalar phase,
- Style style) : fPath(path) {
- SkASSERT(advance > 0 && !path.isEmpty());
- SkASSERT((unsigned)style <= kMorph_Style);
+class SkPath1DPathEffectImpl : public Sk1DPathEffect {
+public:
+ SkPath1DPathEffectImpl(const SkPath& path, SkScalar advance, SkScalar phase,
+ SkPath1DPathEffect::Style style) : fPath(path) {
+ SkASSERT(advance > 0 && !path.isEmpty());
- // Make the path thread-safe.
- fPath.updateBoundsCache();
- (void)fPath.getGenerationID();
+ // Make the path thread-safe.
+ fPath.updateBoundsCache();
+ (void)fPath.getGenerationID();
- // cleanup their phase parameter, inverting it so that it becomes an
- // offset along the path (to match the interpretation in PostScript)
- if (phase < 0) {
- phase = -phase;
- if (phase > advance) {
- phase = SkScalarMod(phase, advance);
+ // cleanup their phase parameter, inverting it so that it becomes an
+ // offset along the path (to match the interpretation in PostScript)
+ if (phase < 0) {
+ phase = -phase;
+ if (phase > advance) {
+ phase = SkScalarMod(phase, advance);
+ }
+ } else {
+ if (phase > advance) {
+ phase = SkScalarMod(phase, advance);
+ }
+ phase = advance - phase;
}
- } else {
- if (phase > advance) {
- phase = SkScalarMod(phase, advance);
+ // now catch the edge case where phase == advance (within epsilon)
+ if (phase >= advance) {
+ phase = 0;
}
- phase = advance - phase;
- }
- // now catch the edge case where phase == advance (within epsilon)
- if (phase >= advance) {
- phase = 0;
- }
- SkASSERT(phase >= 0);
+ SkASSERT(phase >= 0);
- fAdvance = advance;
- fInitialOffset = phase;
-
- if ((unsigned)style > kMorph_Style) {
- SkDEBUGF("SkPath1DPathEffect style enum out of range %d\n", style);
+ fAdvance = advance;
+ fInitialOffset = phase;
+ fStyle = style;
}
- fStyle = style;
-}
-bool SkPath1DPathEffect::onFilterPath(SkPath* dst, const SkPath& src,
- SkStrokeRec* rec, const SkRect* cullRect) const {
- rec->setFillStyle();
- return this->INHERITED::onFilterPath(dst, src, rec, cullRect);
-}
+ bool onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
+ const SkRect* cullRect) const override {
+ rec->setFillStyle();
+ return this->INHERITED::onFilterPath(dst, src, rec, cullRect);
+ }
+
+ SkScalar begin(SkScalar contourLength) const override {
+ return fInitialOffset;
+ }
+
+ SkScalar next(SkPath*, SkScalar, SkPathMeasure&) const override;
+
+ static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) {
+ SkScalar advance = buffer.readScalar();
+ SkPath path;
+ buffer.readPath(&path);
+ SkScalar phase = buffer.readScalar();
+ SkPath1DPathEffect::Style style = buffer.read32LE(SkPath1DPathEffect::kLastEnum_Style);
+ return buffer.isValid() ? SkPath1DPathEffect::Make(path, advance, phase, style) : nullptr;
+ }
+
+ void flatten(SkWriteBuffer& buffer) const override {
+ buffer.writeScalar(fAdvance);
+ buffer.writePath(fPath);
+ buffer.writeScalar(fInitialOffset);
+ buffer.writeUInt(fStyle);
+ }
+
+ Factory getFactory() const override { return CreateProc; }
+ const char* getTypeName() const override { return "SkPath1DPathEffect"; }
+
+private:
+ SkPath fPath; // copied from constructor
+ SkScalar fAdvance; // copied from constructor
+ SkScalar fInitialOffset; // computed from phase
+ SkPath1DPathEffect::Style fStyle; // copied from constructor
+
+ using INHERITED = Sk1DPathEffect;
+};
static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
SkPathMeasure& meas, SkScalar dist) {
@@ -153,52 +204,29 @@
}
}
-SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
- return fInitialOffset;
-}
-
-sk_sp<SkFlattenable> SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
- SkScalar advance = buffer.readScalar();
- SkPath path;
- buffer.readPath(&path);
- SkScalar phase = buffer.readScalar();
- Style style = buffer.read32LE(kLastEnum_Style);
- return buffer.isValid() ? SkPath1DPathEffect::Make(path, advance, phase, style) : nullptr;
-}
-
-void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
- buffer.writeScalar(fAdvance);
- buffer.writePath(fPath);
- buffer.writeScalar(fInitialOffset);
- buffer.writeUInt(fStyle);
-}
-
-SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
- SkPathMeasure& meas) const {
+SkScalar SkPath1DPathEffectImpl::next(SkPath* dst, SkScalar distance,
+ SkPathMeasure& meas) const {
#if defined(SK_BUILD_FOR_FUZZER)
if (dst->countPoints() > 100000) {
return fAdvance;
}
#endif
switch (fStyle) {
- case kTranslate_Style: {
+ case SkPath1DPathEffect::kTranslate_Style: {
SkPoint pos;
if (meas.getPosTan(distance, &pos, nullptr)) {
dst->addPath(fPath, pos.fX, pos.fY);
}
} break;
- case kRotate_Style: {
+ case SkPath1DPathEffect::kRotate_Style: {
SkMatrix matrix;
if (meas.getMatrix(distance, &matrix)) {
dst->addPath(fPath, matrix);
}
} break;
- case kMorph_Style:
+ case SkPath1DPathEffect::kMorph_Style:
morphpath(dst, fPath, meas, distance);
break;
- default:
- SkDEBUGFAIL("unknown Style enum");
- break;
}
return fAdvance;
}
@@ -210,5 +238,9 @@
if (advance <= 0 || !SkScalarIsFinite(advance) || !SkScalarIsFinite(phase) || path.isEmpty()) {
return nullptr;
}
- return sk_sp<SkPathEffect>(new SkPath1DPathEffect(path, advance, phase, style));
+ return sk_sp<SkPathEffect>(new SkPath1DPathEffectImpl(path, advance, phase, style));
+}
+
+void SkPath1DPathEffect::RegisterFlattenables() {
+ SK_REGISTER_FLATTENABLE(SkPath1DPathEffectImpl);
}