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" |
12 #include "cc/animation/element_animations.h" | 14 #include "cc/animation/element_animations.h" |
15 #include "cc/animation/scroll_offset_animation_curve.h" | |
16 #include "cc/animation/timing_function.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 = scroll_offset_animation_player_->element_animations() | |
77 ->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_->mutator_host_client()); | |
108 animation_host_->mutator_host_client()->ScrollOffsetAnimationFinished(); | |
109 } | |
110 | |
111 private: | |
112 AnimationHost* animation_host_; | |
113 scoped_refptr<AnimationTimeline> scroll_offset_timeline_; | |
114 scoped_refptr<AnimationPlayer> scroll_offset_animation_player_; | |
ajuma
2015/07/06 17:41:00
If I'm understanding, having a single such player
loyso (OOO)
2015/07/07 04:14:38
Done.
| |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(ScrollOffsetAnimations); | |
117 }; | |
118 | |
17 scoped_ptr<AnimationHost> AnimationHost::Create( | 119 scoped_ptr<AnimationHost> AnimationHost::Create( |
18 ThreadInstance thread_instance) { | 120 ThreadInstance thread_instance) { |
19 return make_scoped_ptr(new AnimationHost(thread_instance)); | 121 return make_scoped_ptr(new AnimationHost(thread_instance)); |
20 } | 122 } |
21 | 123 |
22 AnimationHost::AnimationHost(ThreadInstance thread_instance) | 124 AnimationHost::AnimationHost(ThreadInstance thread_instance) |
23 : animation_registrar_(AnimationRegistrar::Create()), | 125 : animation_registrar_(AnimationRegistrar::Create()), |
24 mutator_host_client_(), | 126 mutator_host_client_(), |
25 thread_instance_(thread_instance) { | 127 thread_instance_(thread_instance) { |
128 if (thread_instance_ == ThreadInstance::IMPL) | |
129 scroll_offset_animations_ = | |
130 make_scoped_ptr(new ScrollOffsetAnimations(this)); | |
26 } | 131 } |
27 | 132 |
28 AnimationHost::~AnimationHost() { | 133 AnimationHost::~AnimationHost() { |
134 scroll_offset_animations_ = nullptr; | |
135 | |
29 ClearTimelines(); | 136 ClearTimelines(); |
30 DCHECK(!mutator_host_client()); | 137 DCHECK(!mutator_host_client()); |
31 DCHECK(layer_to_element_animations_map_.empty()); | 138 DCHECK(layer_to_element_animations_map_.empty()); |
32 } | 139 } |
33 | 140 |
34 AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) const { | 141 AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) const { |
35 for (auto& timeline : timelines_) | 142 for (auto& timeline : timelines_) |
36 if (timeline->id() == timeline_id) | 143 if (timeline->id() == timeline_id) |
37 return timeline.get(); | 144 return timeline.get(); |
38 return nullptr; | 145 return nullptr; |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 bool AnimationHost::HasAnyAnimation(int layer_id) const { | 478 bool AnimationHost::HasAnyAnimation(int layer_id) const { |
372 LayerAnimationController* controller = GetControllerForLayerId(layer_id); | 479 LayerAnimationController* controller = GetControllerForLayerId(layer_id); |
373 return controller ? controller->has_any_animation() : false; | 480 return controller ? controller->has_any_animation() : false; |
374 } | 481 } |
375 | 482 |
376 bool AnimationHost::HasActiveAnimation(int layer_id) const { | 483 bool AnimationHost::HasActiveAnimation(int layer_id) const { |
377 LayerAnimationController* controller = GetControllerForLayerId(layer_id); | 484 LayerAnimationController* controller = GetControllerForLayerId(layer_id); |
378 return controller ? controller->HasActiveAnimation() : false; | 485 return controller ? controller->HasActiveAnimation() : false; |
379 } | 486 } |
380 | 487 |
488 void AnimationHost::ImplOnlyScrollAnimationCreate( | |
489 int layer_id, | |
490 const gfx::ScrollOffset& target_offset, | |
491 const gfx::ScrollOffset& current_offset) { | |
492 DCHECK(scroll_offset_animations_); | |
493 scroll_offset_animations_->ScrollAnimationCreate(layer_id, target_offset, | |
494 current_offset); | |
495 } | |
496 | |
497 bool AnimationHost::ImplOnlyScrollAnimationUpdateTarget( | |
498 int layer_id, | |
499 const gfx::Vector2dF& scroll_delta, | |
500 const gfx::ScrollOffset& max_scroll_offset, | |
501 base::TimeTicks frame_monotonic_time) { | |
502 DCHECK(scroll_offset_animations_); | |
503 return scroll_offset_animations_->ScrollAnimationUpdateTarget( | |
504 layer_id, scroll_delta, max_scroll_offset, frame_monotonic_time); | |
505 } | |
506 | |
381 } // namespace cc | 507 } // namespace cc |
OLD | NEW |