layer_animation_sequence.h revision 3551c9c881056c480085172ff9840cab31610854
1// Copyright (c) 2012 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 UI_COMPOSITOR_LAYER_ANIMATION_SEQUENCE_H_
6#define UI_COMPOSITOR_LAYER_ANIMATION_SEQUENCE_H_
7
8#include <vector>
9
10#include "base/gtest_prod_util.h"
11#include "base/memory/linked_ptr.h"
12#include "base/memory/weak_ptr.h"
13#include "base/observer_list.h"
14#include "base/time/time.h"
15#include "ui/compositor/compositor_export.h"
16#include "ui/compositor/layer_animation_element.h"
17
18namespace ui {
19
20class LayerAnimationDelegate;
21class LayerAnimationObserver;
22
23// Contains a collection of layer animation elements to be played one after
24// another. Although it has a similar interface to LayerAnimationElement, it is
25// not a LayerAnimationElement (i.e., it is not permitted to have a sequence in
26// a sequence). Sequences own their elements, and sequences are themselves owned
27// by a LayerAnimator.
28//
29// TODO(vollick) Create a 'blended' sequence for transitioning between
30// sequences.
31// TODO(vollick) Eventually, the LayerAnimator will switch to a model where new
32// work is scheduled rather than calling methods directly. This should make it
33// impossible for temporary pointers to running animations to go stale. When
34// this happens, there will be no need for LayerAnimationSequences to support
35// weak pointers.
36class COMPOSITOR_EXPORT LayerAnimationSequence
37    : public base::SupportsWeakPtr<LayerAnimationSequence> {
38 public:
39  LayerAnimationSequence();
40  // Takes ownership of the given element and adds it to the sequence.
41  explicit LayerAnimationSequence(LayerAnimationElement* element);
42  virtual ~LayerAnimationSequence();
43
44  // Sets the start time for the animation. This must be called before the
45  // first call to {Start, IsFinished}. Once the animation is finished, this
46  // must be called again in order to restart the animation.
47  void set_start_time(base::TimeTicks start_time) { start_time_ = start_time; }
48  base::TimeTicks start_time() const { return start_time_; }
49
50  // Sets a flag indicating that this sequence will start together with other
51  // sequences, and at least one of the sequences in this group has a threaded
52  // first element.
53  void set_waiting_for_group_start(bool waiting) {
54    waiting_for_group_start_ = waiting;
55  }
56  bool waiting_for_group_start() { return waiting_for_group_start_; }
57
58  // This must be called before the first call to Progress. If starting the
59  // animation involves dispatching to another thread, then this will proceed
60  // with that dispatch, ultimately resulting in the animation getting an
61  // effective start time (the time the animation starts on the other thread).
62  void Start(LayerAnimationDelegate* delegate);
63
64  // Updates the delegate to the appropriate value for |now|. Requests a
65  // redraw if it is required.
66  void Progress(base::TimeTicks now, LayerAnimationDelegate* delegate);
67
68  // Returns true if calling Progress now, with the given time, will finish
69  // the animation.
70  bool IsFinished(base::TimeTicks time);
71
72  // Updates the delegate to the end of the animation; if this sequence is
73  // cyclic, updates the delegate to the end of one cycle of the sequence.
74  void ProgressToEnd(LayerAnimationDelegate* delegate);
75
76  // Sets the target value to the value that would have been set had
77  // the sequence completed. Does nothing if the sequence is cyclic.
78  void GetTargetValue(LayerAnimationElement::TargetValue* target) const;
79
80  // Aborts the given animation.
81  void Abort(LayerAnimationDelegate* delegate);
82
83  // All properties modified by the sequence.
84  const LayerAnimationElement::AnimatableProperties& properties() const {
85    return properties_;
86  }
87
88  // Adds an element to the sequence. The sequences takes ownership of this
89  // element.
90  void AddElement(LayerAnimationElement* element);
91
92  // Sequences can be looped indefinitely.
93  void set_is_cyclic(bool is_cyclic) { is_cyclic_ = is_cyclic; }
94  bool is_cyclic() const { return is_cyclic_; }
95
96  // Returns true if this sequence has at least one element conflicting with a
97  // property in |other|.
98  bool HasConflictingProperty(
99      const LayerAnimationElement::AnimatableProperties& other) const;
100
101  // Returns true if the first element animates on the compositor thread.
102  bool IsFirstElementThreaded() const;
103
104  // Used to identify groups of sequences that are supposed to start together.
105  // Once started, used to identify the sequence that owns a particular
106  // threaded animation.
107  int animation_group_id() const { return animation_group_id_; }
108  void set_animation_group_id(int id) { animation_group_id_ = id; }
109
110  // These functions are used for adding or removing observers from the observer
111  // list. The observers are notified when animations end.
112  void AddObserver(LayerAnimationObserver* observer);
113  void RemoveObserver(LayerAnimationObserver* observer);
114
115  // Called when a threaded animation is actually started.
116  void OnThreadedAnimationStarted(const cc::AnimationEvent& event);
117
118  // Called when the animator schedules this sequence.
119  void OnScheduled();
120
121  // Called when the animator is destroyed.
122  void OnAnimatorDestroyed();
123
124  // The last_progressed_fraction of the element most recently progressed by
125  // by this sequence. Returns 0.0 if no elements have been progressed.
126  double last_progressed_fraction() const { return last_progressed_fraction_; }
127
128 private:
129  friend class LayerAnimatorTestController;
130
131  typedef std::vector<linked_ptr<LayerAnimationElement> > Elements;
132
133  FRIEND_TEST_ALL_PREFIXES(LayerAnimatorTest,
134                           ObserverReleasedBeforeAnimationSequenceEnds);
135
136  // Notifies the observers that this sequence has been scheduled.
137  void NotifyScheduled();
138
139  // Notifies the observers that this sequence has ended.
140  void NotifyEnded();
141
142  // Notifies the observers that this sequence has been aborted.
143  void NotifyAborted();
144
145  // The currently animating element.
146  LayerAnimationElement* CurrentElement();
147
148  // The union of all the properties modified by all elements in the sequence.
149  LayerAnimationElement::AnimatableProperties properties_;
150
151  // The elements in the sequence.
152  Elements elements_;
153
154  // True if the sequence should be looped forever.
155  bool is_cyclic_;
156
157  // These are used when animating to efficiently find the next element.
158  size_t last_element_;
159  base::TimeTicks last_start_;
160
161  // The start time of the current run of the sequence.
162  base::TimeTicks start_time_;
163
164  // True if this sequence will start together with other sequences, and at
165  // least one of the sequences in this group has a threaded first element.
166  bool waiting_for_group_start_;
167
168  // Identifies groups of sequences that are supposed to start together.
169  // Also used to identify the owner of a particular threaded animation; any
170  // in-progress threaded animation owned by this sequence will have this
171  // group id.
172  int animation_group_id_;
173
174  // These parties are notified when layer animations end.
175  ObserverList<LayerAnimationObserver> observers_;
176
177  // Tracks the last_progressed_fraction() of the most recently progressed
178  // element.
179  double last_progressed_fraction_;
180
181  DISALLOW_COPY_AND_ASSIGN(LayerAnimationSequence);
182};
183
184}  // namespace ui
185
186#endif  // UI_COMPOSITOR_LAYER_ANIMATION_SEQUENCE_H_
187