40c37426b3
Allow embedders to pass a font manager. In order to avoid excessive factory API clutter, introduce an Animation::Builder helper to wrap factory options. Also clean up various bits: * hoist scene parsing out of the Animation ctor * store the animation duration explicitly (instead of unused fps) * plumb const SkFontMgr& internally (instead of a ref) Change-Id: I3e180dfa85ba18c8462cfeb5a7385bef985ed6c4 Reviewed-on: https://skia-review.googlesource.com/148800 Reviewed-by: Ben Wagner <bungeman@google.com> Commit-Queue: Florin Malita <fmalita@chromium.org>
137 lines
3.7 KiB
C++
137 lines
3.7 KiB
C++
/*
|
|
* Copyright 2017 Google Inc.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
#ifndef Skottie_DEFINED
|
|
#define Skottie_DEFINED
|
|
|
|
#include "SkFontMgr.h"
|
|
#include "SkRefCnt.h"
|
|
#include "SkSize.h"
|
|
#include "SkString.h"
|
|
#include "SkTypes.h"
|
|
|
|
#include <memory>
|
|
|
|
class SkCanvas;
|
|
class SkData;
|
|
struct SkRect;
|
|
class SkStream;
|
|
|
|
namespace skjson { class ObjectValue; }
|
|
namespace sksg { class Scene; }
|
|
|
|
namespace skottie {
|
|
|
|
class SK_API ResourceProvider {
|
|
public:
|
|
ResourceProvider() = default;
|
|
virtual ~ResourceProvider() = default;
|
|
ResourceProvider(const ResourceProvider&) = delete;
|
|
ResourceProvider& operator=(const ResourceProvider&) = delete;
|
|
|
|
virtual sk_sp<SkData> load(const char resource_path[], const char resource_name[]) const = 0;
|
|
};
|
|
|
|
class SK_API Animation : public SkRefCnt {
|
|
public:
|
|
|
|
class Builder final {
|
|
public:
|
|
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; }
|
|
|
|
/**
|
|
* Specify a loader for external resources (images, etc.). Ownership stays with the caller
|
|
* (the ResrouceProvider must be valid for this builder's lifespan).
|
|
*/
|
|
Builder& setResourceProvider(const ResourceProvider*);
|
|
|
|
/**
|
|
* Specify a font manager for loading animation fonts.
|
|
*/
|
|
Builder& setFontManager(sk_sp<SkFontMgr>);
|
|
|
|
/**
|
|
* 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:
|
|
const ResourceProvider* fResourceProvider = nullptr;
|
|
sk_sp<SkFontMgr> fFontMgr;
|
|
Stats fStats;
|
|
};
|
|
|
|
/**
|
|
* 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[]);
|
|
|
|
~Animation() override;
|
|
|
|
/**
|
|
* 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|.
|
|
*
|
|
* @param t normalized [0..1] frame selector (0 -> first frame, 1 -> final frame)
|
|
*
|
|
*/
|
|
void seek(SkScalar t);
|
|
|
|
/**
|
|
* Returns the animation duration in seconds.
|
|
*/
|
|
SkScalar duration() const { return fDuration; }
|
|
|
|
const SkString& version() const { return fVersion; }
|
|
const SkSize& size() const { return fSize; }
|
|
|
|
void setShowInval(bool show);
|
|
|
|
private:
|
|
Animation(std::unique_ptr<sksg::Scene>, SkString ver, const SkSize& size,
|
|
SkScalar inPoint, SkScalar outPoint, SkScalar duration);
|
|
|
|
std::unique_ptr<sksg::Scene> fScene;
|
|
const SkString fVersion;
|
|
const SkSize fSize;
|
|
const SkScalar fInPoint,
|
|
fOutPoint,
|
|
fDuration;
|
|
|
|
typedef SkRefCnt INHERITED;
|
|
};
|
|
|
|
} // namespace skottie
|
|
|
|
#endif // Skottie_DEFINED
|