[sksg] Simplify TrimEffect
Move the Lottie-specific bits to Skottie and keep TrimEffect as a thin SkTrimPathEffect wrapper. TBR= Change-Id: Iecc6624d01ba61eb96a2056ef8e9e24e731f8979 Reviewed-on: https://skia-review.googlesource.com/115923 Reviewed-by: Florin Malita <fmalita@chromium.org> Commit-Queue: Florin Malita <fmalita@chromium.org>
This commit is contained in:
parent
1a605cd396
commit
69526b023c
@ -427,19 +427,21 @@ std::vector<sk_sp<sksg::GeometryNode>> AttachTrimGeometryEffect(
|
||||
std::vector<sk_sp<sksg::GeometryNode>> trimmed;
|
||||
trimmed.reserve(inputs.size());
|
||||
for (const auto& i : inputs) {
|
||||
const auto trim = sksg::TrimEffect::Make(i);
|
||||
trimmed.push_back(trim);
|
||||
const auto trimEffect = sksg::TrimEffect::Make(i);
|
||||
trimmed.push_back(trimEffect);
|
||||
|
||||
const auto trimComposite = sk_make_sp<CompositeTrimEffect>(std::move(trimEffect));
|
||||
BindProperty<ScalarValue>(jtrim["s"], &ctx->fAnimators,
|
||||
[trim](const ScalarValue& s) {
|
||||
trim->setStart(s * 0.01f);
|
||||
[trimComposite](const ScalarValue& s) {
|
||||
trimComposite->setStart(s);
|
||||
});
|
||||
BindProperty<ScalarValue>(jtrim["e"], &ctx->fAnimators,
|
||||
[trim](const ScalarValue& e) {
|
||||
trim->setEnd(e * 0.01f);
|
||||
[trimComposite](const ScalarValue& e) {
|
||||
trimComposite->setEnd(e);
|
||||
});
|
||||
BindProperty<ScalarValue>(jtrim["o"], &ctx->fAnimators,
|
||||
[trim](const ScalarValue& o) {
|
||||
trim->setOffset(o / 360);
|
||||
[trimComposite](const ScalarValue& o) {
|
||||
trimComposite->setOffset(o);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,7 @@
|
||||
#include "SkSGPath.h"
|
||||
#include "SkSGRect.h"
|
||||
#include "SkSGTransform.h"
|
||||
#include "SkSGTrimEffect.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
@ -190,4 +191,37 @@ void CompositeRadialGradient::onApply() {
|
||||
grad->setEndRadius(SkPoint::Distance(this->startPoint(), this->endPoint()));
|
||||
}
|
||||
|
||||
CompositeTrimEffect::CompositeTrimEffect(sk_sp<sksg::TrimEffect> trimEffect)
|
||||
: fTrimEffect(std::move(trimEffect)) {
|
||||
SkASSERT(fTrimEffect);
|
||||
}
|
||||
|
||||
void CompositeTrimEffect::apply() {
|
||||
// BM semantics: start/end are percentages, offset is "degrees" (?!).
|
||||
const auto start = fStart / 100,
|
||||
end = fEnd / 100,
|
||||
offset = fOffset / 360;
|
||||
|
||||
auto startT = SkTMin(start, end) + offset,
|
||||
stopT = SkTMax(start, end) + offset;
|
||||
auto mode = SkTrimPathEffect::Mode::kNormal;
|
||||
|
||||
if (stopT - startT < 1) {
|
||||
startT -= SkScalarFloorToScalar(startT);
|
||||
stopT -= SkScalarFloorToScalar(stopT);
|
||||
|
||||
if (startT > stopT) {
|
||||
SkTSwap(startT, stopT);
|
||||
mode = SkTrimPathEffect::Mode::kInverted;
|
||||
}
|
||||
} else {
|
||||
startT = 0;
|
||||
stopT = 1;
|
||||
}
|
||||
|
||||
fTrimEffect->setStart(startT);
|
||||
fTrimEffect->setStop(stopT);
|
||||
fTrimEffect->setMode(mode);
|
||||
}
|
||||
|
||||
} // namespace skottie
|
||||
|
@ -28,6 +28,7 @@ class Path;
|
||||
class RadialGradient;
|
||||
class RRect;
|
||||
class RenderNode;;
|
||||
class TrimEffect;
|
||||
}
|
||||
|
||||
namespace Json { class Value; }
|
||||
@ -161,6 +162,22 @@ private:
|
||||
using INHERITED = CompositeGradient;
|
||||
};
|
||||
|
||||
class CompositeTrimEffect final : public SkRefCnt {
|
||||
public:
|
||||
explicit CompositeTrimEffect(sk_sp<sksg::TrimEffect>);
|
||||
|
||||
COMPOSITE_PROPERTY(Start , SkScalar, 0)
|
||||
COMPOSITE_PROPERTY(End , SkScalar, 100)
|
||||
COMPOSITE_PROPERTY(Offset, SkScalar, 0)
|
||||
|
||||
private:
|
||||
void apply();
|
||||
|
||||
sk_sp<sksg::TrimEffect> fTrimEffect;
|
||||
|
||||
using INHERITED = SkRefCnt;
|
||||
};
|
||||
|
||||
#undef COMPOSITE_PROPERTY
|
||||
|
||||
} // namespace skottie
|
||||
|
@ -42,22 +42,7 @@ SkRect TrimEffect::onRevalidate(InvalidationController* ic, const SkMatrix& ctm)
|
||||
const auto childbounds = fChild->revalidate(ic, ctm);
|
||||
const auto path = fChild->asPath();
|
||||
|
||||
// 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<SkPathEffect> 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) {
|
||||
if (auto trim = SkTrimPathEffect::Make(fStart, fStop, fMode)) {
|
||||
fTrimmedPath.reset();
|
||||
SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
|
||||
SkAssertResult(trim->filterPath(&fTrimmedPath, path, &rec, &childbounds));
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include "SkSGGeometryNode.h"
|
||||
|
||||
#include "SkPath.h"
|
||||
#include "SkTrimPathEffect.h"
|
||||
|
||||
class SkCanvas;
|
||||
class SkPaint;
|
||||
@ -28,9 +29,9 @@ public:
|
||||
|
||||
~TrimEffect() override;
|
||||
|
||||
SG_ATTRIBUTE(Start , SkScalar, fStart )
|
||||
SG_ATTRIBUTE(End , SkScalar, fEnd )
|
||||
SG_ATTRIBUTE(Offset, SkScalar, fOffset)
|
||||
SG_ATTRIBUTE(Start , SkScalar , fStart )
|
||||
SG_ATTRIBUTE(Stop , SkScalar , fStop )
|
||||
SG_ATTRIBUTE(Mode , SkTrimPathEffect::Mode, fMode )
|
||||
|
||||
protected:
|
||||
void onClip(SkCanvas*, bool antiAlias) const override;
|
||||
@ -45,9 +46,9 @@ private:
|
||||
const sk_sp<GeometryNode> fChild;
|
||||
|
||||
SkPath fTrimmedPath;
|
||||
SkScalar fStart = 0, // starting t
|
||||
fEnd = 1, // ending t
|
||||
fOffset = 0; // t offset
|
||||
SkScalar fStart = 0,
|
||||
fStop = 1;
|
||||
SkTrimPathEffect::Mode fMode = SkTrimPathEffect::Mode::kNormal;
|
||||
|
||||
using INHERITED = GeometryNode;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user