skia2/modules/sksg/include/SkSGScene.h

84 lines
1.6 KiB
C
Raw Normal View History

/*
* 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 SkSGScene_DEFINED
#define SkSGScene_DEFINED
#include "include/core/SkRefCnt.h"
#include "include/core/SkTypes.h"
#include <memory>
#include <vector>
class SkCanvas;
struct SkPoint;
namespace sksg {
class InvalidationController;
class RenderNode;
/**
* Base class for animators.
*
*/
class Animator : public SkRefCnt {
public:
virtual ~Animator();
Animator(const Animator&) = delete;
Animator& operator=(const Animator&) = delete;
[skottie] Refactor property animators Currently, property animators use lambda captures (std::function<>) to push values to adapters and then to the scene graph. Some downsides: * complex lambda captures are expensive in terms of object code size * adapters with multiple animated properties don't synchronize/quiesce: each individual property tick triggers a SG synchronization, possibly with inconsistent state (as animator running order is unspecified) * there is no enforced scoping, resulting in fragile constructs when SG fragments are discarded This CL introduces a simplified and more robust animator pattern: * property animators are scoped to explicit containers * instead of capturing arbitrary value functors, animators only capture a pointer to the target value Some implementation details: * keyframe/interpolation logic is pretty much unchanged (just relocated) * introduced AnimatablePropertyContainer - a base class for animatable adapters * legacy binding functions are refactored based on the new mechanism (they now/transitionally inject adapter objects) * converted a handful of effects, to exercise trivial refactoring patterns * converted the text animator goo, to exercise non-trivial refactoring: - detecting value changes is now trickier (no more lambda magic) - value adjustments must be hoisted into adapter logic (no more lambda magic) - all dependent animated values (selectors, etc) must be scoped to the text adapter to avoid lifetime issues TBR= Change-Id: Ia5821982f251de0de58fd3f87812219ff7fcc726 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/263938 Commit-Queue: Florin Malita <fmalita@chromium.org> Reviewed-by: Mike Reed <reed@google.com>
2020-01-16 21:46:04 +00:00
void tick(float t) { this->onTick(t); }
protected:
Animator();
virtual void onTick(float t) = 0;
};
using AnimatorList = std::vector<sk_sp<Animator>>;
class GroupAnimator : public Animator {
protected:
explicit GroupAnimator(AnimatorList&&);
void onTick(float t) override;
private:
const AnimatorList fAnimators;
using INHERITED = Animator;
};
/**
* Holds a scene root and a list of animators.
*
* Provides high-level mehods for driving rendering and animations.
*
*/
class Scene final {
public:
static std::unique_ptr<Scene> Make(sk_sp<RenderNode> root, AnimatorList&& animators);
~Scene();
Scene(const Scene&) = delete;
Scene& operator=(const Scene&) = delete;
void render(SkCanvas*) const;
void animate(float t, InvalidationController* = nullptr);
const RenderNode* nodeAt(const SkPoint&) const;
private:
Scene(sk_sp<RenderNode> root, AnimatorList&& animators);
const sk_sp<RenderNode> fRoot;
const AnimatorList fAnimators;
};
} // namespace sksg
#endif // SkSGScene_DEFINED