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/gfx/animation/animation_container.h" | 17 #include "ui/compositor/layer_animator_collection.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 | |
36 namespace { | 34 namespace { |
37 | 35 |
38 const int kDefaultTransitionDurationMs = 120; | 36 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 } | |
50 | 37 |
51 } // namespace | 38 } // namespace |
52 | 39 |
53 // LayerAnimator public -------------------------------------------------------- | 40 // LayerAnimator public -------------------------------------------------------- |
54 | 41 |
55 LayerAnimator::LayerAnimator(base::TimeDelta transition_duration) | 42 LayerAnimator::LayerAnimator(base::TimeDelta transition_duration) |
56 : delegate_(NULL), | 43 : delegate_(NULL), |
57 preemption_strategy_(IMMEDIATELY_SET_NEW_TARGET), | 44 preemption_strategy_(IMMEDIATELY_SET_NEW_TARGET), |
58 is_transition_duration_locked_(false), | 45 is_transition_duration_locked_(false), |
59 transition_duration_(transition_duration), | 46 transition_duration_(transition_duration), |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 if (preemption_strategy_ == IMMEDIATELY_SET_NEW_TARGET) { | 161 if (preemption_strategy_ == IMMEDIATELY_SET_NEW_TARGET) { |
175 std::vector<LayerAnimationSequence*>::const_iterator iter; | 162 std::vector<LayerAnimationSequence*>::const_iterator iter; |
176 for (iter = animations.begin(); iter != animations.end(); ++iter) { | 163 for (iter = animations.begin(); iter != animations.end(); ++iter) { |
177 StartAnimation(*iter); | 164 StartAnimation(*iter); |
178 } | 165 } |
179 return; | 166 return; |
180 } | 167 } |
181 | 168 |
182 adding_animations_ = true; | 169 adding_animations_ = true; |
183 if (!is_animating()) { | 170 if (!is_animating()) { |
184 if (GetAnimationContainer()->is_running()) | 171 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
185 last_step_time_ = GetAnimationContainer()->last_tick_time(); | 172 if (collection && collection->HasActiveAnimators()) |
| 173 last_step_time_ = collection->last_tick_time(); |
186 else | 174 else |
187 last_step_time_ = gfx::FrameTime::Now(); | 175 last_step_time_ = gfx::FrameTime::Now(); |
188 } | 176 } |
189 | 177 |
190 // Collect all the affected properties. | 178 // Collect all the affected properties. |
191 LayerAnimationElement::AnimatableProperties animated_properties = | 179 LayerAnimationElement::AnimatableProperties animated_properties = |
192 LayerAnimationElement::UNKNOWN; | 180 LayerAnimationElement::UNKNOWN; |
193 | 181 |
194 std::vector<LayerAnimationSequence*>::const_iterator iter; | 182 std::vector<LayerAnimationSequence*>::const_iterator iter; |
195 for (iter = animations.begin(); iter != animations.end(); ++iter) | 183 for (iter = animations.begin(); iter != animations.end(); ++iter) |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 continue; | 376 continue; |
389 | 377 |
390 if (running_animations_copy[i].sequence()->IsFinished(now)) { | 378 if (running_animations_copy[i].sequence()->IsFinished(now)) { |
391 SAFE_INVOKE_VOID(FinishAnimation, running_animations_copy[i], false); | 379 SAFE_INVOKE_VOID(FinishAnimation, running_animations_copy[i], false); |
392 } else { | 380 } else { |
393 SAFE_INVOKE_VOID(ProgressAnimation, running_animations_copy[i], now); | 381 SAFE_INVOKE_VOID(ProgressAnimation, running_animations_copy[i], now); |
394 } | 382 } |
395 } | 383 } |
396 } | 384 } |
397 | 385 |
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 | |
406 void LayerAnimator::StopAnimatingInternal(bool abort) { | 386 void LayerAnimator::StopAnimatingInternal(bool abort) { |
407 scoped_refptr<LayerAnimator> retain(this); | 387 scoped_refptr<LayerAnimator> retain(this); |
408 while (is_animating()) { | 388 while (is_animating()) { |
409 // We're going to attempt to finish the first running animation. Let's | 389 // We're going to attempt to finish the first running animation. Let's |
410 // ensure that it's valid. | 390 // ensure that it's valid. |
411 PurgeDeletedAnimations(); | 391 PurgeDeletedAnimations(); |
412 | 392 |
413 // If we've purged all running animations, attempt to start one up. | 393 // If we've purged all running animations, attempt to start one up. |
414 if (running_animations_.empty()) | 394 if (running_animations_.empty()) |
415 ProcessQueue(); | 395 ProcessQueue(); |
416 | 396 |
417 DCHECK(!running_animations_.empty()); | 397 DCHECK(!running_animations_.empty()); |
418 | 398 |
419 // Still no luck, let's just bail and clear all animations. | 399 // Still no luck, let's just bail and clear all animations. |
420 if (running_animations_.empty()) { | 400 if (running_animations_.empty()) { |
421 ClearAnimationsInternal(); | 401 ClearAnimationsInternal(); |
422 break; | 402 break; |
423 } | 403 } |
424 | 404 |
425 SAFE_INVOKE_VOID(FinishAnimation, running_animations_[0], abort); | 405 SAFE_INVOKE_VOID(FinishAnimation, running_animations_[0], abort); |
426 } | 406 } |
427 } | 407 } |
428 | 408 |
429 void LayerAnimator::UpdateAnimationState() { | 409 void LayerAnimator::UpdateAnimationState() { |
430 if (disable_timer_for_test_) | 410 if (disable_timer_for_test_) |
431 return; | 411 return; |
432 | 412 |
433 const bool should_start = is_animating(); | 413 const bool should_start = is_animating(); |
434 if (should_start && !is_started_) | 414 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
435 GetAnimationContainer()->Start(this); | 415 if (collection) { |
436 else if (!should_start && is_started_) | 416 if (should_start && !is_started_) |
437 GetAnimationContainer()->Stop(this); | 417 collection->StartAnimator(this); |
438 | 418 else if (!should_start && is_started_) |
439 is_started_ = should_start; | 419 collection->StopAnimator(this); |
| 420 is_started_ = should_start; |
| 421 } else { |
| 422 is_started_ = false; |
| 423 } |
440 } | 424 } |
441 | 425 |
442 LayerAnimationSequence* LayerAnimator::RemoveAnimation( | 426 LayerAnimationSequence* LayerAnimator::RemoveAnimation( |
443 LayerAnimationSequence* sequence) { | 427 LayerAnimationSequence* sequence) { |
444 linked_ptr<LayerAnimationSequence> to_return; | 428 linked_ptr<LayerAnimationSequence> to_return; |
445 | 429 |
446 bool is_running = false; | 430 bool is_running = false; |
447 | 431 |
448 // First remove from running animations | 432 // First remove from running animations |
449 for (RunningAnimations::iterator iter = running_animations_.begin(); | 433 for (RunningAnimations::iterator iter = running_animations_.begin(); |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
746 for (RunningAnimations::const_iterator iter = running_animations_.begin(); | 730 for (RunningAnimations::const_iterator iter = running_animations_.begin(); |
747 iter != running_animations_.end(); ++iter) { | 731 iter != running_animations_.end(); ++iter) { |
748 if ((*iter).sequence()->HasConflictingProperty(sequence->properties())) | 732 if ((*iter).sequence()->HasConflictingProperty(sequence->properties())) |
749 return false; | 733 return false; |
750 } | 734 } |
751 | 735 |
752 // All clear, actually start the sequence. Note: base::TimeTicks::Now has | 736 // All clear, actually start the sequence. Note: base::TimeTicks::Now has |
753 // a resolution that can be as bad as 15ms. If this causes glitches in the | 737 // a resolution that can be as bad as 15ms. If this causes glitches in the |
754 // animations, this can be switched to HighResNow() (animation uses Now() | 738 // animations, this can be switched to HighResNow() (animation uses Now() |
755 // internally). | 739 // internally). |
756 // All LayerAnimators share the same AnimationContainer. Use the | 740 // All LayerAnimators share the same LayerAnimatorCollection. Use the |
757 // last_tick_time() from there to ensure animations started during the same | 741 // last_tick_time() from there to ensure animations started during the same |
758 // event complete at the same time. | 742 // event complete at the same time. |
759 base::TimeTicks start_time; | 743 base::TimeTicks start_time; |
| 744 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
760 if (is_animating() || adding_animations_) | 745 if (is_animating() || adding_animations_) |
761 start_time = last_step_time_; | 746 start_time = last_step_time_; |
762 else if (GetAnimationContainer()->is_running()) | 747 else if (collection && collection->HasActiveAnimators()) |
763 start_time = GetAnimationContainer()->last_tick_time(); | 748 start_time = collection->last_tick_time(); |
764 else | 749 else |
765 start_time = gfx::FrameTime::Now(); | 750 start_time = gfx::FrameTime::Now(); |
766 | 751 |
767 if (!sequence->animation_group_id()) | 752 if (!sequence->animation_group_id()) |
768 sequence->set_animation_group_id(cc::AnimationIdProvider::NextGroupId()); | 753 sequence->set_animation_group_id(cc::AnimationIdProvider::NextGroupId()); |
769 if (!sequence->waiting_for_group_start() || | 754 if (!sequence->waiting_for_group_start() || |
770 sequence->IsFirstElementThreaded()) { | 755 sequence->IsFirstElementThreaded()) { |
771 sequence->set_start_time(start_time); | 756 sequence->set_start_time(start_time); |
772 sequence->Start(delegate()); | 757 sequence->Start(delegate()); |
773 } | 758 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
832 | 817 |
833 void LayerAnimator::PurgeDeletedAnimations() { | 818 void LayerAnimator::PurgeDeletedAnimations() { |
834 for (size_t i = 0; i < running_animations_.size();) { | 819 for (size_t i = 0; i < running_animations_.size();) { |
835 if (!running_animations_[i].is_sequence_alive()) | 820 if (!running_animations_[i].is_sequence_alive()) |
836 running_animations_.erase(running_animations_.begin() + i); | 821 running_animations_.erase(running_animations_.begin() + i); |
837 else | 822 else |
838 i++; | 823 i++; |
839 } | 824 } |
840 } | 825 } |
841 | 826 |
| 827 LayerAnimatorCollection* LayerAnimator::GetLayerAnimatorCollection() { |
| 828 return delegate_ ? delegate_->GetLayerAnimatorCollection() : NULL; |
| 829 } |
| 830 |
842 LayerAnimator::RunningAnimation::RunningAnimation( | 831 LayerAnimator::RunningAnimation::RunningAnimation( |
843 const base::WeakPtr<LayerAnimationSequence>& sequence) | 832 const base::WeakPtr<LayerAnimationSequence>& sequence) |
844 : sequence_(sequence) { | 833 : sequence_(sequence) { |
845 } | 834 } |
846 | 835 |
847 LayerAnimator::RunningAnimation::~RunningAnimation() { } | 836 LayerAnimator::RunningAnimation::~RunningAnimation() { } |
848 | 837 |
849 } // namespace ui | 838 } // namespace ui |
OLD | NEW |