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

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: Code changed as per review comments. Created 6 years, 7 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_(),
ajuma 2014/05/07 17:04:42 Not needed.
Sikugu_ 2014/05/12 16:01:53 Done.
28 value_provider_(NULL), 28 value_provider_(NULL),
29 layer_animation_delegate_(NULL) {} 29 layer_animation_delegate_(NULL) {
30 }
30 31
31 LayerAnimationController::~LayerAnimationController() { 32 LayerAnimationController::~LayerAnimationController() {
32 if (registrar_) 33 if (registrar_)
33 registrar_->UnregisterAnimationController(this); 34 registrar_->UnregisterAnimationController(this);
34 } 35 }
35 36
36 scoped_refptr<LayerAnimationController> LayerAnimationController::Create( 37 scoped_refptr<LayerAnimationController> LayerAnimationController::Create(
37 int id) { 38 int id) {
38 return make_scoped_refptr(new LayerAnimationController(id)); 39 return make_scoped_refptr(new LayerAnimationController(id));
39 } 40 }
40 41
41 void LayerAnimationController::PauseAnimation(int animation_id, 42 void LayerAnimationController::PauseAnimation(int animation_id,
42 double time_offset) { 43 base::TimeDelta time_offset) {
43 for (size_t i = 0; i < animations_.size(); ++i) { 44 for (size_t i = 0; i < animations_.size(); ++i) {
44 if (animations_[i]->id() == animation_id) { 45 if (animations_[i]->id() == animation_id) {
45 animations_[i]->SetRunState(Animation::Paused, 46 animations_[i]->SetRunState(Animation::Paused,
46 time_offset + animations_[i]->start_time()); 47 time_offset + animations_[i]->start_time());
47 } 48 }
48 } 49 }
49 } 50 }
50 51
51 struct HasAnimationId { 52 struct HasAnimationId {
52 explicit HasAnimationId(int id) : id_(id) {} 53 explicit HasAnimationId(int id) : id_(id) {}
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 // Remove finished impl side animations only after pushing, 115 // Remove finished impl side animations only after pushing,
115 // and only after the animations are deleted on the main thread 116 // and only after the animations are deleted on the main thread
116 // this insures we will never push an animation twice. 117 // this insures we will never push an animation twice.
117 RemoveAnimationsCompletedOnMainThread(controller_impl); 118 RemoveAnimationsCompletedOnMainThread(controller_impl);
118 119
119 PushPropertiesToImplThread(controller_impl); 120 PushPropertiesToImplThread(controller_impl);
120 controller_impl->UpdateActivation(NormalActivation); 121 controller_impl->UpdateActivation(NormalActivation);
121 UpdateActivation(NormalActivation); 122 UpdateActivation(NormalActivation);
122 } 123 }
123 124
124 void LayerAnimationController::Animate(double monotonic_time) { 125 void LayerAnimationController::Animate(base::TimeTicks monotonic_time) {
125 DCHECK(monotonic_time); 126 DCHECK(monotonic_time != base::TimeTicks());
126 if (!HasValueObserver()) 127 if (!HasValueObserver())
127 return; 128 return;
128 129
129 StartAnimations(monotonic_time); 130 StartAnimations(monotonic_time);
130 TickAnimations(monotonic_time); 131 TickAnimations(monotonic_time);
131 last_tick_time_ = monotonic_time; 132 last_tick_time_ = monotonic_time;
132 } 133 }
133 134
134 void LayerAnimationController::AccumulatePropertyUpdates( 135 void LayerAnimationController::AccumulatePropertyUpdates(
135 double monotonic_time, 136 base::TimeTicks monotonic_time,
136 AnimationEventsVector* events) { 137 AnimationEventsVector* events) {
137 if (!events) 138 if (!events)
138 return; 139 return;
139 140
140 for (size_t i = 0; i < animations_.size(); ++i) { 141 for (size_t i = 0; i < animations_.size(); ++i) {
141 Animation* animation = animations_[i]; 142 Animation* animation = animations_[i];
142 if (!animation->is_impl_only()) 143 if (!animation->is_impl_only())
143 continue; 144 continue;
144 145
145 double trimmed = animation->TrimTimeToCurrentIteration(monotonic_time); 146 double trimmed = animation->TrimTimeToCurrentIteration(monotonic_time);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 NOTREACHED(); 197 NOTREACHED();
197 } 198 }
198 } 199 }
199 } 200 }
200 201
201 void LayerAnimationController::UpdateState(bool start_ready_animations, 202 void LayerAnimationController::UpdateState(bool start_ready_animations,
202 AnimationEventsVector* events) { 203 AnimationEventsVector* events) {
203 if (!HasActiveValueObserver()) 204 if (!HasActiveValueObserver())
204 return; 205 return;
205 206
206 DCHECK(last_tick_time_); 207 DCHECK(last_tick_time_ != base::TimeTicks());
207 if (start_ready_animations) 208 if (start_ready_animations)
208 PromoteStartedAnimations(last_tick_time_, events); 209 PromoteStartedAnimations(last_tick_time_, events);
209 210
210 MarkFinishedAnimations(last_tick_time_); 211 MarkFinishedAnimations(last_tick_time_);
211 MarkAnimationsForDeletion(last_tick_time_, events); 212 MarkAnimationsForDeletion(last_tick_time_, events);
212 213
213 if (start_ready_animations) { 214 if (start_ready_animations) {
214 StartAnimations(last_tick_time_); 215 StartAnimations(last_tick_time_);
215 PromoteStartedAnimations(last_tick_time_, events); 216 PromoteStartedAnimations(last_tick_time_, events);
216 } 217 }
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 294
294 registrar_ = registrar; 295 registrar_ = registrar;
295 if (registrar_) 296 if (registrar_)
296 registrar_->RegisterAnimationController(this); 297 registrar_->RegisterAnimationController(this);
297 298
298 UpdateActivation(ForceActivation); 299 UpdateActivation(ForceActivation);
299 } 300 }
300 301
301 void LayerAnimationController::NotifyAnimationStarted( 302 void LayerAnimationController::NotifyAnimationStarted(
302 const AnimationEvent& event) { 303 const AnimationEvent& event) {
303 base::TimeTicks monotonic_time = base::TimeTicks::FromInternalValue(
304 event.monotonic_time * base::Time::kMicrosecondsPerSecond);
305 if (event.is_impl_only) { 304 if (event.is_impl_only) {
306 FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_, 305 FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_,
307 OnAnimationStarted(event)); 306 OnAnimationStarted(event));
308 if (layer_animation_delegate_) 307 if (layer_animation_delegate_)
309 layer_animation_delegate_->NotifyAnimationStarted(monotonic_time, 308 layer_animation_delegate_->NotifyAnimationStarted(event.monotonic_time,
310 event.target_property); 309 event.target_property);
311
312 return; 310 return;
313 } 311 }
314 312
315 for (size_t i = 0; i < animations_.size(); ++i) { 313 for (size_t i = 0; i < animations_.size(); ++i) {
316 if (animations_[i]->group() == event.group_id && 314 if (animations_[i]->group() == event.group_id &&
317 animations_[i]->target_property() == event.target_property && 315 animations_[i]->target_property() == event.target_property &&
318 animations_[i]->needs_synchronized_start_time()) { 316 animations_[i]->needs_synchronized_start_time()) {
319 animations_[i]->set_needs_synchronized_start_time(false); 317 animations_[i]->set_needs_synchronized_start_time(false);
320 if (!animations_[i]->has_set_start_time()) 318 if (!animations_[i]->has_set_start_time())
321 animations_[i]->set_start_time(event.monotonic_time); 319 animations_[i]->set_start_time(event.monotonic_time);
322 320
323 FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_, 321 FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_,
324 OnAnimationStarted(event)); 322 OnAnimationStarted(event));
325 if (layer_animation_delegate_) 323 if (layer_animation_delegate_)
326 layer_animation_delegate_->NotifyAnimationStarted( 324 layer_animation_delegate_->NotifyAnimationStarted(
327 monotonic_time, event.target_property); 325 event.monotonic_time, event.target_property);
328 326
329 return; 327 return;
330 } 328 }
331 } 329 }
332 } 330 }
333 331
334 void LayerAnimationController::NotifyAnimationFinished( 332 void LayerAnimationController::NotifyAnimationFinished(
335 const AnimationEvent& event) { 333 const AnimationEvent& event) {
336 base::TimeTicks monotonic_time = base::TimeTicks::FromInternalValue(
337 event.monotonic_time * base::Time::kMicrosecondsPerSecond);
338 if (event.is_impl_only) { 334 if (event.is_impl_only) {
339 if (layer_animation_delegate_) 335 if (layer_animation_delegate_)
340 layer_animation_delegate_->NotifyAnimationFinished(monotonic_time, 336 layer_animation_delegate_->NotifyAnimationFinished(event.monotonic_time,
341 event.target_property); 337 event.target_property);
342 return; 338 return;
343 } 339 }
344 340
345 for (size_t i = 0; i < animations_.size(); ++i) { 341 for (size_t i = 0; i < animations_.size(); ++i) {
346 if (animations_[i]->group() == event.group_id && 342 if (animations_[i]->group() == event.group_id &&
347 animations_[i]->target_property() == event.target_property) { 343 animations_[i]->target_property() == event.target_property) {
348 animations_[i]->set_received_finished_event(true); 344 animations_[i]->set_received_finished_event(true);
349 if (layer_animation_delegate_) 345 if (layer_animation_delegate_)
350 layer_animation_delegate_->NotifyAnimationFinished( 346 layer_animation_delegate_->NotifyAnimationFinished(
351 monotonic_time, event.target_property); 347 event.monotonic_time, event.target_property);
352 348
353 return; 349 return;
354 } 350 }
355 } 351 }
356 } 352 }
357 353
358 void LayerAnimationController::NotifyAnimationAborted( 354 void LayerAnimationController::NotifyAnimationAborted(
359 const AnimationEvent& event) { 355 const AnimationEvent& event) {
360 for (size_t i = 0; i < animations_.size(); ++i) { 356 for (size_t i = 0; i < animations_.size(); ++i) {
361 if (animations_[i]->group() == event.group_id && 357 if (animations_[i]->group() == event.group_id &&
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 void LayerAnimationController::PushPropertiesToImplThread( 587 void LayerAnimationController::PushPropertiesToImplThread(
592 LayerAnimationController* controller_impl) const { 588 LayerAnimationController* controller_impl) const {
593 for (size_t i = 0; i < animations_.size(); ++i) { 589 for (size_t i = 0; i < animations_.size(); ++i) {
594 Animation* current_impl = controller_impl->GetAnimation( 590 Animation* current_impl = controller_impl->GetAnimation(
595 animations_[i]->group(), animations_[i]->target_property()); 591 animations_[i]->group(), animations_[i]->target_property());
596 if (current_impl) 592 if (current_impl)
597 animations_[i]->PushPropertiesTo(current_impl); 593 animations_[i]->PushPropertiesTo(current_impl);
598 } 594 }
599 } 595 }
600 596
601 void LayerAnimationController::StartAnimations(double monotonic_time) { 597 void LayerAnimationController::StartAnimations(base::TimeTicks monotonic_time) {
602 // First collect running properties affecting each type of observer. 598 // First collect running properties affecting each type of observer.
603 TargetProperties blocked_properties_for_active_observers; 599 TargetProperties blocked_properties_for_active_observers;
604 TargetProperties blocked_properties_for_pending_observers; 600 TargetProperties blocked_properties_for_pending_observers;
605 for (size_t i = 0; i < animations_.size(); ++i) { 601 for (size_t i = 0; i < animations_.size(); ++i) {
606 if (animations_[i]->run_state() == Animation::Starting || 602 if (animations_[i]->run_state() == Animation::Starting ||
607 animations_[i]->run_state() == Animation::Running) { 603 animations_[i]->run_state() == Animation::Running) {
608 if (animations_[i]->affects_active_observers()) { 604 if (animations_[i]->affects_active_observers()) {
609 blocked_properties_for_active_observers.insert( 605 blocked_properties_for_active_observers.insert(
610 animations_[i]->target_property()); 606 animations_[i]->target_property());
611 } 607 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 if (animations_[i]->group() == animations_[j]->group()) { 658 if (animations_[i]->group() == animations_[j]->group()) {
663 animations_[j]->SetRunState(Animation::Starting, monotonic_time); 659 animations_[j]->SetRunState(Animation::Starting, monotonic_time);
664 } 660 }
665 } 661 }
666 } 662 }
667 } 663 }
668 } 664 }
669 } 665 }
670 666
671 void LayerAnimationController::PromoteStartedAnimations( 667 void LayerAnimationController::PromoteStartedAnimations(
672 double monotonic_time, 668 base::TimeTicks monotonic_time,
673 AnimationEventsVector* events) { 669 AnimationEventsVector* events) {
674 for (size_t i = 0; i < animations_.size(); ++i) { 670 for (size_t i = 0; i < animations_.size(); ++i) {
675 if (animations_[i]->run_state() == Animation::Starting && 671 if (animations_[i]->run_state() == Animation::Starting &&
676 animations_[i]->affects_active_observers()) { 672 animations_[i]->affects_active_observers()) {
677 animations_[i]->SetRunState(Animation::Running, monotonic_time); 673 animations_[i]->SetRunState(Animation::Running, monotonic_time);
678 if (!animations_[i]->has_set_start_time() && 674 if (!animations_[i]->has_set_start_time() &&
679 !animations_[i]->needs_synchronized_start_time()) 675 !animations_[i]->needs_synchronized_start_time())
680 animations_[i]->set_start_time(monotonic_time); 676 animations_[i]->set_start_time(monotonic_time);
681 if (events) { 677 if (events) {
682 AnimationEvent started_event(AnimationEvent::Started, 678 AnimationEvent started_event(AnimationEvent::Started,
683 id_, 679 id_,
684 animations_[i]->group(), 680 animations_[i]->group(),
685 animations_[i]->target_property(), 681 animations_[i]->target_property(),
686 monotonic_time); 682 monotonic_time);
687 started_event.is_impl_only = animations_[i]->is_impl_only(); 683 started_event.is_impl_only = animations_[i]->is_impl_only();
688 events->push_back(started_event); 684 events->push_back(started_event);
689 } 685 }
690 } 686 }
691 } 687 }
692 } 688 }
693 689
694 void LayerAnimationController::MarkFinishedAnimations(double monotonic_time) { 690 void LayerAnimationController::MarkFinishedAnimations(
691 base::TimeTicks monotonic_time) {
695 for (size_t i = 0; i < animations_.size(); ++i) { 692 for (size_t i = 0; i < animations_.size(); ++i) {
696 if (animations_[i]->IsFinishedAt(monotonic_time) && 693 if (animations_[i]->IsFinishedAt(monotonic_time) &&
697 animations_[i]->run_state() != Animation::Aborted && 694 animations_[i]->run_state() != Animation::Aborted &&
698 animations_[i]->run_state() != Animation::WaitingForDeletion) 695 animations_[i]->run_state() != Animation::WaitingForDeletion)
699 animations_[i]->SetRunState(Animation::Finished, monotonic_time); 696 animations_[i]->SetRunState(Animation::Finished, monotonic_time);
700 } 697 }
701 } 698 }
702 699
703 void LayerAnimationController::MarkAnimationsForDeletion( 700 void LayerAnimationController::MarkAnimationsForDeletion(
704 double monotonic_time, AnimationEventsVector* events) { 701 base::TimeTicks monotonic_time,
702 AnimationEventsVector* events) {
705 bool marked_animations_for_deletions = false; 703 bool marked_animations_for_deletions = false;
706 704
707 // Non-aborted animations are marked for deletion after a corresponding 705 // Non-aborted animations are marked for deletion after a corresponding
708 // AnimationEvent::Finished event is sent or received. This means that if 706 // AnimationEvent::Finished event is sent or received. This means that if
709 // we don't have an events vector, we must ensure that non-aborted animations 707 // we don't have an events vector, we must ensure that non-aborted animations
710 // have received a finished event before marking them for deletion. 708 // have received a finished event before marking them for deletion.
711 for (size_t i = 0; i < animations_.size(); i++) { 709 for (size_t i = 0; i < animations_.size(); i++) {
712 int group_id = animations_[i]->group(); 710 int group_id = animations_[i]->group();
713 if (animations_[i]->run_state() == Animation::Aborted) { 711 if (animations_[i]->run_state() == Animation::Aborted) {
714 if (events && !animations_[i]->is_impl_only()) { 712 if (events && !animations_[i]->is_impl_only()) {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 } 779 }
782 780
783 void LayerAnimationController::PurgeAnimationsMarkedForDeletion() { 781 void LayerAnimationController::PurgeAnimationsMarkedForDeletion() {
784 animations_.erase(cc::remove_if(&animations_, 782 animations_.erase(cc::remove_if(&animations_,
785 animations_.begin(), 783 animations_.begin(),
786 animations_.end(), 784 animations_.end(),
787 IsWaitingForDeletion), 785 IsWaitingForDeletion),
788 animations_.end()); 786 animations_.end());
789 } 787 }
790 788
791 void LayerAnimationController::TickAnimations(double monotonic_time) { 789 void LayerAnimationController::TickAnimations(base::TimeTicks monotonic_time) {
792 for (size_t i = 0; i < animations_.size(); ++i) { 790 for (size_t i = 0; i < animations_.size(); ++i) {
793 if (animations_[i]->run_state() == Animation::Starting || 791 if (animations_[i]->run_state() == Animation::Starting ||
794 animations_[i]->run_state() == Animation::Running || 792 animations_[i]->run_state() == Animation::Running ||
795 animations_[i]->run_state() == Animation::Paused) { 793 animations_[i]->run_state() == Animation::Paused) {
796 double trimmed = 794 double trimmed =
797 animations_[i]->TrimTimeToCurrentIteration(monotonic_time); 795 animations_[i]->TrimTimeToCurrentIteration(monotonic_time);
798 796
799 switch (animations_[i]->target_property()) { 797 switch (animations_[i]->target_property()) {
800 case Animation::Transform: { 798 case Animation::Transform: {
801 const TransformAnimationCurve* transform_animation_curve = 799 const TransformAnimationCurve* transform_animation_curve =
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 value_observers_); 959 value_observers_);
962 LayerAnimationValueObserver* obs; 960 LayerAnimationValueObserver* obs;
963 while ((obs = it.GetNext()) != NULL) 961 while ((obs = it.GetNext()) != NULL)
964 if (obs->IsActive()) 962 if (obs->IsActive())
965 return true; 963 return true;
966 } 964 }
967 return false; 965 return false;
968 } 966 }
969 967
970 } // namespace cc 968 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698