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/keyframed_animation_curve.h"
9 #include "cc/animation/layer_animation_controller.h"
10 #include "cc/animation/transform_operations.h"
11 #include "cc/layers/layer.h"
12 #include "cc/layers/layer_impl.h"
15 using cc::AnimationCurve;
16 using cc::EaseTimingFunction;
17 using cc::FloatKeyframe;
18 using cc::KeyframedFloatAnimationCurve;
19 using cc::KeyframedTransformAnimationCurve;
20 using cc::TimingFunction;
21 using cc::TransformKeyframe;
25 template <class Target>
26 int AddOpacityTransition(Target* target,
30 bool use_timing_function) {
31 scoped_ptr<KeyframedFloatAnimationCurve>
32 curve(KeyframedFloatAnimationCurve::Create());
34 scoped_ptr<TimingFunction> func;
35 if (!use_timing_function)
36 func = EaseTimingFunction::Create();
38 curve->AddKeyframe(FloatKeyframe::Create(0.0, start_opacity, func.Pass()));
39 curve->AddKeyframe(FloatKeyframe::Create(
40 duration, end_opacity, scoped_ptr<TimingFunction>()));
42 int id = AnimationIdProvider::NextAnimationId();
44 scoped_ptr<Animation> animation(Animation::Create(
45 curve.PassAs<AnimationCurve>(),
47 AnimationIdProvider::NextGroupId(),
49 animation->set_needs_synchronized_start_time(true);
51 target->AddAnimation(animation.Pass());
55 template <class Target>
56 int AddAnimatedTransform(Target* target,
58 TransformOperations start_operations,
59 TransformOperations operations) {
60 scoped_ptr<KeyframedTransformAnimationCurve>
61 curve(KeyframedTransformAnimationCurve::Create());
64 curve->AddKeyframe(TransformKeyframe::Create(
65 0.0, start_operations, scoped_ptr<TimingFunction>()));
68 curve->AddKeyframe(TransformKeyframe::Create(
69 duration, operations, scoped_ptr<TimingFunction>()));
71 int id = AnimationIdProvider::NextAnimationId();
73 scoped_ptr<Animation> animation(Animation::Create(
74 curve.PassAs<AnimationCurve>(),
76 AnimationIdProvider::NextGroupId(),
77 Animation::Transform));
78 animation->set_needs_synchronized_start_time(true);
80 target->AddAnimation(animation.Pass());
84 template <class Target>
85 int AddAnimatedTransform(Target* target,
89 TransformOperations start_operations;
91 start_operations.AppendTranslate(delta_x, delta_y, 0.0);
94 TransformOperations operations;
95 operations.AppendTranslate(delta_x, delta_y, 0.0);
96 return AddAnimatedTransform(target, duration, start_operations, operations);
99 template <class Target>
100 int AddAnimatedFilter(Target* target,
102 float start_brightness,
103 float end_brightness) {
104 scoped_ptr<KeyframedFilterAnimationCurve>
105 curve(KeyframedFilterAnimationCurve::Create());
107 if (duration > 0.0) {
108 FilterOperations start_filters;
109 start_filters.Append(
110 FilterOperation::CreateBrightnessFilter(start_brightness));
111 curve->AddKeyframe(FilterKeyframe::Create(
112 0.0, start_filters, scoped_ptr<TimingFunction>()));
115 FilterOperations filters;
116 filters.Append(FilterOperation::CreateBrightnessFilter(end_brightness));
118 FilterKeyframe::Create(duration, filters, scoped_ptr<TimingFunction>()));
120 int id = AnimationIdProvider::NextAnimationId();
122 scoped_ptr<Animation> animation(Animation::Create(
123 curve.PassAs<AnimationCurve>(),
125 AnimationIdProvider::NextGroupId(),
127 animation->set_needs_synchronized_start_time(true);
129 target->AddAnimation(animation.Pass());
133 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
136 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration)
137 : duration_(duration) {}
139 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {}
141 double FakeFloatAnimationCurve::Duration() const {
145 float FakeFloatAnimationCurve::GetValue(double now) const {
149 scoped_ptr<AnimationCurve> FakeFloatAnimationCurve::Clone() const {
150 return make_scoped_ptr(new FakeFloatAnimationCurve).PassAs<AnimationCurve>();
153 FakeTransformTransition::FakeTransformTransition(double duration)
154 : duration_(duration) {}
156 FakeTransformTransition::~FakeTransformTransition() {}
158 double FakeTransformTransition::Duration() const {
162 gfx::Transform FakeTransformTransition::GetValue(double time) const {
163 return gfx::Transform();
166 bool FakeTransformTransition::AnimatedBoundsForBox(const gfx::BoxF& box,
167 gfx::BoxF* bounds) const {
171 bool FakeTransformTransition::AffectsScale() const { return false; }
173 bool FakeTransformTransition::IsTranslation() const { return true; }
175 bool FakeTransformTransition::MaximumScale(float* max_scale) const {
180 scoped_ptr<AnimationCurve> FakeTransformTransition::Clone() const {
181 return make_scoped_ptr(new FakeTransformTransition(*this))
182 .PassAs<AnimationCurve>();
186 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to)
187 : duration_(duration), from_(from), to_(to) {}
189 FakeFloatTransition::~FakeFloatTransition() {}
191 double FakeFloatTransition::Duration() const {
195 float FakeFloatTransition::GetValue(double time) const {
199 return (1.0 - time) * from_ + time * to_;
202 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
204 animation_waiting_for_deletion_(false) {}
206 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {}
208 void FakeLayerAnimationValueObserver::OnFilterAnimated(
209 const FilterOperations& filters) {
213 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity) {
217 void FakeLayerAnimationValueObserver::OnTransformAnimated(
218 const gfx::Transform& transform) {
219 transform_ = transform;
222 void FakeLayerAnimationValueObserver::OnScrollOffsetAnimated(
223 const gfx::Vector2dF& scroll_offset) {
224 scroll_offset_ = scroll_offset;
227 void FakeLayerAnimationValueObserver::OnAnimationWaitingForDeletion() {
228 animation_waiting_for_deletion_ = true;
231 bool FakeLayerAnimationValueObserver::IsActive() const {
235 bool FakeInactiveLayerAnimationValueObserver::IsActive() const {
239 gfx::Vector2dF FakeLayerAnimationValueProvider::ScrollOffsetForAnimation()
241 return scroll_offset_;
244 scoped_ptr<AnimationCurve> FakeFloatTransition::Clone() const {
245 return make_scoped_ptr(new FakeFloatTransition(*this))
246 .PassAs<AnimationCurve>();
249 int AddOpacityTransitionToController(LayerAnimationController* controller,
253 bool use_timing_function) {
254 return AddOpacityTransition(controller,
258 use_timing_function);
261 int AddAnimatedTransformToController(LayerAnimationController* controller,
265 return AddAnimatedTransform(controller,
271 int AddAnimatedFilterToController(LayerAnimationController* controller,
273 float start_brightness,
274 float end_brightness) {
275 return AddAnimatedFilter(
276 controller, duration, start_brightness, end_brightness);
279 int AddOpacityTransitionToLayer(Layer* layer,
283 bool use_timing_function) {
284 return AddOpacityTransition(layer,
288 use_timing_function);
291 int AddOpacityTransitionToLayer(LayerImpl* layer,
295 bool use_timing_function) {
296 return AddOpacityTransition(layer->layer_animation_controller(),
300 use_timing_function);
303 int AddAnimatedTransformToLayer(Layer* layer,
307 return AddAnimatedTransform(layer, duration, delta_x, delta_y);
310 int AddAnimatedTransformToLayer(LayerImpl* layer,
314 return AddAnimatedTransform(layer->layer_animation_controller(),
320 int AddAnimatedTransformToLayer(Layer* layer,
322 TransformOperations start_operations,
323 TransformOperations operations) {
324 return AddAnimatedTransform(layer, duration, start_operations, operations);
327 int AddAnimatedTransformToLayer(LayerImpl* layer,
329 TransformOperations start_operations,
330 TransformOperations operations) {
331 return AddAnimatedTransform(layer->layer_animation_controller(),
337 int AddAnimatedFilterToLayer(Layer* layer,
339 float start_brightness,
340 float end_brightness) {
341 return AddAnimatedFilter(layer, duration, start_brightness, end_brightness);
344 int AddAnimatedFilterToLayer(LayerImpl* layer,
346 float start_brightness,
347 float end_brightness) {
348 return AddAnimatedFilter(layer->layer_animation_controller(),