1 // Copyright 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 CC_ANIMATION_ANIMATION_H_ 6 #define CC_ANIMATION_ANIMATION_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/time/time.h" 11 #include "cc/base/cc_export.h" 12 13 namespace cc { 14 15 class AnimationCurve; 16 17 // An Animation contains all the state required to play an AnimationCurve. 18 // Specifically, the affected property, the run state (paused, finished, etc.), 19 // loop count, last pause time, and the total time spent paused. 20 class CC_EXPORT Animation { 21 public: 22 // Animations begin in the 'WaitingForTargetAvailability' state. An Animation 23 // waiting for target availibility will run as soon as its target property 24 // is free (and all the animations animating with it are also able to run). 25 // When this time arrives, the controller will move the animation into the 26 // Starting state, and then into the Running state. Running animations may 27 // toggle between Running and Paused, and may be stopped by moving into either 28 // the Aborted or Finished states. A Finished animation was allowed to run to 29 // completion, but an Aborted animation was not. 30 enum RunState { 31 WaitingForTargetAvailability = 0, 32 WaitingForDeletion, 33 Starting, 34 Running, 35 Paused, 36 Finished, 37 Aborted, 38 // This sentinel must be last. 39 RunStateEnumSize 40 }; 41 42 enum TargetProperty { 43 Transform = 0, 44 Opacity, 45 Filter, 46 ScrollOffset, 47 BackgroundColor, 48 // This sentinel must be last. 49 TargetPropertyEnumSize 50 }; 51 52 enum Direction { Normal, Reverse, Alternate, AlternateReverse }; 53 54 enum FillMode { 55 FillModeNone, 56 FillModeForwards, 57 FillModeBackwards, 58 FillModeBoth 59 }; 60 61 static scoped_ptr<Animation> Create(scoped_ptr<AnimationCurve> curve, 62 int animation_id, 63 int group_id, 64 TargetProperty target_property); 65 66 virtual ~Animation(); 67 id()68 int id() const { return id_; } group()69 int group() const { return group_; } target_property()70 TargetProperty target_property() const { return target_property_; } 71 run_state()72 RunState run_state() const { return run_state_; } 73 void SetRunState(RunState run_state, base::TimeTicks monotonic_time); 74 75 // This is the number of times that the animation will play. If this 76 // value is zero the animation will not play. If it is negative, then 77 // the animation will loop indefinitely. iterations()78 double iterations() const { return iterations_; } set_iterations(double n)79 void set_iterations(double n) { iterations_ = n; } 80 iteration_start()81 double iteration_start() const { return iteration_start_; } set_iteration_start(double iteration_start)82 void set_iteration_start(double iteration_start) { 83 iteration_start_ = iteration_start; 84 } 85 start_time()86 base::TimeTicks start_time() const { return start_time_; } 87 set_start_time(base::TimeTicks monotonic_time)88 void set_start_time(base::TimeTicks monotonic_time) { 89 start_time_ = monotonic_time; 90 } has_set_start_time()91 bool has_set_start_time() const { return !start_time_.is_null(); } 92 time_offset()93 base::TimeDelta time_offset() const { return time_offset_; } set_time_offset(base::TimeDelta monotonic_time)94 void set_time_offset(base::TimeDelta monotonic_time) { 95 time_offset_ = monotonic_time; 96 } 97 98 void Suspend(base::TimeTicks monotonic_time); 99 void Resume(base::TimeTicks monotonic_time); 100 direction()101 Direction direction() { return direction_; } set_direction(Direction direction)102 void set_direction(Direction direction) { direction_ = direction; } 103 fill_mode()104 FillMode fill_mode() { return fill_mode_; } set_fill_mode(FillMode fill_mode)105 void set_fill_mode(FillMode fill_mode) { fill_mode_ = fill_mode; } 106 playback_rate()107 double playback_rate() { return playback_rate_; } set_playback_rate(double playback_rate)108 void set_playback_rate(double playback_rate) { 109 playback_rate_ = playback_rate; 110 } 111 112 bool IsFinishedAt(base::TimeTicks monotonic_time) const; is_finished()113 bool is_finished() const { 114 return run_state_ == Finished || 115 run_state_ == Aborted || 116 run_state_ == WaitingForDeletion; 117 } 118 119 bool InEffect(base::TimeTicks monotonic_time) const; 120 curve()121 AnimationCurve* curve() { return curve_.get(); } curve()122 const AnimationCurve* curve() const { return curve_.get(); } 123 124 // If this is true, even if the animation is running, it will not be tickable 125 // until it is given a start time. This is true for animations running on the 126 // main thread. needs_synchronized_start_time()127 bool needs_synchronized_start_time() const { 128 return needs_synchronized_start_time_; 129 } set_needs_synchronized_start_time(bool needs_synchronized_start_time)130 void set_needs_synchronized_start_time(bool needs_synchronized_start_time) { 131 needs_synchronized_start_time_ = needs_synchronized_start_time; 132 } 133 134 // This is true for animations running on the main thread when the Finished 135 // event sent by the corresponding impl animation has been received. received_finished_event()136 bool received_finished_event() const { 137 return received_finished_event_; 138 } set_received_finished_event(bool received_finished_event)139 void set_received_finished_event(bool received_finished_event) { 140 received_finished_event_ = received_finished_event; 141 } 142 143 // Takes the given absolute time, and using the start time and the number 144 // of iterations, returns the relative time in the current iteration. 145 double TrimTimeToCurrentIteration(base::TimeTicks monotonic_time) const; 146 147 scoped_ptr<Animation> CloneAndInitialize(RunState initial_run_state) const; 148 is_controlling_instance()149 bool is_controlling_instance() const { return is_controlling_instance_; } 150 151 void PushPropertiesTo(Animation* other) const; 152 set_is_impl_only(bool is_impl_only)153 void set_is_impl_only(bool is_impl_only) { is_impl_only_ = is_impl_only; } is_impl_only()154 bool is_impl_only() const { return is_impl_only_; } 155 set_affects_active_observers(bool affects_active_observers)156 void set_affects_active_observers(bool affects_active_observers) { 157 affects_active_observers_ = affects_active_observers; 158 } affects_active_observers()159 bool affects_active_observers() const { return affects_active_observers_; } 160 set_affects_pending_observers(bool affects_pending_observers)161 void set_affects_pending_observers(bool affects_pending_observers) { 162 affects_pending_observers_ = affects_pending_observers; 163 } affects_pending_observers()164 bool affects_pending_observers() const { return affects_pending_observers_; } 165 166 private: 167 Animation(scoped_ptr<AnimationCurve> curve, 168 int animation_id, 169 int group_id, 170 TargetProperty target_property); 171 172 double ConvertToActiveTime(base::TimeTicks monotonic_time) const; 173 174 scoped_ptr<AnimationCurve> curve_; 175 176 // IDs are not necessarily unique. 177 int id_; 178 179 // Animations that must be run together are called 'grouped' and have the same 180 // group id. Grouped animations are guaranteed to start at the same time and 181 // no other animations may animate any of the group's target properties until 182 // all animations in the group have finished animating. Note: an active 183 // animation's group id and target property uniquely identify that animation. 184 int group_; 185 186 TargetProperty target_property_; 187 RunState run_state_; 188 double iterations_; 189 double iteration_start_; 190 base::TimeTicks start_time_; 191 Direction direction_; 192 double playback_rate_; 193 FillMode fill_mode_; 194 195 // The time offset effectively pushes the start of the animation back in time. 196 // This is used for resuming paused animations -- an animation is added with a 197 // non-zero time offset, causing the animation to skip ahead to the desired 198 // point in time. 199 base::TimeDelta time_offset_; 200 201 bool needs_synchronized_start_time_; 202 bool received_finished_event_; 203 204 // When an animation is suspended, it behaves as if it is paused and it also 205 // ignores all run state changes until it is resumed. This is used for testing 206 // purposes. 207 bool suspended_; 208 209 // These are used in TrimTimeToCurrentIteration to account for time 210 // spent while paused. This is not included in AnimationState since it 211 // there is absolutely no need for clients of this controller to know 212 // about these values. 213 base::TimeTicks pause_time_; 214 base::TimeDelta total_paused_time_; 215 216 // Animations lead dual lives. An active animation will be conceptually owned 217 // by two controllers, one on the impl thread and one on the main. In reality, 218 // there will be two separate Animation instances for the same animation. They 219 // will have the same group id and the same target property (these two values 220 // uniquely identify an animation). The instance on the impl thread is the 221 // instance that ultimately controls the values of the animating layer and so 222 // we will refer to it as the 'controlling instance'. 223 bool is_controlling_instance_; 224 225 bool is_impl_only_; 226 227 // When pushed from a main-thread controller to a compositor-thread 228 // controller, an animation will initially only affect pending observers 229 // (corresponding to layers in the pending tree). Animations that only 230 // affect pending observers are able to reach the Starting state and tick 231 // pending observers, but cannot proceed any further and do not tick active 232 // observers. After activation, such animations affect both kinds of observers 233 // and are able to proceed past the Starting state. When the removal of 234 // an animation is pushed from a main-thread controller to a 235 // compositor-thread controller, this initially only makes the animation 236 // stop affecting pending observers. After activation, such animations no 237 // longer affect any observers, and are deleted. 238 bool affects_active_observers_; 239 bool affects_pending_observers_; 240 241 DISALLOW_COPY_AND_ASSIGN(Animation); 242 }; 243 244 } // namespace cc 245 246 #endif // CC_ANIMATION_ANIMATION_H_ 247