[email protected] | b7a1b66 | 2012-01-18 14:12:18 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 5 | #include "ui/compositor/layer_animator.h" |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 6 | |
avi | 87b8b58 | 2015-12-24 21:35:25 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 11 | #include "base/logging.h" |
ssid | 334fb87a | 2015-01-27 20:12:07 | [diff] [blame] | 12 | #include "base/trace_event/trace_event.h" |
loyso | 3338dfb | 2016-03-09 03:07:32 | [diff] [blame] | 13 | #include "cc/animation/animation_host.h" |
[email protected] | 95e4e1a0 | 2013-03-18 07:09:09 | [diff] [blame] | 14 | #include "cc/animation/animation_id_provider.h" |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 15 | #include "cc/animation/animation_player.h" |
| 16 | #include "cc/animation/animation_timeline.h" |
| 17 | #include "cc/animation/element_animations.h" |
[email protected] | de2cf8c | 2013-10-25 19:46:46 | [diff] [blame] | 18 | #include "cc/output/begin_frame_args.h" |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 19 | #include "ui/compositor/compositor.h" |
| 20 | #include "ui/compositor/layer.h" |
| 21 | #include "ui/compositor/layer_animation_delegate.h" |
| 22 | #include "ui/compositor/layer_animation_observer.h" |
| 23 | #include "ui/compositor/layer_animation_sequence.h" |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 24 | #include "ui/compositor/layer_animator_collection.h" |
[email protected] | 0ed187e | 2011-10-21 20:07:42 | [diff] [blame] | 25 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 26 | #define SAFE_INVOKE_VOID(function, running_anim, ...) \ |
| 27 | if (running_anim.is_sequence_alive()) \ |
| 28 | function(running_anim.sequence(), ##__VA_ARGS__) |
| 29 | #define SAFE_INVOKE_BOOL(function, running_anim) \ |
| 30 | ((running_anim.is_sequence_alive()) \ |
| 31 | ? function(running_anim.sequence()) \ |
| 32 | : false) |
| 33 | #define SAFE_INVOKE_PTR(function, running_anim) \ |
| 34 | ((running_anim.is_sequence_alive()) \ |
| 35 | ? function(running_anim.sequence()) \ |
| 36 | : NULL) |
| 37 | |
[email protected] | 21445e47 | 2011-10-21 20:36:32 | [diff] [blame] | 38 | namespace ui { |
[email protected] | 0ed187e | 2011-10-21 20:07:42 | [diff] [blame] | 39 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 40 | namespace { |
| 41 | |
[email protected] | 98e2ed2 | 2012-11-21 14:02:23 | [diff] [blame] | 42 | const int kDefaultTransitionDurationMs = 120; |
[email protected] | 1778cbc | 2012-09-06 04:31:19 | [diff] [blame] | 43 | |
[email protected] | 9861f175 | 2012-06-01 07:16:14 | [diff] [blame] | 44 | } // namespace |
| 45 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 46 | // LayerAnimator public -------------------------------------------------------- |
| 47 | |
| 48 | LayerAnimator::LayerAnimator(base::TimeDelta transition_duration) |
| 49 | : delegate_(NULL), |
| 50 | preemption_strategy_(IMMEDIATELY_SET_NEW_TARGET), |
[email protected] | 0d31625 | 2014-01-20 15:31:19 | [diff] [blame] | 51 | is_transition_duration_locked_(false), |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 52 | transition_duration_(transition_duration), |
[email protected] | ffb15d1 | 2013-09-15 17:29:30 | [diff] [blame] | 53 | tween_type_(gfx::Tween::LINEAR), |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 54 | is_started_(false), |
[email protected] | d064ef8 | 2012-11-13 10:26:59 | [diff] [blame] | 55 | disable_timer_for_test_(false), |
| 56 | adding_animations_(false) { |
loyso | 5311049 | 2016-03-03 00:11:51 | [diff] [blame] | 57 | animation_player_ = |
| 58 | cc::AnimationPlayer::Create(cc::AnimationIdProvider::NextPlayerId()); |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | LayerAnimator::~LayerAnimator() { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 62 | for (size_t i = 0; i < running_animations_.size(); ++i) { |
| 63 | if (running_animations_[i].is_sequence_alive()) |
| 64 | running_animations_[i].sequence()->OnAnimatorDestroyed(); |
| 65 | } |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 66 | ClearAnimationsInternal(); |
| 67 | delegate_ = NULL; |
loyso | e2fed92 | 2016-03-09 04:41:55 | [diff] [blame] | 68 | DCHECK(!animation_player_->animation_timeline()); |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 71 | // static |
| 72 | LayerAnimator* LayerAnimator::CreateDefaultAnimator() { |
| 73 | return new LayerAnimator(base::TimeDelta::FromMilliseconds(0)); |
| 74 | } |
| 75 | |
| 76 | // static |
| 77 | LayerAnimator* LayerAnimator::CreateImplicitAnimator() { |
[email protected] | 98e2ed2 | 2012-11-21 14:02:23 | [diff] [blame] | 78 | return new LayerAnimator( |
| 79 | base::TimeDelta::FromMilliseconds(kDefaultTransitionDurationMs)); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | 7713e24 | 2012-11-15 17:43:19 | [diff] [blame] | 82 | // This macro provides the implementation for the setter and getter (well, |
| 83 | // the getter of the target value) for an animated property. For example, |
| 84 | // it is used for the implementations of SetTransform and GetTargetTransform. |
| 85 | // It is worth noting that SetFoo avoids invoking the usual animation machinery |
| 86 | // if the transition duration is zero -- in this case we just set the property |
| 87 | // on the layer animation delegate immediately. |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 88 | #define ANIMATED_PROPERTY(type, property, name, member_type, member) \ |
| 89 | void LayerAnimator::Set##name(type value) { \ |
| 90 | base::TimeDelta duration = GetTransitionDuration(); \ |
thestig | 550eecbe | 2016-04-22 23:55:47 | [diff] [blame] | 91 | if (duration.is_zero() && delegate() && \ |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 92 | (preemption_strategy_ != ENQUEUE_NEW_ANIMATION)) { \ |
| 93 | StopAnimatingProperty(LayerAnimationElement::property); \ |
| 94 | delegate()->Set##name##FromAnimation(value); \ |
| 95 | return; \ |
| 96 | } \ |
| 97 | std::unique_ptr<LayerAnimationElement> element( \ |
| 98 | LayerAnimationElement::Create##name##Element(value, duration)); \ |
| 99 | element->set_tween_type(tween_type_); \ |
| 100 | StartAnimation(new LayerAnimationSequence(element.release())); \ |
| 101 | } \ |
| 102 | \ |
| 103 | member_type LayerAnimator::GetTarget##name() const { \ |
| 104 | LayerAnimationElement::TargetValue target(delegate()); \ |
| 105 | GetTargetValue(&target); \ |
| 106 | return target.member; \ |
| 107 | } |
[email protected] | fe7074c6 | 2011-10-28 15:22:54 | [diff] [blame] | 108 | |
[email protected] | 7713e24 | 2012-11-15 17:43:19 | [diff] [blame] | 109 | ANIMATED_PROPERTY( |
| 110 | const gfx::Transform&, TRANSFORM, Transform, gfx::Transform, transform); |
| 111 | ANIMATED_PROPERTY(const gfx::Rect&, BOUNDS, Bounds, gfx::Rect, bounds); |
| 112 | ANIMATED_PROPERTY(float, OPACITY, Opacity, float, opacity); |
| 113 | ANIMATED_PROPERTY(bool, VISIBILITY, Visibility, bool, visibility); |
| 114 | ANIMATED_PROPERTY(float, BRIGHTNESS, Brightness, float, brightness); |
| 115 | ANIMATED_PROPERTY(float, GRAYSCALE, Grayscale, float, grayscale); |
| 116 | ANIMATED_PROPERTY(SkColor, COLOR, Color, SkColor, color); |
[email protected] | e81480f1f | 2012-10-11 23:06:45 | [diff] [blame] | 117 | |
[email protected] | 0d31625 | 2014-01-20 15:31:19 | [diff] [blame] | 118 | base::TimeDelta LayerAnimator::GetTransitionDuration() const { |
| 119 | return transition_duration_; |
| 120 | } |
| 121 | |
[email protected] | e876c271 | 2011-11-02 16:42:45 | [diff] [blame] | 122 | void LayerAnimator::SetDelegate(LayerAnimationDelegate* delegate) { |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 123 | if (delegate_ && is_started_) { |
| 124 | LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
| 125 | if (collection) |
| 126 | collection->StopAnimator(this); |
| 127 | } |
loyso | 1fbd9f9 | 2015-12-17 07:43:13 | [diff] [blame] | 128 | SwitchToLayer(delegate ? delegate->GetCcLayer() : nullptr); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 129 | delegate_ = delegate; |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 130 | if (delegate_ && is_started_) { |
| 131 | LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
| 132 | if (collection) |
| 133 | collection->StartAnimator(this); |
| 134 | } |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 135 | } |
| 136 | |
loyso | 1fbd9f9 | 2015-12-17 07:43:13 | [diff] [blame] | 137 | void LayerAnimator::SwitchToLayer(scoped_refptr<cc::Layer> new_layer) { |
loyso | 47a0f19 | 2016-04-22 00:30:09 | [diff] [blame] | 138 | // Release ElementAnimations state for old layer. |
| 139 | element_animations_state_ = nullptr; |
loyso | 3338dfb | 2016-03-09 03:07:32 | [diff] [blame] | 140 | |
loyso | e2fed92 | 2016-03-09 04:41:55 | [diff] [blame] | 141 | if (delegate_) |
| 142 | DetachLayerFromAnimationPlayer(); |
| 143 | if (new_layer) |
| 144 | AttachLayerToAnimationPlayer(new_layer->id()); |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void LayerAnimator::SetCompositor(Compositor* compositor) { |
| 148 | DCHECK(compositor); |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 149 | |
loyso | e2fed92 | 2016-03-09 04:41:55 | [diff] [blame] | 150 | cc::AnimationTimeline* timeline = compositor->GetAnimationTimeline(); |
| 151 | DCHECK(timeline); |
loyso | 3338dfb | 2016-03-09 03:07:32 | [diff] [blame] | 152 | |
loyso | e2fed92 | 2016-03-09 04:41:55 | [diff] [blame] | 153 | DCHECK(delegate_->GetCcLayer()); |
loyso | 3338dfb | 2016-03-09 03:07:32 | [diff] [blame] | 154 | |
loyso | 47a0f19 | 2016-04-22 00:30:09 | [diff] [blame] | 155 | // Register ElementAnimations so it will be picked up by |
| 156 | // AnimationHost::RegisterPlayerForLayer via |
| 157 | // AnimationHost::GetElementAnimationsForLayerId. |
| 158 | if (element_animations_state_) { |
vollick | ef2ae92 | 2016-06-29 17:54:27 | [diff] [blame] | 159 | DCHECK_EQ(element_animations_state_->element_id().primaryId, |
loyso | e2fed92 | 2016-03-09 04:41:55 | [diff] [blame] | 160 | delegate_->GetCcLayer()->id()); |
loyso | 47a0f19 | 2016-04-22 00:30:09 | [diff] [blame] | 161 | timeline->animation_host()->RegisterElementAnimations( |
| 162 | element_animations_state_.get()); |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 163 | } |
loyso | e2fed92 | 2016-03-09 04:41:55 | [diff] [blame] | 164 | |
| 165 | timeline->AttachPlayer(animation_player_); |
| 166 | |
| 167 | AttachLayerToAnimationPlayer(delegate_->GetCcLayer()->id()); |
| 168 | |
loyso | 47a0f19 | 2016-04-22 00:30:09 | [diff] [blame] | 169 | // Release ElementAnimations state. |
| 170 | element_animations_state_ = nullptr; |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void LayerAnimator::ResetCompositor(Compositor* compositor) { |
| 174 | DCHECK(compositor); |
loyso | 3338dfb | 2016-03-09 03:07:32 | [diff] [blame] | 175 | |
loyso | de0a002 | 2016-04-15 02:28:38 | [diff] [blame] | 176 | cc::AnimationTimeline* timeline = compositor->GetAnimationTimeline(); |
| 177 | DCHECK(timeline); |
| 178 | |
vollick | ef2ae92 | 2016-06-29 17:54:27 | [diff] [blame] | 179 | cc::ElementId element_id(animation_player_->element_id()); |
loyso | de0a002 | 2016-04-15 02:28:38 | [diff] [blame] | 180 | |
loyso | 47a0f19 | 2016-04-22 00:30:09 | [diff] [blame] | 181 | // Store a reference to ElementAnimations (if any) |
| 182 | // so it may be picked up in LayerAnimator::SetCompositor. |
vollick | ef2ae92 | 2016-06-29 17:54:27 | [diff] [blame] | 183 | if (element_id) { |
loyso | 47a0f19 | 2016-04-22 00:30:09 | [diff] [blame] | 184 | element_animations_state_ = |
vollick | ef2ae92 | 2016-06-29 17:54:27 | [diff] [blame] | 185 | timeline->animation_host()->GetElementAnimationsForElementId( |
| 186 | element_id); |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 187 | } |
loyso | e2fed92 | 2016-03-09 04:41:55 | [diff] [blame] | 188 | |
| 189 | DetachLayerFromAnimationPlayer(); |
| 190 | |
loyso | e2fed92 | 2016-03-09 04:41:55 | [diff] [blame] | 191 | timeline->DetachPlayer(animation_player_); |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void LayerAnimator::AttachLayerToAnimationPlayer(int layer_id) { |
vollick | ef2ae92 | 2016-06-29 17:54:27 | [diff] [blame] | 195 | // For ui, layer and element ids are equivalent. |
| 196 | cc::ElementId element_id(layer_id, 0); |
loyso | 7337a62 | 2016-04-26 23:54:30 | [diff] [blame] | 197 | if (!animation_player_->element_id()) |
vollick | ef2ae92 | 2016-06-29 17:54:27 | [diff] [blame] | 198 | animation_player_->AttachElement(element_id); |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 199 | else |
vollick | ef2ae92 | 2016-06-29 17:54:27 | [diff] [blame] | 200 | DCHECK_EQ(animation_player_->element_id(), element_id); |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 201 | |
loyso | 9f6a55e6 | 2016-04-27 23:58:25 | [diff] [blame] | 202 | animation_player_->set_animation_delegate(this); |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void LayerAnimator::DetachLayerFromAnimationPlayer() { |
loyso | 9f6a55e6 | 2016-04-27 23:58:25 | [diff] [blame] | 206 | animation_player_->set_animation_delegate(nullptr); |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 207 | |
loyso | 7337a62 | 2016-04-26 23:54:30 | [diff] [blame] | 208 | if (animation_player_->element_id()) |
loyso | 9f6a55e6 | 2016-04-27 23:58:25 | [diff] [blame] | 209 | animation_player_->DetachElement(); |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 210 | } |
| 211 | |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 212 | void LayerAnimator::AddThreadedAnimation( |
| 213 | std::unique_ptr<cc::Animation> animation) { |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 214 | animation_player_->AddAnimation(std::move(animation)); |
| 215 | } |
| 216 | |
| 217 | void LayerAnimator::RemoveThreadedAnimation(int animation_id) { |
loyso | 84122900 | 2015-12-21 10:03:24 | [diff] [blame] | 218 | animation_player_->RemoveAnimation(animation_id); |
loyso | 1fbd9f9 | 2015-12-17 07:43:13 | [diff] [blame] | 219 | } |
| 220 | |
loyso | 1fe980ff | 2016-01-18 23:58:15 | [diff] [blame] | 221 | bool LayerAnimator::HasPendingThreadedAnimationsForTesting() const { |
loyso | 1fe980ff | 2016-01-18 23:58:15 | [diff] [blame] | 222 | return animation_player_->has_pending_animations_for_testing(); |
| 223 | } |
| 224 | |
loyso | d412744 | 2016-02-03 02:29:40 | [diff] [blame] | 225 | cc::AnimationPlayer* LayerAnimator::GetAnimationPlayerForTesting() const { |
| 226 | return animation_player_.get(); |
| 227 | } |
| 228 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 229 | void LayerAnimator::StartAnimation(LayerAnimationSequence* animation) { |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 230 | scoped_refptr<LayerAnimator> retain(this); |
[email protected] | e876c271 | 2011-11-02 16:42:45 | [diff] [blame] | 231 | OnScheduled(animation); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 232 | if (!StartSequenceImmediately(animation)) { |
| 233 | // Attempt to preempt a running animation. |
| 234 | switch (preemption_strategy_) { |
| 235 | case IMMEDIATELY_SET_NEW_TARGET: |
| 236 | ImmediatelySetNewTarget(animation); |
| 237 | break; |
| 238 | case IMMEDIATELY_ANIMATE_TO_NEW_TARGET: |
| 239 | ImmediatelyAnimateToNewTarget(animation); |
| 240 | break; |
| 241 | case ENQUEUE_NEW_ANIMATION: |
| 242 | EnqueueNewAnimation(animation); |
| 243 | break; |
| 244 | case REPLACE_QUEUED_ANIMATIONS: |
| 245 | ReplaceQueuedAnimations(animation); |
| 246 | break; |
| 247 | case BLEND_WITH_CURRENT_ANIMATION: { |
| 248 | // TODO(vollick) Add support for blended sequences and use them here. |
| 249 | NOTIMPLEMENTED(); |
| 250 | break; |
| 251 | } |
[email protected] | 7fca53d4 | 2011-09-29 15:38:12 | [diff] [blame] | 252 | } |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 253 | } |
| 254 | FinishAnyAnimationWithZeroDuration(); |
[email protected] | fe7074c6 | 2011-10-28 15:22:54 | [diff] [blame] | 255 | UpdateAnimationState(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void LayerAnimator::ScheduleAnimation(LayerAnimationSequence* animation) { |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 259 | scoped_refptr<LayerAnimator> retain(this); |
[email protected] | e876c271 | 2011-11-02 16:42:45 | [diff] [blame] | 260 | OnScheduled(animation); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 261 | if (is_animating()) { |
| 262 | animation_queue_.push_back(make_linked_ptr(animation)); |
| 263 | ProcessQueue(); |
| 264 | } else { |
| 265 | StartSequenceImmediately(animation); |
[email protected] | 7fca53d4 | 2011-09-29 15:38:12 | [diff] [blame] | 266 | } |
[email protected] | fe7074c6 | 2011-10-28 15:22:54 | [diff] [blame] | 267 | UpdateAnimationState(); |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 268 | } |
| 269 | |
[email protected] | d064ef8 | 2012-11-13 10:26:59 | [diff] [blame] | 270 | void LayerAnimator::StartTogether( |
| 271 | const std::vector<LayerAnimationSequence*>& animations) { |
| 272 | scoped_refptr<LayerAnimator> retain(this); |
| 273 | if (preemption_strategy_ == IMMEDIATELY_SET_NEW_TARGET) { |
| 274 | std::vector<LayerAnimationSequence*>::const_iterator iter; |
| 275 | for (iter = animations.begin(); iter != animations.end(); ++iter) { |
| 276 | StartAnimation(*iter); |
| 277 | } |
| 278 | return; |
| 279 | } |
[email protected] | d764fd3d | 2012-11-15 20:07:51 | [diff] [blame] | 280 | |
[email protected] | d064ef8 | 2012-11-13 10:26:59 | [diff] [blame] | 281 | adding_animations_ = true; |
[email protected] | d764fd3d | 2012-11-15 20:07:51 | [diff] [blame] | 282 | if (!is_animating()) { |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 283 | LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
| 284 | if (collection && collection->HasActiveAnimators()) |
| 285 | last_step_time_ = collection->last_tick_time(); |
[email protected] | d764fd3d | 2012-11-15 20:07:51 | [diff] [blame] | 286 | else |
abhishek.ka | 7215854d | 2015-05-26 06:13:17 | [diff] [blame] | 287 | last_step_time_ = base::TimeTicks::Now(); |
[email protected] | d764fd3d | 2012-11-15 20:07:51 | [diff] [blame] | 288 | } |
[email protected] | d064ef8 | 2012-11-13 10:26:59 | [diff] [blame] | 289 | |
| 290 | // Collect all the affected properties. |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 291 | LayerAnimationElement::AnimatableProperties animated_properties = |
| 292 | LayerAnimationElement::UNKNOWN; |
| 293 | |
[email protected] | d064ef8 | 2012-11-13 10:26:59 | [diff] [blame] | 294 | std::vector<LayerAnimationSequence*>::const_iterator iter; |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 295 | for (iter = animations.begin(); iter != animations.end(); ++iter) |
| 296 | animated_properties |= (*iter)->properties(); |
[email protected] | d064ef8 | 2012-11-13 10:26:59 | [diff] [blame] | 297 | |
| 298 | // Starting a zero duration pause that affects all the animated properties |
| 299 | // will prevent any of the sequences from animating until there are no |
| 300 | // running animations that affect any of these properties, as well as |
| 301 | // handle preemption strategy. |
| 302 | StartAnimation(new LayerAnimationSequence( |
| 303 | LayerAnimationElement::CreatePauseElement(animated_properties, |
| 304 | base::TimeDelta()))); |
| 305 | |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 306 | bool wait_for_group_start = false; |
| 307 | for (iter = animations.begin(); iter != animations.end(); ++iter) |
| 308 | wait_for_group_start |= (*iter)->IsFirstElementThreaded(); |
| 309 | |
| 310 | int group_id = cc::AnimationIdProvider::NextGroupId(); |
| 311 | |
[email protected] | d064ef8 | 2012-11-13 10:26:59 | [diff] [blame] | 312 | // These animations (provided they don't animate any common properties) will |
| 313 | // now animate together if trivially scheduled. |
| 314 | for (iter = animations.begin(); iter != animations.end(); ++iter) { |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 315 | (*iter)->set_animation_group_id(group_id); |
| 316 | (*iter)->set_waiting_for_group_start(wait_for_group_start); |
[email protected] | d064ef8 | 2012-11-13 10:26:59 | [diff] [blame] | 317 | ScheduleAnimation(*iter); |
| 318 | } |
| 319 | |
| 320 | adding_animations_ = false; |
| 321 | UpdateAnimationState(); |
| 322 | } |
| 323 | |
| 324 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 325 | void LayerAnimator::ScheduleTogether( |
| 326 | const std::vector<LayerAnimationSequence*>& animations) { |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 327 | scoped_refptr<LayerAnimator> retain(this); |
| 328 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 329 | // Collect all the affected properties. |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 330 | LayerAnimationElement::AnimatableProperties animated_properties = |
| 331 | LayerAnimationElement::UNKNOWN; |
| 332 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 333 | std::vector<LayerAnimationSequence*>::const_iterator iter; |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 334 | for (iter = animations.begin(); iter != animations.end(); ++iter) |
| 335 | animated_properties |= (*iter)->properties(); |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 336 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 337 | // Scheduling a zero duration pause that affects all the animated properties |
| 338 | // will prevent any of the sequences from animating until there are no |
| 339 | // running animations that affect any of these properties. |
[email protected] | e4cbcc77 | 2012-03-07 18:59:31 | [diff] [blame] | 340 | ScheduleAnimation(new LayerAnimationSequence( |
| 341 | LayerAnimationElement::CreatePauseElement(animated_properties, |
| 342 | base::TimeDelta()))); |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 343 | |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 344 | bool wait_for_group_start = false; |
| 345 | for (iter = animations.begin(); iter != animations.end(); ++iter) |
| 346 | wait_for_group_start |= (*iter)->IsFirstElementThreaded(); |
| 347 | |
| 348 | int group_id = cc::AnimationIdProvider::NextGroupId(); |
| 349 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 350 | // These animations (provided they don't animate any common properties) will |
| 351 | // now animate together if trivially scheduled. |
| 352 | for (iter = animations.begin(); iter != animations.end(); ++iter) { |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 353 | (*iter)->set_animation_group_id(group_id); |
| 354 | (*iter)->set_waiting_for_group_start(wait_for_group_start); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 355 | ScheduleAnimation(*iter); |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 356 | } |
[email protected] | fe7074c6 | 2011-10-28 15:22:54 | [diff] [blame] | 357 | |
| 358 | UpdateAnimationState(); |
| 359 | } |
| 360 | |
[email protected] | 2a88c70 | 2012-09-04 23:15:02 | [diff] [blame] | 361 | void LayerAnimator::SchedulePauseForProperties( |
| 362 | base::TimeDelta duration, |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 363 | LayerAnimationElement::AnimatableProperties properties_to_pause) { |
[email protected] | 2a88c70 | 2012-09-04 23:15:02 | [diff] [blame] | 364 | ScheduleAnimation(new ui::LayerAnimationSequence( |
| 365 | ui::LayerAnimationElement::CreatePauseElement( |
| 366 | properties_to_pause, duration))); |
| 367 | } |
| 368 | |
[email protected] | e876c271 | 2011-11-02 16:42:45 | [diff] [blame] | 369 | bool LayerAnimator::IsAnimatingProperty( |
| 370 | LayerAnimationElement::AnimatableProperty property) const { |
| 371 | for (AnimationQueue::const_iterator queue_iter = animation_queue_.begin(); |
| 372 | queue_iter != animation_queue_.end(); ++queue_iter) { |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 373 | if ((*queue_iter)->properties() & property) |
[email protected] | e876c271 | 2011-11-02 16:42:45 | [diff] [blame] | 374 | return true; |
[email protected] | e876c271 | 2011-11-02 16:42:45 | [diff] [blame] | 375 | } |
| 376 | return false; |
[email protected] | 7fca53d4 | 2011-09-29 15:38:12 | [diff] [blame] | 377 | } |
| 378 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 379 | void LayerAnimator::StopAnimatingProperty( |
| 380 | LayerAnimationElement::AnimatableProperty property) { |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 381 | scoped_refptr<LayerAnimator> retain(this); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 382 | while (true) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 383 | // GetRunningAnimation purges deleted animations before searching, so we are |
| 384 | // guaranteed to find a live animation if any is returned at all. |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 385 | RunningAnimation* running = GetRunningAnimation(property); |
| 386 | if (!running) |
| 387 | break; |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 388 | // As was mentioned above, this sequence must be alive. |
| 389 | DCHECK(running->is_sequence_alive()); |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 390 | FinishAnimation(running->sequence(), false); |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 391 | } |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 392 | } |
| 393 | |
[email protected] | e876c271 | 2011-11-02 16:42:45 | [diff] [blame] | 394 | void LayerAnimator::AddObserver(LayerAnimationObserver* observer) { |
| 395 | if (!observers_.HasObserver(observer)) |
| 396 | observers_.AddObserver(observer); |
| 397 | } |
| 398 | |
| 399 | void LayerAnimator::RemoveObserver(LayerAnimationObserver* observer) { |
| 400 | observers_.RemoveObserver(observer); |
[email protected] | 5cc8538d | 2011-11-07 15:24:54 | [diff] [blame] | 401 | // Remove the observer from all sequences as well. |
| 402 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
| 403 | queue_iter != animation_queue_.end(); ++queue_iter) { |
| 404 | (*queue_iter)->RemoveObserver(observer); |
| 405 | } |
[email protected] | e876c271 | 2011-11-02 16:42:45 | [diff] [blame] | 406 | } |
| 407 | |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 408 | void LayerAnimator::OnThreadedAnimationStarted( |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 409 | base::TimeTicks monotonic_time, |
| 410 | cc::TargetProperty::Type target_property, |
| 411 | int group_id) { |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 412 | LayerAnimationElement::AnimatableProperty property = |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 413 | LayerAnimationElement::ToAnimatableProperty(target_property); |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 414 | |
| 415 | RunningAnimation* running = GetRunningAnimation(property); |
| 416 | if (!running) |
| 417 | return; |
| 418 | DCHECK(running->is_sequence_alive()); |
| 419 | |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 420 | if (running->sequence()->animation_group_id() != group_id) |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 421 | return; |
| 422 | |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 423 | running->sequence()->OnThreadedAnimationStarted(monotonic_time, |
| 424 | target_property, group_id); |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 425 | if (!running->sequence()->waiting_for_group_start()) |
| 426 | return; |
| 427 | |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 428 | base::TimeTicks start_time = monotonic_time; |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 429 | |
| 430 | running->sequence()->set_waiting_for_group_start(false); |
| 431 | |
| 432 | // The call to GetRunningAnimation made above already purged deleted |
| 433 | // animations, so we are guaranteed that all the animations we iterate |
| 434 | // over now are alive. |
| 435 | for (RunningAnimations::iterator iter = running_animations_.begin(); |
| 436 | iter != running_animations_.end(); ++iter) { |
| 437 | // Ensure that each sequence is only Started once, regardless of the |
| 438 | // number of sequences in the group that have threaded first elements. |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 439 | if (((*iter).sequence()->animation_group_id() == group_id) && |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 440 | !(*iter).sequence()->IsFirstElementThreaded() && |
| 441 | (*iter).sequence()->waiting_for_group_start()) { |
| 442 | (*iter).sequence()->set_start_time(start_time); |
| 443 | (*iter).sequence()->set_waiting_for_group_start(false); |
| 444 | (*iter).sequence()->Start(delegate()); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 449 | void LayerAnimator::AddToCollection(LayerAnimatorCollection* collection) { |
| 450 | if (is_animating() && !is_started_) { |
| 451 | collection->StartAnimator(this); |
| 452 | is_started_ = true; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void LayerAnimator::RemoveFromCollection(LayerAnimatorCollection* collection) { |
bruthig | 33b1edab | 2016-03-02 02:22:35 | [diff] [blame] | 457 | if (is_started_) { |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 458 | collection->StopAnimator(this); |
| 459 | is_started_ = false; |
| 460 | } |
| 461 | } |
| 462 | |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 463 | // LayerAnimator protected ----------------------------------------------------- |
| 464 | |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 465 | void LayerAnimator::ProgressAnimation(LayerAnimationSequence* sequence, |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 466 | base::TimeTicks now) { |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 467 | if (!delegate() || sequence->waiting_for_group_start()) |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 468 | return; |
| 469 | |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 470 | sequence->Progress(now, delegate()); |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 471 | } |
| 472 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 473 | void LayerAnimator::ProgressAnimationToEnd(LayerAnimationSequence* sequence) { |
[email protected] | 4a386e4 | 2012-12-05 01:19:30 | [diff] [blame] | 474 | if (!delegate()) |
| 475 | return; |
| 476 | |
| 477 | sequence->ProgressToEnd(delegate()); |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 478 | } |
| 479 | |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 480 | bool LayerAnimator::HasAnimation(LayerAnimationSequence* sequence) const { |
| 481 | for (AnimationQueue::const_iterator queue_iter = animation_queue_.begin(); |
| 482 | queue_iter != animation_queue_.end(); ++queue_iter) { |
| 483 | if ((*queue_iter).get() == sequence) |
| 484 | return true; |
| 485 | } |
| 486 | return false; |
| 487 | } |
| 488 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 489 | // LayerAnimator private ------------------------------------------------------- |
| 490 | |
| 491 | void LayerAnimator::Step(base::TimeTicks now) { |
[email protected] | e4cbcc77 | 2012-03-07 18:59:31 | [diff] [blame] | 492 | TRACE_EVENT0("ui", "LayerAnimator::Step"); |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 493 | scoped_refptr<LayerAnimator> retain(this); |
[email protected] | e4cbcc77 | 2012-03-07 18:59:31 | [diff] [blame] | 494 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 495 | last_step_time_ = now; |
[email protected] | 1778cbc | 2012-09-06 04:31:19 | [diff] [blame] | 496 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 497 | PurgeDeletedAnimations(); |
| 498 | |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 499 | // We need to make a copy of the running animations because progressing them |
| 500 | // and finishing them may indirectly affect the collection of running |
| 501 | // animations. |
| 502 | RunningAnimations running_animations_copy = running_animations_; |
| 503 | for (size_t i = 0; i < running_animations_copy.size(); ++i) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 504 | if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i])) |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 505 | continue; |
| 506 | |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 507 | if (running_animations_copy[i].sequence()->IsFinished(now)) { |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 508 | SAFE_INVOKE_VOID(FinishAnimation, running_animations_copy[i], false); |
| 509 | } else { |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 510 | SAFE_INVOKE_VOID(ProgressAnimation, running_animations_copy[i], now); |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 511 | } |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 512 | } |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 513 | } |
| 514 | |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 515 | void LayerAnimator::StopAnimatingInternal(bool abort) { |
| 516 | scoped_refptr<LayerAnimator> retain(this); |
ljagielski | 2057098 | 2015-01-07 20:32:55 | [diff] [blame] | 517 | while (is_animating() && delegate()) { |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 518 | // We're going to attempt to finish the first running animation. Let's |
| 519 | // ensure that it's valid. |
| 520 | PurgeDeletedAnimations(); |
| 521 | |
| 522 | // If we've purged all running animations, attempt to start one up. |
| 523 | if (running_animations_.empty()) |
| 524 | ProcessQueue(); |
| 525 | |
| 526 | DCHECK(!running_animations_.empty()); |
| 527 | |
| 528 | // Still no luck, let's just bail and clear all animations. |
| 529 | if (running_animations_.empty()) { |
| 530 | ClearAnimationsInternal(); |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | SAFE_INVOKE_VOID(FinishAnimation, running_animations_[0], abort); |
| 535 | } |
| 536 | } |
| 537 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 538 | void LayerAnimator::UpdateAnimationState() { |
| 539 | if (disable_timer_for_test_) |
[email protected] | 7fca53d4 | 2011-09-29 15:38:12 | [diff] [blame] | 540 | return; |
| 541 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 542 | const bool should_start = is_animating(); |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 543 | LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
| 544 | if (collection) { |
| 545 | if (should_start && !is_started_) |
| 546 | collection->StartAnimator(this); |
| 547 | else if (!should_start && is_started_) |
| 548 | collection->StopAnimator(this); |
| 549 | is_started_ = should_start; |
| 550 | } else { |
| 551 | is_started_ = false; |
| 552 | } |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 553 | } |
| 554 | |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 555 | LayerAnimationSequence* LayerAnimator::RemoveAnimation( |
| 556 | LayerAnimationSequence* sequence) { |
| 557 | linked_ptr<LayerAnimationSequence> to_return; |
| 558 | |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 559 | bool is_running = false; |
| 560 | |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 561 | // First remove from running animations |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 562 | for (RunningAnimations::iterator iter = running_animations_.begin(); |
| 563 | iter != running_animations_.end(); ++iter) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 564 | if ((*iter).sequence() == sequence) { |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 565 | running_animations_.erase(iter); |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 566 | is_running = true; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 567 | break; |
[email protected] | 21445e47 | 2011-10-21 20:36:32 | [diff] [blame] | 568 | } |
| 569 | } |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 570 | |
| 571 | // Then remove from the queue |
| 572 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
| 573 | queue_iter != animation_queue_.end(); ++queue_iter) { |
| 574 | if ((*queue_iter) == sequence) { |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 575 | to_return = *queue_iter; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 576 | animation_queue_.erase(queue_iter); |
| 577 | break; |
| 578 | } |
| 579 | } |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 580 | |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 581 | if (!to_return.get() || |
| 582 | !to_return->waiting_for_group_start() || |
| 583 | !to_return->IsFirstElementThreaded()) |
| 584 | return to_return.release(); |
| 585 | |
| 586 | // The removed sequence may have been responsible for making other sequences |
| 587 | // wait for a group start. If no other sequences in the group have a |
| 588 | // threaded first element, the group no longer needs the additional wait. |
| 589 | bool is_wait_still_needed = false; |
| 590 | int group_id = to_return->animation_group_id(); |
| 591 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
| 592 | queue_iter != animation_queue_.end(); ++queue_iter) { |
| 593 | if (((*queue_iter)->animation_group_id() == group_id) && |
| 594 | (*queue_iter)->IsFirstElementThreaded()) { |
| 595 | is_wait_still_needed = true; |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | if (is_wait_still_needed) |
| 601 | return to_return.release(); |
| 602 | |
| 603 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
| 604 | queue_iter != animation_queue_.end(); ++queue_iter) { |
| 605 | if ((*queue_iter)->animation_group_id() == group_id && |
| 606 | (*queue_iter)->waiting_for_group_start()) { |
| 607 | (*queue_iter)->set_waiting_for_group_start(false); |
| 608 | if (is_running) { |
| 609 | (*queue_iter)->set_start_time(last_step_time_); |
| 610 | (*queue_iter)->Start(delegate()); |
| 611 | } |
| 612 | } |
| 613 | } |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 614 | return to_return.release(); |
[email protected] | 0ed187e | 2011-10-21 20:07:42 | [diff] [blame] | 615 | } |
| 616 | |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 617 | void LayerAnimator::FinishAnimation( |
| 618 | LayerAnimationSequence* sequence, bool abort) { |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 619 | scoped_refptr<LayerAnimator> retain(this); |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 620 | std::unique_ptr<LayerAnimationSequence> removed(RemoveAnimation(sequence)); |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 621 | if (abort) |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 622 | sequence->Abort(delegate()); |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 623 | else |
| 624 | ProgressAnimationToEnd(sequence); |
ljagielski | 2057098 | 2015-01-07 20:32:55 | [diff] [blame] | 625 | if (!delegate()) |
| 626 | return; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 627 | ProcessQueue(); |
| 628 | UpdateAnimationState(); |
[email protected] | 21445e47 | 2011-10-21 20:36:32 | [diff] [blame] | 629 | } |
[email protected] | 0ed187e | 2011-10-21 20:07:42 | [diff] [blame] | 630 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 631 | void LayerAnimator::FinishAnyAnimationWithZeroDuration() { |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 632 | scoped_refptr<LayerAnimator> retain(this); |
[email protected] | 2ddfe43 | 2011-11-07 19:26:30 | [diff] [blame] | 633 | // Special case: if we've started a 0 duration animation, just finish it now |
| 634 | // and get rid of it. We need to make a copy because Progress may indirectly |
| 635 | // cause new animations to start running. |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 636 | RunningAnimations running_animations_copy = running_animations_; |
| 637 | for (size_t i = 0; i < running_animations_copy.size(); ++i) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 638 | if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i])) |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 639 | continue; |
| 640 | |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 641 | if (running_animations_copy[i].sequence()->IsFinished( |
| 642 | running_animations_copy[i].sequence()->start_time())) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 643 | SAFE_INVOKE_VOID(ProgressAnimationToEnd, running_animations_copy[i]); |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 644 | std::unique_ptr<LayerAnimationSequence> removed( |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 645 | SAFE_INVOKE_PTR(RemoveAnimation, running_animations_copy[i])); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | ProcessQueue(); |
| 649 | UpdateAnimationState(); |
[email protected] | 21445e47 | 2011-10-21 20:36:32 | [diff] [blame] | 650 | } |
[email protected] | 0ed187e | 2011-10-21 20:07:42 | [diff] [blame] | 651 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 652 | void LayerAnimator::ClearAnimations() { |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 653 | scoped_refptr<LayerAnimator> retain(this); |
| 654 | ClearAnimationsInternal(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | LayerAnimator::RunningAnimation* LayerAnimator::GetRunningAnimation( |
| 658 | LayerAnimationElement::AnimatableProperty property) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 659 | PurgeDeletedAnimations(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 660 | for (RunningAnimations::iterator iter = running_animations_.begin(); |
| 661 | iter != running_animations_.end(); ++iter) { |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 662 | if ((*iter).sequence()->properties() & property) |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 663 | return &(*iter); |
| 664 | } |
| 665 | return NULL; |
| 666 | } |
| 667 | |
| 668 | void LayerAnimator::AddToQueueIfNotPresent(LayerAnimationSequence* animation) { |
| 669 | // If we don't have the animation in the queue yet, add it. |
| 670 | bool found_sequence = false; |
| 671 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
| 672 | queue_iter != animation_queue_.end(); ++queue_iter) { |
| 673 | if ((*queue_iter) == animation) { |
| 674 | found_sequence = true; |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | if (!found_sequence) |
| 680 | animation_queue_.push_front(make_linked_ptr(animation)); |
| 681 | } |
| 682 | |
| 683 | void LayerAnimator::RemoveAllAnimationsWithACommonProperty( |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 684 | LayerAnimationSequence* sequence, bool abort) { |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 685 | // For all the running animations, if they animate the same property, |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 686 | // progress them to the end and remove them. Note, Aborting or Progressing |
| 687 | // animations may affect the collection of running animations, so we need to |
| 688 | // operate on a copy. |
| 689 | RunningAnimations running_animations_copy = running_animations_; |
| 690 | for (size_t i = 0; i < running_animations_copy.size(); ++i) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 691 | if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i])) |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 692 | continue; |
| 693 | |
[email protected] | 712f4b64 | 2013-03-14 07:09:15 | [diff] [blame] | 694 | if (running_animations_copy[i].sequence()->HasConflictingProperty( |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 695 | sequence->properties())) { |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 696 | std::unique_ptr<LayerAnimationSequence> removed( |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 697 | SAFE_INVOKE_PTR(RemoveAnimation, running_animations_copy[i])); |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 698 | if (abort) |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 699 | running_animations_copy[i].sequence()->Abort(delegate()); |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 700 | else |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 701 | SAFE_INVOKE_VOID(ProgressAnimationToEnd, running_animations_copy[i]); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 705 | // Same for the queued animations that haven't been started. Again, we'll |
| 706 | // need to operate on a copy. |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 707 | std::vector<base::WeakPtr<LayerAnimationSequence> > sequences; |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 708 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
| 709 | queue_iter != animation_queue_.end(); ++queue_iter) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 710 | sequences.push_back((*queue_iter)->AsWeakPtr()); |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 711 | |
| 712 | for (size_t i = 0; i < sequences.size(); ++i) { |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 713 | if (!sequences[i].get() || !HasAnimation(sequences[i].get())) |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 714 | continue; |
| 715 | |
[email protected] | 712f4b64 | 2013-03-14 07:09:15 | [diff] [blame] | 716 | if (sequences[i]->HasConflictingProperty(sequence->properties())) { |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 717 | std::unique_ptr<LayerAnimationSequence> removed( |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 718 | RemoveAnimation(sequences[i].get())); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 719 | if (abort) |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 720 | sequences[i]->Abort(delegate()); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 721 | else |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 722 | ProgressAnimationToEnd(sequences[i].get()); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | void LayerAnimator::ImmediatelySetNewTarget(LayerAnimationSequence* sequence) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 728 | // Need to detect if our sequence gets destroyed. |
| 729 | base::WeakPtr<LayerAnimationSequence> weak_sequence_ptr = |
| 730 | sequence->AsWeakPtr(); |
| 731 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 732 | const bool abort = false; |
| 733 | RemoveAllAnimationsWithACommonProperty(sequence, abort); |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 734 | if (!weak_sequence_ptr.get()) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 735 | return; |
| 736 | |
[email protected] | d3ba37ab | 2012-02-09 19:53:13 | [diff] [blame] | 737 | LayerAnimationSequence* removed = RemoveAnimation(sequence); |
| 738 | DCHECK(removed == NULL || removed == sequence); |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 739 | if (!weak_sequence_ptr.get()) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 740 | return; |
| 741 | |
[email protected] | 4a386e4 | 2012-12-05 01:19:30 | [diff] [blame] | 742 | ProgressAnimationToEnd(sequence); |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 743 | if (!weak_sequence_ptr.get()) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 744 | return; |
| 745 | |
| 746 | delete sequence; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | void LayerAnimator::ImmediatelyAnimateToNewTarget( |
| 750 | LayerAnimationSequence* sequence) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 751 | // Need to detect if our sequence gets destroyed. |
| 752 | base::WeakPtr<LayerAnimationSequence> weak_sequence_ptr = |
| 753 | sequence->AsWeakPtr(); |
| 754 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 755 | const bool abort = true; |
| 756 | RemoveAllAnimationsWithACommonProperty(sequence, abort); |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 757 | if (!weak_sequence_ptr.get()) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 758 | return; |
| 759 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 760 | AddToQueueIfNotPresent(sequence); |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 761 | if (!weak_sequence_ptr.get()) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 762 | return; |
| 763 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 764 | StartSequenceImmediately(sequence); |
| 765 | } |
| 766 | |
| 767 | void LayerAnimator::EnqueueNewAnimation(LayerAnimationSequence* sequence) { |
| 768 | // It is assumed that if there was no conflicting animation, we would |
| 769 | // not have been called. No need to check for a collision; just |
| 770 | // add to the queue. |
| 771 | animation_queue_.push_back(make_linked_ptr(sequence)); |
| 772 | ProcessQueue(); |
| 773 | } |
| 774 | |
| 775 | void LayerAnimator::ReplaceQueuedAnimations(LayerAnimationSequence* sequence) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 776 | // Need to detect if our sequence gets destroyed. |
| 777 | base::WeakPtr<LayerAnimationSequence> weak_sequence_ptr = |
| 778 | sequence->AsWeakPtr(); |
| 779 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 780 | // Remove all animations that aren't running. Note: at each iteration i is |
| 781 | // incremented or an element is removed from the queue, so |
| 782 | // animation_queue_.size() - i is always decreasing and we are always making |
| 783 | // progress towards the loop terminating. |
| 784 | for (size_t i = 0; i < animation_queue_.size();) { |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 785 | if (!weak_sequence_ptr.get()) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 786 | break; |
| 787 | |
| 788 | PurgeDeletedAnimations(); |
| 789 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 790 | bool is_running = false; |
| 791 | for (RunningAnimations::const_iterator iter = running_animations_.begin(); |
| 792 | iter != running_animations_.end(); ++iter) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 793 | if ((*iter).sequence() == animation_queue_[i].get()) { |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 794 | is_running = true; |
| 795 | break; |
| 796 | } |
| 797 | } |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 798 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 799 | if (!is_running) |
[email protected] | 300548c | 2012-06-17 08:54:55 | [diff] [blame] | 800 | delete RemoveAnimation(animation_queue_[i].get()); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 801 | else |
| 802 | ++i; |
| 803 | } |
| 804 | animation_queue_.push_back(make_linked_ptr(sequence)); |
| 805 | ProcessQueue(); |
| 806 | } |
| 807 | |
| 808 | void LayerAnimator::ProcessQueue() { |
| 809 | bool started_sequence = false; |
| 810 | do { |
| 811 | started_sequence = false; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 812 | // Build a list of all currently animated properties. |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 813 | LayerAnimationElement::AnimatableProperties animated = |
| 814 | LayerAnimationElement::UNKNOWN; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 815 | for (RunningAnimations::const_iterator iter = running_animations_.begin(); |
| 816 | iter != running_animations_.end(); ++iter) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 817 | if (!(*iter).is_sequence_alive()) |
| 818 | continue; |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 819 | |
| 820 | animated |= (*iter).sequence()->properties(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | // Try to find an animation that doesn't conflict with an animated |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 824 | // property or a property that will be animated before it. Note: starting |
| 825 | // the animation may indirectly cause more animations to be started, so we |
| 826 | // need to operate on a copy. |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 827 | std::vector<base::WeakPtr<LayerAnimationSequence> > sequences; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 828 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 829 | queue_iter != animation_queue_.end(); ++queue_iter) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 830 | sequences.push_back((*queue_iter)->AsWeakPtr()); |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 831 | |
| 832 | for (size_t i = 0; i < sequences.size(); ++i) { |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 833 | if (!sequences[i].get() || !HasAnimation(sequences[i].get())) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 834 | continue; |
| 835 | |
[email protected] | 712f4b64 | 2013-03-14 07:09:15 | [diff] [blame] | 836 | if (!sequences[i]->HasConflictingProperty(animated)) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 837 | StartSequenceImmediately(sequences[i].get()); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 838 | started_sequence = true; |
| 839 | break; |
| 840 | } |
| 841 | |
| 842 | // Animation couldn't be started. Add its properties to the collection so |
| 843 | // that we don't start a conflicting animation. For example, if our queue |
| 844 | // has the elements { {T,B}, {B} } (that is, an element that animates both |
| 845 | // the transform and the bounds followed by an element that animates the |
| 846 | // bounds), and we're currently animating the transform, we can't start |
| 847 | // the first element because it animates the transform, too. We cannot |
| 848 | // start the second element, either, because the first element animates |
| 849 | // bounds too, and needs to go first. |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 850 | animated |= sequences[i]->properties(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | // If we started a sequence, try again. We may be able to start several. |
| 854 | } while (started_sequence); |
| 855 | } |
| 856 | |
| 857 | bool LayerAnimator::StartSequenceImmediately(LayerAnimationSequence* sequence) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 858 | PurgeDeletedAnimations(); |
| 859 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 860 | // Ensure that no one is animating one of the sequence's properties already. |
| 861 | for (RunningAnimations::const_iterator iter = running_animations_.begin(); |
| 862 | iter != running_animations_.end(); ++iter) { |
[email protected] | 712f4b64 | 2013-03-14 07:09:15 | [diff] [blame] | 863 | if ((*iter).sequence()->HasConflictingProperty(sequence->properties())) |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 864 | return false; |
| 865 | } |
| 866 | |
charliea | 3be83970 | 2015-01-26 17:35:41 | [diff] [blame] | 867 | // All clear, actually start the sequence. |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 868 | // All LayerAnimators share the same LayerAnimatorCollection. Use the |
[email protected] | 1778cbc | 2012-09-06 04:31:19 | [diff] [blame] | 869 | // last_tick_time() from there to ensure animations started during the same |
| 870 | // event complete at the same time. |
| 871 | base::TimeTicks start_time; |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 872 | LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
[email protected] | d064ef8 | 2012-11-13 10:26:59 | [diff] [blame] | 873 | if (is_animating() || adding_animations_) |
[email protected] | 1778cbc | 2012-09-06 04:31:19 | [diff] [blame] | 874 | start_time = last_step_time_; |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 875 | else if (collection && collection->HasActiveAnimators()) |
| 876 | start_time = collection->last_tick_time(); |
[email protected] | 1778cbc | 2012-09-06 04:31:19 | [diff] [blame] | 877 | else |
abhishek.ka | 7215854d | 2015-05-26 06:13:17 | [diff] [blame] | 878 | start_time = base::TimeTicks::Now(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 879 | |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 880 | if (!sequence->animation_group_id()) |
| 881 | sequence->set_animation_group_id(cc::AnimationIdProvider::NextGroupId()); |
| 882 | if (!sequence->waiting_for_group_start() || |
| 883 | sequence->IsFirstElementThreaded()) { |
| 884 | sequence->set_start_time(start_time); |
| 885 | sequence->Start(delegate()); |
| 886 | } |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 887 | running_animations_.push_back( |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 888 | RunningAnimation(sequence->AsWeakPtr())); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 889 | |
| 890 | // Need to keep a reference to the animation. |
| 891 | AddToQueueIfNotPresent(sequence); |
| 892 | |
| 893 | // Ensure that animations get stepped at their start time. |
| 894 | Step(start_time); |
| 895 | |
| 896 | return true; |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 897 | } |
| 898 | |
[email protected] | fe7074c6 | 2011-10-28 15:22:54 | [diff] [blame] | 899 | void LayerAnimator::GetTargetValue( |
| 900 | LayerAnimationElement::TargetValue* target) const { |
[email protected] | b1c37fc | 2012-03-22 03:36:13 | [diff] [blame] | 901 | for (AnimationQueue::const_iterator iter = animation_queue_.begin(); |
| 902 | iter != animation_queue_.end(); ++iter) { |
| 903 | (*iter)->GetTargetValue(target); |
[email protected] | fe7074c6 | 2011-10-28 15:22:54 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
[email protected] | e876c271 | 2011-11-02 16:42:45 | [diff] [blame] | 907 | void LayerAnimator::OnScheduled(LayerAnimationSequence* sequence) { |
| 908 | if (observers_.might_have_observers()) { |
brettw | 5a1613dc | 2015-06-02 05:34:43 | [diff] [blame] | 909 | base::ObserverListBase<LayerAnimationObserver>::Iterator it(&observers_); |
[email protected] | e876c271 | 2011-11-02 16:42:45 | [diff] [blame] | 910 | LayerAnimationObserver* obs; |
| 911 | while ((obs = it.GetNext()) != NULL) { |
| 912 | sequence->AddObserver(obs); |
| 913 | } |
| 914 | } |
| 915 | sequence->OnScheduled(); |
| 916 | } |
| 917 | |
[email protected] | 0d31625 | 2014-01-20 15:31:19 | [diff] [blame] | 918 | void LayerAnimator::SetTransitionDuration(base::TimeDelta duration) { |
| 919 | if (is_transition_duration_locked_) |
| 920 | return; |
| 921 | transition_duration_ = duration; |
[email protected] | 9861f175 | 2012-06-01 07:16:14 | [diff] [blame] | 922 | } |
| 923 | |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 924 | void LayerAnimator::ClearAnimationsInternal() { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 925 | PurgeDeletedAnimations(); |
| 926 | |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 927 | // Abort should never affect the set of running animations, but just in case |
| 928 | // clients are badly behaved, we will use a copy of the running animations. |
| 929 | RunningAnimations running_animations_copy = running_animations_; |
| 930 | for (size_t i = 0; i < running_animations_copy.size(); ++i) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 931 | if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i])) |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 932 | continue; |
| 933 | |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 934 | std::unique_ptr<LayerAnimationSequence> removed( |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 935 | RemoveAnimation(running_animations_copy[i].sequence())); |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 936 | if (removed.get()) |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 937 | removed->Abort(delegate()); |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 938 | } |
| 939 | // This *should* have cleared the list of running animations. |
| 940 | DCHECK(running_animations_.empty()); |
| 941 | running_animations_.clear(); |
| 942 | animation_queue_.clear(); |
| 943 | UpdateAnimationState(); |
| 944 | } |
| 945 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 946 | void LayerAnimator::PurgeDeletedAnimations() { |
| 947 | for (size_t i = 0; i < running_animations_.size();) { |
| 948 | if (!running_animations_[i].is_sequence_alive()) |
| 949 | running_animations_.erase(running_animations_.begin() + i); |
| 950 | else |
| 951 | i++; |
| 952 | } |
| 953 | } |
| 954 | |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 955 | LayerAnimatorCollection* LayerAnimator::GetLayerAnimatorCollection() { |
| 956 | return delegate_ ? delegate_->GetLayerAnimatorCollection() : NULL; |
| 957 | } |
| 958 | |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 959 | void LayerAnimator::NotifyAnimationStarted( |
| 960 | base::TimeTicks monotonic_time, |
| 961 | cc::TargetProperty::Type target_property, |
| 962 | int group) { |
| 963 | OnThreadedAnimationStarted(monotonic_time, target_property, group); |
loyso | 1fbd9f9 | 2015-12-17 07:43:13 | [diff] [blame] | 964 | } |
| 965 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 966 | LayerAnimator::RunningAnimation::RunningAnimation( |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 967 | const base::WeakPtr<LayerAnimationSequence>& sequence) |
| 968 | : sequence_(sequence) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 969 | } |
| 970 | |
vmpstr | 0ae825e7 | 2016-02-25 20:31:31 | [diff] [blame] | 971 | LayerAnimator::RunningAnimation::RunningAnimation( |
| 972 | const RunningAnimation& other) = default; |
| 973 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 974 | LayerAnimator::RunningAnimation::~RunningAnimation() { } |
| 975 | |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 976 | } // namespace ui |