2017-12-30 17:27:00 +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 "Skotty.h"
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkottyAnimator.h"
|
|
|
|
#include "SkottyPriv.h"
|
|
|
|
#include "SkottyProperties.h"
|
|
|
|
#include "SkData.h"
|
2018-01-08 17:51:12 +00:00
|
|
|
#include "SkImage.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
#include "SkMakeUnique.h"
|
2018-01-08 17:51:12 +00:00
|
|
|
#include "SkOSPath.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
#include "SkPaint.h"
|
2018-01-09 22:10:18 +00:00
|
|
|
#include "SkParse.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkSGColor.h"
|
|
|
|
#include "SkSGDraw.h"
|
|
|
|
#include "SkSGGroup.h"
|
2018-01-08 17:51:12 +00:00
|
|
|
#include "SkSGImage.h"
|
|
|
|
#include "SkSGInvalidationController.h"
|
2018-01-10 18:36:22 +00:00
|
|
|
#include "SkSGMaskEffect.h"
|
2018-01-04 04:37:54 +00:00
|
|
|
#include "SkSGMerge.h"
|
2018-01-08 21:42:59 +00:00
|
|
|
#include "SkSGOpacityEffect.h"
|
2017-12-30 17:27:00 +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-01-07 13:54:24 +00:00
|
|
|
#include "SkSGTrimEffect.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTArray.h"
|
|
|
|
#include "SkTHash.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
2018-01-05 02:11:55 +00:00
|
|
|
#include <unordered_map>
|
2018-01-04 04:37:54 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2017-12-30 17:27:00 +00:00
|
|
|
#include "stdlib.h"
|
|
|
|
|
|
|
|
namespace skotty {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using AssetMap = SkTHashMap<SkString, const Json::Value*>;
|
|
|
|
|
|
|
|
struct AttachContext {
|
2018-01-08 17:51:12 +00:00
|
|
|
const ResourceProvider& fResources;
|
2017-12-30 17:27:00 +00:00
|
|
|
const AssetMap& fAssets;
|
|
|
|
SkTArray<std::unique_ptr<AnimatorBase>>& fAnimators;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool LogFail(const Json::Value& json, const char* msg) {
|
|
|
|
const auto dump = json.toStyledString();
|
|
|
|
LOG("!! %s: %s", msg, dump.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the workhorse for binding properties: depending on whether the property is animated,
|
|
|
|
// it will either apply immediately or instantiate and attach a keyframe animator.
|
2018-01-09 16:56:09 +00:00
|
|
|
template <typename ValT, typename NodeT>
|
|
|
|
bool BindProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node,
|
|
|
|
typename Animator<ValT, NodeT>::ApplyFuncT&& apply) {
|
2017-12-30 17:27:00 +00:00
|
|
|
if (!jprop.isObject())
|
|
|
|
return false;
|
|
|
|
|
2018-01-08 15:15:12 +00:00
|
|
|
const auto& jpropA = jprop["a"];
|
|
|
|
const auto& jpropK = jprop["k"];
|
|
|
|
|
|
|
|
// Older Json versions don't have an "a" animation marker.
|
|
|
|
// For those, we attempt to parse both ways.
|
|
|
|
if (jpropA.isNull() || !ParseBool(jpropA, "false")) {
|
2018-01-09 16:56:09 +00:00
|
|
|
ValT val;
|
|
|
|
if (ValueTraits<ValT>::Parse(jpropK, &val)) {
|
2018-01-08 15:15:12 +00:00
|
|
|
// Static property.
|
2018-01-09 16:56:09 +00:00
|
|
|
apply(node.get(), val);
|
2018-01-08 15:15:12 +00:00
|
|
|
return true;
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 15:15:12 +00:00
|
|
|
if (!jpropA.isNull()) {
|
|
|
|
return LogFail(jprop, "Could not parse (explicit) static property");
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
2018-01-08 15:15:12 +00:00
|
|
|
}
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-08 15:15:12 +00:00
|
|
|
// Keyframe property.
|
2018-01-09 16:56:09 +00:00
|
|
|
using AnimatorT = Animator<ValT, NodeT>;
|
|
|
|
auto animator = AnimatorT::Make(ParseFrames<ValT>(jpropK), node, std::move(apply));
|
2018-01-08 15:15:12 +00:00
|
|
|
|
|
|
|
if (!animator) {
|
|
|
|
return LogFail(jprop, "Could not parse keyframed property");
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 15:15:12 +00:00
|
|
|
ctx->fAnimators.push_back(std::move(animator));
|
|
|
|
|
2017-12-30 17:27:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:11:55 +00:00
|
|
|
sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx,
|
|
|
|
sk_sp<sksg::Matrix> parentMatrix) {
|
|
|
|
if (!t.isObject())
|
|
|
|
return nullptr;
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-05 02:11:55 +00:00
|
|
|
auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix));
|
|
|
|
auto composite = sk_make_sp<CompositeTransform>(matrix);
|
2018-01-09 16:56:09 +00:00
|
|
|
auto anchor_attached = BindProperty<VectorValue>(t["a"], ctx, composite,
|
|
|
|
[](CompositeTransform* node, const VectorValue& a) {
|
|
|
|
node->setAnchorPoint(ValueTraits<VectorValue>::As<SkPoint>(a));
|
2017-12-30 17:27:00 +00:00
|
|
|
});
|
2018-01-09 16:56:09 +00:00
|
|
|
auto position_attached = BindProperty<VectorValue>(t["p"], ctx, composite,
|
|
|
|
[](CompositeTransform* node, const VectorValue& p) {
|
|
|
|
node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
|
2017-12-30 17:27:00 +00:00
|
|
|
});
|
2018-01-09 16:56:09 +00:00
|
|
|
auto scale_attached = BindProperty<VectorValue>(t["s"], ctx, composite,
|
|
|
|
[](CompositeTransform* node, const VectorValue& s) {
|
|
|
|
node->setScale(ValueTraits<VectorValue>::As<SkVector>(s));
|
2017-12-30 17:27:00 +00:00
|
|
|
});
|
2018-01-09 16:56:09 +00:00
|
|
|
auto rotation_attached = BindProperty<ScalarValue>(t["r"], ctx, composite,
|
|
|
|
[](CompositeTransform* node, const ScalarValue& r) {
|
2017-12-30 17:27:00 +00:00
|
|
|
node->setRotation(r);
|
|
|
|
});
|
2018-01-09 16:56:09 +00:00
|
|
|
auto skew_attached = BindProperty<ScalarValue>(t["sk"], ctx, composite,
|
|
|
|
[](CompositeTransform* node, const ScalarValue& sk) {
|
2017-12-30 17:27:00 +00:00
|
|
|
node->setSkew(sk);
|
|
|
|
});
|
2018-01-09 16:56:09 +00:00
|
|
|
auto skewaxis_attached = BindProperty<ScalarValue>(t["sa"], ctx, composite,
|
|
|
|
[](CompositeTransform* node, const ScalarValue& sa) {
|
2017-12-30 17:27:00 +00:00
|
|
|
node->setSkewAxis(sa);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!anchor_attached &&
|
|
|
|
!position_attached &&
|
|
|
|
!scale_attached &&
|
|
|
|
!rotation_attached &&
|
|
|
|
!skew_attached &&
|
|
|
|
!skewaxis_attached) {
|
|
|
|
LogFail(t, "Could not parse transform");
|
2018-01-05 02:11:55 +00:00
|
|
|
return nullptr;
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 02:11:55 +00:00
|
|
|
return matrix;
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 21:42:59 +00:00
|
|
|
sk_sp<sksg::RenderNode> AttachOpacity(const Json::Value& jtransform, AttachContext* ctx,
|
|
|
|
sk_sp<sksg::RenderNode> childNode) {
|
|
|
|
if (!jtransform.isObject() || !childNode)
|
|
|
|
return childNode;
|
|
|
|
|
|
|
|
// This is more peeky than other attachers, because we want to avoid redundant opacity
|
|
|
|
// nodes for the extremely common case of static opaciy == 100.
|
|
|
|
const auto& opacity = jtransform["o"];
|
|
|
|
if (opacity.isObject() &&
|
|
|
|
!ParseBool(opacity["a"], true) &&
|
|
|
|
ParseScalar(opacity["k"], -1) == 100) {
|
|
|
|
// Ignoring static full opacity.
|
|
|
|
return childNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto opacityNode = sksg::OpacityEffect::Make(childNode);
|
2018-01-09 16:56:09 +00:00
|
|
|
BindProperty<ScalarValue>(opacity, ctx, opacityNode,
|
|
|
|
[](sksg::OpacityEffect* node, const ScalarValue& o) {
|
2018-01-08 21:42:59 +00:00
|
|
|
// BM opacity is [0..100]
|
|
|
|
node->setOpacity(o * 0.01f);
|
|
|
|
});
|
|
|
|
|
|
|
|
return opacityNode;
|
|
|
|
}
|
|
|
|
|
2017-12-30 17:27:00 +00:00
|
|
|
sk_sp<sksg::RenderNode> AttachShape(const Json::Value&, AttachContext* ctx);
|
|
|
|
sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx);
|
|
|
|
|
|
|
|
sk_sp<sksg::RenderNode> AttachShapeGroup(const Json::Value& jgroup, AttachContext* ctx) {
|
|
|
|
SkASSERT(jgroup.isObject());
|
|
|
|
|
|
|
|
return AttachShape(jgroup["it"], ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) {
|
|
|
|
SkASSERT(jpath.isObject());
|
|
|
|
|
|
|
|
auto path_node = sksg::Path::Make();
|
2018-01-09 16:56:09 +00:00
|
|
|
auto path_attached = BindProperty<ShapeValue>(jpath["ks"], ctx, path_node,
|
|
|
|
[](sksg::Path* node, const ShapeValue& p) { node->setPath(p); });
|
2017-12-30 17:27:00 +00:00
|
|
|
|
|
|
|
if (path_attached)
|
|
|
|
LOG("** Attached path geometry - verbs: %d\n", path_node->getPath().countVerbs());
|
|
|
|
|
|
|
|
return path_attached ? path_node : nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-02 15:40:00 +00:00
|
|
|
sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) {
|
|
|
|
SkASSERT(jrect.isObject());
|
|
|
|
|
|
|
|
auto rect_node = sksg::RRect::Make();
|
|
|
|
auto composite = sk_make_sp<CompositeRRect>(rect_node);
|
|
|
|
|
2018-01-09 16:56:09 +00:00
|
|
|
auto p_attached = BindProperty<VectorValue>(jrect["p"], ctx, composite,
|
|
|
|
[](CompositeRRect* node, const VectorValue& p) {
|
|
|
|
node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
|
|
|
|
});
|
|
|
|
auto s_attached = BindProperty<VectorValue>(jrect["s"], ctx, composite,
|
|
|
|
[](CompositeRRect* node, const VectorValue& s) {
|
|
|
|
node->setSize(ValueTraits<VectorValue>::As<SkSize>(s));
|
|
|
|
});
|
|
|
|
auto r_attached = BindProperty<ScalarValue>(jrect["r"], ctx, composite,
|
|
|
|
[](CompositeRRect* node, const ScalarValue& r) {
|
|
|
|
node->setRadius(SkSize::Make(r, r));
|
|
|
|
});
|
2018-01-02 15:40:00 +00:00
|
|
|
|
|
|
|
if (!p_attached && !s_attached && !r_attached) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-04 15:26:35 +00:00
|
|
|
LOG("** Attached (r)rect geometry\n");
|
|
|
|
|
|
|
|
return rect_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, AttachContext* ctx) {
|
|
|
|
SkASSERT(jellipse.isObject());
|
|
|
|
|
|
|
|
auto rect_node = sksg::RRect::Make();
|
|
|
|
auto composite = sk_make_sp<CompositeRRect>(rect_node);
|
|
|
|
|
2018-01-09 16:56:09 +00:00
|
|
|
auto p_attached = BindProperty<VectorValue>(jellipse["p"], ctx, composite,
|
|
|
|
[](CompositeRRect* node, const VectorValue& p) {
|
|
|
|
node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
|
|
|
|
});
|
|
|
|
auto s_attached = BindProperty<VectorValue>(jellipse["s"], ctx, composite,
|
|
|
|
[](CompositeRRect* node, const VectorValue& s) {
|
|
|
|
const auto sz = ValueTraits<VectorValue>::As<SkSize>(s);
|
|
|
|
node->setSize(sz);
|
|
|
|
node->setRadius(SkSize::Make(sz.width() / 2, sz.height() / 2));
|
|
|
|
});
|
2018-01-04 15:26:35 +00:00
|
|
|
|
|
|
|
if (!p_attached && !s_attached) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("** Attached ellipse geometry\n");
|
|
|
|
|
2018-01-02 15:40:00 +00:00
|
|
|
return rect_node;
|
|
|
|
}
|
|
|
|
|
2018-01-04 16:27:09 +00:00
|
|
|
sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) {
|
|
|
|
SkASSERT(jstar.isObject());
|
|
|
|
|
|
|
|
static constexpr CompositePolyStar::Type gTypes[] = {
|
|
|
|
CompositePolyStar::Type::kStar, // "sy": 1
|
|
|
|
CompositePolyStar::Type::kPoly, // "sy": 2
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto type = ParseInt(jstar["sy"], 0) - 1;
|
|
|
|
if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) {
|
|
|
|
LogFail(jstar, "Unknown polystar type");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto path_node = sksg::Path::Make();
|
|
|
|
auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]);
|
|
|
|
|
2018-01-09 16:56:09 +00:00
|
|
|
BindProperty<VectorValue>(jstar["p"], ctx, composite,
|
|
|
|
[](CompositePolyStar* node, const VectorValue& p) {
|
|
|
|
node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
|
|
|
|
});
|
|
|
|
BindProperty<ScalarValue>(jstar["pt"], ctx, composite,
|
|
|
|
[](CompositePolyStar* node, const ScalarValue& pt) {
|
|
|
|
node->setPointCount(pt);
|
|
|
|
});
|
|
|
|
BindProperty<ScalarValue>(jstar["ir"], ctx, composite,
|
|
|
|
[](CompositePolyStar* node, const ScalarValue& ir) {
|
|
|
|
node->setInnerRadius(ir);
|
|
|
|
});
|
|
|
|
BindProperty<ScalarValue>(jstar["or"], ctx, composite,
|
|
|
|
[](CompositePolyStar* node, const ScalarValue& otr) {
|
2018-01-06 19:25:49 +00:00
|
|
|
node->setOuterRadius(otr);
|
|
|
|
});
|
2018-01-09 16:56:09 +00:00
|
|
|
BindProperty<ScalarValue>(jstar["is"], ctx, composite,
|
|
|
|
[](CompositePolyStar* node, const ScalarValue& is) {
|
2018-01-06 19:25:49 +00:00
|
|
|
node->setInnerRoundness(is);
|
|
|
|
});
|
2018-01-09 16:56:09 +00:00
|
|
|
BindProperty<ScalarValue>(jstar["os"], ctx, composite,
|
|
|
|
[](CompositePolyStar* node, const ScalarValue& os) {
|
2018-01-06 19:25:49 +00:00
|
|
|
node->setOuterRoundness(os);
|
|
|
|
});
|
2018-01-09 16:56:09 +00:00
|
|
|
BindProperty<ScalarValue>(jstar["r"], ctx, composite,
|
|
|
|
[](CompositePolyStar* node, const ScalarValue& r) {
|
|
|
|
node->setRotation(r);
|
|
|
|
});
|
2018-01-04 16:27:09 +00:00
|
|
|
|
|
|
|
return path_node;
|
|
|
|
}
|
|
|
|
|
2017-12-30 17:27:00 +00:00
|
|
|
sk_sp<sksg::Color> AttachColorPaint(const Json::Value& obj, AttachContext* ctx) {
|
|
|
|
SkASSERT(obj.isObject());
|
|
|
|
|
|
|
|
auto color_node = sksg::Color::Make(SK_ColorBLACK);
|
|
|
|
color_node->setAntiAlias(true);
|
|
|
|
|
2018-01-09 18:11:56 +00:00
|
|
|
auto composite = sk_make_sp<CompositeColor>(color_node);
|
|
|
|
auto color_attached = BindProperty<VectorValue>(obj["c"], ctx, composite,
|
|
|
|
[](CompositeColor* node, const VectorValue& c) {
|
2018-01-09 16:56:09 +00:00
|
|
|
node->setColor(ValueTraits<VectorValue>::As<SkColor>(c));
|
|
|
|
});
|
2018-01-09 18:11:56 +00:00
|
|
|
auto opacity_attached = BindProperty<ScalarValue>(obj["o"], ctx, composite,
|
|
|
|
[](CompositeColor* node, const ScalarValue& o) {
|
|
|
|
node->setOpacity(o);
|
|
|
|
});
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-09 18:11:56 +00:00
|
|
|
return (color_attached || opacity_attached) ? color_node : nullptr;
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::PaintNode> AttachFillPaint(const Json::Value& jfill, AttachContext* ctx) {
|
|
|
|
SkASSERT(jfill.isObject());
|
|
|
|
|
|
|
|
auto color = AttachColorPaint(jfill, ctx);
|
|
|
|
if (color) {
|
|
|
|
LOG("** Attached color fill: 0x%x\n", color->getColor());
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::PaintNode> AttachStrokePaint(const Json::Value& jstroke, AttachContext* ctx) {
|
|
|
|
SkASSERT(jstroke.isObject());
|
|
|
|
|
|
|
|
auto stroke_node = AttachColorPaint(jstroke, ctx);
|
|
|
|
if (!stroke_node)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
LOG("** Attached color stroke: 0x%x\n", stroke_node->getColor());
|
|
|
|
|
|
|
|
stroke_node->setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
2018-01-09 16:56:09 +00:00
|
|
|
auto width_attached = BindProperty<ScalarValue>(jstroke["w"], ctx, stroke_node,
|
|
|
|
[](sksg::Color* node, const ScalarValue& w) {
|
|
|
|
node->setStrokeWidth(w);
|
|
|
|
});
|
2017-12-30 17:27:00 +00:00
|
|
|
if (!width_attached)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
stroke_node->setStrokeMiter(ParseScalar(jstroke["ml"], 4));
|
|
|
|
|
|
|
|
static constexpr SkPaint::Join gJoins[] = {
|
|
|
|
SkPaint::kMiter_Join,
|
|
|
|
SkPaint::kRound_Join,
|
|
|
|
SkPaint::kBevel_Join,
|
|
|
|
};
|
|
|
|
stroke_node->setStrokeJoin(gJoins[SkTPin<int>(ParseInt(jstroke["lj"], 1) - 1,
|
|
|
|
0, SK_ARRAY_COUNT(gJoins) - 1)]);
|
|
|
|
|
|
|
|
static constexpr SkPaint::Cap gCaps[] = {
|
|
|
|
SkPaint::kButt_Cap,
|
|
|
|
SkPaint::kRound_Cap,
|
|
|
|
SkPaint::kSquare_Cap,
|
|
|
|
};
|
|
|
|
stroke_node->setStrokeCap(gCaps[SkTPin<int>(ParseInt(jstroke["lc"], 1) - 1,
|
|
|
|
0, SK_ARRAY_COUNT(gCaps) - 1)]);
|
|
|
|
|
|
|
|
return stroke_node;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:37:54 +00:00
|
|
|
std::vector<sk_sp<sksg::GeometryNode>> AttachMergeGeometryEffect(
|
|
|
|
const Json::Value& jmerge, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
|
|
|
|
std::vector<sk_sp<sksg::GeometryNode>> merged;
|
|
|
|
|
|
|
|
static constexpr sksg::Merge::Mode gModes[] = {
|
|
|
|
sksg::Merge::Mode::kMerge, // "mm": 1
|
|
|
|
sksg::Merge::Mode::kUnion, // "mm": 2
|
|
|
|
sksg::Merge::Mode::kDifference, // "mm": 3
|
|
|
|
sksg::Merge::Mode::kIntersect, // "mm": 4
|
|
|
|
sksg::Merge::Mode::kXOR , // "mm": 5
|
|
|
|
};
|
|
|
|
|
2018-01-07 13:54:24 +00:00
|
|
|
const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1,
|
|
|
|
0, SK_ARRAY_COUNT(gModes) - 1)];
|
2018-01-04 04:37:54 +00:00
|
|
|
merged.push_back(sksg::Merge::Make(std::move(geos), mode));
|
|
|
|
|
|
|
|
LOG("** Attached merge path effect, mode: %d\n", mode);
|
|
|
|
|
|
|
|
return merged;
|
|
|
|
}
|
|
|
|
|
2018-01-07 13:54:24 +00:00
|
|
|
std::vector<sk_sp<sksg::GeometryNode>> AttachTrimGeometryEffect(
|
|
|
|
const Json::Value& jtrim, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
|
|
|
|
|
|
|
|
enum class Mode {
|
|
|
|
kMerged, // "m": 1
|
|
|
|
kSeparate, // "m": 2
|
|
|
|
} gModes[] = { Mode::kMerged, Mode::kSeparate };
|
|
|
|
|
|
|
|
const auto mode = gModes[SkTPin<int>(ParseInt(jtrim["m"], 1) - 1,
|
|
|
|
0, SK_ARRAY_COUNT(gModes) - 1)];
|
|
|
|
|
|
|
|
std::vector<sk_sp<sksg::GeometryNode>> inputs;
|
|
|
|
if (mode == Mode::kMerged) {
|
|
|
|
inputs.push_back(sksg::Merge::Make(std::move(geos), sksg::Merge::Mode::kMerge));
|
|
|
|
} else {
|
|
|
|
inputs = std::move(geos);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-01-09 16:56:09 +00:00
|
|
|
BindProperty<ScalarValue>(jtrim["s"], ctx, trim,
|
|
|
|
[](sksg::TrimEffect* node, const ScalarValue& s) {
|
2018-01-07 13:54:24 +00:00
|
|
|
node->setStart(s * 0.01f);
|
|
|
|
});
|
2018-01-09 16:56:09 +00:00
|
|
|
BindProperty<ScalarValue>(jtrim["e"], ctx, trim,
|
|
|
|
[](sksg::TrimEffect* node, const ScalarValue& e) {
|
2018-01-07 13:54:24 +00:00
|
|
|
node->setEnd(e * 0.01f);
|
|
|
|
});
|
|
|
|
// TODO: "offset" doesn't currently work the same as BM - figure out what's going on.
|
2018-01-09 16:56:09 +00:00
|
|
|
BindProperty<ScalarValue>(jtrim["o"], ctx, trim,
|
|
|
|
[](sksg::TrimEffect* node, const ScalarValue& o) {
|
2018-01-07 13:54:24 +00:00
|
|
|
node->setOffset(o * 0.01f);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return trimmed;
|
|
|
|
}
|
|
|
|
|
2017-12-30 17:27:00 +00:00
|
|
|
using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*);
|
|
|
|
static constexpr GeometryAttacherT gGeometryAttachers[] = {
|
|
|
|
AttachPathGeometry,
|
2018-01-02 15:40:00 +00:00
|
|
|
AttachRRectGeometry,
|
2018-01-04 15:26:35 +00:00
|
|
|
AttachEllipseGeometry,
|
2018-01-04 16:27:09 +00:00
|
|
|
AttachPolystarGeometry,
|
2017-12-30 17:27:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
|
|
|
|
static constexpr PaintAttacherT gPaintAttachers[] = {
|
|
|
|
AttachFillPaint,
|
|
|
|
AttachStrokePaint,
|
|
|
|
};
|
|
|
|
|
|
|
|
using GroupAttacherT = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
|
|
|
|
static constexpr GroupAttacherT gGroupAttachers[] = {
|
|
|
|
AttachShapeGroup,
|
|
|
|
};
|
|
|
|
|
2018-01-04 04:37:54 +00:00
|
|
|
using GeometryEffectAttacherT =
|
|
|
|
std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&,
|
|
|
|
AttachContext*,
|
|
|
|
std::vector<sk_sp<sksg::GeometryNode>>&&);
|
|
|
|
static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = {
|
|
|
|
AttachMergeGeometryEffect,
|
2018-01-07 13:54:24 +00:00
|
|
|
AttachTrimGeometryEffect,
|
2018-01-04 04:37:54 +00:00
|
|
|
};
|
|
|
|
|
2017-12-30 17:27:00 +00:00
|
|
|
enum class ShapeType {
|
|
|
|
kGeometry,
|
2018-01-04 04:37:54 +00:00
|
|
|
kGeometryEffect,
|
2017-12-30 17:27:00 +00:00
|
|
|
kPaint,
|
|
|
|
kGroup,
|
2017-12-31 14:12:31 +00:00
|
|
|
kTransform,
|
2017-12-30 17:27:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ShapeInfo {
|
|
|
|
const char* fTypeString;
|
|
|
|
ShapeType fShapeType;
|
|
|
|
uint32_t fAttacherIndex; // index into respective attacher tables
|
|
|
|
};
|
|
|
|
|
|
|
|
const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
|
|
|
|
static constexpr ShapeInfo gShapeInfo[] = {
|
2018-01-04 15:26:35 +00:00
|
|
|
{ "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry
|
2018-01-04 04:37:54 +00:00
|
|
|
{ "fl", ShapeType::kPaint , 0 }, // fill -> AttachFillPaint
|
|
|
|
{ "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup
|
|
|
|
{ "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
|
2018-01-04 16:27:09 +00:00
|
|
|
{ "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
|
2018-01-04 04:37:54 +00:00
|
|
|
{ "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
|
2018-01-04 16:27:09 +00:00
|
|
|
{ "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
|
2018-01-04 04:37:54 +00:00
|
|
|
{ "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint
|
2018-01-07 13:54:24 +00:00
|
|
|
{ "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect
|
2018-01-05 02:11:55 +00:00
|
|
|
{ "tr", ShapeType::kTransform , 0 }, // transform -> In-place handler
|
2017-12-30 17:27:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!shape.isObject())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const auto& type = shape["ty"];
|
|
|
|
if (!type.isString())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const auto* info = bsearch(type.asCString(),
|
|
|
|
gShapeInfo,
|
|
|
|
SK_ARRAY_COUNT(gShapeInfo),
|
|
|
|
sizeof(ShapeInfo),
|
|
|
|
[](const void* key, const void* info) {
|
|
|
|
return strcmp(static_cast<const char*>(key),
|
|
|
|
static_cast<const ShapeInfo*>(info)->fTypeString);
|
|
|
|
});
|
|
|
|
|
|
|
|
return static_cast<const ShapeInfo*>(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::RenderNode> AttachShape(const Json::Value& shapeArray, AttachContext* ctx) {
|
|
|
|
if (!shapeArray.isArray())
|
|
|
|
return nullptr;
|
|
|
|
|
2018-01-02 17:52:43 +00:00
|
|
|
// (https://helpx.adobe.com/after-effects/using/overview-shape-layers-paths-vector.html#groups_and_render_order_for_shapes_and_shape_attributes)
|
|
|
|
//
|
|
|
|
// Render order for shapes within a shape layer
|
|
|
|
//
|
|
|
|
// The rules for rendering a shape layer are similar to the rules for rendering a composition
|
|
|
|
// that contains nested compositions:
|
|
|
|
//
|
|
|
|
// * Within a group, the shape at the bottom of the Timeline panel stacking order is rendered
|
|
|
|
// first.
|
|
|
|
//
|
|
|
|
// * All path operations within a group are performed before paint operations. This means,
|
|
|
|
// for example, that the stroke follows the distortions in the path made by the Wiggle Paths
|
|
|
|
// path operation. Path operations within a group are performed from top to bottom.
|
|
|
|
//
|
|
|
|
// * Paint operations within a group are performed from the bottom to the top in the Timeline
|
|
|
|
// panel stacking order. This means, for example, that a stroke is rendered on top of
|
|
|
|
// (in front of) a stroke that appears after it in the Timeline panel.
|
|
|
|
//
|
2017-12-31 14:12:31 +00:00
|
|
|
sk_sp<sksg::Group> shape_group = sksg::Group::Make();
|
|
|
|
sk_sp<sksg::RenderNode> xformed_group = shape_group;
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-04 04:37:54 +00:00
|
|
|
std::vector<sk_sp<sksg::GeometryNode>> geos;
|
|
|
|
std::vector<sk_sp<sksg::RenderNode>> draws;
|
2017-12-30 17:27:00 +00:00
|
|
|
|
|
|
|
for (const auto& s : shapeArray) {
|
|
|
|
const auto* info = FindShapeInfo(s);
|
|
|
|
if (!info) {
|
|
|
|
LogFail(s.isObject() ? s["ty"] : s, "Unknown shape");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (info->fShapeType) {
|
|
|
|
case ShapeType::kGeometry: {
|
|
|
|
SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryAttachers));
|
|
|
|
if (auto geo = gGeometryAttachers[info->fAttacherIndex](s, ctx)) {
|
|
|
|
geos.push_back(std::move(geo));
|
|
|
|
}
|
|
|
|
} break;
|
2018-01-04 04:37:54 +00:00
|
|
|
case ShapeType::kGeometryEffect: {
|
|
|
|
SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
|
|
|
|
geos = gGeometryEffectAttachers[info->fAttacherIndex](s, ctx, std::move(geos));
|
|
|
|
} break;
|
2017-12-30 17:27:00 +00:00
|
|
|
case ShapeType::kPaint: {
|
|
|
|
SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers));
|
|
|
|
if (auto paint = gPaintAttachers[info->fAttacherIndex](s, ctx)) {
|
2018-01-02 17:52:43 +00:00
|
|
|
for (const auto& geo : geos) {
|
|
|
|
draws.push_back(sksg::Draw::Make(geo, paint));
|
|
|
|
}
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case ShapeType::kGroup: {
|
|
|
|
SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGroupAttachers));
|
|
|
|
if (auto group = gGroupAttachers[info->fAttacherIndex](s, ctx)) {
|
2018-01-02 17:52:43 +00:00
|
|
|
draws.push_back(std::move(group));
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
} break;
|
2017-12-31 14:12:31 +00:00
|
|
|
case ShapeType::kTransform: {
|
2018-01-02 17:52:43 +00:00
|
|
|
// TODO: BM appears to transform the geometry, not the draw op itself.
|
2018-01-05 02:11:55 +00:00
|
|
|
if (auto matrix = AttachMatrix(s, ctx, nullptr)) {
|
|
|
|
xformed_group = sksg::Transform::Make(std::move(xformed_group),
|
|
|
|
std::move(matrix));
|
|
|
|
}
|
2018-01-08 21:42:59 +00:00
|
|
|
xformed_group = AttachOpacity(s, ctx, std::move(xformed_group));
|
2017-12-31 14:12:31 +00:00
|
|
|
} break;
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 17:52:43 +00:00
|
|
|
if (draws.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:37:54 +00:00
|
|
|
for (auto draw = draws.rbegin(); draw != draws.rend(); ++draw) {
|
|
|
|
shape_group->addChild(std::move(*draw));
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:37:54 +00:00
|
|
|
LOG("** Attached shape: %zd draws.\n", draws.size());
|
2017-12-31 14:12:31 +00:00
|
|
|
return xformed_group;
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::RenderNode> AttachCompLayer(const Json::Value& layer, AttachContext* ctx) {
|
|
|
|
SkASSERT(layer.isObject());
|
|
|
|
|
|
|
|
auto refId = ParseString(layer["refId"], "");
|
|
|
|
if (refId.isEmpty()) {
|
|
|
|
LOG("!! Comp layer missing refId\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* comp = ctx->fAssets.find(refId);
|
|
|
|
if (!comp) {
|
|
|
|
LOG("!! Pre-comp not found: '%s'\n", refId.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: cycle detection
|
|
|
|
return AttachComposition(**comp, ctx);
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:18 +00:00
|
|
|
sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& jlayer, AttachContext*) {
|
|
|
|
SkASSERT(jlayer.isObject());
|
|
|
|
|
|
|
|
const auto size = SkSize::Make(ParseScalar(jlayer["sw"], -1),
|
|
|
|
ParseScalar(jlayer["sh"], -1));
|
|
|
|
const auto hex = ParseString(jlayer["sc"], "");
|
|
|
|
uint32_t c;
|
|
|
|
if (size.isEmpty() ||
|
|
|
|
!hex.startsWith("#") ||
|
|
|
|
!SkParse::FindHex(hex.c_str() + 1, &c)) {
|
|
|
|
LogFail(jlayer, "Could not parse solid layer");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkColor color = 0xff000000 | c;
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-09 22:10:18 +00:00
|
|
|
return sksg::Draw::Make(sksg::Rect::Make(SkRect::MakeSize(size)),
|
|
|
|
sksg::Color::Make(color));
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 17:51:12 +00:00
|
|
|
sk_sp<sksg::RenderNode> AttachImageAsset(const Json::Value& jimage, AttachContext* ctx) {
|
|
|
|
SkASSERT(jimage.isObject());
|
|
|
|
|
|
|
|
const auto name = ParseString(jimage["p"], ""),
|
|
|
|
path = ParseString(jimage["u"], "");
|
|
|
|
if (name.isEmpty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// TODO: plumb resource paths explicitly to ResourceProvider?
|
|
|
|
const auto resName = path.isEmpty() ? name : SkOSPath::Join(path.c_str(), name.c_str());
|
|
|
|
const auto resStream = ctx->fResources.openStream(resName.c_str());
|
|
|
|
if (!resStream || !resStream->hasLength()) {
|
|
|
|
LOG("!! Could not load image resource: %s\n", resName.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: non-intrisic image sizing
|
|
|
|
return sksg::Image::Make(
|
|
|
|
SkImage::MakeFromEncoded(SkData::MakeFromStream(resStream.get(), resStream->getLength())));
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext* ctx) {
|
2017-12-30 17:27:00 +00:00
|
|
|
SkASSERT(layer.isObject());
|
|
|
|
|
2018-01-08 17:51:12 +00:00
|
|
|
auto refId = ParseString(layer["refId"], "");
|
|
|
|
if (refId.isEmpty()) {
|
|
|
|
LOG("!! Image layer missing refId\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* jimage = ctx->fAssets.find(refId);
|
|
|
|
if (!jimage) {
|
|
|
|
LOG("!! Image asset not found: '%s'\n", refId.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AttachImageAsset(**jimage, ctx);
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) {
|
|
|
|
SkASSERT(layer.isObject());
|
|
|
|
|
2018-01-05 02:11:55 +00:00
|
|
|
// Null layers are used solely to drive dependent transforms,
|
|
|
|
// but we use free-floating sksg::Matrices for that purpose.
|
2017-12-30 17:27:00 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::RenderNode> AttachShapeLayer(const Json::Value& layer, AttachContext* ctx) {
|
|
|
|
SkASSERT(layer.isObject());
|
|
|
|
|
|
|
|
LOG("** Attaching shape layer ind: %d\n", ParseInt(layer["ind"], 0));
|
|
|
|
|
|
|
|
return AttachShape(layer["shapes"], ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::RenderNode> AttachTextLayer(const Json::Value& layer, AttachContext*) {
|
|
|
|
SkASSERT(layer.isObject());
|
|
|
|
|
|
|
|
LOG("?? Text layer stub\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:11:55 +00:00
|
|
|
struct AttachLayerContext {
|
|
|
|
AttachLayerContext(const Json::Value& jlayers, AttachContext* ctx)
|
|
|
|
: fLayerList(jlayers), fCtx(ctx) {}
|
|
|
|
|
|
|
|
const Json::Value& fLayerList;
|
|
|
|
AttachContext* fCtx;
|
|
|
|
std::unordered_map<const Json::Value*, sk_sp<sksg::Matrix>> fLayerMatrixCache;
|
|
|
|
std::unordered_map<int, const Json::Value*> fLayerIndexCache;
|
2018-01-10 18:36:22 +00:00
|
|
|
sk_sp<sksg::RenderNode> fCurrentMatte;
|
2018-01-05 02:11:55 +00:00
|
|
|
|
|
|
|
const Json::Value* findLayer(int index) {
|
|
|
|
SkASSERT(fLayerList.isArray());
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto cached = fLayerIndexCache.find(index);
|
|
|
|
if (cached != fLayerIndexCache.end()) {
|
|
|
|
return cached->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& l : fLayerList) {
|
|
|
|
if (!l.isObject()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ParseInt(l["ind"], -1) == index) {
|
|
|
|
fLayerIndexCache.insert(std::make_pair(index, &l));
|
|
|
|
return &l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::Matrix> AttachLayerMatrix(const Json::Value& jlayer) {
|
|
|
|
SkASSERT(jlayer.isObject());
|
|
|
|
|
|
|
|
const auto cached = fLayerMatrixCache.find(&jlayer);
|
|
|
|
if (cached != fLayerMatrixCache.end()) {
|
|
|
|
return cached->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* parentLayer = this->findLayer(ParseInt(jlayer["parent"], -1));
|
|
|
|
|
|
|
|
// TODO: cycle detection?
|
|
|
|
auto parentMatrix = (parentLayer && parentLayer != &jlayer)
|
|
|
|
? this->AttachLayerMatrix(*parentLayer) : nullptr;
|
|
|
|
|
|
|
|
auto layerMatrix = AttachMatrix(jlayer["ks"], fCtx, std::move(parentMatrix));
|
|
|
|
fLayerMatrixCache.insert(std::make_pair(&jlayer, layerMatrix));
|
|
|
|
|
|
|
|
return layerMatrix;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer,
|
|
|
|
AttachLayerContext* layerCtx) {
|
|
|
|
if (!jlayer.isObject())
|
2017-12-30 17:27:00 +00:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
using LayerAttacher = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
|
|
|
|
static constexpr LayerAttacher gLayerAttachers[] = {
|
|
|
|
AttachCompLayer, // 'ty': 0
|
|
|
|
AttachSolidLayer, // 'ty': 1
|
|
|
|
AttachImageLayer, // 'ty': 2
|
|
|
|
AttachNullLayer, // 'ty': 3
|
|
|
|
AttachShapeLayer, // 'ty': 4
|
|
|
|
AttachTextLayer, // 'ty': 5
|
|
|
|
};
|
|
|
|
|
2018-01-05 02:11:55 +00:00
|
|
|
int type = ParseInt(jlayer["ty"], -1);
|
2017-12-30 17:27:00 +00:00
|
|
|
if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-09 13:07:14 +00:00
|
|
|
// Layer content.
|
|
|
|
auto layer = gLayerAttachers[type](jlayer, layerCtx->fCtx);
|
|
|
|
if (auto layerMatrix = layerCtx->AttachLayerMatrix(jlayer)) {
|
|
|
|
// Optional layer transform.
|
|
|
|
layer = sksg::Transform::Make(std::move(layer), std::move(layerMatrix));
|
|
|
|
}
|
|
|
|
// Optional layer opacity.
|
|
|
|
layer = AttachOpacity(jlayer["ks"], layerCtx->fCtx, std::move(layer));
|
|
|
|
|
|
|
|
// TODO: we should also disable related/inactive animators.
|
|
|
|
class Activator final : public AnimatorBase {
|
|
|
|
public:
|
|
|
|
Activator(sk_sp<sksg::OpacityEffect> controlNode, float in, float out)
|
|
|
|
: fControlNode(std::move(controlNode))
|
|
|
|
, fIn(in)
|
|
|
|
, fOut(out) {}
|
|
|
|
|
2018-01-09 13:46:52 +00:00
|
|
|
void tick(float t) override {
|
2018-01-09 13:07:14 +00:00
|
|
|
// Keep the layer fully transparent except for its [in..out] lifespan.
|
|
|
|
// (note: opacity == 0 disables rendering, while opacity == 1 is a noop)
|
|
|
|
fControlNode->setOpacity(t >= fIn && t <= fOut ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const sk_sp<sksg::OpacityEffect> fControlNode;
|
|
|
|
const float fIn,
|
|
|
|
fOut;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto layerControl = sksg::OpacityEffect::Make(std::move(layer));
|
|
|
|
const auto in = ParseScalar(jlayer["ip"], 0),
|
|
|
|
out = ParseScalar(jlayer["op"], in);
|
|
|
|
|
|
|
|
if (in >= out || ! layerControl)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
layerCtx->fCtx->fAnimators.push_back(skstd::make_unique<Activator>(layerControl, in, out));
|
2018-01-05 02:11:55 +00:00
|
|
|
|
2018-01-10 18:36:22 +00:00
|
|
|
if (ParseBool(jlayer["td"], false)) {
|
|
|
|
// This layer is a matte. We apply it as a mask to the next layer.
|
|
|
|
layerCtx->fCurrentMatte = std::move(layerControl);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (layerCtx->fCurrentMatte) {
|
|
|
|
// There is a pending matte. Apply and reset.
|
|
|
|
return sksg::MaskEffect::Make(std::move(layerControl), std::move(layerCtx->fCurrentMatte));
|
|
|
|
}
|
|
|
|
|
2018-01-09 13:07:14 +00:00
|
|
|
return layerControl;
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) {
|
|
|
|
if (!comp.isObject())
|
|
|
|
return nullptr;
|
|
|
|
|
2018-01-05 02:11:55 +00:00
|
|
|
const auto& jlayers = comp["layers"];
|
|
|
|
if (!jlayers.isArray())
|
|
|
|
return nullptr;
|
|
|
|
|
2018-01-02 17:52:43 +00:00
|
|
|
SkSTArray<16, sk_sp<sksg::RenderNode>, true> layers;
|
2018-01-05 02:11:55 +00:00
|
|
|
AttachLayerContext layerCtx(jlayers, ctx);
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-05 02:11:55 +00:00
|
|
|
for (const auto& l : jlayers) {
|
|
|
|
if (auto layer_fragment = AttachLayer(l, &layerCtx)) {
|
2018-01-02 17:52:43 +00:00
|
|
|
layers.push_back(std::move(layer_fragment));
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 17:52:43 +00:00
|
|
|
if (layers.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Layers are painted in bottom->top order.
|
|
|
|
auto comp_group = sksg::Group::Make();
|
|
|
|
for (int i = layers.count() - 1; i >= 0; --i) {
|
|
|
|
comp_group->addChild(std::move(layers[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("** Attached composition '%s': %d layers.\n",
|
|
|
|
ParseString(comp["id"], "").c_str(), layers.count());
|
|
|
|
|
2017-12-30 17:27:00 +00:00
|
|
|
return comp_group;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-01-08 17:51:12 +00:00
|
|
|
std::unique_ptr<Animation> Animation::Make(SkStream* stream, const ResourceProvider& res) {
|
2017-12-30 17:27:00 +00:00
|
|
|
if (!stream->hasLength()) {
|
|
|
|
// TODO: handle explicit buffering?
|
|
|
|
LOG("!! cannot parse streaming content\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value json;
|
|
|
|
{
|
|
|
|
auto data = SkData::MakeFromStream(stream, stream->getLength());
|
|
|
|
if (!data) {
|
|
|
|
LOG("!! could not read stream\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Reader reader;
|
|
|
|
|
|
|
|
auto dataStart = static_cast<const char*>(data->data());
|
|
|
|
if (!reader.parse(dataStart, dataStart + data->size(), json, false) || !json.isObject()) {
|
|
|
|
LOG("!! failed to parse json: %s\n", reader.getFormattedErrorMessages().c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto version = ParseString(json["v"], "");
|
|
|
|
const auto size = SkSize::Make(ParseScalar(json["w"], -1), ParseScalar(json["h"], -1));
|
|
|
|
const auto fps = ParseScalar(json["fr"], -1);
|
|
|
|
|
|
|
|
if (size.isEmpty() || version.isEmpty() || fps < 0) {
|
|
|
|
LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f)",
|
|
|
|
version.c_str(), size.width(), size.height(), fps);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-08 17:51:12 +00:00
|
|
|
return std::unique_ptr<Animation>(new Animation(res, std::move(version), size, fps, json));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Animation> Animation::MakeFromFile(const char path[], const ResourceProvider* res) {
|
|
|
|
class DirectoryResourceProvider final : public ResourceProvider {
|
|
|
|
public:
|
|
|
|
explicit DirectoryResourceProvider(SkString dir) : fDir(std::move(dir)) {}
|
|
|
|
|
|
|
|
std::unique_ptr<SkStream> openStream(const char resource[]) const override {
|
|
|
|
const auto resPath = SkOSPath::Join(fDir.c_str(), resource);
|
|
|
|
return SkStream::MakeFromFile(resPath.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SkString fDir;
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto jsonStream = SkStream::MakeFromFile(path);
|
|
|
|
if (!jsonStream)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
std::unique_ptr<ResourceProvider> defaultProvider;
|
|
|
|
if (!res) {
|
|
|
|
defaultProvider = skstd::make_unique<DirectoryResourceProvider>(SkOSPath::Dirname(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Make(jsonStream.get(), res ? *res : *defaultProvider);
|
2017-12-30 17:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 17:51:12 +00:00
|
|
|
Animation::Animation(const ResourceProvider& resources,
|
|
|
|
SkString version, const SkSize& size, SkScalar fps, const Json::Value& json)
|
2017-12-30 17:27:00 +00:00
|
|
|
: fVersion(std::move(version))
|
|
|
|
, fSize(size)
|
|
|
|
, fFrameRate(fps)
|
|
|
|
, fInPoint(ParseScalar(json["ip"], 0))
|
|
|
|
, fOutPoint(SkTMax(ParseScalar(json["op"], SK_ScalarMax), fInPoint)) {
|
|
|
|
|
|
|
|
AssetMap assets;
|
|
|
|
for (const auto& asset : json["assets"]) {
|
|
|
|
if (!asset.isObject()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
assets.set(ParseString(asset["id"], ""), &asset);
|
|
|
|
}
|
|
|
|
|
2018-01-08 17:51:12 +00:00
|
|
|
AttachContext ctx = { resources, assets, fAnimators };
|
2017-12-30 17:27:00 +00:00
|
|
|
fDom = AttachComposition(json, &ctx);
|
|
|
|
|
2018-01-09 17:19:32 +00:00
|
|
|
// In case the client calls render before the first tick.
|
|
|
|
this->animationTick(0);
|
|
|
|
|
2017-12-30 17:27:00 +00:00
|
|
|
LOG("** Attached %d animators\n", fAnimators.count());
|
|
|
|
}
|
|
|
|
|
|
|
|
Animation::~Animation() = default;
|
|
|
|
|
2018-01-08 13:25:27 +00:00
|
|
|
void Animation::render(SkCanvas* canvas, const SkRect* dstR) const {
|
2017-12-30 17:27:00 +00:00
|
|
|
if (!fDom)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sksg::InvalidationController ic;
|
|
|
|
fDom->revalidate(&ic, SkMatrix::I());
|
|
|
|
|
|
|
|
// TODO: proper inval
|
2018-01-08 13:25:27 +00:00
|
|
|
SkAutoCanvasRestore restore(canvas, true);
|
|
|
|
const SkRect srcR = SkRect::MakeSize(this->size());
|
|
|
|
if (dstR) {
|
|
|
|
canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit));
|
|
|
|
}
|
|
|
|
canvas->clipRect(srcR);
|
2017-12-30 17:27:00 +00:00
|
|
|
fDom->render(canvas);
|
|
|
|
|
|
|
|
if (!fShowInval)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SkPaint fill, stroke;
|
|
|
|
fill.setAntiAlias(true);
|
|
|
|
fill.setColor(0x40ff0000);
|
|
|
|
stroke.setAntiAlias(true);
|
|
|
|
stroke.setColor(0xffff0000);
|
|
|
|
stroke.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
for (const auto& r : ic) {
|
|
|
|
canvas->drawRect(r, fill);
|
|
|
|
canvas->drawRect(r, stroke);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animation::animationTick(SkMSec ms) {
|
|
|
|
// 't' in the BM model really means 'frame #'
|
|
|
|
auto t = static_cast<float>(ms) * fFrameRate / 1000;
|
|
|
|
|
|
|
|
t = fInPoint + std::fmod(t, fOutPoint - fInPoint);
|
|
|
|
|
|
|
|
// TODO: this can be optimized quite a bit with some sorting/state tracking.
|
|
|
|
for (const auto& a : fAnimators) {
|
|
|
|
a->tick(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace skotty
|