layer_animation_element.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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_ELEMENT_H_
6#define UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_
7
8#include <set>
9
10#include "base/time.h"
11#include "cc/animation/animation.h"
12#include "cc/animation/animation_events.h"
13#include "third_party/skia/include/core/SkColor.h"
14#include "ui/base/animation/tween.h"
15#include "ui/compositor/compositor_export.h"
16#include "ui/gfx/rect.h"
17#include "ui/gfx/transform.h"
18
19namespace ui {
20
21class InterpolatedTransform;
22class LayerAnimationDelegate;
23
24// LayerAnimationElements represent one segment of an animation between two
25// keyframes. They know how to update a LayerAnimationDelegate given a value
26// between 0 and 1 (0 for initial, and 1 for final).
27class COMPOSITOR_EXPORT LayerAnimationElement {
28 public:
29  enum AnimatableProperty {
30    TRANSFORM = 0,
31    BOUNDS,
32    OPACITY,
33    VISIBILITY,
34    BRIGHTNESS,
35    GRAYSCALE,
36    COLOR,
37  };
38
39  static AnimatableProperty ToAnimatableProperty(
40      cc::Animation::TargetProperty property);
41
42  struct COMPOSITOR_EXPORT TargetValue {
43    TargetValue();
44    // Initializes the target value to match the delegate. NULL may be supplied.
45    explicit TargetValue(const LayerAnimationDelegate* delegate);
46
47    gfx::Rect bounds;
48    gfx::Transform transform;
49    float opacity;
50    bool visibility;
51    float brightness;
52    float grayscale;
53    SkColor color;
54  };
55
56  typedef std::set<AnimatableProperty> AnimatableProperties;
57
58  LayerAnimationElement(const AnimatableProperties& properties,
59                        base::TimeDelta duration);
60  virtual ~LayerAnimationElement();
61
62  // Creates an element that transitions to the given transform. The caller owns
63  // the return value.
64  static LayerAnimationElement* CreateTransformElement(
65      const gfx::Transform& transform,
66      base::TimeDelta duration);
67
68  // Creates an element that transitions to another in a way determined by an
69  // interpolated transform. The element accepts ownership of the interpolated
70  // transform. NB: at every step, the interpolated transform clobbers the
71  // existing transform. That is, it does not interpolate between the existing
72  // transform and the last value the interpolated transform will assume. It is
73  // therefore important that the value of the interpolated at time 0 matches
74  // the current transform.
75  static LayerAnimationElement* CreateInterpolatedTransformElement(
76      InterpolatedTransform* interpolated_transform,
77      base::TimeDelta duration);
78
79  // Creates an element that transitions to the given bounds. The caller owns
80  // the return value.
81  static LayerAnimationElement* CreateBoundsElement(
82      const gfx::Rect& bounds,
83      base::TimeDelta duration);
84
85  // Creates an element that transitions to the given opacity. The caller owns
86  // the return value.
87  static LayerAnimationElement* CreateOpacityElement(
88      float opacity,
89      base::TimeDelta duration);
90
91  // Creates an element that sets visibily following a delay. The caller owns
92  // the return value.
93  static LayerAnimationElement* CreateVisibilityElement(
94      bool visibility,
95      base::TimeDelta duration);
96
97  // Creates an element that transitions to the given brightness.
98  // The caller owns the return value.
99  static LayerAnimationElement* CreateBrightnessElement(
100      float brightness,
101      base::TimeDelta duration);
102
103  // Creates an element that transitions to the given grayscale value.
104  // The caller owns the return value.
105  static LayerAnimationElement* CreateGrayscaleElement(
106      float grayscale,
107      base::TimeDelta duration);
108
109  // Creates an element that pauses the given properties. The caller owns the
110  // return value.
111  static LayerAnimationElement* CreatePauseElement(
112      const AnimatableProperties& properties,
113      base::TimeDelta duration);
114
115  // Creates an element that transitions to the given color. The caller owns the
116  // return value.
117  static LayerAnimationElement* CreateColorElement(
118      SkColor color,
119      base::TimeDelta duration);
120
121  // Sets the start time for the animation. This must be called before the first
122  // call to {Start, IsFinished}. Once the animation is finished, this must
123  // be called again in order to restart the animation.
124  void set_requested_start_time(base::TimeTicks start_time) {
125    requested_start_time_ = start_time;
126  }
127  base::TimeTicks requested_start_time() const { return requested_start_time_; }
128
129  // Sets the actual start time for the animation, taking into account any
130  // queueing delays.
131  void set_effective_start_time(base::TimeTicks start_time) {
132    effective_start_time_ = start_time;
133  }
134  base::TimeTicks effective_start_time() const { return effective_start_time_; }
135
136  // This must be called before the first call to Progress. If starting the
137  // animation involves dispatching to another thread, then this will proceed
138  // with that dispatch, ultimately resulting in the animation getting an
139  // effective start time (the time the animation starts on the other thread).
140  void Start(LayerAnimationDelegate* delegate, int animation_group_id);
141
142  // Returns true if the animation has started but hasn't finished.
143  bool Started() { return !first_frame_; }
144
145  // Updates the delegate to the appropriate value for |now|. Returns true
146  // if a redraw is required.
147  bool Progress(base::TimeTicks now, LayerAnimationDelegate* delegate);
148
149  // If calling Progress now, with the given time, will finish the animation,
150  // returns true and sets |end_duration| to the actual duration for this
151  // animation, incuding any queueing delays.
152  bool IsFinished(base::TimeTicks time, base::TimeDelta* total_duration);
153
154  // Updates the delegate to the end of the animation. Returns true if a
155  // redraw is required.
156  bool ProgressToEnd(LayerAnimationDelegate* delegate);
157
158  // Called if the animation is not allowed to complete. This may be called
159  // before OnStarted or Progress.
160  void Abort(LayerAnimationDelegate* delegate);
161
162  // Assigns the target value to |target|.
163  void GetTargetValue(TargetValue* target) const;
164
165  // The properties that the element modifies.
166  const AnimatableProperties& properties() const { return properties_; }
167
168  // Whether this element animates on the compositor thread.
169  virtual bool IsThreaded() const;
170
171  Tween::Type tween_type() const { return tween_type_; }
172  void set_tween_type(Tween::Type tween_type) { tween_type_ = tween_type; }
173
174  // Each LayerAnimationElement has a unique animation_id. Elements belonging
175  // to sequences that are supposed to start together have the same
176  // animation_group_id.
177  int animation_id() const { return animation_id_; }
178  int animation_group_id() const { return animation_group_id_; }
179  void set_animation_group_id(int id) { animation_group_id_ = id; }
180
181  // The fraction of the animation that has been completed after the last
182  // call made to {Progress, ProgressToEnd}.
183  double last_progressed_fraction() const { return last_progressed_fraction_; }
184
185 protected:
186  // Called once each time the animation element is run before any call to
187  // OnProgress.
188  virtual void OnStart(LayerAnimationDelegate* delegate) = 0;
189  virtual bool OnProgress(double t, LayerAnimationDelegate* delegate) = 0;
190  virtual void OnGetTarget(TargetValue* target) const = 0;
191  virtual void OnAbort(LayerAnimationDelegate* delegate) = 0;
192
193  base::TimeDelta duration() const { return duration_; }
194
195  // Actually start the animation, dispatching to another thread if needed.
196  virtual void RequestEffectiveStart(LayerAnimationDelegate* delegate);
197
198 private:
199  // For debugging purposes, we sometimes alter the duration we actually use.
200  // For example, during tests we often set duration = 0, and it is sometimes
201  // useful to slow animations down to see them more clearly.
202  base::TimeDelta GetEffectiveDuration(const base::TimeDelta& delta);
203
204  bool first_frame_;
205  const AnimatableProperties properties_;
206  base::TimeTicks requested_start_time_;
207
208  // When the animation actually started, taking into account queueing delays.
209  base::TimeTicks effective_start_time_;
210  const base::TimeDelta duration_;
211  Tween::Type tween_type_;
212
213  const int animation_id_;
214  int animation_group_id_;
215
216  double last_progressed_fraction_;
217
218  DISALLOW_COPY_AND_ASSIGN(LayerAnimationElement);
219};
220
221}  // namespace ui
222
223#endif  // UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_
224