Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_animation_sequence.h" | 5 #include "ui/compositor/layer_animation_sequence.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
| 11 #include "ui/compositor/layer_animation_delegate.h" | 11 #include "ui/compositor/layer_animation_delegate.h" |
| 12 #include "ui/compositor/layer_animation_element.h" | 12 #include "ui/compositor/layer_animation_element.h" |
| 13 #include "ui/compositor/layer_animation_observer.h" | 13 #include "ui/compositor/layer_animation_observer.h" |
| 14 | 14 |
| 15 namespace ui { | 15 namespace ui { |
| 16 | 16 |
| 17 LayerAnimationSequence::LayerAnimationSequence() | 17 LayerAnimationSequence::LayerAnimationSequence() |
| 18 : is_cyclic_(false), | 18 : is_cyclic_(false), |
| 19 last_element_(0) { | 19 last_element_(0), |
| 20 waiting_for_group_start_(false), | |
| 21 animation_group_id_(0), | |
| 22 last_progressed_fraction_(0.0) { | |
| 20 } | 23 } |
| 21 | 24 |
| 22 LayerAnimationSequence::LayerAnimationSequence(LayerAnimationElement* element) | 25 LayerAnimationSequence::LayerAnimationSequence(LayerAnimationElement* element) |
|
sky
2013/02/19 17:06:56
Can we get rid of this and only have one construct
ajuma
2013/02/20 16:09:10
Constructing a one-element sequence seems common e
| |
| 23 : is_cyclic_(false), | 26 : is_cyclic_(false), |
| 24 last_element_(0) { | 27 last_element_(0), |
| 28 waiting_for_group_start_(false), | |
| 29 animation_group_id_(0), | |
| 30 last_progressed_fraction_(0.0) { | |
| 25 AddElement(element); | 31 AddElement(element); |
| 26 } | 32 } |
| 27 | 33 |
| 28 LayerAnimationSequence::~LayerAnimationSequence() { | 34 LayerAnimationSequence::~LayerAnimationSequence() { |
| 29 FOR_EACH_OBSERVER(LayerAnimationObserver, | 35 FOR_EACH_OBSERVER(LayerAnimationObserver, |
| 30 observers_, | 36 observers_, |
| 31 DetachedFromSequence(this, true)); | 37 DetachedFromSequence(this, true)); |
| 32 } | 38 } |
| 33 | 39 |
| 40 void LayerAnimationSequence::ProgressToEffectiveStart( | |
| 41 LayerAnimationDelegate* delegate) { | |
| 42 if (elements_.empty()) | |
| 43 return; | |
| 44 | |
| 45 elements_[0]->set_animation_group_id(animation_group_id_); | |
|
sky
2013/02/19 17:06:56
How come this is only done for the first element?
ajuma
2013/02/20 16:09:10
To avoid making an extra pass over the sequence, t
| |
| 46 elements_[0]->ProgressToEffectiveStart(delegate); | |
| 47 } | |
| 48 | |
| 34 void LayerAnimationSequence::Progress(base::TimeTicks now, | 49 void LayerAnimationSequence::Progress(base::TimeTicks now, |
| 35 LayerAnimationDelegate* delegate) { | 50 LayerAnimationDelegate* delegate) { |
| 51 DCHECK(start_time_ != base::TimeTicks()); | |
|
sky
2013/02/19 17:06:56
DCHECK_NE
ajuma
2013/02/20 16:09:10
This won't compile, since values passed to DCHECK_
| |
| 36 bool redraw_required = false; | 52 bool redraw_required = false; |
| 37 | 53 |
| 38 if (elements_.empty()) | 54 if (elements_.empty()) |
| 39 return; | 55 return; |
| 40 | 56 |
| 41 if (last_element_ == 0) | 57 if (last_element_ == 0) |
| 42 last_start_ = start_time_; | 58 last_start_ = start_time_; |
| 43 | 59 |
| 44 size_t current_index = last_element_ % elements_.size(); | 60 size_t current_index = last_element_ % elements_.size(); |
| 45 base::TimeDelta element_duration; | 61 base::TimeDelta element_duration; |
| 46 while (is_cyclic_ || last_element_ < elements_.size()) { | 62 while (is_cyclic_ || last_element_ < elements_.size()) { |
| 47 elements_[current_index]->set_start_time(last_start_); | 63 elements_[current_index]->set_requested_start_time(last_start_); |
| 64 elements_[current_index]->set_animation_group_id(animation_group_id_); | |
| 48 if (!elements_[current_index]->IsFinished(now, &element_duration)) | 65 if (!elements_[current_index]->IsFinished(now, &element_duration)) |
| 49 break; | 66 break; |
| 50 | 67 |
| 51 // Let the element we're passing finish. | 68 // Let the element we're passing finish. |
| 52 if (elements_[current_index]->Progress(now, delegate)) | 69 if (elements_[current_index]->Progress(now, delegate)) |
| 53 redraw_required = true; | 70 redraw_required = true; |
| 54 last_start_ += element_duration; | 71 last_start_ += element_duration; |
| 55 ++last_element_; | 72 ++last_element_; |
| 73 last_progressed_fraction_ = | |
|
sky
2013/02/19 17:06:56
Should this be reset early on to make sure it it s
ajuma
2013/02/20 16:09:10
Done. This is now reset in ::Start.
| |
| 74 elements_[current_index]->last_progressed_fraction(); | |
| 56 current_index = last_element_ % elements_.size(); | 75 current_index = last_element_ % elements_.size(); |
| 57 } | 76 } |
| 58 | 77 |
| 59 if (is_cyclic_ || last_element_ < elements_.size()) { | 78 if (is_cyclic_ || last_element_ < elements_.size()) { |
| 60 if (elements_[current_index]->Progress(now, delegate)) | 79 if (elements_[current_index]->Progress(now, delegate)) |
| 61 redraw_required = true; | 80 redraw_required = true; |
| 81 last_progressed_fraction_ = | |
| 82 elements_[current_index]->last_progressed_fraction(); | |
| 62 } | 83 } |
| 63 | 84 |
| 64 // Since the delegate may be deleted due to the notifications below, it is | 85 // Since the delegate may be deleted due to the notifications below, it is |
| 65 // important that we schedule a draw before sending them. | 86 // important that we schedule a draw before sending them. |
| 66 if (redraw_required) | 87 if (redraw_required) |
| 67 delegate->ScheduleDrawForAnimation(); | 88 delegate->ScheduleDrawForAnimation(); |
| 68 | 89 |
| 69 if (!is_cyclic_ && last_element_ == elements_.size()) { | 90 if (!is_cyclic_ && last_element_ == elements_.size()) { |
| 70 last_element_ = 0; | 91 last_element_ = 0; |
| 92 waiting_for_group_start_ = false; | |
| 93 animation_group_id_ = 0; | |
| 71 NotifyEnded(); | 94 NotifyEnded(); |
| 72 } | 95 } |
| 73 } | 96 } |
| 74 | 97 |
| 75 bool LayerAnimationSequence::IsFinished(base::TimeTicks time) { | 98 bool LayerAnimationSequence::IsFinished(base::TimeTicks time) { |
| 76 if (is_cyclic_) | 99 if (is_cyclic_ || waiting_for_group_start_) |
| 77 return false; | 100 return false; |
| 78 | 101 |
| 79 if (elements_.empty()) | 102 if (elements_.empty()) |
| 80 return true; | 103 return true; |
| 81 | 104 |
| 82 if (last_element_ == 0) | 105 if (last_element_ == 0) |
| 83 last_start_ = start_time_; | 106 last_start_ = start_time_; |
| 84 | 107 |
| 85 base::TimeTicks current_start = last_start_; | 108 base::TimeTicks current_start = last_start_; |
| 86 size_t current_index = last_element_; | 109 size_t current_index = last_element_; |
| 87 base::TimeDelta element_duration; | 110 base::TimeDelta element_duration; |
| 88 while (current_index < elements_.size()) { | 111 while (current_index < elements_.size()) { |
| 89 elements_[current_index]->set_start_time(current_start); | 112 elements_[current_index]->set_requested_start_time(current_start); |
| 90 if (!elements_[current_index]->IsFinished(time, &element_duration)) | 113 if (!elements_[current_index]->IsFinished(time, &element_duration)) |
| 91 break; | 114 break; |
| 92 | 115 |
| 93 current_start += element_duration; | 116 current_start += element_duration; |
| 94 ++current_index; | 117 ++current_index; |
| 95 } | 118 } |
| 96 | 119 |
| 97 return (current_index == elements_.size()); | 120 return (current_index == elements_.size()); |
| 98 } | 121 } |
| 99 | 122 |
| 100 void LayerAnimationSequence::ProgressToEnd(LayerAnimationDelegate* delegate) { | 123 void LayerAnimationSequence::ProgressToEnd(LayerAnimationDelegate* delegate) { |
| 101 bool redraw_required = false; | 124 bool redraw_required = false; |
| 102 | 125 |
| 103 if (elements_.empty()) | 126 if (elements_.empty()) |
| 104 return; | 127 return; |
| 105 | 128 |
| 106 size_t current_index = last_element_ % elements_.size(); | 129 size_t current_index = last_element_ % elements_.size(); |
| 107 while (current_index < elements_.size()) { | 130 while (current_index < elements_.size()) { |
| 108 if (elements_[current_index]->ProgressToEnd(delegate)) | 131 if (elements_[current_index]->ProgressToEnd(delegate)) |
| 109 redraw_required = true; | 132 redraw_required = true; |
| 133 last_progressed_fraction_ = | |
| 134 elements_[current_index]->last_progressed_fraction(); | |
| 110 ++current_index; | 135 ++current_index; |
| 111 ++last_element_; | 136 ++last_element_; |
| 112 } | 137 } |
| 113 | 138 |
| 114 if (redraw_required) | 139 if (redraw_required) |
| 115 delegate->ScheduleDrawForAnimation(); | 140 delegate->ScheduleDrawForAnimation(); |
| 116 | 141 |
| 117 if (!is_cyclic_) { | 142 if (!is_cyclic_) { |
| 118 last_element_ = 0; | 143 last_element_ = 0; |
| 144 waiting_for_group_start_ = false; | |
| 145 animation_group_id_ = 0; | |
| 119 NotifyEnded(); | 146 NotifyEnded(); |
| 120 } | 147 } |
| 121 } | 148 } |
| 122 | 149 |
| 123 void LayerAnimationSequence::GetTargetValue( | 150 void LayerAnimationSequence::GetTargetValue( |
| 124 LayerAnimationElement::TargetValue* target) const { | 151 LayerAnimationElement::TargetValue* target) const { |
| 125 if (is_cyclic_) | 152 if (is_cyclic_) |
| 126 return; | 153 return; |
| 127 | 154 |
| 128 for (size_t i = last_element_; i < elements_.size(); ++i) | 155 for (size_t i = last_element_; i < elements_.size(); ++i) |
| 129 elements_[i]->GetTargetValue(target); | 156 elements_[i]->GetTargetValue(target); |
| 130 } | 157 } |
| 131 | 158 |
| 132 void LayerAnimationSequence::Abort() { | 159 void LayerAnimationSequence::Abort(LayerAnimationDelegate* delegate) { |
| 133 size_t current_index = last_element_ % elements_.size(); | 160 size_t current_index = last_element_ % elements_.size(); |
| 134 while (current_index < elements_.size()) { | 161 while (current_index < elements_.size()) { |
| 135 elements_[current_index]->Abort(); | 162 elements_[current_index]->Abort(delegate); |
| 136 ++current_index; | 163 ++current_index; |
| 137 } | 164 } |
| 138 last_element_ = 0; | 165 last_element_ = 0; |
| 166 waiting_for_group_start_ = false; | |
| 139 NotifyAborted(); | 167 NotifyAborted(); |
| 140 } | 168 } |
| 141 | 169 |
| 142 void LayerAnimationSequence::AddElement(LayerAnimationElement* element) { | 170 void LayerAnimationSequence::AddElement(LayerAnimationElement* element) { |
| 143 properties_.insert(element->properties().begin(), | 171 properties_.insert(element->properties().begin(), |
| 144 element->properties().end()); | 172 element->properties().end()); |
| 145 elements_.push_back(make_linked_ptr(element)); | 173 elements_.push_back(make_linked_ptr(element)); |
| 146 } | 174 } |
| 147 | 175 |
| 148 bool LayerAnimationSequence::HasCommonProperty( | 176 bool LayerAnimationSequence::HasCommonProperty( |
| 149 const LayerAnimationElement::AnimatableProperties& other) const { | 177 const LayerAnimationElement::AnimatableProperties& other) const { |
| 150 LayerAnimationElement::AnimatableProperties intersection; | 178 LayerAnimationElement::AnimatableProperties intersection; |
| 151 std::insert_iterator<LayerAnimationElement::AnimatableProperties> ii( | 179 std::insert_iterator<LayerAnimationElement::AnimatableProperties> ii( |
| 152 intersection, intersection.begin()); | 180 intersection, intersection.begin()); |
| 153 std::set_intersection(properties_.begin(), properties_.end(), | 181 std::set_intersection(properties_.begin(), properties_.end(), |
| 154 other.begin(), other.end(), | 182 other.begin(), other.end(), |
| 155 ii); | 183 ii); |
| 156 return intersection.size() > 0; | 184 return intersection.size() > 0; |
| 157 } | 185 } |
| 158 | 186 |
| 187 bool LayerAnimationSequence::IsFirstElementThreaded() const { | |
| 188 if (!elements_.empty()) | |
| 189 return elements_[0]->IsThreaded(); | |
| 190 | |
| 191 return false; | |
| 192 } | |
| 193 | |
| 159 void LayerAnimationSequence::AddObserver(LayerAnimationObserver* observer) { | 194 void LayerAnimationSequence::AddObserver(LayerAnimationObserver* observer) { |
| 160 if (!observers_.HasObserver(observer)) { | 195 if (!observers_.HasObserver(observer)) { |
| 161 observers_.AddObserver(observer); | 196 observers_.AddObserver(observer); |
| 162 observer->AttachedToSequence(this); | 197 observer->AttachedToSequence(this); |
| 163 } | 198 } |
| 164 } | 199 } |
| 165 | 200 |
| 166 void LayerAnimationSequence::RemoveObserver(LayerAnimationObserver* observer) { | 201 void LayerAnimationSequence::RemoveObserver(LayerAnimationObserver* observer) { |
| 167 observers_.RemoveObserver(observer); | 202 observers_.RemoveObserver(observer); |
| 168 observer->DetachedFromSequence(this, true); | 203 observer->DetachedFromSequence(this, true); |
| 169 } | 204 } |
| 170 | 205 |
| 206 void LayerAnimationSequence::OnThreadedAnimationStarted( | |
| 207 const cc::AnimationEvent& event) { | |
| 208 if (elements_.empty() || event.groupId != animation_group_id_) | |
| 209 return; | |
| 210 | |
| 211 size_t current_index = last_element_ % elements_.size(); | |
| 212 const LayerAnimationElement::AnimatableProperties& element_properties = | |
| 213 elements_[current_index]->properties(); | |
| 214 LayerAnimationElement::AnimatableProperty event_property = | |
| 215 LayerAnimationElement::ToAnimatableProperty(event.targetProperty); | |
| 216 DCHECK(element_properties.find(event_property) != element_properties.end()); | |
| 217 elements_[current_index]->set_effective_start_time( | |
| 218 base::TimeTicks::FromInternalValue( | |
| 219 event.monotonicTime * base::Time::kMicrosecondsPerSecond)); | |
| 220 } | |
| 221 | |
| 171 void LayerAnimationSequence::OnScheduled() { | 222 void LayerAnimationSequence::OnScheduled() { |
| 172 NotifyScheduled(); | 223 NotifyScheduled(); |
| 173 } | 224 } |
| 174 | 225 |
| 175 void LayerAnimationSequence::OnAnimatorDestroyed() { | 226 void LayerAnimationSequence::OnAnimatorDestroyed() { |
| 176 if (observers_.might_have_observers()) { | 227 if (observers_.might_have_observers()) { |
| 177 ObserverListBase<LayerAnimationObserver>::Iterator it(observers_); | 228 ObserverListBase<LayerAnimationObserver>::Iterator it(observers_); |
| 178 LayerAnimationObserver* obs; | 229 LayerAnimationObserver* obs; |
| 179 while ((obs = it.GetNext()) != NULL) { | 230 while ((obs = it.GetNext()) != NULL) { |
| 180 if (!obs->RequiresNotificationWhenAnimatorDestroyed()) { | 231 if (!obs->RequiresNotificationWhenAnimatorDestroyed()) { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 198 OnLayerAnimationEnded(this)); | 249 OnLayerAnimationEnded(this)); |
| 199 } | 250 } |
| 200 | 251 |
| 201 void LayerAnimationSequence::NotifyAborted() { | 252 void LayerAnimationSequence::NotifyAborted() { |
| 202 FOR_EACH_OBSERVER(LayerAnimationObserver, | 253 FOR_EACH_OBSERVER(LayerAnimationObserver, |
| 203 observers_, | 254 observers_, |
| 204 OnLayerAnimationAborted(this)); | 255 OnLayerAnimationAborted(this)); |
| 205 } | 256 } |
| 206 | 257 |
| 207 } // namespace ui | 258 } // namespace ui |
| OLD | NEW |