2017-12-30 17:27:00 +00:00
|
|
|
/*
|
2018-03-23 17:41:58 +00:00
|
|
|
* Copyright 2018 Google Inc.
|
2017-12-30 17:27:00 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
#include "SkottieAdapter.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
#include "SkMatrix.h"
|
|
|
|
#include "SkottieValue.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
#include "SkPath.h"
|
2018-03-23 17:41:58 +00:00
|
|
|
#include "SkRRect.h"
|
2018-01-12 17:25:09 +00:00
|
|
|
#include "SkSGGradient.h"
|
2018-01-04 16:27:09 +00:00
|
|
|
#include "SkSGPath.h"
|
2018-01-02 15:40:00 +00:00
|
|
|
#include "SkSGRect.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
#include "SkSGTransform.h"
|
2018-03-22 16:20:02 +00:00
|
|
|
#include "SkSGTrimEffect.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-04 16:27:09 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
namespace skottie {
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
RRectAdapter::RRectAdapter(sk_sp<sksg::RRect> wrapped_node)
|
2018-01-02 15:40:00 +00:00
|
|
|
: fRRectNode(std::move(wrapped_node)) {}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
void RRectAdapter::apply() {
|
2018-01-02 15:40:00 +00:00
|
|
|
// BM "position" == "center position"
|
|
|
|
auto rr = SkRRect::MakeRectXY(SkRect::MakeXYWH(fPosition.x() - fSize.width() / 2,
|
|
|
|
fPosition.y() - fSize.height() / 2,
|
|
|
|
fSize.width(), fSize.height()),
|
2018-01-04 15:26:35 +00:00
|
|
|
fRadius.width(),
|
|
|
|
fRadius.height());
|
2018-01-02 15:40:00 +00:00
|
|
|
fRRectNode->setRRect(rr);
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
TransformAdapter::TransformAdapter(sk_sp<sksg::Matrix> matrix)
|
2018-01-05 02:11:55 +00:00
|
|
|
: fMatrixNode(std::move(matrix)) {}
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
void TransformAdapter::apply() {
|
2017-12-30 17:27:00 +00:00
|
|
|
SkMatrix t = SkMatrix::MakeTrans(-fAnchorPoint.x(), -fAnchorPoint.y());
|
|
|
|
|
|
|
|
t.postScale(fScale.x() / 100, fScale.y() / 100); // 100% based
|
|
|
|
t.postRotate(fRotation);
|
|
|
|
t.postTranslate(fPosition.x(), fPosition.y());
|
|
|
|
// TODO: skew
|
|
|
|
|
2018-01-05 02:11:55 +00:00
|
|
|
fMatrixNode->setMatrix(t);
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
PolyStarAdapter::PolyStarAdapter(sk_sp<sksg::Path> wrapped_node, Type t)
|
2018-01-04 16:27:09 +00:00
|
|
|
: fPathNode(std::move(wrapped_node))
|
|
|
|
, fType(t) {}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
void PolyStarAdapter::apply() {
|
2018-01-04 16:27:09 +00:00
|
|
|
const auto count = SkScalarTruncToInt(fPointCount);
|
|
|
|
const auto arc = SK_ScalarPI * 2 / count;
|
|
|
|
|
|
|
|
const auto pt_on_circle = [](const SkPoint& c, SkScalar r, SkScalar a) {
|
|
|
|
return SkPoint::Make(c.x() + r * std::cos(a),
|
|
|
|
c.y() + r * std::sin(a));
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: inner/outer "roundness"?
|
|
|
|
|
|
|
|
SkPath poly;
|
|
|
|
|
|
|
|
auto angle = SkDegreesToRadians(fRotation);
|
|
|
|
poly.moveTo(pt_on_circle(fPosition, fOuterRadius, angle));
|
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
if (fType == Type::kStar) {
|
|
|
|
poly.lineTo(pt_on_circle(fPosition, fInnerRadius, angle + arc * 0.5f));
|
|
|
|
}
|
|
|
|
angle += arc;
|
|
|
|
poly.lineTo(pt_on_circle(fPosition, fOuterRadius, angle));
|
|
|
|
}
|
|
|
|
|
|
|
|
poly.close();
|
|
|
|
fPathNode->setPath(poly);
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
GradientAdapter::GradientAdapter(sk_sp<sksg::Gradient> grad, size_t stopCount)
|
2018-01-12 17:25:09 +00:00
|
|
|
: fGradient(std::move(grad))
|
|
|
|
, fStopCount(stopCount) {}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
void GradientAdapter::apply() {
|
2018-01-12 17:25:09 +00:00
|
|
|
this->onApply();
|
|
|
|
|
|
|
|
// |fColorStops| holds |fStopCount| x [ pos, r, g, g ] + ? x [ pos, alpha ]
|
|
|
|
|
|
|
|
if (fColorStops.size() < fStopCount * 4 || ((fColorStops.size() - fStopCount * 4) % 2)) {
|
2018-01-25 20:27:33 +00:00
|
|
|
SkDebugf("!! Invalid gradient stop array size: %zu", fColorStops.size());
|
2018-01-12 17:25:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<sksg::Gradient::ColorStop> stops;
|
|
|
|
|
|
|
|
// TODO: merge/lerp opacity stops
|
|
|
|
const auto csEnd = fColorStops.cbegin() + fStopCount * 4;
|
|
|
|
for (auto cs = fColorStops.cbegin(); cs != csEnd; cs += 4) {
|
2018-03-23 17:41:58 +00:00
|
|
|
const auto pos = cs[0];
|
|
|
|
const VectorValue rgb({ cs[1], cs[2], cs[3] });
|
|
|
|
|
|
|
|
stops.push_back({ pos, ValueTraits<VectorValue>::As<SkColor>(rgb) });
|
2018-01-12 17:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fGradient->setColorStops(std::move(stops));
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
LinearGradientAdapter::LinearGradientAdapter(sk_sp<sksg::LinearGradient> grad, size_t stopCount)
|
2018-01-12 17:25:09 +00:00
|
|
|
: INHERITED(std::move(grad), stopCount) {}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
void LinearGradientAdapter::onApply() {
|
2018-01-12 17:25:09 +00:00
|
|
|
auto* grad = static_cast<sksg::LinearGradient*>(fGradient.get());
|
|
|
|
grad->setStartPoint(this->startPoint());
|
|
|
|
grad->setEndPoint(this->endPoint());
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
RadialGradientAdapter::RadialGradientAdapter(sk_sp<sksg::RadialGradient> grad, size_t stopCount)
|
2018-01-12 17:25:09 +00:00
|
|
|
: INHERITED(std::move(grad), stopCount) {}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
void RadialGradientAdapter::onApply() {
|
2018-01-12 17:25:09 +00:00
|
|
|
auto* grad = static_cast<sksg::RadialGradient*>(fGradient.get());
|
|
|
|
grad->setStartCenter(this->startPoint());
|
|
|
|
grad->setEndCenter(this->startPoint());
|
|
|
|
grad->setStartRadius(0);
|
|
|
|
grad->setEndRadius(SkPoint::Distance(this->startPoint(), this->endPoint()));
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
TrimEffectAdapter::TrimEffectAdapter(sk_sp<sksg::TrimEffect> trimEffect)
|
2018-03-22 16:20:02 +00:00
|
|
|
: fTrimEffect(std::move(trimEffect)) {
|
|
|
|
SkASSERT(fTrimEffect);
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:41:58 +00:00
|
|
|
void TrimEffectAdapter::apply() {
|
2018-03-22 16:20:02 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
} // namespace skottie
|