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

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

Issue 13465014: LayerTreeHost::SetAnimationEvents should use AnimationRegistrar (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 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 | Annotate | Revision Log
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_registrar.h" 10 #include "cc/animation/animation_registrar.h"
11 #include "cc/animation/keyframed_animation_curve.h" 11 #include "cc/animation/keyframed_animation_curve.h"
12 #include "cc/animation/layer_animation_value_observer.h" 12 #include "cc/animation/layer_animation_value_observer.h"
13 #include "cc/base/scoped_ptr_algorithm.h" 13 #include "cc/base/scoped_ptr_algorithm.h"
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebAnimationDelegat e.h"
14 #include "ui/gfx/transform.h" 15 #include "ui/gfx/transform.h"
15 16
16 namespace cc { 17 namespace cc {
17 18
18 LayerAnimationController::LayerAnimationController(int id) 19 LayerAnimationController::LayerAnimationController(int id)
19 : force_sync_(false), 20 : force_sync_(false),
20 registrar_(0), 21 registrar_(0),
21 id_(id), 22 id_(id),
22 is_active_(false), 23 is_active_(false),
23 last_tick_time_(0) {} 24 last_tick_time_(0),
25 layer_animation_delegate_(NULL) {}
24 26
25 LayerAnimationController::~LayerAnimationController() { 27 LayerAnimationController::~LayerAnimationController() {
26 if (registrar_) 28 if (registrar_)
27 registrar_->UnregisterAnimationController(this); 29 registrar_->UnregisterAnimationController(this);
28 } 30 }
29 31
30 scoped_refptr<LayerAnimationController> LayerAnimationController::Create( 32 scoped_refptr<LayerAnimationController> LayerAnimationController::Create(
31 int id) { 33 int id) {
32 return make_scoped_refptr(new LayerAnimationController(id)); 34 return make_scoped_refptr(new LayerAnimationController(id));
33 } 35 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 for (size_t i = 0; i < active_animations_.size(); ++i) { 104 for (size_t i = 0; i < active_animations_.size(); ++i) {
103 if (active_animations_[i]->run_state() == Animation::Paused) 105 if (active_animations_[i]->run_state() == Animation::Paused)
104 active_animations_[i]->SetRunState(Animation::Running, monotonic_time); 106 active_animations_[i]->SetRunState(Animation::Running, monotonic_time);
105 } 107 }
106 } 108 }
107 109
108 // Ensures that the list of active animations on the main thread and the impl 110 // Ensures that the list of active animations on the main thread and the impl
109 // thread are kept in sync. 111 // thread are kept in sync.
110 void LayerAnimationController::PushAnimationUpdatesTo( 112 void LayerAnimationController::PushAnimationUpdatesTo(
111 LayerAnimationController* controller_impl) { 113 LayerAnimationController* controller_impl) {
114 DCHECK(this != controller_impl);
112 if (force_sync_) { 115 if (force_sync_) {
113 ReplaceImplThreadAnimations(controller_impl); 116 ReplaceImplThreadAnimations(controller_impl);
114 force_sync_ = false; 117 force_sync_ = false;
115 } else { 118 } else {
116 PurgeAnimationsMarkedForDeletion(); 119 PurgeAnimationsMarkedForDeletion();
117 PushNewAnimationsToImplThread(controller_impl); 120 PushNewAnimationsToImplThread(controller_impl);
118 121
119 // Remove finished impl side animations only after pushing, 122 // Remove finished impl side animations only after pushing,
120 // and only after the animations are deleted on the main thread 123 // and only after the animations are deleted on the main thread
121 // this insures we will never push an animation twice. 124 // this insures we will never push an animation twice.
122 RemoveAnimationsCompletedOnMainThread(controller_impl); 125 RemoveAnimationsCompletedOnMainThread(controller_impl);
123 126
124 PushPropertiesToImplThread(controller_impl); 127 PushPropertiesToImplThread(controller_impl);
125 } 128 }
126 controller_impl->UpdateActivation(NormalActivation); 129 controller_impl->UpdateActivation(NormalActivation);
127 UpdateActivation(NormalActivation); 130 UpdateActivation(NormalActivation);
128 } 131 }
129 132
133 void LayerAnimationController::TransferAnimationsTo(
134 LayerAnimationController* other_controller) {
135 other_controller->active_animations_.clear();
136 active_animations_.swap(other_controller->active_animations_);
137 UpdateActivation(NormalActivation);
138 set_force_sync();
Ian Vollick 2013/04/04 18:38:58 other_controller->UpdateActivation(NormalActivatio
ajuma 2013/04/05 14:38:18 Done.
139 other_controller->set_force_sync();
140 other_controller->SetAnimationRegistrar(registrar_);
141 }
142
130 void LayerAnimationController::Animate(double monotonic_time) { 143 void LayerAnimationController::Animate(double monotonic_time) {
131 if (!HasActiveObserver()) 144 if (!HasActiveObserver())
132 return; 145 return;
133 146
134 StartAnimationsWaitingForNextTick(monotonic_time); 147 StartAnimationsWaitingForNextTick(monotonic_time);
135 StartAnimationsWaitingForStartTime(monotonic_time); 148 StartAnimationsWaitingForStartTime(monotonic_time);
136 StartAnimationsWaitingForTargetAvailability(monotonic_time); 149 StartAnimationsWaitingForTargetAvailability(monotonic_time);
137 ResolveConflicts(monotonic_time); 150 ResolveConflicts(monotonic_time);
138 TickAnimations(monotonic_time); 151 TickAnimations(monotonic_time);
139 last_tick_time_ = monotonic_time; 152 last_tick_time_ = monotonic_time;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 for (size_t i = 0; i < active_animations_.size(); ++i) { 231 for (size_t i = 0; i < active_animations_.size(); ++i) {
219 if (!active_animations_[i]->is_finished()) 232 if (!active_animations_[i]->is_finished())
220 return true; 233 return true;
221 } 234 }
222 return false; 235 return false;
223 } 236 }
224 237
225 bool LayerAnimationController::IsAnimatingProperty( 238 bool LayerAnimationController::IsAnimatingProperty(
226 Animation::TargetProperty target_property) const { 239 Animation::TargetProperty target_property) const {
227 for (size_t i = 0; i < active_animations_.size(); ++i) { 240 for (size_t i = 0; i < active_animations_.size(); ++i) {
228 if (active_animations_[i]->run_state() != Animation::Finished && 241 if (!active_animations_[i]->is_finished() &&
229 active_animations_[i]->run_state() != Animation::Aborted &&
230 active_animations_[i]->target_property() == target_property) 242 active_animations_[i]->target_property() == target_property)
231 return true; 243 return true;
232 } 244 }
233 return false; 245 return false;
234 } 246 }
235 247
236 void LayerAnimationController::OnAnimationStarted(
237 const AnimationEvent& event) {
238 for (size_t i = 0; i < active_animations_.size(); ++i) {
239 if (active_animations_[i]->group() == event.group_id &&
240 active_animations_[i]->target_property() == event.target_property &&
241 active_animations_[i]->needs_synchronized_start_time()) {
242 active_animations_[i]->set_needs_synchronized_start_time(false);
243 active_animations_[i]->set_start_time(event.monotonic_time);
244 return;
245 }
246 }
247 }
248
249 void LayerAnimationController::SetAnimationRegistrar( 248 void LayerAnimationController::SetAnimationRegistrar(
250 AnimationRegistrar* registrar) { 249 AnimationRegistrar* registrar) {
251 if (registrar_ == registrar) 250 if (registrar_ == registrar)
252 return; 251 return;
253 252
254 if (registrar_) 253 if (registrar_)
255 registrar_->UnregisterAnimationController(this); 254 registrar_->UnregisterAnimationController(this);
256 255
257 registrar_ = registrar; 256 registrar_ = registrar;
258 if (registrar_) 257 if (registrar_)
259 registrar_->RegisterAnimationController(this); 258 registrar_->RegisterAnimationController(this);
260 259
261 UpdateActivation(ForceActivation); 260 UpdateActivation(ForceActivation);
262 } 261 }
263 262
264 void LayerAnimationController::AddObserver( 263 void LayerAnimationController::NotifyAnimationStarted(
265 LayerAnimationValueObserver* observer) { 264 const AnimationEvent& event,
266 if (!observers_.HasObserver(observer)) 265 double wall_clock_time) {
267 observers_.AddObserver(observer); 266 for (size_t i = 0; i < active_animations_.size(); ++i) {
267 if (active_animations_[i]->group() == event.group_id &&
268 active_animations_[i]->target_property() == event.target_property &&
269 active_animations_[i]->needs_synchronized_start_time()) {
270 active_animations_[i]->set_needs_synchronized_start_time(false);
271 active_animations_[i]->set_start_time(event.monotonic_time);
272
273 FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_,
274 OnAnimationStarted(event));
275 if (layer_animation_delegate_)
276 layer_animation_delegate_->notifyAnimationStarted(wall_clock_time);
277
278 return;
279 }
280 }
268 } 281 }
269 282
270 void LayerAnimationController::RemoveObserver( 283 void LayerAnimationController::NotifyAnimationFinished(
284 const AnimationEvent& event,
285 double wall_clock_time) {
286 for (size_t i = 0; i < active_animations_.size(); ++i) {
287 if (active_animations_[i]->group() == event.group_id &&
288 active_animations_[i]->target_property() == event.target_property) {
289 active_animations_[i]->set_received_finished_event(true);
290 if (layer_animation_delegate_)
291 layer_animation_delegate_->notifyAnimationFinished(wall_clock_time);
292
293 return;
294 }
295 }
296 }
297
298 void LayerAnimationController::NotifyAnimationPropertyUpdate(
299 const AnimationEvent& event) {
300 switch (event.target_property) {
301 case Animation::Opacity:
302 NotifyObserversOpacityAnimated(event.opacity);
303 break;
304 case Animation::Transform:
305 NotifyObserversTransformAnimated(event.transform);
306 break;
307 default:
308 NOTREACHED();
309 }
310 }
311
312 void LayerAnimationController::AddValueObserver(
271 LayerAnimationValueObserver* observer) { 313 LayerAnimationValueObserver* observer) {
272 observers_.RemoveObserver(observer); 314 if (!value_observers_.HasObserver(observer))
315 value_observers_.AddObserver(observer);
316 }
317
318 void LayerAnimationController::RemoveValueObserver(
319 LayerAnimationValueObserver* observer) {
320 value_observers_.RemoveObserver(observer);
321 }
322
323 void LayerAnimationController::AddEventObserver(
324 LayerAnimationEventObserver* observer) {
325 if (!event_observers_.HasObserver(observer))
326 event_observers_.AddObserver(observer);
327 }
328
329 void LayerAnimationController::RemoveEventObserver(
330 LayerAnimationEventObserver* observer) {
331 event_observers_.RemoveObserver(observer);
273 } 332 }
274 333
275 void LayerAnimationController::PushNewAnimationsToImplThread( 334 void LayerAnimationController::PushNewAnimationsToImplThread(
276 LayerAnimationController* controller_impl) const { 335 LayerAnimationController* controller_impl) const {
277 // Any new animations owned by the main thread's controller are cloned and 336 // Any new animations owned by the main thread's controller are cloned and
278 // add to the impl thread's controller. 337 // add to the impl thread's controller.
279 for (size_t i = 0; i < active_animations_.size(); ++i) { 338 for (size_t i = 0; i < active_animations_.size(); ++i) {
280 // If the animation is already running on the impl thread, there is no 339 // If the animation is already running on the impl thread, there is no
281 // need to copy it over. 340 // need to copy it over.
282 if (controller_impl->GetAnimation(active_animations_[i]->group(), 341 if (controller_impl->GetAnimation(active_animations_[i]->group(),
(...skipping 17 matching lines...) Expand all
300 Animation::ControllingInstance, initial_run_state, start_time)); 359 Animation::ControllingInstance, initial_run_state, start_time));
301 DCHECK(!to_add->needs_synchronized_start_time()); 360 DCHECK(!to_add->needs_synchronized_start_time());
302 controller_impl->AddAnimation(to_add.Pass()); 361 controller_impl->AddAnimation(to_add.Pass());
303 } 362 }
304 } 363 }
305 364
306 struct IsCompleted { 365 struct IsCompleted {
307 explicit IsCompleted(const LayerAnimationController& main_thread_controller) 366 explicit IsCompleted(const LayerAnimationController& main_thread_controller)
308 : main_thread_controller_(main_thread_controller) {} 367 : main_thread_controller_(main_thread_controller) {}
309 bool operator()(Animation* animation) const { 368 bool operator()(Animation* animation) const {
310 if (animation->is_impl_only()) 369 if (animation->is_impl_only()) {
Ian Vollick 2013/04/04 18:38:58 nit: no braces.
ajuma 2013/04/05 14:38:18 Since the 'else' requires braces, so does the 'if'
311 return false; 370 return (animation->run_state() == Animation::WaitingForDeletion);
312 return !main_thread_controller_.GetAnimation(animation->group(), 371 } else {
313 animation->target_property()); 372 return !main_thread_controller_.GetAnimation(
373 animation->group(),
374 animation->target_property());
375 }
314 } 376 }
315 377
316 private: 378 private:
317 const LayerAnimationController& main_thread_controller_; 379 const LayerAnimationController& main_thread_controller_;
318 }; 380 };
319 381
320 void LayerAnimationController::RemoveAnimationsCompletedOnMainThread( 382 void LayerAnimationController::RemoveAnimationsCompletedOnMainThread(
321 LayerAnimationController* controller_impl) const { 383 LayerAnimationController* controller_impl) const {
322 // Delete all impl thread animations for which there is no corresponding 384 // Delete all impl thread animations for which there is no corresponding
323 // main thread animation. Each iteration, 385 // main thread animation. Each iteration,
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 active_animations_[i]->SetRunState(Animation::Starting, monotonic_time); 422 active_animations_[i]->SetRunState(Animation::Starting, monotonic_time);
361 } 423 }
362 } 424 }
363 425
364 void LayerAnimationController::StartAnimationsWaitingForTargetAvailability( 426 void LayerAnimationController::StartAnimationsWaitingForTargetAvailability(
365 double monotonic_time) { 427 double monotonic_time) {
366 // First collect running properties. 428 // First collect running properties.
367 TargetProperties blocked_properties; 429 TargetProperties blocked_properties;
368 for (size_t i = 0; i < active_animations_.size(); ++i) { 430 for (size_t i = 0; i < active_animations_.size(); ++i) {
369 if (active_animations_[i]->run_state() == Animation::Starting || 431 if (active_animations_[i]->run_state() == Animation::Starting ||
370 active_animations_[i]->run_state() == Animation::Running || 432 active_animations_[i]->run_state() == Animation::Running)
371 active_animations_[i]->run_state() == Animation::Finished)
372 blocked_properties.insert(active_animations_[i]->target_property()); 433 blocked_properties.insert(active_animations_[i]->target_property());
373 } 434 }
374 435
375 for (size_t i = 0; i < active_animations_.size(); ++i) { 436 for (size_t i = 0; i < active_animations_.size(); ++i) {
376 if (active_animations_[i]->run_state() == 437 if (active_animations_[i]->run_state() ==
377 Animation::WaitingForTargetAvailability) { 438 Animation::WaitingForTargetAvailability) {
378 // Collect all properties for animations with the same group id (they 439 // Collect all properties for animations with the same group id (they
379 // should all also be in the list of animations). 440 // should all also be in the list of animations).
380 TargetProperties enqueued_properties; 441 TargetProperties enqueued_properties;
381 enqueued_properties.insert(active_animations_[i]->target_property()); 442 enqueued_properties.insert(active_animations_[i]->target_property());
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 active_animations_[i]->group(), 489 active_animations_[i]->group(),
429 active_animations_[i]->target_property(), 490 active_animations_[i]->target_property(),
430 monotonic_time)); 491 monotonic_time));
431 } 492 }
432 } 493 }
433 } 494 }
434 } 495 }
435 496
436 void LayerAnimationController::MarkFinishedAnimations(double monotonic_time) { 497 void LayerAnimationController::MarkFinishedAnimations(double monotonic_time) {
437 for (size_t i = 0; i < active_animations_.size(); ++i) { 498 for (size_t i = 0; i < active_animations_.size(); ++i) {
438 if (active_animations_[i]->IsFinishedAt(monotonic_time)) 499 if (active_animations_[i]->IsFinishedAt(monotonic_time) &&
500 active_animations_[i]->run_state() != Animation::WaitingForDeletion)
439 active_animations_[i]->SetRunState(Animation::Finished, monotonic_time); 501 active_animations_[i]->SetRunState(Animation::Finished, monotonic_time);
440 } 502 }
441 } 503 }
442 504
443 void LayerAnimationController::ResolveConflicts(double monotonic_time) { 505 void LayerAnimationController::ResolveConflicts(double monotonic_time) {
444 // Find any animations that are animating the same property and resolve the 506 // Find any animations that are animating the same property and resolve the
445 // confict. We could eventually blend, but for now we'll just abort the 507 // confict. We could eventually blend, but for now we'll just abort the
446 // previous animation (where 'previous' means: (1) has a prior start time or 508 // previous animation (where 'previous' means: (1) has a prior start time or
447 // (2) has an equal start time, but was added to the queue earlier, i.e., 509 // (2) has an equal start time, but was added to the queue earlier, i.e.,
448 // has a lower index in active_animations_). 510 // has a lower index in active_animations_).
(...skipping 14 matching lines...) Expand all
463 monotonic_time); 525 monotonic_time);
464 } 526 }
465 } 527 }
466 } 528 }
467 } 529 }
468 } 530 }
469 } 531 }
470 532
471 void LayerAnimationController::MarkAnimationsForDeletion( 533 void LayerAnimationController::MarkAnimationsForDeletion(
472 double monotonic_time, AnimationEventsVector* events) { 534 double monotonic_time, AnimationEventsVector* events) {
535 // Non-aborted animations are marked for deletion after a corresponding
536 // AnimationEvent::Finished event is sent or received. This means that if
537 // we don't have an events vector, we must ensure that non-aborted animations
538 // have received a finished event before marking them for deletion.
473 for (size_t i = 0; i < active_animations_.size(); i++) { 539 for (size_t i = 0; i < active_animations_.size(); i++) {
474 int group_id = active_animations_[i]->group(); 540 int group_id = active_animations_[i]->group();
475 bool all_anims_with_same_id_are_finished = false; 541 bool all_anims_with_same_id_are_finished = false;
476 // If an animation is finished, and not already marked for deletion, 542 // If an animation is finished, and not already marked for deletion,
477 // Find out if all other animations in the same group are also finished. 543 // find out if all other animations in the same group are also finished.
478 if (active_animations_[i]->is_finished() && 544 if (active_animations_[i]->run_state() == Animation::Aborted ||
479 active_animations_[i]->run_state() != Animation::WaitingForDeletion) { 545 (active_animations_[i]->run_state() == Animation::Finished &&
546 (events || active_animations_[i]->received_finished_event()))) {
Ian Vollick 2013/04/04 18:38:58 Please put (events || active_animations_[i]->recei
ajuma 2013/04/05 14:38:18 Done.
480 all_anims_with_same_id_are_finished = true; 547 all_anims_with_same_id_are_finished = true;
481 for (size_t j = 0; j < active_animations_.size(); ++j) { 548 for (size_t j = 0; j < active_animations_.size(); ++j) {
482 if (group_id == active_animations_[j]->group() && 549 if (group_id == active_animations_[j]->group() &&
483 !active_animations_[j]->is_finished()) { 550 (!active_animations_[j]->is_finished() ||
551 (active_animations_[j]->run_state() == Animation::Finished &&
552 !events &&
553 !active_animations_[j]->received_finished_event()))) {
Ian Vollick 2013/04/04 18:38:58 Same thing here (these complicated conditions are
ajuma 2013/04/05 14:38:18 Done.
484 all_anims_with_same_id_are_finished = false; 554 all_anims_with_same_id_are_finished = false;
485 break; 555 break;
486 } 556 }
487 } 557 }
488 } 558 }
489 if (all_anims_with_same_id_are_finished) { 559 if (all_anims_with_same_id_are_finished) {
490 // We now need to remove all animations with the same group id as 560 // We now need to remove all animations with the same group id as
491 // group_id (and send along animation finished notifications, if 561 // group_id (and send along animation finished notifications, if
492 // necessary). 562 // necessary).
493 for (size_t j = i; j < active_animations_.size(); j++) { 563 for (size_t j = i; j < active_animations_.size(); j++) {
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 if (!active_animations_.empty() && (!is_active_ || force)) 664 if (!active_animations_.empty() && (!is_active_ || force))
595 registrar_->DidActivateAnimationController(this); 665 registrar_->DidActivateAnimationController(this);
596 else if (active_animations_.empty() && (is_active_ || force)) 666 else if (active_animations_.empty() && (is_active_ || force))
597 registrar_->DidDeactivateAnimationController(this); 667 registrar_->DidDeactivateAnimationController(this);
598 is_active_ = !active_animations_.empty(); 668 is_active_ = !active_animations_.empty();
599 } 669 }
600 } 670 }
601 671
602 void LayerAnimationController::NotifyObserversOpacityAnimated(float opacity) { 672 void LayerAnimationController::NotifyObserversOpacityAnimated(float opacity) {
603 FOR_EACH_OBSERVER(LayerAnimationValueObserver, 673 FOR_EACH_OBSERVER(LayerAnimationValueObserver,
604 observers_, 674 value_observers_,
605 OnOpacityAnimated(opacity)); 675 OnOpacityAnimated(opacity));
606 } 676 }
607 677
608 void LayerAnimationController::NotifyObserversTransformAnimated( 678 void LayerAnimationController::NotifyObserversTransformAnimated(
609 const gfx::Transform& transform) { 679 const gfx::Transform& transform) {
610 FOR_EACH_OBSERVER(LayerAnimationValueObserver, 680 FOR_EACH_OBSERVER(LayerAnimationValueObserver,
611 observers_, 681 value_observers_,
612 OnTransformAnimated(transform)); 682 OnTransformAnimated(transform));
613 } 683 }
614 684
615 bool LayerAnimationController::HasActiveObserver() { 685 bool LayerAnimationController::HasActiveObserver() {
Ian Vollick 2013/04/04 18:38:58 HasActiveValueObserver
ajuma 2013/04/05 14:38:18 Done.
616 if (observers_.might_have_observers()) { 686 if (value_observers_.might_have_observers()) {
617 ObserverListBase<LayerAnimationValueObserver>::Iterator it(observers_); 687 ObserverListBase<LayerAnimationValueObserver>::Iterator it(
688 value_observers_);
618 LayerAnimationValueObserver* obs; 689 LayerAnimationValueObserver* obs;
619 while ((obs = it.GetNext()) != NULL) 690 while ((obs = it.GetNext()) != NULL)
620 if (obs->IsActive()) 691 if (obs->IsActive())
621 return true; 692 return true;
622 } 693 }
623 return false; 694 return false;
624 } 695 }
625 696
626 } // namespace cc 697 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698