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