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
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFontMgr.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-11-25 18:24:59 +00:00
|
|
|
#include "modules/skresources/include/SkResources.h"
|
2017-12-30 17:27:00 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
class SkCanvas;
|
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-10-02 16:48:00 +00:00
|
|
|
|
2019-07-22 16:05:41 +00:00
|
|
|
namespace sksg {
|
|
|
|
|
|
|
|
class InvalidationController;
|
|
|
|
class Scene;
|
|
|
|
|
|
|
|
} // namespace sksg
|
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
|
|
|
|
2019-11-25 18:24:59 +00:00
|
|
|
using ImageAsset = skresources::ImageAsset;
|
|
|
|
using ResourceProvider = skresources::ResourceProvider;
|
2018-10-03 18:39:56 +00:00
|
|
|
|
2019-11-25 18:24:59 +00:00
|
|
|
class PropertyObserver;
|
2018-01-08 17:51:12 +00:00
|
|
|
|
2018-10-02 16:48:00 +00:00
|
|
|
/**
|
|
|
|
* A Logger subclass can be used to receive Animation::Builder parsing errors and warnings.
|
|
|
|
*/
|
|
|
|
class SK_API Logger : public SkRefCnt {
|
|
|
|
public:
|
|
|
|
enum class Level {
|
|
|
|
kWarning,
|
|
|
|
kError,
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void log(Level, const char message[], const char* json = nullptr);
|
|
|
|
};
|
|
|
|
|
2018-10-09 18:33:08 +00:00
|
|
|
/**
|
2018-11-30 21:46:45 +00:00
|
|
|
* Interface for receiving AE composition markers at Animation build time.
|
2018-10-09 18:33:08 +00:00
|
|
|
*/
|
2018-11-30 21:46:45 +00:00
|
|
|
class SK_API MarkerObserver : public SkRefCnt {
|
2018-10-09 18:33:08 +00:00
|
|
|
public:
|
2018-11-30 21:46:45 +00:00
|
|
|
// t0,t1 are in the Animation::seek() domain.
|
|
|
|
virtual void onMarker(const char name[], float t0, float t1) = 0;
|
2018-10-09 18:33:08 +00:00
|
|
|
};
|
|
|
|
|
2018-10-30 15:23:00 +00:00
|
|
|
class SK_API Animation : public SkNVRefCnt<Animation> {
|
2017-12-30 17:27:00 +00:00
|
|
|
public:
|
2018-08-23 00:37:04 +00:00
|
|
|
|
|
|
|
class Builder final {
|
|
|
|
public:
|
2019-12-05 14:43:35 +00:00
|
|
|
enum Flags : uint32_t {
|
|
|
|
kDeferImageLoading = 0x01, // Normally, all static image frames are resolved at
|
|
|
|
// load time via ImageAsset::getFrame(0). With this flag,
|
|
|
|
// frames are only resolved when needed, at seek() time.
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit Builder(uint32_t flags = 0);
|
2018-09-24 21:24:59 +00:00
|
|
|
~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 21:24:59 +00:00
|
|
|
/**
|
|
|
|
* Specify a PropertyObserver to receive callbacks during parsing.
|
|
|
|
*
|
|
|
|
* See SkottieProperty.h for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
Builder& setPropertyObserver(sk_sp<PropertyObserver>);
|
|
|
|
|
2018-10-02 16:48:00 +00:00
|
|
|
/**
|
|
|
|
* Register a Logger with this builder.
|
|
|
|
*/
|
|
|
|
Builder& setLogger(sk_sp<Logger>);
|
|
|
|
|
2018-10-09 18:33:08 +00:00
|
|
|
/**
|
2018-11-30 21:46:45 +00:00
|
|
|
* Register a MarkerObserver with this builder.
|
2018-10-09 18:33:08 +00:00
|
|
|
*/
|
2018-11-30 21:46:45 +00:00
|
|
|
Builder& setMarkerObserver(sk_sp<MarkerObserver>);
|
2018-10-09 18:33:08 +00:00
|
|
|
|
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:
|
2019-12-05 14:43:35 +00:00
|
|
|
const uint32_t fFlags;
|
|
|
|
|
2018-11-30 21:46:45 +00:00
|
|
|
sk_sp<ResourceProvider> fResourceProvider;
|
|
|
|
sk_sp<SkFontMgr> fFontMgr;
|
|
|
|
sk_sp<PropertyObserver> fPropertyObserver;
|
|
|
|
sk_sp<Logger> fLogger;
|
|
|
|
sk_sp<MarkerObserver> fMarkerObserver;
|
|
|
|
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-10-03 20:42:09 +00:00
|
|
|
~Animation();
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2019-02-25 14:55:04 +00:00
|
|
|
enum RenderFlag : uint32_t {
|
|
|
|
// When rendering into a known transparent buffer, clients can pass
|
|
|
|
// this flag to avoid some unnecessary compositing overhead for
|
|
|
|
// animations using layer blend modes.
|
|
|
|
kSkipTopLevelIsolation = 0x01,
|
|
|
|
};
|
|
|
|
using RenderFlags = uint32_t;
|
|
|
|
|
2018-05-29 17:46:54 +00:00
|
|
|
/**
|
|
|
|
* Draws the current animation frame.
|
|
|
|
*
|
2019-12-03 17:49:27 +00:00
|
|
|
* It is undefined behavior to call render() on a newly created Animation
|
|
|
|
* before specifying an initial frame via one of the seek() variants.
|
|
|
|
*
|
2018-05-29 17:46:54 +00:00
|
|
|
* @param canvas destination canvas
|
|
|
|
* @param dst optional destination rect
|
2019-02-25 14:55:04 +00:00
|
|
|
* @param flags optional RenderFlags
|
2018-05-29 17:46:54 +00:00
|
|
|
*/
|
|
|
|
void render(SkCanvas* canvas, const SkRect* dst = nullptr) const;
|
2019-02-25 14:55:04 +00:00
|
|
|
void render(SkCanvas* canvas, const SkRect* dst, RenderFlags) const;
|
2018-05-29 17:46:54 +00:00
|
|
|
|
|
|
|
/**
|
2019-10-21 16:46:16 +00:00
|
|
|
* [Deprecated: use one of the other versions.]
|
|
|
|
*
|
2018-05-29 17:46:54 +00:00
|
|
|
* 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)
|
2019-07-22 16:05:41 +00:00
|
|
|
* @param ic optional invalidation controller (dirty region tracking)
|
2018-05-31 20:45:29 +00:00
|
|
|
*
|
2018-05-29 17:46:54 +00:00
|
|
|
*/
|
2019-10-21 16:46:16 +00:00
|
|
|
void seek(SkScalar t, sksg::InvalidationController* ic = nullptr) {
|
|
|
|
this->seekFrameTime(t * this->duration(), ic);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the animation state to match |t|, specified as a frame index
|
|
|
|
* i.e. relative to duration() * fps().
|
|
|
|
*
|
|
|
|
* Fractional values are allowed and meaningful - e.g.
|
|
|
|
*
|
|
|
|
* 0.0 -> first frame
|
|
|
|
* 1.0 -> second frame
|
|
|
|
* 0.5 -> halfway between first and second frame
|
|
|
|
*/
|
|
|
|
void seekFrame(double t, sksg::InvalidationController* ic = nullptr);
|
2018-05-29 17:46:54 +00:00
|
|
|
|
2019-06-17 21:45:01 +00:00
|
|
|
/** Update the animation state to match t, specifed in frame time
|
|
|
|
* i.e. relative to duration().
|
|
|
|
*/
|
2019-07-22 16:05:41 +00:00
|
|
|
void seekFrameTime(double t, sksg::InvalidationController* = nullptr);
|
2019-06-17 21:45:01 +00:00
|
|
|
|
2018-05-29 17:46:54 +00:00
|
|
|
/**
|
|
|
|
* Returns the animation duration in seconds.
|
|
|
|
*/
|
2019-10-21 16:46:16 +00:00
|
|
|
double duration() const { return fDuration; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the animation frame rate (frames / second).
|
|
|
|
*/
|
|
|
|
double fps() const { return fFPS; }
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2019-10-21 16:46:16 +00:00
|
|
|
const SkString& version() const { return fVersion; }
|
|
|
|
const SkSize& size() const { return fSize; }
|
2017-12-30 17:27:00 +00:00
|
|
|
|
|
|
|
private:
|
2019-02-25 14:55:04 +00:00
|
|
|
enum Flags : uint32_t {
|
|
|
|
kRequiresTopLevelIsolation = 1 << 0, // Needs to draw into a layer due to layer blending.
|
|
|
|
};
|
|
|
|
|
2018-08-23 00:37:04 +00:00
|
|
|
Animation(std::unique_ptr<sksg::Scene>, SkString ver, const SkSize& size,
|
2019-10-21 16:46:16 +00:00
|
|
|
double inPoint, double outPoint, double duration, double fps, uint32_t flags);
|
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;
|
2019-10-21 16:46:16 +00:00
|
|
|
const double fInPoint,
|
2018-08-23 00:37:04 +00:00
|
|
|
fOutPoint,
|
2019-10-21 16:46:16 +00:00
|
|
|
fDuration,
|
|
|
|
fFPS;
|
2019-02-25 14:55:04 +00:00
|
|
|
const uint32_t fFlags;
|
2017-12-30 17:27:00 +00:00
|
|
|
|
2018-10-30 15:23:00 +00:00
|
|
|
typedef SkNVRefCnt<Animation> 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
|