1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CC_ANIMATION_ANIMATION_TIMELINE_H_
6 #define CC_ANIMATION_ANIMATION_TIMELINE_H_
7 
8 #include <memory>
9 #include <unordered_map>
10 
11 #include "base/memory/ref_counted.h"
12 #include "cc/animation/animation_export.h"
13 
14 namespace cc {
15 
16 class Animation;
17 class AnimationHost;
18 
19 // An AnimationTimeline owns a group of Animations.
20 //
21 // Each AnimationTimeline and its Animations have copies on the impl thread. We
22 // synchronize the main and impl thread instances using their IDs.
23 class CC_ANIMATION_EXPORT AnimationTimeline
24     : public base::RefCounted<AnimationTimeline> {
25  public:
26   static scoped_refptr<AnimationTimeline> Create(int id);
27   virtual scoped_refptr<AnimationTimeline> CreateImplInstance() const;
28 
29   AnimationTimeline(const AnimationTimeline&) = delete;
30   AnimationTimeline& operator=(const AnimationTimeline&) = delete;
31 
id()32   int id() const { return id_; }
33 
34   // Parent AnimationHost.
animation_host()35   AnimationHost* animation_host() { return animation_host_; }
animation_host()36   const AnimationHost* animation_host() const { return animation_host_; }
37   void SetAnimationHost(AnimationHost* animation_host);
38 
set_is_impl_only(bool is_impl_only)39   void set_is_impl_only(bool is_impl_only) { is_impl_only_ = is_impl_only; }
is_impl_only()40   bool is_impl_only() const { return is_impl_only_; }
41 
42   void AttachAnimation(scoped_refptr<Animation> animation);
43   void DetachAnimation(scoped_refptr<Animation> animation);
44 
45   std::vector<Animation*> GetAnimations() const;
46   void ClearAnimations();
HasAnimation()47   bool HasAnimation() const { return !id_to_animation_map_.empty(); }
48 
49   virtual void PushPropertiesTo(AnimationTimeline* timeline_impl);
ActivateTimeline()50   virtual void ActivateTimeline() {}
51 
52   Animation* GetAnimationById(int animation_id) const;
53 
54   void SetNeedsPushProperties();
needs_push_properties()55   bool needs_push_properties() const { return needs_push_properties_; }
56 
57   virtual bool IsScrollTimeline() const;
58 
59  protected:
60   explicit AnimationTimeline(int id);
61   virtual ~AnimationTimeline();
62 
63   // A list of all animations which this timeline owns.
64   using IdToAnimationMap = std::unordered_map<int, scoped_refptr<Animation>>;
65   IdToAnimationMap id_to_animation_map_;
66 
67  private:
68   friend class base::RefCounted<AnimationTimeline>;
69 
70   void PushAttachedAnimationsToImplThread(AnimationTimeline* timeline) const;
71   void RemoveDetachedAnimationsFromImplThread(
72       AnimationTimeline* timeline) const;
73   void PushPropertiesToImplThread(AnimationTimeline* timeline);
74 
75   void EraseAnimation(scoped_refptr<Animation> animation);
76 
77   int id_;
78   AnimationHost* animation_host_;
79   bool needs_push_properties_;
80 
81   // Impl-only AnimationTimeline has no main thread instance and lives on
82   // it's own.
83   bool is_impl_only_;
84 };
85 
86 }  // namespace cc
87 
88 #endif  // CC_ANIMATION_ANIMATION_TIMELINE_H_
89