| 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 "platform/animation/CompositorAnimation.h" | 7 #include "platform/animation/CompositorAnimation.h" |
| 8 #include "platform/animation/CompositorScrollOffsetAnimationCurve.h" | 8 #include "platform/animation/CompositorScrollOffsetAnimationCurve.h" |
| 9 #include "platform/geometry/IntPoint.h" | 9 #include "platform/geometry/IntSize.h" |
| 10 #include "platform/graphics/GraphicsLayer.h" | 10 #include "platform/graphics/GraphicsLayer.h" |
| 11 #include "platform/scroll/ScrollableArea.h" | 11 #include "platform/scroll/ScrollableArea.h" |
| 12 #include "public/platform/Platform.h" | 12 #include "public/platform/Platform.h" |
| 13 #include "public/platform/WebCompositorSupport.h" | 13 #include "public/platform/WebCompositorSupport.h" |
| 14 #include "wtf/PtrUtil.h" | 14 #include "wtf/PtrUtil.h" |
| 15 #include <memory> | 15 #include <memory> |
| 16 | 16 |
| 17 namespace blink { | 17 namespace blink { |
| 18 | 18 |
| 19 ProgrammaticScrollAnimator::ProgrammaticScrollAnimator( | 19 ProgrammaticScrollAnimator::ProgrammaticScrollAnimator( |
| 20 ScrollableArea* scrollableArea) | 20 ScrollableArea* scrollableArea) |
| 21 : m_scrollableArea(scrollableArea), m_startTime(0.0) {} | 21 : m_scrollableArea(scrollableArea), m_startTime(0.0) {} |
| 22 | 22 |
| 23 ProgrammaticScrollAnimator::~ProgrammaticScrollAnimator() {} | 23 ProgrammaticScrollAnimator::~ProgrammaticScrollAnimator() {} |
| 24 | 24 |
| 25 void ProgrammaticScrollAnimator::resetAnimationState() { | 25 void ProgrammaticScrollAnimator::resetAnimationState() { |
| 26 ScrollAnimatorCompositorCoordinator::resetAnimationState(); | 26 ScrollAnimatorCompositorCoordinator::resetAnimationState(); |
| 27 m_animationCurve.reset(); | 27 m_animationCurve.reset(); |
| 28 m_startTime = 0.0; | 28 m_startTime = 0.0; |
| 29 } | 29 } |
| 30 | 30 |
| 31 void ProgrammaticScrollAnimator::notifyPositionChanged( | 31 void ProgrammaticScrollAnimator::notifyOffsetChanged( |
| 32 const DoublePoint& offset) { | 32 const ScrollOffset& offset) { |
| 33 scrollPositionChanged(offset, ProgrammaticScroll); | 33 scrollOffsetChanged(offset, ProgrammaticScroll); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void ProgrammaticScrollAnimator::scrollToOffsetWithoutAnimation( | 36 void ProgrammaticScrollAnimator::scrollToOffsetWithoutAnimation( |
| 37 const FloatPoint& offset) { | 37 const ScrollOffset& offset) { |
| 38 cancelAnimation(); | 38 cancelAnimation(); |
| 39 notifyPositionChanged(offset); | 39 notifyOffsetChanged(offset); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void ProgrammaticScrollAnimator::animateToOffset(FloatPoint offset) { | 42 void ProgrammaticScrollAnimator::animateToOffset(const ScrollOffset& offset) { |
| 43 if (m_runState == RunState::PostAnimationCleanup) | 43 if (m_runState == RunState::PostAnimationCleanup) |
| 44 resetAnimationState(); | 44 resetAnimationState(); |
| 45 | 45 |
| 46 m_startTime = 0.0; | 46 m_startTime = 0.0; |
| 47 m_targetOffset = offset; | 47 m_targetOffset = offset; |
| 48 m_animationCurve = CompositorScrollOffsetAnimationCurve::create( | 48 m_animationCurve = CompositorScrollOffsetAnimationCurve::create( |
| 49 compositorOffsetFromBlinkOffset(m_targetOffset), | 49 compositorOffsetFromBlinkOffset(m_targetOffset), |
| 50 CompositorScrollOffsetAnimationCurve::ScrollDurationDeltaBased); | 50 CompositorScrollOffsetAnimationCurve::ScrollDurationDeltaBased); |
| 51 | 51 |
| 52 m_scrollableArea->registerForAnimation(); | 52 m_scrollableArea->registerForAnimation(); |
| 53 if (!m_scrollableArea->scheduleAnimation()) { | 53 if (!m_scrollableArea->scheduleAnimation()) { |
| 54 resetAnimationState(); | 54 resetAnimationState(); |
| 55 notifyPositionChanged(IntPoint(offset.x(), offset.y())); | 55 notifyOffsetChanged(offset); |
| 56 } | 56 } |
| 57 m_runState = RunState::WaitingToSendToCompositor; | 57 m_runState = RunState::WaitingToSendToCompositor; |
| 58 } | 58 } |
| 59 | 59 |
| 60 void ProgrammaticScrollAnimator::cancelAnimation() { | 60 void ProgrammaticScrollAnimator::cancelAnimation() { |
| 61 ASSERT(m_runState != RunState::RunningOnCompositorButNeedsUpdate); | 61 ASSERT(m_runState != RunState::RunningOnCompositorButNeedsUpdate); |
| 62 ScrollAnimatorCompositorCoordinator::cancelAnimation(); | 62 ScrollAnimatorCompositorCoordinator::cancelAnimation(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void ProgrammaticScrollAnimator::tickAnimation(double monotonicTime) { | 65 void ProgrammaticScrollAnimator::tickAnimation(double monotonicTime) { |
| 66 if (m_runState != RunState::RunningOnMainThread) | 66 if (m_runState != RunState::RunningOnMainThread) |
| 67 return; | 67 return; |
| 68 | 68 |
| 69 if (!m_startTime) | 69 if (!m_startTime) |
| 70 m_startTime = monotonicTime; | 70 m_startTime = monotonicTime; |
| 71 double elapsedTime = monotonicTime - m_startTime; | 71 double elapsedTime = monotonicTime - m_startTime; |
| 72 bool isFinished = (elapsedTime > m_animationCurve->duration()); | 72 bool isFinished = (elapsedTime > m_animationCurve->duration()); |
| 73 FloatPoint offset = | 73 ScrollOffset offset = |
| 74 blinkOffsetFromCompositorOffset(m_animationCurve->getValue(elapsedTime)); | 74 blinkOffsetFromCompositorOffset(m_animationCurve->getValue(elapsedTime)); |
| 75 notifyPositionChanged(IntPoint(offset.x(), offset.y())); | 75 notifyOffsetChanged(offset); |
| 76 | 76 |
| 77 if (isFinished) { | 77 if (isFinished) { |
| 78 m_runState = RunState::PostAnimationCleanup; | 78 m_runState = RunState::PostAnimationCleanup; |
| 79 } else if (!m_scrollableArea->scheduleAnimation()) { | 79 } else if (!m_scrollableArea->scheduleAnimation()) { |
| 80 notifyPositionChanged(IntPoint(m_targetOffset.x(), m_targetOffset.y())); | 80 notifyOffsetChanged(offset); |
| 81 resetAnimationState(); | 81 resetAnimationState(); |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 void ProgrammaticScrollAnimator::updateCompositorAnimations() { | 85 void ProgrammaticScrollAnimator::updateCompositorAnimations() { |
| 86 if (m_runState == RunState::PostAnimationCleanup) { | 86 if (m_runState == RunState::PostAnimationCleanup) { |
| 87 // No special cleanup, simply reset animation state. We have this state | 87 // No special cleanup, simply reset animation state. We have this state |
| 88 // here because the state machine is shared with ScrollAnimator which | 88 // here because the state machine is shared with ScrollAnimator which |
| 89 // has to do some cleanup that requires the compositing state to be clean. | 89 // has to do some cleanup that requires the compositing state to be clean. |
| 90 return resetAnimationState(); | 90 return resetAnimationState(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 if (addAnimation(std::move(animation))) { | 126 if (addAnimation(std::move(animation))) { |
| 127 sentToCompositor = true; | 127 sentToCompositor = true; |
| 128 m_runState = RunState::RunningOnCompositor; | 128 m_runState = RunState::RunningOnCompositor; |
| 129 m_compositorAnimationId = animationId; | 129 m_compositorAnimationId = animationId; |
| 130 m_compositorAnimationGroupId = animationGroupId; | 130 m_compositorAnimationGroupId = animationGroupId; |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 | 133 |
| 134 if (!sentToCompositor) { | 134 if (!sentToCompositor) { |
| 135 m_runState = RunState::RunningOnMainThread; | 135 m_runState = RunState::RunningOnMainThread; |
| 136 m_animationCurve->setInitialValue(compositorOffsetFromBlinkOffset( | 136 m_animationCurve->setInitialValue( |
| 137 FloatPoint(m_scrollableArea->scrollPosition()))); | 137 compositorOffsetFromBlinkOffset(m_scrollableArea->scrollOffset())); |
| 138 if (!m_scrollableArea->scheduleAnimation()) { | 138 if (!m_scrollableArea->scheduleAnimation()) { |
| 139 notifyPositionChanged(IntPoint(m_targetOffset.x(), m_targetOffset.y())); | 139 notifyOffsetChanged(m_targetOffset); |
| 140 resetAnimationState(); | 140 resetAnimationState(); |
| 141 } | 141 } |
| 142 } | 142 } |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 void ProgrammaticScrollAnimator::layerForCompositedScrollingDidChange( | 146 void ProgrammaticScrollAnimator::layerForCompositedScrollingDidChange( |
| 147 CompositorAnimationTimeline* timeline) { | 147 CompositorAnimationTimeline* timeline) { |
| 148 reattachCompositorPlayerIfNeeded(timeline); | 148 reattachCompositorPlayerIfNeeded(timeline); |
| 149 | 149 |
| 150 // If the composited scrolling layer is lost during a composited animation, | 150 // If the composited scrolling layer is lost during a composited animation, |
| 151 // continue the animation on the main thread. | 151 // continue the animation on the main thread. |
| 152 if (m_runState == RunState::RunningOnCompositor && | 152 if (m_runState == RunState::RunningOnCompositor && |
| 153 !m_scrollableArea->layerForScrolling()) { | 153 !m_scrollableArea->layerForScrolling()) { |
| 154 m_runState = RunState::RunningOnMainThread; | 154 m_runState = RunState::RunningOnMainThread; |
| 155 m_compositorAnimationId = 0; | 155 m_compositorAnimationId = 0; |
| 156 m_compositorAnimationGroupId = 0; | 156 m_compositorAnimationGroupId = 0; |
| 157 m_animationCurve->setInitialValue(compositorOffsetFromBlinkOffset( | 157 m_animationCurve->setInitialValue( |
| 158 FloatPoint(m_scrollableArea->scrollPosition()))); | 158 compositorOffsetFromBlinkOffset(m_scrollableArea->scrollOffset())); |
| 159 m_scrollableArea->registerForAnimation(); | 159 m_scrollableArea->registerForAnimation(); |
| 160 if (!m_scrollableArea->scheduleAnimation()) { | 160 if (!m_scrollableArea->scheduleAnimation()) { |
| 161 resetAnimationState(); | 161 resetAnimationState(); |
| 162 notifyPositionChanged(IntPoint(m_targetOffset.x(), m_targetOffset.y())); | 162 notifyOffsetChanged(m_targetOffset); |
| 163 } | 163 } |
| 164 } | 164 } |
| 165 } | 165 } |
| 166 | 166 |
| 167 void ProgrammaticScrollAnimator::notifyCompositorAnimationFinished( | 167 void ProgrammaticScrollAnimator::notifyCompositorAnimationFinished( |
| 168 int groupId) { | 168 int groupId) { |
| 169 ASSERT(m_runState != RunState::RunningOnCompositorButNeedsUpdate); | 169 ASSERT(m_runState != RunState::RunningOnCompositorButNeedsUpdate); |
| 170 ScrollAnimatorCompositorCoordinator::compositorAnimationFinished(groupId); | 170 ScrollAnimatorCompositorCoordinator::compositorAnimationFinished(groupId); |
| 171 } | 171 } |
| 172 | 172 |
| 173 DEFINE_TRACE(ProgrammaticScrollAnimator) { | 173 DEFINE_TRACE(ProgrammaticScrollAnimator) { |
| 174 visitor->trace(m_scrollableArea); | 174 visitor->trace(m_scrollableArea); |
| 175 ScrollAnimatorCompositorCoordinator::trace(visitor); | 175 ScrollAnimatorCompositorCoordinator::trace(visitor); |
| 176 } | 176 } |
| 177 | 177 |
| 178 } // namespace blink | 178 } // namespace blink |
| OLD | NEW |