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
8 #include "modules/skottie/src/Composition.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/private/SkTPin.h"
12 #include "modules/skottie/src/Camera.h"
13 #include "modules/skottie/src/SkottieJson.h"
14 #include "modules/skottie/src/SkottiePriv.h"
15 #include "modules/sksg/include/SkSGGroup.h"
16
17 #include <algorithm>
18
19 namespace skottie {
20 namespace internal {
21
ScopedAssetRef(const AnimationBuilder * abuilder,const skjson::ObjectValue & jlayer)22 AnimationBuilder::ScopedAssetRef::ScopedAssetRef(const AnimationBuilder* abuilder,
23 const skjson::ObjectValue& jlayer) {
24 const auto refId = ParseDefault<SkString>(jlayer["refId"], SkString());
25 if (refId.isEmpty()) {
26 abuilder->log(Logger::Level::kError, nullptr, "Layer missing refId.");
27 return;
28 }
29
30 const auto* asset_info = abuilder->fAssets.find(refId);
31 if (!asset_info) {
32 abuilder->log(Logger::Level::kError, nullptr, "Asset not found: '%s'.", refId.c_str());
33 return;
34 }
35
36 if (asset_info->fIsAttaching) {
37 abuilder->log(Logger::Level::kError, nullptr,
38 "Asset cycle detected for: '%s'", refId.c_str());
39 return;
40 }
41
42 asset_info->fIsAttaching = true;
43
44 fInfo = asset_info;
45 }
46
CompositionBuilder(const AnimationBuilder & abuilder,const SkSize & size,const skjson::ObjectValue & jcomp)47 CompositionBuilder::CompositionBuilder(const AnimationBuilder& abuilder,
48 const SkSize& size,
49 const skjson::ObjectValue& jcomp)
50 : fSize(size) {
51
52 // Optional motion blur params.
53 if (const skjson::ObjectValue* jmb = jcomp["mb"]) {
54 static constexpr size_t kMaxSamplesPerFrame = 64;
55 fMotionBlurSamples = std::min(ParseDefault<size_t>((*jmb)["spf"], 1ul),
56 kMaxSamplesPerFrame);
57 fMotionBlurAngle = SkTPin(ParseDefault((*jmb)["sa"], 0.0f), 0.0f, 720.0f);
58 fMotionBlurPhase = SkTPin(ParseDefault((*jmb)["sp"], 0.0f), -360.0f, 360.0f);
59 }
60
61 int camera_builder_index = -1;
62
63 // Prepare layer builders.
64 if (const skjson::ArrayValue* jlayers = jcomp["layers"]) {
65 fLayerBuilders.reserve(SkToInt(jlayers->size()));
66 for (const skjson::ObjectValue* jlayer : *jlayers) {
67 if (!jlayer) continue;
68
69 const auto lbuilder_index = fLayerBuilders.size();
70 fLayerBuilders.emplace_back(*jlayer, fSize);
71 const auto& lbuilder = fLayerBuilders.back();
72
73 fLayerIndexMap.set(lbuilder.index(), lbuilder_index);
74
75 // Keep track of the camera builder.
76 if (lbuilder.isCamera()) {
77 // We only support one (first) camera for now.
78 if (camera_builder_index < 0) {
79 camera_builder_index = SkToInt(lbuilder_index);
80 } else {
81 abuilder.log(Logger::Level::kWarning, jlayer,
82 "Ignoring duplicate camera layer.");
83 }
84 }
85 }
86 }
87
88 // Attach a camera transform upfront, if needed (required to build
89 // all other 3D transform chains).
90 if (camera_builder_index >= 0) {
91 // Explicit camera.
92 fCameraTransform = fLayerBuilders[camera_builder_index].buildTransform(abuilder, this);
93 } else if (ParseDefault<int>(jcomp["ddd"], 0)) {
94 // Default/implicit camera when 3D layers are present.
95 fCameraTransform = CameraAdaper::DefaultCameraTransform(fSize);
96 }
97 }
98
99 CompositionBuilder::~CompositionBuilder() = default;
100
layerBuilder(int layer_index)101 LayerBuilder* CompositionBuilder::layerBuilder(int layer_index) {
102 if (layer_index < 0) {
103 return nullptr;
104 }
105
106 if (const auto* idx = fLayerIndexMap.find(layer_index)) {
107 return &fLayerBuilders[SkToInt(*idx)];
108 }
109
110 return nullptr;
111 }
112
build(const AnimationBuilder & abuilder)113 sk_sp<sksg::RenderNode> CompositionBuilder::build(const AnimationBuilder& abuilder) {
114 // First pass - transitively attach layer transform chains.
115 for (auto& lbuilder : fLayerBuilders) {
116 lbuilder.buildTransform(abuilder, this);
117 }
118
119 // Second pass - attach actual layer contents and finalize the layer render tree.
120 std::vector<sk_sp<sksg::RenderNode>> layers;
121 layers.reserve(fLayerBuilders.size());
122
123 LayerBuilder* prev_layer = nullptr;
124 for (auto& lbuilder : fLayerBuilders) {
125 if (auto layer = lbuilder.buildRenderTree(abuilder, this, prev_layer)) {
126 layers.push_back(std::move(layer));
127 }
128 prev_layer = &lbuilder;
129 }
130
131 if (layers.empty()) {
132 return nullptr;
133 }
134
135 if (layers.size() == 1) {
136 return std::move(layers[0]);
137 }
138
139 // Layers are painted in bottom->top order.
140 std::reverse(layers.begin(), layers.end());
141 layers.shrink_to_fit();
142
143 return sksg::Group::Make(std::move(layers));
144 }
145
146 } // namespace internal
147 } // namespace skottie
148