OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "platform/scroll/ProgrammaticScrollAnimator.h" | 5 #include "platform/scroll/ProgrammaticScrollAnimator.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include "platform/animation/CompositorAnimation.h" | 8 #include "platform/animation/CompositorAnimation.h" |
9 #include "platform/animation/CompositorScrollOffsetAnimationCurve.h" | 9 #include "platform/animation/CompositorScrollOffsetAnimationCurve.h" |
10 #include "platform/geometry/IntSize.h" | 10 #include "platform/geometry/IntSize.h" |
11 #include "platform/graphics/GraphicsLayer.h" | 11 #include "platform/graphics/GraphicsLayer.h" |
12 #include "platform/scroll/ScrollableArea.h" | 12 #include "platform/scroll/ScrollableArea.h" |
13 #include "platform/scroll/SmoothScrollSequencer.h" | |
13 #include "platform/wtf/PtrUtil.h" | 14 #include "platform/wtf/PtrUtil.h" |
14 #include "public/platform/Platform.h" | 15 #include "public/platform/Platform.h" |
15 #include "public/platform/WebCompositorSupport.h" | 16 #include "public/platform/WebCompositorSupport.h" |
16 | 17 |
17 namespace blink { | 18 namespace blink { |
18 | 19 |
19 ProgrammaticScrollAnimator::ProgrammaticScrollAnimator( | 20 ProgrammaticScrollAnimator::ProgrammaticScrollAnimator( |
20 ScrollableArea* scrollable_area) | 21 ScrollableArea* scrollable_area) |
21 : scrollable_area_(scrollable_area), start_time_(0.0) {} | 22 : scrollable_area_(scrollable_area), start_time_(0.0) {} |
22 | 23 |
23 ProgrammaticScrollAnimator::~ProgrammaticScrollAnimator() {} | 24 ProgrammaticScrollAnimator::~ProgrammaticScrollAnimator() {} |
24 | 25 |
25 void ProgrammaticScrollAnimator::ResetAnimationState() { | 26 void ProgrammaticScrollAnimator::ResetAnimationState() { |
26 ScrollAnimatorCompositorCoordinator::ResetAnimationState(); | 27 ScrollAnimatorCompositorCoordinator::ResetAnimationState(); |
27 animation_curve_.reset(); | 28 animation_curve_.reset(); |
28 start_time_ = 0.0; | 29 start_time_ = 0.0; |
29 } | 30 } |
30 | 31 |
31 void ProgrammaticScrollAnimator::NotifyOffsetChanged( | 32 void ProgrammaticScrollAnimator::NotifyOffsetChanged( |
32 const ScrollOffset& offset) { | 33 const ScrollOffset& offset) { |
33 ScrollOffsetChanged(offset, kProgrammaticScroll); | 34 if (sequenced_for_smooth_scroll_) |
bokan
2017/06/07 17:56:00
nit: scroll_type = sequenced_for_smooth_scroll ? k
sunyunjia
2017/06/07 18:34:10
Done.
| |
35 ScrollOffsetChanged(offset, kSequencedSmoothScroll); | |
36 else | |
37 ScrollOffsetChanged(offset, kProgrammaticScroll); | |
34 } | 38 } |
35 | 39 |
36 void ProgrammaticScrollAnimator::ScrollToOffsetWithoutAnimation( | 40 void ProgrammaticScrollAnimator::ScrollToOffsetWithoutAnimation( |
37 const ScrollOffset& offset) { | 41 const ScrollOffset& offset) { |
38 CancelAnimation(); | 42 CancelAnimation(); |
39 NotifyOffsetChanged(offset); | 43 NotifyOffsetChanged(offset); |
40 } | 44 } |
41 | 45 |
42 void ProgrammaticScrollAnimator::AnimateToOffset(const ScrollOffset& offset) { | 46 void ProgrammaticScrollAnimator::AnimateToOffset( |
47 const ScrollOffset& offset, | |
48 bool sequenced_for_smooth_scroll) { | |
43 if (run_state_ == RunState::kPostAnimationCleanup) | 49 if (run_state_ == RunState::kPostAnimationCleanup) |
44 ResetAnimationState(); | 50 ResetAnimationState(); |
45 | 51 |
46 start_time_ = 0.0; | 52 start_time_ = 0.0; |
47 target_offset_ = offset; | 53 target_offset_ = offset; |
54 sequenced_for_smooth_scroll_ = sequenced_for_smooth_scroll; | |
48 animation_curve_ = CompositorScrollOffsetAnimationCurve::Create( | 55 animation_curve_ = CompositorScrollOffsetAnimationCurve::Create( |
49 CompositorOffsetFromBlinkOffset(target_offset_), | 56 CompositorOffsetFromBlinkOffset(target_offset_), |
50 CompositorScrollOffsetAnimationCurve::kScrollDurationDeltaBased); | 57 CompositorScrollOffsetAnimationCurve::kScrollDurationDeltaBased); |
51 | 58 |
52 scrollable_area_->RegisterForAnimation(); | 59 scrollable_area_->RegisterForAnimation(); |
53 if (!scrollable_area_->ScheduleAnimation()) { | 60 if (!scrollable_area_->ScheduleAnimation()) { |
54 ResetAnimationState(); | 61 ResetAnimationState(); |
55 NotifyOffsetChanged(offset); | 62 NotifyOffsetChanged(offset); |
56 } | 63 } |
57 run_state_ = RunState::kWaitingToSendToCompositor; | 64 run_state_ = RunState::kWaitingToSendToCompositor; |
(...skipping 11 matching lines...) Expand all Loading... | |
69 if (!start_time_) | 76 if (!start_time_) |
70 start_time_ = monotonic_time; | 77 start_time_ = monotonic_time; |
71 double elapsed_time = monotonic_time - start_time_; | 78 double elapsed_time = monotonic_time - start_time_; |
72 bool is_finished = (elapsed_time > animation_curve_->Duration()); | 79 bool is_finished = (elapsed_time > animation_curve_->Duration()); |
73 ScrollOffset offset = | 80 ScrollOffset offset = |
74 BlinkOffsetFromCompositorOffset(animation_curve_->GetValue(elapsed_time)); | 81 BlinkOffsetFromCompositorOffset(animation_curve_->GetValue(elapsed_time)); |
75 NotifyOffsetChanged(offset); | 82 NotifyOffsetChanged(offset); |
76 | 83 |
77 if (is_finished) { | 84 if (is_finished) { |
78 run_state_ = RunState::kPostAnimationCleanup; | 85 run_state_ = RunState::kPostAnimationCleanup; |
86 AnimationFinished(); | |
79 } else if (!scrollable_area_->ScheduleAnimation()) { | 87 } else if (!scrollable_area_->ScheduleAnimation()) { |
80 NotifyOffsetChanged(offset); | 88 NotifyOffsetChanged(offset); |
81 ResetAnimationState(); | 89 ResetAnimationState(); |
82 } | 90 } |
83 } | 91 } |
84 | 92 |
85 void ProgrammaticScrollAnimator::UpdateCompositorAnimations() { | 93 void ProgrammaticScrollAnimator::UpdateCompositorAnimations() { |
86 if (run_state_ == RunState::kPostAnimationCleanup) { | 94 if (run_state_ == RunState::kPostAnimationCleanup) { |
87 // No special cleanup, simply reset animation state. We have this state | 95 // No special cleanup, simply reset animation state. We have this state |
88 // here because the state machine is shared with ScrollAnimator which | 96 // here because the state machine is shared with ScrollAnimator which |
(...skipping 20 matching lines...) Expand all Loading... | |
109 } | 117 } |
110 } | 118 } |
111 | 119 |
112 if (run_state_ == RunState::kWaitingToSendToCompositor) { | 120 if (run_state_ == RunState::kWaitingToSendToCompositor) { |
113 if (!compositor_animation_attached_to_element_id_) | 121 if (!compositor_animation_attached_to_element_id_) |
114 ReattachCompositorPlayerIfNeeded( | 122 ReattachCompositorPlayerIfNeeded( |
115 GetScrollableArea()->GetCompositorAnimationTimeline()); | 123 GetScrollableArea()->GetCompositorAnimationTimeline()); |
116 | 124 |
117 bool sent_to_compositor = false; | 125 bool sent_to_compositor = false; |
118 | 126 |
119 if (!scrollable_area_->ShouldScrollOnMainThread()) { | 127 // TODO(sunyunjia): Sequenced Smooth Scroll should also be able to |
128 // scroll on the compositor thread. We should send the ScrollType | |
129 // information to the compositor thread. | |
bokan
2017/06/07 17:56:53
Oh, and please file a bug to make this work with c
sunyunjia
2017/06/07 18:34:10
Done.
| |
130 if (!scrollable_area_->ShouldScrollOnMainThread() && | |
131 !sequenced_for_smooth_scroll_) { | |
120 std::unique_ptr<CompositorAnimation> animation = | 132 std::unique_ptr<CompositorAnimation> animation = |
121 CompositorAnimation::Create( | 133 CompositorAnimation::Create( |
122 *animation_curve_, CompositorTargetProperty::SCROLL_OFFSET, 0, 0); | 134 *animation_curve_, CompositorTargetProperty::SCROLL_OFFSET, 0, 0); |
123 | 135 |
124 int animation_id = animation->Id(); | 136 int animation_id = animation->Id(); |
125 int animation_group_id = animation->Group(); | 137 int animation_group_id = animation->Group(); |
126 | 138 |
127 if (AddAnimation(std::move(animation))) { | 139 if (AddAnimation(std::move(animation))) { |
128 sent_to_compositor = true; | 140 sent_to_compositor = true; |
129 run_state_ = RunState::kRunningOnCompositor; | 141 run_state_ = RunState::kRunningOnCompositor; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 ResetAnimationState(); | 174 ResetAnimationState(); |
163 NotifyOffsetChanged(target_offset_); | 175 NotifyOffsetChanged(target_offset_); |
164 } | 176 } |
165 } | 177 } |
166 } | 178 } |
167 | 179 |
168 void ProgrammaticScrollAnimator::NotifyCompositorAnimationFinished( | 180 void ProgrammaticScrollAnimator::NotifyCompositorAnimationFinished( |
169 int group_id) { | 181 int group_id) { |
170 DCHECK_NE(run_state_, RunState::kRunningOnCompositorButNeedsUpdate); | 182 DCHECK_NE(run_state_, RunState::kRunningOnCompositorButNeedsUpdate); |
171 ScrollAnimatorCompositorCoordinator::CompositorAnimationFinished(group_id); | 183 ScrollAnimatorCompositorCoordinator::CompositorAnimationFinished(group_id); |
184 AnimationFinished(); | |
185 } | |
186 | |
187 void ProgrammaticScrollAnimator::AnimationFinished() { | |
188 if (sequenced_for_smooth_scroll_) { | |
189 sequenced_for_smooth_scroll_ = false; | |
190 if (SmoothScrollSequencer* sequencer = | |
191 GetScrollableArea()->GetSmoothScrollSequencer()) | |
192 sequencer->RunQueuedAnimations(); | |
193 } | |
172 } | 194 } |
173 | 195 |
174 DEFINE_TRACE(ProgrammaticScrollAnimator) { | 196 DEFINE_TRACE(ProgrammaticScrollAnimator) { |
175 visitor->Trace(scrollable_area_); | 197 visitor->Trace(scrollable_area_); |
176 ScrollAnimatorCompositorCoordinator::Trace(visitor); | 198 ScrollAnimatorCompositorCoordinator::Trace(visitor); |
177 } | 199 } |
178 | 200 |
179 } // namespace blink | 201 } // namespace blink |
OLD | NEW |