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
3 // found in the LICENSE file.
5 #include "cc/test/animation_test_common.h"
7 #include "cc/animation/animation_id_provider.h"
8 #include "cc/animation/animation_player.h"
9 #include "cc/animation/keyframed_animation_curve.h"
10 #include "cc/animation/layer_animation_controller.h"
11 #include "cc/animation/transform_operations.h"
12 #include "cc/base/time_util.h"
13 #include "cc/layers/layer.h"
14 #include "cc/layers/layer_impl.h"
17 using cc::AnimationCurve
;
18 using cc::EaseTimingFunction
;
19 using cc::FloatKeyframe
;
20 using cc::KeyframedFloatAnimationCurve
;
21 using cc::KeyframedTransformAnimationCurve
;
22 using cc::TimingFunction
;
23 using cc::TransformKeyframe
;
27 template <class Target
>
28 int AddOpacityTransition(Target
* target
,
32 bool use_timing_function
) {
33 scoped_ptr
<KeyframedFloatAnimationCurve
>
34 curve(KeyframedFloatAnimationCurve::Create());
36 scoped_ptr
<TimingFunction
> func
;
37 if (!use_timing_function
)
38 func
= EaseTimingFunction::Create();
41 FloatKeyframe::Create(base::TimeDelta(), start_opacity
, func
.Pass()));
42 curve
->AddKeyframe(FloatKeyframe::Create(
43 base::TimeDelta::FromSecondsD(duration
), end_opacity
, nullptr));
45 int id
= AnimationIdProvider::NextAnimationId();
47 scoped_ptr
<Animation
> animation(
48 Animation::Create(curve
.Pass(), id
, AnimationIdProvider::NextGroupId(),
50 animation
->set_needs_synchronized_start_time(true);
52 target
->AddAnimation(animation
.Pass());
56 template <class Target
>
57 int AddAnimatedTransform(Target
* target
,
59 TransformOperations start_operations
,
60 TransformOperations operations
) {
61 scoped_ptr
<KeyframedTransformAnimationCurve
>
62 curve(KeyframedTransformAnimationCurve::Create());
65 curve
->AddKeyframe(TransformKeyframe::Create(base::TimeDelta(),
66 start_operations
, nullptr));
69 curve
->AddKeyframe(TransformKeyframe::Create(
70 base::TimeDelta::FromSecondsD(duration
), operations
, nullptr));
72 int id
= AnimationIdProvider::NextAnimationId();
74 scoped_ptr
<Animation
> animation(
75 Animation::Create(curve
.Pass(), id
, AnimationIdProvider::NextGroupId(),
76 Animation::TRANSFORM
));
77 animation
->set_needs_synchronized_start_time(true);
79 target
->AddAnimation(animation
.Pass());
83 template <class Target
>
84 int AddAnimatedTransform(Target
* target
,
88 TransformOperations start_operations
;
90 start_operations
.AppendTranslate(0, 0, 0.0);
93 TransformOperations operations
;
94 operations
.AppendTranslate(delta_x
, delta_y
, 0.0);
95 return AddAnimatedTransform(target
, duration
, start_operations
, operations
);
98 template <class Target
>
99 int AddAnimatedFilter(Target
* target
,
101 float start_brightness
,
102 float end_brightness
) {
103 scoped_ptr
<KeyframedFilterAnimationCurve
>
104 curve(KeyframedFilterAnimationCurve::Create());
106 if (duration
> 0.0) {
107 FilterOperations start_filters
;
108 start_filters
.Append(
109 FilterOperation::CreateBrightnessFilter(start_brightness
));
111 FilterKeyframe::Create(base::TimeDelta(), start_filters
, nullptr));
114 FilterOperations filters
;
115 filters
.Append(FilterOperation::CreateBrightnessFilter(end_brightness
));
116 curve
->AddKeyframe(FilterKeyframe::Create(
117 base::TimeDelta::FromSecondsD(duration
), filters
, nullptr));
119 int id
= AnimationIdProvider::NextAnimationId();
121 scoped_ptr
<Animation
> animation(Animation::Create(
122 curve
.Pass(), id
, AnimationIdProvider::NextGroupId(), Animation::FILTER
));
123 animation
->set_needs_synchronized_start_time(true);
125 target
->AddAnimation(animation
.Pass());
129 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
130 : duration_(base::TimeDelta::FromSecondsD(1.0)) {
133 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration
)
134 : duration_(base::TimeDelta::FromSecondsD(duration
)) {
137 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {}
139 base::TimeDelta
FakeFloatAnimationCurve::Duration() const {
143 float FakeFloatAnimationCurve::GetValue(base::TimeDelta now
) const {
147 scoped_ptr
<AnimationCurve
> FakeFloatAnimationCurve::Clone() const {
148 return make_scoped_ptr(new FakeFloatAnimationCurve
);
151 FakeTransformTransition::FakeTransformTransition(double duration
)
152 : duration_(base::TimeDelta::FromSecondsD(duration
)) {
155 FakeTransformTransition::~FakeTransformTransition() {}
157 base::TimeDelta
FakeTransformTransition::Duration() const {
161 gfx::Transform
FakeTransformTransition::GetValue(base::TimeDelta time
) const {
162 return gfx::Transform();
165 bool FakeTransformTransition::AnimatedBoundsForBox(const gfx::BoxF
& box
,
166 gfx::BoxF
* bounds
) const {
170 bool FakeTransformTransition::AffectsScale() const { return false; }
172 bool FakeTransformTransition::IsTranslation() const { return true; }
174 bool FakeTransformTransition::PreservesAxisAlignment() const {
178 bool FakeTransformTransition::AnimationStartScale(bool forward_direction
,
179 float* start_scale
) const {
184 bool FakeTransformTransition::MaximumTargetScale(bool forward_direction
,
185 float* max_scale
) const {
190 scoped_ptr
<AnimationCurve
> FakeTransformTransition::Clone() const {
191 return make_scoped_ptr(new FakeTransformTransition(*this));
194 FakeFloatTransition::FakeFloatTransition(double duration
, float from
, float to
)
195 : duration_(base::TimeDelta::FromSecondsD(duration
)), from_(from
), to_(to
) {
198 FakeFloatTransition::~FakeFloatTransition() {}
200 base::TimeDelta
FakeFloatTransition::Duration() const {
204 float FakeFloatTransition::GetValue(base::TimeDelta time
) const {
205 double progress
= TimeUtil::Divide(time
, duration_
);
208 return (1.0 - progress
) * from_
+ progress
* to_
;
211 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
213 animation_waiting_for_deletion_(false),
214 transform_is_animating_(false) {}
216 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {}
218 void FakeLayerAnimationValueObserver::OnFilterAnimated(
219 const FilterOperations
& filters
) {
223 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity
) {
227 void FakeLayerAnimationValueObserver::OnTransformAnimated(
228 const gfx::Transform
& transform
) {
229 transform_
= transform
;
232 void FakeLayerAnimationValueObserver::OnScrollOffsetAnimated(
233 const gfx::ScrollOffset
& scroll_offset
) {
234 scroll_offset_
= scroll_offset
;
237 void FakeLayerAnimationValueObserver::OnAnimationWaitingForDeletion() {
238 animation_waiting_for_deletion_
= true;
241 void FakeLayerAnimationValueObserver::OnTransformIsPotentiallyAnimatingChanged(
243 transform_is_animating_
= is_animating
;
246 bool FakeLayerAnimationValueObserver::IsActive() const {
250 bool FakeInactiveLayerAnimationValueObserver::IsActive() const {
254 gfx::ScrollOffset
FakeLayerAnimationValueProvider::ScrollOffsetForAnimation()
256 return scroll_offset_
;
259 scoped_ptr
<AnimationCurve
> FakeFloatTransition::Clone() const {
260 return make_scoped_ptr(new FakeFloatTransition(*this));
263 int AddOpacityTransitionToController(LayerAnimationController
* controller
,
267 bool use_timing_function
) {
268 return AddOpacityTransition(controller
,
272 use_timing_function
);
275 int AddAnimatedTransformToController(LayerAnimationController
* controller
,
279 return AddAnimatedTransform(controller
,
285 int AddAnimatedFilterToController(LayerAnimationController
* controller
,
287 float start_brightness
,
288 float end_brightness
) {
289 return AddAnimatedFilter(
290 controller
, duration
, start_brightness
, end_brightness
);
293 int AddOpacityTransitionToLayer(Layer
* layer
,
297 bool use_timing_function
) {
298 return AddOpacityTransition(layer
,
302 use_timing_function
);
305 int AddOpacityTransitionToLayer(LayerImpl
* layer
,
309 bool use_timing_function
) {
310 return AddOpacityTransition(layer
->layer_animation_controller(),
314 use_timing_function
);
317 int AddAnimatedTransformToLayer(Layer
* layer
,
321 return AddAnimatedTransform(layer
, duration
, delta_x
, delta_y
);
324 int AddAnimatedTransformToLayer(LayerImpl
* layer
,
328 return AddAnimatedTransform(layer
->layer_animation_controller(),
334 int AddAnimatedTransformToLayer(Layer
* layer
,
336 TransformOperations start_operations
,
337 TransformOperations operations
) {
338 return AddAnimatedTransform(layer
, duration
, start_operations
, operations
);
341 int AddAnimatedTransformToLayer(LayerImpl
* layer
,
343 TransformOperations start_operations
,
344 TransformOperations operations
) {
345 return AddAnimatedTransform(layer
->layer_animation_controller(),
351 int AddAnimatedFilterToLayer(Layer
* layer
,
353 float start_brightness
,
354 float end_brightness
) {
355 return AddAnimatedFilter(layer
, duration
, start_brightness
, end_brightness
);
358 int AddAnimatedFilterToLayer(LayerImpl
* layer
,
360 float start_brightness
,
361 float end_brightness
) {
362 return AddAnimatedFilter(layer
->layer_animation_controller(),
368 int AddAnimatedTransformToPlayer(AnimationPlayer
* player
,
372 return AddAnimatedTransform(player
, duration
, delta_x
, delta_y
);
375 int AddOpacityTransitionToPlayer(AnimationPlayer
* player
,
379 bool use_timing_function
) {
380 return AddOpacityTransition(player
, duration
, start_opacity
, end_opacity
,
381 use_timing_function
);
384 int AddAnimatedFilterToPlayer(AnimationPlayer
* player
,
386 float start_brightness
,
387 float end_brightness
) {
388 return AddAnimatedFilter(player
, duration
, start_brightness
, end_brightness
);
391 int AddOpacityStepsToController(LayerAnimationController
* target
,
396 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(
397 KeyframedFloatAnimationCurve::Create());
399 scoped_ptr
<TimingFunction
> func
=
400 StepsTimingFunction::Create(num_steps
, 0.5f
);
403 FloatKeyframe::Create(base::TimeDelta(), start_opacity
, func
.Pass()));
404 curve
->AddKeyframe(FloatKeyframe::Create(
405 base::TimeDelta::FromSecondsD(duration
), end_opacity
, nullptr));
407 int id
= AnimationIdProvider::NextAnimationId();
409 scoped_ptr
<Animation
> animation(
410 Animation::Create(curve
.Pass(), id
, AnimationIdProvider::NextGroupId(),
411 Animation::OPACITY
));
412 animation
->set_needs_synchronized_start_time(true);
414 target
->AddAnimation(animation
.Pass());