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