Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 8 | #include "modules/skottie/src/Layer.h" |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 9 | |
Florin Malita | a3936c8 | 2020-01-06 12:17:27 -0500 | [diff] [blame] | 10 | #include "modules/skottie/src/Camera.h" |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 11 | #include "modules/skottie/src/Composition.h" |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 12 | #include "modules/skottie/src/SkottieJson.h" |
| 13 | #include "modules/skottie/src/effects/Effects.h" |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 14 | #include "modules/skottie/src/effects/MotionBlurEffect.h" |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 15 | #include "modules/sksg/include/SkSGClipEffect.h" |
| 16 | #include "modules/sksg/include/SkSGDraw.h" |
| 17 | #include "modules/sksg/include/SkSGGroup.h" |
| 18 | #include "modules/sksg/include/SkSGMaskEffect.h" |
| 19 | #include "modules/sksg/include/SkSGMerge.h" |
| 20 | #include "modules/sksg/include/SkSGPaint.h" |
| 21 | #include "modules/sksg/include/SkSGPath.h" |
| 22 | #include "modules/sksg/include/SkSGRect.h" |
| 23 | #include "modules/sksg/include/SkSGRenderEffect.h" |
| 24 | #include "modules/sksg/include/SkSGRenderNode.h" |
| 25 | #include "modules/sksg/include/SkSGTransform.h" |
| 26 | |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 27 | namespace skottie { |
| 28 | namespace internal { |
| 29 | |
| 30 | namespace { |
| 31 | |
Florin Malita | 8b70f4d | 2020-04-23 18:58:21 -0400 | [diff] [blame] | 32 | static constexpr size_t kNullLayerType = 3; |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 33 | |
| 34 | struct MaskInfo { |
| 35 | SkBlendMode fBlendMode; // used when masking with layers/blending |
| 36 | sksg::Merge::Mode fMergeMode; // used when clipping |
| 37 | bool fInvertGeometry; |
| 38 | }; |
| 39 | |
| 40 | const MaskInfo* GetMaskInfo(char mode) { |
| 41 | static constexpr MaskInfo k_add_info = |
| 42 | { SkBlendMode::kSrcOver , sksg::Merge::Mode::kUnion , false }; |
| 43 | static constexpr MaskInfo k_int_info = |
| 44 | { SkBlendMode::kSrcIn , sksg::Merge::Mode::kIntersect , false }; |
| 45 | // AE 'subtract' is the same as 'intersect' + inverted geometry |
| 46 | // (draws the opacity-adjusted paint *outside* the shape). |
| 47 | static constexpr MaskInfo k_sub_info = |
| 48 | { SkBlendMode::kSrcIn , sksg::Merge::Mode::kIntersect , true }; |
| 49 | static constexpr MaskInfo k_dif_info = |
| 50 | { SkBlendMode::kDifference, sksg::Merge::Mode::kDifference, false }; |
| 51 | |
| 52 | switch (mode) { |
| 53 | case 'a': return &k_add_info; |
| 54 | case 'f': return &k_dif_info; |
| 55 | case 'i': return &k_int_info; |
| 56 | case 's': return &k_sub_info; |
| 57 | default: break; |
| 58 | } |
| 59 | |
| 60 | return nullptr; |
| 61 | } |
| 62 | |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 63 | class MaskAdapter final : public AnimatablePropertyContainer { |
| 64 | public: |
| 65 | MaskAdapter(const skjson::ObjectValue& jmask, const AnimationBuilder& abuilder, SkBlendMode bm) |
| 66 | : fMaskPaint(sksg::Color::Make(SK_ColorBLACK)) { |
| 67 | fMaskPaint->setAntiAlias(true); |
| 68 | fMaskPaint->setBlendMode(bm); |
| 69 | |
| 70 | this->bind(abuilder, jmask["o"], fOpacity); |
| 71 | |
| 72 | if (this->bind(abuilder, jmask["f"], fFeather)) { |
| 73 | fMaskFilter = sksg::BlurImageFilter::Make(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | bool hasEffect() const { |
| 78 | return !this->isStatic() |
| 79 | || fOpacity < 100 |
Florin Malita | 6509bc7 | 2020-03-12 11:11:26 -0400 | [diff] [blame] | 80 | || fFeather != SkV2{0,0}; |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | sk_sp<sksg::RenderNode> makeMask(sk_sp<sksg::Path> mask_path) const { |
| 84 | auto mask = sksg::Draw::Make(std::move(mask_path), fMaskPaint); |
| 85 | |
| 86 | // Optional mask blur (feather). |
| 87 | return sksg::ImageFilterEffect::Make(std::move(mask), fMaskFilter); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | void onSync() override { |
| 92 | fMaskPaint->setOpacity(fOpacity * 0.01f); |
| 93 | if (fMaskFilter) { |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 94 | // Close enough to AE. |
| 95 | static constexpr SkScalar kFeatherToSigma = 0.38f; |
Florin Malita | 6509bc7 | 2020-03-12 11:11:26 -0400 | [diff] [blame] | 96 | fMaskFilter->setSigma({fFeather.x * kFeatherToSigma, |
| 97 | fFeather.y * kFeatherToSigma}); |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | const sk_sp<sksg::PaintNode> fMaskPaint; |
| 102 | sk_sp<sksg::BlurImageFilter> fMaskFilter; // optional "feather" |
| 103 | |
Florin Malita | 6509bc7 | 2020-03-12 11:11:26 -0400 | [diff] [blame] | 104 | Vec2Value fFeather = {0,0}; |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 105 | ScalarValue fOpacity = 100; |
| 106 | }; |
| 107 | |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 108 | sk_sp<sksg::RenderNode> AttachMask(const skjson::ArrayValue* jmask, |
| 109 | const AnimationBuilder* abuilder, |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 110 | sk_sp<sksg::RenderNode> childNode) { |
| 111 | if (!jmask) return childNode; |
| 112 | |
| 113 | struct MaskRecord { |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 114 | sk_sp<sksg::Path> mask_path; // for clipping and masking |
| 115 | sk_sp<MaskAdapter> mask_adapter; // for masking |
| 116 | sksg::Merge::Mode merge_mode; // for clipping |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | SkSTArray<4, MaskRecord, true> mask_stack; |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 120 | bool has_effect = false; |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 121 | |
| 122 | for (const skjson::ObjectValue* m : *jmask) { |
| 123 | if (!m) continue; |
| 124 | |
| 125 | const skjson::StringValue* jmode = (*m)["mode"]; |
| 126 | if (!jmode || jmode->size() != 1) { |
| 127 | abuilder->log(Logger::Level::kError, &(*m)["mode"], "Invalid mask mode."); |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | const auto mode = *jmode->begin(); |
| 132 | if (mode == 'n') { |
| 133 | // "None" masks have no effect. |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | const auto* mask_info = GetMaskInfo(mode); |
| 138 | if (!mask_info) { |
| 139 | abuilder->log(Logger::Level::kWarning, nullptr, "Unsupported mask mode: '%c'.", mode); |
| 140 | continue; |
| 141 | } |
| 142 | |
Florin Malita | fd91f6d | 2019-07-25 11:41:07 -0400 | [diff] [blame] | 143 | auto mask_path = abuilder->attachPath((*m)["pt"]); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 144 | if (!mask_path) { |
| 145 | abuilder->log(Logger::Level::kError, m, "Could not parse mask path."); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | // "inv" is cumulative with mask info fInvertGeometry |
| 150 | const auto inverted = |
| 151 | (mask_info->fInvertGeometry != ParseDefault<bool>((*m)["inv"], false)); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 152 | mask_path->setFillType(inverted ? SkPathFillType::kInverseWinding |
| 153 | : SkPathFillType::kWinding); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 154 | |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 155 | const auto blend_mode = mask_stack.empty() ? SkBlendMode::kSrc |
| 156 | : mask_info->fBlendMode; |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 157 | |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 158 | auto mask_adapter = sk_make_sp<MaskAdapter>(*m, *abuilder, blend_mode); |
| 159 | abuilder->attachDiscardableAdapter(mask_adapter); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 160 | |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 161 | has_effect |= mask_adapter->hasEffect(); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 162 | |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 163 | |
| 164 | mask_stack.push_back({ std::move(mask_path), |
| 165 | std::move(mask_adapter), |
| 166 | mask_info->fMergeMode }); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 169 | |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 170 | if (mask_stack.empty()) |
| 171 | return childNode; |
| 172 | |
| 173 | // If the masks are fully opaque, we can clip. |
| 174 | if (!has_effect) { |
| 175 | sk_sp<sksg::GeometryNode> clip_node; |
| 176 | |
| 177 | if (mask_stack.count() == 1) { |
| 178 | // Single path -> just clip. |
| 179 | clip_node = std::move(mask_stack.front().mask_path); |
| 180 | } else { |
| 181 | // Multiple clip paths -> merge. |
| 182 | std::vector<sksg::Merge::Rec> merge_recs; |
| 183 | merge_recs.reserve(SkToSizeT(mask_stack.count())); |
| 184 | |
| 185 | for (auto& mask : mask_stack) { |
| 186 | const auto mode = merge_recs.empty() ? sksg::Merge::Mode::kMerge : mask.merge_mode; |
| 187 | merge_recs.push_back({std::move(mask.mask_path), mode}); |
| 188 | } |
| 189 | clip_node = sksg::Merge::Make(std::move(merge_recs)); |
| 190 | } |
| 191 | |
| 192 | return sksg::ClipEffect::Make(std::move(childNode), std::move(clip_node), true); |
| 193 | } |
| 194 | |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 195 | // Complex masks (non-opaque or blurred) turn into a mask node stack. |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 196 | sk_sp<sksg::RenderNode> maskNode; |
| 197 | if (mask_stack.count() == 1) { |
| 198 | // no group needed for single mask |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 199 | const auto rec = mask_stack.front(); |
| 200 | maskNode = rec.mask_adapter->makeMask(std::move(rec.mask_path)); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 201 | } else { |
| 202 | std::vector<sk_sp<sksg::RenderNode>> masks; |
| 203 | masks.reserve(SkToSizeT(mask_stack.count())); |
| 204 | for (auto& rec : mask_stack) { |
Florin Malita | af99f3e | 2020-02-03 12:09:15 -0500 | [diff] [blame] | 205 | masks.push_back(rec.mask_adapter->makeMask(std::move(rec.mask_path))); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | maskNode = sksg::Group::Make(std::move(masks)); |
| 209 | } |
| 210 | |
| 211 | return sksg::MaskEffect::Make(std::move(childNode), std::move(maskNode)); |
| 212 | } |
| 213 | |
Florin Malita | bb7d95f | 2020-03-26 15:58:56 -0400 | [diff] [blame] | 214 | class LayerController final : public Animator { |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 215 | public: |
Florin Malita | bb7d95f | 2020-03-26 15:58:56 -0400 | [diff] [blame] | 216 | LayerController(AnimatorScope&& layer_animators, |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 217 | sk_sp<sksg::RenderNode> layer, |
| 218 | size_t tanim_count, float in, float out) |
| 219 | : fLayerAnimators(std::move(layer_animators)) |
| 220 | , fLayerNode(std::move(layer)) |
| 221 | , fTransformAnimatorsCount(tanim_count) |
| 222 | , fIn(in) |
| 223 | , fOut(out) {} |
| 224 | |
| 225 | protected: |
Florin Malita | 01f2ce0 | 2020-03-31 05:44:28 -0400 | [diff] [blame] | 226 | StateChanged onSeek(float t) override { |
Florin Malita | c4fae74 | 2020-02-27 15:50:04 -0500 | [diff] [blame] | 227 | // in/out may be inverted for time-reversed layers |
| 228 | const auto active = (t >= fIn && t < fOut) || (t > fOut && t <= fIn); |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 229 | |
Florin Malita | 01f2ce0 | 2020-03-31 05:44:28 -0400 | [diff] [blame] | 230 | bool changed = false; |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 231 | if (fLayerNode) { |
Florin Malita | 01f2ce0 | 2020-03-31 05:44:28 -0400 | [diff] [blame] | 232 | changed |= (fLayerNode->isVisible() != active); |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 233 | fLayerNode->setVisible(active); |
| 234 | } |
| 235 | |
| 236 | // When active, dispatch ticks to all layer animators. |
| 237 | // When inactive, we must still dispatch ticks to the layer transform animators |
| 238 | // (active child layers depend on transforms being updated). |
| 239 | const auto dispatch_count = active ? fLayerAnimators.size() |
| 240 | : fTransformAnimatorsCount; |
| 241 | for (size_t i = 0; i < dispatch_count; ++i) { |
Florin Malita | 01f2ce0 | 2020-03-31 05:44:28 -0400 | [diff] [blame] | 242 | changed |= fLayerAnimators[i]->seek(t); |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 243 | } |
Florin Malita | 0147de4 | 2020-03-27 09:49:03 -0400 | [diff] [blame] | 244 | |
Florin Malita | 01f2ce0 | 2020-03-31 05:44:28 -0400 | [diff] [blame] | 245 | return changed; |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | private: |
Florin Malita | bb7d95f | 2020-03-26 15:58:56 -0400 | [diff] [blame] | 249 | const AnimatorScope fLayerAnimators; |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 250 | const sk_sp<sksg::RenderNode> fLayerNode; |
| 251 | const size_t fTransformAnimatorsCount; |
| 252 | const float fIn, |
| 253 | fOut; |
| 254 | }; |
| 255 | |
Florin Malita | bb7d95f | 2020-03-26 15:58:56 -0400 | [diff] [blame] | 256 | class MotionBlurController final : public Animator { |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 257 | public: |
| 258 | explicit MotionBlurController(sk_sp<MotionBlurEffect> mbe) |
| 259 | : fMotionBlurEffect(std::move(mbe)) {} |
| 260 | |
| 261 | protected: |
| 262 | // When motion blur is present, time ticks are not passed to layer animators |
| 263 | // but to the motion blur effect. The effect then drives the animators/scene-graph |
| 264 | // during reval and render phases. |
Florin Malita | 01f2ce0 | 2020-03-31 05:44:28 -0400 | [diff] [blame] | 265 | StateChanged onSeek(float t) override { |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 266 | fMotionBlurEffect->setT(t); |
Florin Malita | 0147de4 | 2020-03-27 09:49:03 -0400 | [diff] [blame] | 267 | return true; |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | private: |
| 271 | const sk_sp<MotionBlurEffect> fMotionBlurEffect; |
| 272 | }; |
| 273 | |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 274 | } // namespace |
| 275 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 276 | LayerBuilder::LayerBuilder(const skjson::ObjectValue& jlayer) |
| 277 | : fJlayer(jlayer) |
| 278 | , fIndex(ParseDefault<int>(jlayer["ind"], -1)) |
| 279 | , fParentIndex(ParseDefault<int>(jlayer["parent"], -1)) |
| 280 | , fType(ParseDefault<int>(jlayer["ty"], -1)) { |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 281 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 282 | if (this->isCamera() || ParseDefault<int>(jlayer["ddd"], 0)) { |
| 283 | fFlags |= Flags::kIs3D; |
Florin Malita | 60c84fd | 2019-07-02 18:02:54 -0400 | [diff] [blame] | 284 | } |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 287 | LayerBuilder::~LayerBuilder() = default; |
| 288 | |
| 289 | bool LayerBuilder::isCamera() const { |
| 290 | static constexpr int kCameraLayerType = 13; |
| 291 | |
| 292 | return fType == kCameraLayerType; |
| 293 | } |
| 294 | |
| 295 | sk_sp<sksg::Transform> LayerBuilder::buildTransform(const AnimationBuilder& abuilder, |
| 296 | CompositionBuilder* cbuilder) { |
| 297 | // Depending on the leaf node type, we treat the whole transform chain as either 2D or 3D. |
| 298 | const auto transform_chain_type = this->is3D() ? TransformType::k3D |
| 299 | : TransformType::k2D; |
| 300 | fLayerTransform = this->getTransform(abuilder, cbuilder, transform_chain_type); |
| 301 | |
| 302 | return fLayerTransform; |
| 303 | } |
| 304 | |
| 305 | sk_sp<sksg::Transform> LayerBuilder::getTransform(const AnimationBuilder& abuilder, |
| 306 | CompositionBuilder* cbuilder, |
| 307 | TransformType ttype) { |
| 308 | const auto cache_valid_mask = (1ul << ttype); |
| 309 | if (!(fFlags & cache_valid_mask)) { |
| 310 | // Set valid flag upfront to break cycles. |
| 311 | fFlags |= cache_valid_mask; |
| 312 | |
| 313 | const AnimationBuilder::AutoPropertyTracker apt(&abuilder, fJlayer); |
| 314 | AnimationBuilder::AutoScope ascope(&abuilder, std::move(fLayerScope)); |
| 315 | fTransformCache[ttype] = this->doAttachTransform(abuilder, cbuilder, ttype); |
| 316 | fLayerScope = ascope.release(); |
| 317 | fTransformAnimatorCount = fLayerScope.size(); |
Florin Malita | d9e8586 | 2019-10-17 13:46:33 -0400 | [diff] [blame] | 318 | } |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 319 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 320 | return fTransformCache[ttype]; |
| 321 | } |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 322 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 323 | sk_sp<sksg::Transform> LayerBuilder::getParentTransform(const AnimationBuilder& abuilder, |
| 324 | CompositionBuilder* cbuilder, |
| 325 | TransformType ttype) { |
| 326 | if (auto* parent_builder = cbuilder->layerBuilder(fParentIndex)) { |
| 327 | // Explicit parent layer. |
| 328 | return parent_builder->getTransform(abuilder, cbuilder, ttype); |
| 329 | } |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 330 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 331 | if (ttype == TransformType::k3D) { |
| 332 | // During camera transform attachment, cbuilder->getCameraTransform() is null. |
| 333 | // This prevents camera->camera transform chain cycles. |
| 334 | SkASSERT(!this->isCamera() || !cbuilder->getCameraTransform()); |
| 335 | |
| 336 | // 3D transform chains are implicitly rooted onto the camera. |
| 337 | return cbuilder->getCameraTransform(); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | return nullptr; |
| 341 | } |
| 342 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 343 | sk_sp<sksg::Transform> LayerBuilder::doAttachTransform(const AnimationBuilder& abuilder, |
| 344 | CompositionBuilder* cbuilder, |
| 345 | TransformType ttype) { |
| 346 | const skjson::ObjectValue* jtransform = fJlayer["ks"]; |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 347 | if (!jtransform) { |
| 348 | return nullptr; |
| 349 | } |
| 350 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 351 | auto parent_transform = this->getParentTransform(abuilder, cbuilder, ttype); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 352 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 353 | if (this->isCamera()) { |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 354 | // parent_transform applies to the camera itself => it pre-composes inverted to the |
| 355 | // camera/view/adapter transform. |
| 356 | // |
| 357 | // T_camera' = T_camera x Inv(parent_transform) |
| 358 | // |
Florin Malita | e7bd58f | 2020-01-27 11:58:49 -0500 | [diff] [blame] | 359 | return abuilder.attachCamera(fJlayer, |
| 360 | *jtransform, |
| 361 | sksg::Transform::MakeInverse(std::move(parent_transform)), |
| 362 | cbuilder->fSize); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 363 | } |
| 364 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 365 | return this->is3D() |
| 366 | ? abuilder.attachMatrix3D(*jtransform, std::move(parent_transform)) |
| 367 | : abuilder.attachMatrix2D(*jtransform, std::move(parent_transform)); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 368 | } |
| 369 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 370 | bool LayerBuilder::hasMotionBlur(const CompositionBuilder* cbuilder) const { |
| 371 | return cbuilder->fMotionBlurSamples > 1 |
| 372 | && cbuilder->fMotionBlurAngle > 0 |
| 373 | && ParseDefault(fJlayer["mb"], false); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 374 | } |
| 375 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 376 | sk_sp<sksg::RenderNode> LayerBuilder::buildRenderTree(const AnimationBuilder& abuilder, |
Florin Malita | 46a331b | 2019-12-12 15:43:53 -0500 | [diff] [blame] | 377 | CompositionBuilder* cbuilder, |
| 378 | const LayerBuilder* prev_layer) { |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 379 | AnimationBuilder::LayerInfo layer_info = { |
Florin Malita | 6cc4953 | 2019-12-05 09:06:22 -0500 | [diff] [blame] | 380 | cbuilder->fSize, |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 381 | ParseDefault<float>(fJlayer["ip"], 0.0f), |
| 382 | ParseDefault<float>(fJlayer["op"], 0.0f), |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 383 | }; |
Florin Malita | c4fae74 | 2020-02-27 15:50:04 -0500 | [diff] [blame] | 384 | if (SkScalarNearlyEqual(layer_info.fInPoint, layer_info.fOutPoint)) { |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 385 | abuilder.log(Logger::Level::kError, nullptr, |
| 386 | "Invalid layer in/out points: %f/%f.", |
| 387 | layer_info.fInPoint, layer_info.fOutPoint); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 388 | return nullptr; |
| 389 | } |
| 390 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 391 | const AnimationBuilder::AutoPropertyTracker apt(&abuilder, fJlayer); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 392 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 393 | using LayerBuilder = |
| 394 | sk_sp<sksg::RenderNode> (AnimationBuilder::*)(const skjson::ObjectValue&, |
| 395 | AnimationBuilder::LayerInfo*) const; |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 396 | |
| 397 | // AE is annoyingly inconsistent in how effects interact with layer transforms: depending on |
| 398 | // the layer type, effects are applied before or after the content is transformed. |
| 399 | // |
| 400 | // Empirically, pre-rendered layers (for some loose meaning of "pre-rendered") are in the |
| 401 | // former category (effects are subject to transformation), while the remaining types are in |
| 402 | // the latter. |
| 403 | enum : uint32_t { |
| 404 | kTransformEffects = 1, // The layer transform also applies to its effects. |
| 405 | }; |
| 406 | |
| 407 | static constexpr struct { |
Florin Malita | 60c84fd | 2019-07-02 18:02:54 -0400 | [diff] [blame] | 408 | LayerBuilder fBuilder; |
| 409 | uint32_t fFlags; |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 410 | } gLayerBuildInfo[] = { |
Florin Malita | 8b70f4d | 2020-04-23 18:58:21 -0400 | [diff] [blame] | 411 | { &AnimationBuilder::attachPrecompLayer, kTransformEffects }, // 'ty': 0 -> precomp |
| 412 | { &AnimationBuilder::attachSolidLayer , kTransformEffects }, // 'ty': 1 -> solid |
Florin Malita | 71c3141 | 2020-05-07 16:44:18 -0400 | [diff] [blame^] | 413 | { &AnimationBuilder::attachFootageLayer, kTransformEffects }, // 'ty': 2 -> image |
Florin Malita | 8b70f4d | 2020-04-23 18:58:21 -0400 | [diff] [blame] | 414 | { &AnimationBuilder::attachNullLayer , 0 }, // 'ty': 3 -> null |
| 415 | { &AnimationBuilder::attachShapeLayer , 0 }, // 'ty': 4 -> shape |
| 416 | { &AnimationBuilder::attachTextLayer , 0 }, // 'ty': 5 -> text |
| 417 | { nullptr , 0 }, // 'ty': 6 -> audio |
| 418 | { nullptr , 0 }, // 'ty': 7 -> pholderVideo |
| 419 | { nullptr , 0 }, // 'ty': 8 -> imageSeq |
Florin Malita | 71c3141 | 2020-05-07 16:44:18 -0400 | [diff] [blame^] | 420 | { &AnimationBuilder::attachFootageLayer, kTransformEffects }, // 'ty': 9 -> video |
Florin Malita | 8b70f4d | 2020-04-23 18:58:21 -0400 | [diff] [blame] | 421 | { nullptr , 0 }, // 'ty': 10 -> pholderStill |
| 422 | { nullptr , 0 }, // 'ty': 11 -> guide |
| 423 | { nullptr , 0 }, // 'ty': 12 -> adjustment |
| 424 | { &AnimationBuilder::attachNullLayer , 0 }, // 'ty': 13 -> camera |
| 425 | { nullptr , 0 }, // 'ty': 14 -> light |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 426 | }; |
| 427 | |
Florin Malita | 8b70f4d | 2020-04-23 18:58:21 -0400 | [diff] [blame] | 428 | // Treat all hidden layers as null. |
| 429 | const auto type = ParseDefault<bool>(fJlayer["hd"], false) |
| 430 | ? kNullLayerType |
| 431 | : SkToSizeT(fType); |
| 432 | |
| 433 | if (type >= SK_ARRAY_COUNT(gLayerBuildInfo)) { |
Florin Malita | 60c84fd | 2019-07-02 18:02:54 -0400 | [diff] [blame] | 434 | return nullptr; |
| 435 | } |
| 436 | |
Florin Malita | 8b70f4d | 2020-04-23 18:58:21 -0400 | [diff] [blame] | 437 | const auto& build_info = gLayerBuildInfo[type]; |
| 438 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 439 | // Switch to the layer animator scope (which at this point holds transform-only animators). |
| 440 | AnimationBuilder::AutoScope ascope(&abuilder, std::move(fLayerScope)); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 441 | |
Florin Malita | 8b70f4d | 2020-04-23 18:58:21 -0400 | [diff] [blame] | 442 | // Potentially null. |
| 443 | sk_sp<sksg::RenderNode> layer; |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 444 | |
| 445 | // Build the layer content fragment. |
Florin Malita | 8b70f4d | 2020-04-23 18:58:21 -0400 | [diff] [blame] | 446 | if (build_info.fBuilder) { |
| 447 | layer = (abuilder.*(build_info.fBuilder))(fJlayer, &layer_info); |
| 448 | } |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 449 | |
| 450 | // Clip layers with explicit dimensions. |
| 451 | float w = 0, h = 0; |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 452 | if (Parse<float>(fJlayer["w"], &w) && Parse<float>(fJlayer["h"], &h)) { |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 453 | layer = sksg::ClipEffect::Make(std::move(layer), |
| 454 | sksg::Rect::Make(SkRect::MakeWH(w, h)), |
| 455 | true); |
| 456 | } |
| 457 | |
| 458 | // Optional layer mask. |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 459 | layer = AttachMask(fJlayer["masksProperties"], &abuilder, std::move(layer)); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 460 | |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 461 | // Does the transform apply to effects also? |
| 462 | // (AE quirk: it doesn't - except for solid layers) |
| 463 | const auto transform_effects = (build_info.fFlags & kTransformEffects); |
| 464 | |
| 465 | // Attach the transform before effects, when needed. |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 466 | if (fLayerTransform && !transform_effects) { |
| 467 | layer = sksg::TransformEffect::Make(std::move(layer), fLayerTransform); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | // Optional layer effects. |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 471 | if (const skjson::ArrayValue* jeffects = fJlayer["ef"]) { |
| 472 | layer = EffectBuilder(&abuilder, layer_info.fSize).attachEffects(*jeffects, |
| 473 | std::move(layer)); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // Attach the transform after effects, when needed. |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 477 | if (fLayerTransform && transform_effects) { |
| 478 | layer = sksg::TransformEffect::Make(std::move(layer), std::move(fLayerTransform)); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 479 | } |
| 480 | |
Florin Malita | ae58199 | 2020-03-24 21:18:42 -0400 | [diff] [blame] | 481 | // Optional layer styles. |
| 482 | if (const skjson::ArrayValue* jstyles = fJlayer["sy"]) { |
| 483 | layer = EffectBuilder(&abuilder, layer_info.fSize).attachStyles(*jstyles, std::move(layer)); |
| 484 | } |
| 485 | |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 486 | // Optional layer opacity. |
| 487 | // TODO: de-dupe this "ks" lookup with matrix above. |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 488 | if (const skjson::ObjectValue* jtransform = fJlayer["ks"]) { |
| 489 | layer = abuilder.attachOpacity(*jtransform, std::move(layer)); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 490 | } |
| 491 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 492 | const auto has_animators = !abuilder.fCurrentAnimatorScope->empty(); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 493 | |
Florin Malita | bb7d95f | 2020-03-26 15:58:56 -0400 | [diff] [blame] | 494 | sk_sp<Animator> controller = sk_make_sp<LayerController>(ascope.release(), |
| 495 | layer, |
| 496 | fTransformAnimatorCount, |
| 497 | layer_info.fInPoint, |
| 498 | layer_info.fOutPoint); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 499 | |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 500 | // Optional motion blur. |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 501 | if (layer && has_animators && this->hasMotionBlur(cbuilder)) { |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 502 | // Wrap both the layer node and the controller. |
| 503 | auto motion_blur = MotionBlurEffect::Make(std::move(controller), std::move(layer), |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 504 | cbuilder->fMotionBlurSamples, |
| 505 | cbuilder->fMotionBlurAngle, |
| 506 | cbuilder->fMotionBlurPhase); |
Florin Malita | 5f24018 | 2019-07-23 17:28:53 -0400 | [diff] [blame] | 507 | controller = sk_make_sp<MotionBlurController>(motion_blur); |
Florin Malita | 5f1108c | 2019-07-03 10:09:31 -0400 | [diff] [blame] | 508 | layer = std::move(motion_blur); |
| 509 | } |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 510 | |
Florin Malita | c6fbedc | 2019-10-28 13:57:12 -0400 | [diff] [blame] | 511 | abuilder.fCurrentAnimatorScope->push_back(std::move(controller)); |
Florin Malita | 60c84fd | 2019-07-02 18:02:54 -0400 | [diff] [blame] | 512 | |
Florin Malita | 46a331b | 2019-12-12 15:43:53 -0500 | [diff] [blame] | 513 | // Stash the content tree in case it is needed for later mattes. |
| 514 | fContentTree = layer; |
| 515 | |
| 516 | if (ParseDefault<bool>(fJlayer["td"], false)) { |
| 517 | // |layer| is a track matte. We apply it as a mask to the next layer. |
Florin Malita | 60c84fd | 2019-07-02 18:02:54 -0400 | [diff] [blame] | 518 | return nullptr; |
| 519 | } |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 520 | |
Florin Malita | 46a331b | 2019-12-12 15:43:53 -0500 | [diff] [blame] | 521 | // Optional matte. |
| 522 | size_t matte_mode; |
| 523 | if (prev_layer && Parse(fJlayer["tt"], &matte_mode)) { |
| 524 | static constexpr sksg::MaskEffect::Mode gMatteModes[] = { |
Florin Malita | 59e72b7 | 2019-10-16 08:51:09 -0400 | [diff] [blame] | 525 | sksg::MaskEffect::Mode::kAlphaNormal, // tt: 1 |
| 526 | sksg::MaskEffect::Mode::kAlphaInvert, // tt: 2 |
| 527 | sksg::MaskEffect::Mode::kLumaNormal, // tt: 3 |
| 528 | sksg::MaskEffect::Mode::kLumaInvert, // tt: 4 |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 529 | }; |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 530 | |
Florin Malita | 46a331b | 2019-12-12 15:43:53 -0500 | [diff] [blame] | 531 | if (matte_mode > 0 && matte_mode <= SK_ARRAY_COUNT(gMatteModes)) { |
| 532 | // The current layer is masked with the previous layer *content*. |
Florin Malita | 512eb94 | 2019-10-28 21:22:42 -0400 | [diff] [blame] | 533 | layer = sksg::MaskEffect::Make(std::move(layer), |
Florin Malita | 46a331b | 2019-12-12 15:43:53 -0500 | [diff] [blame] | 534 | prev_layer->fContentTree, |
| 535 | gMatteModes[matte_mode - 1]); |
| 536 | } else { |
| 537 | abuilder.log(Logger::Level::kError, nullptr, |
| 538 | "Unknown track matte mode: %zu\n", matte_mode); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 539 | } |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 540 | } |
| 541 | |
Florin Malita | 46a331b | 2019-12-12 15:43:53 -0500 | [diff] [blame] | 542 | // Finally, attach an optional blend mode. |
| 543 | // NB: blend modes are never applied to matte sources (layer content only). |
| 544 | return abuilder.attachBlendMode(fJlayer, std::move(layer)); |
Florin Malita | 45dc1f0 | 2019-07-01 13:57:43 -0400 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | } // namespace internal |
| 548 | } // namespace skottie |