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