OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "cc/test/animation_test_common.h" | 5 #include "cc/test/animation_test_common.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "cc/animation/animation_host.h" | 8 #include "cc/animation/animation_host.h" |
9 #include "cc/animation/animation_id_provider.h" | 9 #include "cc/animation/animation_id_provider.h" |
10 #include "cc/animation/animation_player.h" | 10 #include "cc/animation/animation_player.h" |
11 #include "cc/animation/element_animations.h" | 11 #include "cc/animation/element_animations.h" |
12 #include "cc/animation/keyframed_animation_curve.h" | 12 #include "cc/animation/keyframed_animation_curve.h" |
13 #include "cc/animation/scroll_offset_animation_curve.h" | 13 #include "cc/animation/scroll_offset_animation_curve.h" |
14 #include "cc/animation/timing_function.h" | 14 #include "cc/animation/timing_function.h" |
15 #include "cc/animation/transform_operations.h" | 15 #include "cc/animation/transform_operations.h" |
16 #include "cc/base/time_util.h" | 16 #include "cc/base/time_util.h" |
17 #include "cc/layers/layer.h" | 17 #include "cc/layers/layer.h" |
18 #include "cc/layers/layer_impl.h" | 18 #include "cc/layers/layer_impl.h" |
19 | 19 |
20 using cc::Animation; | 20 using cc::Animation; |
21 using cc::AnimationCurve; | 21 using cc::AnimationCurve; |
22 using cc::FloatKeyframe; | 22 using cc::FloatKeyframe; |
23 using cc::KeyframedFloatAnimationCurve; | 23 using cc::KeyframedFloatAnimationCurve; |
24 using cc::KeyframedTransformAnimationCurve; | 24 using cc::KeyframedTransformAnimationCurve; |
25 using cc::TimingFunction; | 25 using cc::TimingFunction; |
26 using cc::TransformKeyframe; | 26 using cc::TransformKeyframe; |
27 | 27 |
28 namespace cc { | 28 namespace cc { |
29 | 29 |
30 template <class Target> | 30 int AddOpacityTransition(AnimationPlayer* target, |
31 int AddOpacityTransition(Target* target, | |
32 double duration, | 31 double duration, |
33 float start_opacity, | 32 float start_opacity, |
34 float end_opacity, | 33 float end_opacity, |
35 bool use_timing_function) { | 34 bool use_timing_function) { |
36 std::unique_ptr<KeyframedFloatAnimationCurve> curve( | 35 std::unique_ptr<KeyframedFloatAnimationCurve> curve( |
37 KeyframedFloatAnimationCurve::Create()); | 36 KeyframedFloatAnimationCurve::Create()); |
38 | 37 |
39 std::unique_ptr<TimingFunction> func; | 38 std::unique_ptr<TimingFunction> func; |
40 if (!use_timing_function) | 39 if (!use_timing_function) |
41 func = CubicBezierTimingFunction::CreatePreset( | 40 func = CubicBezierTimingFunction::CreatePreset( |
42 CubicBezierTimingFunction::EaseType::EASE); | 41 CubicBezierTimingFunction::EaseType::EASE); |
43 if (duration > 0.0) | 42 if (duration > 0.0) |
44 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), start_opacity, | 43 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), start_opacity, |
45 std::move(func))); | 44 std::move(func))); |
46 curve->AddKeyframe(FloatKeyframe::Create( | 45 curve->AddKeyframe(FloatKeyframe::Create( |
47 base::TimeDelta::FromSecondsD(duration), end_opacity, nullptr)); | 46 base::TimeDelta::FromSecondsD(duration), end_opacity, nullptr)); |
48 | 47 |
49 int id = AnimationIdProvider::NextAnimationId(); | 48 int id = AnimationIdProvider::NextAnimationId(); |
50 | 49 |
51 std::unique_ptr<Animation> animation(Animation::Create( | 50 std::unique_ptr<Animation> animation(Animation::Create( |
52 std::move(curve), id, AnimationIdProvider::NextGroupId(), | 51 std::move(curve), id, AnimationIdProvider::NextGroupId(), |
53 TargetProperty::OPACITY)); | 52 TargetProperty::OPACITY)); |
54 animation->set_needs_synchronized_start_time(true); | 53 animation->set_needs_synchronized_start_time(true); |
55 | 54 |
56 target->AddAnimation(std::move(animation)); | 55 target->AddAnimation(std::move(animation)); |
57 return id; | 56 return id; |
58 } | 57 } |
59 | 58 |
60 template <class Target> | 59 int AddAnimatedTransform(AnimationPlayer* target, |
61 int AddAnimatedTransform(Target* target, | |
62 double duration, | 60 double duration, |
63 TransformOperations start_operations, | 61 TransformOperations start_operations, |
64 TransformOperations operations) { | 62 TransformOperations operations) { |
65 std::unique_ptr<KeyframedTransformAnimationCurve> curve( | 63 std::unique_ptr<KeyframedTransformAnimationCurve> curve( |
66 KeyframedTransformAnimationCurve::Create()); | 64 KeyframedTransformAnimationCurve::Create()); |
67 | 65 |
68 if (duration > 0.0) { | 66 if (duration > 0.0) { |
69 curve->AddKeyframe(TransformKeyframe::Create(base::TimeDelta(), | 67 curve->AddKeyframe(TransformKeyframe::Create(base::TimeDelta(), |
70 start_operations, nullptr)); | 68 start_operations, nullptr)); |
71 } | 69 } |
72 | 70 |
73 curve->AddKeyframe(TransformKeyframe::Create( | 71 curve->AddKeyframe(TransformKeyframe::Create( |
74 base::TimeDelta::FromSecondsD(duration), operations, nullptr)); | 72 base::TimeDelta::FromSecondsD(duration), operations, nullptr)); |
75 | 73 |
76 int id = AnimationIdProvider::NextAnimationId(); | 74 int id = AnimationIdProvider::NextAnimationId(); |
77 | 75 |
78 std::unique_ptr<Animation> animation(Animation::Create( | 76 std::unique_ptr<Animation> animation(Animation::Create( |
79 std::move(curve), id, AnimationIdProvider::NextGroupId(), | 77 std::move(curve), id, AnimationIdProvider::NextGroupId(), |
80 TargetProperty::TRANSFORM)); | 78 TargetProperty::TRANSFORM)); |
81 animation->set_needs_synchronized_start_time(true); | 79 animation->set_needs_synchronized_start_time(true); |
82 | 80 |
83 target->AddAnimation(std::move(animation)); | 81 target->AddAnimation(std::move(animation)); |
84 return id; | 82 return id; |
85 } | 83 } |
86 | 84 |
87 template <class Target> | 85 int AddAnimatedTransform(AnimationPlayer* target, |
88 int AddAnimatedTransform(Target* target, | |
89 double duration, | 86 double duration, |
90 int delta_x, | 87 int delta_x, |
91 int delta_y) { | 88 int delta_y) { |
92 TransformOperations start_operations; | 89 TransformOperations start_operations; |
93 if (duration > 0.0) { | 90 if (duration > 0.0) { |
94 start_operations.AppendTranslate(0, 0, 0.0); | 91 start_operations.AppendTranslate(0, 0, 0.0); |
95 } | 92 } |
96 | 93 |
97 TransformOperations operations; | 94 TransformOperations operations; |
98 operations.AppendTranslate(delta_x, delta_y, 0.0); | 95 operations.AppendTranslate(delta_x, delta_y, 0.0); |
99 return AddAnimatedTransform(target, duration, start_operations, operations); | 96 return AddAnimatedTransform(target, duration, start_operations, operations); |
100 } | 97 } |
101 | 98 |
102 template <class Target> | 99 int AddAnimatedFilter(AnimationPlayer* target, |
103 int AddAnimatedFilter(Target* target, | |
104 double duration, | 100 double duration, |
105 float start_brightness, | 101 float start_brightness, |
106 float end_brightness) { | 102 float end_brightness) { |
107 std::unique_ptr<KeyframedFilterAnimationCurve> curve( | 103 std::unique_ptr<KeyframedFilterAnimationCurve> curve( |
108 KeyframedFilterAnimationCurve::Create()); | 104 KeyframedFilterAnimationCurve::Create()); |
109 | 105 |
110 if (duration > 0.0) { | 106 if (duration > 0.0) { |
111 FilterOperations start_filters; | 107 FilterOperations start_filters; |
112 start_filters.Append( | 108 start_filters.Append( |
113 FilterOperation::CreateBrightnessFilter(start_brightness)); | 109 FilterOperation::CreateBrightnessFilter(start_brightness)); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 double progress = TimeUtil::Divide(time, duration_); | 204 double progress = TimeUtil::Divide(time, duration_); |
209 if (progress >= 1.0) | 205 if (progress >= 1.0) |
210 progress = 1.0; | 206 progress = 1.0; |
211 return (1.0 - progress) * from_ + progress * to_; | 207 return (1.0 - progress) * from_ + progress * to_; |
212 } | 208 } |
213 | 209 |
214 std::unique_ptr<AnimationCurve> FakeFloatTransition::Clone() const { | 210 std::unique_ptr<AnimationCurve> FakeFloatTransition::Clone() const { |
215 return base::WrapUnique(new FakeFloatTransition(*this)); | 211 return base::WrapUnique(new FakeFloatTransition(*this)); |
216 } | 212 } |
217 | 213 |
218 int AddScrollOffsetAnimationToElementAnimations(ElementAnimations* target, | 214 int AddScrollOffsetAnimationToPlayer(AnimationPlayer* player, |
219 gfx::ScrollOffset initial_value, | 215 gfx::ScrollOffset initial_value, |
220 gfx::ScrollOffset target_value, | 216 gfx::ScrollOffset target_value, |
221 bool impl_only) { | 217 bool impl_only) { |
222 std::unique_ptr<ScrollOffsetAnimationCurve> curve( | 218 std::unique_ptr<ScrollOffsetAnimationCurve> curve( |
223 ScrollOffsetAnimationCurve::Create( | 219 ScrollOffsetAnimationCurve::Create( |
224 target_value, CubicBezierTimingFunction::CreatePreset( | 220 target_value, CubicBezierTimingFunction::CreatePreset( |
225 CubicBezierTimingFunction::EaseType::EASE_IN_OUT))); | 221 CubicBezierTimingFunction::EaseType::EASE_IN_OUT))); |
226 curve->SetInitialValue(initial_value); | 222 curve->SetInitialValue(initial_value); |
227 | 223 |
228 int id = AnimationIdProvider::NextAnimationId(); | 224 int id = AnimationIdProvider::NextAnimationId(); |
229 | 225 |
230 std::unique_ptr<Animation> animation(Animation::Create( | 226 std::unique_ptr<Animation> animation(Animation::Create( |
231 std::move(curve), id, AnimationIdProvider::NextGroupId(), | 227 std::move(curve), id, AnimationIdProvider::NextGroupId(), |
232 TargetProperty::SCROLL_OFFSET)); | 228 TargetProperty::SCROLL_OFFSET)); |
233 animation->set_is_impl_only(impl_only); | 229 animation->set_is_impl_only(impl_only); |
234 | 230 |
235 target->AddAnimation(std::move(animation)); | 231 player->AddAnimation(std::move(animation)); |
236 | 232 |
237 return id; | 233 return id; |
238 } | 234 } |
239 | 235 |
240 int AddOpacityTransitionToElementAnimations(ElementAnimations* target, | |
241 double duration, | |
242 float start_opacity, | |
243 float end_opacity, | |
244 bool use_timing_function) { | |
245 return AddOpacityTransition(target, duration, start_opacity, end_opacity, | |
246 use_timing_function); | |
247 } | |
248 | |
249 int AddAnimatedTransformToElementAnimations(ElementAnimations* target, | |
250 double duration, | |
251 int delta_x, | |
252 int delta_y) { | |
253 return AddAnimatedTransform(target, duration, delta_x, delta_y); | |
254 } | |
255 | |
256 int AddAnimatedFilterToElementAnimations(ElementAnimations* target, | |
257 double duration, | |
258 float start_brightness, | |
259 float end_brightness) { | |
260 return AddAnimatedFilter(target, duration, start_brightness, end_brightness); | |
261 } | |
262 | |
263 int AddAnimatedTransformToPlayer(AnimationPlayer* player, | 236 int AddAnimatedTransformToPlayer(AnimationPlayer* player, |
264 double duration, | 237 double duration, |
265 int delta_x, | 238 int delta_x, |
266 int delta_y) { | 239 int delta_y) { |
267 return AddAnimatedTransform(player, duration, delta_x, delta_y); | 240 return AddAnimatedTransform(player, duration, delta_x, delta_y); |
268 } | 241 } |
269 | 242 |
270 int AddAnimatedTransformToPlayer(AnimationPlayer* player, | 243 int AddAnimatedTransformToPlayer(AnimationPlayer* player, |
271 double duration, | 244 double duration, |
272 TransformOperations start_operations, | 245 TransformOperations start_operations, |
(...skipping 10 matching lines...) Expand all Loading... |
283 use_timing_function); | 256 use_timing_function); |
284 } | 257 } |
285 | 258 |
286 int AddAnimatedFilterToPlayer(AnimationPlayer* player, | 259 int AddAnimatedFilterToPlayer(AnimationPlayer* player, |
287 double duration, | 260 double duration, |
288 float start_brightness, | 261 float start_brightness, |
289 float end_brightness) { | 262 float end_brightness) { |
290 return AddAnimatedFilter(player, duration, start_brightness, end_brightness); | 263 return AddAnimatedFilter(player, duration, start_brightness, end_brightness); |
291 } | 264 } |
292 | 265 |
293 int AddOpacityStepsToElementAnimations(ElementAnimations* target, | 266 int AddOpacityStepsToPlayer(AnimationPlayer* player, |
294 double duration, | 267 double duration, |
295 float start_opacity, | 268 float start_opacity, |
296 float end_opacity, | 269 float end_opacity, |
297 int num_steps) { | 270 int num_steps) { |
298 std::unique_ptr<KeyframedFloatAnimationCurve> curve( | 271 std::unique_ptr<KeyframedFloatAnimationCurve> curve( |
299 KeyframedFloatAnimationCurve::Create()); | 272 KeyframedFloatAnimationCurve::Create()); |
300 | 273 |
301 std::unique_ptr<TimingFunction> func = StepsTimingFunction::Create( | 274 std::unique_ptr<TimingFunction> func = StepsTimingFunction::Create( |
302 num_steps, StepsTimingFunction::StepPosition::MIDDLE); | 275 num_steps, StepsTimingFunction::StepPosition::MIDDLE); |
303 if (duration > 0.0) | 276 if (duration > 0.0) |
304 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), start_opacity, | 277 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), start_opacity, |
305 std::move(func))); | 278 std::move(func))); |
306 curve->AddKeyframe(FloatKeyframe::Create( | 279 curve->AddKeyframe(FloatKeyframe::Create( |
307 base::TimeDelta::FromSecondsD(duration), end_opacity, nullptr)); | 280 base::TimeDelta::FromSecondsD(duration), end_opacity, nullptr)); |
308 | 281 |
309 int id = AnimationIdProvider::NextAnimationId(); | 282 int id = AnimationIdProvider::NextAnimationId(); |
310 | 283 |
311 std::unique_ptr<Animation> animation(Animation::Create( | 284 std::unique_ptr<Animation> animation(Animation::Create( |
312 std::move(curve), id, AnimationIdProvider::NextGroupId(), | 285 std::move(curve), id, AnimationIdProvider::NextGroupId(), |
313 TargetProperty::OPACITY)); | 286 TargetProperty::OPACITY)); |
314 animation->set_needs_synchronized_start_time(true); | 287 animation->set_needs_synchronized_start_time(true); |
315 | 288 |
316 target->AddAnimation(std::move(animation)); | 289 player->AddAnimation(std::move(animation)); |
317 return id; | 290 return id; |
318 } | 291 } |
319 | 292 |
320 void AddAnimationToElementWithPlayer(ElementId element_id, | 293 void AddAnimationToElementWithPlayer(ElementId element_id, |
321 scoped_refptr<AnimationTimeline> timeline, | 294 scoped_refptr<AnimationTimeline> timeline, |
322 std::unique_ptr<Animation> animation) { | 295 std::unique_ptr<Animation> animation) { |
323 scoped_refptr<AnimationPlayer> player = | 296 scoped_refptr<AnimationPlayer> player = |
324 AnimationPlayer::Create(AnimationIdProvider::NextPlayerId()); | 297 AnimationPlayer::Create(AnimationIdProvider::NextPlayerId()); |
325 timeline->AttachPlayer(player); | 298 timeline->AttachPlayer(player); |
326 player->AttachElement(element_id); | 299 player->AttachElement(element_id); |
327 DCHECK(player->element_animations()); | 300 DCHECK(player->element_animations()); |
328 player->AddAnimation(std::move(animation)); | 301 player->AddAnimation(std::move(animation)); |
329 } | 302 } |
330 | 303 |
331 void AddAnimationToElementWithExistingPlayer( | 304 void AddAnimationToElementWithExistingPlayer( |
332 ElementId element_id, | 305 ElementId element_id, |
333 scoped_refptr<AnimationTimeline> timeline, | 306 scoped_refptr<AnimationTimeline> timeline, |
334 std::unique_ptr<Animation> animation) { | 307 std::unique_ptr<Animation> animation) { |
335 scoped_refptr<ElementAnimations> element_animations = | 308 scoped_refptr<ElementAnimations> element_animations = |
336 timeline->animation_host()->GetElementAnimationsForElementId(element_id); | 309 timeline->animation_host()->GetElementAnimationsForElementId(element_id); |
337 DCHECK(element_animations); | 310 DCHECK(element_animations); |
338 element_animations->AddAnimation(std::move(animation)); | 311 DCHECK(element_animations->players_list().might_have_observers()); |
| 312 ElementAnimations::PlayersList::Iterator it( |
| 313 &element_animations->players_list()); |
| 314 AnimationPlayer* player = it.GetNext(); |
| 315 DCHECK(player); |
| 316 player->AddAnimation(std::move(animation)); |
339 } | 317 } |
340 | 318 |
341 void RemoveAnimationFromElementWithExistingPlayer( | 319 void RemoveAnimationFromElementWithExistingPlayer( |
342 ElementId element_id, | 320 ElementId element_id, |
343 scoped_refptr<AnimationTimeline> timeline, | 321 scoped_refptr<AnimationTimeline> timeline, |
344 int animation_id) { | 322 int animation_id) { |
345 scoped_refptr<ElementAnimations> element_animations = | 323 scoped_refptr<ElementAnimations> element_animations = |
346 timeline->animation_host()->GetElementAnimationsForElementId(element_id); | 324 timeline->animation_host()->GetElementAnimationsForElementId(element_id); |
347 DCHECK(element_animations); | 325 DCHECK(element_animations); |
348 element_animations->RemoveAnimation(animation_id); | 326 DCHECK(element_animations->players_list().might_have_observers()); |
| 327 ElementAnimations::PlayersList::Iterator it( |
| 328 &element_animations->players_list()); |
| 329 AnimationPlayer* player = it.GetNext(); |
| 330 DCHECK(player); |
| 331 player->RemoveAnimation(animation_id); |
349 } | 332 } |
350 | 333 |
351 Animation* GetAnimationFromElementWithExistingPlayer( | 334 Animation* GetAnimationFromElementWithExistingPlayer( |
352 ElementId element_id, | 335 ElementId element_id, |
353 scoped_refptr<AnimationTimeline> timeline, | 336 scoped_refptr<AnimationTimeline> timeline, |
354 int animation_id) { | 337 int animation_id) { |
355 scoped_refptr<ElementAnimations> element_animations = | 338 scoped_refptr<ElementAnimations> element_animations = |
356 timeline->animation_host()->GetElementAnimationsForElementId(element_id); | 339 timeline->animation_host()->GetElementAnimationsForElementId(element_id); |
357 DCHECK(element_animations); | 340 DCHECK(element_animations); |
358 return element_animations->GetAnimationById(animation_id); | 341 DCHECK(element_animations->players_list().might_have_observers()); |
| 342 ElementAnimations::PlayersList::Iterator it( |
| 343 &element_animations->players_list()); |
| 344 AnimationPlayer* player = it.GetNext(); |
| 345 DCHECK(player); |
| 346 return player->GetAnimationById(animation_id); |
359 } | 347 } |
360 | 348 |
361 int AddAnimatedFilterToElementWithPlayer( | 349 int AddAnimatedFilterToElementWithPlayer( |
362 ElementId element_id, | 350 ElementId element_id, |
363 scoped_refptr<AnimationTimeline> timeline, | 351 scoped_refptr<AnimationTimeline> timeline, |
364 double duration, | 352 double duration, |
365 float start_brightness, | 353 float start_brightness, |
366 float end_brightness) { | 354 float end_brightness) { |
367 scoped_refptr<AnimationPlayer> player = | 355 scoped_refptr<AnimationPlayer> player = |
368 AnimationPlayer::Create(AnimationIdProvider::NextPlayerId()); | 356 AnimationPlayer::Create(AnimationIdProvider::NextPlayerId()); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 bool use_timing_function) { | 399 bool use_timing_function) { |
412 scoped_refptr<AnimationPlayer> player = | 400 scoped_refptr<AnimationPlayer> player = |
413 AnimationPlayer::Create(AnimationIdProvider::NextPlayerId()); | 401 AnimationPlayer::Create(AnimationIdProvider::NextPlayerId()); |
414 timeline->AttachPlayer(player); | 402 timeline->AttachPlayer(player); |
415 player->AttachElement(element_id); | 403 player->AttachElement(element_id); |
416 DCHECK(player->element_animations()); | 404 DCHECK(player->element_animations()); |
417 return AddOpacityTransitionToPlayer(player.get(), duration, start_opacity, | 405 return AddOpacityTransitionToPlayer(player.get(), duration, start_opacity, |
418 end_opacity, use_timing_function); | 406 end_opacity, use_timing_function); |
419 } | 407 } |
420 | 408 |
421 void AbortAnimationsOnElementWithPlayer( | |
422 ElementId element_id, | |
423 scoped_refptr<AnimationTimeline> timeline, | |
424 TargetProperty::Type target_property) { | |
425 scoped_refptr<ElementAnimations> element_animations = | |
426 timeline->animation_host()->GetElementAnimationsForElementId(element_id); | |
427 DCHECK(element_animations); | |
428 element_animations->AbortAnimations(target_property); | |
429 } | |
430 | |
431 } // namespace cc | 409 } // namespace cc |
OLD | NEW |