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

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

Issue 226543005: CC should use TimeTicks and 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 double 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 active_animations_[i]->SetRunState(
46 Animation::Paused, time_offset + active_animations_[i]->start_time()); 46 Animation::Paused,
47 base::TimeTicks::FromInternalValue(
48 (time_offset + active_animations_[i]->start_time()) *
ajuma 2014/04/08 15:44:16 We should change time_offset to a TimeDelta, and t
49 base::Time::kMicrosecondsPerSecond));
47 } 50 }
48 } 51 }
49 } 52 }
50 53
51 struct HasAnimationId { 54 struct HasAnimationId {
52 explicit HasAnimationId(int id) : id_(id) {} 55 explicit HasAnimationId(int id) : id_(id) {}
53 bool operator()(Animation* animation) const { 56 bool operator()(Animation* animation) const {
54 return animation->id() == id_; 57 return animation->id() == id_;
55 } 58 }
56 59
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 // Remove finished impl side animations only after pushing, 119 // Remove finished impl side animations only after pushing,
117 // and only after the animations are deleted on the main thread 120 // and only after the animations are deleted on the main thread
118 // this insures we will never push an animation twice. 121 // this insures we will never push an animation twice.
119 RemoveAnimationsCompletedOnMainThread(controller_impl); 122 RemoveAnimationsCompletedOnMainThread(controller_impl);
120 123
121 PushPropertiesToImplThread(controller_impl); 124 PushPropertiesToImplThread(controller_impl);
122 controller_impl->UpdateActivation(NormalActivation); 125 controller_impl->UpdateActivation(NormalActivation);
123 UpdateActivation(NormalActivation); 126 UpdateActivation(NormalActivation);
124 } 127 }
125 128
126 void LayerAnimationController::Animate(double monotonic_time) { 129 void LayerAnimationController::Animate(base::TimeTicks monotonic_time) {
127 DCHECK(monotonic_time); 130 bool time = (monotonic_time - base::TimeTicks()).InSecondsF();
131 DCHECK(time);
ajuma 2014/04/08 15:44:16 Instead of converting to double, just DCHECK(monot
128 if (!HasValueObserver()) 132 if (!HasValueObserver())
129 return; 133 return;
130 134
131 StartAnimations(monotonic_time); 135 StartAnimations(monotonic_time);
132 TickAnimations(monotonic_time); 136 TickAnimations(monotonic_time);
133 last_tick_time_ = monotonic_time; 137 last_tick_time_ = monotonic_time;
134 } 138 }
135 139
136 void LayerAnimationController::AccumulatePropertyUpdates( 140 void LayerAnimationController::AccumulatePropertyUpdates(
137 double monotonic_time, 141 base::TimeTicks monotonic_time,
138 AnimationEventsVector* events) { 142 AnimationEventsVector* events) {
139 if (!events) 143 if (!events)
140 return; 144 return;
141 145
142 for (size_t i = 0; i < active_animations_.size(); ++i) { 146 for (size_t i = 0; i < active_animations_.size(); ++i) {
143 Animation* animation = active_animations_[i]; 147 Animation* animation = active_animations_[i];
144 if (!animation->is_impl_only()) 148 if (!animation->is_impl_only())
145 continue; 149 continue;
146 150
147 double trimmed = animation->TrimTimeToCurrentIteration(monotonic_time); 151 double trimmed = animation->TrimTimeToCurrentIteration(monotonic_time);
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 278
275 registrar_ = registrar; 279 registrar_ = registrar;
276 if (registrar_) 280 if (registrar_)
277 registrar_->RegisterAnimationController(this); 281 registrar_->RegisterAnimationController(this);
278 282
279 UpdateActivation(ForceActivation); 283 UpdateActivation(ForceActivation);
280 } 284 }
281 285
282 void LayerAnimationController::NotifyAnimationStarted( 286 void LayerAnimationController::NotifyAnimationStarted(
283 const AnimationEvent& event) { 287 const AnimationEvent& event) {
284 base::TimeTicks monotonic_time = base::TimeTicks::FromInternalValue( 288 base::TimeTicks monotonic_time = event.monotonic_time;
285 event.monotonic_time * base::Time::kMicrosecondsPerSecond);
286 if (event.is_impl_only) { 289 if (event.is_impl_only) {
287 FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_, 290 FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_,
288 OnAnimationStarted(event)); 291 OnAnimationStarted(event));
289 if (layer_animation_delegate_) 292 if (layer_animation_delegate_)
290 layer_animation_delegate_->NotifyAnimationStarted(monotonic_time, 293 layer_animation_delegate_->NotifyAnimationStarted(monotonic_time,
291 event.target_property); 294 event.target_property);
292 295
293 return; 296 return;
294 } 297 }
295 298
(...skipping 10 matching lines...) Expand all
306 layer_animation_delegate_->NotifyAnimationStarted( 309 layer_animation_delegate_->NotifyAnimationStarted(
307 monotonic_time, event.target_property); 310 monotonic_time, event.target_property);
308 311
309 return; 312 return;
310 } 313 }
311 } 314 }
312 } 315 }
313 316
314 void LayerAnimationController::NotifyAnimationFinished( 317 void LayerAnimationController::NotifyAnimationFinished(
315 const AnimationEvent& event) { 318 const AnimationEvent& event) {
316 base::TimeTicks monotonic_time = base::TimeTicks::FromInternalValue( 319 base::TimeTicks monotonic_time = event.monotonic_time;
317 event.monotonic_time * base::Time::kMicrosecondsPerSecond);
318 if (event.is_impl_only) { 320 if (event.is_impl_only) {
319 if (layer_animation_delegate_) 321 if (layer_animation_delegate_)
320 layer_animation_delegate_->NotifyAnimationFinished(monotonic_time, 322 layer_animation_delegate_->NotifyAnimationFinished(monotonic_time,
321 event.target_property); 323 event.target_property);
322 return; 324 return;
323 } 325 }
324 326
325 for (size_t i = 0; i < active_animations_.size(); ++i) { 327 for (size_t i = 0; i < active_animations_.size(); ++i) {
326 if (active_animations_[i]->group() == event.group_id && 328 if (active_animations_[i]->group() == event.group_id &&
327 active_animations_[i]->target_property() == event.target_property) { 329 active_animations_[i]->target_property() == event.target_property) {
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 // scroll offset will be up-to-date. 518 // scroll offset will be up-to-date.
517 current_scroll_offset = value_provider_->ScrollOffsetForAnimation(); 519 current_scroll_offset = value_provider_->ScrollOffsetForAnimation();
518 } 520 }
519 active_animations_[i]->curve()->ToScrollOffsetAnimationCurve() 521 active_animations_[i]->curve()->ToScrollOffsetAnimationCurve()
520 ->SetInitialValue(current_scroll_offset); 522 ->SetInitialValue(current_scroll_offset);
521 } 523 }
522 524
523 // The new animation should be set to run as soon as possible. 525 // The new animation should be set to run as soon as possible.
524 Animation::RunState initial_run_state = 526 Animation::RunState initial_run_state =
525 Animation::WaitingForTargetAvailability; 527 Animation::WaitingForTargetAvailability;
526 double start_time = 0;
527 scoped_ptr<Animation> to_add(active_animations_[i]->CloneAndInitialize( 528 scoped_ptr<Animation> to_add(active_animations_[i]->CloneAndInitialize(
528 initial_run_state, start_time)); 529 initial_run_state, base::TimeTicks()));
529 DCHECK(!to_add->needs_synchronized_start_time()); 530 DCHECK(!to_add->needs_synchronized_start_time());
530 controller_impl->AddAnimation(to_add.Pass()); 531 controller_impl->AddAnimation(to_add.Pass());
531 } 532 }
532 } 533 }
533 534
534 struct IsCompleted { 535 struct IsCompleted {
535 explicit IsCompleted(const LayerAnimationController& main_thread_controller) 536 explicit IsCompleted(const LayerAnimationController& main_thread_controller)
536 : main_thread_controller_(main_thread_controller) {} 537 : main_thread_controller_(main_thread_controller) {}
537 bool operator()(Animation* animation) const { 538 bool operator()(Animation* animation) const {
538 if (animation->is_impl_only()) { 539 if (animation->is_impl_only()) {
(...skipping 29 matching lines...) Expand all
568 for (size_t i = 0; i < active_animations_.size(); ++i) { 569 for (size_t i = 0; i < active_animations_.size(); ++i) {
569 Animation* current_impl = 570 Animation* current_impl =
570 controller_impl->GetAnimation( 571 controller_impl->GetAnimation(
571 active_animations_[i]->group(), 572 active_animations_[i]->group(),
572 active_animations_[i]->target_property()); 573 active_animations_[i]->target_property());
573 if (current_impl) 574 if (current_impl)
574 active_animations_[i]->PushPropertiesTo(current_impl); 575 active_animations_[i]->PushPropertiesTo(current_impl);
575 } 576 }
576 } 577 }
577 578
578 void LayerAnimationController::StartAnimations(double monotonic_time) { 579 void LayerAnimationController::StartAnimations(base::TimeTicks monotonic_time) {
579 // First collect running properties. 580 // First collect running properties.
580 TargetProperties blocked_properties; 581 TargetProperties blocked_properties;
581 for (size_t i = 0; i < active_animations_.size(); ++i) { 582 for (size_t i = 0; i < active_animations_.size(); ++i) {
582 if (active_animations_[i]->run_state() == Animation::Starting || 583 if (active_animations_[i]->run_state() == Animation::Starting ||
583 active_animations_[i]->run_state() == Animation::Running) 584 active_animations_[i]->run_state() == Animation::Running)
584 blocked_properties.insert(active_animations_[i]->target_property()); 585 blocked_properties.insert(active_animations_[i]->target_property());
585 } 586 }
586 587
587 for (size_t i = 0; i < active_animations_.size(); ++i) { 588 for (size_t i = 0; i < active_animations_.size(); ++i) {
588 if (active_animations_[i]->run_state() == 589 if (active_animations_[i]->run_state() ==
(...skipping 20 matching lines...) Expand all
609 } 610 }
610 611
611 // If the intersection is null, then we are free to start the animations 612 // If the intersection is null, then we are free to start the animations
612 // in the group. 613 // in the group.
613 if (null_intersection) { 614 if (null_intersection) {
614 active_animations_[i]->SetRunState( 615 active_animations_[i]->SetRunState(
615 Animation::Starting, monotonic_time); 616 Animation::Starting, monotonic_time);
616 for (size_t j = i + 1; j < active_animations_.size(); ++j) { 617 for (size_t j = i + 1; j < active_animations_.size(); ++j) {
617 if (active_animations_[i]->group() == 618 if (active_animations_[i]->group() ==
618 active_animations_[j]->group()) { 619 active_animations_[j]->group()) {
619 active_animations_[j]->SetRunState( 620 active_animations_[j]->SetRunState(Animation::Starting,
620 Animation::Starting, monotonic_time); 621 monotonic_time);
621 } 622 }
622 } 623 }
623 } 624 }
624 } 625 }
625 } 626 }
626 } 627 }
627 628
628 void LayerAnimationController::PromoteStartedAnimations( 629 void LayerAnimationController::PromoteStartedAnimations(
629 double monotonic_time, 630 base::TimeTicks monotonic_time,
630 AnimationEventsVector* events) { 631 AnimationEventsVector* events) {
631 for (size_t i = 0; i < active_animations_.size(); ++i) { 632 for (size_t i = 0; i < active_animations_.size(); ++i) {
632 if (active_animations_[i]->run_state() == Animation::Starting) { 633 if (active_animations_[i]->run_state() == Animation::Starting) {
633 active_animations_[i]->SetRunState(Animation::Running, monotonic_time); 634 active_animations_[i]->SetRunState(Animation::Running, monotonic_time);
634 if (!active_animations_[i]->has_set_start_time()) 635 if (!active_animations_[i]->has_set_start_time())
635 active_animations_[i]->set_start_time(monotonic_time); 636 active_animations_[i]->set_start_time(monotonic_time);
636 if (events) { 637 if (events) {
637 AnimationEvent started_event( 638 AnimationEvent started_event(
638 AnimationEvent::Started, 639 AnimationEvent::Started,
639 id_, 640 id_,
640 active_animations_[i]->group(), 641 active_animations_[i]->group(),
641 active_animations_[i]->target_property(), 642 active_animations_[i]->target_property(),
642 monotonic_time); 643 monotonic_time);
643 started_event.is_impl_only = active_animations_[i]->is_impl_only(); 644 started_event.is_impl_only = active_animations_[i]->is_impl_only();
644 events->push_back(started_event); 645 events->push_back(started_event);
645 } 646 }
646 } 647 }
647 } 648 }
648 } 649 }
649 650
650 void LayerAnimationController::MarkFinishedAnimations(double monotonic_time) { 651 void LayerAnimationController::MarkFinishedAnimations(
652 base::TimeTicks monotonic_time) {
651 for (size_t i = 0; i < active_animations_.size(); ++i) { 653 for (size_t i = 0; i < active_animations_.size(); ++i) {
652 if (active_animations_[i]->IsFinishedAt(monotonic_time) && 654 if (active_animations_[i]->IsFinishedAt(monotonic_time) &&
653 active_animations_[i]->run_state() != Animation::Aborted && 655 active_animations_[i]->run_state() != Animation::Aborted &&
654 active_animations_[i]->run_state() != Animation::WaitingForDeletion) 656 active_animations_[i]->run_state() != Animation::WaitingForDeletion)
655 active_animations_[i]->SetRunState(Animation::Finished, monotonic_time); 657 active_animations_[i]->SetRunState(Animation::Finished, monotonic_time);
656 } 658 }
657 } 659 }
658 660
659 void LayerAnimationController::MarkAnimationsForDeletion( 661 void LayerAnimationController::MarkAnimationsForDeletion(
660 double monotonic_time, AnimationEventsVector* events) { 662 base::TimeTicks monotonic_time,
663 AnimationEventsVector* events) {
661 bool marked_animations_for_deletions = false; 664 bool marked_animations_for_deletions = false;
662 665
663 // Non-aborted animations are marked for deletion after a corresponding 666 // Non-aborted animations are marked for deletion after a corresponding
664 // AnimationEvent::Finished event is sent or received. This means that if 667 // AnimationEvent::Finished event is sent or received. This means that if
665 // we don't have an events vector, we must ensure that non-aborted animations 668 // we don't have an events vector, we must ensure that non-aborted animations
666 // have received a finished event before marking them for deletion. 669 // have received a finished event before marking them for deletion.
667 for (size_t i = 0; i < active_animations_.size(); i++) { 670 for (size_t i = 0; i < active_animations_.size(); i++) {
668 int group_id = active_animations_[i]->group(); 671 int group_id = active_animations_[i]->group();
669 if (active_animations_[i]->run_state() == Animation::Aborted) { 672 if (active_animations_[i]->run_state() == Animation::Aborted) {
670 if (events && !active_animations_[i]->is_impl_only()) { 673 if (events && !active_animations_[i]->is_impl_only()) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 743
741 void LayerAnimationController::PurgeAnimationsMarkedForDeletion() { 744 void LayerAnimationController::PurgeAnimationsMarkedForDeletion() {
742 ScopedPtrVector<Animation>& animations = active_animations_; 745 ScopedPtrVector<Animation>& animations = active_animations_;
743 animations.erase(cc::remove_if(&animations, 746 animations.erase(cc::remove_if(&animations,
744 animations.begin(), 747 animations.begin(),
745 animations.end(), 748 animations.end(),
746 IsWaitingForDeletion), 749 IsWaitingForDeletion),
747 animations.end()); 750 animations.end());
748 } 751 }
749 752
750 void LayerAnimationController::TickAnimations(double monotonic_time) { 753 void LayerAnimationController::TickAnimations(base::TimeTicks monotonic_time) {
751 for (size_t i = 0; i < active_animations_.size(); ++i) { 754 for (size_t i = 0; i < active_animations_.size(); ++i) {
752 if (active_animations_[i]->run_state() == Animation::Starting || 755 if (active_animations_[i]->run_state() == Animation::Starting ||
753 active_animations_[i]->run_state() == Animation::Running || 756 active_animations_[i]->run_state() == Animation::Running ||
754 active_animations_[i]->run_state() == Animation::Paused) { 757 active_animations_[i]->run_state() == Animation::Paused) {
755 double trimmed = 758 double trimmed =
756 active_animations_[i]->TrimTimeToCurrentIteration(monotonic_time); 759 active_animations_[i]->TrimTimeToCurrentIteration(monotonic_time);
757 760
758 switch (active_animations_[i]->target_property()) { 761 switch (active_animations_[i]->target_property()) {
759 case Animation::Transform: { 762 case Animation::Transform: {
760 const TransformAnimationCurve* transform_animation_curve = 763 const TransformAnimationCurve* transform_animation_curve =
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 value_observers_); 874 value_observers_);
872 LayerAnimationValueObserver* obs; 875 LayerAnimationValueObserver* obs;
873 while ((obs = it.GetNext()) != NULL) 876 while ((obs = it.GetNext()) != NULL)
874 if (obs->IsActive()) 877 if (obs->IsActive())
875 return true; 878 return true;
876 } 879 }
877 return false; 880 return false;
878 } 881 }
879 882
880 } // namespace cc 883 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698