1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/*
* Copyright (C) 2016 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS 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 APPLE INC. OR ITS 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 "ScrollAnimationKinetic.h"
#include "PlatformWheelEvent.h"
#if USE(GLIB_EVENT_LOOP)
#include <wtf/glib/RunLoopSourcePriority.h>
#endif
/*
* PerAxisData is a port of GtkKineticScrolling as of GTK+ 3.20,
* mimicking its API and its behavior.
*
* All our curves are second degree linear differential equations, and
* so they can always be written as linear combinations of 2 base
* solutions. coef1 and coef2 are the coefficients to these two base
* solutions, and are computed from the initial position and velocity.
*
* In the case of simple deceleration, the differential equation is
*
* y'' = -my'
*
* With m the resistence factor. For this we use the following 2
* base solutions:
*
* f1(x) = 1
* f2(x) = exp(-mx)
*
* In the case of overshoot, the differential equation is
*
* y'' = -my' - ky
*
* With m the resistance, and k the spring stiffness constant. We let
* k = m^2 / 4, so that the system is critically damped (ie, returns to its
* equilibrium position as quickly as possible, without oscillating), and offset
* the whole thing, such that the equilibrium position is at 0. This gives the
* base solutions
*
* f1(x) = exp(-mx / 2)
* f2(x) = t exp(-mx / 2)
*/
static constexpr double decelFriction = 4;
static constexpr double frameRate = 60;
static constexpr double velocityAccumulationFloor = 0.33;
static constexpr double velocityAccumulationCeil = 1.0;
static constexpr double velocityAccumulationMax = 6.0;
static constexpr Seconds tickTime = 1_s / frameRate;
static constexpr Seconds minimumTimerInterval { 1_ms };
static constexpr Seconds scrollCaptureThreshold { 150_ms };
namespace WebCore {
ScrollAnimationKinetic::PerAxisData::PerAxisData(double lower, double upper, double initialPosition, double initialVelocity)
: m_lower(lower)
, m_upper(upper)
, m_coef1(initialVelocity / decelFriction + initialPosition)
, m_coef2(-initialVelocity / decelFriction)
, m_position(clampTo(initialPosition, lower, upper))
, m_velocity(initialPosition < lower || initialPosition > upper ? 0 : initialVelocity)
{
}
bool ScrollAnimationKinetic::PerAxisData::animateScroll(Seconds timeDelta)
{
auto lastPosition = m_position;
auto lastTime = m_elapsedTime;
m_elapsedTime += timeDelta;
double exponentialPart = exp(-decelFriction * m_elapsedTime.value());
m_position = m_coef1 + m_coef2 * exponentialPart;
m_velocity = -decelFriction * m_coef2 * exponentialPart;
if (m_position < m_lower) {
m_velocity = m_lower - m_position;
m_position = m_lower;
} else if (m_position > m_upper) {
m_velocity = m_upper - m_position;
m_position = m_upper;
}
if (fabs(m_velocity) < 1 || (lastTime && fabs(m_position - lastPosition) < 1)) {
m_position = round(m_position);
m_velocity = 0;
}
return m_velocity;
}
ScrollAnimationKinetic::ScrollAnimationKinetic(ScrollExtentsCallback&& scrollExtentsFunction, NotifyPositionChangedCallback&& notifyPositionChangedFunction)
: m_scrollExtentsFunction(WTFMove(scrollExtentsFunction))
, m_notifyPositionChangedFunction(WTFMove(notifyPositionChangedFunction))
, m_animationTimer(RunLoop::current(), this, &ScrollAnimationKinetic::animationTimerFired)
{
#if USE(GLIB_EVENT_LOOP)
m_animationTimer.setPriority(WTF::RunLoopSourcePriority::DisplayRefreshMonitorTimer);
#endif
}
ScrollAnimationKinetic::~ScrollAnimationKinetic() = default;
void ScrollAnimationKinetic::stop()
{
m_animationTimer.stop();
}
bool ScrollAnimationKinetic::isActive() const
{
return m_animationTimer.isActive();
}
void ScrollAnimationKinetic::appendToScrollHistory(const PlatformWheelEvent& event)
{
m_scrollHistory.removeAllMatching([&event] (PlatformWheelEvent& otherEvent) -> bool {
return (event.timestamp() - otherEvent.timestamp()) > scrollCaptureThreshold;
});
m_scrollHistory.append(event);
}
void ScrollAnimationKinetic::clearScrollHistory()
{
m_scrollHistory.clear();
}
FloatPoint ScrollAnimationKinetic::computeVelocity()
{
if (m_scrollHistory.isEmpty())
return { };
auto first = m_scrollHistory[0].timestamp();
auto last = m_scrollHistory.rbegin()->timestamp();
if (last == first)
return { };
FloatPoint accumDelta;
for (const auto& scrollEvent : m_scrollHistory)
accumDelta += FloatPoint(scrollEvent.deltaX(), scrollEvent.deltaY());
m_scrollHistory.clear();
return FloatPoint(accumDelta.x() * -1 / (last - first).value(), accumDelta.y() * -1 / (last - first).value());
}
void ScrollAnimationKinetic::start(const FloatPoint& initialPosition, const FloatPoint& velocity, bool mayHScroll, bool mayVScroll)
{
stop();
if (!velocity.x() && !velocity.y()) {
m_position = initialPosition;
m_horizontalData = std::nullopt;
m_verticalData = std::nullopt;
return;
}
auto delta = deltaToNextFrame();
auto extents = m_scrollExtentsFunction();
auto accumulateVelocity = [&](double velocity, std::optional<PerAxisData> axisData) -> double {
if (axisData && axisData.value().animateScroll(delta)) {
double lastVelocity = axisData.value().velocity();
if ((std::signbit(lastVelocity) == std::signbit(velocity))
&& (std::abs(velocity) >= std::abs(lastVelocity * velocityAccumulationFloor))) {
double minVelocity = lastVelocity * velocityAccumulationFloor;
double maxVelocity = lastVelocity * velocityAccumulationCeil;
double accumulationMultiplier = (velocity - minVelocity) / (maxVelocity - minVelocity);
velocity += lastVelocity * std::min(accumulationMultiplier, velocityAccumulationMax);
}
}
return velocity;
};
if (mayHScroll) {
m_horizontalData = PerAxisData(extents.minimumScrollPosition.x(),
extents.maximumScrollPosition.x(),
initialPosition.x(), accumulateVelocity(velocity.x(), m_horizontalData));
} else
m_horizontalData = std::nullopt;
if (mayVScroll) {
m_verticalData = PerAxisData(extents.minimumScrollPosition.y(),
extents.maximumScrollPosition.y(),
initialPosition.y(), accumulateVelocity(velocity.y(), m_verticalData));
} else
m_verticalData = std::nullopt;
m_position = initialPosition;
m_startTime = MonotonicTime::now() - tickTime / 2.;
animationTimerFired();
}
void ScrollAnimationKinetic::animationTimerFired()
{
auto delta = deltaToNextFrame();
if (m_horizontalData && !m_horizontalData.value().animateScroll(delta))
m_horizontalData = std::nullopt;
if (m_verticalData && !m_verticalData.value().animateScroll(delta))
m_verticalData = std::nullopt;
// If one of the axes didn't finish its animation we must continue it.
if (m_horizontalData || m_verticalData)
m_animationTimer.startOneShot(std::max(minimumTimerInterval, delta));
double x = m_horizontalData ? m_horizontalData.value().position() : m_position.x();
double y = m_verticalData ? m_verticalData.value().position() : m_position.y();
m_position = FloatPoint(x, y);
m_notifyPositionChangedFunction(FloatPoint(m_position));
}
Seconds ScrollAnimationKinetic::deltaToNextFrame()
{
MonotonicTime currentTime = MonotonicTime::now();
return 1_s * ceil((currentTime - m_startTime).value() * frameRate) / frameRate - (currentTime - m_startTime);
}
} // namespace WebCore
|