2018-11-09 21:19:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkottieUtils.h"
|
|
|
|
|
|
|
|
#include "SkAnimCodecPlayer.h"
|
|
|
|
#include "SkData.h"
|
|
|
|
#include "SkCodec.h"
|
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkMakeUnique.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkOSPath.h"
|
|
|
|
|
2019-04-05 14:50:19 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2018-11-09 21:19:44 +00:00
|
|
|
namespace skottie_utils {
|
|
|
|
|
2019-04-05 14:50:19 +00:00
|
|
|
sk_sp<MultiFrameImageAsset> MultiFrameImageAsset::Make(sk_sp<SkData> data, bool predecode) {
|
2018-11-09 21:19:44 +00:00
|
|
|
if (auto codec = SkCodec::MakeFromData(std::move(data))) {
|
|
|
|
return sk_sp<MultiFrameImageAsset>(
|
2019-04-05 14:50:19 +00:00
|
|
|
new MultiFrameImageAsset(skstd::make_unique<SkAnimCodecPlayer>(std::move(codec)),
|
|
|
|
predecode));
|
2018-11-09 21:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-04-05 14:50:19 +00:00
|
|
|
MultiFrameImageAsset::MultiFrameImageAsset(std::unique_ptr<SkAnimCodecPlayer> player,
|
|
|
|
bool predecode)
|
|
|
|
: fPlayer(std::move(player))
|
|
|
|
, fPreDecode(predecode) {
|
2018-11-09 21:19:44 +00:00
|
|
|
SkASSERT(fPlayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MultiFrameImageAsset::isMultiFrame() {
|
|
|
|
return fPlayer->duration() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> MultiFrameImageAsset::getFrame(float t) {
|
2019-04-05 14:50:19 +00:00
|
|
|
auto decode = [](sk_sp<SkImage> image) {
|
|
|
|
SkASSERT(image->isLazyGenerated());
|
|
|
|
|
|
|
|
static constexpr size_t kMaxArea = 2048 * 2048;
|
|
|
|
const auto image_area = SkToSizeT(image->width() * image->height());
|
|
|
|
|
|
|
|
if (image_area > kMaxArea) {
|
|
|
|
// When the image is too large, decode and scale down to a reasonable size.
|
|
|
|
const auto scale = std::sqrt(static_cast<float>(kMaxArea) / image_area);
|
|
|
|
const auto info = SkImageInfo::MakeN32Premul(scale * image->width(),
|
|
|
|
scale * image->height());
|
|
|
|
SkBitmap bm;
|
|
|
|
if (bm.tryAllocPixels(info, info.minRowBytes()) &&
|
|
|
|
image->scalePixels(bm.pixmap(),
|
|
|
|
SkFilterQuality::kMedium_SkFilterQuality,
|
|
|
|
SkImage::kDisallow_CachingHint)) {
|
|
|
|
image = SkImage::MakeFromBitmap(bm);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// When the image size is OK, just force-decode.
|
|
|
|
image = image->makeRasterImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return image;
|
|
|
|
};
|
|
|
|
|
2018-11-09 21:19:44 +00:00
|
|
|
fPlayer->seek(static_cast<uint32_t>(t * 1000));
|
2019-04-05 14:50:19 +00:00
|
|
|
auto frame = fPlayer->getFrame();
|
|
|
|
|
|
|
|
if (fPreDecode && frame && frame->isLazyGenerated()) {
|
|
|
|
frame = decode(std::move(frame));
|
|
|
|
}
|
|
|
|
|
|
|
|
return frame;
|
2018-11-09 21:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<FileResourceProvider> FileResourceProvider::Make(SkString base_dir) {
|
|
|
|
return sk_isdir(base_dir.c_str())
|
|
|
|
? sk_sp<FileResourceProvider>(new FileResourceProvider(std::move(base_dir)))
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileResourceProvider::FileResourceProvider(SkString base_dir) : fDir(std::move(base_dir)) {}
|
|
|
|
|
|
|
|
sk_sp<SkData> FileResourceProvider::load(const char resource_path[],
|
|
|
|
const char resource_name[]) const {
|
|
|
|
const auto full_dir = SkOSPath::Join(fDir.c_str() , resource_path),
|
|
|
|
full_path = SkOSPath::Join(full_dir.c_str(), resource_name);
|
|
|
|
return SkData::MakeFromFileName(full_path.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<skottie::ImageAsset> FileResourceProvider::loadImageAsset(const char resource_path[],
|
|
|
|
const char resource_name[]) const {
|
|
|
|
return MultiFrameImageAsset::Make(this->load(resource_path, resource_name));
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
class CustomPropertyManager::PropertyInterceptor final : public skottie::PropertyObserver {
|
|
|
|
public:
|
|
|
|
explicit PropertyInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {}
|
|
|
|
|
|
|
|
void onColorProperty(const char node_name[],
|
|
|
|
const LazyHandle<skottie::ColorPropertyHandle>& c) override {
|
|
|
|
const auto key = fMgr->acceptKey(node_name);
|
|
|
|
if (!key.empty()) {
|
|
|
|
fMgr->fColorMap[key].push_back(c());
|
|
|
|
}
|
|
|
|
}
|
2018-11-28 16:39:39 +00:00
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
void onOpacityProperty(const char node_name[],
|
|
|
|
const LazyHandle<skottie::OpacityPropertyHandle>& o) override {
|
|
|
|
const auto key = fMgr->acceptKey(node_name);
|
|
|
|
if (!key.empty()) {
|
|
|
|
fMgr->fOpacityMap[key].push_back(o());
|
|
|
|
}
|
2018-11-28 16:39:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
void onTransformProperty(const char node_name[],
|
|
|
|
const LazyHandle<skottie::TransformPropertyHandle>& t) override {
|
|
|
|
const auto key = fMgr->acceptKey(node_name);
|
|
|
|
if (!key.empty()) {
|
|
|
|
fMgr->fTransformMap[key].push_back(t());
|
|
|
|
}
|
2018-11-28 16:39:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
private:
|
|
|
|
CustomPropertyManager* fMgr;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CustomPropertyManager::MarkerInterceptor final : public skottie::MarkerObserver {
|
|
|
|
public:
|
|
|
|
explicit MarkerInterceptor(CustomPropertyManager* mgr) : fMgr(mgr) {}
|
|
|
|
|
|
|
|
void onMarker(const char name[], float t0, float t1) override {
|
|
|
|
const auto key = fMgr->acceptKey(name);
|
|
|
|
if (!key.empty()) {
|
|
|
|
fMgr->fMarkers.push_back({ std::move(key), t0, t1 });
|
|
|
|
}
|
2018-11-28 16:39:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
private:
|
|
|
|
CustomPropertyManager* fMgr;
|
|
|
|
};
|
|
|
|
|
|
|
|
CustomPropertyManager::CustomPropertyManager()
|
|
|
|
: fPropertyInterceptor(sk_make_sp<PropertyInterceptor>(this))
|
|
|
|
, fMarkerInterceptor(sk_make_sp<MarkerInterceptor>(this)) {}
|
2018-11-28 16:39:39 +00:00
|
|
|
|
|
|
|
CustomPropertyManager::~CustomPropertyManager() = default;
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
sk_sp<skottie::PropertyObserver> CustomPropertyManager::getPropertyObserver() const {
|
|
|
|
return fPropertyInterceptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<skottie::MarkerObserver> CustomPropertyManager::getMarkerObserver() const {
|
|
|
|
return fMarkerInterceptor;
|
|
|
|
}
|
|
|
|
|
2018-11-28 16:39:39 +00:00
|
|
|
template <typename T>
|
|
|
|
std::vector<CustomPropertyManager::PropKey>
|
|
|
|
CustomPropertyManager::getProps(const PropMap<T>& container) const {
|
|
|
|
std::vector<PropKey> props;
|
|
|
|
|
|
|
|
for (const auto& prop_list : container) {
|
|
|
|
SkASSERT(!prop_list.second.empty());
|
|
|
|
props.push_back(prop_list.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename V, typename T>
|
|
|
|
V CustomPropertyManager::get(const PropKey& key, const PropMap<T>& container) const {
|
|
|
|
auto prop_group = container.find(key);
|
|
|
|
|
|
|
|
return prop_group == container.end()
|
|
|
|
? V()
|
|
|
|
: prop_group->second.front()->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename V, typename T>
|
|
|
|
bool CustomPropertyManager::set(const PropKey& key, const V& val, const PropMap<T>& container) {
|
|
|
|
auto prop_group = container.find(key);
|
|
|
|
|
|
|
|
if (prop_group == container.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& handle : prop_group->second) {
|
|
|
|
handle->set(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CustomPropertyManager::PropKey>
|
|
|
|
CustomPropertyManager::getColorProps() const {
|
|
|
|
return this->getProps(fColorMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
skottie::ColorPropertyValue CustomPropertyManager::getColor(const PropKey& key) const {
|
|
|
|
return this->get<skottie::ColorPropertyValue>(key, fColorMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CustomPropertyManager::setColor(const PropKey& key, const skottie::ColorPropertyValue& c) {
|
|
|
|
return this->set(key, c, fColorMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CustomPropertyManager::PropKey>
|
|
|
|
CustomPropertyManager::getOpacityProps() const {
|
|
|
|
return this->getProps(fOpacityMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
skottie::OpacityPropertyValue CustomPropertyManager::getOpacity(const PropKey& key) const {
|
|
|
|
return this->get<skottie::OpacityPropertyValue>(key, fOpacityMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CustomPropertyManager::setOpacity(const PropKey& key, const skottie::OpacityPropertyValue& o) {
|
|
|
|
return this->set(key, o, fOpacityMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CustomPropertyManager::PropKey>
|
|
|
|
CustomPropertyManager::getTransformProps() const {
|
|
|
|
return this->getProps(fTransformMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CustomPropertyManager::setTransform(const PropKey& key,
|
|
|
|
const skottie::TransformPropertyValue& t) {
|
|
|
|
return this->set(key, t, fTransformMap);
|
|
|
|
}
|
|
|
|
|
2018-11-09 21:19:44 +00:00
|
|
|
} // namespace skottie_utils
|