Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: ui/compositor/layer_animator.cc

Issue 11316135: NOT READY FOR REVIEW - Add support for custom animation timers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/compositor/layer_animator.h ('k') | ui/compositor/test/test_layer_animation_timer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "ui/base/animation/animation_container.h" 10 #include "ui/base/animation/animation_container.h"
11 #include "ui/compositor/compositor.h" 11 #include "ui/compositor/compositor.h"
12 #include "ui/compositor/layer.h" 12 #include "ui/compositor/layer.h"
13 #include "ui/compositor/layer_animation_delegate.h" 13 #include "ui/compositor/layer_animation_delegate.h"
14 #include "ui/compositor/layer_animation_observer.h" 14 #include "ui/compositor/layer_animation_observer.h"
15 #include "ui/compositor/layer_animation_sequence.h" 15 #include "ui/compositor/layer_animation_sequence.h"
16 #include "ui/compositor/layer_animation_timer.h"
16 17
17 #define SAFE_INVOKE_VOID(function, running_anim, ...) \ 18 #define SAFE_INVOKE_VOID(function, running_anim, ...) \
18 if (running_anim.is_sequence_alive()) \ 19 if (running_anim.is_sequence_alive()) \
19 function(running_anim.sequence(), ##__VA_ARGS__) 20 function(running_anim.sequence(), ##__VA_ARGS__)
20 #define SAFE_INVOKE_BOOL(function, running_anim) \ 21 #define SAFE_INVOKE_BOOL(function, running_anim) \
21 ((running_anim.is_sequence_alive()) \ 22 ((running_anim.is_sequence_alive()) \
22 ? function(running_anim.sequence()) \ 23 ? function(running_anim.sequence()) \
23 : false) 24 : false)
24 #define SAFE_INVOKE_PTR(function, running_anim) \ 25 #define SAFE_INVOKE_PTR(function, running_anim) \
25 ((running_anim.is_sequence_alive()) \ 26 ((running_anim.is_sequence_alive()) \
26 ? function(running_anim.sequence()) \ 27 ? function(running_anim.sequence()) \
27 : NULL) 28 : NULL)
28 29
29 namespace ui { 30 namespace ui {
30 31
31 class LayerAnimator; 32 class LayerAnimator;
32 33
33 namespace { 34 namespace {
34 35
35 static const base::TimeDelta kDefaultTransitionDuration = 36 static const base::TimeDelta kDefaultTransitionDuration =
36 base::TimeDelta::FromMilliseconds(120); 37 base::TimeDelta::FromMilliseconds(120);
37 38
38 static const base::TimeDelta kTimerInterval = 39 static const base::TimeDelta kTimerInterval =
39 base::TimeDelta::FromMilliseconds(10); 40 base::TimeDelta::FromMilliseconds(10);
40 41
41 // Returns the AnimationContainer we're added to. 42 // The default timer is simply a thin wrapper around an AnimationContainer.
42 ui::AnimationContainer* GetAnimationContainer() { 43 class DefaultLayerAnimationTimer : public LayerAnimationTimer {
43 static ui::AnimationContainer* container = NULL; 44 public:
44 if (!container) { 45 DefaultLayerAnimationTimer() : container_(new AnimationContainer()) {
45 container = new AnimationContainer();
46 container->AddRef();
47 } 46 }
48 return container; 47
48 private:
49 // LayerAnimationTimer implementation.
50 virtual void Start(AnimationContainerElement* element) {
51 container_->Start(element);
52 }
53
54 virtual void Stop(AnimationContainerElement* element) {
55 container_->Stop(element);
56 }
57
58 virtual base::TimeTicks last_tick_time() const {
59 return container_->last_tick_time();
60 }
61
62 virtual bool is_running() const {
63 return container_->is_running();
64 }
65
66 scoped_refptr<AnimationContainer> container_;
67
68 DISALLOW_COPY_AND_ASSIGN(DefaultLayerAnimationTimer);
69 };
70
71 static LayerAnimationTimer* s_timer = NULL;
72
73 // Returns the LayerAnimationTimer we're added to.
74 static LayerAnimationTimer* GetAnimationTimer() {
75 if (!s_timer)
76 s_timer = new DefaultLayerAnimationTimer();
77 return s_timer;
49 } 78 }
50 79
51 } // namespace 80 } // namespace
52 81
53 // static 82 // static
54 bool LayerAnimator::disable_animations_for_test_ = false; 83 bool LayerAnimator::disable_animations_for_test_ = false;
55 // static 84 // static
56 bool LayerAnimator::slow_animation_mode_ = false; 85 bool LayerAnimator::slow_animation_mode_ = false;
57 // static 86 // static
58 int LayerAnimator::slow_animation_scale_factor_ = 4; 87 int LayerAnimator::slow_animation_scale_factor_ = 4;
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 if (preemption_strategy_ == IMMEDIATELY_SET_NEW_TARGET) { 203 if (preemption_strategy_ == IMMEDIATELY_SET_NEW_TARGET) {
175 std::vector<LayerAnimationSequence*>::const_iterator iter; 204 std::vector<LayerAnimationSequence*>::const_iterator iter;
176 for (iter = animations.begin(); iter != animations.end(); ++iter) { 205 for (iter = animations.begin(); iter != animations.end(); ++iter) {
177 StartAnimation(*iter); 206 StartAnimation(*iter);
178 } 207 }
179 return; 208 return;
180 } 209 }
181 210
182 adding_animations_ = true; 211 adding_animations_ = true;
183 if (!is_animating()) { 212 if (!is_animating()) {
184 if (GetAnimationContainer()->is_running()) 213 if (GetAnimationTimer()->is_running())
185 last_step_time_ = GetAnimationContainer()->last_tick_time(); 214 last_step_time_ = GetAnimationTimer()->last_tick_time();
186 else 215 else
187 last_step_time_ = base::TimeTicks::Now(); 216 last_step_time_ = base::TimeTicks::Now();
188 } 217 }
189 218
190 // Collect all the affected properties. 219 // Collect all the affected properties.
191 LayerAnimationElement::AnimatableProperties animated_properties; 220 LayerAnimationElement::AnimatableProperties animated_properties;
192 std::vector<LayerAnimationSequence*>::const_iterator iter; 221 std::vector<LayerAnimationSequence*>::const_iterator iter;
193 for (iter = animations.begin(); iter != animations.end(); ++iter) { 222 for (iter = animations.begin(); iter != animations.end(); ++iter) {
194 animated_properties.insert((*iter)->properties().begin(), 223 animated_properties.insert((*iter)->properties().begin(),
195 (*iter)->properties().end()); 224 (*iter)->properties().end());
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 345
317 void LayerAnimator::RemoveObserver(LayerAnimationObserver* observer) { 346 void LayerAnimator::RemoveObserver(LayerAnimationObserver* observer) {
318 observers_.RemoveObserver(observer); 347 observers_.RemoveObserver(observer);
319 // Remove the observer from all sequences as well. 348 // Remove the observer from all sequences as well.
320 for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); 349 for (AnimationQueue::iterator queue_iter = animation_queue_.begin();
321 queue_iter != animation_queue_.end(); ++queue_iter) { 350 queue_iter != animation_queue_.end(); ++queue_iter) {
322 (*queue_iter)->RemoveObserver(observer); 351 (*queue_iter)->RemoveObserver(observer);
323 } 352 }
324 } 353 }
325 354
355 // static
356 void LayerAnimator::SetAnimationTimer(LayerAnimationTimer* timer) {
357 if (s_timer && s_timer != timer)
358 delete s_timer;
359 s_timer = timer;
360 }
361
326 // LayerAnimator protected ----------------------------------------------------- 362 // LayerAnimator protected -----------------------------------------------------
327 363
328 void LayerAnimator::ProgressAnimation(LayerAnimationSequence* sequence, 364 void LayerAnimator::ProgressAnimation(LayerAnimationSequence* sequence,
329 base::TimeDelta delta) { 365 base::TimeDelta delta) {
330 if (!delegate()) 366 if (!delegate())
331 return; 367 return;
332 368
333 sequence->Progress(delta, delegate()); 369 sequence->Progress(delta, delegate());
334 } 370 }
335 371
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 base::TimeDelta LayerAnimator::GetTimerInterval() const { 417 base::TimeDelta LayerAnimator::GetTimerInterval() const {
382 return kTimerInterval; 418 return kTimerInterval;
383 } 419 }
384 420
385 void LayerAnimator::UpdateAnimationState() { 421 void LayerAnimator::UpdateAnimationState() {
386 if (disable_timer_for_test_) 422 if (disable_timer_for_test_)
387 return; 423 return;
388 424
389 const bool should_start = is_animating(); 425 const bool should_start = is_animating();
390 if (should_start && !is_started_) 426 if (should_start && !is_started_)
391 GetAnimationContainer()->Start(this); 427 GetAnimationTimer()->Start(this);
392 else if (!should_start && is_started_) 428 else if (!should_start && is_started_)
393 GetAnimationContainer()->Stop(this); 429 GetAnimationTimer()->Stop(this);
394 430
395 is_started_ = should_start; 431 is_started_ = should_start;
396 } 432 }
397 433
398 LayerAnimationSequence* LayerAnimator::RemoveAnimation( 434 LayerAnimationSequence* LayerAnimator::RemoveAnimation(
399 LayerAnimationSequence* sequence) { 435 LayerAnimationSequence* sequence) {
400 linked_ptr<LayerAnimationSequence> to_return; 436 linked_ptr<LayerAnimationSequence> to_return;
401 437
402 // First remove from running animations 438 // First remove from running animations
403 for (RunningAnimations::iterator iter = running_animations_.begin(); 439 for (RunningAnimations::iterator iter = running_animations_.begin();
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 // All clear, actually start the sequence. Note: base::TimeTicks::Now has 705 // All clear, actually start the sequence. Note: base::TimeTicks::Now has
670 // a resolution that can be as bad as 15ms. If this causes glitches in the 706 // a resolution that can be as bad as 15ms. If this causes glitches in the
671 // animations, this can be switched to HighResNow() (animation uses Now() 707 // animations, this can be switched to HighResNow() (animation uses Now()
672 // internally). 708 // internally).
673 // All LayerAnimators share the same AnimationContainer. Use the 709 // All LayerAnimators share the same AnimationContainer. Use the
674 // last_tick_time() from there to ensure animations started during the same 710 // last_tick_time() from there to ensure animations started during the same
675 // event complete at the same time. 711 // event complete at the same time.
676 base::TimeTicks start_time; 712 base::TimeTicks start_time;
677 if (is_animating() || adding_animations_) 713 if (is_animating() || adding_animations_)
678 start_time = last_step_time_; 714 start_time = last_step_time_;
679 else if (GetAnimationContainer()->is_running()) 715 else if (GetAnimationTimer()->is_running())
680 start_time = GetAnimationContainer()->last_tick_time(); 716 start_time = GetAnimationTimer()->last_tick_time();
681 else 717 else
682 start_time = base::TimeTicks::Now(); 718 start_time = base::TimeTicks::Now();
683 719
684 running_animations_.push_back( 720 running_animations_.push_back(
685 RunningAnimation(sequence->AsWeakPtr(), start_time)); 721 RunningAnimation(sequence->AsWeakPtr(), start_time));
686 722
687 // Need to keep a reference to the animation. 723 // Need to keep a reference to the animation.
688 AddToQueueIfNotPresent(sequence); 724 AddToQueueIfNotPresent(sequence);
689 725
690 // Ensure that animations get stepped at their start time. 726 // Ensure that animations get stepped at their start time.
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
750 LayerAnimator::RunningAnimation::RunningAnimation( 786 LayerAnimator::RunningAnimation::RunningAnimation(
751 const base::WeakPtr<LayerAnimationSequence>& sequence, 787 const base::WeakPtr<LayerAnimationSequence>& sequence,
752 base::TimeTicks start_time) 788 base::TimeTicks start_time)
753 : sequence_(sequence), 789 : sequence_(sequence),
754 start_time_(start_time) { 790 start_time_(start_time) {
755 } 791 }
756 792
757 LayerAnimator::RunningAnimation::~RunningAnimation() { } 793 LayerAnimator::RunningAnimation::~RunningAnimation() { }
758 794
759 } // namespace ui 795 } // namespace ui
OLDNEW
« no previous file with comments | « ui/compositor/layer_animator.h ('k') | ui/compositor/test/test_layer_animation_timer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698