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

Side by Side Diff: cc/animation/layer_animation_controller.cc

Issue 231133002: CC::Animations should use TimeTicks & TimeDelta to represent time (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebasing TOT. Created 6 years, 8 months 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
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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 "cc/animation/layer_animation_controller.h" 5 #include "cc/animation/layer_animation_controller.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "cc/animation/animation.h" 9 #include "cc/animation/animation.h"
10 #include "cc/animation/animation_delegate.h" 10 #include "cc/animation/animation_delegate.h"
11 #include "cc/animation/animation_registrar.h" 11 #include "cc/animation/animation_registrar.h"
12 #include "cc/animation/keyframed_animation_curve.h" 12 #include "cc/animation/keyframed_animation_curve.h"
13 #include "cc/animation/layer_animation_value_observer.h" 13 #include "cc/animation/layer_animation_value_observer.h"
14 #include "cc/animation/layer_animation_value_provider.h" 14 #include "cc/animation/layer_animation_value_provider.h"
15 #include "cc/animation/scroll_offset_animation_curve.h" 15 #include "cc/animation/scroll_offset_animation_curve.h"
16 #include "cc/base/scoped_ptr_algorithm.h" 16 #include "cc/base/scoped_ptr_algorithm.h"
17 #include "cc/output/filter_operations.h" 17 #include "cc/output/filter_operations.h"
18 #include "ui/gfx/box_f.h" 18 #include "ui/gfx/box_f.h"
19 #include "ui/gfx/transform.h" 19 #include "ui/gfx/transform.h"
20 20
21 namespace cc { 21 namespace cc {
22 22
23 static double TimeTicksToDoubleTime(base::TimeTicks time) {
24 return (time - base::TimeTicks()).InSecondsF();
25 }
26
23 LayerAnimationController::LayerAnimationController(int id) 27 LayerAnimationController::LayerAnimationController(int id)
24 : registrar_(0), 28 : registrar_(0),
25 id_(id), 29 id_(id),
26 is_active_(false), 30 is_active_(false),
27 last_tick_time_(0), 31 last_tick_time_(base::TimeTicks()),
28 value_provider_(NULL), 32 value_provider_(NULL),
29 layer_animation_delegate_(NULL) {} 33 layer_animation_delegate_(NULL) {}
30 34
31 LayerAnimationController::~LayerAnimationController() { 35 LayerAnimationController::~LayerAnimationController() {
32 if (registrar_) 36 if (registrar_)
33 registrar_->UnregisterAnimationController(this); 37 registrar_->UnregisterAnimationController(this);
34 } 38 }
35 39
36 scoped_refptr<LayerAnimationController> LayerAnimationController::Create( 40 scoped_refptr<LayerAnimationController> LayerAnimationController::Create(
37 int id) { 41 int id) {
38 return make_scoped_refptr(new LayerAnimationController(id)); 42 return make_scoped_refptr(new LayerAnimationController(id));
39 } 43 }
40 44
41 void LayerAnimationController::PauseAnimation(int animation_id, 45 void LayerAnimationController::PauseAnimation(int animation_id,
42 double time_offset) { 46 double time_offset) {
43 for (size_t i = 0; i < active_animations_.size(); ++i) { 47 for (size_t i = 0; i < active_animations_.size(); ++i) {
44 if (active_animations_[i]->id() == animation_id) { 48 if (active_animations_[i]->id() == animation_id) {
49 // TODO(sikugu): http://crbug.com/178171 - Remove double
50 // and use base::TimeTicks/TimeDelta instead all over the file.
51 // Remove usage of TimeTicksToDoubleTime.
45 active_animations_[i]->SetRunState( 52 active_animations_[i]->SetRunState(
46 Animation::Paused, time_offset + active_animations_[i]->start_time()); 53 Animation::Paused, time_offset + active_animations_[i]->start_time());
47 } 54 }
48 } 55 }
49 } 56 }
50 57
51 struct HasAnimationId { 58 struct HasAnimationId {
52 explicit HasAnimationId(int id) : id_(id) {} 59 explicit HasAnimationId(int id) : id_(id) {}
53 bool operator()(Animation* animation) const { 60 bool operator()(Animation* animation) const {
54 return animation->id() == id_; 61 return animation->id() == id_;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 HasAnimationIdAndProperty(animation_id, target_property)), 99 HasAnimationIdAndProperty(animation_id, target_property)),
93 animations.end()); 100 animations.end());
94 UpdateActivation(NormalActivation); 101 UpdateActivation(NormalActivation);
95 } 102 }
96 103
97 void LayerAnimationController::AbortAnimations( 104 void LayerAnimationController::AbortAnimations(
98 Animation::TargetProperty target_property) { 105 Animation::TargetProperty target_property) {
99 for (size_t i = 0; i < active_animations_.size(); ++i) { 106 for (size_t i = 0; i < active_animations_.size(); ++i) {
100 if (active_animations_[i]->target_property() == target_property && 107 if (active_animations_[i]->target_property() == target_property &&
101 !active_animations_[i]->is_finished()) 108 !active_animations_[i]->is_finished())
102 active_animations_[i]->SetRunState(Animation::Aborted, last_tick_time_); 109 active_animations_[i]->SetRunState(
110 Animation::Aborted, TimeTicksToDoubleTime(last_tick_time_));
103 } 111 }
104 } 112 }
105 113
106 // Ensures that the list of active animations on the main thread and the impl 114 // Ensures that the list of active animations on the main thread and the impl
107 // thread are kept in sync. 115 // thread are kept in sync.
108 void LayerAnimationController::PushAnimationUpdatesTo( 116 void LayerAnimationController::PushAnimationUpdatesTo(
109 LayerAnimationController* controller_impl) { 117 LayerAnimationController* controller_impl) {
110 DCHECK(this != controller_impl); 118 DCHECK(this != controller_impl);
111 if (!has_any_animation() && !controller_impl->has_any_animation()) 119 if (!has_any_animation() && !controller_impl->has_any_animation())
112 return; 120 return;
113 PurgeAnimationsMarkedForDeletion(); 121 PurgeAnimationsMarkedForDeletion();
114 PushNewAnimationsToImplThread(controller_impl); 122 PushNewAnimationsToImplThread(controller_impl);
115 123
116 // Remove finished impl side animations only after pushing, 124 // Remove finished impl side animations only after pushing,
117 // and only after the animations are deleted on the main thread 125 // and only after the animations are deleted on the main thread
118 // this insures we will never push an animation twice. 126 // this insures we will never push an animation twice.
119 RemoveAnimationsCompletedOnMainThread(controller_impl); 127 RemoveAnimationsCompletedOnMainThread(controller_impl);
120 128
121 PushPropertiesToImplThread(controller_impl); 129 PushPropertiesToImplThread(controller_impl);
122 controller_impl->UpdateActivation(NormalActivation); 130 controller_impl->UpdateActivation(NormalActivation);
123 UpdateActivation(NormalActivation); 131 UpdateActivation(NormalActivation);
124 } 132 }
125 133
126 void LayerAnimationController::Animate(double monotonic_time) { 134 void LayerAnimationController::Animate(base::TimeTicks monotonic_time) {
127 DCHECK(monotonic_time); 135 DCHECK(monotonic_time != base::TimeTicks());
128 if (!HasValueObserver()) 136 if (!HasValueObserver())
129 return; 137 return;
130 138
131 StartAnimations(monotonic_time); 139 StartAnimations(monotonic_time);
132 TickAnimations(monotonic_time); 140 TickAnimations(monotonic_time);
133 last_tick_time_ = monotonic_time; 141 last_tick_time_ = monotonic_time;
134 } 142 }
135 143
136 void LayerAnimationController::AccumulatePropertyUpdates( 144 void LayerAnimationController::AccumulatePropertyUpdates(
137 double monotonic_time, 145 base::TimeTicks time,
138 AnimationEventsVector* events) { 146 AnimationEventsVector* events) {
147 double monotonic_time = TimeTicksToDoubleTime(time);
139 if (!events) 148 if (!events)
140 return; 149 return;
141 150
142 for (size_t i = 0; i < active_animations_.size(); ++i) { 151 for (size_t i = 0; i < active_animations_.size(); ++i) {
143 Animation* animation = active_animations_[i]; 152 Animation* animation = active_animations_[i];
144 if (!animation->is_impl_only()) 153 if (!animation->is_impl_only())
145 continue; 154 continue;
146 155
147 double trimmed = animation->TrimTimeToCurrentIteration(monotonic_time); 156 double trimmed = animation->TrimTimeToCurrentIteration(monotonic_time);
148 switch (animation->target_property()) { 157 switch (animation->target_property()) {
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 void LayerAnimationController::NotifyAnimationStarted( 291 void LayerAnimationController::NotifyAnimationStarted(
283 const AnimationEvent& event) { 292 const AnimationEvent& event) {
284 base::TimeTicks monotonic_time = base::TimeTicks::FromInternalValue( 293 base::TimeTicks monotonic_time = base::TimeTicks::FromInternalValue(
285 event.monotonic_time * base::Time::kMicrosecondsPerSecond); 294 event.monotonic_time * base::Time::kMicrosecondsPerSecond);
286 if (event.is_impl_only) { 295 if (event.is_impl_only) {
287 FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_, 296 FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_,
288 OnAnimationStarted(event)); 297 OnAnimationStarted(event));
289 if (layer_animation_delegate_) 298 if (layer_animation_delegate_)
290 layer_animation_delegate_->NotifyAnimationStarted(monotonic_time, 299 layer_animation_delegate_->NotifyAnimationStarted(monotonic_time,
291 event.target_property); 300 event.target_property);
292
293 return; 301 return;
294 } 302 }
295 303
296 for (size_t i = 0; i < active_animations_.size(); ++i) { 304 for (size_t i = 0; i < active_animations_.size(); ++i) {
297 if (active_animations_[i]->group() == event.group_id && 305 if (active_animations_[i]->group() == event.group_id &&
298 active_animations_[i]->target_property() == event.target_property && 306 active_animations_[i]->target_property() == event.target_property &&
299 active_animations_[i]->needs_synchronized_start_time()) { 307 active_animations_[i]->needs_synchronized_start_time()) {
300 active_animations_[i]->set_needs_synchronized_start_time(false); 308 active_animations_[i]->set_needs_synchronized_start_time(false);
301 if (!active_animations_[i]->has_set_start_time()) 309 if (!active_animations_[i]->has_set_start_time())
302 active_animations_[i]->set_start_time(event.monotonic_time); 310 active_animations_[i]->set_start_time(event.monotonic_time);
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 for (size_t i = 0; i < active_animations_.size(); ++i) { 576 for (size_t i = 0; i < active_animations_.size(); ++i) {
569 Animation* current_impl = 577 Animation* current_impl =
570 controller_impl->GetAnimation( 578 controller_impl->GetAnimation(
571 active_animations_[i]->group(), 579 active_animations_[i]->group(),
572 active_animations_[i]->target_property()); 580 active_animations_[i]->target_property());
573 if (current_impl) 581 if (current_impl)
574 active_animations_[i]->PushPropertiesTo(current_impl); 582 active_animations_[i]->PushPropertiesTo(current_impl);
575 } 583 }
576 } 584 }
577 585
578 void LayerAnimationController::StartAnimations(double monotonic_time) { 586 void LayerAnimationController::StartAnimations(base::TimeTicks time) {
587 double monotonic_time = TimeTicksToDoubleTime(time);
579 // First collect running properties. 588 // First collect running properties.
580 TargetProperties blocked_properties; 589 TargetProperties blocked_properties;
581 for (size_t i = 0; i < active_animations_.size(); ++i) { 590 for (size_t i = 0; i < active_animations_.size(); ++i) {
582 if (active_animations_[i]->run_state() == Animation::Starting || 591 if (active_animations_[i]->run_state() == Animation::Starting ||
583 active_animations_[i]->run_state() == Animation::Running) 592 active_animations_[i]->run_state() == Animation::Running)
584 blocked_properties.insert(active_animations_[i]->target_property()); 593 blocked_properties.insert(active_animations_[i]->target_property());
585 } 594 }
586 595
587 for (size_t i = 0; i < active_animations_.size(); ++i) { 596 for (size_t i = 0; i < active_animations_.size(); ++i) {
588 if (active_animations_[i]->run_state() == 597 if (active_animations_[i]->run_state() ==
(...skipping 30 matching lines...) Expand all
619 active_animations_[j]->SetRunState( 628 active_animations_[j]->SetRunState(
620 Animation::Starting, monotonic_time); 629 Animation::Starting, monotonic_time);
621 } 630 }
622 } 631 }
623 } 632 }
624 } 633 }
625 } 634 }
626 } 635 }
627 636
628 void LayerAnimationController::PromoteStartedAnimations( 637 void LayerAnimationController::PromoteStartedAnimations(
629 double monotonic_time, 638 base::TimeTicks time,
630 AnimationEventsVector* events) { 639 AnimationEventsVector* events) {
640 double monotonic_time = TimeTicksToDoubleTime(time);
631 for (size_t i = 0; i < active_animations_.size(); ++i) { 641 for (size_t i = 0; i < active_animations_.size(); ++i) {
632 if (active_animations_[i]->run_state() == Animation::Starting) { 642 if (active_animations_[i]->run_state() == Animation::Starting) {
633 active_animations_[i]->SetRunState(Animation::Running, monotonic_time); 643 active_animations_[i]->SetRunState(Animation::Running, monotonic_time);
634 if (!active_animations_[i]->has_set_start_time() && 644 if (!active_animations_[i]->has_set_start_time() &&
635 !active_animations_[i]->needs_synchronized_start_time()) 645 !active_animations_[i]->needs_synchronized_start_time())
636 active_animations_[i]->set_start_time(monotonic_time); 646 active_animations_[i]->set_start_time(monotonic_time);
637 if (events) { 647 if (events) {
638 AnimationEvent started_event( 648 AnimationEvent started_event(
639 AnimationEvent::Started, 649 AnimationEvent::Started,
640 id_, 650 id_,
641 active_animations_[i]->group(), 651 active_animations_[i]->group(),
642 active_animations_[i]->target_property(), 652 active_animations_[i]->target_property(),
643 monotonic_time); 653 monotonic_time);
644 started_event.is_impl_only = active_animations_[i]->is_impl_only(); 654 started_event.is_impl_only = active_animations_[i]->is_impl_only();
645 events->push_back(started_event); 655 events->push_back(started_event);
646 } 656 }
647 } 657 }
648 } 658 }
649 } 659 }
650 660
651 void LayerAnimationController::MarkFinishedAnimations(double monotonic_time) { 661 void LayerAnimationController::MarkFinishedAnimations(base::TimeTicks time) {
662 double monotonic_time = TimeTicksToDoubleTime(time);
652 for (size_t i = 0; i < active_animations_.size(); ++i) { 663 for (size_t i = 0; i < active_animations_.size(); ++i) {
653 if (active_animations_[i]->IsFinishedAt(monotonic_time) && 664 if (active_animations_[i]->IsFinishedAt(monotonic_time) &&
654 active_animations_[i]->run_state() != Animation::Aborted && 665 active_animations_[i]->run_state() != Animation::Aborted &&
655 active_animations_[i]->run_state() != Animation::WaitingForDeletion) 666 active_animations_[i]->run_state() != Animation::WaitingForDeletion)
656 active_animations_[i]->SetRunState(Animation::Finished, monotonic_time); 667 active_animations_[i]->SetRunState(Animation::Finished, monotonic_time);
657 } 668 }
658 } 669 }
659 670
660 void LayerAnimationController::MarkAnimationsForDeletion( 671 void LayerAnimationController::MarkAnimationsForDeletion(
661 double monotonic_time, AnimationEventsVector* events) { 672 base::TimeTicks time,
673 AnimationEventsVector* events) {
674 double monotonic_time = TimeTicksToDoubleTime(time);
662 bool marked_animations_for_deletions = false; 675 bool marked_animations_for_deletions = false;
663 676
664 // Non-aborted animations are marked for deletion after a corresponding 677 // Non-aborted animations are marked for deletion after a corresponding
665 // AnimationEvent::Finished event is sent or received. This means that if 678 // AnimationEvent::Finished event is sent or received. This means that if
666 // we don't have an events vector, we must ensure that non-aborted animations 679 // we don't have an events vector, we must ensure that non-aborted animations
667 // have received a finished event before marking them for deletion. 680 // have received a finished event before marking them for deletion.
668 for (size_t i = 0; i < active_animations_.size(); i++) { 681 for (size_t i = 0; i < active_animations_.size(); i++) {
669 int group_id = active_animations_[i]->group(); 682 int group_id = active_animations_[i]->group();
670 if (active_animations_[i]->run_state() == Animation::Aborted) { 683 if (active_animations_[i]->run_state() == Animation::Aborted) {
671 if (events && !active_animations_[i]->is_impl_only()) { 684 if (events && !active_animations_[i]->is_impl_only()) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 754
742 void LayerAnimationController::PurgeAnimationsMarkedForDeletion() { 755 void LayerAnimationController::PurgeAnimationsMarkedForDeletion() {
743 ScopedPtrVector<Animation>& animations = active_animations_; 756 ScopedPtrVector<Animation>& animations = active_animations_;
744 animations.erase(cc::remove_if(&animations, 757 animations.erase(cc::remove_if(&animations,
745 animations.begin(), 758 animations.begin(),
746 animations.end(), 759 animations.end(),
747 IsWaitingForDeletion), 760 IsWaitingForDeletion),
748 animations.end()); 761 animations.end());
749 } 762 }
750 763
751 void LayerAnimationController::TickAnimations(double monotonic_time) { 764 void LayerAnimationController::TickAnimations(base::TimeTicks monotonic_time) {
752 for (size_t i = 0; i < active_animations_.size(); ++i) { 765 for (size_t i = 0; i < active_animations_.size(); ++i) {
753 if (active_animations_[i]->run_state() == Animation::Starting || 766 if (active_animations_[i]->run_state() == Animation::Starting ||
754 active_animations_[i]->run_state() == Animation::Running || 767 active_animations_[i]->run_state() == Animation::Running ||
755 active_animations_[i]->run_state() == Animation::Paused) { 768 active_animations_[i]->run_state() == Animation::Paused) {
756 double trimmed = 769 double trimmed = active_animations_[i]->TrimTimeToCurrentIteration(
757 active_animations_[i]->TrimTimeToCurrentIteration(monotonic_time); 770 TimeTicksToDoubleTime(monotonic_time));
758 771
759 switch (active_animations_[i]->target_property()) { 772 switch (active_animations_[i]->target_property()) {
760 case Animation::Transform: { 773 case Animation::Transform: {
761 const TransformAnimationCurve* transform_animation_curve = 774 const TransformAnimationCurve* transform_animation_curve =
762 active_animations_[i]->curve()->ToTransformAnimationCurve(); 775 active_animations_[i]->curve()->ToTransformAnimationCurve();
763 const gfx::Transform transform = 776 const gfx::Transform transform =
764 transform_animation_curve->GetValue(trimmed); 777 transform_animation_curve->GetValue(trimmed);
765 NotifyObserversTransformAnimated(transform); 778 NotifyObserversTransformAnimated(transform);
766 break; 779 break;
767 } 780 }
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 value_observers_); 885 value_observers_);
873 LayerAnimationValueObserver* obs; 886 LayerAnimationValueObserver* obs;
874 while ((obs = it.GetNext()) != NULL) 887 while ((obs = it.GetNext()) != NULL)
875 if (obs->IsActive()) 888 if (obs->IsActive())
876 return true; 889 return true;
877 } 890 }
878 return false; 891 return false;
879 } 892 }
880 893
881 } // namespace cc 894 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698