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

Side by Side Diff: ui/compositor/layer_animator.cc

Issue 11896017: Thread ui opacity animations (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address comments Created 7 years, 10 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 "ui/compositor/layer_animator.h" 5 #include "ui/compositor/layer_animator.h"
6 6
7 #include "base/debug/trace_event.h" 7 #include "base/debug/trace_event.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "cc/animation_id_provider.h"
10 #include "ui/base/animation/animation_container.h" 11 #include "ui/base/animation/animation_container.h"
11 #include "ui/compositor/compositor.h" 12 #include "ui/compositor/compositor.h"
12 #include "ui/compositor/layer.h" 13 #include "ui/compositor/layer.h"
13 #include "ui/compositor/layer_animation_delegate.h" 14 #include "ui/compositor/layer_animation_delegate.h"
14 #include "ui/compositor/layer_animation_observer.h" 15 #include "ui/compositor/layer_animation_observer.h"
15 #include "ui/compositor/layer_animation_sequence.h" 16 #include "ui/compositor/layer_animation_sequence.h"
16 17
17 #define SAFE_INVOKE_VOID(function, running_anim, ...) \ 18 #define SAFE_INVOKE_VOID(function, running_anim, ...) \
18 if (running_anim.is_sequence_alive()) \ 19 if (running_anim.is_sequence_alive()) \
19 function(running_anim.sequence(), ##__VA_ARGS__) 20 function(running_anim.sequence(), ##__VA_ARGS__)
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 } 196 }
196 197
197 // Starting a zero duration pause that affects all the animated properties 198 // Starting a zero duration pause that affects all the animated properties
198 // will prevent any of the sequences from animating until there are no 199 // will prevent any of the sequences from animating until there are no
199 // running animations that affect any of these properties, as well as 200 // running animations that affect any of these properties, as well as
200 // handle preemption strategy. 201 // handle preemption strategy.
201 StartAnimation(new LayerAnimationSequence( 202 StartAnimation(new LayerAnimationSequence(
202 LayerAnimationElement::CreatePauseElement(animated_properties, 203 LayerAnimationElement::CreatePauseElement(animated_properties,
203 base::TimeDelta()))); 204 base::TimeDelta())));
204 205
206 bool wait_for_group_start = false;
207 for (iter = animations.begin(); iter != animations.end(); ++iter)
208 wait_for_group_start |= (*iter)->IsFirstElementThreaded();
209
210 int group_id = cc::AnimationIdProvider::NextGroupId();
211
205 // These animations (provided they don't animate any common properties) will 212 // These animations (provided they don't animate any common properties) will
206 // now animate together if trivially scheduled. 213 // now animate together if trivially scheduled.
207 for (iter = animations.begin(); iter != animations.end(); ++iter) { 214 for (iter = animations.begin(); iter != animations.end(); ++iter) {
215 (*iter)->set_animation_group_id(group_id);
216 (*iter)->set_waiting_for_group_start(wait_for_group_start);
208 ScheduleAnimation(*iter); 217 ScheduleAnimation(*iter);
209 } 218 }
210 219
211 adding_animations_ = false; 220 adding_animations_ = false;
212 UpdateAnimationState(); 221 UpdateAnimationState();
213 } 222 }
214 223
215 224
216 void LayerAnimator::ScheduleTogether( 225 void LayerAnimator::ScheduleTogether(
217 const std::vector<LayerAnimationSequence*>& animations) { 226 const std::vector<LayerAnimationSequence*>& animations) {
218 scoped_refptr<LayerAnimator> retain(this); 227 scoped_refptr<LayerAnimator> retain(this);
219 228
220 // Collect all the affected properties. 229 // Collect all the affected properties.
221 LayerAnimationElement::AnimatableProperties animated_properties; 230 LayerAnimationElement::AnimatableProperties animated_properties;
222 std::vector<LayerAnimationSequence*>::const_iterator iter; 231 std::vector<LayerAnimationSequence*>::const_iterator iter;
223 for (iter = animations.begin(); iter != animations.end(); ++iter) { 232 for (iter = animations.begin(); iter != animations.end(); ++iter) {
224 animated_properties.insert((*iter)->properties().begin(), 233 animated_properties.insert((*iter)->properties().begin(),
225 (*iter)->properties().end()); 234 (*iter)->properties().end());
226 } 235 }
227 236
228 // Scheduling a zero duration pause that affects all the animated properties 237 // Scheduling a zero duration pause that affects all the animated properties
229 // will prevent any of the sequences from animating until there are no 238 // will prevent any of the sequences from animating until there are no
230 // running animations that affect any of these properties. 239 // running animations that affect any of these properties.
231 ScheduleAnimation(new LayerAnimationSequence( 240 ScheduleAnimation(new LayerAnimationSequence(
232 LayerAnimationElement::CreatePauseElement(animated_properties, 241 LayerAnimationElement::CreatePauseElement(animated_properties,
233 base::TimeDelta()))); 242 base::TimeDelta())));
234 243
244 bool wait_for_group_start = false;
245 for (iter = animations.begin(); iter != animations.end(); ++iter)
246 wait_for_group_start |= (*iter)->IsFirstElementThreaded();
247
248 int group_id = cc::AnimationIdProvider::NextGroupId();
249
235 // These animations (provided they don't animate any common properties) will 250 // These animations (provided they don't animate any common properties) will
236 // now animate together if trivially scheduled. 251 // now animate together if trivially scheduled.
237 for (iter = animations.begin(); iter != animations.end(); ++iter) { 252 for (iter = animations.begin(); iter != animations.end(); ++iter) {
253 (*iter)->set_animation_group_id(group_id);
254 (*iter)->set_waiting_for_group_start(wait_for_group_start);
238 ScheduleAnimation(*iter); 255 ScheduleAnimation(*iter);
239 } 256 }
240 257
241 UpdateAnimationState(); 258 UpdateAnimationState();
242 } 259 }
243 260
244 void LayerAnimator::SchedulePauseForProperties( 261 void LayerAnimator::SchedulePauseForProperties(
245 base::TimeDelta duration, 262 base::TimeDelta duration,
246 LayerAnimationElement::AnimatableProperty property, 263 LayerAnimationElement::AnimatableProperty property,
247 ...) { 264 ...) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 309
293 void LayerAnimator::RemoveObserver(LayerAnimationObserver* observer) { 310 void LayerAnimator::RemoveObserver(LayerAnimationObserver* observer) {
294 observers_.RemoveObserver(observer); 311 observers_.RemoveObserver(observer);
295 // Remove the observer from all sequences as well. 312 // Remove the observer from all sequences as well.
296 for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); 313 for (AnimationQueue::iterator queue_iter = animation_queue_.begin();
297 queue_iter != animation_queue_.end(); ++queue_iter) { 314 queue_iter != animation_queue_.end(); ++queue_iter) {
298 (*queue_iter)->RemoveObserver(observer); 315 (*queue_iter)->RemoveObserver(observer);
299 } 316 }
300 } 317 }
301 318
319 void LayerAnimator::OnThreadedAnimationStarted(
320 const cc::AnimationEvent& event) {
321 LayerAnimationElement::AnimatableProperty property =
322 LayerAnimationElement::ToAnimatableProperty(event.targetProperty);
323
324 RunningAnimation* running = GetRunningAnimation(property);
325 if (!running)
326 return;
327 DCHECK(running->is_sequence_alive());
328
329 if (running->sequence()->animation_group_id() != event.groupId)
330 return;
331
332 running->sequence()->OnThreadedAnimationStarted(event);
333 if (!running->sequence()->waiting_for_group_start())
334 return;
335
336 base::TimeTicks start_time = base::TimeTicks::FromInternalValue(
337 event.monotonicTime * base::Time::kMicrosecondsPerSecond);
338
339 // The call to GetRunningAnimation made above already purged deleted
340 // animations, so we are guaranteed that all the animations we iterate
341 // over now are alive.
342 for (RunningAnimations::iterator iter = running_animations_.begin();
343 iter != running_animations_.end(); ++iter) {
344 if ((*iter).sequence()->animation_group_id() == event.groupId) {
345 (*iter).sequence()->set_start_time(start_time);
346 (*iter).sequence()->set_waiting_for_group_start(false);
347 }
348 }
349 }
350
351 LayerAnimator::TestController::TestController(
352 scoped_refptr<LayerAnimator> animator)
353 : animator_(animator) {
sky 2013/02/19 17:06:56 too many spaces here.
ajuma 2013/02/20 16:09:10 Done and moved to ui/compositor/test/layer_animato
354 }
355
356 LayerAnimator::TestController::~TestController() {
357 }
358
359 LayerAnimationSequence* LayerAnimator::TestController::GetRunningSequence(
360 LayerAnimationElement::AnimatableProperty property) {
361 RunningAnimation* running_animation =
362 animator_->GetRunningAnimation(property);
363 if (running_animation)
364 return running_animation->sequence();
365 else
366 return NULL;
367 }
368
302 // LayerAnimator protected ----------------------------------------------------- 369 // LayerAnimator protected -----------------------------------------------------
303 370
304 void LayerAnimator::ProgressAnimation(LayerAnimationSequence* sequence, 371 void LayerAnimator::ProgressAnimation(LayerAnimationSequence* sequence,
305 base::TimeTicks now) { 372 base::TimeTicks now) {
306 if (!delegate()) 373 if (!delegate() || sequence->waiting_for_group_start())
307 return; 374 return;
308 375
309 sequence->Progress(now, delegate()); 376 sequence->Progress(now, delegate());
310 } 377 }
311 378
312 void LayerAnimator::ProgressAnimationToEnd(LayerAnimationSequence* sequence) { 379 void LayerAnimator::ProgressAnimationToEnd(LayerAnimationSequence* sequence) {
313 if (!delegate()) 380 if (!delegate())
314 return; 381 return;
315 382
316 sequence->ProgressToEnd(delegate()); 383 sequence->ProgressToEnd(delegate());
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 else if (!should_start && is_started_) 459 else if (!should_start && is_started_)
393 GetAnimationContainer()->Stop(this); 460 GetAnimationContainer()->Stop(this);
394 461
395 is_started_ = should_start; 462 is_started_ = should_start;
396 } 463 }
397 464
398 LayerAnimationSequence* LayerAnimator::RemoveAnimation( 465 LayerAnimationSequence* LayerAnimator::RemoveAnimation(
399 LayerAnimationSequence* sequence) { 466 LayerAnimationSequence* sequence) {
400 linked_ptr<LayerAnimationSequence> to_return; 467 linked_ptr<LayerAnimationSequence> to_return;
401 468
469 bool is_running = false;
470
402 // First remove from running animations 471 // First remove from running animations
403 for (RunningAnimations::iterator iter = running_animations_.begin(); 472 for (RunningAnimations::iterator iter = running_animations_.begin();
404 iter != running_animations_.end(); ++iter) { 473 iter != running_animations_.end(); ++iter) {
405 if ((*iter).sequence() == sequence) { 474 if ((*iter).sequence() == sequence) {
406 running_animations_.erase(iter); 475 running_animations_.erase(iter);
476 is_running = true;
407 break; 477 break;
408 } 478 }
409 } 479 }
410 480
411 // Then remove from the queue 481 // Then remove from the queue
412 for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); 482 for (AnimationQueue::iterator queue_iter = animation_queue_.begin();
413 queue_iter != animation_queue_.end(); ++queue_iter) { 483 queue_iter != animation_queue_.end(); ++queue_iter) {
414 if ((*queue_iter) == sequence) { 484 if ((*queue_iter) == sequence) {
415 to_return = *queue_iter; 485 to_return = *queue_iter;
416 animation_queue_.erase(queue_iter); 486 animation_queue_.erase(queue_iter);
417 break; 487 break;
418 } 488 }
419 } 489 }
420 490
491 if (!to_return.get() ||
492 !to_return->waiting_for_group_start() ||
493 !to_return->IsFirstElementThreaded())
494 return to_return.release();
495
496 // The removed sequence may have been responsible for making other sequences
497 // wait for a group start. If no other sequences in the group have a
498 // threaded first element, the group no longer needs the additional wait.
499 bool is_wait_still_needed = false;
500 int group_id = to_return->animation_group_id();
501 for (AnimationQueue::iterator queue_iter = animation_queue_.begin();
502 queue_iter != animation_queue_.end(); ++queue_iter) {
503 if (((*queue_iter)->animation_group_id() == group_id) &&
504 (*queue_iter)->IsFirstElementThreaded()) {
505 is_wait_still_needed = true;
506 break;
507 }
508 }
509
510 if (is_wait_still_needed)
511 return to_return.release();
512
513 for (AnimationQueue::iterator queue_iter = animation_queue_.begin();
514 queue_iter != animation_queue_.end(); ++queue_iter) {
515 if ((*queue_iter)->animation_group_id() == group_id) {
516 (*queue_iter)->set_waiting_for_group_start(false);
517 if (is_running)
518 (*queue_iter)->set_start_time(last_step_time_);
519 }
520 }
421 return to_return.release(); 521 return to_return.release();
422 } 522 }
423 523
424 void LayerAnimator::FinishAnimation( 524 void LayerAnimator::FinishAnimation(
425 LayerAnimationSequence* sequence, bool abort) { 525 LayerAnimationSequence* sequence, bool abort) {
426 scoped_refptr<LayerAnimator> retain(this); 526 scoped_refptr<LayerAnimator> retain(this);
427 scoped_ptr<LayerAnimationSequence> removed(RemoveAnimation(sequence)); 527 scoped_ptr<LayerAnimationSequence> removed(RemoveAnimation(sequence));
428 if (abort) 528 if (abort)
429 sequence->Abort(); 529 sequence->Abort(delegate());
430 else 530 else
431 ProgressAnimationToEnd(sequence); 531 ProgressAnimationToEnd(sequence);
432 ProcessQueue(); 532 ProcessQueue();
433 UpdateAnimationState(); 533 UpdateAnimationState();
434 } 534 }
435 535
436 void LayerAnimator::FinishAnyAnimationWithZeroDuration() { 536 void LayerAnimator::FinishAnyAnimationWithZeroDuration() {
437 scoped_refptr<LayerAnimator> retain(this); 537 scoped_refptr<LayerAnimator> retain(this);
438 // Special case: if we've started a 0 duration animation, just finish it now 538 // Special case: if we've started a 0 duration animation, just finish it now
439 // and get rid of it. We need to make a copy because Progress may indirectly 539 // and get rid of it. We need to make a copy because Progress may indirectly
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 RunningAnimations running_animations_copy = running_animations_; 595 RunningAnimations running_animations_copy = running_animations_;
496 for (size_t i = 0; i < running_animations_copy.size(); ++i) { 596 for (size_t i = 0; i < running_animations_copy.size(); ++i) {
497 if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i])) 597 if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i]))
498 continue; 598 continue;
499 599
500 if (running_animations_copy[i].sequence()->HasCommonProperty( 600 if (running_animations_copy[i].sequence()->HasCommonProperty(
501 sequence->properties())) { 601 sequence->properties())) {
502 scoped_ptr<LayerAnimationSequence> removed( 602 scoped_ptr<LayerAnimationSequence> removed(
503 SAFE_INVOKE_PTR(RemoveAnimation, running_animations_copy[i])); 603 SAFE_INVOKE_PTR(RemoveAnimation, running_animations_copy[i]));
504 if (abort) 604 if (abort)
505 running_animations_copy[i].sequence()->Abort(); 605 running_animations_copy[i].sequence()->Abort(delegate());
506 else 606 else
507 SAFE_INVOKE_VOID(ProgressAnimationToEnd, running_animations_copy[i]); 607 SAFE_INVOKE_VOID(ProgressAnimationToEnd, running_animations_copy[i]);
508 } 608 }
509 } 609 }
510 610
511 // Same for the queued animations that haven't been started. Again, we'll 611 // Same for the queued animations that haven't been started. Again, we'll
512 // need to operate on a copy. 612 // need to operate on a copy.
513 std::vector<base::WeakPtr<LayerAnimationSequence> > sequences; 613 std::vector<base::WeakPtr<LayerAnimationSequence> > sequences;
514 for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); 614 for (AnimationQueue::iterator queue_iter = animation_queue_.begin();
515 queue_iter != animation_queue_.end(); ++queue_iter) 615 queue_iter != animation_queue_.end(); ++queue_iter)
516 sequences.push_back((*queue_iter)->AsWeakPtr()); 616 sequences.push_back((*queue_iter)->AsWeakPtr());
517 617
518 for (size_t i = 0; i < sequences.size(); ++i) { 618 for (size_t i = 0; i < sequences.size(); ++i) {
519 if (!sequences[i] || !HasAnimation(sequences[i])) 619 if (!sequences[i] || !HasAnimation(sequences[i]))
520 continue; 620 continue;
521 621
522 if (sequences[i]->HasCommonProperty(sequence->properties())) { 622 if (sequences[i]->HasCommonProperty(sequence->properties())) {
523 scoped_ptr<LayerAnimationSequence> removed(RemoveAnimation(sequences[i])); 623 scoped_ptr<LayerAnimationSequence> removed(RemoveAnimation(sequences[i]));
524 if (abort) 624 if (abort)
525 sequences[i]->Abort(); 625 sequences[i]->Abort(delegate());
526 else 626 else
527 ProgressAnimationToEnd(sequences[i]); 627 ProgressAnimationToEnd(sequences[i]);
528 } 628 }
529 } 629 }
530 } 630 }
531 631
532 void LayerAnimator::ImmediatelySetNewTarget(LayerAnimationSequence* sequence) { 632 void LayerAnimator::ImmediatelySetNewTarget(LayerAnimationSequence* sequence) {
533 // Need to detect if our sequence gets destroyed. 633 // Need to detect if our sequence gets destroyed.
534 base::WeakPtr<LayerAnimationSequence> weak_sequence_ptr = 634 base::WeakPtr<LayerAnimationSequence> weak_sequence_ptr =
535 sequence->AsWeakPtr(); 635 sequence->AsWeakPtr();
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
677 // last_tick_time() from there to ensure animations started during the same 777 // last_tick_time() from there to ensure animations started during the same
678 // event complete at the same time. 778 // event complete at the same time.
679 base::TimeTicks start_time; 779 base::TimeTicks start_time;
680 if (is_animating() || adding_animations_) 780 if (is_animating() || adding_animations_)
681 start_time = last_step_time_; 781 start_time = last_step_time_;
682 else if (GetAnimationContainer()->is_running()) 782 else if (GetAnimationContainer()->is_running())
683 start_time = GetAnimationContainer()->last_tick_time(); 783 start_time = GetAnimationContainer()->last_tick_time();
684 else 784 else
685 start_time = base::TimeTicks::Now(); 785 start_time = base::TimeTicks::Now();
686 786
687 sequence->set_start_time(start_time); 787 if (!sequence->animation_group_id())
788 sequence->set_animation_group_id(cc::AnimationIdProvider::NextGroupId());
789 if (sequence->waiting_for_group_start())
790 sequence->ProgressToEffectiveStart(delegate());
791 else
792 sequence->set_start_time(start_time);
688 running_animations_.push_back( 793 running_animations_.push_back(
689 RunningAnimation(sequence->AsWeakPtr())); 794 RunningAnimation(sequence->AsWeakPtr()));
690 795
691 // Need to keep a reference to the animation. 796 // Need to keep a reference to the animation.
692 AddToQueueIfNotPresent(sequence); 797 AddToQueueIfNotPresent(sequence);
693 798
694 // Ensure that animations get stepped at their start time. 799 // Ensure that animations get stepped at their start time.
695 Step(start_time); 800 Step(start_time);
696 801
697 return true; 802 return true;
(...skipping 28 matching lines...) Expand all
726 // Abort should never affect the set of running animations, but just in case 831 // Abort should never affect the set of running animations, but just in case
727 // clients are badly behaved, we will use a copy of the running animations. 832 // clients are badly behaved, we will use a copy of the running animations.
728 RunningAnimations running_animations_copy = running_animations_; 833 RunningAnimations running_animations_copy = running_animations_;
729 for (size_t i = 0; i < running_animations_copy.size(); ++i) { 834 for (size_t i = 0; i < running_animations_copy.size(); ++i) {
730 if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i])) 835 if (!SAFE_INVOKE_BOOL(HasAnimation, running_animations_copy[i]))
731 continue; 836 continue;
732 837
733 scoped_ptr<LayerAnimationSequence> removed( 838 scoped_ptr<LayerAnimationSequence> removed(
734 RemoveAnimation(running_animations_copy[i].sequence())); 839 RemoveAnimation(running_animations_copy[i].sequence()));
735 if (removed.get()) 840 if (removed.get())
736 removed->Abort(); 841 removed->Abort(delegate());
737 } 842 }
738 // This *should* have cleared the list of running animations. 843 // This *should* have cleared the list of running animations.
739 DCHECK(running_animations_.empty()); 844 DCHECK(running_animations_.empty());
740 running_animations_.clear(); 845 running_animations_.clear();
741 animation_queue_.clear(); 846 animation_queue_.clear();
742 UpdateAnimationState(); 847 UpdateAnimationState();
743 } 848 }
744 849
745 void LayerAnimator::PurgeDeletedAnimations() { 850 void LayerAnimator::PurgeDeletedAnimations() {
746 for (size_t i = 0; i < running_animations_.size();) { 851 for (size_t i = 0; i < running_animations_.size();) {
747 if (!running_animations_[i].is_sequence_alive()) 852 if (!running_animations_[i].is_sequence_alive())
748 running_animations_.erase(running_animations_.begin() + i); 853 running_animations_.erase(running_animations_.begin() + i);
749 else 854 else
750 i++; 855 i++;
751 } 856 }
752 } 857 }
753 858
754 LayerAnimator::RunningAnimation::RunningAnimation( 859 LayerAnimator::RunningAnimation::RunningAnimation(
755 const base::WeakPtr<LayerAnimationSequence>& sequence) 860 const base::WeakPtr<LayerAnimationSequence>& sequence)
756 : sequence_(sequence) { 861 : sequence_(sequence) {
757 } 862 }
758 863
759 LayerAnimator::RunningAnimation::~RunningAnimation() { } 864 LayerAnimator::RunningAnimation::~RunningAnimation() { }
760 865
761 } // namespace ui 866 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698