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) |
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::Start(LayerAnimationDelegate* delegate) { |
| 41 DCHECK(start_time_ != base::TimeTicks()); |
| 42 last_progressed_fraction_ = 0.0; |
| 43 if (elements_.empty()) |
| 44 return; |
| 45 |
| 46 elements_[0]->set_requested_start_time(start_time_); |
| 47 elements_[0]->Start(delegate, animation_group_id_); |
| 48 } |
| 49 |
34 void LayerAnimationSequence::Progress(base::TimeTicks now, | 50 void LayerAnimationSequence::Progress(base::TimeTicks now, |
35 LayerAnimationDelegate* delegate) { | 51 LayerAnimationDelegate* delegate) { |
| 52 DCHECK(start_time_ != base::TimeTicks()); |
36 bool redraw_required = false; | 53 bool redraw_required = false; |
37 | 54 |
38 if (elements_.empty()) | 55 if (elements_.empty()) |
39 return; | 56 return; |
40 | 57 |
41 if (last_element_ == 0) | 58 if (last_element_ == 0) |
42 last_start_ = start_time_; | 59 last_start_ = start_time_; |
43 | 60 |
44 size_t current_index = last_element_ % elements_.size(); | 61 size_t current_index = last_element_ % elements_.size(); |
45 base::TimeDelta element_duration; | 62 base::TimeDelta element_duration; |
46 while (is_cyclic_ || last_element_ < elements_.size()) { | 63 while (is_cyclic_ || last_element_ < elements_.size()) { |
47 elements_[current_index]->set_start_time(last_start_); | 64 elements_[current_index]->set_requested_start_time(last_start_); |
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]->ProgressToEnd(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_ = |
| 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()) { |
| 79 if (!elements_[current_index]->Started()) |
| 80 elements_[current_index]->Start(delegate, animation_group_id_); |
60 if (elements_[current_index]->Progress(now, delegate)) | 81 if (elements_[current_index]->Progress(now, delegate)) |
61 redraw_required = true; | 82 redraw_required = true; |
| 83 last_progressed_fraction_ = |
| 84 elements_[current_index]->last_progressed_fraction(); |
62 } | 85 } |
63 | 86 |
64 // Since the delegate may be deleted due to the notifications below, it is | 87 // Since the delegate may be deleted due to the notifications below, it is |
65 // important that we schedule a draw before sending them. | 88 // important that we schedule a draw before sending them. |
66 if (redraw_required) | 89 if (redraw_required) |
67 delegate->ScheduleDrawForAnimation(); | 90 delegate->ScheduleDrawForAnimation(); |
68 | 91 |
69 if (!is_cyclic_ && last_element_ == elements_.size()) { | 92 if (!is_cyclic_ && last_element_ == elements_.size()) { |
70 last_element_ = 0; | 93 last_element_ = 0; |
| 94 waiting_for_group_start_ = false; |
| 95 animation_group_id_ = 0; |
71 NotifyEnded(); | 96 NotifyEnded(); |
72 } | 97 } |
73 } | 98 } |
74 | 99 |
75 bool LayerAnimationSequence::IsFinished(base::TimeTicks time) { | 100 bool LayerAnimationSequence::IsFinished(base::TimeTicks time) { |
76 if (is_cyclic_) | 101 if (is_cyclic_ || waiting_for_group_start_) |
77 return false; | 102 return false; |
78 | 103 |
79 if (elements_.empty()) | 104 if (elements_.empty()) |
80 return true; | 105 return true; |
81 | 106 |
82 if (last_element_ == 0) | 107 if (last_element_ == 0) |
83 last_start_ = start_time_; | 108 last_start_ = start_time_; |
84 | 109 |
85 base::TimeTicks current_start = last_start_; | 110 base::TimeTicks current_start = last_start_; |
86 size_t current_index = last_element_; | 111 size_t current_index = last_element_; |
87 base::TimeDelta element_duration; | 112 base::TimeDelta element_duration; |
88 while (current_index < elements_.size()) { | 113 while (current_index < elements_.size()) { |
89 elements_[current_index]->set_start_time(current_start); | 114 elements_[current_index]->set_requested_start_time(current_start); |
90 if (!elements_[current_index]->IsFinished(time, &element_duration)) | 115 if (!elements_[current_index]->IsFinished(time, &element_duration)) |
91 break; | 116 break; |
92 | 117 |
93 current_start += element_duration; | 118 current_start += element_duration; |
94 ++current_index; | 119 ++current_index; |
95 } | 120 } |
96 | 121 |
97 return (current_index == elements_.size()); | 122 return (current_index == elements_.size()); |
98 } | 123 } |
99 | 124 |
100 void LayerAnimationSequence::ProgressToEnd(LayerAnimationDelegate* delegate) { | 125 void LayerAnimationSequence::ProgressToEnd(LayerAnimationDelegate* delegate) { |
101 bool redraw_required = false; | 126 bool redraw_required = false; |
102 | 127 |
103 if (elements_.empty()) | 128 if (elements_.empty()) |
104 return; | 129 return; |
105 | 130 |
106 size_t current_index = last_element_ % elements_.size(); | 131 size_t current_index = last_element_ % elements_.size(); |
107 while (current_index < elements_.size()) { | 132 while (current_index < elements_.size()) { |
108 if (elements_[current_index]->ProgressToEnd(delegate)) | 133 if (elements_[current_index]->ProgressToEnd(delegate)) |
109 redraw_required = true; | 134 redraw_required = true; |
| 135 last_progressed_fraction_ = |
| 136 elements_[current_index]->last_progressed_fraction(); |
110 ++current_index; | 137 ++current_index; |
111 ++last_element_; | 138 ++last_element_; |
112 } | 139 } |
113 | 140 |
114 if (redraw_required) | 141 if (redraw_required) |
115 delegate->ScheduleDrawForAnimation(); | 142 delegate->ScheduleDrawForAnimation(); |
116 | 143 |
117 if (!is_cyclic_) { | 144 if (!is_cyclic_) { |
118 last_element_ = 0; | 145 last_element_ = 0; |
| 146 waiting_for_group_start_ = false; |
| 147 animation_group_id_ = 0; |
119 NotifyEnded(); | 148 NotifyEnded(); |
120 } | 149 } |
121 } | 150 } |
122 | 151 |
123 void LayerAnimationSequence::GetTargetValue( | 152 void LayerAnimationSequence::GetTargetValue( |
124 LayerAnimationElement::TargetValue* target) const { | 153 LayerAnimationElement::TargetValue* target) const { |
125 if (is_cyclic_) | 154 if (is_cyclic_) |
126 return; | 155 return; |
127 | 156 |
128 for (size_t i = last_element_; i < elements_.size(); ++i) | 157 for (size_t i = last_element_; i < elements_.size(); ++i) |
129 elements_[i]->GetTargetValue(target); | 158 elements_[i]->GetTargetValue(target); |
130 } | 159 } |
131 | 160 |
132 void LayerAnimationSequence::Abort() { | 161 void LayerAnimationSequence::Abort(LayerAnimationDelegate* delegate) { |
133 size_t current_index = last_element_ % elements_.size(); | 162 size_t current_index = last_element_ % elements_.size(); |
134 while (current_index < elements_.size()) { | 163 while (current_index < elements_.size()) { |
135 elements_[current_index]->Abort(); | 164 elements_[current_index]->Abort(delegate); |
136 ++current_index; | 165 ++current_index; |
137 } | 166 } |
138 last_element_ = 0; | 167 last_element_ = 0; |
| 168 waiting_for_group_start_ = false; |
139 NotifyAborted(); | 169 NotifyAborted(); |
140 } | 170 } |
141 | 171 |
142 void LayerAnimationSequence::AddElement(LayerAnimationElement* element) { | 172 void LayerAnimationSequence::AddElement(LayerAnimationElement* element) { |
143 properties_.insert(element->properties().begin(), | 173 properties_.insert(element->properties().begin(), |
144 element->properties().end()); | 174 element->properties().end()); |
145 elements_.push_back(make_linked_ptr(element)); | 175 elements_.push_back(make_linked_ptr(element)); |
146 } | 176 } |
147 | 177 |
148 bool LayerAnimationSequence::HasCommonProperty( | 178 bool LayerAnimationSequence::HasCommonProperty( |
149 const LayerAnimationElement::AnimatableProperties& other) const { | 179 const LayerAnimationElement::AnimatableProperties& other) const { |
150 LayerAnimationElement::AnimatableProperties intersection; | 180 LayerAnimationElement::AnimatableProperties intersection; |
151 std::insert_iterator<LayerAnimationElement::AnimatableProperties> ii( | 181 std::insert_iterator<LayerAnimationElement::AnimatableProperties> ii( |
152 intersection, intersection.begin()); | 182 intersection, intersection.begin()); |
153 std::set_intersection(properties_.begin(), properties_.end(), | 183 std::set_intersection(properties_.begin(), properties_.end(), |
154 other.begin(), other.end(), | 184 other.begin(), other.end(), |
155 ii); | 185 ii); |
156 return intersection.size() > 0; | 186 return intersection.size() > 0; |
157 } | 187 } |
158 | 188 |
| 189 bool LayerAnimationSequence::IsFirstElementThreaded() const { |
| 190 if (!elements_.empty()) |
| 191 return elements_[0]->IsThreaded(); |
| 192 |
| 193 return false; |
| 194 } |
| 195 |
159 void LayerAnimationSequence::AddObserver(LayerAnimationObserver* observer) { | 196 void LayerAnimationSequence::AddObserver(LayerAnimationObserver* observer) { |
160 if (!observers_.HasObserver(observer)) { | 197 if (!observers_.HasObserver(observer)) { |
161 observers_.AddObserver(observer); | 198 observers_.AddObserver(observer); |
162 observer->AttachedToSequence(this); | 199 observer->AttachedToSequence(this); |
163 } | 200 } |
164 } | 201 } |
165 | 202 |
166 void LayerAnimationSequence::RemoveObserver(LayerAnimationObserver* observer) { | 203 void LayerAnimationSequence::RemoveObserver(LayerAnimationObserver* observer) { |
167 observers_.RemoveObserver(observer); | 204 observers_.RemoveObserver(observer); |
168 observer->DetachedFromSequence(this, true); | 205 observer->DetachedFromSequence(this, true); |
169 } | 206 } |
170 | 207 |
| 208 void LayerAnimationSequence::OnThreadedAnimationStarted( |
| 209 const cc::AnimationEvent& event) { |
| 210 if (elements_.empty() || event.groupId != animation_group_id_) |
| 211 return; |
| 212 |
| 213 size_t current_index = last_element_ % elements_.size(); |
| 214 const LayerAnimationElement::AnimatableProperties& element_properties = |
| 215 elements_[current_index]->properties(); |
| 216 LayerAnimationElement::AnimatableProperty event_property = |
| 217 LayerAnimationElement::ToAnimatableProperty(event.targetProperty); |
| 218 DCHECK(element_properties.find(event_property) != element_properties.end()); |
| 219 elements_[current_index]->set_effective_start_time( |
| 220 base::TimeTicks::FromInternalValue( |
| 221 event.monotonicTime * base::Time::kMicrosecondsPerSecond)); |
| 222 } |
| 223 |
171 void LayerAnimationSequence::OnScheduled() { | 224 void LayerAnimationSequence::OnScheduled() { |
172 NotifyScheduled(); | 225 NotifyScheduled(); |
173 } | 226 } |
174 | 227 |
175 void LayerAnimationSequence::OnAnimatorDestroyed() { | 228 void LayerAnimationSequence::OnAnimatorDestroyed() { |
176 if (observers_.might_have_observers()) { | 229 if (observers_.might_have_observers()) { |
177 ObserverListBase<LayerAnimationObserver>::Iterator it(observers_); | 230 ObserverListBase<LayerAnimationObserver>::Iterator it(observers_); |
178 LayerAnimationObserver* obs; | 231 LayerAnimationObserver* obs; |
179 while ((obs = it.GetNext()) != NULL) { | 232 while ((obs = it.GetNext()) != NULL) { |
180 if (!obs->RequiresNotificationWhenAnimatorDestroyed()) { | 233 if (!obs->RequiresNotificationWhenAnimatorDestroyed()) { |
(...skipping 16 matching lines...) Expand all Loading... |
197 observers_, | 250 observers_, |
198 OnLayerAnimationEnded(this)); | 251 OnLayerAnimationEnded(this)); |
199 } | 252 } |
200 | 253 |
201 void LayerAnimationSequence::NotifyAborted() { | 254 void LayerAnimationSequence::NotifyAborted() { |
202 FOR_EACH_OBSERVER(LayerAnimationObserver, | 255 FOR_EACH_OBSERVER(LayerAnimationObserver, |
203 observers_, | 256 observers_, |
204 OnLayerAnimationAborted(this)); | 257 OnLayerAnimationAborted(this)); |
205 } | 258 } |
206 | 259 |
| 260 LayerAnimationElement* LayerAnimationSequence::CurrentElement() { |
| 261 if (elements_.empty()) |
| 262 return NULL; |
| 263 |
| 264 size_t current_index = last_element_ % elements_.size(); |
| 265 return elements_[current_index].get(); |
| 266 } |
| 267 |
207 } // namespace ui | 268 } // namespace ui |
OLD | NEW |