OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "components/mus/ws/scheduled_animation_group.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "components/mus/ws/server_window.h" | |
10 | |
11 using mus::mojom::AnimationProperty; | |
12 | |
13 namespace mus { | |
14 namespace ws { | |
15 namespace { | |
16 | |
17 using Sequences = std::vector<ScheduledAnimationSequence>; | |
18 | |
19 // Gets the value of |property| from |window| into |value|. | |
20 void GetValueFromWindow(const ServerWindow* window, | |
21 AnimationProperty property, | |
22 ScheduledAnimationValue* value) { | |
23 switch (property) { | |
24 case AnimationProperty::NONE: | |
25 NOTREACHED(); | |
26 break; | |
27 case AnimationProperty::OPACITY: | |
28 value->float_value = window->opacity(); | |
29 break; | |
30 case AnimationProperty::TRANSFORM: | |
31 value->transform = window->transform(); | |
32 break; | |
33 } | |
34 } | |
35 | |
36 // Sets the value of |property| from |value| into |window|. | |
37 void SetWindowPropertyFromValue(ServerWindow* window, | |
38 AnimationProperty property, | |
39 const ScheduledAnimationValue& value) { | |
40 switch (property) { | |
41 case AnimationProperty::NONE: | |
42 break; | |
43 case AnimationProperty::OPACITY: | |
44 window->SetOpacity(value.float_value); | |
45 break; | |
46 case AnimationProperty::TRANSFORM: | |
47 window->SetTransform(value.transform); | |
48 break; | |
49 } | |
50 } | |
51 | |
52 // Sets the value of |property| into |window| between two points. | |
53 void SetWindowPropertyFromValueBetween(ServerWindow* window, | |
54 AnimationProperty property, | |
55 double value, | |
56 gfx::Tween::Type tween_type, | |
57 const ScheduledAnimationValue& start, | |
58 const ScheduledAnimationValue& target) { | |
59 const double tween_value = gfx::Tween::CalculateValue(tween_type, value); | |
60 switch (property) { | |
61 case AnimationProperty::NONE: | |
62 break; | |
63 case AnimationProperty::OPACITY: | |
64 window->SetOpacity(gfx::Tween::FloatValueBetween( | |
65 tween_value, start.float_value, target.float_value)); | |
66 break; | |
67 case AnimationProperty::TRANSFORM: | |
68 window->SetTransform(gfx::Tween::TransformValueBetween( | |
69 tween_value, start.transform, target.transform)); | |
70 break; | |
71 } | |
72 } | |
73 | |
74 // TODO(mfomitchev): Use struct traits for this? | |
75 gfx::Tween::Type AnimationTypeToTweenType(mojom::AnimationTweenType type) { | |
76 switch (type) { | |
77 case mojom::AnimationTweenType::LINEAR: | |
78 return gfx::Tween::LINEAR; | |
79 case mojom::AnimationTweenType::EASE_IN: | |
80 return gfx::Tween::EASE_IN; | |
81 case mojom::AnimationTweenType::EASE_OUT: | |
82 return gfx::Tween::EASE_OUT; | |
83 case mojom::AnimationTweenType::EASE_IN_OUT: | |
84 return gfx::Tween::EASE_IN_OUT; | |
85 } | |
86 return gfx::Tween::LINEAR; | |
87 } | |
88 | |
89 void ConvertToScheduledValue(const mojom::AnimationValue& transport_value, | |
90 ScheduledAnimationValue* value) { | |
91 value->float_value = transport_value.float_value; | |
92 value->transform = transport_value.transform; | |
93 } | |
94 | |
95 void ConvertToScheduledElement(const mojom::AnimationElement& transport_element, | |
96 ScheduledAnimationElement* element) { | |
97 element->property = transport_element.property; | |
98 element->duration = | |
99 base::TimeDelta::FromMicroseconds(transport_element.duration); | |
100 element->tween_type = AnimationTypeToTweenType(transport_element.tween_type); | |
101 if (transport_element.property != AnimationProperty::NONE) { | |
102 if (transport_element.start_value.get()) { | |
103 element->is_start_valid = true; | |
104 ConvertToScheduledValue(*transport_element.start_value, | |
105 &(element->start_value)); | |
106 } else { | |
107 element->is_start_valid = false; | |
108 } | |
109 ConvertToScheduledValue(*transport_element.target_value, | |
110 &(element->target_value)); | |
111 } | |
112 } | |
113 | |
114 bool IsAnimationValueValid(AnimationProperty property, | |
115 const mojom::AnimationValue& value) { | |
116 bool result; | |
117 switch (property) { | |
118 case AnimationProperty::NONE: | |
119 NOTREACHED(); | |
120 return false; | |
121 case AnimationProperty::OPACITY: | |
122 result = value.float_value >= 0.f && value.float_value <= 1.f; | |
123 DVLOG_IF(1, !result) << "Invalid AnimationValue for opacity: " | |
124 << value.float_value; | |
125 return result; | |
126 case AnimationProperty::TRANSFORM: | |
127 return true; | |
128 } | |
129 return false; | |
130 } | |
131 | |
132 bool IsAnimationElementValid(const mojom::AnimationElement& element) { | |
133 if (element.property == AnimationProperty::NONE) | |
134 return true; // None is a pause and doesn't need any values. | |
135 if (element.start_value.get() && | |
136 !IsAnimationValueValid(element.property, *element.start_value)) | |
137 return false; | |
138 // For all other properties we require a target. | |
139 return element.target_value.get() && | |
140 IsAnimationValueValid(element.property, *element.target_value); | |
141 } | |
142 | |
143 bool IsAnimationSequenceValid(const mojom::AnimationSequence& sequence) { | |
144 if (sequence.elements.size() == 0) { | |
145 DVLOG(1) << "Empty or null AnimationSequence - invalid."; | |
146 return false; | |
147 } | |
148 | |
149 for (size_t i = 0; i < sequence.elements.size(); ++i) { | |
150 if (!IsAnimationElementValid(*sequence.elements[i])) | |
151 return false; | |
152 } | |
153 return true; | |
154 } | |
155 | |
156 bool IsAnimationGroupValid(const mojom::AnimationGroup& transport_group) { | |
157 if (transport_group.sequences.size() == 0) { | |
158 DVLOG(1) << "Empty or null AnimationGroup - invalid; window_id=" | |
159 << transport_group.window_id; | |
160 return false; | |
161 } | |
162 for (size_t i = 0; i < transport_group.sequences.size(); ++i) { | |
163 if (!IsAnimationSequenceValid(*transport_group.sequences[i])) | |
164 return false; | |
165 } | |
166 return true; | |
167 } | |
168 | |
169 // If the start value for |element| isn't valid, the value for the property | |
170 // is obtained from |window| and placed into |element|. | |
171 void GetStartValueFromWindowIfNecessary(const ServerWindow* window, | |
172 ScheduledAnimationElement* element) { | |
173 if (element->property != AnimationProperty::NONE && | |
174 !element->is_start_valid) { | |
175 GetValueFromWindow(window, element->property, &(element->start_value)); | |
176 } | |
177 } | |
178 | |
179 void GetScheduledAnimationProperties(const Sequences& sequences, | |
180 std::set<AnimationProperty>* properties) { | |
181 for (const ScheduledAnimationSequence& sequence : sequences) { | |
182 for (const ScheduledAnimationElement& element : sequence.elements) | |
183 properties->insert(element.property); | |
184 } | |
185 } | |
186 | |
187 // Set |window|'s specified |property| to the value resulting from running all | |
188 // the |sequences|. | |
189 void SetPropertyToTargetProperty(ServerWindow* window, | |
190 mojom::AnimationProperty property, | |
191 const Sequences& sequences) { | |
192 // NOTE: this doesn't deal with |cycle_count| quite right, but I'm honestly | |
193 // not sure we really want to support the same property in multiple sequences | |
194 // animating at once so I'm not dealing. | |
195 base::TimeDelta max_end_duration; | |
196 std::unique_ptr<ScheduledAnimationValue> value; | |
197 for (const ScheduledAnimationSequence& sequence : sequences) { | |
198 base::TimeDelta duration; | |
199 for (const ScheduledAnimationElement& element : sequence.elements) { | |
200 if (element.property != property) | |
201 continue; | |
202 | |
203 duration += element.duration; | |
204 if (duration > max_end_duration) { | |
205 max_end_duration = duration; | |
206 value.reset(new ScheduledAnimationValue(element.target_value)); | |
207 } | |
208 } | |
209 } | |
210 if (value.get()) | |
211 SetWindowPropertyFromValue(window, property, *value); | |
212 } | |
213 | |
214 void ConvertSequenceToScheduled( | |
215 const mojom::AnimationSequence& transport_sequence, | |
216 base::TimeTicks now, | |
217 ScheduledAnimationSequence* sequence) { | |
218 sequence->run_until_stopped = transport_sequence.cycle_count == 0u; | |
219 sequence->cycle_count = transport_sequence.cycle_count; | |
220 DCHECK_NE(0u, transport_sequence.elements.size()); | |
221 sequence->elements.resize(transport_sequence.elements.size()); | |
222 | |
223 base::TimeTicks element_start_time = now; | |
224 for (size_t i = 0; i < transport_sequence.elements.size(); ++i) { | |
225 ConvertToScheduledElement(*(transport_sequence.elements[i].get()), | |
226 &(sequence->elements[i])); | |
227 sequence->elements[i].start_time = element_start_time; | |
228 sequence->duration += sequence->elements[i].duration; | |
229 element_start_time += sequence->elements[i].duration; | |
230 } | |
231 } | |
232 | |
233 bool AdvanceSequence(ServerWindow* window, | |
234 ScheduledAnimationSequence* sequence, | |
235 base::TimeTicks now) { | |
236 ScheduledAnimationElement* element = | |
237 &(sequence->elements[sequence->current_index]); | |
238 while (element->start_time + element->duration < now) { | |
239 SetWindowPropertyFromValue(window, element->property, | |
240 element->target_value); | |
241 if (++sequence->current_index == sequence->elements.size()) { | |
242 if (!sequence->run_until_stopped && --sequence->cycle_count == 0) { | |
243 return false; | |
244 } | |
245 | |
246 sequence->current_index = 0; | |
247 } | |
248 sequence->elements[sequence->current_index].start_time = | |
249 element->start_time + element->duration; | |
250 element = &(sequence->elements[sequence->current_index]); | |
251 GetStartValueFromWindowIfNecessary(window, element); | |
252 | |
253 // It's possible for the delta between now and |last_tick_time_| to be very | |
254 // big (could happen if machine sleeps and is woken up much later). Normally | |
255 // the repeat count is smallish, so we don't bother optimizing it. OTOH if | |
256 // a sequence repeats forever we optimize it lest we get stuck in this loop | |
257 // for a very long time. | |
258 if (sequence->run_until_stopped && sequence->current_index == 0) { | |
259 element->start_time = | |
260 now - base::TimeDelta::FromMicroseconds( | |
261 (now - element->start_time).InMicroseconds() % | |
262 sequence->duration.InMicroseconds()); | |
263 } | |
264 } | |
265 return true; | |
266 } | |
267 | |
268 } // namespace | |
269 | |
270 ScheduledAnimationValue::ScheduledAnimationValue() : float_value(0) {} | |
271 ScheduledAnimationValue::~ScheduledAnimationValue() {} | |
272 | |
273 ScheduledAnimationElement::ScheduledAnimationElement() | |
274 : property(AnimationProperty::OPACITY), | |
275 tween_type(gfx::Tween::EASE_IN), | |
276 is_start_valid(false) {} | |
277 ScheduledAnimationElement::ScheduledAnimationElement( | |
278 const ScheduledAnimationElement& other) = default; | |
279 ScheduledAnimationElement::~ScheduledAnimationElement() {} | |
280 | |
281 ScheduledAnimationSequence::ScheduledAnimationSequence() | |
282 : run_until_stopped(false), cycle_count(0), current_index(0u) {} | |
283 ScheduledAnimationSequence::ScheduledAnimationSequence( | |
284 const ScheduledAnimationSequence& other) = default; | |
285 ScheduledAnimationSequence::~ScheduledAnimationSequence() {} | |
286 | |
287 ScheduledAnimationGroup::~ScheduledAnimationGroup() {} | |
288 | |
289 // static | |
290 std::unique_ptr<ScheduledAnimationGroup> ScheduledAnimationGroup::Create( | |
291 ServerWindow* window, | |
292 base::TimeTicks now, | |
293 uint32_t id, | |
294 const mojom::AnimationGroup& transport_group) { | |
295 if (!IsAnimationGroupValid(transport_group)) | |
296 return nullptr; | |
297 | |
298 std::unique_ptr<ScheduledAnimationGroup> group( | |
299 new ScheduledAnimationGroup(window, id, now)); | |
300 group->sequences_.resize(transport_group.sequences.size()); | |
301 for (size_t i = 0; i < transport_group.sequences.size(); ++i) { | |
302 const mojom::AnimationSequence& transport_sequence( | |
303 *(transport_group.sequences[i])); | |
304 DCHECK_NE(0u, transport_sequence.elements.size()); | |
305 ConvertSequenceToScheduled(transport_sequence, now, &group->sequences_[i]); | |
306 } | |
307 return group; | |
308 } | |
309 | |
310 void ScheduledAnimationGroup::ObtainStartValues() { | |
311 for (ScheduledAnimationSequence& sequence : sequences_) | |
312 GetStartValueFromWindowIfNecessary(window_, &(sequence.elements[0])); | |
313 } | |
314 | |
315 void ScheduledAnimationGroup::SetValuesToTargetValuesForPropertiesNotIn( | |
316 const ScheduledAnimationGroup& other) { | |
317 std::set<AnimationProperty> our_properties; | |
318 GetScheduledAnimationProperties(sequences_, &our_properties); | |
319 | |
320 std::set<AnimationProperty> other_properties; | |
321 GetScheduledAnimationProperties(other.sequences_, &other_properties); | |
322 | |
323 for (AnimationProperty property : our_properties) { | |
324 if (other_properties.count(property) == 0 && | |
325 property != AnimationProperty::NONE) { | |
326 SetPropertyToTargetProperty(window_, property, sequences_); | |
327 } | |
328 } | |
329 } | |
330 | |
331 bool ScheduledAnimationGroup::Tick(base::TimeTicks time) { | |
332 for (Sequences::iterator i = sequences_.begin(); i != sequences_.end();) { | |
333 if (!AdvanceSequence(window_, &(*i), time)) { | |
334 i = sequences_.erase(i); | |
335 continue; | |
336 } | |
337 const ScheduledAnimationElement& active_element( | |
338 i->elements[i->current_index]); | |
339 const double percent = | |
340 (time - active_element.start_time).InMillisecondsF() / | |
341 active_element.duration.InMillisecondsF(); | |
342 SetWindowPropertyFromValueBetween( | |
343 window_, active_element.property, percent, active_element.tween_type, | |
344 active_element.start_value, active_element.target_value); | |
345 ++i; | |
346 } | |
347 return sequences_.empty(); | |
348 } | |
349 | |
350 ScheduledAnimationGroup::ScheduledAnimationGroup(ServerWindow* window, | |
351 uint32_t id, | |
352 base::TimeTicks time_scheduled) | |
353 : window_(window), id_(id), time_scheduled_(time_scheduled) {} | |
354 | |
355 } // namespace ws | |
356 } // namespace mus | |
OLD | NEW |