skia2/experimental/sksg/geometry/SkSGTrimEffect.cpp
Florin Malita 3ba3fa72ae [sksg] Refactor inval registration
... to avoid having too many Node friends.

TBR=
Change-Id: I8f8ff570d94ea48017935066a3d51cd8265ec120
Reviewed-on: https://skia-review.googlesource.com/97980
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
2018-01-22 15:50:49 +00:00

66 lines
1.8 KiB
C++

/*
* 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)) {
this->observeInval(fChild);
}
TrimEffect::~TrimEffect() {
this->unobserveInval(fChild);
}
// 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());
const auto start = SkScalarPin(fStart , 0, 1) * pathLen,
end = SkScalarPin(fEnd , 0, 1) * pathLen,
offset = SkScalarPin(fOffset, 0, 1) * pathLen,
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));
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