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> |
Andrew Rayskiy | 0b0a148 | 2024-02-27 16:07:56 | [diff] [blame] | 10 | #include <vector> |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 11 | |
Hans Wennborg | 8586102b | 2020-05-05 13:43:29 | [diff] [blame] | 12 | #include "base/check_op.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 |
Tom Sepez | ce83030 | 2023-06-30 22:58:43 | [diff] [blame] | 66 | scoped_refptr<LayerAnimator> LayerAnimator::CreateDefaultAnimator() { |
| 67 | return base::MakeRefCounted<LayerAnimator>(base::Milliseconds(0)); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // static |
Tom Sepez | ce83030 | 2023-06-30 22:58:43 | [diff] [blame] | 71 | scoped_refptr<LayerAnimator> LayerAnimator::CreateImplicitAnimator() { |
| 72 | return base::MakeRefCounted<LayerAnimator>( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 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) |
Zoraiz Naeem | 3abbe3d | 2024-12-06 00:06:59 | [diff] [blame] | 118 | ANIMATED_PROPERTY(SkColor4f, COLOR, Color, SkColor4f, 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) { |
Andrew Rayskiy | 0b0a148 | 2024-02-27 16:07:56 | [diff] [blame] | 402 | std::erase_if(owned_observer_list_, |
| 403 | [animation_observer]( |
| 404 | const std::unique_ptr<ImplicitAnimationObserver>& other) { |
| 405 | return other.get() == animation_observer; |
| 406 | }); |
wutao | 4f1c0d5d | 2017-10-05 01:02:43 | [diff] [blame] | 407 | } |
| 408 | |
Allen Bauer | 8a3cd65 | 2021-09-13 22:20:03 | [diff] [blame] | 409 | base::CallbackListSubscription LayerAnimator::AddSequenceScheduledCallback( |
| 410 | SequenceScheduledCallback callback) { |
| 411 | return sequence_scheduled_callbacks_.Add(std::move(callback)); |
| 412 | } |
| 413 | |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 414 | void LayerAnimator::OnThreadedAnimationStarted( |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 415 | base::TimeTicks monotonic_time, |
| 416 | cc::TargetProperty::Type target_property, |
| 417 | int group_id) { |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 418 | LayerAnimationElement::AnimatableProperty property = |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 419 | LayerAnimationElement::ToAnimatableProperty(target_property); |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 420 | |
| 421 | RunningAnimation* running = GetRunningAnimation(property); |
| 422 | if (!running) |
| 423 | return; |
| 424 | DCHECK(running->is_sequence_alive()); |
| 425 | |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 426 | if (running->sequence()->animation_group_id() != group_id) |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 427 | return; |
| 428 | |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 429 | running->sequence()->OnThreadedAnimationStarted(monotonic_time, |
| 430 | target_property, group_id); |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 431 | if (!running->sequence()->waiting_for_group_start()) |
| 432 | return; |
| 433 | |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 434 | base::TimeTicks start_time = monotonic_time; |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 435 | |
| 436 | running->sequence()->set_waiting_for_group_start(false); |
| 437 | |
| 438 | // The call to GetRunningAnimation made above already purged deleted |
| 439 | // animations, so we are guaranteed that all the animations we iterate |
| 440 | // over now are alive. |
jdoerrie | dcef4b1 | 2018-10-10 12:04:32 | [diff] [blame] | 441 | for (auto iter = running_animations_.begin(); |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 442 | iter != running_animations_.end(); ++iter) { |
| 443 | // Ensure that each sequence is only Started once, regardless of the |
| 444 | // number of sequences in the group that have threaded first elements. |
loyso | 79c0bfc | 2016-04-18 23:46:42 | [diff] [blame] | 445 | if (((*iter).sequence()->animation_group_id() == group_id) && |
wutao | 914b06e6 | 2017-09-28 17:41:15 | [diff] [blame] | 446 | !(*iter).sequence()->IsFirstElementThreaded(delegate_) && |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 447 | (*iter).sequence()->waiting_for_group_start()) { |
| 448 | (*iter).sequence()->set_start_time(start_time); |
| 449 | (*iter).sequence()->set_waiting_for_group_start(false); |
| 450 | (*iter).sequence()->Start(delegate()); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 455 | void LayerAnimator::AddToCollection(LayerAnimatorCollection* collection) { |
L. David Baron | 138321d | 2022-05-24 22:30:24 | [diff] [blame] | 456 | DCHECK_EQ(collection, GetLayerAnimatorCollection()); |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 457 | if (is_animating() && !is_started_) { |
| 458 | collection->StartAnimator(this); |
| 459 | is_started_ = true; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | void LayerAnimator::RemoveFromCollection(LayerAnimatorCollection* collection) { |
L. David Baron | 138321d | 2022-05-24 22:30:24 | [diff] [blame] | 464 | DCHECK_EQ(collection, GetLayerAnimatorCollection()); |
bruthig | 33b1edab | 2016-03-02 02:22:35 | [diff] [blame] | 465 | if (is_started_) { |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 466 | collection->StopAnimator(this); |
| 467 | is_started_ = false; |
| 468 | } |
L. David Baron | 138321d | 2022-05-24 22:30:24 | [diff] [blame] | 469 | DCHECK(!animation_->element_animations() || |
| 470 | !animation_->element_animations()->HasTickingKeyframeEffect()); |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 471 | } |
| 472 | |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 473 | // LayerAnimator protected ----------------------------------------------------- |
| 474 | |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 475 | void LayerAnimator::ProgressAnimation(LayerAnimationSequence* sequence, |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 476 | base::TimeTicks now) { |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 477 | if (!delegate() || sequence->waiting_for_group_start()) |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 478 | return; |
| 479 | |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 480 | sequence->Progress(now, delegate()); |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 481 | } |
| 482 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 483 | void LayerAnimator::ProgressAnimationToEnd(LayerAnimationSequence* sequence) { |
[email protected] | 4a386e4 | 2012-12-05 01:19:30 | [diff] [blame] | 484 | if (!delegate()) |
| 485 | return; |
| 486 | |
| 487 | sequence->ProgressToEnd(delegate()); |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 488 | } |
| 489 | |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 490 | bool LayerAnimator::HasAnimation(LayerAnimationSequence* sequence) const { |
| 491 | for (AnimationQueue::const_iterator queue_iter = animation_queue_.begin(); |
| 492 | queue_iter != animation_queue_.end(); ++queue_iter) { |
| 493 | if ((*queue_iter).get() == sequence) |
| 494 | return true; |
| 495 | } |
| 496 | return false; |
| 497 | } |
| 498 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 499 | // LayerAnimator private ------------------------------------------------------- |
| 500 | |
| 501 | void LayerAnimator::Step(base::TimeTicks now) { |
[email protected] | e4cbcc77 | 2012-03-07 18:59:31 | [diff] [blame] | 502 | TRACE_EVENT0("ui", "LayerAnimator::Step"); |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 503 | scoped_refptr<LayerAnimator> retain(this); |
[email protected] | e4cbcc77 | 2012-03-07 18:59:31 | [diff] [blame] | 504 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 505 | last_step_time_ = now; |
[email protected] | 1778cbc | 2012-09-06 04:31:19 | [diff] [blame] | 506 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 507 | PurgeDeletedAnimations(); |
| 508 | |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 509 | // We need to make a copy of the running animations because progressing them |
| 510 | // and finishing them may indirectly affect the collection of running |
| 511 | // animations. |
| 512 | RunningAnimations running_animations_copy = running_animations_; |
| 513 | for (size_t i = 0; i < running_animations_copy.size(); ++i) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 514 | if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i])) |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 515 | continue; |
| 516 | |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 517 | if (running_animations_copy[i].sequence()->IsFinished(now)) { |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 518 | SAFE_INVOKE_VOID(FinishAnimation, running_animations_copy[i], false); |
| 519 | } else { |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 520 | SAFE_INVOKE_VOID(ProgressAnimation, running_animations_copy[i], now); |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 521 | } |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 522 | } |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 523 | } |
| 524 | |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 525 | void LayerAnimator::StopAnimatingInternal(bool abort) { |
| 526 | scoped_refptr<LayerAnimator> retain(this); |
ljagielski | 2057098 | 2015-01-07 20:32:55 | [diff] [blame] | 527 | while (is_animating() && delegate()) { |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 528 | // We're going to attempt to finish the first running animation. Let's |
| 529 | // ensure that it's valid. |
| 530 | PurgeDeletedAnimations(); |
| 531 | |
| 532 | // If we've purged all running animations, attempt to start one up. |
| 533 | if (running_animations_.empty()) |
| 534 | ProcessQueue(); |
| 535 | |
| 536 | DCHECK(!running_animations_.empty()); |
| 537 | |
| 538 | // Still no luck, let's just bail and clear all animations. |
| 539 | if (running_animations_.empty()) { |
| 540 | ClearAnimationsInternal(); |
| 541 | break; |
| 542 | } |
| 543 | |
| 544 | SAFE_INVOKE_VOID(FinishAnimation, running_animations_[0], abort); |
| 545 | } |
| 546 | } |
| 547 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 548 | void LayerAnimator::UpdateAnimationState() { |
| 549 | if (disable_timer_for_test_) |
[email protected] | 7fca53d4 | 2011-09-29 15:38:12 | [diff] [blame] | 550 | return; |
| 551 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 552 | const bool should_start = is_animating(); |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 553 | LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
| 554 | if (collection) { |
| 555 | if (should_start && !is_started_) |
| 556 | collection->StartAnimator(this); |
| 557 | else if (!should_start && is_started_) |
| 558 | collection->StopAnimator(this); |
| 559 | is_started_ = should_start; |
| 560 | } else { |
| 561 | is_started_ = false; |
| 562 | } |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 563 | } |
| 564 | |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 565 | LayerAnimationSequence* LayerAnimator::RemoveAnimation( |
| 566 | LayerAnimationSequence* sequence) { |
Ian Vollick | 37e51bd1 | 2018-12-18 14:32:09 | [diff] [blame] | 567 | std::unique_ptr<LayerAnimationSequence> to_return; |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 568 | |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 569 | bool is_running = false; |
| 570 | |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 571 | // First remove from running animations |
jdoerrie | dcef4b1 | 2018-10-10 12:04:32 | [diff] [blame] | 572 | for (auto iter = running_animations_.begin(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 573 | iter != running_animations_.end(); ++iter) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 574 | if ((*iter).sequence() == sequence) { |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 575 | running_animations_.erase(iter); |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 576 | is_running = true; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 577 | break; |
[email protected] | 21445e47 | 2011-10-21 20:36:32 | [diff] [blame] | 578 | } |
| 579 | } |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 580 | |
| 581 | // Then remove from the queue |
| 582 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
| 583 | queue_iter != animation_queue_.end(); ++queue_iter) { |
Ian Vollick | 37e51bd1 | 2018-12-18 14:32:09 | [diff] [blame] | 584 | if (queue_iter->get() == sequence) { |
| 585 | to_return = std::move(*queue_iter); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 586 | animation_queue_.erase(queue_iter); |
| 587 | break; |
| 588 | } |
| 589 | } |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 590 | |
Elaine Chien | a2c1d70 | 2021-09-08 22:50:38 | [diff] [blame] | 591 | // Do not continue and attempt to start other sequences if the delegate is |
| 592 | // nullptr. |
Alison Gale | c5ea1859 | 2024-04-16 16:08:51 | [diff] [blame] | 593 | // TODO(crbug.com/40790139): Guard other uses of delegate_ in this class. |
Elaine Chien | a2c1d70 | 2021-09-08 22:50:38 | [diff] [blame] | 594 | if (!delegate()) |
| 595 | return to_return.release(); |
| 596 | |
wutao | 914b06e6 | 2017-09-28 17:41:15 | [diff] [blame] | 597 | if (!to_return.get() || !to_return->waiting_for_group_start() || |
| 598 | !to_return->IsFirstElementThreaded(delegate_)) |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 599 | return to_return.release(); |
| 600 | |
| 601 | // The removed sequence may have been responsible for making other sequences |
| 602 | // wait for a group start. If no other sequences in the group have a |
| 603 | // threaded first element, the group no longer needs the additional wait. |
| 604 | bool is_wait_still_needed = false; |
| 605 | int group_id = to_return->animation_group_id(); |
| 606 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
| 607 | queue_iter != animation_queue_.end(); ++queue_iter) { |
| 608 | if (((*queue_iter)->animation_group_id() == group_id) && |
wutao | 914b06e6 | 2017-09-28 17:41:15 | [diff] [blame] | 609 | (*queue_iter)->IsFirstElementThreaded(delegate_)) { |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 610 | is_wait_still_needed = true; |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | if (is_wait_still_needed) |
| 616 | return to_return.release(); |
| 617 | |
| 618 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
| 619 | queue_iter != animation_queue_.end(); ++queue_iter) { |
| 620 | if ((*queue_iter)->animation_group_id() == group_id && |
| 621 | (*queue_iter)->waiting_for_group_start()) { |
| 622 | (*queue_iter)->set_waiting_for_group_start(false); |
| 623 | if (is_running) { |
| 624 | (*queue_iter)->set_start_time(last_step_time_); |
| 625 | (*queue_iter)->Start(delegate()); |
| 626 | } |
| 627 | } |
| 628 | } |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 629 | return to_return.release(); |
[email protected] | 0ed187e | 2011-10-21 20:07:42 | [diff] [blame] | 630 | } |
| 631 | |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 632 | void LayerAnimator::FinishAnimation( |
| 633 | LayerAnimationSequence* sequence, bool abort) { |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 634 | scoped_refptr<LayerAnimator> retain(this); |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 635 | std::unique_ptr<LayerAnimationSequence> removed(RemoveAnimation(sequence)); |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 636 | if (abort) |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 637 | sequence->Abort(delegate()); |
[email protected] | 6f110e4 | 2012-12-18 00:21:14 | [diff] [blame] | 638 | else |
| 639 | ProgressAnimationToEnd(sequence); |
ljagielski | 2057098 | 2015-01-07 20:32:55 | [diff] [blame] | 640 | if (!delegate()) |
| 641 | return; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 642 | ProcessQueue(); |
| 643 | UpdateAnimationState(); |
[email protected] | 21445e47 | 2011-10-21 20:36:32 | [diff] [blame] | 644 | } |
[email protected] | 0ed187e | 2011-10-21 20:07:42 | [diff] [blame] | 645 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 646 | void LayerAnimator::FinishAnyAnimationWithZeroDuration() { |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 647 | scoped_refptr<LayerAnimator> retain(this); |
[email protected] | 2ddfe43 | 2011-11-07 19:26:30 | [diff] [blame] | 648 | // Special case: if we've started a 0 duration animation, just finish it now |
| 649 | // and get rid of it. We need to make a copy because Progress may indirectly |
| 650 | // cause new animations to start running. |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 651 | RunningAnimations running_animations_copy = running_animations_; |
| 652 | for (size_t i = 0; i < running_animations_copy.size(); ++i) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 653 | if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i])) |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 654 | continue; |
| 655 | |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 656 | if (running_animations_copy[i].sequence()->IsFinished( |
| 657 | running_animations_copy[i].sequence()->start_time())) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 658 | SAFE_INVOKE_VOID(ProgressAnimationToEnd, running_animations_copy[i]); |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 659 | std::unique_ptr<LayerAnimationSequence> removed( |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 660 | SAFE_INVOKE_PTR(RemoveAnimation, running_animations_copy[i])); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | ProcessQueue(); |
| 664 | UpdateAnimationState(); |
[email protected] | 21445e47 | 2011-10-21 20:36:32 | [diff] [blame] | 665 | } |
[email protected] | 0ed187e | 2011-10-21 20:07:42 | [diff] [blame] | 666 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 667 | void LayerAnimator::ClearAnimations() { |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 668 | scoped_refptr<LayerAnimator> retain(this); |
| 669 | ClearAnimationsInternal(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | LayerAnimator::RunningAnimation* LayerAnimator::GetRunningAnimation( |
| 673 | LayerAnimationElement::AnimatableProperty property) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 674 | PurgeDeletedAnimations(); |
jdoerrie | dcef4b1 | 2018-10-10 12:04:32 | [diff] [blame] | 675 | for (auto iter = running_animations_.begin(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 676 | iter != running_animations_.end(); ++iter) { |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 677 | if ((*iter).sequence()->properties() & property) |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 678 | return &(*iter); |
| 679 | } |
Bartek Nowierski | eb07a5e | 2020-06-03 06:45:17 | [diff] [blame] | 680 | return nullptr; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | void LayerAnimator::AddToQueueIfNotPresent(LayerAnimationSequence* animation) { |
| 684 | // If we don't have the animation in the queue yet, add it. |
| 685 | bool found_sequence = false; |
| 686 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
| 687 | queue_iter != animation_queue_.end(); ++queue_iter) { |
Ian Vollick | 37e51bd1 | 2018-12-18 14:32:09 | [diff] [blame] | 688 | if (queue_iter->get() == animation) { |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 689 | found_sequence = true; |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | |
Ian Vollick | 37e51bd1 | 2018-12-18 14:32:09 | [diff] [blame] | 694 | if (!found_sequence) { |
| 695 | animation_queue_.push_front( |
| 696 | std::unique_ptr<LayerAnimationSequence>(animation)); |
| 697 | } |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | void LayerAnimator::RemoveAllAnimationsWithACommonProperty( |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 701 | LayerAnimationSequence* sequence, bool abort) { |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 702 | // For all the running animations, if they animate the same property, |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 703 | // progress them to the end and remove them. Note, Aborting or Progressing |
| 704 | // animations may affect the collection of running animations, so we need to |
| 705 | // operate on a copy. |
| 706 | RunningAnimations running_animations_copy = running_animations_; |
| 707 | for (size_t i = 0; i < running_animations_copy.size(); ++i) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 708 | if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i])) |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 709 | continue; |
| 710 | |
[email protected] | 712f4b64 | 2013-03-14 07:09:15 | [diff] [blame] | 711 | if (running_animations_copy[i].sequence()->HasConflictingProperty( |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 712 | sequence->properties())) { |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 713 | std::unique_ptr<LayerAnimationSequence> removed( |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 714 | SAFE_INVOKE_PTR(RemoveAnimation, running_animations_copy[i])); |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 715 | if (abort) |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 716 | running_animations_copy[i].sequence()->Abort(delegate()); |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 717 | else |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 718 | SAFE_INVOKE_VOID(ProgressAnimationToEnd, running_animations_copy[i]); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 722 | // Same for the queued animations that haven't been started. Again, we'll |
| 723 | // need to operate on a copy. |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 724 | std::vector<base::WeakPtr<LayerAnimationSequence> > sequences; |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 725 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
| 726 | queue_iter != animation_queue_.end(); ++queue_iter) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 727 | sequences.push_back((*queue_iter)->AsWeakPtr()); |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 728 | |
| 729 | for (size_t i = 0; i < sequences.size(); ++i) { |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 730 | if (!sequences[i].get() || !HasAnimation(sequences[i].get())) |
[email protected] | d59a369 | 2012-04-10 20:27:31 | [diff] [blame] | 731 | continue; |
| 732 | |
[email protected] | 712f4b64 | 2013-03-14 07:09:15 | [diff] [blame] | 733 | if (sequences[i]->HasConflictingProperty(sequence->properties())) { |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 734 | std::unique_ptr<LayerAnimationSequence> removed( |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 735 | RemoveAnimation(sequences[i].get())); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 736 | if (abort) |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 737 | sequences[i]->Abort(delegate()); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 738 | else |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 739 | ProgressAnimationToEnd(sequences[i].get()); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | void LayerAnimator::ImmediatelySetNewTarget(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 = false; |
| 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] | d3ba37ab | 2012-02-09 19:53:13 | [diff] [blame] | 754 | LayerAnimationSequence* removed = RemoveAnimation(sequence); |
Bartek Nowierski | eb07a5e | 2020-06-03 06:45:17 | [diff] [blame] | 755 | DCHECK(removed == nullptr || removed == sequence); |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 756 | if (!weak_sequence_ptr.get()) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 757 | return; |
| 758 | |
[email protected] | 4a386e4 | 2012-12-05 01:19:30 | [diff] [blame] | 759 | ProgressAnimationToEnd(sequence); |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 760 | if (!weak_sequence_ptr.get()) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 761 | return; |
| 762 | |
| 763 | delete sequence; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | void LayerAnimator::ImmediatelyAnimateToNewTarget( |
| 767 | LayerAnimationSequence* sequence) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 768 | // Need to detect if our sequence gets destroyed. |
| 769 | base::WeakPtr<LayerAnimationSequence> weak_sequence_ptr = |
| 770 | sequence->AsWeakPtr(); |
| 771 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 772 | const bool abort = true; |
| 773 | RemoveAllAnimationsWithACommonProperty(sequence, abort); |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 774 | if (!weak_sequence_ptr.get()) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 775 | return; |
| 776 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 777 | AddToQueueIfNotPresent(sequence); |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 778 | if (!weak_sequence_ptr.get()) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 779 | return; |
| 780 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 781 | StartSequenceImmediately(sequence); |
| 782 | } |
| 783 | |
| 784 | void LayerAnimator::EnqueueNewAnimation(LayerAnimationSequence* sequence) { |
| 785 | // It is assumed that if there was no conflicting animation, we would |
| 786 | // not have been called. No need to check for a collision; just |
| 787 | // add to the queue. |
Ian Vollick | 37e51bd1 | 2018-12-18 14:32:09 | [diff] [blame] | 788 | animation_queue_.push_back(std::unique_ptr<LayerAnimationSequence>(sequence)); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 789 | ProcessQueue(); |
| 790 | } |
| 791 | |
| 792 | void LayerAnimator::ReplaceQueuedAnimations(LayerAnimationSequence* sequence) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 793 | // Need to detect if our sequence gets destroyed. |
| 794 | base::WeakPtr<LayerAnimationSequence> weak_sequence_ptr = |
| 795 | sequence->AsWeakPtr(); |
| 796 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 797 | // Remove all animations that aren't running. Note: at each iteration i is |
| 798 | // incremented or an element is removed from the queue, so |
| 799 | // animation_queue_.size() - i is always decreasing and we are always making |
| 800 | // progress towards the loop terminating. |
| 801 | for (size_t i = 0; i < animation_queue_.size();) { |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 802 | if (!weak_sequence_ptr.get()) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 803 | break; |
| 804 | |
| 805 | PurgeDeletedAnimations(); |
| 806 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 807 | bool is_running = false; |
| 808 | for (RunningAnimations::const_iterator iter = running_animations_.begin(); |
| 809 | iter != running_animations_.end(); ++iter) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 810 | if ((*iter).sequence() == animation_queue_[i].get()) { |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 811 | is_running = true; |
| 812 | break; |
| 813 | } |
| 814 | } |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 815 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 816 | if (!is_running) |
[email protected] | 300548c | 2012-06-17 08:54:55 | [diff] [blame] | 817 | delete RemoveAnimation(animation_queue_[i].get()); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 818 | else |
| 819 | ++i; |
| 820 | } |
Ian Vollick | 37e51bd1 | 2018-12-18 14:32:09 | [diff] [blame] | 821 | animation_queue_.push_back(std::unique_ptr<LayerAnimationSequence>(sequence)); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 822 | ProcessQueue(); |
| 823 | } |
| 824 | |
| 825 | void LayerAnimator::ProcessQueue() { |
| 826 | bool started_sequence = false; |
| 827 | do { |
| 828 | started_sequence = false; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 829 | // Build a list of all currently animated properties. |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 830 | LayerAnimationElement::AnimatableProperties animated = |
| 831 | LayerAnimationElement::UNKNOWN; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 832 | for (RunningAnimations::const_iterator iter = running_animations_.begin(); |
| 833 | iter != running_animations_.end(); ++iter) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 834 | if (!(*iter).is_sequence_alive()) |
| 835 | continue; |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 836 | |
| 837 | animated |= (*iter).sequence()->properties(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | // Try to find an animation that doesn't conflict with an animated |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 841 | // property or a property that will be animated before it. Note: starting |
| 842 | // the animation may indirectly cause more animations to be started, so we |
| 843 | // need to operate on a copy. |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 844 | std::vector<base::WeakPtr<LayerAnimationSequence> > sequences; |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 845 | for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 846 | queue_iter != animation_queue_.end(); ++queue_iter) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 847 | sequences.push_back((*queue_iter)->AsWeakPtr()); |
[email protected] | f5cd9e5 | 2011-11-03 21:38:08 | [diff] [blame] | 848 | |
| 849 | for (size_t i = 0; i < sequences.size(); ++i) { |
[email protected] | 7ffde47 | 2013-06-04 06:42:19 | [diff] [blame] | 850 | if (!sequences[i].get() || !HasAnimation(sequences[i].get())) |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 851 | continue; |
| 852 | |
[email protected] | 712f4b64 | 2013-03-14 07:09:15 | [diff] [blame] | 853 | if (!sequences[i]->HasConflictingProperty(animated)) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 854 | StartSequenceImmediately(sequences[i].get()); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 855 | started_sequence = true; |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | // Animation couldn't be started. Add its properties to the collection so |
| 860 | // that we don't start a conflicting animation. For example, if our queue |
| 861 | // has the elements { {T,B}, {B} } (that is, an element that animates both |
| 862 | // the transform and the bounds followed by an element that animates the |
| 863 | // bounds), and we're currently animating the transform, we can't start |
| 864 | // the first element because it animates the transform, too. We cannot |
| 865 | // start the second element, either, because the first element animates |
| 866 | // bounds too, and needs to go first. |
[email protected] | e03193d | 2014-01-14 03:10:24 | [diff] [blame] | 867 | animated |= sequences[i]->properties(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | // If we started a sequence, try again. We may be able to start several. |
| 871 | } while (started_sequence); |
| 872 | } |
| 873 | |
| 874 | bool LayerAnimator::StartSequenceImmediately(LayerAnimationSequence* sequence) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 875 | PurgeDeletedAnimations(); |
| 876 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 877 | // Ensure that no one is animating one of the sequence's properties already. |
| 878 | for (RunningAnimations::const_iterator iter = running_animations_.begin(); |
| 879 | iter != running_animations_.end(); ++iter) { |
[email protected] | 712f4b64 | 2013-03-14 07:09:15 | [diff] [blame] | 880 | if ((*iter).sequence()->HasConflictingProperty(sequence->properties())) |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 881 | return false; |
| 882 | } |
| 883 | |
charliea | 3be83970 | 2015-01-26 17:35:41 | [diff] [blame] | 884 | // All clear, actually start the sequence. |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 885 | // All LayerAnimators share the same LayerAnimatorCollection. Use the |
[email protected] | 1778cbc | 2012-09-06 04:31:19 | [diff] [blame] | 886 | // last_tick_time() from there to ensure animations started during the same |
| 887 | // event complete at the same time. |
| 888 | base::TimeTicks start_time; |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 889 | LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
[email protected] | d064ef8 | 2012-11-13 10:26:59 | [diff] [blame] | 890 | if (is_animating() || adding_animations_) |
[email protected] | 1778cbc | 2012-09-06 04:31:19 | [diff] [blame] | 891 | start_time = last_step_time_; |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 892 | else if (collection && collection->HasActiveAnimators()) |
| 893 | start_time = collection->last_tick_time(); |
[email protected] | 1778cbc | 2012-09-06 04:31:19 | [diff] [blame] | 894 | else |
abhishek.ka | 7215854d | 2015-05-26 06:13:17 | [diff] [blame] | 895 | start_time = base::TimeTicks::Now(); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 896 | |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 897 | if (!sequence->animation_group_id()) |
| 898 | sequence->set_animation_group_id(cc::AnimationIdProvider::NextGroupId()); |
bruthig | 4a12c2b | 2016-11-05 00:19:18 | [diff] [blame] | 899 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 900 | running_animations_.push_back( |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 901 | RunningAnimation(sequence->AsWeakPtr())); |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 902 | |
| 903 | // Need to keep a reference to the animation. |
| 904 | AddToQueueIfNotPresent(sequence); |
| 905 | |
bruthig | 4a12c2b | 2016-11-05 00:19:18 | [diff] [blame] | 906 | if (!sequence->waiting_for_group_start() || |
wutao | 914b06e6 | 2017-09-28 17:41:15 | [diff] [blame] | 907 | sequence->IsFirstElementThreaded(delegate_)) { |
bruthig | 4a12c2b | 2016-11-05 00:19:18 | [diff] [blame] | 908 | sequence->set_start_time(start_time); |
| 909 | sequence->Start(delegate()); |
| 910 | } |
| 911 | |
[email protected] | b4db937 | 2011-10-24 14:44:19 | [diff] [blame] | 912 | // Ensure that animations get stepped at their start time. |
| 913 | Step(start_time); |
| 914 | |
| 915 | return true; |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 916 | } |
| 917 | |
[email protected] | fe7074c6 | 2011-10-28 15:22:54 | [diff] [blame] | 918 | void LayerAnimator::GetTargetValue( |
| 919 | LayerAnimationElement::TargetValue* target) const { |
[email protected] | b1c37fc | 2012-03-22 03:36:13 | [diff] [blame] | 920 | for (AnimationQueue::const_iterator iter = animation_queue_.begin(); |
| 921 | iter != animation_queue_.end(); ++iter) { |
| 922 | (*iter)->GetTargetValue(target); |
[email protected] | fe7074c6 | 2011-10-28 15:22:54 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
[email protected] | e876c27 | 2011-11-02 16:42:45 | [diff] [blame] | 926 | void LayerAnimator::OnScheduled(LayerAnimationSequence* sequence) { |
Allen Bauer | 8a3cd65 | 2021-09-13 22:20:03 | [diff] [blame] | 927 | sequence_scheduled_callbacks_.Notify(sequence); |
dcheng | d38152e | 2016-10-13 18:21:37 | [diff] [blame] | 928 | for (LayerAnimationObserver& observer : observers_) |
| 929 | sequence->AddObserver(&observer); |
[email protected] | e876c27 | 2011-11-02 16:42:45 | [diff] [blame] | 930 | sequence->OnScheduled(); |
| 931 | } |
| 932 | |
[email protected] | 0d31625 | 2014-01-20 15:31:19 | [diff] [blame] | 933 | void LayerAnimator::SetTransitionDuration(base::TimeDelta duration) { |
| 934 | if (is_transition_duration_locked_) |
| 935 | return; |
| 936 | transition_duration_ = duration; |
[email protected] | 9861f175 | 2012-06-01 07:16:14 | [diff] [blame] | 937 | } |
| 938 | |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 939 | void LayerAnimator::ClearAnimationsInternal() { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 940 | PurgeDeletedAnimations(); |
| 941 | |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 942 | // Abort should never affect the set of running animations, but just in case |
| 943 | // clients are badly behaved, we will use a copy of the running animations. |
| 944 | RunningAnimations running_animations_copy = running_animations_; |
| 945 | for (size_t i = 0; i < running_animations_copy.size(); ++i) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 946 | if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i])) |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 947 | continue; |
| 948 | |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 949 | std::unique_ptr<LayerAnimationSequence> removed( |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 950 | RemoveAnimation(running_animations_copy[i].sequence())); |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 951 | if (removed.get()) |
[email protected] | bf91227 | 2013-02-23 01:54:16 | [diff] [blame] | 952 | removed->Abort(delegate()); |
[email protected] | 5d86a11 | 2012-09-23 00:21:58 | [diff] [blame] | 953 | } |
| 954 | // This *should* have cleared the list of running animations. |
| 955 | DCHECK(running_animations_.empty()); |
| 956 | running_animations_.clear(); |
| 957 | animation_queue_.clear(); |
| 958 | UpdateAnimationState(); |
| 959 | } |
| 960 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 961 | void LayerAnimator::PurgeDeletedAnimations() { |
| 962 | for (size_t i = 0; i < running_animations_.size();) { |
| 963 | if (!running_animations_[i].is_sequence_alive()) |
| 964 | running_animations_.erase(running_animations_.begin() + i); |
| 965 | else |
| 966 | i++; |
| 967 | } |
| 968 | } |
| 969 | |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 970 | LayerAnimatorCollection* LayerAnimator::GetLayerAnimatorCollection() { |
Bartek Nowierski | eb07a5e | 2020-06-03 06:45:17 | [diff] [blame] | 971 | return delegate_ ? delegate_->GetLayerAnimatorCollection() : nullptr; |
[email protected] | 9034a28 | 2014-06-05 03:11:47 | [diff] [blame] | 972 | } |
| 973 | |
Ian Vollick | ba1f8592 | 2017-07-21 18:10:03 | [diff] [blame] | 974 | void LayerAnimator::NotifyAnimationStarted(base::TimeTicks monotonic_time, |
| 975 | int target_property, |
| 976 | int group) { |
| 977 | OnThreadedAnimationStarted( |
| 978 | monotonic_time, static_cast<cc::TargetProperty::Type>(target_property), |
| 979 | group); |
loyso | 1fbd9f9 | 2015-12-17 07:43:13 | [diff] [blame] | 980 | } |
| 981 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 982 | LayerAnimator::RunningAnimation::RunningAnimation( |
[email protected] | 3d75fed | 2012-12-15 18:47:38 | [diff] [blame] | 983 | const base::WeakPtr<LayerAnimationSequence>& sequence) |
| 984 | : sequence_(sequence) { |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 985 | } |
| 986 | |
vmpstr | 0ae825e7 | 2016-02-25 20:31:31 | [diff] [blame] | 987 | LayerAnimator::RunningAnimation::RunningAnimation( |
| 988 | const RunningAnimation& other) = default; |
| 989 | |
[email protected] | a48f30d | 2012-10-30 00:35:41 | [diff] [blame] | 990 | LayerAnimator::RunningAnimation::~RunningAnimation() { } |
| 991 | |
[email protected] | 710a98d | 2011-06-23 20:13:29 | [diff] [blame] | 992 | } // namespace ui |