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

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: 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 LayerAnimationController::LayerAnimationController(int id) 23 LayerAnimationController::LayerAnimationController(int id)
24 : registrar_(0), 24 : registrar_(0),
25 id_(id), 25 id_(id),
26 is_active_(false), 26 is_active_(false),
27 last_tick_time_(0), 27 last_tick_time_(base::TimeTicks()),
28 value_provider_(NULL), 28 value_provider_(NULL),
29 layer_animation_delegate_(NULL) {} 29 layer_animation_delegate_(NULL) {}
30 30
31 LayerAnimationController::~LayerAnimationController() { 31 LayerAnimationController::~LayerAnimationController() {
32 if (registrar_) 32 if (registrar_)
33 registrar_->UnregisterAnimationController(this); 33 registrar_->UnregisterAnimationController(this);
34 } 34 }
35 35
36 scoped_refptr<LayerAnimationController> LayerAnimationController::Create( 36 scoped_refptr<LayerAnimationController> LayerAnimationController::Create(
37 int id) { 37 int id) {
38 return make_scoped_refptr(new LayerAnimationController(id)); 38 return make_scoped_refptr(new LayerAnimationController(id));
39 } 39 }
40 40
41 void LayerAnimationController::PauseAnimation(int animation_id, 41 void LayerAnimationController::PauseAnimation(int animation_id,
42 double time_offset) { 42 base::TimeTicks time_offset) {
43 for (size_t i = 0; i < active_animations_.size(); ++i) { 43 for (size_t i = 0; i < active_animations_.size(); ++i) {
44 if (active_animations_[i]->id() == animation_id) { 44 if (active_animations_[i]->id() == animation_id) {
45 active_animations_[i]->SetRunState( 45 // TODO(sikugu): http://crbug.com/178171 - Remove double
46 Animation::Paused, time_offset + active_animations_[i]->start_time()); 46 // and use base::TimeTicks/TimeDelta instead all over the file.
47 // Remove usage of GetTimeTicks.
48 double time =
49 GetTimeTicks(time_offset) + active_animations_[i]->start_time();
50 active_animations_[i]->SetRunState(Animation::Paused, time);
47 } 51 }
48 } 52 }
49 } 53 }
50 54
51 struct HasAnimationId { 55 struct HasAnimationId {
52 explicit HasAnimationId(int id) : id_(id) {} 56 explicit HasAnimationId(int id) : id_(id) {}
53 bool operator()(Animation* animation) const { 57 bool operator()(Animation* animation) const {
54 return animation->id() == id_; 58 return animation->id() == id_;
55 } 59 }
56 60
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 HasAnimationIdAndProperty(animation_id, target_property)), 96 HasAnimationIdAndProperty(animation_id, target_property)),
93 animations.end()); 97 animations.end());
94 UpdateActivation(NormalActivation); 98 UpdateActivation(NormalActivation);
95 } 99 }
96 100
97 void LayerAnimationController::AbortAnimations( 101 void LayerAnimationController::AbortAnimations(
98 Animation::TargetProperty target_property) { 102 Animation::TargetProperty target_property) {
99 for (size_t i = 0; i < active_animations_.size(); ++i) { 103 for (size_t i = 0; i < active_animations_.size(); ++i) {
100 if (active_animations_[i]->target_property() == target_property && 104 if (active_animations_[i]->target_property() == target_property &&
101 !active_animations_[i]->is_finished()) 105 !active_animations_[i]->is_finished())
102 active_animations_[i]->SetRunState(Animation::Aborted, last_tick_time_); 106 active_animations_[i]->SetRunState(Animation::Aborted,
107 GetTimeTicks(last_tick_time_));
103 } 108 }
104 } 109 }
105 110
106 // Ensures that the list of active animations on the main thread and the impl 111 // Ensures that the list of active animations on the main thread and the impl
107 // thread are kept in sync. 112 // thread are kept in sync.
108 void LayerAnimationController::PushAnimationUpdatesTo( 113 void LayerAnimationController::PushAnimationUpdatesTo(
109 LayerAnimationController* controller_impl) { 114 LayerAnimationController* controller_impl) {
110 DCHECK(this != controller_impl); 115 DCHECK(this != controller_impl);
111 if (!has_any_animation() && !controller_impl->has_any_animation()) 116 if (!has_any_animation() && !controller_impl->has_any_animation())
112 return; 117 return;
113 PurgeAnimationsMarkedForDeletion(); 118 PurgeAnimationsMarkedForDeletion();
114 PushNewAnimationsToImplThread(controller_impl); 119 PushNewAnimationsToImplThread(controller_impl);
115 120
116 // Remove finished impl side animations only after pushing, 121 // Remove finished impl side animations only after pushing,
117 // and only after the animations are deleted on the main thread 122 // and only after the animations are deleted on the main thread
118 // this insures we will never push an animation twice. 123 // this insures we will never push an animation twice.
119 RemoveAnimationsCompletedOnMainThread(controller_impl); 124 RemoveAnimationsCompletedOnMainThread(controller_impl);
120 125
121 PushPropertiesToImplThread(controller_impl); 126 PushPropertiesToImplThread(controller_impl);
122 controller_impl->UpdateActivation(NormalActivation); 127 controller_impl->UpdateActivation(NormalActivation);
123 UpdateActivation(NormalActivation); 128 UpdateActivation(NormalActivation);
124 } 129 }
125 130
126 void LayerAnimationController::Animate(double monotonic_time) { 131 void LayerAnimationController::Animate(base::TimeTicks monotonic_time) {
127 DCHECK(monotonic_time); 132 DCHECK(monotonic_time != base::TimeTicks());
128 if (!HasValueObserver()) 133 if (!HasValueObserver())
129 return; 134 return;
130 135
131 StartAnimations(monotonic_time); 136 StartAnimations(monotonic_time);
132 TickAnimations(monotonic_time); 137 TickAnimations(monotonic_time);
133 last_tick_time_ = monotonic_time; 138 last_tick_time_ = monotonic_time;
134 } 139 }
135 140
136 void LayerAnimationController::AccumulatePropertyUpdates( 141 void LayerAnimationController::AccumulatePropertyUpdates(
137 double monotonic_time, 142 base::TimeTicks time,
138 AnimationEventsVector* events) { 143 AnimationEventsVector* events) {
144 double monotonic_time = GetTimeTicks(time);
139 if (!events) 145 if (!events)
140 return; 146 return;
141 147
142 for (size_t i = 0; i < active_animations_.size(); ++i) { 148 for (size_t i = 0; i < active_animations_.size(); ++i) {
143 Animation* animation = active_animations_[i]; 149 Animation* animation = active_animations_[i];
144 if (!animation->is_impl_only()) 150 if (!animation->is_impl_only())
145 continue; 151 continue;
146 152
147 double trimmed = animation->TrimTimeToCurrentIteration(monotonic_time); 153 double trimmed = animation->TrimTimeToCurrentIteration(monotonic_time);
148 switch (animation->target_property()) { 154 switch (animation->target_property()) {
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 if (registrar_) 282 if (registrar_)
277 registrar_->RegisterAnimationController(this); 283 registrar_->RegisterAnimationController(this);
278 284
279 UpdateActivation(ForceActivation); 285 UpdateActivation(ForceActivation);
280 } 286 }
281 287
282 void LayerAnimationController::NotifyAnimationStarted( 288 void LayerAnimationController::NotifyAnimationStarted(
283 const AnimationEvent& event) { 289 const AnimationEvent& event) {
284 base::TimeTicks monotonic_time = base::TimeTicks::FromInternalValue( 290 base::TimeTicks monotonic_time = base::TimeTicks::FromInternalValue(
285 event.monotonic_time * base::Time::kMicrosecondsPerSecond); 291 event.monotonic_time * base::Time::kMicrosecondsPerSecond);
292
danakj 2014/04/09 16:33:03 whitespace?
Sikugu_ 2014/04/10 14:04:57 Done.
286 if (event.is_impl_only) { 293 if (event.is_impl_only) {
287 FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_, 294 FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_,
288 OnAnimationStarted(event)); 295 OnAnimationStarted(event));
289 if (layer_animation_delegate_) 296 if (layer_animation_delegate_)
297
danakj 2014/04/09 16:33:03 whitespace?
Sikugu_ 2014/04/10 14:04:57 Done.
290 layer_animation_delegate_->NotifyAnimationStarted(monotonic_time, 298 layer_animation_delegate_->NotifyAnimationStarted(monotonic_time,
291 event.target_property); 299 event.target_property);
292 300
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()) {
(...skipping 268 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 = GetTimeTicks(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 = GetTimeTicks(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 = GetTimeTicks(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 = GetTimeTicks(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 GetTimeTicks(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