xref: /aosp_15_r20/external/skia/modules/skottie/src/animator/Animator.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkottieAnimator_DEFINED
9 #define SkottieAnimator_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 
13 #include <vector>
14 
15 struct SkV2;
16 
17 namespace skjson {
18 
19 class ObjectValue;
20 
21 } // namespace skjson
22 
23 namespace skottie {
24 
25 class SlotManager;
26 
27 namespace internal {
28 
29 class AnimationBuilder;
30 class AnimatorBuilder;
31 
32 class Animator : public SkRefCnt {
33 public:
34     using StateChanged = bool;
seek(float t)35     StateChanged seek(float t) { return this->onSeek(t); }
36 
37 protected:
38     Animator() = default;
39 
40     virtual StateChanged onSeek(float t) = 0;
41 
42 private:
43     Animator(const Animator&) = delete;
44     Animator& operator=(const Animator&) = delete;
45 };
46 
47 class AnimatablePropertyContainer : public Animator {
48 public:
49     // This is the workhorse for property binding: depending on whether the property is animated,
50     // it will either apply immediately or instantiate and attach a keyframe animator, scoped to
51     // this container.
52     template <typename T>
53     bool bind(const AnimationBuilder&, const skjson::ObjectValue*, T*);
54 
55     template <typename T>
bind(const AnimationBuilder & abuilder,const skjson::ObjectValue * jobject,T & v)56     bool bind(const AnimationBuilder& abuilder, const skjson::ObjectValue* jobject, T& v) {
57         return this->bind<T>(abuilder, jobject, &v);
58     }
59 
60     // A flavor of bind<Vec2Value> which drives an additional/optional orientation target
61     // (rotation in degrees), when bound to a motion path property.
62     bool bindAutoOrientable(const AnimationBuilder& abuilder,
63                             const skjson::ObjectValue* jobject,
64                             SkV2* v, float* orientation);
65 
isStatic()66     bool isStatic() const { return fAnimators.empty() && !fHasSlotID; }
67 
68 protected:
69     friend class skottie::SlotManager;
70     virtual void onSync() = 0;
71 
72     void shrink_to_fit();
73 
74     void attachDiscardableAdapter(sk_sp<AnimatablePropertyContainer>);
75 
76 private:
77     StateChanged onSeek(float) final;
78 
79     bool bindImpl(const AnimationBuilder&, const skjson::ObjectValue*, AnimatorBuilder&);
80 
81     std::vector<sk_sp<Animator>> fAnimators;
82     bool                         fHasSynced = false;
83     bool                         fHasSlotID = false;
84 };
85 
86 } // namespace internal
87 } // namespace skottie
88 
89 #endif // SkottieAnimator_DEFINED
90