2017-12-30 17:27:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
#ifndef Skottie_DEFINED
|
|
|
|
#define Skottie_DEFINED
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-08-23 00:37:04 +00:00
|
|
|
#include "SkFontMgr.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkSize.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
class SkCanvas;
|
2018-08-06 18:13:04 +00:00
|
|
|
class SkData;
|
2018-01-08 13:25:27 +00:00
|
|
|
struct SkRect;
|
2017-12-30 17:27:00 +00:00
|
|
|
class SkStream;
|
|
|
|
|
2018-06-14 20:16:01 +00:00
|
|
|
namespace skjson { class ObjectValue; }
|
2018-01-22 17:57:06 +00:00
|
|
|
namespace sksg { class Scene; }
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
namespace skottie {
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-09-24 18:43:04 +00:00
|
|
|
class PropertyObserver;
|
|
|
|
|
2018-09-20 01:43:29 +00:00
|
|
|
/**
|
|
|
|
* ResourceProvider allows Skottie embedders to control loading of external
|
|
|
|
* Skottie resources -- e.g. images, fonts, nested animations.
|
|
|
|
*/
|
2018-08-23 18:49:05 +00:00
|
|
|
class SK_API ResourceProvider : public SkRefCnt {
|
2018-01-08 17:51:12 +00:00
|
|
|
public:
|
2018-07-16 21:44:06 +00:00
|
|
|
ResourceProvider() = default;
|
2018-01-08 17:51:12 +00:00
|
|
|
virtual ~ResourceProvider() = default;
|
2018-07-16 21:44:06 +00:00
|
|
|
ResourceProvider(const ResourceProvider&) = delete;
|
|
|
|
ResourceProvider& operator=(const ResourceProvider&) = delete;
|
2018-01-08 17:51:12 +00:00
|
|
|
|
2018-09-20 01:43:29 +00:00
|
|
|
/**
|
|
|
|
* Load a resource (image, nested animation) specified by |path| + |name|, and
|
|
|
|
* return as an SkData.
|
|
|
|
*/
|
|
|
|
virtual sk_sp<SkData> load(const char resource_path[],
|
|
|
|
const char resource_name[]) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a web font from |url| and return as an SkData.
|
|
|
|
*
|
|
|
|
* -- Note --
|
|
|
|
*
|
|
|
|
* This mechanism assumes monolithic fonts (single data blob). Some web font providers may
|
|
|
|
* serve multiple font blobs, segmented for various unicode ranges, depending on user agent
|
|
|
|
* capabilities (woff, woff2). In that case, the embedder would need to advertise no user
|
|
|
|
* agent capabilities when fetching the URL, in order to receive full font data.
|
|
|
|
*/
|
|
|
|
virtual sk_sp<SkData> loadWebFont(const char url[]) const;
|
2018-01-08 17:51:12 +00:00
|
|
|
};
|
|
|
|
|
2018-05-23 20:35:56 +00:00
|
|
|
class SK_API Animation : public SkRefCnt {
|
2017-12-30 17:27:00 +00:00
|
|
|
public:
|
2018-08-23 00:37:04 +00:00
|
|
|
|
|
|
|
class Builder final {
|
|
|
|
public:
|
2018-09-24 18:43:04 +00:00
|
|
|
Builder();
|
|
|
|
~Builder();
|
|
|
|
|
2018-08-23 00:37:04 +00:00
|
|
|
struct Stats {
|
|
|
|
float fTotalLoadTimeMS = 0, // Total animation instantiation time.
|
|
|
|
fJsonParseTimeMS = 0, // Time spent building a JSON DOM.
|
|
|
|
fSceneParseTimeMS = 0; // Time spent constructing the animation scene graph.
|
|
|
|
size_t fJsonSize = 0, // Input JSON size.
|
|
|
|
fAnimatorCount = 0; // Number of dynamically animated properties.
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns various animation build stats.
|
|
|
|
*
|
|
|
|
* @return Stats (see above).
|
|
|
|
*/
|
|
|
|
const Stats& getStats() const { return fStats; }
|
|
|
|
|
|
|
|
/**
|
2018-08-23 18:49:05 +00:00
|
|
|
* Specify a loader for external resources (images, etc.).
|
2018-08-23 00:37:04 +00:00
|
|
|
*/
|
2018-08-23 18:49:05 +00:00
|
|
|
Builder& setResourceProvider(sk_sp<ResourceProvider>);
|
2018-08-23 00:37:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify a font manager for loading animation fonts.
|
|
|
|
*/
|
|
|
|
Builder& setFontManager(sk_sp<SkFontMgr>);
|
|
|
|
|
2018-09-24 18:43:04 +00:00
|
|
|
/**
|
|
|
|
* Specify a PropertyObserver to receive callbacks during parsing.
|
|
|
|
*
|
|
|
|
* See SkottieProperty.h for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
Builder& setPropertyObserver(sk_sp<PropertyObserver>);
|
|
|
|
|
2018-08-23 00:37:04 +00:00
|
|
|
/**
|
|
|
|
* Animation factories.
|
|
|
|
*/
|
|
|
|
sk_sp<Animation> make(SkStream*);
|
|
|
|
sk_sp<Animation> make(const char* data, size_t length);
|
|
|
|
sk_sp<Animation> makeFromFile(const char path[]);
|
|
|
|
|
|
|
|
private:
|
2018-08-23 18:49:05 +00:00
|
|
|
sk_sp<ResourceProvider> fResourceProvider;
|
2018-08-23 00:37:04 +00:00
|
|
|
sk_sp<SkFontMgr> fFontMgr;
|
2018-09-24 18:43:04 +00:00
|
|
|
sk_sp<PropertyObserver> fPropertyObserver;
|
2018-08-23 00:37:04 +00:00
|
|
|
Stats fStats;
|
2018-04-30 14:32:18 +00:00
|
|
|
};
|
|
|
|
|
2018-08-23 00:37:04 +00:00
|
|
|
/**
|
|
|
|
* Animation factories.
|
|
|
|
*
|
|
|
|
* Use the Builder helper above for more options/control.
|
|
|
|
*/
|
|
|
|
static sk_sp<Animation> Make(const char* data, size_t length);
|
|
|
|
static sk_sp<Animation> Make(SkStream*);
|
|
|
|
static sk_sp<Animation> MakeFromFile(const char path[]);
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-02-23 16:10:22 +00:00
|
|
|
~Animation() override;
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-05-29 17:46:54 +00:00
|
|
|
/**
|
|
|
|
* Draws the current animation frame.
|
|
|
|
*
|
|
|
|
* @param canvas destination canvas
|
|
|
|
* @param dst optional destination rect
|
|
|
|
*/
|
|
|
|
void render(SkCanvas* canvas, const SkRect* dst = nullptr) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the animation state for |t|.
|
|
|
|
*
|
2018-05-31 20:45:29 +00:00
|
|
|
* @param t normalized [0..1] frame selector (0 -> first frame, 1 -> final frame)
|
|
|
|
*
|
2018-05-29 17:46:54 +00:00
|
|
|
*/
|
|
|
|
void seek(SkScalar t);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the animation duration in seconds.
|
|
|
|
*/
|
2018-08-23 00:37:04 +00:00
|
|
|
SkScalar duration() const { return fDuration; }
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2017-12-31 16:08:42 +00:00
|
|
|
const SkString& version() const { return fVersion; }
|
|
|
|
const SkSize& size() const { return fSize; }
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-22 17:57:06 +00:00
|
|
|
void setShowInval(bool show);
|
2017-12-30 17:27:00 +00:00
|
|
|
|
|
|
|
private:
|
2018-08-23 00:37:04 +00:00
|
|
|
Animation(std::unique_ptr<sksg::Scene>, SkString ver, const SkSize& size,
|
|
|
|
SkScalar inPoint, SkScalar outPoint, SkScalar duration);
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-22 17:57:06 +00:00
|
|
|
std::unique_ptr<sksg::Scene> fScene;
|
2018-08-23 00:37:04 +00:00
|
|
|
const SkString fVersion;
|
|
|
|
const SkSize fSize;
|
|
|
|
const SkScalar fInPoint,
|
|
|
|
fOutPoint,
|
|
|
|
fDuration;
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-02-23 16:10:22 +00:00
|
|
|
typedef SkRefCnt INHERITED;
|
2017-12-30 17:27:00 +00:00
|
|
|
};
|
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
} // namespace skottie
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-01-16 22:04:30 +00:00
|
|
|
#endif // Skottie_DEFINED
|