| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/compositor/layer_animator.h" | 5 #include "ui/compositor/layer_animator.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "cc/animation/animation_id_provider.h" | 10 #include "cc/animation/animation_id_provider.h" |
| 11 #include "cc/output/begin_frame_args.h" | 11 #include "cc/output/begin_frame_args.h" |
| 12 #include "ui/compositor/compositor.h" | 12 #include "ui/compositor/compositor.h" |
| 13 #include "ui/compositor/layer.h" | 13 #include "ui/compositor/layer.h" |
| 14 #include "ui/compositor/layer_animation_delegate.h" | 14 #include "ui/compositor/layer_animation_delegate.h" |
| 15 #include "ui/compositor/layer_animation_observer.h" | 15 #include "ui/compositor/layer_animation_observer.h" |
| 16 #include "ui/compositor/layer_animation_sequence.h" | 16 #include "ui/compositor/layer_animation_sequence.h" |
| 17 #include "ui/compositor/layer_animator_collection.h" | 17 #include "ui/gfx/animation/animation_container.h" |
| 18 #include "ui/gfx/frame_time.h" | 18 #include "ui/gfx/frame_time.h" |
| 19 | 19 |
| 20 #define SAFE_INVOKE_VOID(function, running_anim, ...) \ | 20 #define SAFE_INVOKE_VOID(function, running_anim, ...) \ |
| 21 if (running_anim.is_sequence_alive()) \ | 21 if (running_anim.is_sequence_alive()) \ |
| 22 function(running_anim.sequence(), ##__VA_ARGS__) | 22 function(running_anim.sequence(), ##__VA_ARGS__) |
| 23 #define SAFE_INVOKE_BOOL(function, running_anim) \ | 23 #define SAFE_INVOKE_BOOL(function, running_anim) \ |
| 24 ((running_anim.is_sequence_alive()) \ | 24 ((running_anim.is_sequence_alive()) \ |
| 25 ? function(running_anim.sequence()) \ | 25 ? function(running_anim.sequence()) \ |
| 26 : false) | 26 : false) |
| 27 #define SAFE_INVOKE_PTR(function, running_anim) \ | 27 #define SAFE_INVOKE_PTR(function, running_anim) \ |
| 28 ((running_anim.is_sequence_alive()) \ | 28 ((running_anim.is_sequence_alive()) \ |
| 29 ? function(running_anim.sequence()) \ | 29 ? function(running_anim.sequence()) \ |
| 30 : NULL) | 30 : NULL) |
| 31 | 31 |
| 32 namespace ui { | 32 namespace ui { |
| 33 | 33 |
| 34 class LayerAnimator; |
| 35 |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| 36 const int kDefaultTransitionDurationMs = 120; | 38 const int kDefaultTransitionDurationMs = 120; |
| 39 const int kTimerIntervalMs = 10; |
| 40 |
| 41 // Returns the AnimationContainer we're added to. |
| 42 gfx::AnimationContainer* GetAnimationContainer() { |
| 43 static gfx::AnimationContainer* container = NULL; |
| 44 if (!container) { |
| 45 container = new gfx::AnimationContainer(); |
| 46 container->AddRef(); |
| 47 } |
| 48 return container; |
| 49 } |
| 37 | 50 |
| 38 } // namespace | 51 } // namespace |
| 39 | 52 |
| 40 // LayerAnimator public -------------------------------------------------------- | 53 // LayerAnimator public -------------------------------------------------------- |
| 41 | 54 |
| 42 LayerAnimator::LayerAnimator(base::TimeDelta transition_duration) | 55 LayerAnimator::LayerAnimator(base::TimeDelta transition_duration) |
| 43 : delegate_(NULL), | 56 : delegate_(NULL), |
| 44 preemption_strategy_(IMMEDIATELY_SET_NEW_TARGET), | 57 preemption_strategy_(IMMEDIATELY_SET_NEW_TARGET), |
| 45 is_transition_duration_locked_(false), | 58 is_transition_duration_locked_(false), |
| 46 transition_duration_(transition_duration), | 59 transition_duration_(transition_duration), |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 ANIMATED_PROPERTY(bool, VISIBILITY, Visibility, bool, visibility); | 117 ANIMATED_PROPERTY(bool, VISIBILITY, Visibility, bool, visibility); |
| 105 ANIMATED_PROPERTY(float, BRIGHTNESS, Brightness, float, brightness); | 118 ANIMATED_PROPERTY(float, BRIGHTNESS, Brightness, float, brightness); |
| 106 ANIMATED_PROPERTY(float, GRAYSCALE, Grayscale, float, grayscale); | 119 ANIMATED_PROPERTY(float, GRAYSCALE, Grayscale, float, grayscale); |
| 107 ANIMATED_PROPERTY(SkColor, COLOR, Color, SkColor, color); | 120 ANIMATED_PROPERTY(SkColor, COLOR, Color, SkColor, color); |
| 108 | 121 |
| 109 base::TimeDelta LayerAnimator::GetTransitionDuration() const { | 122 base::TimeDelta LayerAnimator::GetTransitionDuration() const { |
| 110 return transition_duration_; | 123 return transition_duration_; |
| 111 } | 124 } |
| 112 | 125 |
| 113 void LayerAnimator::SetDelegate(LayerAnimationDelegate* delegate) { | 126 void LayerAnimator::SetDelegate(LayerAnimationDelegate* delegate) { |
| 114 if (delegate_ && is_started_) { | |
| 115 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); | |
| 116 if (collection) | |
| 117 collection->StopAnimator(this); | |
| 118 } | |
| 119 delegate_ = delegate; | 127 delegate_ = delegate; |
| 120 if (delegate_ && is_started_) { | |
| 121 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); | |
| 122 if (collection) | |
| 123 collection->StartAnimator(this); | |
| 124 } | |
| 125 } | 128 } |
| 126 | 129 |
| 127 void LayerAnimator::StartAnimation(LayerAnimationSequence* animation) { | 130 void LayerAnimator::StartAnimation(LayerAnimationSequence* animation) { |
| 128 scoped_refptr<LayerAnimator> retain(this); | 131 scoped_refptr<LayerAnimator> retain(this); |
| 129 OnScheduled(animation); | 132 OnScheduled(animation); |
| 130 if (!StartSequenceImmediately(animation)) { | 133 if (!StartSequenceImmediately(animation)) { |
| 131 // Attempt to preempt a running animation. | 134 // Attempt to preempt a running animation. |
| 132 switch (preemption_strategy_) { | 135 switch (preemption_strategy_) { |
| 133 case IMMEDIATELY_SET_NEW_TARGET: | 136 case IMMEDIATELY_SET_NEW_TARGET: |
| 134 ImmediatelySetNewTarget(animation); | 137 ImmediatelySetNewTarget(animation); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 if (preemption_strategy_ == IMMEDIATELY_SET_NEW_TARGET) { | 174 if (preemption_strategy_ == IMMEDIATELY_SET_NEW_TARGET) { |
| 172 std::vector<LayerAnimationSequence*>::const_iterator iter; | 175 std::vector<LayerAnimationSequence*>::const_iterator iter; |
| 173 for (iter = animations.begin(); iter != animations.end(); ++iter) { | 176 for (iter = animations.begin(); iter != animations.end(); ++iter) { |
| 174 StartAnimation(*iter); | 177 StartAnimation(*iter); |
| 175 } | 178 } |
| 176 return; | 179 return; |
| 177 } | 180 } |
| 178 | 181 |
| 179 adding_animations_ = true; | 182 adding_animations_ = true; |
| 180 if (!is_animating()) { | 183 if (!is_animating()) { |
| 181 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); | 184 if (GetAnimationContainer()->is_running()) |
| 182 if (collection && collection->HasActiveAnimators()) | 185 last_step_time_ = GetAnimationContainer()->last_tick_time(); |
| 183 last_step_time_ = collection->last_tick_time(); | |
| 184 else | 186 else |
| 185 last_step_time_ = gfx::FrameTime::Now(); | 187 last_step_time_ = gfx::FrameTime::Now(); |
| 186 } | 188 } |
| 187 | 189 |
| 188 // Collect all the affected properties. | 190 // Collect all the affected properties. |
| 189 LayerAnimationElement::AnimatableProperties animated_properties = | 191 LayerAnimationElement::AnimatableProperties animated_properties = |
| 190 LayerAnimationElement::UNKNOWN; | 192 LayerAnimationElement::UNKNOWN; |
| 191 | 193 |
| 192 std::vector<LayerAnimationSequence*>::const_iterator iter; | 194 std::vector<LayerAnimationSequence*>::const_iterator iter; |
| 193 for (iter = animations.begin(); iter != animations.end(); ++iter) | 195 for (iter = animations.begin(); iter != animations.end(); ++iter) |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 if (((*iter).sequence()->animation_group_id() == event.group_id) && | 336 if (((*iter).sequence()->animation_group_id() == event.group_id) && |
| 335 !(*iter).sequence()->IsFirstElementThreaded() && | 337 !(*iter).sequence()->IsFirstElementThreaded() && |
| 336 (*iter).sequence()->waiting_for_group_start()) { | 338 (*iter).sequence()->waiting_for_group_start()) { |
| 337 (*iter).sequence()->set_start_time(start_time); | 339 (*iter).sequence()->set_start_time(start_time); |
| 338 (*iter).sequence()->set_waiting_for_group_start(false); | 340 (*iter).sequence()->set_waiting_for_group_start(false); |
| 339 (*iter).sequence()->Start(delegate()); | 341 (*iter).sequence()->Start(delegate()); |
| 340 } | 342 } |
| 341 } | 343 } |
| 342 } | 344 } |
| 343 | 345 |
| 344 void LayerAnimator::AddToCollection(LayerAnimatorCollection* collection) { | |
| 345 if (is_animating() && !is_started_) { | |
| 346 collection->StartAnimator(this); | |
| 347 is_started_ = true; | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 void LayerAnimator::RemoveFromCollection(LayerAnimatorCollection* collection) { | |
| 352 if (is_animating() && is_started_) { | |
| 353 collection->StopAnimator(this); | |
| 354 is_started_ = false; | |
| 355 } | |
| 356 } | |
| 357 | |
| 358 // LayerAnimator protected ----------------------------------------------------- | 346 // LayerAnimator protected ----------------------------------------------------- |
| 359 | 347 |
| 360 void LayerAnimator::ProgressAnimation(LayerAnimationSequence* sequence, | 348 void LayerAnimator::ProgressAnimation(LayerAnimationSequence* sequence, |
| 361 base::TimeTicks now) { | 349 base::TimeTicks now) { |
| 362 if (!delegate() || sequence->waiting_for_group_start()) | 350 if (!delegate() || sequence->waiting_for_group_start()) |
| 363 return; | 351 return; |
| 364 | 352 |
| 365 sequence->Progress(now, delegate()); | 353 sequence->Progress(now, delegate()); |
| 366 } | 354 } |
| 367 | 355 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 continue; | 388 continue; |
| 401 | 389 |
| 402 if (running_animations_copy[i].sequence()->IsFinished(now)) { | 390 if (running_animations_copy[i].sequence()->IsFinished(now)) { |
| 403 SAFE_INVOKE_VOID(FinishAnimation, running_animations_copy[i], false); | 391 SAFE_INVOKE_VOID(FinishAnimation, running_animations_copy[i], false); |
| 404 } else { | 392 } else { |
| 405 SAFE_INVOKE_VOID(ProgressAnimation, running_animations_copy[i], now); | 393 SAFE_INVOKE_VOID(ProgressAnimation, running_animations_copy[i], now); |
| 406 } | 394 } |
| 407 } | 395 } |
| 408 } | 396 } |
| 409 | 397 |
| 398 void LayerAnimator::SetStartTime(base::TimeTicks start_time) { |
| 399 // Do nothing. |
| 400 } |
| 401 |
| 402 base::TimeDelta LayerAnimator::GetTimerInterval() const { |
| 403 return base::TimeDelta::FromMilliseconds(kTimerIntervalMs); |
| 404 } |
| 405 |
| 410 void LayerAnimator::StopAnimatingInternal(bool abort) { | 406 void LayerAnimator::StopAnimatingInternal(bool abort) { |
| 411 scoped_refptr<LayerAnimator> retain(this); | 407 scoped_refptr<LayerAnimator> retain(this); |
| 412 while (is_animating()) { | 408 while (is_animating()) { |
| 413 // We're going to attempt to finish the first running animation. Let's | 409 // We're going to attempt to finish the first running animation. Let's |
| 414 // ensure that it's valid. | 410 // ensure that it's valid. |
| 415 PurgeDeletedAnimations(); | 411 PurgeDeletedAnimations(); |
| 416 | 412 |
| 417 // If we've purged all running animations, attempt to start one up. | 413 // If we've purged all running animations, attempt to start one up. |
| 418 if (running_animations_.empty()) | 414 if (running_animations_.empty()) |
| 419 ProcessQueue(); | 415 ProcessQueue(); |
| 420 | 416 |
| 421 DCHECK(!running_animations_.empty()); | 417 DCHECK(!running_animations_.empty()); |
| 422 | 418 |
| 423 // Still no luck, let's just bail and clear all animations. | 419 // Still no luck, let's just bail and clear all animations. |
| 424 if (running_animations_.empty()) { | 420 if (running_animations_.empty()) { |
| 425 ClearAnimationsInternal(); | 421 ClearAnimationsInternal(); |
| 426 break; | 422 break; |
| 427 } | 423 } |
| 428 | 424 |
| 429 SAFE_INVOKE_VOID(FinishAnimation, running_animations_[0], abort); | 425 SAFE_INVOKE_VOID(FinishAnimation, running_animations_[0], abort); |
| 430 } | 426 } |
| 431 } | 427 } |
| 432 | 428 |
| 433 void LayerAnimator::UpdateAnimationState() { | 429 void LayerAnimator::UpdateAnimationState() { |
| 434 if (disable_timer_for_test_) | 430 if (disable_timer_for_test_) |
| 435 return; | 431 return; |
| 436 | 432 |
| 437 const bool should_start = is_animating(); | 433 const bool should_start = is_animating(); |
| 438 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); | 434 if (should_start && !is_started_) |
| 439 if (collection) { | 435 GetAnimationContainer()->Start(this); |
| 440 if (should_start && !is_started_) | 436 else if (!should_start && is_started_) |
| 441 collection->StartAnimator(this); | 437 GetAnimationContainer()->Stop(this); |
| 442 else if (!should_start && is_started_) | 438 |
| 443 collection->StopAnimator(this); | 439 is_started_ = should_start; |
| 444 is_started_ = should_start; | |
| 445 } else { | |
| 446 is_started_ = false; | |
| 447 } | |
| 448 } | 440 } |
| 449 | 441 |
| 450 LayerAnimationSequence* LayerAnimator::RemoveAnimation( | 442 LayerAnimationSequence* LayerAnimator::RemoveAnimation( |
| 451 LayerAnimationSequence* sequence) { | 443 LayerAnimationSequence* sequence) { |
| 452 linked_ptr<LayerAnimationSequence> to_return; | 444 linked_ptr<LayerAnimationSequence> to_return; |
| 453 | 445 |
| 454 bool is_running = false; | 446 bool is_running = false; |
| 455 | 447 |
| 456 // First remove from running animations | 448 // First remove from running animations |
| 457 for (RunningAnimations::iterator iter = running_animations_.begin(); | 449 for (RunningAnimations::iterator iter = running_animations_.begin(); |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 754 for (RunningAnimations::const_iterator iter = running_animations_.begin(); | 746 for (RunningAnimations::const_iterator iter = running_animations_.begin(); |
| 755 iter != running_animations_.end(); ++iter) { | 747 iter != running_animations_.end(); ++iter) { |
| 756 if ((*iter).sequence()->HasConflictingProperty(sequence->properties())) | 748 if ((*iter).sequence()->HasConflictingProperty(sequence->properties())) |
| 757 return false; | 749 return false; |
| 758 } | 750 } |
| 759 | 751 |
| 760 // All clear, actually start the sequence. Note: base::TimeTicks::Now has | 752 // All clear, actually start the sequence. Note: base::TimeTicks::Now has |
| 761 // a resolution that can be as bad as 15ms. If this causes glitches in the | 753 // a resolution that can be as bad as 15ms. If this causes glitches in the |
| 762 // animations, this can be switched to HighResNow() (animation uses Now() | 754 // animations, this can be switched to HighResNow() (animation uses Now() |
| 763 // internally). | 755 // internally). |
| 764 // All LayerAnimators share the same LayerAnimatorCollection. Use the | 756 // All LayerAnimators share the same AnimationContainer. Use the |
| 765 // last_tick_time() from there to ensure animations started during the same | 757 // last_tick_time() from there to ensure animations started during the same |
| 766 // event complete at the same time. | 758 // event complete at the same time. |
| 767 base::TimeTicks start_time; | 759 base::TimeTicks start_time; |
| 768 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); | |
| 769 if (is_animating() || adding_animations_) | 760 if (is_animating() || adding_animations_) |
| 770 start_time = last_step_time_; | 761 start_time = last_step_time_; |
| 771 else if (collection && collection->HasActiveAnimators()) | 762 else if (GetAnimationContainer()->is_running()) |
| 772 start_time = collection->last_tick_time(); | 763 start_time = GetAnimationContainer()->last_tick_time(); |
| 773 else | 764 else |
| 774 start_time = gfx::FrameTime::Now(); | 765 start_time = gfx::FrameTime::Now(); |
| 775 | 766 |
| 776 if (!sequence->animation_group_id()) | 767 if (!sequence->animation_group_id()) |
| 777 sequence->set_animation_group_id(cc::AnimationIdProvider::NextGroupId()); | 768 sequence->set_animation_group_id(cc::AnimationIdProvider::NextGroupId()); |
| 778 if (!sequence->waiting_for_group_start() || | 769 if (!sequence->waiting_for_group_start() || |
| 779 sequence->IsFirstElementThreaded()) { | 770 sequence->IsFirstElementThreaded()) { |
| 780 sequence->set_start_time(start_time); | 771 sequence->set_start_time(start_time); |
| 781 sequence->Start(delegate()); | 772 sequence->Start(delegate()); |
| 782 } | 773 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 841 | 832 |
| 842 void LayerAnimator::PurgeDeletedAnimations() { | 833 void LayerAnimator::PurgeDeletedAnimations() { |
| 843 for (size_t i = 0; i < running_animations_.size();) { | 834 for (size_t i = 0; i < running_animations_.size();) { |
| 844 if (!running_animations_[i].is_sequence_alive()) | 835 if (!running_animations_[i].is_sequence_alive()) |
| 845 running_animations_.erase(running_animations_.begin() + i); | 836 running_animations_.erase(running_animations_.begin() + i); |
| 846 else | 837 else |
| 847 i++; | 838 i++; |
| 848 } | 839 } |
| 849 } | 840 } |
| 850 | 841 |
| 851 LayerAnimatorCollection* LayerAnimator::GetLayerAnimatorCollection() { | |
| 852 return delegate_ ? delegate_->GetLayerAnimatorCollection() : NULL; | |
| 853 } | |
| 854 | |
| 855 LayerAnimator::RunningAnimation::RunningAnimation( | 842 LayerAnimator::RunningAnimation::RunningAnimation( |
| 856 const base::WeakPtr<LayerAnimationSequence>& sequence) | 843 const base::WeakPtr<LayerAnimationSequence>& sequence) |
| 857 : sequence_(sequence) { | 844 : sequence_(sequence) { |
| 858 } | 845 } |
| 859 | 846 |
| 860 LayerAnimator::RunningAnimation::~RunningAnimation() { } | 847 LayerAnimator::RunningAnimation::~RunningAnimation() { } |
| 861 | 848 |
| 862 } // namespace ui | 849 } // namespace ui |
| OLD | NEW |