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