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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkottieUtils_DEFINED
|
|
|
|
#define SkottieUtils_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/private/SkTHash.h"
|
|
|
|
#include "modules/skottie/include/Skottie.h"
|
|
|
|
#include "modules/skottie/include/SkottieProperty.h"
|
2018-11-09 21:19:44 +00:00
|
|
|
|
|
|
|
#include <memory>
|
2018-11-28 16:39:39 +00:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2018-11-09 21:19:44 +00:00
|
|
|
|
|
|
|
class SkAnimCodecPlayer;
|
|
|
|
class SkData;
|
|
|
|
class SkImage;
|
|
|
|
|
|
|
|
namespace skottie_utils {
|
|
|
|
|
|
|
|
class MultiFrameImageAsset final : public skottie::ImageAsset {
|
|
|
|
public:
|
2019-04-05 14:50:19 +00:00
|
|
|
/**
|
|
|
|
* By default, images are decoded on-the-fly, at rasterization time.
|
|
|
|
* Large images may cause jank as decoding is expensive (and can thrash internal caches).
|
|
|
|
*
|
|
|
|
* Pass |predecode| true to force-decode all images upfront, at the cost of potentially more RAM
|
|
|
|
* and slower animation build times.
|
|
|
|
*/
|
|
|
|
static sk_sp<MultiFrameImageAsset> Make(sk_sp<SkData>, bool predecode = false);
|
2018-11-09 21:19:44 +00:00
|
|
|
|
|
|
|
bool isMultiFrame() override;
|
|
|
|
|
|
|
|
sk_sp<SkImage> getFrame(float t) override;
|
|
|
|
|
|
|
|
private:
|
2019-04-05 14:50:19 +00:00
|
|
|
explicit MultiFrameImageAsset(std::unique_ptr<SkAnimCodecPlayer>, bool predecode);
|
2018-11-09 21:19:44 +00:00
|
|
|
|
|
|
|
std::unique_ptr<SkAnimCodecPlayer> fPlayer;
|
2019-04-05 14:50:19 +00:00
|
|
|
bool fPreDecode;
|
2018-11-09 21:19:44 +00:00
|
|
|
|
|
|
|
using INHERITED = skottie::ImageAsset;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileResourceProvider final : public skottie::ResourceProvider {
|
|
|
|
public:
|
|
|
|
static sk_sp<FileResourceProvider> Make(SkString base_dir);
|
|
|
|
|
|
|
|
sk_sp<SkData> load(const char resource_path[], const char resource_name[]) const override;
|
|
|
|
|
|
|
|
sk_sp<skottie::ImageAsset> loadImageAsset(const char[], const char []) const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit FileResourceProvider(SkString);
|
|
|
|
|
|
|
|
const SkString fDir;
|
|
|
|
|
|
|
|
using INHERITED = skottie::ResourceProvider;
|
|
|
|
};
|
|
|
|
|
2018-11-28 16:39:39 +00:00
|
|
|
/**
|
|
|
|
* CustomPropertyManager implements a property management scheme where color/opacity/transform
|
|
|
|
* attributes are grouped and manipulated by name (one-to-many mapping).
|
|
|
|
*
|
|
|
|
* - setters apply the value to all properties in a named group
|
|
|
|
*
|
|
|
|
* - getters return all the managed property groups, and the first value within each of them
|
|
|
|
* (unchecked assumption: all properties within the same group have the same value)
|
|
|
|
*
|
2018-11-30 21:46:45 +00:00
|
|
|
* Attach to an Animation::Builder using the utility methods below to intercept properties and
|
|
|
|
* markers at build time.
|
2018-11-28 16:39:39 +00:00
|
|
|
*/
|
|
|
|
class CustomPropertyManager final {
|
|
|
|
public:
|
2018-11-30 21:46:45 +00:00
|
|
|
CustomPropertyManager();
|
2018-11-28 16:39:39 +00:00
|
|
|
~CustomPropertyManager();
|
|
|
|
|
|
|
|
using PropKey = std::string;
|
|
|
|
|
|
|
|
std::vector<PropKey> getColorProps() const;
|
|
|
|
skottie::ColorPropertyValue getColor(const PropKey&) const;
|
|
|
|
bool setColor(const PropKey&, const skottie::ColorPropertyValue&);
|
|
|
|
|
|
|
|
std::vector<PropKey> getOpacityProps() const;
|
|
|
|
skottie::OpacityPropertyValue getOpacity(const PropKey&) const;
|
|
|
|
bool setOpacity(const PropKey&, const skottie::OpacityPropertyValue&);
|
|
|
|
|
|
|
|
std::vector<PropKey> getTransformProps() const;
|
|
|
|
skottie::TransformPropertyValue getTransform(const PropKey&) const;
|
|
|
|
bool setTransform(const PropKey&, const skottie::TransformPropertyValue&);
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
struct MarkerInfo {
|
|
|
|
std::string name;
|
|
|
|
float t0, t1;
|
|
|
|
};
|
|
|
|
const std::vector<MarkerInfo>& markers() const { return fMarkers; }
|
|
|
|
|
|
|
|
// Returns a property observer to be attached to an animation builder.
|
|
|
|
sk_sp<skottie::PropertyObserver> getPropertyObserver() const;
|
|
|
|
|
|
|
|
// Returns a marker observer to be attached to an animation builder.
|
|
|
|
sk_sp<skottie::MarkerObserver> getMarkerObserver() const;
|
|
|
|
|
2018-11-28 16:39:39 +00:00
|
|
|
private:
|
2018-11-30 21:46:45 +00:00
|
|
|
class PropertyInterceptor;
|
|
|
|
class MarkerInterceptor;
|
|
|
|
|
|
|
|
static std::string acceptKey(const char* name) {
|
|
|
|
static constexpr char kPrefix = '$';
|
|
|
|
|
|
|
|
return (name[0] == kPrefix && name[1] != '\0')
|
|
|
|
? std::string(name + 1)
|
|
|
|
: std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<PropertyInterceptor> fPropertyInterceptor;
|
|
|
|
sk_sp<MarkerInterceptor> fMarkerInterceptor;
|
2018-11-28 16:39:39 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using PropGroup = std::vector<std::unique_ptr<T>>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using PropMap = std::unordered_map<PropKey, PropGroup<T>>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::vector<PropKey> getProps(const PropMap<T>& container) const;
|
|
|
|
|
|
|
|
template <typename V, typename T>
|
|
|
|
V get(const PropKey&, const PropMap<T>& container) const;
|
|
|
|
|
|
|
|
template <typename V, typename T>
|
|
|
|
bool set(const PropKey&, const V&, const PropMap<T>& container);
|
|
|
|
|
|
|
|
PropMap<skottie::ColorPropertyHandle> fColorMap;
|
|
|
|
PropMap<skottie::OpacityPropertyHandle> fOpacityMap;
|
|
|
|
PropMap<skottie::TransformPropertyHandle> fTransformMap;
|
2018-11-30 21:46:45 +00:00
|
|
|
std::vector<MarkerInfo> fMarkers;
|
2018-11-28 16:39:39 +00:00
|
|
|
};
|
|
|
|
|
2018-11-09 21:19:44 +00:00
|
|
|
} // namespace skottie_utils
|
|
|
|
|
|
|
|
#endif // SkottieUtils_DEFINED
|