From 19f292247ef1a16bfff49d14f44c97fb030c40cc Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Wed, 7 Mar 2018 16:37:38 -0500 Subject: [sksg] Cache transformed TrimEffect geometry Apply the effect at revalidation time, and cache the result. TBR= Change-Id: I166fc0e4e2869bea51e5e45e5a2a50df2f034691 Reviewed-on: https://skia-review.googlesource.com/112801 Reviewed-by: Florin Malita Commit-Queue: Florin Malita --- experimental/sksg/geometry/SkSGTrimEffect.cpp | 52 +++++++++++++++------------ experimental/sksg/geometry/SkSGTrimEffect.h | 1 + 2 files changed, 31 insertions(+), 22 deletions(-) (limited to 'experimental') diff --git a/experimental/sksg/geometry/SkSGTrimEffect.cpp b/experimental/sksg/geometry/SkSGTrimEffect.cpp index 0b664fd436..195b09ece0 100644 --- a/experimental/sksg/geometry/SkSGTrimEffect.cpp +++ b/experimental/sksg/geometry/SkSGTrimEffect.cpp @@ -10,6 +10,7 @@ #include "SkCanvas.h" #include "SkDashPathEffect.h" #include "SkPathMeasure.h" +#include "SkStrokeRec.h" namespace sksg { @@ -23,15 +24,24 @@ TrimEffect::~TrimEffect() { } void TrimEffect::onClip(SkCanvas* canvas, bool antiAlias) const { - canvas->clipPath(fChild->asPath(), SkClipOp::kIntersect, antiAlias); + canvas->clipPath(fTrimmedPath, SkClipOp::kIntersect, antiAlias); } -// TODO -// This is a quick hack to get something on the screen. What we really want here is to apply -// the geometry transformation and cache the result on revalidation. Or an SkTrimPathEffect. void TrimEffect::onDraw(SkCanvas* canvas, const SkPaint& paint) const { SkASSERT(!paint.getPathEffect()); + canvas->drawPath(fTrimmedPath, paint); +} + +SkPath TrimEffect::onAsPath() const { + return fTrimmedPath; +} + +SkRect TrimEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) { + SkASSERT(this->hasInval()); + + const auto childbounds = fChild->revalidate(ic, ctm); + const auto path = fChild->asPath(); SkScalar pathLen = 0; SkPathMeasure measure(path, false); @@ -44,26 +54,24 @@ void TrimEffect::onDraw(SkCanvas* canvas, const SkPaint& paint) const { offset = pathLen * fOffset, len = end - start; - if (len <= 0) { - return; - } - - const SkScalar dashes[] = { len, pathLen - len }; - SkPaint dashedPaint(paint); - dashedPaint.setPathEffect(SkDashPathEffect::Make(dashes, - SK_ARRAY_COUNT(dashes), - -start - offset)); + fTrimmedPath.reset(); - canvas->drawPath(path, dashedPaint); -} - -SkPath TrimEffect::onAsPath() const { - return fChild->asPath(); -} + 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)); + } + } -SkRect TrimEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) { - SkASSERT(this->hasInval()); - return fChild->revalidate(ic, ctm); + return fTrimmedPath.computeTightBounds(); } } // namespace sksg diff --git a/experimental/sksg/geometry/SkSGTrimEffect.h b/experimental/sksg/geometry/SkSGTrimEffect.h index e86ede83e6..4e950a7ade 100644 --- a/experimental/sksg/geometry/SkSGTrimEffect.h +++ b/experimental/sksg/geometry/SkSGTrimEffect.h @@ -44,6 +44,7 @@ private: const sk_sp fChild; + SkPath fTrimmedPath; SkScalar fStart = 0, // starting t fEnd = 1, // ending t fOffset = 0; // t offset -- cgit v1.2.3