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/geometry/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
{
35 VISIBILITY
= (1 << 3),
36 BRIGHTNESS
= (1 << 4),
40 // Used when iterating over properties.
41 FIRST_PROPERTY
= TRANSFORM
,
45 static AnimatableProperty
ToAnimatableProperty(
46 cc::Animation::TargetProperty property
);
48 struct COMPOSITOR_EXPORT TargetValue
{
50 // Initializes the target value to match the delegate. NULL may be supplied.
51 explicit TargetValue(const LayerAnimationDelegate
* delegate
);
54 gfx::Transform transform
;
62 typedef uint32 AnimatableProperties
;
64 LayerAnimationElement(AnimatableProperties properties
,
65 base::TimeDelta duration
);
67 virtual ~LayerAnimationElement();
69 // Creates an element that transitions to the given transform. The caller owns
71 static LayerAnimationElement
* CreateTransformElement(
72 const gfx::Transform
& transform
,
73 base::TimeDelta duration
);
75 // Creates an element that counters a transition to the given transform.
76 // This element maintains the invariant uninverted_transition->at(t) *
77 // this->at(t) == base_transform * this->at(t_start) for any t. The caller
78 // owns the return value.
79 static LayerAnimationElement
* CreateInverseTransformElement(
80 const gfx::Transform
& base_transform
,
81 const LayerAnimationElement
* uninverted_transition
);
84 // Duplicates elements as created by CreateInverseTransformElement.
85 static LayerAnimationElement
* CloneInverseTransformElement(
86 const LayerAnimationElement
* other
);
88 // Creates an element that transitions to another in a way determined by an
89 // interpolated transform. The element accepts ownership of the interpolated
90 // transform. NB: at every step, the interpolated transform clobbers the
91 // existing transform. That is, it does not interpolate between the existing
92 // transform and the last value the interpolated transform will assume. It is
93 // therefore important that the value of the interpolated at time 0 matches
94 // the current transform.
95 static LayerAnimationElement
* CreateInterpolatedTransformElement(
96 InterpolatedTransform
* interpolated_transform
,
97 base::TimeDelta duration
);
99 // Creates an element that transitions to the given bounds. The caller owns
101 static LayerAnimationElement
* CreateBoundsElement(
102 const gfx::Rect
& bounds
,
103 base::TimeDelta duration
);
105 // Creates an element that transitions to the given opacity. The caller owns
107 static LayerAnimationElement
* CreateOpacityElement(
109 base::TimeDelta duration
);
111 // Creates an element that sets visibily following a delay. The caller owns
113 static LayerAnimationElement
* CreateVisibilityElement(
115 base::TimeDelta duration
);
117 // Creates an element that transitions to the given brightness.
118 // The caller owns the return value.
119 static LayerAnimationElement
* CreateBrightnessElement(
121 base::TimeDelta duration
);
123 // Creates an element that transitions to the given grayscale value.
124 // The caller owns the return value.
125 static LayerAnimationElement
* CreateGrayscaleElement(
127 base::TimeDelta duration
);
129 // Creates an element that pauses the given properties. The caller owns the
131 static LayerAnimationElement
* CreatePauseElement(
132 AnimatableProperties properties
,
133 base::TimeDelta duration
);
135 // Creates an element that transitions to the given color. The caller owns the
137 static LayerAnimationElement
* CreateColorElement(
139 base::TimeDelta duration
);
141 // Sets the start time for the animation. This must be called before the first
142 // call to {Start, IsFinished}. Once the animation is finished, this must
143 // be called again in order to restart the animation.
144 void set_requested_start_time(base::TimeTicks start_time
) {
145 requested_start_time_
= start_time
;
147 base::TimeTicks
requested_start_time() const { return requested_start_time_
; }
149 // Sets the actual start time for the animation, taking into account any
151 void set_effective_start_time(base::TimeTicks start_time
) {
152 effective_start_time_
= start_time
;
154 base::TimeTicks
effective_start_time() const { return effective_start_time_
; }
156 // This must be called before the first call to Progress. If starting the
157 // animation involves dispatching to another thread, then this will proceed
158 // with that dispatch, ultimately resulting in the animation getting an
159 // effective start time (the time the animation starts on the other thread).
160 void Start(LayerAnimationDelegate
* delegate
, int animation_group_id
);
162 // Returns true if the animation has started but hasn't finished.
163 bool Started() { return !first_frame_
; }
165 // Updates the delegate to the appropriate value for |now|. Returns true
166 // if a redraw is required.
167 bool Progress(base::TimeTicks now
, LayerAnimationDelegate
* delegate
);
169 // If calling Progress now, with the given time, will finish the animation,
170 // returns true and sets |end_duration| to the actual duration for this
171 // animation, incuding any queueing delays.
172 bool IsFinished(base::TimeTicks time
, base::TimeDelta
* total_duration
);
174 // Updates the delegate to the end of the animation. Returns true if a
175 // redraw is required.
176 bool ProgressToEnd(LayerAnimationDelegate
* delegate
);
178 // Called if the animation is not allowed to complete. This may be called
179 // before OnStarted or Progress.
180 void Abort(LayerAnimationDelegate
* delegate
);
182 // Assigns the target value to |target|.
183 void GetTargetValue(TargetValue
* target
) const;
185 // The properties that the element modifies.
186 AnimatableProperties
properties() const { return properties_
; }
188 // Whether this element animates on the compositor thread.
189 virtual bool IsThreaded() const;
191 gfx::Tween::Type
tween_type() const { return tween_type_
; }
192 void set_tween_type(gfx::Tween::Type tween_type
) { tween_type_
= tween_type
; }
194 // Each LayerAnimationElement has a unique animation_id. Elements belonging
195 // to sequences that are supposed to start together have the same
196 // animation_group_id.
197 int animation_id() const { return animation_id_
; }
198 int animation_group_id() const { return animation_group_id_
; }
199 void set_animation_group_id(int id
) { animation_group_id_
= id
; }
201 base::TimeDelta
duration() const { return duration_
; }
203 // The fraction of the animation that has been completed after the last
204 // call made to {Progress, ProgressToEnd}.
205 double last_progressed_fraction() const { return last_progressed_fraction_
; }
208 // Called once each time the animation element is run before any call to
210 virtual void OnStart(LayerAnimationDelegate
* delegate
) = 0;
211 virtual bool OnProgress(double t
, LayerAnimationDelegate
* delegate
) = 0;
212 virtual void OnGetTarget(TargetValue
* target
) const = 0;
213 virtual void OnAbort(LayerAnimationDelegate
* delegate
) = 0;
215 // Actually start the animation, dispatching to another thread if needed.
216 virtual void RequestEffectiveStart(LayerAnimationDelegate
* delegate
);
218 LayerAnimationElement(const LayerAnimationElement
& element
);
221 // For debugging purposes, we sometimes alter the duration we actually use.
222 // For example, during tests we often set duration = 0, and it is sometimes
223 // useful to slow animations down to see them more clearly.
224 base::TimeDelta
GetEffectiveDuration(const base::TimeDelta
& delta
);
227 const AnimatableProperties properties_
;
228 base::TimeTicks requested_start_time_
;
230 // When the animation actually started, taking into account queueing delays.
231 base::TimeTicks effective_start_time_
;
232 const base::TimeDelta duration_
;
233 gfx::Tween::Type tween_type_
;
235 const int animation_id_
;
236 int animation_group_id_
;
238 double last_progressed_fraction_
;
240 base::WeakPtrFactory
<LayerAnimationElement
> weak_ptr_factory_
;
242 DISALLOW_ASSIGN(LayerAnimationElement
);
247 #endif // UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_