1 /*
2 * Copyright 2018 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/utils/SkottieUtils.h"
9
10 #include "include/codec/SkCodec.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkImage.h"
13 #include "include/utils/SkAnimCodecPlayer.h"
14 #include "src/core/SkMakeUnique.h"
15 #include "src/core/SkOSFile.h"
16 #include "src/utils/SkOSPath.h"
17
18 #include <cmath>
19
20 namespace skottie_utils {
21
Make(sk_sp<SkData> data,bool predecode)22 sk_sp<MultiFrameImageAsset> MultiFrameImageAsset::Make(sk_sp<SkData> data, bool predecode) {
23 if (auto codec = SkCodec::MakeFromData(std::move(data))) {
24 return sk_sp<MultiFrameImageAsset>(
25 new MultiFrameImageAsset(skstd::make_unique<SkAnimCodecPlayer>(std::move(codec)),
26 predecode));
27 }
28
29 return nullptr;
30 }
31
MultiFrameImageAsset(std::unique_ptr<SkAnimCodecPlayer> player,bool predecode)32 MultiFrameImageAsset::MultiFrameImageAsset(std::unique_ptr<SkAnimCodecPlayer> player,
33 bool predecode)
34 : fPlayer(std::move(player))
35 , fPreDecode(predecode) {
36 SkASSERT(fPlayer);
37 }
38
isMultiFrame()39 bool MultiFrameImageAsset::isMultiFrame() {
40 return fPlayer->duration() > 0;
41 }
42
getFrame(float t)43 sk_sp<SkImage> MultiFrameImageAsset::getFrame(float t) {
44 auto decode = [](sk_sp<SkImage> image) {
45 SkASSERT(image->isLazyGenerated());
46
47 static constexpr size_t kMaxArea = 2048 * 2048;
48 const auto image_area = SkToSizeT(image->width() * image->height());
49
50 if (image_area > kMaxArea) {
51 // When the image is too large, decode and scale down to a reasonable size.
52 const auto scale = std::sqrt(static_cast<float>(kMaxArea) / image_area);
53 const auto info = SkImageInfo::MakeN32Premul(scale * image->width(),
54 scale * image->height());
55 SkBitmap bm;
56 if (bm.tryAllocPixels(info, info.minRowBytes()) &&
57 image->scalePixels(bm.pixmap(),
58 SkFilterQuality::kMedium_SkFilterQuality,
59 SkImage::kDisallow_CachingHint)) {
60 image = SkImage::MakeFromBitmap(bm);
61 }
62 } else {
63 // When the image size is OK, just force-decode.
64 image = image->makeRasterImage();
65 }
66
67 return image;
68 };
69
70 fPlayer->seek(static_cast<uint32_t>(t * 1000));
71 auto frame = fPlayer->getFrame();
72
73 if (fPreDecode && frame && frame->isLazyGenerated()) {
74 frame = decode(std::move(frame));
75 }
76
77 return frame;
78 }
79
Make(SkString base_dir)80 sk_sp<FileResourceProvider> FileResourceProvider::Make(SkString base_dir) {
81 return sk_isdir(base_dir.c_str())
82 ? sk_sp<FileResourceProvider>(new FileResourceProvider(std::move(base_dir)))
83 : nullptr;
84 }
85
FileResourceProvider(SkString base_dir)86 FileResourceProvider::FileResourceProvider(SkString base_dir) : fDir(std::move(base_dir)) {}
87
load(const char resource_path[],const char resource_name[]) const88 sk_sp<SkData> FileResourceProvider::load(const char resource_path[],
89 const char resource_name[]) const {
90 const auto full_dir = SkOSPath::Join(fDir.c_str() , resource_path),
91 full_path = SkOSPath::Join(full_dir.c_str(), resource_name);
92 return SkData::MakeFromFileName(full_path.c_str());
93 }
94
loadImageAsset(const char resource_path[],const char resource_name[],const char[]) const95 sk_sp<skottie::ImageAsset> FileResourceProvider::loadImageAsset(const char resource_path[],
96 const char resource_name[],
97 const char[]) const {
98 return MultiFrameImageAsset::Make(this->load(resource_path, resource_name));
99 }
100
101 class CustomPropertyManager::PropertyInterceptor final : public skottie::PropertyObserver {
102 public:
PropertyInterceptor(CustomPropertyManager * mgr)103 explicit PropertyInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {}
104
onColorProperty(const char node_name[],const LazyHandle<skottie::ColorPropertyHandle> & c)105 void onColorProperty(const char node_name[],
106 const LazyHandle<skottie::ColorPropertyHandle>& c) override {
107 const auto key = fMgr->acceptKey(node_name);
108 if (!key.empty()) {
109 fMgr->fColorMap[key].push_back(c());
110 }
111 }
112
onOpacityProperty(const char node_name[],const LazyHandle<skottie::OpacityPropertyHandle> & o)113 void onOpacityProperty(const char node_name[],
114 const LazyHandle<skottie::OpacityPropertyHandle>& o) override {
115 const auto key = fMgr->acceptKey(node_name);
116 if (!key.empty()) {
117 fMgr->fOpacityMap[key].push_back(o());
118 }
119 }
120
onTransformProperty(const char node_name[],const LazyHandle<skottie::TransformPropertyHandle> & t)121 void onTransformProperty(const char node_name[],
122 const LazyHandle<skottie::TransformPropertyHandle>& t) override {
123 const auto key = fMgr->acceptKey(node_name);
124 if (!key.empty()) {
125 fMgr->fTransformMap[key].push_back(t());
126 }
127 }
128
129 private:
130 CustomPropertyManager* fMgr;
131 };
132
133 class CustomPropertyManager::MarkerInterceptor final : public skottie::MarkerObserver {
134 public:
MarkerInterceptor(CustomPropertyManager * mgr)135 explicit MarkerInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {}
136
onMarker(const char name[],float t0,float t1)137 void onMarker(const char name[], float t0, float t1) override {
138 const auto key = fMgr->acceptKey(name);
139 if (!key.empty()) {
140 fMgr->fMarkers.push_back({ std::move(key), t0, t1 });
141 }
142 }
143
144 private:
145 CustomPropertyManager* fMgr;
146 };
147
CustomPropertyManager()148 CustomPropertyManager::CustomPropertyManager()
149 : fPropertyInterceptor(sk_make_sp<PropertyInterceptor>(this))
150 , fMarkerInterceptor(sk_make_sp<MarkerInterceptor>(this)) {}
151
152 CustomPropertyManager::~CustomPropertyManager() = default;
153
getPropertyObserver() const154 sk_sp<skottie::PropertyObserver> CustomPropertyManager::getPropertyObserver() const {
155 return fPropertyInterceptor;
156 }
157
getMarkerObserver() const158 sk_sp<skottie::MarkerObserver> CustomPropertyManager::getMarkerObserver() const {
159 return fMarkerInterceptor;
160 }
161
162 template <typename T>
163 std::vector<CustomPropertyManager::PropKey>
getProps(const PropMap<T> & container) const164 CustomPropertyManager::getProps(const PropMap<T>& container) const {
165 std::vector<PropKey> props;
166
167 for (const auto& prop_list : container) {
168 SkASSERT(!prop_list.second.empty());
169 props.push_back(prop_list.first);
170 }
171
172 return props;
173 }
174
175 template <typename V, typename T>
get(const PropKey & key,const PropMap<T> & container) const176 V CustomPropertyManager::get(const PropKey& key, const PropMap<T>& container) const {
177 auto prop_group = container.find(key);
178
179 return prop_group == container.end()
180 ? V()
181 : prop_group->second.front()->get();
182 }
183
184 template <typename V, typename T>
set(const PropKey & key,const V & val,const PropMap<T> & container)185 bool CustomPropertyManager::set(const PropKey& key, const V& val, const PropMap<T>& container) {
186 auto prop_group = container.find(key);
187
188 if (prop_group == container.end()) {
189 return false;
190 }
191
192 for (auto& handle : prop_group->second) {
193 handle->set(val);
194 }
195
196 return true;
197 }
198
199 std::vector<CustomPropertyManager::PropKey>
getColorProps() const200 CustomPropertyManager::getColorProps() const {
201 return this->getProps(fColorMap);
202 }
203
getColor(const PropKey & key) const204 skottie::ColorPropertyValue CustomPropertyManager::getColor(const PropKey& key) const {
205 return this->get<skottie::ColorPropertyValue>(key, fColorMap);
206 }
207
setColor(const PropKey & key,const skottie::ColorPropertyValue & c)208 bool CustomPropertyManager::setColor(const PropKey& key, const skottie::ColorPropertyValue& c) {
209 return this->set(key, c, fColorMap);
210 }
211
212 std::vector<CustomPropertyManager::PropKey>
getOpacityProps() const213 CustomPropertyManager::getOpacityProps() const {
214 return this->getProps(fOpacityMap);
215 }
216
getOpacity(const PropKey & key) const217 skottie::OpacityPropertyValue CustomPropertyManager::getOpacity(const PropKey& key) const {
218 return this->get<skottie::OpacityPropertyValue>(key, fOpacityMap);
219 }
220
setOpacity(const PropKey & key,const skottie::OpacityPropertyValue & o)221 bool CustomPropertyManager::setOpacity(const PropKey& key, const skottie::OpacityPropertyValue& o) {
222 return this->set(key, o, fOpacityMap);
223 }
224
225 std::vector<CustomPropertyManager::PropKey>
getTransformProps() const226 CustomPropertyManager::getTransformProps() const {
227 return this->getProps(fTransformMap);
228 }
229
setTransform(const PropKey & key,const skottie::TransformPropertyValue & t)230 bool CustomPropertyManager::setTransform(const PropKey& key,
231 const skottie::TransformPropertyValue& t) {
232 return this->set(key, t, fTransformMap);
233 }
234
235 } // namespace skottie_utils
236