From 43398a8a4a18d11196be8a2f4ffc65b53ad92144 Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Tue, 13 Mar 2018 17:52:41 -0400 Subject: [sksg] Use SkTrimPathEffect for path trimming Refactor TrimEffect using SkTrimPathEffect instead of SkDashPathEffect. TBR= Change-Id: I1415b30b58db28cb74b70fb176307634ab0774e8 Reviewed-on: https://skia-review.googlesource.com/114264 Reviewed-by: Florin Malita Commit-Queue: Florin Malita --- experimental/sksg/geometry/SkSGTrimEffect.cpp | 52 ++++++++++++--------------- 1 file changed, 23 insertions(+), 29 deletions(-) (limited to 'experimental') diff --git a/experimental/sksg/geometry/SkSGTrimEffect.cpp b/experimental/sksg/geometry/SkSGTrimEffect.cpp index 195b09ece0..fea8249161 100644 --- a/experimental/sksg/geometry/SkSGTrimEffect.cpp +++ b/experimental/sksg/geometry/SkSGTrimEffect.cpp @@ -8,9 +8,8 @@ #include "SkSGTrimEffect.h" #include "SkCanvas.h" -#include "SkDashPathEffect.h" -#include "SkPathMeasure.h" #include "SkStrokeRec.h" +#include "SkTrimPathEffect.h" namespace sksg { @@ -41,34 +40,29 @@ SkRect TrimEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) SkASSERT(this->hasInval()); const auto childbounds = fChild->revalidate(ic, ctm); + const auto path = fChild->asPath(); - const auto path = fChild->asPath(); - SkScalar pathLen = 0; - SkPathMeasure measure(path, false); - do { - pathLen += measure.getLength(); - } while (measure.nextContour()); - - const auto start = pathLen * fStart, - end = pathLen * fEnd, - offset = pathLen * fOffset, - len = end - start; - - fTrimmedPath.reset(); - - if (len > 0) { - // If the trim is positioned exactly at the beginning of the path, we don't expect any - // visible wrap-around. But due to limited precision / accumulated error, the dash effect - // may sometimes start one extra dash at the very end (a flickering dot during animation). - // To avoid this, we bump the dash len by |epsilon|. - const SkScalar dashes[] = { len, pathLen + SK_ScalarNearlyZero - len }; - auto dashEffect = SkDashPathEffect::Make(dashes, - SK_ARRAY_COUNT(dashes), - -start - offset); - if (dashEffect) { - SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle); - SkAssertResult(dashEffect->filterPath(&fTrimmedPath, path, &rec, &childbounds)); - } + // TODO: relocate these funky semantics to a Skottie composite helper, + // and refactor TrimEffect as a thin SkTrimpPathEffect wrapper. + auto start = SkTMin(fStart, fEnd) + fOffset, + stop = SkTMax(fStart, fEnd) + fOffset; + + sk_sp trim; + if (stop - start < 1) { + start -= SkScalarFloorToScalar(start); + stop -= SkScalarFloorToScalar(stop); + + trim = start <= stop + ? SkTrimPathEffect::Make(start, stop, SkTrimPathEffect::Mode::kNormal) + : SkTrimPathEffect::Make(stop, start, SkTrimPathEffect::Mode::kInverted); + } + + if (trim) { + fTrimmedPath.reset(); + SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle); + SkAssertResult(trim->filterPath(&fTrimmedPath, path, &rec, &childbounds)); + } else { + fTrimmedPath = path; } return fTrimmedPath.computeTightBounds(); -- cgit v1.2.3