add a use_alsa gyp setting
[chromium-blink-merge.git] / cc / test / animation_test_common.cc
blob3c80b179ac11deefe74bac27b34488525444b32d
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/keyframed_animation_curve.h"
8 #include "cc/layer.h"
9 #include "cc/layer_animation_controller.h"
10 #include "cc/layer_impl.h"
11 #include "cc/transform_operations.h"
13 using cc::Animation;
14 using cc::AnimationCurve;
15 using cc::EaseTimingFunction;
16 using cc::FloatKeyframe;
17 using cc::KeyframedFloatAnimationCurve;
18 using cc::KeyframedTransformAnimationCurve;
19 using cc::TimingFunction;
20 using cc::TransformKeyframe;
22 namespace cc {
24 static int nextAnimationId = 0;
26 template <class Target>
27 int addOpacityTransition(Target& target, double duration, float startOpacity, float endOpacity, bool useTimingFunction)
29 scoped_ptr<KeyframedFloatAnimationCurve> curve(KeyframedFloatAnimationCurve::create());
31 scoped_ptr<TimingFunction> func;
32 if (!useTimingFunction)
33 func = EaseTimingFunction::create();
34 if (duration > 0)
35 curve->addKeyframe(FloatKeyframe::create(0, startOpacity, func.Pass()));
36 curve->addKeyframe(FloatKeyframe::create(duration, endOpacity, scoped_ptr<cc::TimingFunction>()));
38 int id = nextAnimationId++;
40 scoped_ptr<Animation> animation(Animation::create(curve.PassAs<AnimationCurve>(), id, 0, Animation::Opacity));
41 animation->setNeedsSynchronizedStartTime(true);
43 target.addAnimation(animation.Pass());
44 return id;
47 template <class Target>
48 int addAnimatedTransform(Target& target, double duration, int deltaX, int deltaY)
50 scoped_ptr<KeyframedTransformAnimationCurve> curve(KeyframedTransformAnimationCurve::create());
52 if (duration > 0) {
53 TransformOperations startOperations;
54 startOperations.AppendTranslate(deltaX, deltaY, 0);
55 curve->addKeyframe(TransformKeyframe::create(0, startOperations, scoped_ptr<cc::TimingFunction>()));
58 TransformOperations operations;
59 operations.AppendTranslate(deltaX, deltaY, 0);
60 curve->addKeyframe(TransformKeyframe::create(duration, operations, scoped_ptr<cc::TimingFunction>()));
62 int id = nextAnimationId++;
64 scoped_ptr<Animation> animation(Animation::create(curve.PassAs<AnimationCurve>(), id, 0, Animation::Transform));
65 animation->setNeedsSynchronizedStartTime(true);
67 target.addAnimation(animation.Pass());
68 return id;
71 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
72 : m_duration(1)
76 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration)
77 : m_duration(duration)
81 FakeFloatAnimationCurve::~FakeFloatAnimationCurve()
85 double FakeFloatAnimationCurve::duration() const
87 return m_duration;
90 float FakeFloatAnimationCurve::getValue(double now) const
92 return 0;
95 scoped_ptr<cc::AnimationCurve> FakeFloatAnimationCurve::clone() const
97 return make_scoped_ptr(new FakeFloatAnimationCurve).PassAs<cc::AnimationCurve>();
100 FakeTransformTransition::FakeTransformTransition(double duration)
101 : m_duration(duration)
105 FakeTransformTransition::~FakeTransformTransition()
109 double FakeTransformTransition::duration() const
111 return m_duration;
114 gfx::Transform FakeTransformTransition::getValue(double time) const
116 return gfx::Transform();
119 scoped_ptr<cc::AnimationCurve> FakeTransformTransition::clone() const
121 return make_scoped_ptr(new FakeTransformTransition(*this)).PassAs<cc::AnimationCurve>();
125 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to)
126 : m_duration(duration)
127 , m_from(from)
128 , m_to(to)
132 FakeFloatTransition::~FakeFloatTransition()
136 double FakeFloatTransition::duration() const
138 return m_duration;
141 float FakeFloatTransition::getValue(double time) const
143 time /= m_duration;
144 if (time >= 1)
145 time = 1;
146 return (1 - time) * m_from + time * m_to;
149 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
150 : m_opacity(0)
154 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver()
158 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity)
160 m_opacity = opacity;
163 void FakeLayerAnimationValueObserver::OnTransformAnimated(const gfx::Transform& transform)
165 m_transform = transform;
168 bool FakeLayerAnimationValueObserver::IsActive() const
170 return true;
173 scoped_ptr<cc::AnimationCurve> FakeFloatTransition::clone() const
175 return make_scoped_ptr(new FakeFloatTransition(*this)).PassAs<cc::AnimationCurve>();
178 int addOpacityTransitionToController(cc::LayerAnimationController& controller, double duration, float startOpacity, float endOpacity, bool useTimingFunction)
180 return addOpacityTransition(controller, duration, startOpacity, endOpacity, useTimingFunction);
183 int addAnimatedTransformToController(cc::LayerAnimationController& controller, double duration, int deltaX, int deltaY)
185 return addAnimatedTransform(controller, duration, deltaX, deltaY);
188 int addOpacityTransitionToLayer(cc::Layer& layer, double duration, float startOpacity, float endOpacity, bool useTimingFunction)
190 return addOpacityTransition(layer, duration, startOpacity, endOpacity, useTimingFunction);
193 int addOpacityTransitionToLayer(cc::LayerImpl& layer, double duration, float startOpacity, float endOpacity, bool useTimingFunction)
195 return addOpacityTransition(*layer.layerAnimationController(), duration, startOpacity, endOpacity, useTimingFunction);
198 int addAnimatedTransformToLayer(cc::Layer& layer, double duration, int deltaX, int deltaY)
200 return addAnimatedTransform(layer, duration, deltaX, deltaY);
203 int addAnimatedTransformToLayer(cc::LayerImpl& layer, double duration, int deltaX, int deltaY)
205 return addAnimatedTransform(*layer.layerAnimationController(), duration, deltaX, deltaY);
208 } // namespace cc