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