Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / cc / test / animation_test_common.cc
blob84f91348a2beff192fdf47d40078fb83b4d6989e
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"
14 using cc::Animation;
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;
23 namespace cc {
25 template <class Target>
26 int AddOpacityTransition(Target* target,
27 double duration,
28 float start_opacity,
29 float end_opacity,
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();
37 if (duration > 0.0)
38 curve->AddKeyframe(FloatKeyframe::Create(0.0, start_opacity, func.Pass()));
39 curve->AddKeyframe(FloatKeyframe::Create(duration,
40 end_opacity,
41 scoped_ptr<cc::TimingFunction>()));
43 int id = AnimationIdProvider::NextAnimationId();
45 scoped_ptr<Animation> animation(Animation::Create(
46 curve.PassAs<AnimationCurve>(),
47 id,
48 AnimationIdProvider::NextGroupId(),
49 Animation::Opacity));
50 animation->set_needs_synchronized_start_time(true);
52 target->AddAnimation(animation.Pass());
53 return id;
56 template <class Target>
57 int AddAnimatedTransform(Target* target,
58 double duration,
59 int delta_x,
60 int delta_y) {
61 scoped_ptr<KeyframedTransformAnimationCurve>
62 curve(KeyframedTransformAnimationCurve::Create());
64 if (duration > 0.0) {
65 TransformOperations start_operations;
66 start_operations.AppendTranslate(delta_x, delta_y, 0.0);
67 curve->AddKeyframe(TransformKeyframe::Create(
68 0.0,
69 start_operations,
70 scoped_ptr<cc::TimingFunction>()));
73 TransformOperations operations;
74 operations.AppendTranslate(delta_x, delta_y, 0.0);
75 curve->AddKeyframe(TransformKeyframe::Create(
76 duration,
77 operations,
78 scoped_ptr<cc::TimingFunction>()));
80 int id = AnimationIdProvider::NextAnimationId();
82 scoped_ptr<Animation> animation(Animation::Create(
83 curve.PassAs<AnimationCurve>(),
84 id,
85 AnimationIdProvider::NextGroupId(),
86 Animation::Transform));
87 animation->set_needs_synchronized_start_time(true);
89 target->AddAnimation(animation.Pass());
90 return id;
93 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
94 : duration_(1.0) {}
96 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration)
97 : duration_(duration) {}
99 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {}
101 double FakeFloatAnimationCurve::Duration() const {
102 return duration_;
105 float FakeFloatAnimationCurve::GetValue(double now) const {
106 return 0.0f;
109 scoped_ptr<cc::AnimationCurve> FakeFloatAnimationCurve::Clone() const {
110 return make_scoped_ptr(
111 new FakeFloatAnimationCurve).PassAs<cc::AnimationCurve>();
114 FakeTransformTransition::FakeTransformTransition(double duration)
115 : duration_(duration) {}
117 FakeTransformTransition::~FakeTransformTransition() {}
119 double FakeTransformTransition::Duration() const {
120 return duration_;
123 gfx::Transform FakeTransformTransition::GetValue(double time) const {
124 return gfx::Transform();
127 scoped_ptr<cc::AnimationCurve> FakeTransformTransition::Clone() const {
128 return make_scoped_ptr(
129 new FakeTransformTransition(*this)).PassAs<cc::AnimationCurve>();
133 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to)
134 : duration_(duration), from_(from), to_(to) {}
136 FakeFloatTransition::~FakeFloatTransition() {}
138 double FakeFloatTransition::Duration() const {
139 return duration_;
142 float FakeFloatTransition::GetValue(double time) const {
143 time /= duration_;
144 if (time >= 1.0)
145 time = 1.0;
146 return (1.0 - time) * from_ + time * to_;
149 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
150 : opacity_(0.0f) {}
152 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {}
154 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity) {
155 opacity_ = opacity;
158 void FakeLayerAnimationValueObserver::OnTransformAnimated(
159 const gfx::Transform& transform) {
160 transform_ = transform;
163 bool FakeLayerAnimationValueObserver::IsActive() const {
164 return true;
167 scoped_ptr<cc::AnimationCurve> FakeFloatTransition::Clone() const {
168 return make_scoped_ptr(
169 new FakeFloatTransition(*this)).PassAs<cc::AnimationCurve>();
172 int AddOpacityTransitionToController(cc::LayerAnimationController* controller,
173 double duration,
174 float start_opacity,
175 float end_opacity,
176 bool use_timing_function) {
177 return AddOpacityTransition(controller,
178 duration,
179 start_opacity,
180 end_opacity,
181 use_timing_function);
184 int AddAnimatedTransformToController(cc::LayerAnimationController* controller,
185 double duration,
186 int delta_x,
187 int delta_y) {
188 return AddAnimatedTransform(controller,
189 duration,
190 delta_x,
191 delta_y);
194 int AddOpacityTransitionToLayer(cc::Layer* layer,
195 double duration,
196 float start_opacity,
197 float end_opacity,
198 bool use_timing_function) {
199 return AddOpacityTransition(layer,
200 duration,
201 start_opacity,
202 end_opacity,
203 use_timing_function);
206 int AddOpacityTransitionToLayer(cc::LayerImpl* layer,
207 double duration,
208 float start_opacity,
209 float end_opacity,
210 bool use_timing_function) {
211 return AddOpacityTransition(layer->layer_animation_controller(),
212 duration,
213 start_opacity,
214 end_opacity,
215 use_timing_function);
218 int AddAnimatedTransformToLayer(cc::Layer* layer,
219 double duration,
220 int delta_x,
221 int delta_y) {
222 return AddAnimatedTransform(layer, duration, delta_x, delta_y);
225 int AddAnimatedTransformToLayer(cc::LayerImpl* layer,
226 double duration,
227 int delta_x,
228 int delta_y) {
229 return AddAnimatedTransform(layer->layer_animation_controller(),
230 duration,
231 delta_x,
232 delta_y);
235 } // namespace cc