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 "SkottieUtils.h"
9
10 #include "SkAnimCodecPlayer.h"
11 #include "SkData.h"
12 #include "SkCodec.h"
13 #include "SkImage.h"
14 #include "SkMakeUnique.h"
15 #include "SkOSFile.h"
16 #include "SkOSPath.h"
17
18 namespace skottie_utils {
19
Make(sk_sp<SkData> data)20 sk_sp<MultiFrameImageAsset> MultiFrameImageAsset::Make(sk_sp<SkData> data) {
21 if (auto codec = SkCodec::MakeFromData(std::move(data))) {
22 return sk_sp<MultiFrameImageAsset>(
23 new MultiFrameImageAsset(skstd::make_unique<SkAnimCodecPlayer>(std::move(codec))));
24 }
25
26 return nullptr;
27 }
28
MultiFrameImageAsset(std::unique_ptr<SkAnimCodecPlayer> player)29 MultiFrameImageAsset::MultiFrameImageAsset(std::unique_ptr<SkAnimCodecPlayer> player)
30 : fPlayer(std::move(player)) {
31 SkASSERT(fPlayer);
32 }
33
isMultiFrame()34 bool MultiFrameImageAsset::isMultiFrame() {
35 return fPlayer->duration() > 0;
36 }
37
getFrame(float t)38 sk_sp<SkImage> MultiFrameImageAsset::getFrame(float t) {
39 fPlayer->seek(static_cast<uint32_t>(t * 1000));
40 return fPlayer->getFrame();
41 }
42
Make(SkString base_dir)43 sk_sp<FileResourceProvider> FileResourceProvider::Make(SkString base_dir) {
44 return sk_isdir(base_dir.c_str())
45 ? sk_sp<FileResourceProvider>(new FileResourceProvider(std::move(base_dir)))
46 : nullptr;
47 }
48
FileResourceProvider(SkString base_dir)49 FileResourceProvider::FileResourceProvider(SkString base_dir) : fDir(std::move(base_dir)) {}
50
load(const char resource_path[],const char resource_name[]) const51 sk_sp<SkData> FileResourceProvider::load(const char resource_path[],
52 const char resource_name[]) const {
53 const auto full_dir = SkOSPath::Join(fDir.c_str() , resource_path),
54 full_path = SkOSPath::Join(full_dir.c_str(), resource_name);
55 return SkData::MakeFromFileName(full_path.c_str());
56 }
57
loadImageAsset(const char resource_path[],const char resource_name[]) const58 sk_sp<skottie::ImageAsset> FileResourceProvider::loadImageAsset(const char resource_path[],
59 const char resource_name[]) const {
60 return MultiFrameImageAsset::Make(this->load(resource_path, resource_name));
61 }
62
63 class CustomPropertyManager::PropertyInterceptor final : public skottie::PropertyObserver {
64 public:
PropertyInterceptor(CustomPropertyManager * mgr)65 explicit PropertyInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {}
66
onColorProperty(const char node_name[],const LazyHandle<skottie::ColorPropertyHandle> & c)67 void onColorProperty(const char node_name[],
68 const LazyHandle<skottie::ColorPropertyHandle>& c) override {
69 const auto key = fMgr->acceptKey(node_name);
70 if (!key.empty()) {
71 fMgr->fColorMap[key].push_back(c());
72 }
73 }
74
onOpacityProperty(const char node_name[],const LazyHandle<skottie::OpacityPropertyHandle> & o)75 void onOpacityProperty(const char node_name[],
76 const LazyHandle<skottie::OpacityPropertyHandle>& o) override {
77 const auto key = fMgr->acceptKey(node_name);
78 if (!key.empty()) {
79 fMgr->fOpacityMap[key].push_back(o());
80 }
81 }
82
onTransformProperty(const char node_name[],const LazyHandle<skottie::TransformPropertyHandle> & t)83 void onTransformProperty(const char node_name[],
84 const LazyHandle<skottie::TransformPropertyHandle>& t) override {
85 const auto key = fMgr->acceptKey(node_name);
86 if (!key.empty()) {
87 fMgr->fTransformMap[key].push_back(t());
88 }
89 }
90
91 private:
92 CustomPropertyManager* fMgr;
93 };
94
95 class CustomPropertyManager::MarkerInterceptor final : public skottie::MarkerObserver {
96 public:
MarkerInterceptor(CustomPropertyManager * mgr)97 explicit MarkerInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {}
98
onMarker(const char name[],float t0,float t1)99 void onMarker(const char name[], float t0, float t1) override {
100 const auto key = fMgr->acceptKey(name);
101 if (!key.empty()) {
102 fMgr->fMarkers.push_back({ std::move(key), t0, t1 });
103 }
104 }
105
106 private:
107 CustomPropertyManager* fMgr;
108 };
109
CustomPropertyManager()110 CustomPropertyManager::CustomPropertyManager()
111 : fPropertyInterceptor(sk_make_sp<PropertyInterceptor>(this))
112 , fMarkerInterceptor(sk_make_sp<MarkerInterceptor>(this)) {}
113
114 CustomPropertyManager::~CustomPropertyManager() = default;
115
getPropertyObserver() const116 sk_sp<skottie::PropertyObserver> CustomPropertyManager::getPropertyObserver() const {
117 return fPropertyInterceptor;
118 }
119
getMarkerObserver() const120 sk_sp<skottie::MarkerObserver> CustomPropertyManager::getMarkerObserver() const {
121 return fMarkerInterceptor;
122 }
123
124 template <typename T>
125 std::vector<CustomPropertyManager::PropKey>
getProps(const PropMap<T> & container) const126 CustomPropertyManager::getProps(const PropMap<T>& container) const {
127 std::vector<PropKey> props;
128
129 for (const auto& prop_list : container) {
130 SkASSERT(!prop_list.second.empty());
131 props.push_back(prop_list.first);
132 }
133
134 return props;
135 }
136
137 template <typename V, typename T>
get(const PropKey & key,const PropMap<T> & container) const138 V CustomPropertyManager::get(const PropKey& key, const PropMap<T>& container) const {
139 auto prop_group = container.find(key);
140
141 return prop_group == container.end()
142 ? V()
143 : prop_group->second.front()->get();
144 }
145
146 template <typename V, typename T>
set(const PropKey & key,const V & val,const PropMap<T> & container)147 bool CustomPropertyManager::set(const PropKey& key, const V& val, const PropMap<T>& container) {
148 auto prop_group = container.find(key);
149
150 if (prop_group == container.end()) {
151 return false;
152 }
153
154 for (auto& handle : prop_group->second) {
155 handle->set(val);
156 }
157
158 return true;
159 }
160
161 std::vector<CustomPropertyManager::PropKey>
getColorProps() const162 CustomPropertyManager::getColorProps() const {
163 return this->getProps(fColorMap);
164 }
165
getColor(const PropKey & key) const166 skottie::ColorPropertyValue CustomPropertyManager::getColor(const PropKey& key) const {
167 return this->get<skottie::ColorPropertyValue>(key, fColorMap);
168 }
169
setColor(const PropKey & key,const skottie::ColorPropertyValue & c)170 bool CustomPropertyManager::setColor(const PropKey& key, const skottie::ColorPropertyValue& c) {
171 return this->set(key, c, fColorMap);
172 }
173
174 std::vector<CustomPropertyManager::PropKey>
getOpacityProps() const175 CustomPropertyManager::getOpacityProps() const {
176 return this->getProps(fOpacityMap);
177 }
178
getOpacity(const PropKey & key) const179 skottie::OpacityPropertyValue CustomPropertyManager::getOpacity(const PropKey& key) const {
180 return this->get<skottie::OpacityPropertyValue>(key, fOpacityMap);
181 }
182
setOpacity(const PropKey & key,const skottie::OpacityPropertyValue & o)183 bool CustomPropertyManager::setOpacity(const PropKey& key, const skottie::OpacityPropertyValue& o) {
184 return this->set(key, o, fOpacityMap);
185 }
186
187 std::vector<CustomPropertyManager::PropKey>
getTransformProps() const188 CustomPropertyManager::getTransformProps() const {
189 return this->getProps(fTransformMap);
190 }
191
setTransform(const PropKey & key,const skottie::TransformPropertyValue & t)192 bool CustomPropertyManager::setTransform(const PropKey& key,
193 const skottie::TransformPropertyValue& t) {
194 return this->set(key, t, fTransformMap);
195 }
196
197 } // namespace skottie_utils
198