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