1 // Copyright 2014 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/blink/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 "cc/blink/web_filter_animation_curve_impl.h" 11 #include "cc/blink/web_float_animation_curve_impl.h" 12 #include "cc/blink/web_scroll_offset_animation_curve_impl.h" 13 #include "cc/blink/web_transform_animation_curve_impl.h" 14 #include "third_party/WebKit/public/platform/WebCompositorAnimationCurve.h" 15 16 using cc::Animation; 17 using cc::AnimationIdProvider; 18 19 using blink::WebCompositorAnimation; 20 using blink::WebCompositorAnimationCurve; 21 22 namespace cc_blink { 23 WebCompositorAnimationImpl(const WebCompositorAnimationCurve & web_curve,TargetProperty target_property,int animation_id,int group_id)24WebCompositorAnimationImpl::WebCompositorAnimationImpl( 25 const WebCompositorAnimationCurve& 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 WebCompositorAnimationCurve::AnimationCurveType curve_type = web_curve.type(); 35 scoped_ptr<cc::AnimationCurve> curve; 36 switch (curve_type) { 37 case WebCompositorAnimationCurve::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 WebCompositorAnimationCurve::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 WebCompositorAnimationCurve::AnimationCurveTypeFilter: { 50 const WebFilterAnimationCurveImpl* filter_curve_impl = 51 static_cast<const WebFilterAnimationCurveImpl*>(&web_curve); 52 curve = filter_curve_impl->CloneToAnimationCurve(); 53 break; 54 } 55 case WebCompositorAnimationCurve::AnimationCurveTypeScrollOffset: { 56 const WebScrollOffsetAnimationCurveImpl* scroll_curve_impl = 57 static_cast<const WebScrollOffsetAnimationCurveImpl*>(&web_curve); 58 curve = scroll_curve_impl->CloneToAnimationCurve(); 59 break; 60 } 61 } 62 animation_ = Animation::Create( 63 curve.Pass(), 64 animation_id, 65 group_id, 66 static_cast<cc::Animation::TargetProperty>(target_property)); 67 } 68 ~WebCompositorAnimationImpl()69WebCompositorAnimationImpl::~WebCompositorAnimationImpl() { 70 } 71 id()72int WebCompositorAnimationImpl::id() { 73 return animation_->id(); 74 } 75 76 blink::WebCompositorAnimation::TargetProperty targetProperty() const77WebCompositorAnimationImpl::targetProperty() const { 78 return static_cast<WebCompositorAnimationImpl::TargetProperty>( 79 animation_->target_property()); 80 } 81 iterations() const82double WebCompositorAnimationImpl::iterations() const { 83 return animation_->iterations(); 84 } 85 setIterations(double n)86void WebCompositorAnimationImpl::setIterations(double n) { 87 animation_->set_iterations(n); 88 } 89 iterationStart() const90double WebCompositorAnimationImpl::iterationStart() const { 91 return animation_->iteration_start(); 92 } 93 setIterationStart(double iteration_start)94void WebCompositorAnimationImpl::setIterationStart(double iteration_start) { 95 animation_->set_iteration_start(iteration_start); 96 } 97 startTime() const98double WebCompositorAnimationImpl::startTime() const { 99 return (animation_->start_time() - base::TimeTicks()).InSecondsF(); 100 } 101 setStartTime(double monotonic_time)102void WebCompositorAnimationImpl::setStartTime(double monotonic_time) { 103 animation_->set_start_time(base::TimeTicks::FromInternalValue( 104 monotonic_time * base::Time::kMicrosecondsPerSecond)); 105 } 106 timeOffset() const107double WebCompositorAnimationImpl::timeOffset() const { 108 return animation_->time_offset().InSecondsF(); 109 } 110 setTimeOffset(double monotonic_time)111void WebCompositorAnimationImpl::setTimeOffset(double monotonic_time) { 112 animation_->set_time_offset(base::TimeDelta::FromSecondsD(monotonic_time)); 113 } 114 direction() const115blink::WebCompositorAnimation::Direction WebCompositorAnimationImpl::direction() 116 const { 117 switch (animation_->direction()) { 118 case cc::Animation::Normal: 119 return DirectionNormal; 120 case cc::Animation::Reverse: 121 return DirectionReverse; 122 case cc::Animation::Alternate: 123 return DirectionAlternate; 124 case cc::Animation::AlternateReverse: 125 return DirectionAlternateReverse; 126 default: 127 NOTREACHED(); 128 } 129 return DirectionNormal; 130 } 131 setDirection(Direction direction)132void WebCompositorAnimationImpl::setDirection(Direction direction) { 133 switch (direction) { 134 case DirectionNormal: 135 animation_->set_direction(cc::Animation::Normal); 136 break; 137 case DirectionReverse: 138 animation_->set_direction(cc::Animation::Reverse); 139 break; 140 case DirectionAlternate: 141 animation_->set_direction(cc::Animation::Alternate); 142 break; 143 case DirectionAlternateReverse: 144 animation_->set_direction(cc::Animation::AlternateReverse); 145 break; 146 } 147 } 148 playbackRate() const149double WebCompositorAnimationImpl::playbackRate() const { 150 return animation_->playback_rate(); 151 } 152 setPlaybackRate(double playback_rate)153void WebCompositorAnimationImpl::setPlaybackRate(double playback_rate) { 154 animation_->set_playback_rate(playback_rate); 155 } 156 157 #if WEB_ANIMATION_SUPPORTS_FILL_MODE fillMode() const158blink::WebCompositorAnimation::FillMode WebCompositorAnimationImpl::fillMode() 159 const { 160 switch (animation_->fill_mode()) { 161 case cc::Animation::FillModeNone: 162 return FillModeNone; 163 case cc::Animation::FillModeForwards: 164 return FillModeForwards; 165 case cc::Animation::FillModeBackwards: 166 return FillModeBackwards; 167 case cc::Animation::FillModeBoth: 168 return FillModeBoth; 169 default: 170 NOTREACHED(); 171 } 172 return FillModeNone; 173 } 174 setFillMode(FillMode fill_mode)175void WebCompositorAnimationImpl::setFillMode(FillMode fill_mode) { 176 switch (fill_mode) { 177 case FillModeNone: 178 animation_->set_fill_mode(cc::Animation::FillModeNone); 179 break; 180 case FillModeForwards: 181 animation_->set_fill_mode(cc::Animation::FillModeForwards); 182 break; 183 case FillModeBackwards: 184 animation_->set_fill_mode(cc::Animation::FillModeBackwards); 185 break; 186 case FillModeBoth: 187 animation_->set_fill_mode(cc::Animation::FillModeBoth); 188 break; 189 } 190 } 191 #endif PassAnimation()192scoped_ptr<cc::Animation> WebCompositorAnimationImpl::PassAnimation() { 193 animation_->set_needs_synchronized_start_time(true); 194 return animation_.Pass(); 195 } 196 197 } // namespace cc_blink 198