| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 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 "ui/compositor/layer_animation_element.h" | |
| 6 | |
| 7 #include "base/compiler_specific.h" | |
| 8 #include "cc/animation/animation.h" | |
| 9 #include "cc/animation/animation_id_provider.h" | |
| 10 #include "ui/compositor/float_animation_curve_adapter.h" | |
| 11 #include "ui/compositor/layer.h" | |
| 12 #include "ui/compositor/layer_animation_delegate.h" | |
| 13 #include "ui/compositor/layer_animator.h" | |
| 14 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | |
| 15 #include "ui/compositor/transform_animation_curve_adapter.h" | |
| 16 #include "ui/gfx/animation/tween.h" | |
| 17 #include "ui/gfx/interpolated_transform.h" | |
| 18 | |
| 19 namespace ui { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // The factor by which duration is scaled up or down when using | |
| 24 // ScopedAnimationDurationScaleMode. | |
| 25 const int kSlowDurationScaleMultiplier = 4; | |
| 26 const int kFastDurationScaleDivisor = 4; | |
| 27 const int kNonZeroDurationScaleDivisor = 20; | |
| 28 | |
| 29 // Pause ----------------------------------------------------------------------- | |
| 30 class Pause : public LayerAnimationElement { | |
| 31 public: | |
| 32 Pause(AnimatableProperties properties, base::TimeDelta duration) | |
| 33 : LayerAnimationElement(properties, duration) { | |
| 34 } | |
| 35 ~Pause() override {} | |
| 36 | |
| 37 private: | |
| 38 void OnStart(LayerAnimationDelegate* delegate) override {} | |
| 39 bool OnProgress(double t, LayerAnimationDelegate* delegate) override { | |
| 40 return false; | |
| 41 } | |
| 42 void OnGetTarget(TargetValue* target) const override {} | |
| 43 void OnAbort(LayerAnimationDelegate* delegate) override {} | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(Pause); | |
| 46 }; | |
| 47 | |
| 48 // TransformTransition --------------------------------------------------------- | |
| 49 | |
| 50 class TransformTransition : public LayerAnimationElement { | |
| 51 public: | |
| 52 TransformTransition(const gfx::Transform& target, base::TimeDelta duration) | |
| 53 : LayerAnimationElement(TRANSFORM, duration), | |
| 54 target_(target) { | |
| 55 } | |
| 56 ~TransformTransition() override {} | |
| 57 | |
| 58 protected: | |
| 59 void OnStart(LayerAnimationDelegate* delegate) override { | |
| 60 start_ = delegate->GetTransformForAnimation(); | |
| 61 } | |
| 62 | |
| 63 bool OnProgress(double t, LayerAnimationDelegate* delegate) override { | |
| 64 delegate->SetTransformFromAnimation( | |
| 65 gfx::Tween::TransformValueBetween(t, start_, target_)); | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 void OnGetTarget(TargetValue* target) const override { | |
| 70 target->transform = target_; | |
| 71 } | |
| 72 | |
| 73 void OnAbort(LayerAnimationDelegate* delegate) override {} | |
| 74 | |
| 75 private: | |
| 76 gfx::Transform start_; | |
| 77 const gfx::Transform target_; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(TransformTransition); | |
| 80 }; | |
| 81 | |
| 82 // InterpolatedTransformTransition --------------------------------------------- | |
| 83 | |
| 84 class InterpolatedTransformTransition : public LayerAnimationElement { | |
| 85 public: | |
| 86 InterpolatedTransformTransition(InterpolatedTransform* interpolated_transform, | |
| 87 base::TimeDelta duration) | |
| 88 : LayerAnimationElement(TRANSFORM, duration), | |
| 89 interpolated_transform_(interpolated_transform) { | |
| 90 } | |
| 91 ~InterpolatedTransformTransition() override {} | |
| 92 | |
| 93 protected: | |
| 94 void OnStart(LayerAnimationDelegate* delegate) override {} | |
| 95 | |
| 96 bool OnProgress(double t, LayerAnimationDelegate* delegate) override { | |
| 97 delegate->SetTransformFromAnimation( | |
| 98 interpolated_transform_->Interpolate(static_cast<float>(t))); | |
| 99 return true; | |
| 100 } | |
| 101 | |
| 102 void OnGetTarget(TargetValue* target) const override { | |
| 103 target->transform = interpolated_transform_->Interpolate(1.0f); | |
| 104 } | |
| 105 | |
| 106 void OnAbort(LayerAnimationDelegate* delegate) override {} | |
| 107 | |
| 108 private: | |
| 109 scoped_ptr<InterpolatedTransform> interpolated_transform_; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(InterpolatedTransformTransition); | |
| 112 }; | |
| 113 | |
| 114 // BoundsTransition ------------------------------------------------------------ | |
| 115 | |
| 116 class BoundsTransition : public LayerAnimationElement { | |
| 117 public: | |
| 118 BoundsTransition(const gfx::Rect& target, base::TimeDelta duration) | |
| 119 : LayerAnimationElement(BOUNDS, duration), | |
| 120 target_(target) { | |
| 121 } | |
| 122 ~BoundsTransition() override {} | |
| 123 | |
| 124 protected: | |
| 125 void OnStart(LayerAnimationDelegate* delegate) override { | |
| 126 start_ = delegate->GetBoundsForAnimation(); | |
| 127 } | |
| 128 | |
| 129 bool OnProgress(double t, LayerAnimationDelegate* delegate) override { | |
| 130 delegate->SetBoundsFromAnimation( | |
| 131 gfx::Tween::RectValueBetween(t, start_, target_)); | |
| 132 return true; | |
| 133 } | |
| 134 | |
| 135 void OnGetTarget(TargetValue* target) const override { | |
| 136 target->bounds = target_; | |
| 137 } | |
| 138 | |
| 139 void OnAbort(LayerAnimationDelegate* delegate) override {} | |
| 140 | |
| 141 private: | |
| 142 gfx::Rect start_; | |
| 143 const gfx::Rect target_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(BoundsTransition); | |
| 146 }; | |
| 147 | |
| 148 // OpacityTransition ----------------------------------------------------------- | |
| 149 | |
| 150 class OpacityTransition : public LayerAnimationElement { | |
| 151 public: | |
| 152 OpacityTransition(float target, base::TimeDelta duration) | |
| 153 : LayerAnimationElement(OPACITY, duration), | |
| 154 start_(0.0f), | |
| 155 target_(target) { | |
| 156 } | |
| 157 ~OpacityTransition() override {} | |
| 158 | |
| 159 protected: | |
| 160 void OnStart(LayerAnimationDelegate* delegate) override { | |
| 161 start_ = delegate->GetOpacityForAnimation(); | |
| 162 } | |
| 163 | |
| 164 bool OnProgress(double t, LayerAnimationDelegate* delegate) override { | |
| 165 delegate->SetOpacityFromAnimation( | |
| 166 gfx::Tween::FloatValueBetween(t, start_, target_)); | |
| 167 return true; | |
| 168 } | |
| 169 | |
| 170 void OnGetTarget(TargetValue* target) const override { | |
| 171 target->opacity = target_; | |
| 172 } | |
| 173 | |
| 174 void OnAbort(LayerAnimationDelegate* delegate) override {} | |
| 175 | |
| 176 private: | |
| 177 float start_; | |
| 178 const float target_; | |
| 179 | |
| 180 DISALLOW_COPY_AND_ASSIGN(OpacityTransition); | |
| 181 }; | |
| 182 | |
| 183 // VisibilityTransition -------------------------------------------------------- | |
| 184 | |
| 185 class VisibilityTransition : public LayerAnimationElement { | |
| 186 public: | |
| 187 VisibilityTransition(bool target, base::TimeDelta duration) | |
| 188 : LayerAnimationElement(VISIBILITY, duration), | |
| 189 start_(false), | |
| 190 target_(target) { | |
| 191 } | |
| 192 ~VisibilityTransition() override {} | |
| 193 | |
| 194 protected: | |
| 195 void OnStart(LayerAnimationDelegate* delegate) override { | |
| 196 start_ = delegate->GetVisibilityForAnimation(); | |
| 197 } | |
| 198 | |
| 199 bool OnProgress(double t, LayerAnimationDelegate* delegate) override { | |
| 200 delegate->SetVisibilityFromAnimation(t == 1.0 ? target_ : start_); | |
| 201 return t == 1.0; | |
| 202 } | |
| 203 | |
| 204 void OnGetTarget(TargetValue* target) const override { | |
| 205 target->visibility = target_; | |
| 206 } | |
| 207 | |
| 208 void OnAbort(LayerAnimationDelegate* delegate) override {} | |
| 209 | |
| 210 private: | |
| 211 bool start_; | |
| 212 const bool target_; | |
| 213 | |
| 214 DISALLOW_COPY_AND_ASSIGN(VisibilityTransition); | |
| 215 }; | |
| 216 | |
| 217 // BrightnessTransition -------------------------------------------------------- | |
| 218 | |
| 219 class BrightnessTransition : public LayerAnimationElement { | |
| 220 public: | |
| 221 BrightnessTransition(float target, base::TimeDelta duration) | |
| 222 : LayerAnimationElement(BRIGHTNESS, duration), | |
| 223 start_(0.0f), | |
| 224 target_(target) { | |
| 225 } | |
| 226 ~BrightnessTransition() override {} | |
| 227 | |
| 228 protected: | |
| 229 void OnStart(LayerAnimationDelegate* delegate) override { | |
| 230 start_ = delegate->GetBrightnessForAnimation(); | |
| 231 } | |
| 232 | |
| 233 bool OnProgress(double t, LayerAnimationDelegate* delegate) override { | |
| 234 delegate->SetBrightnessFromAnimation( | |
| 235 gfx::Tween::FloatValueBetween(t, start_, target_)); | |
| 236 return true; | |
| 237 } | |
| 238 | |
| 239 void OnGetTarget(TargetValue* target) const override { | |
| 240 target->brightness = target_; | |
| 241 } | |
| 242 | |
| 243 void OnAbort(LayerAnimationDelegate* delegate) override {} | |
| 244 | |
| 245 private: | |
| 246 float start_; | |
| 247 const float target_; | |
| 248 | |
| 249 DISALLOW_COPY_AND_ASSIGN(BrightnessTransition); | |
| 250 }; | |
| 251 | |
| 252 // GrayscaleTransition --------------------------------------------------------- | |
| 253 | |
| 254 class GrayscaleTransition : public LayerAnimationElement { | |
| 255 public: | |
| 256 GrayscaleTransition(float target, base::TimeDelta duration) | |
| 257 : LayerAnimationElement(GRAYSCALE, duration), | |
| 258 start_(0.0f), | |
| 259 target_(target) { | |
| 260 } | |
| 261 ~GrayscaleTransition() override {} | |
| 262 | |
| 263 protected: | |
| 264 void OnStart(LayerAnimationDelegate* delegate) override { | |
| 265 start_ = delegate->GetGrayscaleForAnimation(); | |
| 266 } | |
| 267 | |
| 268 bool OnProgress(double t, LayerAnimationDelegate* delegate) override { | |
| 269 delegate->SetGrayscaleFromAnimation( | |
| 270 gfx::Tween::FloatValueBetween(t, start_, target_)); | |
| 271 return true; | |
| 272 } | |
| 273 | |
| 274 void OnGetTarget(TargetValue* target) const override { | |
| 275 target->grayscale = target_; | |
| 276 } | |
| 277 | |
| 278 void OnAbort(LayerAnimationDelegate* delegate) override {} | |
| 279 | |
| 280 private: | |
| 281 float start_; | |
| 282 const float target_; | |
| 283 | |
| 284 DISALLOW_COPY_AND_ASSIGN(GrayscaleTransition); | |
| 285 }; | |
| 286 | |
| 287 // ColorTransition ------------------------------------------------------------- | |
| 288 | |
| 289 class ColorTransition : public LayerAnimationElement { | |
| 290 public: | |
| 291 ColorTransition(SkColor target, base::TimeDelta duration) | |
| 292 : LayerAnimationElement(COLOR, duration), | |
| 293 start_(SK_ColorBLACK), | |
| 294 target_(target) { | |
| 295 } | |
| 296 ~ColorTransition() override {} | |
| 297 | |
| 298 protected: | |
| 299 void OnStart(LayerAnimationDelegate* delegate) override { | |
| 300 start_ = delegate->GetColorForAnimation(); | |
| 301 } | |
| 302 | |
| 303 bool OnProgress(double t, LayerAnimationDelegate* delegate) override { | |
| 304 delegate->SetColorFromAnimation( | |
| 305 gfx::Tween::ColorValueBetween(t, start_, target_)); | |
| 306 return true; | |
| 307 } | |
| 308 | |
| 309 void OnGetTarget(TargetValue* target) const override { | |
| 310 target->color = target_; | |
| 311 } | |
| 312 | |
| 313 void OnAbort(LayerAnimationDelegate* delegate) override {} | |
| 314 | |
| 315 private: | |
| 316 SkColor start_; | |
| 317 const SkColor target_; | |
| 318 | |
| 319 DISALLOW_COPY_AND_ASSIGN(ColorTransition); | |
| 320 }; | |
| 321 | |
| 322 // ThreadedLayerAnimationElement ----------------------------------------------- | |
| 323 | |
| 324 class ThreadedLayerAnimationElement : public LayerAnimationElement { | |
| 325 public: | |
| 326 ThreadedLayerAnimationElement(AnimatableProperties properties, | |
| 327 base::TimeDelta duration) | |
| 328 : LayerAnimationElement(properties, duration) { | |
| 329 } | |
| 330 ~ThreadedLayerAnimationElement() override {} | |
| 331 | |
| 332 bool IsThreaded() const override { return (duration() != base::TimeDelta()); } | |
| 333 | |
| 334 protected: | |
| 335 explicit ThreadedLayerAnimationElement(const LayerAnimationElement& element) | |
| 336 : LayerAnimationElement(element) { | |
| 337 } | |
| 338 | |
| 339 bool OnProgress(double t, LayerAnimationDelegate* delegate) override { | |
| 340 if (t < 1.0) | |
| 341 return false; | |
| 342 | |
| 343 if (Started()) { | |
| 344 delegate->RemoveThreadedAnimation(animation_id()); | |
| 345 } | |
| 346 | |
| 347 OnEnd(delegate); | |
| 348 return true; | |
| 349 } | |
| 350 | |
| 351 void OnAbort(LayerAnimationDelegate* delegate) override { | |
| 352 if (delegate && Started()) { | |
| 353 delegate->RemoveThreadedAnimation(animation_id()); | |
| 354 } | |
| 355 } | |
| 356 | |
| 357 void RequestEffectiveStart(LayerAnimationDelegate* delegate) override { | |
| 358 DCHECK(animation_group_id()); | |
| 359 if (duration() == base::TimeDelta()) { | |
| 360 set_effective_start_time(requested_start_time()); | |
| 361 return; | |
| 362 } | |
| 363 set_effective_start_time(base::TimeTicks()); | |
| 364 scoped_ptr<cc::Animation> animation = CreateCCAnimation(); | |
| 365 animation->set_needs_synchronized_start_time(true); | |
| 366 delegate->AddThreadedAnimation(animation.Pass()); | |
| 367 } | |
| 368 | |
| 369 virtual void OnEnd(LayerAnimationDelegate* delegate) = 0; | |
| 370 | |
| 371 virtual scoped_ptr<cc::Animation> CreateCCAnimation() = 0; | |
| 372 | |
| 373 private: | |
| 374 DISALLOW_COPY_AND_ASSIGN(ThreadedLayerAnimationElement); | |
| 375 }; | |
| 376 | |
| 377 // ThreadedOpacityTransition --------------------------------------------------- | |
| 378 | |
| 379 class ThreadedOpacityTransition : public ThreadedLayerAnimationElement { | |
| 380 public: | |
| 381 ThreadedOpacityTransition(float target, base::TimeDelta duration) | |
| 382 : ThreadedLayerAnimationElement(OPACITY, duration), | |
| 383 start_(0.0f), | |
| 384 target_(target) { | |
| 385 } | |
| 386 ~ThreadedOpacityTransition() override {} | |
| 387 | |
| 388 protected: | |
| 389 void OnStart(LayerAnimationDelegate* delegate) override { | |
| 390 start_ = delegate->GetOpacityForAnimation(); | |
| 391 } | |
| 392 | |
| 393 void OnAbort(LayerAnimationDelegate* delegate) override { | |
| 394 if (delegate && Started()) { | |
| 395 ThreadedLayerAnimationElement::OnAbort(delegate); | |
| 396 delegate->SetOpacityFromAnimation(gfx::Tween::FloatValueBetween( | |
| 397 gfx::Tween::CalculateValue(tween_type(), last_progressed_fraction()), | |
| 398 start_, | |
| 399 target_)); | |
| 400 } | |
| 401 } | |
| 402 | |
| 403 void OnEnd(LayerAnimationDelegate* delegate) override { | |
| 404 delegate->SetOpacityFromAnimation(target_); | |
| 405 } | |
| 406 | |
| 407 scoped_ptr<cc::Animation> CreateCCAnimation() override { | |
| 408 scoped_ptr<cc::AnimationCurve> animation_curve( | |
| 409 new FloatAnimationCurveAdapter(tween_type(), | |
| 410 start_, | |
| 411 target_, | |
| 412 duration())); | |
| 413 scoped_ptr<cc::Animation> animation( | |
| 414 cc::Animation::Create(animation_curve.Pass(), | |
| 415 animation_id(), | |
| 416 animation_group_id(), | |
| 417 cc::Animation::Opacity)); | |
| 418 return animation.Pass(); | |
| 419 } | |
| 420 | |
| 421 void OnGetTarget(TargetValue* target) const override { | |
| 422 target->opacity = target_; | |
| 423 } | |
| 424 | |
| 425 private: | |
| 426 float start_; | |
| 427 const float target_; | |
| 428 | |
| 429 DISALLOW_COPY_AND_ASSIGN(ThreadedOpacityTransition); | |
| 430 }; | |
| 431 | |
| 432 // ThreadedTransformTransition ------------------------------------------------- | |
| 433 | |
| 434 class ThreadedTransformTransition : public ThreadedLayerAnimationElement { | |
| 435 public: | |
| 436 ThreadedTransformTransition(const gfx::Transform& target, | |
| 437 base::TimeDelta duration) | |
| 438 : ThreadedLayerAnimationElement(TRANSFORM, duration), | |
| 439 target_(target) { | |
| 440 } | |
| 441 ~ThreadedTransformTransition() override {} | |
| 442 | |
| 443 protected: | |
| 444 void OnStart(LayerAnimationDelegate* delegate) override { | |
| 445 start_ = delegate->GetTransformForAnimation(); | |
| 446 } | |
| 447 | |
| 448 void OnAbort(LayerAnimationDelegate* delegate) override { | |
| 449 if (delegate && Started()) { | |
| 450 ThreadedLayerAnimationElement::OnAbort(delegate); | |
| 451 delegate->SetTransformFromAnimation(gfx::Tween::TransformValueBetween( | |
| 452 gfx::Tween::CalculateValue(tween_type(), last_progressed_fraction()), | |
| 453 start_, | |
| 454 target_)); | |
| 455 } | |
| 456 } | |
| 457 | |
| 458 void OnEnd(LayerAnimationDelegate* delegate) override { | |
| 459 delegate->SetTransformFromAnimation(target_); | |
| 460 } | |
| 461 | |
| 462 scoped_ptr<cc::Animation> CreateCCAnimation() override { | |
| 463 scoped_ptr<cc::AnimationCurve> animation_curve( | |
| 464 new TransformAnimationCurveAdapter(tween_type(), | |
| 465 start_, | |
| 466 target_, | |
| 467 duration())); | |
| 468 scoped_ptr<cc::Animation> animation( | |
| 469 cc::Animation::Create(animation_curve.Pass(), | |
| 470 animation_id(), | |
| 471 animation_group_id(), | |
| 472 cc::Animation::Transform)); | |
| 473 return animation.Pass(); | |
| 474 } | |
| 475 | |
| 476 void OnGetTarget(TargetValue* target) const override { | |
| 477 target->transform = target_; | |
| 478 } | |
| 479 | |
| 480 private: | |
| 481 gfx::Transform start_; | |
| 482 const gfx::Transform target_; | |
| 483 | |
| 484 DISALLOW_COPY_AND_ASSIGN(ThreadedTransformTransition); | |
| 485 }; | |
| 486 | |
| 487 // InverseTransformTransision -------------------------------------------------- | |
| 488 | |
| 489 class InverseTransformTransition : public ThreadedLayerAnimationElement { | |
| 490 public: | |
| 491 InverseTransformTransition(const gfx::Transform& base_transform, | |
| 492 const LayerAnimationElement* uninverted_transition) | |
| 493 : ThreadedLayerAnimationElement(*uninverted_transition), | |
| 494 base_transform_(base_transform), | |
| 495 uninverted_transition_( | |
| 496 CheckAndCast<const ThreadedTransformTransition*>( | |
| 497 uninverted_transition)) { | |
| 498 } | |
| 499 ~InverseTransformTransition() override {} | |
| 500 | |
| 501 static InverseTransformTransition* Clone(const LayerAnimationElement* other) { | |
| 502 const InverseTransformTransition* other_inverse = | |
| 503 CheckAndCast<const InverseTransformTransition*>(other); | |
| 504 return new InverseTransformTransition( | |
| 505 other_inverse->base_transform_, other_inverse->uninverted_transition_); | |
| 506 } | |
| 507 | |
| 508 protected: | |
| 509 void OnStart(LayerAnimationDelegate* delegate) override { | |
| 510 gfx::Transform start(delegate->GetTransformForAnimation()); | |
| 511 effective_start_ = base_transform_ * start; | |
| 512 | |
| 513 TargetValue target; | |
| 514 uninverted_transition_->GetTargetValue(&target); | |
| 515 base_target_ = target.transform; | |
| 516 | |
| 517 set_tween_type(uninverted_transition_->tween_type()); | |
| 518 | |
| 519 TransformAnimationCurveAdapter base_curve(tween_type(), | |
| 520 base_transform_, | |
| 521 base_target_, | |
| 522 duration()); | |
| 523 | |
| 524 animation_curve_.reset(new InverseTransformCurveAdapter( | |
| 525 base_curve, start, duration())); | |
| 526 computed_target_transform_ = ComputeWithBaseTransform(effective_start_, | |
| 527 base_target_); | |
| 528 } | |
| 529 | |
| 530 void OnAbort(LayerAnimationDelegate* delegate) override { | |
| 531 if (delegate && Started()) { | |
| 532 ThreadedLayerAnimationElement::OnAbort(delegate); | |
| 533 delegate->SetTransformFromAnimation(ComputeCurrentTransform()); | |
| 534 } | |
| 535 } | |
| 536 | |
| 537 void OnEnd(LayerAnimationDelegate* delegate) override { | |
| 538 delegate->SetTransformFromAnimation(computed_target_transform_); | |
| 539 } | |
| 540 | |
| 541 scoped_ptr<cc::Animation> CreateCCAnimation() override { | |
| 542 scoped_ptr<cc::Animation> animation( | |
| 543 cc::Animation::Create(animation_curve_->Clone(), | |
| 544 animation_id(), | |
| 545 animation_group_id(), | |
| 546 cc::Animation::Transform)); | |
| 547 return animation.Pass(); | |
| 548 } | |
| 549 | |
| 550 void OnGetTarget(TargetValue* target) const override { | |
| 551 target->transform = computed_target_transform_; | |
| 552 } | |
| 553 | |
| 554 private: | |
| 555 gfx::Transform ComputeCurrentTransform() const { | |
| 556 gfx::Transform base_current = gfx::Tween::TransformValueBetween( | |
| 557 gfx::Tween::CalculateValue(tween_type(), last_progressed_fraction()), | |
| 558 base_transform_, | |
| 559 base_target_); | |
| 560 return ComputeWithBaseTransform(effective_start_, base_current); | |
| 561 } | |
| 562 | |
| 563 gfx::Transform ComputeWithBaseTransform(gfx::Transform start, | |
| 564 gfx::Transform target) const { | |
| 565 gfx::Transform to_return(gfx::Transform::kSkipInitialization); | |
| 566 bool success = target.GetInverse(&to_return); | |
| 567 DCHECK(success) << "Target transform must be invertible."; | |
| 568 | |
| 569 to_return.PreconcatTransform(start); | |
| 570 return to_return; | |
| 571 } | |
| 572 | |
| 573 template <typename T> | |
| 574 static T CheckAndCast(const LayerAnimationElement* element) { | |
| 575 AnimatableProperties properties = element->properties(); | |
| 576 DCHECK(properties & TRANSFORM); | |
| 577 return static_cast<T>(element); | |
| 578 } | |
| 579 | |
| 580 gfx::Transform effective_start_; | |
| 581 gfx::Transform computed_target_transform_; | |
| 582 | |
| 583 const gfx::Transform base_transform_; | |
| 584 gfx::Transform base_target_; | |
| 585 | |
| 586 scoped_ptr<cc::AnimationCurve> animation_curve_; | |
| 587 | |
| 588 const ThreadedTransformTransition* const uninverted_transition_; | |
| 589 | |
| 590 DISALLOW_COPY_AND_ASSIGN(InverseTransformTransition); | |
| 591 }; | |
| 592 | |
| 593 } // namespace | |
| 594 | |
| 595 // LayerAnimationElement::TargetValue ------------------------------------------ | |
| 596 | |
| 597 LayerAnimationElement::TargetValue::TargetValue() | |
| 598 : opacity(0.0f), | |
| 599 visibility(false), | |
| 600 brightness(0.0f), | |
| 601 grayscale(0.0f), | |
| 602 color(SK_ColorBLACK) { | |
| 603 } | |
| 604 | |
| 605 LayerAnimationElement::TargetValue::TargetValue( | |
| 606 const LayerAnimationDelegate* delegate) | |
| 607 : bounds(delegate ? delegate->GetBoundsForAnimation() : gfx::Rect()), | |
| 608 transform(delegate ? | |
| 609 delegate->GetTransformForAnimation() : gfx::Transform()), | |
| 610 opacity(delegate ? delegate->GetOpacityForAnimation() : 0.0f), | |
| 611 visibility(delegate ? delegate->GetVisibilityForAnimation() : false), | |
| 612 brightness(delegate ? delegate->GetBrightnessForAnimation() : 0.0f), | |
| 613 grayscale(delegate ? delegate->GetGrayscaleForAnimation() : 0.0f), | |
| 614 color(delegate ? delegate->GetColorForAnimation() : 0.0f) { | |
| 615 } | |
| 616 | |
| 617 // LayerAnimationElement ------------------------------------------------------- | |
| 618 | |
| 619 LayerAnimationElement::LayerAnimationElement( | |
| 620 AnimatableProperties properties, base::TimeDelta duration) | |
| 621 : first_frame_(true), | |
| 622 properties_(properties), | |
| 623 duration_(GetEffectiveDuration(duration)), | |
| 624 tween_type_(gfx::Tween::LINEAR), | |
| 625 animation_id_(cc::AnimationIdProvider::NextAnimationId()), | |
| 626 animation_group_id_(0), | |
| 627 last_progressed_fraction_(0.0), | |
| 628 weak_ptr_factory_(this) { | |
| 629 } | |
| 630 | |
| 631 LayerAnimationElement::LayerAnimationElement( | |
| 632 const LayerAnimationElement &element) | |
| 633 : first_frame_(element.first_frame_), | |
| 634 properties_(element.properties_), | |
| 635 duration_(element.duration_), | |
| 636 tween_type_(element.tween_type_), | |
| 637 animation_id_(cc::AnimationIdProvider::NextAnimationId()), | |
| 638 animation_group_id_(element.animation_group_id_), | |
| 639 last_progressed_fraction_(element.last_progressed_fraction_), | |
| 640 weak_ptr_factory_(this) { | |
| 641 } | |
| 642 | |
| 643 LayerAnimationElement::~LayerAnimationElement() { | |
| 644 } | |
| 645 | |
| 646 void LayerAnimationElement::Start(LayerAnimationDelegate* delegate, | |
| 647 int animation_group_id) { | |
| 648 DCHECK(requested_start_time_ != base::TimeTicks()); | |
| 649 DCHECK(first_frame_); | |
| 650 animation_group_id_ = animation_group_id; | |
| 651 last_progressed_fraction_ = 0.0; | |
| 652 OnStart(delegate); | |
| 653 RequestEffectiveStart(delegate); | |
| 654 first_frame_ = false; | |
| 655 } | |
| 656 | |
| 657 bool LayerAnimationElement::Progress(base::TimeTicks now, | |
| 658 LayerAnimationDelegate* delegate) { | |
| 659 DCHECK(requested_start_time_ != base::TimeTicks()); | |
| 660 DCHECK(!first_frame_); | |
| 661 | |
| 662 bool need_draw; | |
| 663 double t = 1.0; | |
| 664 | |
| 665 if ((effective_start_time_ == base::TimeTicks()) || | |
| 666 (now < effective_start_time_)) { | |
| 667 // This hasn't actually started yet. | |
| 668 need_draw = false; | |
| 669 last_progressed_fraction_ = 0.0; | |
| 670 return need_draw; | |
| 671 } | |
| 672 | |
| 673 base::TimeDelta elapsed = now - effective_start_time_; | |
| 674 if ((duration_ > base::TimeDelta()) && (elapsed < duration_)) | |
| 675 t = elapsed.InMillisecondsF() / duration_.InMillisecondsF(); | |
| 676 base::WeakPtr<LayerAnimationElement> alive(weak_ptr_factory_.GetWeakPtr()); | |
| 677 need_draw = OnProgress(gfx::Tween::CalculateValue(tween_type_, t), delegate); | |
| 678 if (!alive) | |
| 679 return need_draw; | |
| 680 first_frame_ = t == 1.0; | |
| 681 last_progressed_fraction_ = t; | |
| 682 return need_draw; | |
| 683 } | |
| 684 | |
| 685 bool LayerAnimationElement::IsFinished(base::TimeTicks time, | |
| 686 base::TimeDelta* total_duration) { | |
| 687 // If an effective start has been requested but the effective start time | |
| 688 // hasn't yet been set, the animation is not finished, regardless of the | |
| 689 // value of |time|. | |
| 690 if (!first_frame_ && (effective_start_time_ == base::TimeTicks())) | |
| 691 return false; | |
| 692 | |
| 693 base::TimeDelta queueing_delay; | |
| 694 if (!first_frame_) | |
| 695 queueing_delay = effective_start_time_ - requested_start_time_; | |
| 696 | |
| 697 base::TimeDelta elapsed = time - requested_start_time_; | |
| 698 if (elapsed >= duration_ + queueing_delay) { | |
| 699 *total_duration = duration_ + queueing_delay; | |
| 700 return true; | |
| 701 } | |
| 702 return false; | |
| 703 } | |
| 704 | |
| 705 bool LayerAnimationElement::ProgressToEnd(LayerAnimationDelegate* delegate) { | |
| 706 if (first_frame_) | |
| 707 OnStart(delegate); | |
| 708 base::WeakPtr<LayerAnimationElement> alive(weak_ptr_factory_.GetWeakPtr()); | |
| 709 bool need_draw = OnProgress(1.0, delegate); | |
| 710 if (!alive) | |
| 711 return need_draw; | |
| 712 last_progressed_fraction_ = 1.0; | |
| 713 first_frame_ = true; | |
| 714 return need_draw; | |
| 715 } | |
| 716 | |
| 717 void LayerAnimationElement::GetTargetValue(TargetValue* target) const { | |
| 718 OnGetTarget(target); | |
| 719 } | |
| 720 | |
| 721 bool LayerAnimationElement::IsThreaded() const { | |
| 722 return false; | |
| 723 } | |
| 724 | |
| 725 void LayerAnimationElement::Abort(LayerAnimationDelegate* delegate) { | |
| 726 OnAbort(delegate); | |
| 727 first_frame_ = true; | |
| 728 } | |
| 729 | |
| 730 void LayerAnimationElement::RequestEffectiveStart( | |
| 731 LayerAnimationDelegate* delegate) { | |
| 732 DCHECK(requested_start_time_ != base::TimeTicks()); | |
| 733 effective_start_time_ = requested_start_time_; | |
| 734 } | |
| 735 | |
| 736 // static | |
| 737 LayerAnimationElement::AnimatableProperty | |
| 738 LayerAnimationElement::ToAnimatableProperty( | |
| 739 cc::Animation::TargetProperty property) { | |
| 740 switch (property) { | |
| 741 case cc::Animation::Transform: | |
| 742 return TRANSFORM; | |
| 743 case cc::Animation::Opacity: | |
| 744 return OPACITY; | |
| 745 default: | |
| 746 NOTREACHED(); | |
| 747 return AnimatableProperty(); | |
| 748 } | |
| 749 } | |
| 750 | |
| 751 // static | |
| 752 base::TimeDelta LayerAnimationElement::GetEffectiveDuration( | |
| 753 const base::TimeDelta& duration) { | |
| 754 switch (ScopedAnimationDurationScaleMode::duration_scale_mode()) { | |
| 755 case ScopedAnimationDurationScaleMode::NORMAL_DURATION: | |
| 756 return duration; | |
| 757 case ScopedAnimationDurationScaleMode::FAST_DURATION: | |
| 758 return duration / kFastDurationScaleDivisor; | |
| 759 case ScopedAnimationDurationScaleMode::SLOW_DURATION: | |
| 760 return duration * kSlowDurationScaleMultiplier; | |
| 761 case ScopedAnimationDurationScaleMode::NON_ZERO_DURATION: | |
| 762 return duration / kNonZeroDurationScaleDivisor; | |
| 763 case ScopedAnimationDurationScaleMode::ZERO_DURATION: | |
| 764 return base::TimeDelta(); | |
| 765 default: | |
| 766 NOTREACHED(); | |
| 767 return base::TimeDelta(); | |
| 768 } | |
| 769 } | |
| 770 | |
| 771 // static | |
| 772 LayerAnimationElement* LayerAnimationElement::CreateTransformElement( | |
| 773 const gfx::Transform& transform, | |
| 774 base::TimeDelta duration) { | |
| 775 return new ThreadedTransformTransition(transform, duration); | |
| 776 } | |
| 777 | |
| 778 // static | |
| 779 LayerAnimationElement* LayerAnimationElement::CreateInverseTransformElement( | |
| 780 const gfx::Transform& base_transform, | |
| 781 const LayerAnimationElement* uninverted_transition) { | |
| 782 return new InverseTransformTransition(base_transform, uninverted_transition); | |
| 783 } | |
| 784 | |
| 785 // static | |
| 786 LayerAnimationElement* LayerAnimationElement::CloneInverseTransformElement( | |
| 787 const LayerAnimationElement* other) { | |
| 788 return InverseTransformTransition::Clone(other); | |
| 789 } | |
| 790 | |
| 791 // static | |
| 792 LayerAnimationElement* | |
| 793 LayerAnimationElement::CreateInterpolatedTransformElement( | |
| 794 InterpolatedTransform* interpolated_transform, | |
| 795 base::TimeDelta duration) { | |
| 796 return new InterpolatedTransformTransition(interpolated_transform, duration); | |
| 797 } | |
| 798 | |
| 799 // static | |
| 800 LayerAnimationElement* LayerAnimationElement::CreateBoundsElement( | |
| 801 const gfx::Rect& bounds, | |
| 802 base::TimeDelta duration) { | |
| 803 return new BoundsTransition(bounds, duration); | |
| 804 } | |
| 805 | |
| 806 // static | |
| 807 LayerAnimationElement* LayerAnimationElement::CreateOpacityElement( | |
| 808 float opacity, | |
| 809 base::TimeDelta duration) { | |
| 810 return new ThreadedOpacityTransition(opacity, duration); | |
| 811 } | |
| 812 | |
| 813 // static | |
| 814 LayerAnimationElement* LayerAnimationElement::CreateVisibilityElement( | |
| 815 bool visibility, | |
| 816 base::TimeDelta duration) { | |
| 817 return new VisibilityTransition(visibility, duration); | |
| 818 } | |
| 819 | |
| 820 // static | |
| 821 LayerAnimationElement* LayerAnimationElement::CreateBrightnessElement( | |
| 822 float brightness, | |
| 823 base::TimeDelta duration) { | |
| 824 return new BrightnessTransition(brightness, duration); | |
| 825 } | |
| 826 | |
| 827 // static | |
| 828 LayerAnimationElement* LayerAnimationElement::CreateGrayscaleElement( | |
| 829 float grayscale, | |
| 830 base::TimeDelta duration) { | |
| 831 return new GrayscaleTransition(grayscale, duration); | |
| 832 } | |
| 833 | |
| 834 // static | |
| 835 LayerAnimationElement* LayerAnimationElement::CreatePauseElement( | |
| 836 AnimatableProperties properties, | |
| 837 base::TimeDelta duration) { | |
| 838 return new Pause(properties, duration); | |
| 839 } | |
| 840 | |
| 841 // static | |
| 842 LayerAnimationElement* LayerAnimationElement::CreateColorElement( | |
| 843 SkColor color, | |
| 844 base::TimeDelta duration) { | |
| 845 return new ColorTransition(color, duration); | |
| 846 } | |
| 847 | |
| 848 } // namespace ui | |
| OLD | NEW |