blob: 92efc2f31025909edb9b0c26873ee233a08652cc [file] [log] [blame]
Florin Malitaa8316552018-11-09 16:19:44 -05001/*
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
18namespace skottie_utils {
19
20sk_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
29MultiFrameImageAsset::MultiFrameImageAsset(std::unique_ptr<SkAnimCodecPlayer> player)
30 : fPlayer(std::move(player)) {
31 SkASSERT(fPlayer);
32}
33
34bool MultiFrameImageAsset::isMultiFrame() {
35 return fPlayer->duration() > 0;
36}
37
38sk_sp<SkImage> MultiFrameImageAsset::getFrame(float t) {
39 fPlayer->seek(static_cast<uint32_t>(t * 1000));
40 return fPlayer->getFrame();
41}
42
43sk_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
49FileResourceProvider::FileResourceProvider(SkString base_dir) : fDir(std::move(base_dir)) {}
50
51sk_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
58sk_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
Florin Malita8ac81b72018-11-28 11:39:39 -050063CustomPropertyManagerBuilder::CustomPropertyManagerBuilder() = default;
64CustomPropertyManagerBuilder::~CustomPropertyManagerBuilder() = default;
65
66std::unique_ptr<CustomPropertyManager> CustomPropertyManagerBuilder::build() {
67 return std::unique_ptr<CustomPropertyManager>(
68 new CustomPropertyManager(std::move(fColorMap),
69 std::move(fOpacityMap),
70 std::move(fTransformMap)));
71}
72
73void CustomPropertyManagerBuilder::onColorProperty(
74 const char node_name[],
75 const LazyHandle<skottie::ColorPropertyHandle>& c) {
76 const auto key = this->acceptProperty(node_name);
77 if (!key.empty()) {
78 fColorMap[key].push_back(c());
79 }
80}
81
82void CustomPropertyManagerBuilder::onOpacityProperty(
83 const char node_name[],
84 const LazyHandle<skottie::OpacityPropertyHandle>& o) {
85 const auto key = this->acceptProperty(node_name);
86 if (!key.empty()) {
87 fOpacityMap[key].push_back(o());
88 }
89}
90
91void CustomPropertyManagerBuilder::onTransformProperty(
92 const char node_name[],
93 const LazyHandle<skottie::TransformPropertyHandle>& t) {
94 const auto key = this->acceptProperty(node_name);
95 if (!key.empty()) {
96 fTransformMap[key].push_back(t());
97 }
98}
99
100CustomPropertyManager::CustomPropertyManager(PropMap<skottie::ColorPropertyHandle> cmap,
101 PropMap<skottie::OpacityPropertyHandle> omap,
102 PropMap<skottie::TransformPropertyHandle> tmap)
103 : fColorMap(std::move(cmap))
104 , fOpacityMap(std::move(omap))
105 , fTransformMap(std::move(tmap)) {}
106
107CustomPropertyManager::~CustomPropertyManager() = default;
108
109template <typename T>
110std::vector<CustomPropertyManager::PropKey>
111CustomPropertyManager::getProps(const PropMap<T>& container) const {
112 std::vector<PropKey> props;
113
114 for (const auto& prop_list : container) {
115 SkASSERT(!prop_list.second.empty());
116 props.push_back(prop_list.first);
117 }
118
119 return props;
120}
121
122template <typename V, typename T>
123V CustomPropertyManager::get(const PropKey& key, const PropMap<T>& container) const {
124 auto prop_group = container.find(key);
125
126 return prop_group == container.end()
127 ? V()
128 : prop_group->second.front()->get();
129}
130
131template <typename V, typename T>
132bool CustomPropertyManager::set(const PropKey& key, const V& val, const PropMap<T>& container) {
133 auto prop_group = container.find(key);
134
135 if (prop_group == container.end()) {
136 return false;
137 }
138
139 for (auto& handle : prop_group->second) {
140 handle->set(val);
141 }
142
143 return true;
144}
145
146std::vector<CustomPropertyManager::PropKey>
147CustomPropertyManager::getColorProps() const {
148 return this->getProps(fColorMap);
149}
150
151skottie::ColorPropertyValue CustomPropertyManager::getColor(const PropKey& key) const {
152 return this->get<skottie::ColorPropertyValue>(key, fColorMap);
153}
154
155bool CustomPropertyManager::setColor(const PropKey& key, const skottie::ColorPropertyValue& c) {
156 return this->set(key, c, fColorMap);
157}
158
159std::vector<CustomPropertyManager::PropKey>
160CustomPropertyManager::getOpacityProps() const {
161 return this->getProps(fOpacityMap);
162}
163
164skottie::OpacityPropertyValue CustomPropertyManager::getOpacity(const PropKey& key) const {
165 return this->get<skottie::OpacityPropertyValue>(key, fOpacityMap);
166}
167
168bool CustomPropertyManager::setOpacity(const PropKey& key, const skottie::OpacityPropertyValue& o) {
169 return this->set(key, o, fOpacityMap);
170}
171
172std::vector<CustomPropertyManager::PropKey>
173CustomPropertyManager::getTransformProps() const {
174 return this->getProps(fTransformMap);
175}
176
177bool CustomPropertyManager::setTransform(const PropKey& key,
178 const skottie::TransformPropertyValue& t) {
179 return this->set(key, t, fTransformMap);
180}
181
Florin Malitaa8316552018-11-09 16:19:44 -0500182} // namespace skottie_utils