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-10-03 18:39:56 +00:00
|
|
|
class SkImage;
|
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
|
|
|
|
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 21:24:59 +00:00
|
|
|
class PropertyObserver;
|
|
|
|
|
2018-10-03 18:39:56 +00:00
|
|
|
/**
|
|
|
|
* Image asset proxy interface.
|
|
|
|
*/
|
|
|
|
class SK_API ImageAsset : public SkRefCnt {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Returns true if the image asset is animated.
|
|
|
|
*/
|
|
|
|
virtual bool isMultiFrame() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the SkImage for a given frame.
|
|
|
|
*
|
|
|
|
* If the image asset is static, getImage() is only called once, at animation load time.
|
|
|
|
* Otherwise, this gets invoked every time the animation time is adjusted (on every seek).
|
|
|
|
*
|
|
|
|
* Embedders should cache and serve the same SkImage whenever possible, for efficiency.
|
|
|
|
*
|
|
|
|
* @param t Frame time code, in seconds, relative to the image layer timeline origin
|
|
|
|
* (in-point).
|
|
|
|
*/
|
|
|
|
virtual sk_sp<SkImage> getFrame(float t) = 0;
|
|
|
|
};
|
|
|
|
|
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-09-20 01:43:29 +00:00
|
|
|
/**
|
2018-10-03 18:39:56 +00:00
|
|
|
* Load a generic resource (currently only nested animations) specified by |path| + |name|,
|
|
|
|
* and return as an SkData.
|
2018-09-20 01:43:29 +00:00
|
|
|
*/
|
|
|
|
virtual sk_sp<SkData> load(const char resource_path[],
|
|
|
|
const char resource_name[]) const;
|
|
|
|
|
2018-10-03 18:39:56 +00:00
|
|
|
/**
|
|
|
|
* Load an image asset specified by |path| + |name|, and returns the corresponding
|
|
|
|
* ImageAsset proxy.
|
|
|
|
*/
|
|
|
|
virtual sk_sp<ImageAsset> loadImageAsset(const char resource_path[],
|
|
|
|
const char resource_name[]) const;
|
|
|
|
|
2018-09-20 01:43:29 +00:00
|
|
|
/**
|
2018-09-27 14:59:52 +00:00
|
|
|
* Load an external font and return as SkData.
|
|
|
|
*
|
|
|
|
* @param name font name ("fName" Lottie property)
|
|
|
|
* @param url web font URL ("fPath" Lottie property)
|
2018-09-20 01:43:29 +00:00
|
|
|
*
|
|
|
|
* -- 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.
|
|
|
|
*/
|
2018-09-27 14:59:52 +00:00
|
|
|
virtual sk_sp<SkData> loadFont(const char name[], const char url[]) const;
|
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:
|
2018-09-24 21:24:59 +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 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:
|
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.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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:
|
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-02-25 14:55:04 +00:00
|
|
|
SkScalar inPoint, SkScalar outPoint, SkScalar duration, uint32_t flags = 0);
|
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;
|
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
|