Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(590)

Side by Side Diff: components/mus/ws/scheduled_animation_group.cc

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

Powered by Google App Engine
This is Rietveld 408576698