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.
5 #ifndef UI_COMPOSITOR_LAYER_ANIMATION_SEQUENCE_H_
6 #define UI_COMPOSITOR_LAYER_ANIMATION_SEQUENCE_H_
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.h"
15 #include "ui/compositor/compositor_export.h"
16 #include "ui/compositor/layer_animation_element.h"
20 class LayerAnimationDelegate
;
21 class LayerAnimationObserver
;
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.
29 // TODO(vollick) Create a 'blended' sequence for transitioning between
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
36 class COMPOSITOR_EXPORT LayerAnimationSequence
37 : public base::SupportsWeakPtr
<LayerAnimationSequence
> {
39 LayerAnimationSequence();
40 // Takes ownership of the given element and adds it to the sequence.
41 explicit LayerAnimationSequence(LayerAnimationElement
* element
);
42 virtual ~LayerAnimationSequence();
44 // Sets the start time for the animation. This must be called before the
45 // first call to {Progress, 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_
; }
50 // Updates the delegate to the appropriate value for |now|. Requests a
51 // redraw if it is required.
52 void Progress(base::TimeTicks now
, LayerAnimationDelegate
* delegate
);
54 // Returns true if calling Progress now, with the given time, will finish
56 bool IsFinished(base::TimeTicks time
);
58 // Updates the delegate to the end of the animation; if this sequence is
59 // cyclic, updates the delegate to the end of one cycle of the sequence.
60 void ProgressToEnd(LayerAnimationDelegate
* delegate
);
62 // Sets the target value to the value that would have been set had
63 // the sequence completed. Does nothing if the sequence is cyclic.
64 void GetTargetValue(LayerAnimationElement::TargetValue
* target
) const;
66 // Aborts the given animation.
69 // All properties modified by the sequence.
70 const LayerAnimationElement::AnimatableProperties
& properties() const {
74 // Adds an element to the sequence. The sequences takes ownership of this
76 void AddElement(LayerAnimationElement
* element
);
78 // Sequences can be looped indefinitely.
79 void set_is_cyclic(bool is_cyclic
) { is_cyclic_
= is_cyclic
; }
80 bool is_cyclic() const { return is_cyclic_
; }
82 // Returns true if this sequence has at least one element affecting a
83 // property in |other|.
84 bool HasCommonProperty(
85 const LayerAnimationElement::AnimatableProperties
& other
) const;
87 // These functions are used for adding or removing observers from the observer
88 // list. The observers are notified when animations end.
89 void AddObserver(LayerAnimationObserver
* observer
);
90 void RemoveObserver(LayerAnimationObserver
* observer
);
92 // Called when the animator schedules this sequence.
95 // Called when the animator is destroyed.
96 void OnAnimatorDestroyed();
99 typedef std::vector
<linked_ptr
<LayerAnimationElement
> > Elements
;
101 FRIEND_TEST_ALL_PREFIXES(LayerAnimatorTest
,
102 ObserverReleasedBeforeAnimationSequenceEnds
);
104 // Notifies the observers that this sequence has been scheduled.
105 void NotifyScheduled();
107 // Notifies the observers that this sequence has ended.
110 // Notifies the observers that this sequence has been aborted.
111 void NotifyAborted();
113 // The union of all the properties modified by all elements in the sequence.
114 LayerAnimationElement::AnimatableProperties properties_
;
116 // The elements in the sequence.
119 // True if the sequence should be looped forever.
122 // These are used when animating to efficiently find the next element.
123 size_t last_element_
;
124 base::TimeTicks last_start_
;
126 // The start time of the current run of the sequence.
127 base::TimeTicks start_time_
;
129 // These parties are notified when layer animations end.
130 ObserverList
<LayerAnimationObserver
> observers_
;
132 DISALLOW_COPY_AND_ASSIGN(LayerAnimationSequence
);
137 #endif // UI_COMPOSITOR_LAYER_ANIMATION_SEQUENCE_H_