Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / webkit / renderer / compositor_bindings / web_animation_impl.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 "webkit/renderer/compositor_bindings/web_animation_impl.h"
6
7 #include "cc/animation/animation.h"
8 #include "cc/animation/animation_curve.h"
9 #include "cc/animation/animation_id_provider.h"
10 #include "third_party/WebKit/public/platform/WebAnimation.h"
11 #include "third_party/WebKit/public/platform/WebAnimationCurve.h"
12 #include "webkit/renderer/compositor_bindings/web_filter_animation_curve_impl.h"
13 #include "webkit/renderer/compositor_bindings/web_float_animation_curve_impl.h"
14 #include "webkit/renderer/compositor_bindings/web_scroll_offset_animation_curve_impl.h"
15 #include "webkit/renderer/compositor_bindings/web_transform_animation_curve_impl.h"
16
17 using cc::Animation;
18 using cc::AnimationIdProvider;
19
20 using blink::WebAnimation;
21 using blink::WebAnimationCurve;
22
23 namespace webkit {
24
25 WebAnimationImpl::WebAnimationImpl(const WebAnimationCurve& web_curve,
26                                    TargetProperty target_property,
27                                    int animation_id,
28                                    int group_id) {
29   if (!animation_id)
30     animation_id = AnimationIdProvider::NextAnimationId();
31   if (!group_id)
32     group_id = AnimationIdProvider::NextGroupId();
33
34   WebAnimationCurve::AnimationCurveType curve_type = web_curve.type();
35   scoped_ptr<cc::AnimationCurve> curve;
36   switch (curve_type) {
37     case WebAnimationCurve::AnimationCurveTypeFloat: {
38       const WebFloatAnimationCurveImpl* float_curve_impl =
39           static_cast<const WebFloatAnimationCurveImpl*>(&web_curve);
40       curve = float_curve_impl->CloneToAnimationCurve();
41       break;
42     }
43     case WebAnimationCurve::AnimationCurveTypeTransform: {
44       const WebTransformAnimationCurveImpl* transform_curve_impl =
45           static_cast<const WebTransformAnimationCurveImpl*>(&web_curve);
46       curve = transform_curve_impl->CloneToAnimationCurve();
47       break;
48     }
49     case WebAnimationCurve::AnimationCurveTypeFilter: {
50       const WebFilterAnimationCurveImpl* filter_curve_impl =
51           static_cast<const WebFilterAnimationCurveImpl*>(&web_curve);
52       curve = filter_curve_impl->CloneToAnimationCurve();
53       break;
54     }
55 #if WEB_SCROLL_OFFSET_ANIMATION_CURVE_IS_DEFINED
56     case WebAnimationCurve::AnimationCurveTypeScrollOffset: {
57       const WebScrollOffsetAnimationCurveImpl* scroll_curve_impl =
58           static_cast<const WebScrollOffsetAnimationCurveImpl*>(&web_curve);
59       curve = scroll_curve_impl->CloneToAnimationCurve();
60       break;
61     }
62 #endif
63   }
64   animation_ = Animation::Create(
65       curve.Pass(),
66       animation_id,
67       group_id,
68       static_cast<cc::Animation::TargetProperty>(target_property));
69 }
70
71 WebAnimationImpl::~WebAnimationImpl() {}
72
73 int WebAnimationImpl::id() { return animation_->id(); }
74
75 blink::WebAnimation::TargetProperty WebAnimationImpl::targetProperty() const {
76   return static_cast<WebAnimationImpl::TargetProperty>(
77       animation_->target_property());
78 }
79
80 int WebAnimationImpl::iterations() const { return animation_->iterations(); }
81
82 void WebAnimationImpl::setIterations(int n) { animation_->set_iterations(n); }
83
84 double WebAnimationImpl::startTime() const { return animation_->start_time(); }
85
86 void WebAnimationImpl::setStartTime(double monotonic_time) {
87   animation_->set_start_time(monotonic_time);
88 }
89
90 double WebAnimationImpl::timeOffset() const {
91   return animation_->time_offset();
92 }
93
94 void WebAnimationImpl::setTimeOffset(double monotonic_time) {
95   animation_->set_time_offset(monotonic_time);
96 }
97
98 #if WEB_ANIMATION_SUPPORTS_FULL_DIRECTION
99 blink::WebAnimation::Direction WebAnimationImpl::direction() const {
100   switch (animation_->direction()) {
101     case cc::Animation::Normal:
102       return DirectionNormal;
103     case cc::Animation::Reverse:
104       return DirectionReverse;
105     case cc::Animation::Alternate:
106       return DirectionAlternate;
107     case cc::Animation::AlternateReverse:
108       return DirectionAlternateReverse;
109     default:
110       NOTREACHED();
111   }
112   return DirectionNormal;
113 }
114
115 void WebAnimationImpl::setDirection(Direction direction) {
116   switch (direction) {
117     case DirectionNormal:
118       animation_->set_direction(cc::Animation::Normal);
119       break;
120     case DirectionReverse:
121       animation_->set_direction(cc::Animation::Reverse);
122       break;
123     case DirectionAlternate:
124       animation_->set_direction(cc::Animation::Alternate);
125       break;
126     case DirectionAlternateReverse:
127       animation_->set_direction(cc::Animation::AlternateReverse);
128       break;
129   }
130 }
131 #else
132 bool WebAnimationImpl::alternatesDirection() const {
133   return animation_->direction() == cc::Animation::Alternate;
134 }
135
136 void WebAnimationImpl::setAlternatesDirection(bool alternates) {
137   if (alternates)
138     animation_->set_direction(cc::Animation::Alternate);
139   else
140     animation_->set_direction(cc::Animation::Normal);
141 }
142 #endif
143
144 scoped_ptr<cc::Animation> WebAnimationImpl::PassAnimation() {
145   animation_->set_needs_synchronized_start_time(true);
146   return animation_.Pass();
147 }
148
149 #define COMPILE_ASSERT_MATCHING_ENUMS(webkit_name, cc_name)                    \
150     COMPILE_ASSERT(static_cast<int>(webkit_name) == static_cast<int>(cc_name), \
151                    mismatching_enums)
152
153 COMPILE_ASSERT_MATCHING_ENUMS(
154     WebAnimation::TargetPropertyTransform, Animation::Transform);
155 COMPILE_ASSERT_MATCHING_ENUMS(
156     WebAnimation::TargetPropertyOpacity, Animation::Opacity);
157 COMPILE_ASSERT_MATCHING_ENUMS(
158     WebAnimation::TargetPropertyFilter, Animation::Filter);
159 #if WEB_SCROLL_OFFSET_ANIMATION_CURVE_IS_DEFINED
160 COMPILE_ASSERT_MATCHING_ENUMS(
161     WebAnimation::TargetPropertyScrollOffset, Animation::ScrollOffset);
162 #endif
163
164 }  // namespace webkit