| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/gfx/compositor/layer_animation_sequence.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <iterator> | |
| 9 | |
| 10 #include "base/debug/trace_event.h" | |
| 11 #include "ui/gfx/compositor/layer_animation_delegate.h" | |
| 12 #include "ui/gfx/compositor/layer_animation_element.h" | |
| 13 #include "ui/gfx/compositor/layer_animation_observer.h" | |
| 14 | |
| 15 namespace ui { | |
| 16 | |
| 17 LayerAnimationSequence::LayerAnimationSequence() | |
| 18 : is_cyclic_(false), | |
| 19 last_element_(0) { | |
| 20 } | |
| 21 | |
| 22 LayerAnimationSequence::LayerAnimationSequence(LayerAnimationElement* element) | |
| 23 : is_cyclic_(false), | |
| 24 last_element_(0) { | |
| 25 AddElement(element); | |
| 26 } | |
| 27 | |
| 28 LayerAnimationSequence::~LayerAnimationSequence() { | |
| 29 FOR_EACH_OBSERVER(LayerAnimationObserver, | |
| 30 observers_, | |
| 31 DetachedFromSequence(this, true)); | |
| 32 } | |
| 33 | |
| 34 bool LayerAnimationSequence::Progress(base::TimeDelta elapsed, | |
| 35 LayerAnimationDelegate* delegate) { | |
| 36 bool redraw_required = false; | |
| 37 | |
| 38 if (elements_.empty()) | |
| 39 return redraw_required; | |
| 40 | |
| 41 if (is_cyclic_ && duration_ > base::TimeDelta()) { | |
| 42 // If delta = elapsed - last_start_ is huge, we can skip ahead by complete | |
| 43 // loops to save time. | |
| 44 base::TimeDelta delta = elapsed - last_start_; | |
| 45 int64 k = delta.ToInternalValue() / duration_.ToInternalValue() - 1; | |
| 46 | |
| 47 last_start_ += base::TimeDelta::FromInternalValue( | |
| 48 k * duration_.ToInternalValue()); | |
| 49 } | |
| 50 | |
| 51 size_t current_index = last_element_ % elements_.size(); | |
| 52 while ((is_cyclic_ || last_element_ < elements_.size()) && | |
| 53 (last_start_ + elements_[current_index]->duration() < elapsed)) { | |
| 54 // Let the element we're passing finish. | |
| 55 if (elements_[current_index]->Progress(1.0, delegate)) | |
| 56 redraw_required = true; | |
| 57 last_start_ += elements_[current_index]->duration(); | |
| 58 ++last_element_; | |
| 59 current_index = last_element_ % elements_.size(); | |
| 60 } | |
| 61 | |
| 62 if (is_cyclic_ || last_element_ < elements_.size()) { | |
| 63 double t = 1.0; | |
| 64 if (elements_[current_index]->duration() > base::TimeDelta()) { | |
| 65 t = (elapsed - last_start_).InMillisecondsF() / | |
| 66 elements_[current_index]->duration().InMillisecondsF(); | |
| 67 } | |
| 68 if (elements_[current_index]->Progress(t, delegate)) | |
| 69 redraw_required = true; | |
| 70 } | |
| 71 | |
| 72 if (!is_cyclic_ && elapsed == duration_) { | |
| 73 last_element_ = 0; | |
| 74 last_start_ = base::TimeDelta::FromMilliseconds(0); | |
| 75 NotifyEnded(); | |
| 76 } | |
| 77 | |
| 78 return redraw_required; | |
| 79 } | |
| 80 | |
| 81 void LayerAnimationSequence::GetTargetValue( | |
| 82 LayerAnimationElement::TargetValue* target) const { | |
| 83 if (is_cyclic_) | |
| 84 return; | |
| 85 | |
| 86 for (size_t i = last_element_; i < elements_.size(); ++i) | |
| 87 elements_[i]->GetTargetValue(target); | |
| 88 } | |
| 89 | |
| 90 void LayerAnimationSequence::Abort() { | |
| 91 size_t current_index = last_element_ % elements_.size(); | |
| 92 while (current_index < elements_.size()) { | |
| 93 elements_[current_index]->Abort(); | |
| 94 ++current_index; | |
| 95 } | |
| 96 last_element_ = 0; | |
| 97 last_start_ = base::TimeDelta::FromMilliseconds(0); | |
| 98 NotifyAborted(); | |
| 99 } | |
| 100 | |
| 101 void LayerAnimationSequence::AddElement(LayerAnimationElement* element) { | |
| 102 // Update duration and properties. | |
| 103 duration_ += element->duration(); | |
| 104 properties_.insert(element->properties().begin(), | |
| 105 element->properties().end()); | |
| 106 elements_.push_back(make_linked_ptr(element)); | |
| 107 } | |
| 108 | |
| 109 bool LayerAnimationSequence::HasCommonProperty( | |
| 110 const LayerAnimationElement::AnimatableProperties& other) const { | |
| 111 LayerAnimationElement::AnimatableProperties intersection; | |
| 112 std::insert_iterator<LayerAnimationElement::AnimatableProperties> ii( | |
| 113 intersection, intersection.begin()); | |
| 114 std::set_intersection(properties_.begin(), properties_.end(), | |
| 115 other.begin(), other.end(), | |
| 116 ii); | |
| 117 return intersection.size() > 0; | |
| 118 } | |
| 119 | |
| 120 void LayerAnimationSequence::AddObserver(LayerAnimationObserver* observer) { | |
| 121 if (!observers_.HasObserver(observer)) { | |
| 122 observers_.AddObserver(observer); | |
| 123 observer->AttachedToSequence(this); | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 void LayerAnimationSequence::RemoveObserver(LayerAnimationObserver* observer) { | |
| 128 observers_.RemoveObserver(observer); | |
| 129 observer->DetachedFromSequence(this, true); | |
| 130 } | |
| 131 | |
| 132 void LayerAnimationSequence::OnScheduled() { | |
| 133 NotifyScheduled(); | |
| 134 } | |
| 135 | |
| 136 void LayerAnimationSequence::OnAnimatorDestroyed() { | |
| 137 if (observers_.might_have_observers()) { | |
| 138 ObserverListBase<LayerAnimationObserver>::Iterator it(observers_); | |
| 139 LayerAnimationObserver* obs; | |
| 140 while ((obs = it.GetNext()) != NULL) { | |
| 141 if (!obs->RequiresNotificationWhenAnimatorDestroyed()) { | |
| 142 // Remove the observer, but do not allow notifications to be sent. | |
| 143 observers_.RemoveObserver(obs); | |
| 144 obs->DetachedFromSequence(this, false); | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 void LayerAnimationSequence::NotifyScheduled() { | |
| 151 FOR_EACH_OBSERVER(LayerAnimationObserver, | |
| 152 observers_, | |
| 153 OnLayerAnimationScheduled(this)); | |
| 154 } | |
| 155 | |
| 156 void LayerAnimationSequence::NotifyEnded() { | |
| 157 FOR_EACH_OBSERVER(LayerAnimationObserver, | |
| 158 observers_, | |
| 159 OnLayerAnimationEnded(this)); | |
| 160 } | |
| 161 | |
| 162 void LayerAnimationSequence::NotifyAborted() { | |
| 163 FOR_EACH_OBSERVER(LayerAnimationObserver, | |
| 164 observers_, | |
| 165 OnLayerAnimationAborted(this)); | |
| 166 } | |
| 167 | |
| 168 } // namespace ui | |
| OLD | NEW |