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_ELEMENT_H_
6 #define UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_
10 #include "base/memory/weak_ptr.h"
11 #include "base/time/time.h"
12 #include "cc/animation/animation.h"
13 #include "cc/animation/animation_events.h"
14 #include "third_party/skia/include/core/SkColor.h"
15 #include "ui/compositor/compositor_export.h"
16 #include "ui/gfx/animation/tween.h"
17 #include "ui/gfx/rect.h"
18 #include "ui/gfx/transform.h"
22 class InterpolatedTransform;
23 class LayerAnimationDelegate;
25 // LayerAnimationElements represent one segment of an animation between two
26 // keyframes. They know how to update a LayerAnimationDelegate given a value
27 // between 0 and 1 (0 for initial, and 1 for final).
28 class COMPOSITOR_EXPORT LayerAnimationElement {
30 enum AnimatableProperty {
40 static AnimatableProperty ToAnimatableProperty(
41 cc::Animation::TargetProperty property);
43 struct COMPOSITOR_EXPORT TargetValue {
45 // Initializes the target value to match the delegate. NULL may be supplied.
46 explicit TargetValue(const LayerAnimationDelegate* delegate);
49 gfx::Transform transform;
57 typedef std::set<AnimatableProperty> AnimatableProperties;
59 LayerAnimationElement(const AnimatableProperties& properties,
60 base::TimeDelta duration);
62 virtual ~LayerAnimationElement();
64 // Creates an element that transitions to the given transform. The caller owns
66 static LayerAnimationElement* CreateTransformElement(
67 const gfx::Transform& transform,
68 base::TimeDelta duration);
70 // Creates an element that counters a transition to the given transform.
71 // This element maintains the invariant uninverted_transition->at(t) *
72 // this->at(t) == base_transform * this->at(t_start) for any t. The caller
73 // owns the return value.
74 static LayerAnimationElement* CreateInverseTransformElement(
75 const gfx::Transform& base_transform,
76 const LayerAnimationElement* uninverted_transition);
79 // Duplicates elements as created by CreateInverseTransformElement.
80 static LayerAnimationElement* CloneInverseTransformElement(
81 const LayerAnimationElement* other);
83 // Creates an element that transitions to another in a way determined by an
84 // interpolated transform. The element accepts ownership of the interpolated
85 // transform. NB: at every step, the interpolated transform clobbers the
86 // existing transform. That is, it does not interpolate between the existing
87 // transform and the last value the interpolated transform will assume. It is
88 // therefore important that the value of the interpolated at time 0 matches
89 // the current transform.
90 static LayerAnimationElement* CreateInterpolatedTransformElement(
91 InterpolatedTransform* interpolated_transform,
92 base::TimeDelta duration);
94 // Creates an element that transitions to the given bounds. The caller owns
96 static LayerAnimationElement* CreateBoundsElement(
97 const gfx::Rect& bounds,
98 base::TimeDelta duration);
100 // Creates an element that transitions to the given opacity. The caller owns
102 static LayerAnimationElement* CreateOpacityElement(
104 base::TimeDelta duration);
106 // Creates an element that sets visibily following a delay. The caller owns
108 static LayerAnimationElement* CreateVisibilityElement(
110 base::TimeDelta duration);
112 // Creates an element that transitions to the given brightness.
113 // The caller owns the return value.
114 static LayerAnimationElement* CreateBrightnessElement(
116 base::TimeDelta duration);
118 // Creates an element that transitions to the given grayscale value.
119 // The caller owns the return value.
120 static LayerAnimationElement* CreateGrayscaleElement(
122 base::TimeDelta duration);
124 // Creates an element that pauses the given properties. The caller owns the
126 static LayerAnimationElement* CreatePauseElement(
127 const AnimatableProperties& properties,
128 base::TimeDelta duration);
130 // Creates an element that transitions to the given color. The caller owns the
132 static LayerAnimationElement* CreateColorElement(
134 base::TimeDelta duration);
136 // Sets the start time for the animation. This must be called before the first
137 // call to {Start, IsFinished}. Once the animation is finished, this must
138 // be called again in order to restart the animation.
139 void set_requested_start_time(base::TimeTicks start_time) {
140 requested_start_time_ = start_time;
142 base::TimeTicks requested_start_time() const { return requested_start_time_; }
144 // Sets the actual start time for the animation, taking into account any
146 void set_effective_start_time(base::TimeTicks start_time) {
147 effective_start_time_ = start_time;
149 base::TimeTicks effective_start_time() const { return effective_start_time_; }
151 // This must be called before the first call to Progress. If starting the
152 // animation involves dispatching to another thread, then this will proceed
153 // with that dispatch, ultimately resulting in the animation getting an
154 // effective start time (the time the animation starts on the other thread).
155 void Start(LayerAnimationDelegate* delegate, int animation_group_id);
157 // Returns true if the animation has started but hasn't finished.
158 bool Started() { return !first_frame_; }
160 // Updates the delegate to the appropriate value for |now|. Returns true
161 // if a redraw is required.
162 bool Progress(base::TimeTicks now, LayerAnimationDelegate* delegate);
164 // If calling Progress now, with the given time, will finish the animation,
165 // returns true and sets |end_duration| to the actual duration for this
166 // animation, incuding any queueing delays.
167 bool IsFinished(base::TimeTicks time, base::TimeDelta* total_duration);
169 // Updates the delegate to the end of the animation. Returns true if a
170 // redraw is required.
171 bool ProgressToEnd(LayerAnimationDelegate* delegate);
173 // Called if the animation is not allowed to complete. This may be called
174 // before OnStarted or Progress.
175 void Abort(LayerAnimationDelegate* delegate);
177 // Assigns the target value to |target|.
178 void GetTargetValue(TargetValue* target) const;
180 // The properties that the element modifies.
181 const AnimatableProperties& properties() const { return properties_; }
183 // Whether this element animates on the compositor thread.
184 virtual bool IsThreaded() const;
186 gfx::Tween::Type tween_type() const { return tween_type_; }
187 void set_tween_type(gfx::Tween::Type tween_type) { tween_type_ = tween_type; }
189 // Each LayerAnimationElement has a unique animation_id. Elements belonging
190 // to sequences that are supposed to start together have the same
191 // animation_group_id.
192 int animation_id() const { return animation_id_; }
193 int animation_group_id() const { return animation_group_id_; }
194 void set_animation_group_id(int id) { animation_group_id_ = id; }
196 base::TimeDelta duration() const { return duration_; }
198 // The fraction of the animation that has been completed after the last
199 // call made to {Progress, ProgressToEnd}.
200 double last_progressed_fraction() const { return last_progressed_fraction_; }
203 // Called once each time the animation element is run before any call to
205 virtual void OnStart(LayerAnimationDelegate* delegate) = 0;
206 virtual bool OnProgress(double t, LayerAnimationDelegate* delegate) = 0;
207 virtual void OnGetTarget(TargetValue* target) const = 0;
208 virtual void OnAbort(LayerAnimationDelegate* delegate) = 0;
210 // Actually start the animation, dispatching to another thread if needed.
211 virtual void RequestEffectiveStart(LayerAnimationDelegate* delegate);
213 LayerAnimationElement(const LayerAnimationElement& element);
216 // For debugging purposes, we sometimes alter the duration we actually use.
217 // For example, during tests we often set duration = 0, and it is sometimes
218 // useful to slow animations down to see them more clearly.
219 base::TimeDelta GetEffectiveDuration(const base::TimeDelta& delta);
222 const AnimatableProperties properties_;
223 base::TimeTicks requested_start_time_;
225 // When the animation actually started, taking into account queueing delays.
226 base::TimeTicks effective_start_time_;
227 const base::TimeDelta duration_;
228 gfx::Tween::Type tween_type_;
230 const int animation_id_;
231 int animation_group_id_;
233 double last_progressed_fraction_;
235 base::WeakPtrFactory<LayerAnimationElement> weak_ptr_factory_;
237 DISALLOW_ASSIGN(LayerAnimationElement);
242 #endif // UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_