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