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"
|
|
|
|
|
|
|
|
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 {
|
|
|
|
canvas->clipPath(fChild->asPath(), SkClipOp::kIntersect, antiAlias);
|
|
|
|
}
|
|
|
|
|
2018-01-07 13:54:24 +00:00
|
|
|
// 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());
|
|
|
|
|
|
|
|
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-01-21 16:47:22 +00:00
|
|
|
if (len <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkScalar dashes[] = { len, pathLen - len };
|
2018-01-07 13:54:24 +00:00
|
|
|
SkPaint dashedPaint(paint);
|
2018-01-21 16:47:22 +00:00
|
|
|
dashedPaint.setPathEffect(SkDashPathEffect::Make(dashes,
|
|
|
|
SK_ARRAY_COUNT(dashes),
|
|
|
|
-start - offset));
|
2018-01-07 13:54:24 +00:00
|
|
|
|
|
|
|
canvas->drawPath(path, dashedPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPath TrimEffect::onAsPath() const {
|
|
|
|
return fChild->asPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkRect TrimEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
|
|
|
|
SkASSERT(this->hasInval());
|
|
|
|
return fChild->revalidate(ic, ctm);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sksg
|