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_transform_animation_curve_impl.h"
15
16 using cc::Animation;
17 using cc::AnimationIdProvider;
18
19 using blink::WebAnimation;
20 using blink::WebAnimationCurve;
21
22 namespace webkit {
23
WebAnimationImpl(const WebAnimationCurve & web_curve,TargetProperty target_property,int animation_id,int group_id)24 WebAnimationImpl::WebAnimationImpl(const WebAnimationCurve& web_curve,
25 TargetProperty target_property,
26 int animation_id,
27 int group_id) {
28 if (!animation_id)
29 animation_id = AnimationIdProvider::NextAnimationId();
30 if (!group_id)
31 group_id = AnimationIdProvider::NextGroupId();
32
33 WebAnimationCurve::AnimationCurveType curve_type = web_curve.type();
34 scoped_ptr<cc::AnimationCurve> curve;
35 switch (curve_type) {
36 case WebAnimationCurve::AnimationCurveTypeFloat: {
37 const WebFloatAnimationCurveImpl* float_curve_impl =
38 static_cast<const WebFloatAnimationCurveImpl*>(&web_curve);
39 curve = float_curve_impl->CloneToAnimationCurve();
40 break;
41 }
42 case WebAnimationCurve::AnimationCurveTypeTransform: {
43 const WebTransformAnimationCurveImpl* transform_curve_impl =
44 static_cast<const WebTransformAnimationCurveImpl*>(&web_curve);
45 curve = transform_curve_impl->CloneToAnimationCurve();
46 break;
47 }
48 case WebAnimationCurve::AnimationCurveTypeFilter: {
49 const WebFilterAnimationCurveImpl* filter_curve_impl =
50 static_cast<const WebFilterAnimationCurveImpl*>(&web_curve);
51 curve = filter_curve_impl->CloneToAnimationCurve();
52 break;
53 }
54 }
55 animation_ = Animation::Create(
56 curve.Pass(),
57 animation_id,
58 group_id,
59 static_cast<cc::Animation::TargetProperty>(target_property));
60 }
61
~WebAnimationImpl()62 WebAnimationImpl::~WebAnimationImpl() {}
63
id()64 int WebAnimationImpl::id() { return animation_->id(); }
65
targetProperty() const66 blink::WebAnimation::TargetProperty WebAnimationImpl::targetProperty() const {
67 return static_cast<WebAnimationImpl::TargetProperty>(
68 animation_->target_property());
69 }
70
iterations() const71 int WebAnimationImpl::iterations() const { return animation_->iterations(); }
72
setIterations(int n)73 void WebAnimationImpl::setIterations(int n) { animation_->set_iterations(n); }
74
startTime() const75 double WebAnimationImpl::startTime() const { return animation_->start_time(); }
76
setStartTime(double monotonic_time)77 void WebAnimationImpl::setStartTime(double monotonic_time) {
78 animation_->set_start_time(monotonic_time);
79 }
80
timeOffset() const81 double WebAnimationImpl::timeOffset() const {
82 return animation_->time_offset();
83 }
84
setTimeOffset(double monotonic_time)85 void WebAnimationImpl::setTimeOffset(double monotonic_time) {
86 animation_->set_time_offset(monotonic_time);
87 }
88
alternatesDirection() const89 bool WebAnimationImpl::alternatesDirection() const {
90 return animation_->alternates_direction();
91 }
92
setAlternatesDirection(bool alternates)93 void WebAnimationImpl::setAlternatesDirection(bool alternates) {
94 animation_->set_alternates_direction(alternates);
95 }
96
PassAnimation()97 scoped_ptr<cc::Animation> WebAnimationImpl::PassAnimation() {
98 animation_->set_needs_synchronized_start_time(true);
99 return animation_.Pass();
100 }
101
102 #define COMPILE_ASSERT_MATCHING_ENUMS(webkit_name, cc_name) \
103 COMPILE_ASSERT(static_cast<int>(webkit_name) == static_cast<int>(cc_name), \
104 mismatching_enums)
105
106 COMPILE_ASSERT_MATCHING_ENUMS(
107 WebAnimation::TargetPropertyTransform, Animation::Transform);
108 COMPILE_ASSERT_MATCHING_ENUMS(
109 WebAnimation::TargetPropertyOpacity, Animation::Opacity);
110 COMPILE_ASSERT_MATCHING_ENUMS(
111 WebAnimation::TargetPropertyFilter, Animation::Filter);
112
113 } // namespace webkit
114