Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / cc / test / animation_test_common.cc
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.
4
5 #include "cc/test/animation_test_common.h"
6
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"
13
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;
22
23 namespace cc {
24
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());
33
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, end_opacity, nullptr));
40
41   int id = AnimationIdProvider::NextAnimationId();
42
43   scoped_ptr<Animation> animation(
44       Animation::Create(curve.Pass(),
45                         id,
46                         AnimationIdProvider::NextGroupId(),
47                         Animation::Opacity));
48   animation->set_needs_synchronized_start_time(true);
49
50   target->AddAnimation(animation.Pass());
51   return id;
52 }
53
54 template <class Target>
55 int AddAnimatedTransform(Target* target,
56                          double duration,
57                          TransformOperations start_operations,
58                          TransformOperations operations) {
59   scoped_ptr<KeyframedTransformAnimationCurve>
60       curve(KeyframedTransformAnimationCurve::Create());
61
62   if (duration > 0.0) {
63     curve->AddKeyframe(
64         TransformKeyframe::Create(0.0, start_operations, nullptr));
65   }
66
67   curve->AddKeyframe(TransformKeyframe::Create(duration, operations, nullptr));
68
69   int id = AnimationIdProvider::NextAnimationId();
70
71   scoped_ptr<Animation> animation(
72       Animation::Create(curve.Pass(),
73                         id,
74                         AnimationIdProvider::NextGroupId(),
75                         Animation::Transform));
76   animation->set_needs_synchronized_start_time(true);
77
78   target->AddAnimation(animation.Pass());
79   return id;
80 }
81
82 template <class Target>
83 int AddAnimatedTransform(Target* target,
84                          double duration,
85                          int delta_x,
86                          int delta_y) {
87   TransformOperations start_operations;
88   if (duration > 0.0) {
89     start_operations.AppendTranslate(delta_x, delta_y, 0.0);
90   }
91
92   TransformOperations operations;
93   operations.AppendTranslate(delta_x, delta_y, 0.0);
94   return AddAnimatedTransform(target, duration, start_operations, operations);
95 }
96
97 template <class Target>
98 int AddAnimatedFilter(Target* target,
99                       double duration,
100                       float start_brightness,
101                       float end_brightness) {
102   scoped_ptr<KeyframedFilterAnimationCurve>
103       curve(KeyframedFilterAnimationCurve::Create());
104
105   if (duration > 0.0) {
106     FilterOperations start_filters;
107     start_filters.Append(
108         FilterOperation::CreateBrightnessFilter(start_brightness));
109     curve->AddKeyframe(FilterKeyframe::Create(0.0, start_filters, nullptr));
110   }
111
112   FilterOperations filters;
113   filters.Append(FilterOperation::CreateBrightnessFilter(end_brightness));
114   curve->AddKeyframe(FilterKeyframe::Create(duration, filters, nullptr));
115
116   int id = AnimationIdProvider::NextAnimationId();
117
118   scoped_ptr<Animation> animation(Animation::Create(
119       curve.Pass(), id, AnimationIdProvider::NextGroupId(), Animation::Filter));
120   animation->set_needs_synchronized_start_time(true);
121
122   target->AddAnimation(animation.Pass());
123   return id;
124 }
125
126 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
127     : duration_(1.0) {}
128
129 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration)
130     : duration_(duration) {}
131
132 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {}
133
134 double FakeFloatAnimationCurve::Duration() const {
135   return duration_;
136 }
137
138 float FakeFloatAnimationCurve::GetValue(double now) const {
139   return 0.0f;
140 }
141
142 scoped_ptr<AnimationCurve> FakeFloatAnimationCurve::Clone() const {
143   return make_scoped_ptr(new FakeFloatAnimationCurve);
144 }
145
146 FakeTransformTransition::FakeTransformTransition(double duration)
147     : duration_(duration) {}
148
149 FakeTransformTransition::~FakeTransformTransition() {}
150
151 double FakeTransformTransition::Duration() const {
152   return duration_;
153 }
154
155 gfx::Transform FakeTransformTransition::GetValue(double time) const {
156   return gfx::Transform();
157 }
158
159 bool FakeTransformTransition::AnimatedBoundsForBox(const gfx::BoxF& box,
160                                                    gfx::BoxF* bounds) const {
161   return false;
162 }
163
164 bool FakeTransformTransition::AffectsScale() const { return false; }
165
166 bool FakeTransformTransition::IsTranslation() const { return true; }
167
168 bool FakeTransformTransition::MaximumTargetScale(bool forward_direction,
169                                                  float* max_scale) const {
170   *max_scale = 1.f;
171   return true;
172 }
173
174 scoped_ptr<AnimationCurve> FakeTransformTransition::Clone() const {
175   return make_scoped_ptr(new FakeTransformTransition(*this));
176 }
177
178
179 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to)
180     : duration_(duration), from_(from), to_(to) {}
181
182 FakeFloatTransition::~FakeFloatTransition() {}
183
184 double FakeFloatTransition::Duration() const {
185   return duration_;
186 }
187
188 float FakeFloatTransition::GetValue(double time) const {
189   time /= duration_;
190   if (time >= 1.0)
191     time = 1.0;
192   return (1.0 - time) * from_ + time * to_;
193 }
194
195 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
196     : opacity_(0.0f),
197       animation_waiting_for_deletion_(false) {}
198
199 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {}
200
201 void FakeLayerAnimationValueObserver::OnFilterAnimated(
202     const FilterOperations& filters) {
203   filters_ = filters;
204 }
205
206 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity) {
207   opacity_ = opacity;
208 }
209
210 void FakeLayerAnimationValueObserver::OnTransformAnimated(
211     const gfx::Transform& transform) {
212   transform_ = transform;
213 }
214
215 void FakeLayerAnimationValueObserver::OnScrollOffsetAnimated(
216     const gfx::ScrollOffset& scroll_offset) {
217   scroll_offset_ = scroll_offset;
218 }
219
220 void FakeLayerAnimationValueObserver::OnAnimationWaitingForDeletion() {
221   animation_waiting_for_deletion_ = true;
222 }
223
224 bool FakeLayerAnimationValueObserver::IsActive() const {
225   return true;
226 }
227
228 bool FakeInactiveLayerAnimationValueObserver::IsActive() const {
229   return false;
230 }
231
232 gfx::ScrollOffset FakeLayerAnimationValueProvider::ScrollOffsetForAnimation()
233     const {
234   return scroll_offset_;
235 }
236
237 scoped_ptr<AnimationCurve> FakeFloatTransition::Clone() const {
238   return make_scoped_ptr(new FakeFloatTransition(*this));
239 }
240
241 int AddOpacityTransitionToController(LayerAnimationController* controller,
242                                      double duration,
243                                      float start_opacity,
244                                      float end_opacity,
245                                      bool use_timing_function) {
246   return AddOpacityTransition(controller,
247                               duration,
248                               start_opacity,
249                               end_opacity,
250                               use_timing_function);
251 }
252
253 int AddAnimatedTransformToController(LayerAnimationController* controller,
254                                      double duration,
255                                      int delta_x,
256                                      int delta_y) {
257   return AddAnimatedTransform(controller,
258                               duration,
259                               delta_x,
260                               delta_y);
261 }
262
263 int AddAnimatedFilterToController(LayerAnimationController* controller,
264                                   double duration,
265                                   float start_brightness,
266                                   float end_brightness) {
267   return AddAnimatedFilter(
268       controller, duration, start_brightness, end_brightness);
269 }
270
271 int AddOpacityTransitionToLayer(Layer* layer,
272                                 double duration,
273                                 float start_opacity,
274                                 float end_opacity,
275                                 bool use_timing_function) {
276   return AddOpacityTransition(layer,
277                               duration,
278                               start_opacity,
279                               end_opacity,
280                               use_timing_function);
281 }
282
283 int AddOpacityTransitionToLayer(LayerImpl* layer,
284                                 double duration,
285                                 float start_opacity,
286                                 float end_opacity,
287                                 bool use_timing_function) {
288   return AddOpacityTransition(layer->layer_animation_controller(),
289                               duration,
290                               start_opacity,
291                               end_opacity,
292                               use_timing_function);
293 }
294
295 int AddAnimatedTransformToLayer(Layer* layer,
296                                 double duration,
297                                 int delta_x,
298                                 int delta_y) {
299   return AddAnimatedTransform(layer, duration, delta_x, delta_y);
300 }
301
302 int AddAnimatedTransformToLayer(LayerImpl* layer,
303                                 double duration,
304                                 int delta_x,
305                                 int delta_y) {
306   return AddAnimatedTransform(layer->layer_animation_controller(),
307                               duration,
308                               delta_x,
309                               delta_y);
310 }
311
312 int AddAnimatedTransformToLayer(Layer* layer,
313                                 double duration,
314                                 TransformOperations start_operations,
315                                 TransformOperations operations) {
316   return AddAnimatedTransform(layer, duration, start_operations, operations);
317 }
318
319 int AddAnimatedTransformToLayer(LayerImpl* layer,
320                                 double duration,
321                                 TransformOperations start_operations,
322                                 TransformOperations operations) {
323   return AddAnimatedTransform(layer->layer_animation_controller(),
324                               duration,
325                               start_operations,
326                               operations);
327 }
328
329 int AddAnimatedFilterToLayer(Layer* layer,
330                              double duration,
331                              float start_brightness,
332                              float end_brightness) {
333   return AddAnimatedFilter(layer, duration, start_brightness, end_brightness);
334 }
335
336 int AddAnimatedFilterToLayer(LayerImpl* layer,
337                              double duration,
338                              float start_brightness,
339                              float end_brightness) {
340   return AddAnimatedFilter(layer->layer_animation_controller(),
341                            duration,
342                            start_brightness,
343                            end_brightness);
344 }
345
346 }  // namespace cc