layer_animation_sequence.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.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 // Updates the delegate to the appropriate value for |elapsed|, which is in 45 // the range [0, Duration()]. If the animation is not aborted, it is 46 // guaranteed that Animate will be called with elapsed = Duration(). Requests 47 // a redraw if it is required. 48 void Progress(base::TimeDelta elapsed, LayerAnimationDelegate* delegate); 49 50 // Sets the target value to the value that would have been set had 51 // the sequence completed. Does nothing if the sequence is cyclic. 52 void GetTargetValue(LayerAnimationElement::TargetValue* target) const; 53 54 // Aborts the given animation. 55 void Abort(); 56 57 // All properties modified by the sequence. 58 const LayerAnimationElement::AnimatableProperties& properties() const { 59 return properties_; 60 } 61 62 // The total, finite duration of one cycle of the sequence. 63 base::TimeDelta duration() const { 64 return duration_; 65 } 66 67 // Adds an element to the sequence. The sequences takes ownership of this 68 // element. 69 void AddElement(LayerAnimationElement* element); 70 71 // Sequences can be looped indefinitely. 72 void set_is_cyclic(bool is_cyclic) { is_cyclic_ = is_cyclic; } 73 bool is_cyclic() const { return is_cyclic_; } 74 75 // Returns true if this sequence has at least one element affecting a 76 // property in |other|. 77 bool HasCommonProperty( 78 const LayerAnimationElement::AnimatableProperties& other) const; 79 80 // These functions are used for adding or removing observers from the observer 81 // list. The observers are notified when animations end. 82 void AddObserver(LayerAnimationObserver* observer); 83 void RemoveObserver(LayerAnimationObserver* observer); 84 85 // Called when the animator schedules this sequence. 86 void OnScheduled(); 87 88 // Called when the animator is destroyed. 89 void OnAnimatorDestroyed(); 90 91 private: 92 typedef std::vector<linked_ptr<LayerAnimationElement> > Elements; 93 94 FRIEND_TEST_ALL_PREFIXES(LayerAnimatorTest, 95 ObserverReleasedBeforeAnimationSequenceEnds); 96 97 // Notifies the observers that this sequence has been scheduled. 98 void NotifyScheduled(); 99 100 // Notifies the observers that this sequence has ended. 101 void NotifyEnded(); 102 103 // Notifies the observers that this sequence has been aborted. 104 void NotifyAborted(); 105 106 // The sum of the durations of all the elements in the sequence. 107 base::TimeDelta duration_; 108 109 // The union of all the properties modified by all elements in the sequence. 110 LayerAnimationElement::AnimatableProperties properties_; 111 112 // The elements in the sequence. 113 Elements elements_; 114 115 // True if the sequence should be looped forever. 116 bool is_cyclic_; 117 118 // These are used when animating to efficiently find the next element. 119 size_t last_element_; 120 base::TimeDelta last_start_; 121 122 // These parties are notified when layer animations end. 123 ObserverList<LayerAnimationObserver> observers_; 124 125 DISALLOW_COPY_AND_ASSIGN(LayerAnimationSequence); 126}; 127 128} // namespace ui 129 130#endif // UI_COMPOSITOR_LAYER_ANIMATION_SEQUENCE_H_ 131