15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/layer_animation_sequence.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <iterator>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/debug/trace_event.h"
113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "cc/animation/animation_id_provider.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/layer_animation_delegate.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/layer_animation_element.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/layer_animation_observer.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ui {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LayerAnimationSequence::LayerAnimationSequence()
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : properties_(LayerAnimationElement::UNKNOWN),
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      is_cyclic_(false),
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      last_element_(0),
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      waiting_for_group_start_(false),
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      animation_group_id_(0),
244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      last_progressed_fraction_(0.0),
254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      weak_ptr_factory_(this) {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LayerAnimationSequence::LayerAnimationSequence(LayerAnimationElement* element)
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : properties_(LayerAnimationElement::UNKNOWN),
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      is_cyclic_(false),
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      last_element_(0),
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      waiting_for_group_start_(false),
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      animation_group_id_(0),
344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      last_progressed_fraction_(0.0),
354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      weak_ptr_factory_(this) {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AddElement(element);
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LayerAnimationSequence::~LayerAnimationSequence() {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(LayerAnimationObserver,
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    observers_,
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    DetachedFromSequence(this, true));
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void LayerAnimationSequence::Start(LayerAnimationDelegate* delegate) {
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(start_time_ != base::TimeTicks());
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  last_progressed_fraction_ = 0.0;
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (elements_.empty())
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  elements_[0]->set_requested_start_time(start_time_);
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  elements_[0]->Start(delegate, animation_group_id_);
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void LayerAnimationSequence::Progress(base::TimeTicks now,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      LayerAnimationDelegate* delegate) {
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(start_time_ != base::TimeTicks());
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool redraw_required = false;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (elements_.empty())
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (last_element_ == 0)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    last_start_ = start_time_;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t current_index = last_element_ % elements_.size();
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::TimeDelta element_duration;
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  while (is_cyclic_ || last_element_ < elements_.size()) {
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    elements_[current_index]->set_requested_start_time(last_start_);
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!elements_[current_index]->IsFinished(now, &element_duration))
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Let the element we're passing finish.
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (elements_[current_index]->ProgressToEnd(delegate))
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      redraw_required = true;
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    last_start_ += element_duration;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ++last_element_;
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    last_progressed_fraction_ =
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        elements_[current_index]->last_progressed_fraction();
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    current_index = last_element_ % elements_.size();
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (is_cyclic_ || last_element_ < elements_.size()) {
843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (!elements_[current_index]->Started()) {
853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      animation_group_id_ = cc::AnimationIdProvider::NextGroupId();
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      elements_[current_index]->Start(delegate, animation_group_id_);
873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    base::WeakPtr<LayerAnimationSequence> alive(weak_ptr_factory_.GetWeakPtr());
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (elements_[current_index]->Progress(now, delegate))
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      redraw_required = true;
914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (!alive)
924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return;
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    last_progressed_fraction_ =
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        elements_[current_index]->last_progressed_fraction();
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Since the delegate may be deleted due to the notifications below, it is
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // important that we schedule a draw before sending them.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (redraw_required)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delegate->ScheduleDrawForAnimation();
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!is_cyclic_ && last_element_ == elements_.size()) {
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    last_element_ = 0;
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    waiting_for_group_start_ = false;
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    animation_group_id_ = 0;
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NotifyEnded();
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool LayerAnimationSequence::IsFinished(base::TimeTicks time) {
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (is_cyclic_ || waiting_for_group_start_)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (elements_.empty())
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (last_element_ == 0)
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    last_start_ = start_time_;
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::TimeTicks current_start = last_start_;
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t current_index = last_element_;
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::TimeDelta element_duration;
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  while (current_index < elements_.size()) {
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    elements_[current_index]->set_requested_start_time(current_start);
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!elements_[current_index]->IsFinished(time, &element_duration))
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    current_start += element_duration;
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ++current_index;
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return (current_index == elements_.size());
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void LayerAnimationSequence::ProgressToEnd(LayerAnimationDelegate* delegate) {
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool redraw_required = false;
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (elements_.empty())
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t current_index = last_element_ % elements_.size();
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  while (current_index < elements_.size()) {
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (elements_[current_index]->ProgressToEnd(delegate))
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      redraw_required = true;
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    last_progressed_fraction_ =
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        elements_[current_index]->last_progressed_fraction();
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ++current_index;
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ++last_element_;
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (redraw_required)
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    delegate->ScheduleDrawForAnimation();
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!is_cyclic_) {
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    last_element_ = 0;
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    waiting_for_group_start_ = false;
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    animation_group_id_ = 0;
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NotifyEnded();
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LayerAnimationSequence::GetTargetValue(
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LayerAnimationElement::TargetValue* target) const {
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (is_cyclic_)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = last_element_; i < elements_.size(); ++i)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    elements_[i]->GetTargetValue(target);
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void LayerAnimationSequence::Abort(LayerAnimationDelegate* delegate) {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t current_index = last_element_ % elements_.size();
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (current_index < elements_.size()) {
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    elements_[current_index]->Abort(delegate);
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ++current_index;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  last_element_ = 0;
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  waiting_for_group_start_ = false;
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NotifyAborted();
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LayerAnimationSequence::AddElement(LayerAnimationElement* element) {
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  properties_ |= element->properties();
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  elements_.push_back(make_linked_ptr(element));
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool LayerAnimationSequence::HasConflictingProperty(
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LayerAnimationElement::AnimatableProperties other) const {
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return (properties_ & other) != LayerAnimationElement::UNKNOWN;
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool LayerAnimationSequence::IsFirstElementThreaded() const {
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!elements_.empty())
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return elements_[0]->IsThreaded();
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LayerAnimationSequence::AddObserver(LayerAnimationObserver* observer) {
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!observers_.HasObserver(observer)) {
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    observers_.AddObserver(observer);
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    observer->AttachedToSequence(this);
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LayerAnimationSequence::RemoveObserver(LayerAnimationObserver* observer) {
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observers_.RemoveObserver(observer);
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observer->DetachedFromSequence(this, true);
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void LayerAnimationSequence::OnThreadedAnimationStarted(
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const cc::AnimationEvent& event) {
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (elements_.empty() || event.group_id != animation_group_id_)
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t current_index = last_element_ % elements_.size();
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  LayerAnimationElement::AnimatableProperties element_properties =
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    elements_[current_index]->properties();
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LayerAnimationElement::AnimatableProperty event_property =
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      LayerAnimationElement::ToAnimatableProperty(event.target_property);
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(element_properties & event_property);
222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  elements_[current_index]->set_effective_start_time(event.monotonic_time);
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LayerAnimationSequence::OnScheduled() {
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NotifyScheduled();
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LayerAnimationSequence::OnAnimatorDestroyed() {
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (observers_.might_have_observers()) {
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ObserverListBase<LayerAnimationObserver>::Iterator it(observers_);
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LayerAnimationObserver* obs;
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    while ((obs = it.GetNext()) != NULL) {
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!obs->RequiresNotificationWhenAnimatorDestroyed()) {
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // Remove the observer, but do not allow notifications to be sent.
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        observers_.RemoveObserver(obs);
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        obs->DetachedFromSequence(this, false);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
243d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)size_t LayerAnimationSequence::size() const {
244d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return elements_.size();
245d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
246d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
247d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)LayerAnimationElement* LayerAnimationSequence::FirstElement() const {
248d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  if (elements_.empty()) {
249d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return NULL;
250d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
251d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
252d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return elements_[0].get();
253d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
254d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LayerAnimationSequence::NotifyScheduled() {
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(LayerAnimationObserver,
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    observers_,
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    OnLayerAnimationScheduled(this));
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LayerAnimationSequence::NotifyEnded() {
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(LayerAnimationObserver,
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    observers_,
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    OnLayerAnimationEnded(this));
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LayerAnimationSequence::NotifyAborted() {
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(LayerAnimationObserver,
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    observers_,
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    OnLayerAnimationAborted(this));
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
273d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)LayerAnimationElement* LayerAnimationSequence::CurrentElement() const {
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (elements_.empty())
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t current_index = last_element_ % elements_.size();
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return elements_[current_index].get();
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ui
282