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
16using cc::Animation;
17using cc::AnimationIdProvider;
18
19using blink::WebAnimation;
20using blink::WebAnimationCurve;
21
22namespace webkit {
23
24WebAnimationImpl::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
62WebAnimationImpl::~WebAnimationImpl() {}
63
64int WebAnimationImpl::id() { return animation_->id(); }
65
66blink::WebAnimation::TargetProperty WebAnimationImpl::targetProperty() const {
67  return static_cast<WebAnimationImpl::TargetProperty>(
68      animation_->target_property());
69}
70
71int WebAnimationImpl::iterations() const { return animation_->iterations(); }
72
73void WebAnimationImpl::setIterations(int n) { animation_->set_iterations(n); }
74
75double WebAnimationImpl::startTime() const { return animation_->start_time(); }
76
77void WebAnimationImpl::setStartTime(double monotonic_time) {
78  animation_->set_start_time(monotonic_time);
79}
80
81double WebAnimationImpl::timeOffset() const {
82  return animation_->time_offset();
83}
84
85void WebAnimationImpl::setTimeOffset(double monotonic_time) {
86  animation_->set_time_offset(monotonic_time);
87}
88
89bool WebAnimationImpl::alternatesDirection() const {
90  return animation_->alternates_direction();
91}
92
93void WebAnimationImpl::setAlternatesDirection(bool alternates) {
94  animation_->set_alternates_direction(alternates);
95}
96
97scoped_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
106COMPILE_ASSERT_MATCHING_ENUMS(
107    WebAnimation::TargetPropertyTransform, Animation::Transform);
108COMPILE_ASSERT_MATCHING_ENUMS(
109    WebAnimation::TargetPropertyOpacity, Animation::Opacity);
110COMPILE_ASSERT_MATCHING_ENUMS(
111    WebAnimation::TargetPropertyFilter, Animation::Filter);
112
113}  // namespace webkit
114