fbd02f38be914b2bdbd652e1e823aa00d7a04888
[platform/framework/web/crosswalk.git] / src / cc / animation / layer_animation_controller.cc
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
3 // found in the LICENSE file.
4
5 #include "cc/animation/layer_animation_controller.h"
6
7 #include <algorithm>
8
9 #include "cc/animation/animation.h"
10 #include "cc/animation/animation_delegate.h"
11 #include "cc/animation/animation_registrar.h"
12 #include "cc/animation/keyframed_animation_curve.h"
13 #include "cc/animation/layer_animation_value_observer.h"
14 #include "cc/animation/layer_animation_value_provider.h"
15 #include "cc/animation/scroll_offset_animation_curve.h"
16 #include "cc/base/scoped_ptr_algorithm.h"
17 #include "cc/output/filter_operations.h"
18 #include "ui/gfx/box_f.h"
19 #include "ui/gfx/transform.h"
20
21 namespace cc {
22
23 LayerAnimationController::LayerAnimationController(int id)
24     : registrar_(0),
25       id_(id),
26       is_active_(false),
27       value_provider_(NULL),
28       layer_animation_delegate_(NULL),
29       needs_to_start_animations_(false) {
30 }
31
32 LayerAnimationController::~LayerAnimationController() {
33   if (registrar_)
34     registrar_->UnregisterAnimationController(this);
35 }
36
37 scoped_refptr<LayerAnimationController> LayerAnimationController::Create(
38     int id) {
39   return make_scoped_refptr(new LayerAnimationController(id));
40 }
41
42 void LayerAnimationController::PauseAnimation(int animation_id,
43                                               base::TimeDelta time_offset) {
44   for (size_t i = 0; i < animations_.size(); ++i) {
45     if (animations_[i]->id() == animation_id) {
46       animations_[i]->SetRunState(Animation::Paused,
47                                   time_offset + animations_[i]->start_time());
48     }
49   }
50 }
51
52 struct HasAnimationId {
53   explicit HasAnimationId(int id) : id_(id) {}
54   bool operator()(Animation* animation) const {
55     return animation->id() == id_;
56   }
57
58  private:
59   int id_;
60 };
61
62 void LayerAnimationController::RemoveAnimation(int animation_id) {
63   animations_.erase(cc::remove_if(&animations_,
64                                   animations_.begin(),
65                                   animations_.end(),
66                                   HasAnimationId(animation_id)),
67                     animations_.end());
68   UpdateActivation(NormalActivation);
69 }
70
71 struct HasAnimationIdAndProperty {
72   HasAnimationIdAndProperty(int id, Animation::TargetProperty target_property)
73       : id_(id), target_property_(target_property) {}
74   bool operator()(Animation* animation) const {
75     return animation->id() == id_ &&
76         animation->target_property() == target_property_;
77   }
78
79  private:
80   int id_;
81   Animation::TargetProperty target_property_;
82 };
83
84 void LayerAnimationController::RemoveAnimation(
85     int animation_id,
86     Animation::TargetProperty target_property) {
87   animations_.erase(
88       cc::remove_if(&animations_,
89                     animations_.begin(),
90                     animations_.end(),
91                     HasAnimationIdAndProperty(animation_id, target_property)),
92       animations_.end());
93   UpdateActivation(NormalActivation);
94 }
95
96 void LayerAnimationController::AbortAnimations(
97     Animation::TargetProperty target_property) {
98   for (size_t i = 0; i < animations_.size(); ++i) {
99     if (animations_[i]->target_property() == target_property &&
100         !animations_[i]->is_finished())
101       animations_[i]->SetRunState(Animation::Aborted, last_tick_time_);
102   }
103 }
104
105 // Ensures that the list of active animations on the main thread and the impl
106 // thread are kept in sync.
107 void LayerAnimationController::PushAnimationUpdatesTo(
108     LayerAnimationController* controller_impl) {
109   DCHECK(this != controller_impl);
110   if (!has_any_animation() && !controller_impl->has_any_animation())
111     return;
112   PurgeAnimationsMarkedForDeletion();
113   PushNewAnimationsToImplThread(controller_impl);
114
115   // Remove finished impl side animations only after pushing,
116   // and only after the animations are deleted on the main thread
117   // this insures we will never push an animation twice.
118   RemoveAnimationsCompletedOnMainThread(controller_impl);
119
120   PushPropertiesToImplThread(controller_impl);
121   controller_impl->UpdateActivation(NormalActivation);
122   UpdateActivation(NormalActivation);
123 }
124
125 void LayerAnimationController::Animate(base::TimeTicks monotonic_time) {
126   DCHECK(!monotonic_time.is_null());
127   if (!HasValueObserver())
128     return;
129
130   if (needs_to_start_animations_)
131     StartAnimations(monotonic_time);
132   TickAnimations(monotonic_time);
133   last_tick_time_ = monotonic_time;
134 }
135
136 void LayerAnimationController::AccumulatePropertyUpdates(
137     base::TimeTicks monotonic_time,
138     AnimationEventsVector* events) {
139   if (!events)
140     return;
141
142   for (size_t i = 0; i < animations_.size(); ++i) {
143     Animation* animation = animations_[i];
144     if (!animation->is_impl_only())
145       continue;
146
147     double trimmed = animation->TrimTimeToCurrentIteration(monotonic_time);
148     switch (animation->target_property()) {
149       case Animation::Opacity: {
150         AnimationEvent event(AnimationEvent::PropertyUpdate,
151                              id_,
152                              animation->group(),
153                              Animation::Opacity,
154                              monotonic_time);
155         const FloatAnimationCurve* float_animation_curve =
156             animation->curve()->ToFloatAnimationCurve();
157         event.opacity = float_animation_curve->GetValue(trimmed);
158         event.is_impl_only = true;
159         events->push_back(event);
160         break;
161       }
162
163       case Animation::Transform: {
164         AnimationEvent event(AnimationEvent::PropertyUpdate,
165                              id_,
166                              animation->group(),
167                              Animation::Transform,
168                              monotonic_time);
169         const TransformAnimationCurve* transform_animation_curve =
170             animation->curve()->ToTransformAnimationCurve();
171         event.transform = transform_animation_curve->GetValue(trimmed);
172         event.is_impl_only = true;
173         events->push_back(event);
174         break;
175       }
176
177       case Animation::Filter: {
178         AnimationEvent event(AnimationEvent::PropertyUpdate,
179                              id_,
180                              animation->group(),
181                              Animation::Filter,
182                              monotonic_time);
183         const FilterAnimationCurve* filter_animation_curve =
184             animation->curve()->ToFilterAnimationCurve();
185         event.filters = filter_animation_curve->GetValue(trimmed);
186         event.is_impl_only = true;
187         events->push_back(event);
188         break;
189       }
190
191       case Animation::BackgroundColor: { break; }
192
193       case Animation::ScrollOffset: {
194         // Impl-side changes to scroll offset are already sent back to the
195         // main thread (e.g. for user-driven scrolling), so a PropertyUpdate
196         // isn't needed.
197         break;
198       }
199
200       case Animation::TargetPropertyEnumSize:
201         NOTREACHED();
202     }
203   }
204 }
205
206 void LayerAnimationController::UpdateState(bool start_ready_animations,
207                                            AnimationEventsVector* events) {
208   if (!HasActiveValueObserver())
209     return;
210
211   DCHECK(last_tick_time_ != base::TimeTicks());
212   if (start_ready_animations)
213     PromoteStartedAnimations(last_tick_time_, events);
214
215   MarkFinishedAnimations(last_tick_time_);
216   MarkAnimationsForDeletion(last_tick_time_, events);
217
218   if (needs_to_start_animations_ && start_ready_animations) {
219     StartAnimations(last_tick_time_);
220     PromoteStartedAnimations(last_tick_time_, events);
221   }
222
223   AccumulatePropertyUpdates(last_tick_time_, events);
224
225   UpdateActivation(NormalActivation);
226 }
227
228 struct AffectsNoObservers {
229   bool operator()(Animation* animation) const {
230     return !animation->affects_active_observers() &&
231            !animation->affects_pending_observers();
232   }
233 };
234
235 void LayerAnimationController::ActivateAnimations() {
236   for (size_t i = 0; i < animations_.size(); ++i) {
237     animations_[i]->set_affects_active_observers(
238         animations_[i]->affects_pending_observers());
239   }
240   animations_.erase(cc::remove_if(&animations_,
241                                   animations_.begin(),
242                                   animations_.end(),
243                                   AffectsNoObservers()),
244                     animations_.end());
245   UpdateActivation(NormalActivation);
246 }
247
248 void LayerAnimationController::AddAnimation(scoped_ptr<Animation> animation) {
249   animations_.push_back(animation.Pass());
250   needs_to_start_animations_ = true;
251   UpdateActivation(NormalActivation);
252 }
253
254 Animation* LayerAnimationController::GetAnimation(
255     int group_id,
256     Animation::TargetProperty target_property) const {
257   for (size_t i = 0; i < animations_.size(); ++i)
258     if (animations_[i]->group() == group_id &&
259         animations_[i]->target_property() == target_property)
260       return animations_[i];
261   return 0;
262 }
263
264 Animation* LayerAnimationController::GetAnimation(
265     Animation::TargetProperty target_property) const {
266   for (size_t i = 0; i < animations_.size(); ++i) {
267     size_t index = animations_.size() - i - 1;
268     if (animations_[index]->target_property() == target_property)
269       return animations_[index];
270   }
271   return 0;
272 }
273
274 bool LayerAnimationController::HasActiveAnimation() const {
275   for (size_t i = 0; i < animations_.size(); ++i) {
276     if (!animations_[i]->is_finished())
277       return true;
278   }
279   return false;
280 }
281
282 bool LayerAnimationController::IsAnimatingProperty(
283     Animation::TargetProperty target_property) const {
284   for (size_t i = 0; i < animations_.size(); ++i) {
285     if (!animations_[i]->is_finished() &&
286         animations_[i]->target_property() == target_property)
287       return true;
288   }
289   return false;
290 }
291
292 void LayerAnimationController::SetAnimationRegistrar(
293     AnimationRegistrar* registrar) {
294   if (registrar_ == registrar)
295     return;
296
297   if (registrar_)
298     registrar_->UnregisterAnimationController(this);
299
300   registrar_ = registrar;
301   if (registrar_)
302     registrar_->RegisterAnimationController(this);
303
304   UpdateActivation(ForceActivation);
305 }
306
307 void LayerAnimationController::NotifyAnimationStarted(
308     const AnimationEvent& event) {
309   if (event.is_impl_only) {
310     FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_,
311                       OnAnimationStarted(event));
312     if (layer_animation_delegate_)
313       layer_animation_delegate_->NotifyAnimationStarted(event.monotonic_time,
314                                                         event.target_property);
315     return;
316   }
317
318   for (size_t i = 0; i < animations_.size(); ++i) {
319     if (animations_[i]->group() == event.group_id &&
320         animations_[i]->target_property() == event.target_property &&
321         animations_[i]->needs_synchronized_start_time()) {
322       animations_[i]->set_needs_synchronized_start_time(false);
323       if (!animations_[i]->has_set_start_time())
324         animations_[i]->set_start_time(event.monotonic_time);
325
326       FOR_EACH_OBSERVER(LayerAnimationEventObserver, event_observers_,
327                         OnAnimationStarted(event));
328       if (layer_animation_delegate_)
329         layer_animation_delegate_->NotifyAnimationStarted(
330             event.monotonic_time, event.target_property);
331
332       return;
333     }
334   }
335 }
336
337 void LayerAnimationController::NotifyAnimationFinished(
338     const AnimationEvent& event) {
339   if (event.is_impl_only) {
340     if (layer_animation_delegate_)
341       layer_animation_delegate_->NotifyAnimationFinished(event.monotonic_time,
342                                                          event.target_property);
343     return;
344   }
345
346   for (size_t i = 0; i < animations_.size(); ++i) {
347     if (animations_[i]->group() == event.group_id &&
348         animations_[i]->target_property() == event.target_property) {
349       animations_[i]->set_received_finished_event(true);
350       if (layer_animation_delegate_)
351         layer_animation_delegate_->NotifyAnimationFinished(
352             event.monotonic_time, event.target_property);
353
354       return;
355     }
356   }
357 }
358
359 void LayerAnimationController::NotifyAnimationAborted(
360     const AnimationEvent& event) {
361   for (size_t i = 0; i < animations_.size(); ++i) {
362     if (animations_[i]->group() == event.group_id &&
363         animations_[i]->target_property() == event.target_property) {
364       animations_[i]->SetRunState(Animation::Aborted, event.monotonic_time);
365     }
366   }
367 }
368
369 void LayerAnimationController::NotifyAnimationPropertyUpdate(
370     const AnimationEvent& event) {
371   bool notify_active_observers = true;
372   bool notify_pending_observers = true;
373   switch (event.target_property) {
374     case Animation::Opacity:
375       NotifyObserversOpacityAnimated(
376           event.opacity, notify_active_observers, notify_pending_observers);
377       break;
378     case Animation::Transform:
379       NotifyObserversTransformAnimated(
380           event.transform, notify_active_observers, notify_pending_observers);
381       break;
382     default:
383       NOTREACHED();
384   }
385 }
386
387 void LayerAnimationController::AddValueObserver(
388     LayerAnimationValueObserver* observer) {
389   if (!value_observers_.HasObserver(observer))
390     value_observers_.AddObserver(observer);
391 }
392
393 void LayerAnimationController::RemoveValueObserver(
394     LayerAnimationValueObserver* observer) {
395   value_observers_.RemoveObserver(observer);
396 }
397
398 void LayerAnimationController::AddEventObserver(
399     LayerAnimationEventObserver* observer) {
400   if (!event_observers_.HasObserver(observer))
401     event_observers_.AddObserver(observer);
402 }
403
404 void LayerAnimationController::RemoveEventObserver(
405     LayerAnimationEventObserver* observer) {
406   event_observers_.RemoveObserver(observer);
407 }
408
409 bool LayerAnimationController::HasFilterAnimationThatInflatesBounds() const {
410   for (size_t i = 0; i < animations_.size(); ++i) {
411     if (!animations_[i]->is_finished() &&
412         animations_[i]->target_property() == Animation::Filter &&
413         animations_[i]
414             ->curve()
415             ->ToFilterAnimationCurve()
416             ->HasFilterThatMovesPixels())
417       return true;
418   }
419
420   return false;
421 }
422
423 bool LayerAnimationController::HasTransformAnimationThatInflatesBounds() const {
424   return IsAnimatingProperty(Animation::Transform);
425 }
426
427 bool LayerAnimationController::FilterAnimationBoundsForBox(
428     const gfx::BoxF& box, gfx::BoxF* bounds) const {
429   // TODO(avallee): Implement.
430   return false;
431 }
432
433 bool LayerAnimationController::TransformAnimationBoundsForBox(
434     const gfx::BoxF& box,
435     gfx::BoxF* bounds) const {
436   DCHECK(HasTransformAnimationThatInflatesBounds())
437       << "TransformAnimationBoundsForBox will give incorrect results if there "
438       << "are no transform animations affecting bounds, non-animated transform "
439       << "is not known";
440
441   // Compute bounds based on animations for which is_finished() is false.
442   // Do nothing if there are no such animations; in this case, it is assumed
443   // that callers will take care of computing bounds based on the owning layer's
444   // actual transform.
445   *bounds = gfx::BoxF();
446   for (size_t i = 0; i < animations_.size(); ++i) {
447     if (animations_[i]->is_finished() ||
448         animations_[i]->target_property() != Animation::Transform)
449       continue;
450
451     const TransformAnimationCurve* transform_animation_curve =
452         animations_[i]->curve()->ToTransformAnimationCurve();
453     gfx::BoxF animation_bounds;
454     bool success =
455         transform_animation_curve->AnimatedBoundsForBox(box, &animation_bounds);
456     if (!success)
457       return false;
458     bounds->Union(animation_bounds);
459   }
460
461   return true;
462 }
463
464 bool LayerAnimationController::HasAnimationThatAffectsScale() const {
465   for (size_t i = 0; i < animations_.size(); ++i) {
466     if (animations_[i]->is_finished() ||
467         animations_[i]->target_property() != Animation::Transform)
468       continue;
469
470     const TransformAnimationCurve* transform_animation_curve =
471         animations_[i]->curve()->ToTransformAnimationCurve();
472     if (transform_animation_curve->AffectsScale())
473       return true;
474   }
475
476   return false;
477 }
478
479 bool LayerAnimationController::HasOnlyTranslationTransforms() const {
480   for (size_t i = 0; i < animations_.size(); ++i) {
481     if (animations_[i]->is_finished() ||
482         animations_[i]->target_property() != Animation::Transform)
483       continue;
484
485     const TransformAnimationCurve* transform_animation_curve =
486         animations_[i]->curve()->ToTransformAnimationCurve();
487     if (!transform_animation_curve->IsTranslation())
488       return false;
489   }
490
491   return true;
492 }
493
494 bool LayerAnimationController::MaximumScale(float* max_scale) const {
495   *max_scale = 0.f;
496   for (size_t i = 0; i < animations_.size(); ++i) {
497     if (animations_[i]->is_finished() ||
498         animations_[i]->target_property() != Animation::Transform)
499       continue;
500
501     const TransformAnimationCurve* transform_animation_curve =
502         animations_[i]->curve()->ToTransformAnimationCurve();
503     float animation_scale = 0.f;
504     if (!transform_animation_curve->MaximumScale(&animation_scale))
505       return false;
506     *max_scale = std::max(*max_scale, animation_scale);
507   }
508
509   return true;
510 }
511
512 void LayerAnimationController::PushNewAnimationsToImplThread(
513     LayerAnimationController* controller_impl) const {
514   // Any new animations owned by the main thread's controller are cloned and
515   // add to the impl thread's controller.
516   for (size_t i = 0; i < animations_.size(); ++i) {
517     // If the animation is already running on the impl thread, there is no
518     // need to copy it over.
519     if (controller_impl->GetAnimation(animations_[i]->group(),
520                                       animations_[i]->target_property()))
521       continue;
522
523     // If the animation is not running on the impl thread, it does not
524     // necessarily mean that it needs to be copied over and started; it may
525     // have already finished. In this case, the impl thread animation will
526     // have already notified that it has started and the main thread animation
527     // will no longer need
528     // a synchronized start time.
529     if (!animations_[i]->needs_synchronized_start_time())
530       continue;
531
532     // Scroll animations always start at the current scroll offset.
533     if (animations_[i]->target_property() == Animation::ScrollOffset) {
534       gfx::Vector2dF current_scroll_offset;
535       if (controller_impl->value_provider_) {
536         current_scroll_offset =
537             controller_impl->value_provider_->ScrollOffsetForAnimation();
538       } else {
539         // The owning layer isn't yet in the active tree, so the main thread
540         // scroll offset will be up-to-date.
541         current_scroll_offset = value_provider_->ScrollOffsetForAnimation();
542       }
543       animations_[i]->curve()->ToScrollOffsetAnimationCurve()->SetInitialValue(
544           current_scroll_offset);
545     }
546
547     // The new animation should be set to run as soon as possible.
548     Animation::RunState initial_run_state =
549         Animation::WaitingForTargetAvailability;
550     scoped_ptr<Animation> to_add(
551         animations_[i]->CloneAndInitialize(initial_run_state));
552     DCHECK(!to_add->needs_synchronized_start_time());
553     to_add->set_affects_active_observers(false);
554     controller_impl->AddAnimation(to_add.Pass());
555   }
556 }
557
558 static bool IsCompleted(
559     Animation* animation,
560     const LayerAnimationController* main_thread_controller) {
561   if (animation->is_impl_only()) {
562     return (animation->run_state() == Animation::WaitingForDeletion);
563   } else {
564     return !main_thread_controller->GetAnimation(animation->group(),
565                                                  animation->target_property());
566   }
567 }
568
569 static bool AffectsActiveOnlyAndIsWaitingForDeletion(Animation* animation) {
570   return animation->run_state() == Animation::WaitingForDeletion &&
571          !animation->affects_pending_observers();
572 }
573
574 void LayerAnimationController::RemoveAnimationsCompletedOnMainThread(
575     LayerAnimationController* controller_impl) const {
576   // Animations removed on the main thread should no longer affect pending
577   // observers, and should stop affecting active observers after the next call
578   // to ActivateAnimations. If already WaitingForDeletion, they can be removed
579   // immediately.
580   ScopedPtrVector<Animation>& animations = controller_impl->animations_;
581   for (size_t i = 0; i < animations.size(); ++i) {
582     if (IsCompleted(animations[i], this))
583       animations[i]->set_affects_pending_observers(false);
584   }
585   animations.erase(cc::remove_if(&animations,
586                                  animations.begin(),
587                                  animations.end(),
588                                  AffectsActiveOnlyAndIsWaitingForDeletion),
589                    animations.end());
590 }
591
592 void LayerAnimationController::PushPropertiesToImplThread(
593     LayerAnimationController* controller_impl) const {
594   for (size_t i = 0; i < animations_.size(); ++i) {
595     Animation* current_impl = controller_impl->GetAnimation(
596         animations_[i]->group(), animations_[i]->target_property());
597     if (current_impl)
598       animations_[i]->PushPropertiesTo(current_impl);
599   }
600 }
601
602 void LayerAnimationController::StartAnimations(base::TimeTicks monotonic_time) {
603   DCHECK(needs_to_start_animations_);
604   needs_to_start_animations_ = false;
605   // First collect running properties affecting each type of observer.
606   TargetProperties blocked_properties_for_active_observers;
607   TargetProperties blocked_properties_for_pending_observers;
608   std::vector<size_t> animations_waiting_for_target;
609
610   animations_waiting_for_target.reserve(animations_.size());
611   for (size_t i = 0; i < animations_.size(); ++i) {
612     if (animations_[i]->run_state() == Animation::Starting ||
613         animations_[i]->run_state() == Animation::Running) {
614       if (animations_[i]->affects_active_observers()) {
615         blocked_properties_for_active_observers.insert(
616             animations_[i]->target_property());
617       }
618       if (animations_[i]->affects_pending_observers()) {
619         blocked_properties_for_pending_observers.insert(
620             animations_[i]->target_property());
621       }
622     } else if (animations_[i]->run_state() ==
623                Animation::WaitingForTargetAvailability) {
624       animations_waiting_for_target.push_back(i);
625     }
626   }
627
628   for (size_t i = 0; i < animations_waiting_for_target.size(); ++i) {
629       // Collect all properties for animations with the same group id (they
630       // should all also be in the list of animations).
631     size_t animation_index = animations_waiting_for_target[i];
632     Animation* animation_waiting_for_target = animations_[animation_index];
633     // Check for the run state again even though the animation was waiting
634     // for target because it might have changed the run state while handling
635     // previous animation in this loop (if they belong to same group).
636     if (animation_waiting_for_target->run_state() ==
637         Animation::WaitingForTargetAvailability) {
638       TargetProperties enqueued_properties;
639       bool affects_active_observers =
640           animation_waiting_for_target->affects_active_observers();
641       bool affects_pending_observers =
642           animation_waiting_for_target->affects_pending_observers();
643       enqueued_properties.insert(
644           animation_waiting_for_target->target_property());
645       for (size_t j = animation_index + 1; j < animations_.size(); ++j) {
646         if (animation_waiting_for_target->group() == animations_[j]->group()) {
647           enqueued_properties.insert(animations_[j]->target_property());
648           affects_active_observers |=
649               animations_[j]->affects_active_observers();
650           affects_pending_observers |=
651               animations_[j]->affects_pending_observers();
652         }
653       }
654
655       // Check to see if intersection of the list of properties affected by
656       // the group and the list of currently blocked properties is null, taking
657       // into account the type(s) of observers affected by the group. In any
658       // case, the group's target properties need to be added to the lists of
659       // blocked properties.
660       bool null_intersection = true;
661       for (TargetProperties::iterator p_iter = enqueued_properties.begin();
662            p_iter != enqueued_properties.end();
663            ++p_iter) {
664         if (affects_active_observers &&
665             !blocked_properties_for_active_observers.insert(*p_iter).second)
666           null_intersection = false;
667         if (affects_pending_observers &&
668             !blocked_properties_for_pending_observers.insert(*p_iter).second)
669           null_intersection = false;
670       }
671
672       // If the intersection is null, then we are free to start the animations
673       // in the group.
674       if (null_intersection) {
675         animation_waiting_for_target->SetRunState(Animation::Starting,
676                                                   monotonic_time);
677         for (size_t j = animation_index + 1; j < animations_.size(); ++j) {
678           if (animation_waiting_for_target->group() ==
679               animations_[j]->group()) {
680             animations_[j]->SetRunState(Animation::Starting, monotonic_time);
681           }
682         }
683       } else {
684         needs_to_start_animations_ = true;
685       }
686     }
687   }
688 }
689
690 void LayerAnimationController::PromoteStartedAnimations(
691     base::TimeTicks monotonic_time,
692     AnimationEventsVector* events) {
693   for (size_t i = 0; i < animations_.size(); ++i) {
694     if (animations_[i]->run_state() == Animation::Starting &&
695         animations_[i]->affects_active_observers()) {
696       animations_[i]->SetRunState(Animation::Running, monotonic_time);
697       if (!animations_[i]->has_set_start_time() &&
698           !animations_[i]->needs_synchronized_start_time())
699         animations_[i]->set_start_time(monotonic_time);
700       if (events) {
701         AnimationEvent started_event(AnimationEvent::Started,
702                                      id_,
703                                      animations_[i]->group(),
704                                      animations_[i]->target_property(),
705                                      monotonic_time);
706         started_event.is_impl_only = animations_[i]->is_impl_only();
707         if (started_event.is_impl_only)
708           NotifyAnimationStarted(started_event);
709         else
710           events->push_back(started_event);
711       }
712     }
713   }
714 }
715
716 void LayerAnimationController::MarkFinishedAnimations(
717     base::TimeTicks monotonic_time) {
718   for (size_t i = 0; i < animations_.size(); ++i) {
719     if (animations_[i]->IsFinishedAt(monotonic_time) &&
720         animations_[i]->run_state() != Animation::Aborted &&
721         animations_[i]->run_state() != Animation::WaitingForDeletion)
722       animations_[i]->SetRunState(Animation::Finished, monotonic_time);
723   }
724 }
725
726 void LayerAnimationController::MarkAnimationsForDeletion(
727     base::TimeTicks monotonic_time,
728     AnimationEventsVector* events) {
729   bool marked_animations_for_deletions = false;
730   std::vector<size_t> animations_with_same_group_id;
731
732   animations_with_same_group_id.reserve(animations_.size());
733   // Non-aborted animations are marked for deletion after a corresponding
734   // AnimationEvent::Finished event is sent or received. This means that if
735   // we don't have an events vector, we must ensure that non-aborted animations
736   // have received a finished event before marking them for deletion.
737   for (size_t i = 0; i < animations_.size(); i++) {
738     int group_id = animations_[i]->group();
739     if (animations_[i]->run_state() == Animation::Aborted) {
740       if (events && !animations_[i]->is_impl_only()) {
741         AnimationEvent aborted_event(AnimationEvent::Aborted,
742                                      id_,
743                                      group_id,
744                                      animations_[i]->target_property(),
745                                      monotonic_time);
746         events->push_back(aborted_event);
747       }
748       animations_[i]->SetRunState(Animation::WaitingForDeletion,
749                                   monotonic_time);
750       marked_animations_for_deletions = true;
751       continue;
752     }
753
754     bool all_anims_with_same_id_are_finished = false;
755
756     // Since deleting an animation on the main thread leads to its deletion
757     // on the impl thread, we only mark a Finished main thread animation for
758     // deletion once it has received a Finished event from the impl thread.
759     bool animation_i_will_send_or_has_received_finish_event =
760         events || animations_[i]->received_finished_event();
761     // If an animation is finished, and not already marked for deletion,
762     // find out if all other animations in the same group are also finished.
763     if (animations_[i]->run_state() == Animation::Finished &&
764         animation_i_will_send_or_has_received_finish_event) {
765       // Clear the animations_with_same_group_id if it was added for
766       // the previous animation's iteration.
767       if (animations_with_same_group_id.size() > 0)
768         animations_with_same_group_id.clear();
769       all_anims_with_same_id_are_finished = true;
770       for (size_t j = 0; j < animations_.size(); ++j) {
771         bool animation_j_will_send_or_has_received_finish_event =
772             events || animations_[j]->received_finished_event();
773         if (group_id == animations_[j]->group()) {
774           if (!animations_[j]->is_finished() ||
775               (animations_[j]->run_state() == Animation::Finished &&
776                !animation_j_will_send_or_has_received_finish_event)) {
777             all_anims_with_same_id_are_finished = false;
778             break;
779           } else if (j >= i &&
780                      animations_[j]->run_state() != Animation::Aborted) {
781             // Mark down the animations which belong to the same group
782             // and is not yet aborted. If this current iteration finds that all
783             // animations with same ID are finished, then the marked
784             // animations below will be set to WaitingForDeletion in next
785             // iteration.
786             animations_with_same_group_id.push_back(j);
787           }
788         }
789       }
790     }
791     if (all_anims_with_same_id_are_finished) {
792       // We now need to remove all animations with the same group id as
793       // group_id (and send along animation finished notifications, if
794       // necessary).
795       for (size_t j = 0; j < animations_with_same_group_id.size(); j++) {
796         size_t animation_index = animations_with_same_group_id[j];
797           if (events) {
798             AnimationEvent finished_event(
799                 AnimationEvent::Finished,
800                 id_,
801                 animations_[animation_index]->group(),
802                 animations_[animation_index]->target_property(),
803                 monotonic_time);
804             finished_event.is_impl_only =
805                 animations_[animation_index]->is_impl_only();
806             if (finished_event.is_impl_only)
807               NotifyAnimationFinished(finished_event);
808             else
809               events->push_back(finished_event);
810           }
811           animations_[animation_index]->SetRunState(
812               Animation::WaitingForDeletion, monotonic_time);
813       }
814       marked_animations_for_deletions = true;
815     }
816   }
817   if (marked_animations_for_deletions)
818     NotifyObserversAnimationWaitingForDeletion();
819 }
820
821 static bool IsWaitingForDeletion(Animation* animation) {
822   return animation->run_state() == Animation::WaitingForDeletion;
823 }
824
825 void LayerAnimationController::PurgeAnimationsMarkedForDeletion() {
826   animations_.erase(cc::remove_if(&animations_,
827                                   animations_.begin(),
828                                   animations_.end(),
829                                   IsWaitingForDeletion),
830                     animations_.end());
831 }
832
833 void LayerAnimationController::TickAnimations(base::TimeTicks monotonic_time) {
834   for (size_t i = 0; i < animations_.size(); ++i) {
835     if (animations_[i]->run_state() == Animation::Starting ||
836         animations_[i]->run_state() == Animation::Running ||
837         animations_[i]->run_state() == Animation::Paused) {
838       double trimmed =
839           animations_[i]->TrimTimeToCurrentIteration(monotonic_time);
840
841       switch (animations_[i]->target_property()) {
842         case Animation::Transform: {
843           const TransformAnimationCurve* transform_animation_curve =
844               animations_[i]->curve()->ToTransformAnimationCurve();
845           const gfx::Transform transform =
846               transform_animation_curve->GetValue(trimmed);
847           NotifyObserversTransformAnimated(
848               transform,
849               animations_[i]->affects_active_observers(),
850               animations_[i]->affects_pending_observers());
851           break;
852         }
853
854         case Animation::Opacity: {
855           const FloatAnimationCurve* float_animation_curve =
856               animations_[i]->curve()->ToFloatAnimationCurve();
857           const float opacity = std::max(
858               std::min(float_animation_curve->GetValue(trimmed), 1.0f), 0.f);
859           NotifyObserversOpacityAnimated(
860               opacity,
861               animations_[i]->affects_active_observers(),
862               animations_[i]->affects_pending_observers());
863           break;
864         }
865
866         case Animation::Filter: {
867           const FilterAnimationCurve* filter_animation_curve =
868               animations_[i]->curve()->ToFilterAnimationCurve();
869           const FilterOperations filter =
870               filter_animation_curve->GetValue(trimmed);
871           NotifyObserversFilterAnimated(
872               filter,
873               animations_[i]->affects_active_observers(),
874               animations_[i]->affects_pending_observers());
875           break;
876         }
877
878         case Animation::BackgroundColor: {
879           // Not yet implemented.
880           break;
881         }
882
883         case Animation::ScrollOffset: {
884           const ScrollOffsetAnimationCurve* scroll_offset_animation_curve =
885               animations_[i]->curve()->ToScrollOffsetAnimationCurve();
886           const gfx::Vector2dF scroll_offset =
887               scroll_offset_animation_curve->GetValue(trimmed);
888           NotifyObserversScrollOffsetAnimated(
889               scroll_offset,
890               animations_[i]->affects_active_observers(),
891               animations_[i]->affects_pending_observers());
892           break;
893         }
894
895         // Do nothing for sentinel value.
896         case Animation::TargetPropertyEnumSize:
897           NOTREACHED();
898       }
899     }
900   }
901 }
902
903 void LayerAnimationController::UpdateActivation(UpdateActivationType type) {
904   bool force = type == ForceActivation;
905   if (registrar_) {
906     bool was_active = is_active_;
907     is_active_ = false;
908     for (size_t i = 0; i < animations_.size(); ++i) {
909       if (animations_[i]->run_state() != Animation::WaitingForDeletion) {
910         is_active_ = true;
911         break;
912       }
913     }
914
915     if (is_active_ && (!was_active || force))
916       registrar_->DidActivateAnimationController(this);
917     else if (!is_active_ && (was_active || force))
918       registrar_->DidDeactivateAnimationController(this);
919   }
920 }
921
922 void LayerAnimationController::NotifyObserversOpacityAnimated(
923     float opacity,
924     bool notify_active_observers,
925     bool notify_pending_observers) {
926   if (value_observers_.might_have_observers()) {
927     ObserverListBase<LayerAnimationValueObserver>::Iterator it(
928         value_observers_);
929     LayerAnimationValueObserver* obs;
930     while ((obs = it.GetNext()) != NULL) {
931       if ((notify_active_observers && notify_pending_observers) ||
932           (notify_active_observers && obs->IsActive()) ||
933           (notify_pending_observers && !obs->IsActive()))
934         obs->OnOpacityAnimated(opacity);
935     }
936   }
937 }
938
939 void LayerAnimationController::NotifyObserversTransformAnimated(
940     const gfx::Transform& transform,
941     bool notify_active_observers,
942     bool notify_pending_observers) {
943   if (value_observers_.might_have_observers()) {
944     ObserverListBase<LayerAnimationValueObserver>::Iterator it(
945         value_observers_);
946     LayerAnimationValueObserver* obs;
947     while ((obs = it.GetNext()) != NULL) {
948       if ((notify_active_observers && notify_pending_observers) ||
949           (notify_active_observers && obs->IsActive()) ||
950           (notify_pending_observers && !obs->IsActive()))
951         obs->OnTransformAnimated(transform);
952     }
953   }
954 }
955
956 void LayerAnimationController::NotifyObserversFilterAnimated(
957     const FilterOperations& filters,
958     bool notify_active_observers,
959     bool notify_pending_observers) {
960   if (value_observers_.might_have_observers()) {
961     ObserverListBase<LayerAnimationValueObserver>::Iterator it(
962         value_observers_);
963     LayerAnimationValueObserver* obs;
964     while ((obs = it.GetNext()) != NULL) {
965       if ((notify_active_observers && notify_pending_observers) ||
966           (notify_active_observers && obs->IsActive()) ||
967           (notify_pending_observers && !obs->IsActive()))
968         obs->OnFilterAnimated(filters);
969     }
970   }
971 }
972
973 void LayerAnimationController::NotifyObserversScrollOffsetAnimated(
974     const gfx::Vector2dF& scroll_offset,
975     bool notify_active_observers,
976     bool notify_pending_observers) {
977   if (value_observers_.might_have_observers()) {
978     ObserverListBase<LayerAnimationValueObserver>::Iterator it(
979         value_observers_);
980     LayerAnimationValueObserver* obs;
981     while ((obs = it.GetNext()) != NULL) {
982       if ((notify_active_observers && notify_pending_observers) ||
983           (notify_active_observers && obs->IsActive()) ||
984           (notify_pending_observers && !obs->IsActive()))
985         obs->OnScrollOffsetAnimated(scroll_offset);
986     }
987   }
988 }
989
990 void LayerAnimationController::NotifyObserversAnimationWaitingForDeletion() {
991   FOR_EACH_OBSERVER(LayerAnimationValueObserver,
992                     value_observers_,
993                     OnAnimationWaitingForDeletion());
994 }
995
996 bool LayerAnimationController::HasValueObserver() {
997   if (value_observers_.might_have_observers()) {
998     ObserverListBase<LayerAnimationValueObserver>::Iterator it(
999         value_observers_);
1000     return it.GetNext() != NULL;
1001   }
1002   return false;
1003 }
1004
1005 bool LayerAnimationController::HasActiveValueObserver() {
1006   if (value_observers_.might_have_observers()) {
1007     ObserverListBase<LayerAnimationValueObserver>::Iterator it(
1008         value_observers_);
1009     LayerAnimationValueObserver* obs;
1010     while ((obs = it.GetNext()) != NULL)
1011       if (obs->IsActive())
1012         return true;
1013   }
1014   return false;
1015 }
1016
1017 }  // namespace cc