2018-01-07 13:54:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkSGTrimEffect.h"
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDashPathEffect.h"
|
|
|
|
#include "SkPathMeasure.h"
|
2018-03-07 21:37:38 +00:00
|
|
|
#include "SkStrokeRec.h"
|
2018-01-07 13:54:24 +00:00
|
|
|
|
|
|
|
namespace sksg {
|
|
|
|
|
|
|
|
TrimEffect::TrimEffect(sk_sp<GeometryNode> child)
|
|
|
|
: fChild(std::move(child)) {
|
2018-01-22 15:19:28 +00:00
|
|
|
this->observeInval(fChild);
|
2018-01-07 13:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TrimEffect::~TrimEffect() {
|
2018-01-22 15:19:28 +00:00
|
|
|
this->unobserveInval(fChild);
|
2018-01-07 13:54:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 21:31:14 +00:00
|
|
|
void TrimEffect::onClip(SkCanvas* canvas, bool antiAlias) const {
|
2018-03-07 21:37:38 +00:00
|
|
|
canvas->clipPath(fTrimmedPath, SkClipOp::kIntersect, antiAlias);
|
2018-01-29 21:31:14 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 13:54:24 +00:00
|
|
|
void TrimEffect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
|
|
|
|
SkASSERT(!paint.getPathEffect());
|
|
|
|
|
2018-03-07 21:37:38 +00:00
|
|
|
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);
|
|
|
|
|
2018-01-07 13:54:24 +00:00
|
|
|
const auto path = fChild->asPath();
|
|
|
|
SkScalar pathLen = 0;
|
|
|
|
SkPathMeasure measure(path, false);
|
|
|
|
do {
|
|
|
|
pathLen += measure.getLength();
|
|
|
|
} while (measure.nextContour());
|
|
|
|
|
2018-01-23 04:13:10 +00:00
|
|
|
const auto start = pathLen * fStart,
|
|
|
|
end = pathLen * fEnd,
|
|
|
|
offset = pathLen * fOffset,
|
2018-01-21 16:47:22 +00:00
|
|
|
len = end - start;
|
2018-01-07 13:54:24 +00:00
|
|
|
|
2018-03-07 21:37:38 +00:00
|
|
|
fTrimmedPath.reset();
|
2018-01-07 13:54:24 +00:00
|
|
|
|
2018-03-07 21:37:38 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 13:54:24 +00:00
|
|
|
|
2018-03-07 21:37:38 +00:00
|
|
|
return fTrimmedPath.computeTightBounds();
|
2018-01-07 13:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sksg
|