OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/animation/animation_host.h" | 5 #include "cc/animation/animation_host.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
| 9 #include "cc/animation/animation_delegate.h" |
| 10 #include "cc/animation/animation_id_provider.h" |
9 #include "cc/animation/animation_player.h" | 11 #include "cc/animation/animation_player.h" |
10 #include "cc/animation/animation_registrar.h" | 12 #include "cc/animation/animation_registrar.h" |
11 #include "cc/animation/animation_timeline.h" | 13 #include "cc/animation/animation_timeline.h" |
| 14 #include "cc/animation/scroll_offset_animation_curve.h" |
| 15 #include "cc/animation/timing_function.h" |
12 #include "cc/trees/layer_tree_mutators_client.h" | 16 #include "cc/trees/layer_tree_mutators_client.h" |
13 #include "ui/gfx/geometry/box_f.h" | 17 #include "ui/gfx/geometry/box_f.h" |
| 18 #include "ui/gfx/geometry/scroll_offset.h" |
14 | 19 |
15 namespace cc { | 20 namespace cc { |
16 | 21 |
| 22 class AnimationHost::ScrollOffsetAnimations : public AnimationDelegate { |
| 23 public: |
| 24 explicit ScrollOffsetAnimations(AnimationHost* animation_host) |
| 25 : animation_host_(animation_host), |
| 26 scroll_offset_timeline_( |
| 27 AnimationTimeline::Create(AnimationIdProvider::NextTimelineId())), |
| 28 scroll_offset_animation_player_( |
| 29 AnimationPlayer::Create(AnimationIdProvider::NextPlayerId())) { |
| 30 scroll_offset_timeline_->set_is_impl_only(true); |
| 31 scroll_offset_animation_player_->set_layer_animation_delegate(this); |
| 32 |
| 33 animation_host_->AddAnimationTimeline(scroll_offset_timeline_.get()); |
| 34 scroll_offset_timeline_->AttachPlayer( |
| 35 scroll_offset_animation_player_.get()); |
| 36 } |
| 37 |
| 38 ~ScrollOffsetAnimations() override { |
| 39 scroll_offset_timeline_->DetachPlayer( |
| 40 scroll_offset_animation_player_.get()); |
| 41 animation_host_->RemoveAnimationTimeline(scroll_offset_timeline_.get()); |
| 42 } |
| 43 |
| 44 void ScrollAnimationCreate(int layer_id, |
| 45 const gfx::ScrollOffset& target_offset, |
| 46 const gfx::ScrollOffset& current_offset) { |
| 47 scoped_ptr<ScrollOffsetAnimationCurve> curve = |
| 48 ScrollOffsetAnimationCurve::Create(target_offset, |
| 49 EaseInOutTimingFunction::Create()); |
| 50 curve->SetInitialValue(current_offset); |
| 51 |
| 52 scoped_ptr<Animation> animation = Animation::Create( |
| 53 curve.Pass(), AnimationIdProvider::NextAnimationId(), |
| 54 AnimationIdProvider::NextGroupId(), Animation::SCROLL_OFFSET); |
| 55 animation->set_is_impl_only(true); |
| 56 |
| 57 DCHECK(scroll_offset_animation_player_); |
| 58 DCHECK(scroll_offset_animation_player_->animation_timeline()); |
| 59 |
| 60 if (scroll_offset_animation_player_->layer_id() != layer_id) { |
| 61 if (scroll_offset_animation_player_->layer_id()) |
| 62 scroll_offset_animation_player_->DetachLayer(); |
| 63 scroll_offset_animation_player_->AttachLayer(layer_id); |
| 64 } |
| 65 |
| 66 scroll_offset_animation_player_->AddAnimation(animation.Pass()); |
| 67 } |
| 68 |
| 69 bool ScrollAnimationUpdateTarget(int layer_id, |
| 70 const gfx::Vector2dF& scroll_delta, |
| 71 const gfx::ScrollOffset& max_scroll_offset, |
| 72 base::TimeTicks frame_monotonic_time) { |
| 73 DCHECK(scroll_offset_animation_player_); |
| 74 DCHECK_EQ(layer_id, scroll_offset_animation_player_->layer_id()); |
| 75 |
| 76 Animation* animation = |
| 77 scroll_offset_animation_player_->layer_animation_controller() |
| 78 ->GetAnimation(Animation::SCROLL_OFFSET); |
| 79 if (!animation) { |
| 80 scroll_offset_animation_player_->DetachLayer(); |
| 81 return false; |
| 82 } |
| 83 |
| 84 ScrollOffsetAnimationCurve* curve = |
| 85 animation->curve()->ToScrollOffsetAnimationCurve(); |
| 86 |
| 87 gfx::ScrollOffset new_target = |
| 88 gfx::ScrollOffsetWithDelta(curve->target_value(), scroll_delta); |
| 89 new_target.SetToMax(gfx::ScrollOffset()); |
| 90 new_target.SetToMin(max_scroll_offset); |
| 91 |
| 92 curve->UpdateTarget(animation->TrimTimeToCurrentIteration( |
| 93 frame_monotonic_time).InSecondsF(), |
| 94 new_target); |
| 95 |
| 96 return true; |
| 97 } |
| 98 |
| 99 // AnimationDelegate implementation. |
| 100 void NotifyAnimationStarted(base::TimeTicks monotonic_time, |
| 101 Animation::TargetProperty target_property, |
| 102 int group) override {} |
| 103 void NotifyAnimationFinished(base::TimeTicks monotonic_time, |
| 104 Animation::TargetProperty target_property, |
| 105 int group) override { |
| 106 DCHECK_EQ(target_property, Animation::SCROLL_OFFSET); |
| 107 DCHECK(animation_host_->layer_tree_mutators_client()); |
| 108 animation_host_->layer_tree_mutators_client() |
| 109 ->ScrollOffsetAnimationFinished(); |
| 110 } |
| 111 |
| 112 private: |
| 113 AnimationHost* animation_host_; |
| 114 scoped_refptr<AnimationTimeline> scroll_offset_timeline_; |
| 115 scoped_refptr<AnimationPlayer> scroll_offset_animation_player_; |
| 116 |
| 117 DISALLOW_COPY_AND_ASSIGN(ScrollOffsetAnimations); |
| 118 }; |
| 119 |
17 scoped_ptr<AnimationHost> AnimationHost::Create(bool is_impl_instance) { | 120 scoped_ptr<AnimationHost> AnimationHost::Create(bool is_impl_instance) { |
18 return make_scoped_ptr(new AnimationHost(is_impl_instance)); | 121 return make_scoped_ptr(new AnimationHost(is_impl_instance)); |
19 } | 122 } |
20 | 123 |
21 AnimationHost::AnimationHost(bool is_impl_instance) | 124 AnimationHost::AnimationHost(bool is_impl_instance) |
22 : animation_registrar_(AnimationRegistrar::Create()), | 125 : animation_registrar_(AnimationRegistrar::Create()), |
23 layer_tree_mutators_client_(), | 126 layer_tree_mutators_client_(), |
24 is_impl_instance_(is_impl_instance) { | 127 is_impl_instance_(is_impl_instance) { |
| 128 if (is_impl_instance_) |
| 129 scroll_offset_animations_ = |
| 130 make_scoped_ptr(new ScrollOffsetAnimations(this)); |
25 } | 131 } |
26 | 132 |
27 AnimationHost::~AnimationHost() { | 133 AnimationHost::~AnimationHost() { |
| 134 scroll_offset_animations_ = nullptr; |
| 135 |
28 ClearTimelines(); | 136 ClearTimelines(); |
29 DCHECK(!layer_tree_mutators_client()); | 137 DCHECK(!layer_tree_mutators_client()); |
30 } | 138 } |
31 | 139 |
32 AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) const { | 140 AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) const { |
33 for (auto& timeline : timelines_) | 141 for (auto& timeline : timelines_) |
34 if (timeline->id() == timeline_id) | 142 if (timeline->id() == timeline_id) |
35 return timeline.get(); | 143 return timeline.get(); |
36 return nullptr; | 144 return nullptr; |
37 } | 145 } |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 return player ? player->layer_animation_controller()->has_any_animation() | 429 return player ? player->layer_animation_controller()->has_any_animation() |
322 : false; | 430 : false; |
323 } | 431 } |
324 | 432 |
325 bool AnimationHost::HasActiveAnimation(int layer_id) const { | 433 bool AnimationHost::HasActiveAnimation(int layer_id) const { |
326 AnimationPlayer* player = GetPlayerForLayerId(layer_id); | 434 AnimationPlayer* player = GetPlayerForLayerId(layer_id); |
327 return player ? player->layer_animation_controller()->HasActiveAnimation() | 435 return player ? player->layer_animation_controller()->HasActiveAnimation() |
328 : false; | 436 : false; |
329 } | 437 } |
330 | 438 |
| 439 void AnimationHost::ImplOnlyScrollAnimationCreate( |
| 440 int layer_id, |
| 441 const gfx::ScrollOffset& target_offset, |
| 442 const gfx::ScrollOffset& current_offset) { |
| 443 DCHECK(scroll_offset_animations_); |
| 444 scroll_offset_animations_->ScrollAnimationCreate(layer_id, target_offset, |
| 445 current_offset); |
| 446 } |
| 447 |
| 448 bool AnimationHost::ImplOnlyScrollAnimationUpdateTarget( |
| 449 int layer_id, |
| 450 const gfx::Vector2dF& scroll_delta, |
| 451 const gfx::ScrollOffset& max_scroll_offset, |
| 452 base::TimeTicks frame_monotonic_time) { |
| 453 DCHECK(scroll_offset_animations_); |
| 454 return scroll_offset_animations_->ScrollAnimationUpdateTarget( |
| 455 layer_id, scroll_delta, max_scroll_offset, frame_monotonic_time); |
| 456 } |
| 457 |
331 } // namespace cc | 458 } // namespace cc |
OLD | NEW |