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/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"
19
20namespace ui {
21
22class InterpolatedTransform;
23class LayerAnimationDelegate;
24
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).
28class COMPOSITOR_EXPORT LayerAnimationElement {
29 public:
30  enum AnimatableProperty {
31    TRANSFORM = 0,
32    BOUNDS,
33    OPACITY,
34    VISIBILITY,
35    BRIGHTNESS,
36    GRAYSCALE,
37    COLOR,
38  };
39
40  static AnimatableProperty ToAnimatableProperty(
41      cc::Animation::TargetProperty property);
42
43  struct COMPOSITOR_EXPORT TargetValue {
44    TargetValue();
45    // Initializes the target value to match the delegate. NULL may be supplied.
46    explicit TargetValue(const LayerAnimationDelegate* delegate);
47
48    gfx::Rect bounds;
49    gfx::Transform transform;
50    float opacity;
51    bool visibility;
52    float brightness;
53    float grayscale;
54    SkColor color;
55  };
56
57  typedef std::set<AnimatableProperty> AnimatableProperties;
58
59  LayerAnimationElement(const AnimatableProperties& properties,
60                        base::TimeDelta duration);
61
62  virtual ~LayerAnimationElement();
63
64  // Creates an element that transitions to the given transform. The caller owns
65  // the return value.
66  static LayerAnimationElement* CreateTransformElement(
67      const gfx::Transform& transform,
68      base::TimeDelta duration);
69
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);
77
78
79  // Duplicates elements as created by CreateInverseTransformElement.
80  static LayerAnimationElement* CloneInverseTransformElement(
81      const LayerAnimationElement* other);
82
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);
93
94  // Creates an element that transitions to the given bounds. The caller owns
95  // the return value.
96  static LayerAnimationElement* CreateBoundsElement(
97      const gfx::Rect& bounds,
98      base::TimeDelta duration);
99
100  // Creates an element that transitions to the given opacity. The caller owns
101  // the return value.
102  static LayerAnimationElement* CreateOpacityElement(
103      float opacity,
104      base::TimeDelta duration);
105
106  // Creates an element that sets visibily following a delay. The caller owns
107  // the return value.
108  static LayerAnimationElement* CreateVisibilityElement(
109      bool visibility,
110      base::TimeDelta duration);
111
112  // Creates an element that transitions to the given brightness.
113  // The caller owns the return value.
114  static LayerAnimationElement* CreateBrightnessElement(
115      float brightness,
116      base::TimeDelta duration);
117
118  // Creates an element that transitions to the given grayscale value.
119  // The caller owns the return value.
120  static LayerAnimationElement* CreateGrayscaleElement(
121      float grayscale,
122      base::TimeDelta duration);
123
124  // Creates an element that pauses the given properties. The caller owns the
125  // return value.
126  static LayerAnimationElement* CreatePauseElement(
127      const AnimatableProperties& properties,
128      base::TimeDelta duration);
129
130  // Creates an element that transitions to the given color. The caller owns the
131  // return value.
132  static LayerAnimationElement* CreateColorElement(
133      SkColor color,
134      base::TimeDelta duration);
135
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;
141  }
142  base::TimeTicks requested_start_time() const { return requested_start_time_; }
143
144  // Sets the actual start time for the animation, taking into account any
145  // queueing delays.
146  void set_effective_start_time(base::TimeTicks start_time) {
147    effective_start_time_ = start_time;
148  }
149  base::TimeTicks effective_start_time() const { return effective_start_time_; }
150
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);
156
157  // Returns true if the animation has started but hasn't finished.
158  bool Started() { return !first_frame_; }
159
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);
163
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);
168
169  // Updates the delegate to the end of the animation. Returns true if a
170  // redraw is required.
171  bool ProgressToEnd(LayerAnimationDelegate* delegate);
172
173  // Called if the animation is not allowed to complete. This may be called
174  // before OnStarted or Progress.
175  void Abort(LayerAnimationDelegate* delegate);
176
177  // Assigns the target value to |target|.
178  void GetTargetValue(TargetValue* target) const;
179
180  // The properties that the element modifies.
181  const AnimatableProperties& properties() const { return properties_; }
182
183  // Whether this element animates on the compositor thread.
184  virtual bool IsThreaded() const;
185
186  gfx::Tween::Type tween_type() const { return tween_type_; }
187  void set_tween_type(gfx::Tween::Type tween_type) { tween_type_ = tween_type; }
188
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; }
195
196  base::TimeDelta duration() const { return duration_; }
197
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_; }
201
202 protected:
203  // Called once each time the animation element is run before any call to
204  // OnProgress.
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;
209
210  // Actually start the animation, dispatching to another thread if needed.
211  virtual void RequestEffectiveStart(LayerAnimationDelegate* delegate);
212
213  LayerAnimationElement(const LayerAnimationElement& element);
214
215 private:
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);
220
221  bool first_frame_;
222  const AnimatableProperties properties_;
223  base::TimeTicks requested_start_time_;
224
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_;
229
230  const int animation_id_;
231  int animation_group_id_;
232
233  double last_progressed_fraction_;
234
235  base::WeakPtrFactory<LayerAnimationElement> weak_ptr_factory_;
236
237  DISALLOW_ASSIGN(LayerAnimationElement);
238};
239
240}  // namespace ui
241
242#endif  // UI_COMPOSITOR_LAYER_ANIMATION_ELEMENT_H_
243