| 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 "config.h" | 5 #include "config.h" |
| 6 #include "platform/scroll/ProgrammaticScrollAnimator.h" | 6 #include "platform/scroll/ProgrammaticScrollAnimator.h" |
| 7 | 7 |
| 8 #include "platform/RuntimeEnabledFeatures.h" | |
| 9 #include "platform/geometry/IntPoint.h" | 8 #include "platform/geometry/IntPoint.h" |
| 10 #include "platform/graphics/GraphicsLayer.h" | 9 #include "platform/graphics/GraphicsLayer.h" |
| 11 #include "platform/scroll/ScrollableArea.h" | 10 #include "platform/scroll/ScrollableArea.h" |
| 12 #include "public/platform/Platform.h" | 11 #include "public/platform/Platform.h" |
| 13 #include "public/platform/WebCompositorAnimation.h" | 12 #include "public/platform/WebCompositorAnimation.h" |
| 14 #include "public/platform/WebCompositorAnimationPlayer.h" | |
| 15 #include "public/platform/WebCompositorAnimationTimeline.h" | |
| 16 #include "public/platform/WebCompositorSupport.h" | 13 #include "public/platform/WebCompositorSupport.h" |
| 17 #include "public/platform/WebScrollOffsetAnimationCurve.h" | 14 #include "public/platform/WebScrollOffsetAnimationCurve.h" |
| 18 | 15 |
| 19 namespace blink { | 16 namespace blink { |
| 20 | 17 |
| 21 PassOwnPtr<ProgrammaticScrollAnimator> ProgrammaticScrollAnimator::create(Scroll
ableArea* scrollableArea) | 18 PassOwnPtr<ProgrammaticScrollAnimator> ProgrammaticScrollAnimator::create(Scroll
ableArea* scrollableArea) |
| 22 { | 19 { |
| 23 return adoptPtr(new ProgrammaticScrollAnimator(scrollableArea)); | 20 return adoptPtr(new ProgrammaticScrollAnimator(scrollableArea)); |
| 24 } | 21 } |
| 25 | 22 |
| 26 ProgrammaticScrollAnimator::ProgrammaticScrollAnimator(ScrollableArea* scrollabl
eArea) | 23 ProgrammaticScrollAnimator::ProgrammaticScrollAnimator(ScrollableArea* scrollabl
eArea) |
| 27 : m_compositorAnimationAttachedToLayerId(0) | 24 : m_scrollableArea(scrollableArea) |
| 28 , m_scrollableArea(scrollableArea) | |
| 29 , m_startTime(0.0) | 25 , m_startTime(0.0) |
| 30 , m_runState(RunState::Idle) | 26 , m_runState(RunState::Idle) |
| 31 , m_compositorAnimationId(0) | 27 , m_compositorAnimationId(0) |
| 32 , m_compositorAnimationGroupId(0) | 28 , m_compositorAnimationGroupId(0) |
| 33 { | 29 { |
| 34 if (RuntimeEnabledFeatures::compositorAnimationTimelinesEnabled()) { | |
| 35 ASSERT(Platform::current()->compositorSupport()); | |
| 36 m_compositorPlayer = adoptPtr(Platform::current()->compositorSupport()->
createAnimationPlayer()); | |
| 37 ASSERT(m_compositorPlayer); | |
| 38 m_compositorPlayer->setAnimationDelegate(this); | |
| 39 } | |
| 40 } | 30 } |
| 41 | 31 |
| 42 ProgrammaticScrollAnimator::~ProgrammaticScrollAnimator() | 32 ProgrammaticScrollAnimator::~ProgrammaticScrollAnimator() |
| 43 { | 33 { |
| 44 if (m_compositorPlayer) { | |
| 45 m_compositorPlayer->setAnimationDelegate(nullptr); | |
| 46 m_compositorPlayer.clear(); | |
| 47 } | |
| 48 } | 34 } |
| 49 | 35 |
| 50 void ProgrammaticScrollAnimator::resetAnimationState() | 36 void ProgrammaticScrollAnimator::resetAnimationState() |
| 51 { | 37 { |
| 52 m_animationCurve.clear(); | 38 m_animationCurve.clear(); |
| 53 m_startTime = 0.0; | 39 m_startTime = 0.0; |
| 54 m_runState = RunState::Idle; | 40 m_runState = RunState::Idle; |
| 55 m_compositorAnimationId = 0; | 41 m_compositorAnimationId = 0; |
| 56 m_compositorAnimationGroupId = 0; | 42 m_compositorAnimationGroupId = 0; |
| 57 } | 43 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 } | 129 } |
| 144 | 130 |
| 145 void ProgrammaticScrollAnimator::updateCompositorAnimations() | 131 void ProgrammaticScrollAnimator::updateCompositorAnimations() |
| 146 { | 132 { |
| 147 if (m_compositorAnimationId && m_runState != RunState::RunningOnCompositor)
{ | 133 if (m_compositorAnimationId && m_runState != RunState::RunningOnCompositor)
{ |
| 148 // If the current run state is WaitingToSendToCompositor but we have a | 134 // If the current run state is WaitingToSendToCompositor but we have a |
| 149 // non-zero compositor animation id, there's a currently running | 135 // non-zero compositor animation id, there's a currently running |
| 150 // compositor animation that needs to be removed here before the new | 136 // compositor animation that needs to be removed here before the new |
| 151 // animation is added below. | 137 // animation is added below. |
| 152 ASSERT(m_runState == RunState::WaitingToCancelOnCompositor || m_runState
== RunState::WaitingToSendToCompositor); | 138 ASSERT(m_runState == RunState::WaitingToCancelOnCompositor || m_runState
== RunState::WaitingToSendToCompositor); |
| 153 | 139 if (GraphicsLayer* layer = m_scrollableArea->layerForScrolling()) |
| 154 if (m_compositorPlayer) { | 140 layer->removeAnimation(m_compositorAnimationId); |
| 155 if (m_compositorPlayer->isLayerAttached()) | |
| 156 m_compositorPlayer->removeAnimation(m_compositorAnimationId); | |
| 157 } else { | |
| 158 if (GraphicsLayer* layer = m_scrollableArea->layerForScrolling()) | |
| 159 layer->removeAnimation(m_compositorAnimationId); | |
| 160 } | |
| 161 | |
| 162 m_compositorAnimationId = 0; | 141 m_compositorAnimationId = 0; |
| 163 m_compositorAnimationGroupId = 0; | 142 m_compositorAnimationGroupId = 0; |
| 164 if (m_runState == RunState::WaitingToCancelOnCompositor) { | 143 if (m_runState == RunState::WaitingToCancelOnCompositor) { |
| 165 resetAnimationState(); | 144 resetAnimationState(); |
| 166 return; | 145 return; |
| 167 } | 146 } |
| 168 } | 147 } |
| 169 | 148 |
| 170 if (m_runState == RunState::WaitingToSendToCompositor) { | 149 if (m_runState == RunState::WaitingToSendToCompositor) { |
| 171 bool sentToCompositor = false; | 150 bool sentToCompositor = false; |
| 172 | 151 |
| 173 if (GraphicsLayer* layer = m_scrollableArea->layerForScrolling()) { | 152 if (GraphicsLayer* layer = m_scrollableArea->layerForScrolling()) { |
| 174 if (!layer->platformLayer()->shouldScrollOnMainThread()) { | 153 if (!layer->platformLayer()->shouldScrollOnMainThread()) { |
| 175 OwnPtr<WebCompositorAnimation> animation = adoptPtr(Platform::cu
rrent()->compositorSupport()->createAnimation(*m_animationCurve, WebCompositorAn
imation::TargetPropertyScrollOffset)); | 154 OwnPtr<WebCompositorAnimation> animation = adoptPtr(Platform::cu
rrent()->compositorSupport()->createAnimation(*m_animationCurve, WebCompositorAn
imation::TargetPropertyScrollOffset)); |
| 176 | 155 |
| 177 int animationId = animation->id(); | 156 int animationId = animation->id(); |
| 178 int animationGroupId = animation->group(); | 157 int animationGroupId = animation->group(); |
| 179 | 158 if (m_scrollableArea->layerForScrolling()->addAnimation(animatio
n.release())) { |
| 180 bool animationAdded = false; | |
| 181 if (m_compositorPlayer) { | |
| 182 if (m_compositorPlayer->isLayerAttached()) { | |
| 183 m_compositorPlayer->addAnimation(animation.leakPtr()); | |
| 184 animationAdded = true; | |
| 185 } | |
| 186 } else { | |
| 187 animationAdded = m_scrollableArea->layerForScrolling()->addA
nimation(animation.release()); | |
| 188 } | |
| 189 | |
| 190 if (animationAdded) { | |
| 191 sentToCompositor = true; | 159 sentToCompositor = true; |
| 192 m_runState = RunState::RunningOnCompositor; | 160 m_runState = RunState::RunningOnCompositor; |
| 193 m_compositorAnimationId = animationId; | 161 m_compositorAnimationId = animationId; |
| 194 m_compositorAnimationGroupId = animationGroupId; | 162 m_compositorAnimationGroupId = animationGroupId; |
| 195 } | 163 } |
| 196 } | 164 } |
| 197 } | 165 } |
| 198 | 166 |
| 199 if (!sentToCompositor) { | 167 if (!sentToCompositor) { |
| 200 m_runState = RunState::RunningOnMainThread; | 168 m_runState = RunState::RunningOnMainThread; |
| 201 if (!m_scrollableArea->scheduleAnimation()) { | 169 if (!m_scrollableArea->scheduleAnimation()) { |
| 202 notifyPositionChanged(IntPoint(m_targetOffset.x(), m_targetOffse
t.y())); | 170 notifyPositionChanged(IntPoint(m_targetOffset.x(), m_targetOffse
t.y())); |
| 203 resetAnimationState(); | 171 resetAnimationState(); |
| 204 } | 172 } |
| 205 } | 173 } |
| 206 } | 174 } |
| 207 } | 175 } |
| 208 | 176 |
| 209 void ProgrammaticScrollAnimator::reattachCompositorPlayerIfNeeded(WebCompositorA
nimationTimeline* timeline) | 177 void ProgrammaticScrollAnimator::layerForCompositedScrollingDidChange() |
| 210 { | 178 { |
| 211 int compositorAnimationAttachedToLayerId = 0; | |
| 212 if (m_scrollableArea->layerForScrolling()) | |
| 213 compositorAnimationAttachedToLayerId = m_scrollableArea->layerForScrolli
ng()->platformLayer()->id(); | |
| 214 | |
| 215 if (compositorAnimationAttachedToLayerId != m_compositorAnimationAttachedToL
ayerId) { | |
| 216 if (m_compositorPlayer && timeline) { | |
| 217 // Detach from old layer (if any). | |
| 218 if (m_compositorAnimationAttachedToLayerId) { | |
| 219 ASSERT(m_compositorPlayer->isLayerAttached()); | |
| 220 m_compositorPlayer->detachLayer(); | |
| 221 timeline->playerDestroyed(*this); | |
| 222 } | |
| 223 // Attach to new layer (if any). | |
| 224 if (compositorAnimationAttachedToLayerId) { | |
| 225 ASSERT(m_scrollableArea->layerForScrolling()); | |
| 226 timeline->playerAttached(*this); | |
| 227 m_compositorPlayer->attachLayer(m_scrollableArea->layerForScroll
ing()->platformLayer()); | |
| 228 } | |
| 229 m_compositorAnimationAttachedToLayerId = compositorAnimationAttached
ToLayerId; | |
| 230 } | |
| 231 } | |
| 232 } | |
| 233 | |
| 234 void ProgrammaticScrollAnimator::layerForCompositedScrollingDidChange(WebComposi
torAnimationTimeline* timeline) | |
| 235 { | |
| 236 reattachCompositorPlayerIfNeeded(timeline); | |
| 237 | |
| 238 // If the composited scrolling layer is lost during a composited animation, | 179 // If the composited scrolling layer is lost during a composited animation, |
| 239 // continue the animation on the main thread. | 180 // continue the animation on the main thread. |
| 240 if (m_runState == RunState::RunningOnCompositor && !m_scrollableArea->layerF
orScrolling()) { | 181 if (m_runState == RunState::RunningOnCompositor && !m_scrollableArea->layerF
orScrolling()) { |
| 241 m_runState = RunState::RunningOnMainThread; | 182 m_runState = RunState::RunningOnMainThread; |
| 242 m_compositorAnimationId = 0; | 183 m_compositorAnimationId = 0; |
| 243 m_compositorAnimationGroupId = 0; | 184 m_compositorAnimationGroupId = 0; |
| 244 m_animationCurve->setInitialValue(FloatPoint(m_scrollableArea->scrollPos
ition())); | 185 m_animationCurve->setInitialValue(FloatPoint(m_scrollableArea->scrollPos
ition())); |
| 245 m_scrollableArea->registerForAnimation(); | 186 m_scrollableArea->registerForAnimation(); |
| 246 if (!m_scrollableArea->scheduleAnimation()) { | 187 if (!m_scrollableArea->scheduleAnimation()) { |
| 247 resetAnimationState(); | 188 resetAnimationState(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 264 ASSERT_NOT_REACHED(); | 205 ASSERT_NOT_REACHED(); |
| 265 break; | 206 break; |
| 266 case RunState::WaitingToSendToCompositor: | 207 case RunState::WaitingToSendToCompositor: |
| 267 break; | 208 break; |
| 268 case RunState::RunningOnCompositor: | 209 case RunState::RunningOnCompositor: |
| 269 case RunState::WaitingToCancelOnCompositor: | 210 case RunState::WaitingToCancelOnCompositor: |
| 270 resetAnimationState(); | 211 resetAnimationState(); |
| 271 } | 212 } |
| 272 } | 213 } |
| 273 | 214 |
| 274 void ProgrammaticScrollAnimator::notifyAnimationStarted(double monotonicTime, in
t group) | |
| 275 { | |
| 276 } | |
| 277 | |
| 278 void ProgrammaticScrollAnimator::notifyAnimationFinished(double monotonicTime, i
nt group) | |
| 279 { | |
| 280 notifyCompositorAnimationFinished(group); | |
| 281 } | |
| 282 | |
| 283 WebCompositorAnimationPlayer* ProgrammaticScrollAnimator::compositorPlayer() con
st | |
| 284 { | |
| 285 return m_compositorPlayer.get(); | |
| 286 } | |
| 287 | |
| 288 } // namespace blink | 215 } // namespace blink |
| OLD | NEW |