Chromium Code Reviews| Index: Source/platform/scroll/ProgrammaticScrollAnimator.cpp |
| diff --git a/Source/platform/scroll/ProgrammaticScrollAnimator.cpp b/Source/platform/scroll/ProgrammaticScrollAnimator.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dfc4a43df032f5d6f01e6b0848e190fb3756073c |
| --- /dev/null |
| +++ b/Source/platform/scroll/ProgrammaticScrollAnimator.cpp |
| @@ -0,0 +1,138 @@ |
| +/* |
| + * Copyright (C) 2014 Google Inc. All rights reserved. |
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions are |
| + * met: |
| + * |
| + * * Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * * Redistributions in binary form must reproduce the above |
| + * copyright notice, this list of conditions and the following disclaimer |
| + * in the documentation and/or other materials provided with the |
| + * distribution. |
| + * * Neither the name of Google Inc. nor the names of its |
| + * contributors may be used to endorse or promote products derived from |
| + * this software without specific prior written permission. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#include "config.h" |
| +#include "platform/scroll/ProgrammaticScrollAnimator.h" |
| + |
| +#include "platform/geometry/FloatPoint.h" |
| +#include "platform/geometry/IntPoint.h" |
| +#include "platform/graphics/GraphicsLayer.h" |
| +#include "platform/scroll/ScrollableArea.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/WebAnimation.h" |
| +#include "public/platform/WebAnimationCurve.h" |
| +#include "public/platform/WebCompositorSupport.h" |
| +#include "public/platform/WebScrollOffsetAnimationCurve.h" |
| + |
| +namespace WebCore { |
| + |
| +PassOwnPtr<ProgrammaticScrollAnimator> ProgrammaticScrollAnimator::create(ScrollableArea* scrollableArea) |
| +{ |
| + return adoptPtr(new ProgrammaticScrollAnimator(scrollableArea)); |
| +} |
| + |
| +ProgrammaticScrollAnimator::ProgrammaticScrollAnimator(ScrollableArea* scrollableArea) |
| + : m_scrollableArea(scrollableArea) |
| + , m_animationId(0) |
| + , m_startTime(0.0) |
| +{ |
| +} |
| + |
| +ProgrammaticScrollAnimator::~ProgrammaticScrollAnimator() |
| +{ |
| +} |
| + |
| +void ProgrammaticScrollAnimator::resetAnimationState() |
| +{ |
| + m_animationCurve.clear(); |
| + m_animationId = 0; |
| + m_startTime = 0.0; |
| + m_scrollableArea->deregisterForAnimation(); |
| +} |
| + |
| +void ProgrammaticScrollAnimator::animateToOffset(FloatPoint offset) |
| +{ |
| + m_startTime = 0.0; |
| + m_targetOffset = offset; |
| + OwnPtr<blink::WebScrollOffsetAnimationCurve> curve = adoptPtr(blink::Platform::current()->compositorSupport()->createScrollOffsetAnimationCurve(m_targetOffset, blink::WebAnimationCurve::TimingFunctionTypeEaseInOut)); |
| + if (m_scrollableArea->canUseCompositedScrollAnimations()) { |
| + OwnPtr<blink::WebAnimation> animation = adoptPtr(blink::Platform::current()->compositorSupport()->createAnimation(*curve, blink::WebAnimation::TargetPropertyScrollOffset)); |
| + m_animationId = animation->id(); |
| + m_scrollableArea->layerForScrolling()->addAnimation(animation.release()); |
| + return; |
| + } |
| + |
| + m_animationCurve = curve.release(); |
| + m_animationCurve->setInitialValue(FloatPoint(m_scrollableArea->scrollPosition())); |
|
Ian Vollick
2014/02/06 16:02:31
Will ::tickAnimation continue to be called through
|
| + if (!m_scrollableArea->registerForAnimation() || !m_scrollableArea->scheduleAnimation()) { |
| + resetAnimationState(); |
| + m_scrollableArea->setScrollOffsetFromProgrammaticAnimation(IntPoint(offset.x(), offset.y())); |
| + } |
| +} |
| + |
| +void ProgrammaticScrollAnimator::cancelAnimation() |
| +{ |
| + if (m_animationId && m_scrollableArea->layerForScrolling()) |
| + m_scrollableArea->layerForScrolling()->removeAnimation(m_animationId); |
| + |
| + resetAnimationState(); |
| +} |
| + |
| +void ProgrammaticScrollAnimator::tickAnimation(double monotonicTime) |
| +{ |
| + if (m_animationCurve) { |
| + if (!m_startTime) |
| + m_startTime = monotonicTime; |
| + double elapsedTime = monotonicTime - m_startTime; |
| + bool isFinished = (elapsedTime > m_animationCurve->duration()); |
| + FloatPoint offset = m_animationCurve->getValue(elapsedTime); |
| + m_scrollableArea->setScrollOffsetFromProgrammaticAnimation(IntPoint(offset.x(), offset.y())); |
| + |
| + if (isFinished) { |
| + resetAnimationState(); |
| + } else if (!m_scrollableArea->scheduleAnimation()) { |
| + m_scrollableArea->setScrollOffsetFromProgrammaticAnimation(IntPoint(m_targetOffset.x(), m_targetOffset.y())); |
| + resetAnimationState(); |
| + } |
| + } |
| +} |
| + |
| +void ProgrammaticScrollAnimator::notifyAnimationStarted(double monotonicTime) |
| +{ |
| + m_startTime = monotonicTime; |
| +} |
| + |
| +void ProgrammaticScrollAnimator::notifyAnimationFinished(double monotonicTime) |
| +{ |
| + resetAnimationState(); |
| +} |
| + |
| +void ProgrammaticScrollAnimator::canUseCompositedScrollAnimationsDidChange() |
| +{ |
| + // If we're running an animation on the compositor thread but composited |
| + // scrolling is no longer available, we need to take over the animation on |
| + // the main thread. |
| + if (m_animationId && !m_scrollableArea->canUseCompositedScrollAnimations()) { |
| + cancelAnimation(); |
| + animateToOffset(m_targetOffset); |
| + } |
| +} |
| + |
| +} // namespace WebCore |