Avoid lossy time stamp conversions when caching metadata.
[chromium-blink-merge.git] / cc / test / animation_test_common.cc
blobf6179cc9d6c99a2e117b4d8aa43248200a709d65
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/base/time_util.h"
12 #include "cc/layers/layer.h"
13 #include "cc/layers/layer_impl.h"
15 using cc::Animation;
16 using cc::AnimationCurve;
17 using cc::EaseTimingFunction;
18 using cc::FloatKeyframe;
19 using cc::KeyframedFloatAnimationCurve;
20 using cc::KeyframedTransformAnimationCurve;
21 using cc::TimingFunction;
22 using cc::TransformKeyframe;
24 namespace cc {
26 template <class Target>
27 int AddOpacityTransition(Target* target,
28 double duration,
29 float start_opacity,
30 float end_opacity,
31 bool use_timing_function) {
32 scoped_ptr<KeyframedFloatAnimationCurve>
33 curve(KeyframedFloatAnimationCurve::Create());
35 scoped_ptr<TimingFunction> func;
36 if (!use_timing_function)
37 func = EaseTimingFunction::Create();
38 if (duration > 0.0)
39 curve->AddKeyframe(
40 FloatKeyframe::Create(base::TimeDelta(), start_opacity, func.Pass()));
41 curve->AddKeyframe(FloatKeyframe::Create(
42 base::TimeDelta::FromSecondsD(duration), end_opacity, nullptr));
44 int id = AnimationIdProvider::NextAnimationId();
46 scoped_ptr<Animation> animation(
47 Animation::Create(curve.Pass(),
48 id,
49 AnimationIdProvider::NextGroupId(),
50 Animation::Opacity));
51 animation->set_needs_synchronized_start_time(true);
53 target->AddAnimation(animation.Pass());
54 return id;
57 template <class Target>
58 int AddAnimatedTransform(Target* target,
59 double duration,
60 TransformOperations start_operations,
61 TransformOperations operations) {
62 scoped_ptr<KeyframedTransformAnimationCurve>
63 curve(KeyframedTransformAnimationCurve::Create());
65 if (duration > 0.0) {
66 curve->AddKeyframe(TransformKeyframe::Create(base::TimeDelta(),
67 start_operations, nullptr));
70 curve->AddKeyframe(TransformKeyframe::Create(
71 base::TimeDelta::FromSecondsD(duration), operations, nullptr));
73 int id = AnimationIdProvider::NextAnimationId();
75 scoped_ptr<Animation> animation(
76 Animation::Create(curve.Pass(),
77 id,
78 AnimationIdProvider::NextGroupId(),
79 Animation::Transform));
80 animation->set_needs_synchronized_start_time(true);
82 target->AddAnimation(animation.Pass());
83 return id;
86 template <class Target>
87 int AddAnimatedTransform(Target* target,
88 double duration,
89 int delta_x,
90 int delta_y) {
91 TransformOperations start_operations;
92 if (duration > 0.0) {
93 start_operations.AppendTranslate(delta_x, delta_y, 0.0);
96 TransformOperations operations;
97 operations.AppendTranslate(delta_x, delta_y, 0.0);
98 return AddAnimatedTransform(target, duration, start_operations, operations);
101 template <class Target>
102 int AddAnimatedFilter(Target* target,
103 double duration,
104 float start_brightness,
105 float end_brightness) {
106 scoped_ptr<KeyframedFilterAnimationCurve>
107 curve(KeyframedFilterAnimationCurve::Create());
109 if (duration > 0.0) {
110 FilterOperations start_filters;
111 start_filters.Append(
112 FilterOperation::CreateBrightnessFilter(start_brightness));
113 curve->AddKeyframe(
114 FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr));
117 FilterOperations filters;
118 filters.Append(FilterOperation::CreateBrightnessFilter(end_brightness));
119 curve->AddKeyframe(FilterKeyframe::Create(
120 base::TimeDelta::FromSecondsD(duration), filters, nullptr));
122 int id = AnimationIdProvider::NextAnimationId();
124 scoped_ptr<Animation> animation(Animation::Create(
125 curve.Pass(), id, AnimationIdProvider::NextGroupId(), Animation::Filter));
126 animation->set_needs_synchronized_start_time(true);
128 target->AddAnimation(animation.Pass());
129 return id;
132 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
133 : duration_(base::TimeDelta::FromSecondsD(1.0)) {
136 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration)
137 : duration_(base::TimeDelta::FromSecondsD(duration)) {
140 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {}
142 base::TimeDelta FakeFloatAnimationCurve::Duration() const {
143 return duration_;
146 float FakeFloatAnimationCurve::GetValue(base::TimeDelta now) const {
147 return 0.0f;
150 scoped_ptr<AnimationCurve> FakeFloatAnimationCurve::Clone() const {
151 return make_scoped_ptr(new FakeFloatAnimationCurve);
154 FakeTransformTransition::FakeTransformTransition(double duration)
155 : duration_(base::TimeDelta::FromSecondsD(duration)) {
158 FakeTransformTransition::~FakeTransformTransition() {}
160 base::TimeDelta FakeTransformTransition::Duration() const {
161 return duration_;
164 gfx::Transform FakeTransformTransition::GetValue(base::TimeDelta time) const {
165 return gfx::Transform();
168 bool FakeTransformTransition::AnimatedBoundsForBox(const gfx::BoxF& box,
169 gfx::BoxF* bounds) const {
170 return false;
173 bool FakeTransformTransition::AffectsScale() const { return false; }
175 bool FakeTransformTransition::IsTranslation() const { return true; }
177 bool FakeTransformTransition::PreservesAxisAlignment() const {
178 return true;
181 bool FakeTransformTransition::MaximumTargetScale(bool forward_direction,
182 float* max_scale) const {
183 *max_scale = 1.f;
184 return true;
187 scoped_ptr<AnimationCurve> FakeTransformTransition::Clone() const {
188 return make_scoped_ptr(new FakeTransformTransition(*this));
191 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to)
192 : duration_(base::TimeDelta::FromSecondsD(duration)), from_(from), to_(to) {
195 FakeFloatTransition::~FakeFloatTransition() {}
197 base::TimeDelta FakeFloatTransition::Duration() const {
198 return duration_;
201 float FakeFloatTransition::GetValue(base::TimeDelta time) const {
202 double progress = TimeUtil::Divide(time, duration_);
203 if (progress >= 1.0)
204 progress = 1.0;
205 return (1.0 - progress) * from_ + progress * to_;
208 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
209 : opacity_(0.0f), animation_waiting_for_deletion_(false) {
212 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {}
214 void FakeLayerAnimationValueObserver::OnFilterAnimated(
215 const FilterOperations& filters) {
216 filters_ = filters;
219 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity) {
220 opacity_ = opacity;
223 void FakeLayerAnimationValueObserver::OnTransformAnimated(
224 const gfx::Transform& transform) {
225 transform_ = transform;
228 void FakeLayerAnimationValueObserver::OnScrollOffsetAnimated(
229 const gfx::ScrollOffset& scroll_offset) {
230 scroll_offset_ = scroll_offset;
233 void FakeLayerAnimationValueObserver::OnAnimationWaitingForDeletion() {
234 animation_waiting_for_deletion_ = true;
237 bool FakeLayerAnimationValueObserver::IsActive() const {
238 return true;
241 bool FakeInactiveLayerAnimationValueObserver::IsActive() const {
242 return false;
245 gfx::ScrollOffset FakeLayerAnimationValueProvider::ScrollOffsetForAnimation()
246 const {
247 return scroll_offset_;
250 scoped_ptr<AnimationCurve> FakeFloatTransition::Clone() const {
251 return make_scoped_ptr(new FakeFloatTransition(*this));
254 int AddOpacityTransitionToController(LayerAnimationController* controller,
255 double duration,
256 float start_opacity,
257 float end_opacity,
258 bool use_timing_function) {
259 return AddOpacityTransition(controller,
260 duration,
261 start_opacity,
262 end_opacity,
263 use_timing_function);
266 int AddAnimatedTransformToController(LayerAnimationController* controller,
267 double duration,
268 int delta_x,
269 int delta_y) {
270 return AddAnimatedTransform(controller,
271 duration,
272 delta_x,
273 delta_y);
276 int AddAnimatedFilterToController(LayerAnimationController* controller,
277 double duration,
278 float start_brightness,
279 float end_brightness) {
280 return AddAnimatedFilter(
281 controller, duration, start_brightness, end_brightness);
284 int AddOpacityTransitionToLayer(Layer* layer,
285 double duration,
286 float start_opacity,
287 float end_opacity,
288 bool use_timing_function) {
289 return AddOpacityTransition(layer,
290 duration,
291 start_opacity,
292 end_opacity,
293 use_timing_function);
296 int AddOpacityTransitionToLayer(LayerImpl* layer,
297 double duration,
298 float start_opacity,
299 float end_opacity,
300 bool use_timing_function) {
301 return AddOpacityTransition(layer->layer_animation_controller(),
302 duration,
303 start_opacity,
304 end_opacity,
305 use_timing_function);
308 int AddAnimatedTransformToLayer(Layer* layer,
309 double duration,
310 int delta_x,
311 int delta_y) {
312 return AddAnimatedTransform(layer, duration, delta_x, delta_y);
315 int AddAnimatedTransformToLayer(LayerImpl* layer,
316 double duration,
317 int delta_x,
318 int delta_y) {
319 return AddAnimatedTransform(layer->layer_animation_controller(),
320 duration,
321 delta_x,
322 delta_y);
325 int AddAnimatedTransformToLayer(Layer* layer,
326 double duration,
327 TransformOperations start_operations,
328 TransformOperations operations) {
329 return AddAnimatedTransform(layer, duration, start_operations, operations);
332 int AddAnimatedTransformToLayer(LayerImpl* layer,
333 double duration,
334 TransformOperations start_operations,
335 TransformOperations operations) {
336 return AddAnimatedTransform(layer->layer_animation_controller(),
337 duration,
338 start_operations,
339 operations);
342 int AddAnimatedFilterToLayer(Layer* layer,
343 double duration,
344 float start_brightness,
345 float end_brightness) {
346 return AddAnimatedFilter(layer, duration, start_brightness, end_brightness);
349 int AddAnimatedFilterToLayer(LayerImpl* layer,
350 double duration,
351 float start_brightness,
352 float end_brightness) {
353 return AddAnimatedFilter(layer->layer_animation_controller(),
354 duration,
355 start_brightness,
356 end_brightness);
359 } // namespace cc