1/*
2 * Copyright (C) 2016 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ScrollAnimationKinetic.h"
28
29#include "ScrollableArea.h"
30
31/*
32 * PerAxisData is a port of GtkKineticScrolling as of GTK+ 3.20,
33 * mimicking its API and its behavior.
34 *
35 * All our curves are second degree linear differential equations, and
36 * so they can always be written as linear combinations of 2 base
37 * solutions. coef1 and coef2 are the coefficients to these two base
38 * solutions, and are computed from the initial position and velocity.
39 *
40 * In the case of simple deceleration, the differential equation is
41 *
42 * y'' = -my'
43 *
44 * With m the resistence factor. For this we use the following 2
45 * base solutions:
46 *
47 * f1(x) = 1
48 * f2(x) = exp(-mx)
49 *
50 * In the case of overshoot, the differential equation is
51 *
52 * y'' = -my' - ky
53 *
54 * With m the resistance, and k the spring stiffness constant. We let
55 * k = m^2 / 4, so that the system is critically damped (ie, returns to its
56 * equilibrium position as quickly as possible, without oscillating), and offset
57 * the whole thing, such that the equilibrium position is at 0. This gives the
58 * base solutions
59 *
60 * f1(x) = exp(-mx / 2)
61 * f2(x) = t exp(-mx / 2)
62 */
63
64static const double decelFriction = 4;
65static const double frameRate = 60;
66static const Seconds tickTime = 1_s / frameRate;
67static const Seconds minimumTimerInterval { 1_ms };
68
69namespace WebCore {
70
71ScrollAnimationKinetic::PerAxisData::PerAxisData(double lower, double upper, double initialPosition, double initialVelocity)
72 : m_lower(lower)
73 , m_upper(upper)
74 , m_coef1(initialVelocity / decelFriction + initialPosition)
75 , m_coef2(-initialVelocity / decelFriction)
76 , m_position(clampTo(initialPosition, lower, upper))
77 , m_velocity(initialPosition < lower || initialPosition > upper ? 0 : initialVelocity)
78{
79}
80
81bool ScrollAnimationKinetic::PerAxisData::animateScroll(Seconds timeDelta)
82{
83 auto lastPosition = m_position;
84 auto lastTime = m_elapsedTime;
85 m_elapsedTime += timeDelta;
86
87 double exponentialPart = exp(-decelFriction * m_elapsedTime.value());
88 m_position = m_coef1 + m_coef2 * exponentialPart;
89 m_velocity = -decelFriction * m_coef2 * exponentialPart;
90
91 if (m_position < m_lower) {
92 m_velocity = m_lower - m_position;
93 m_position = m_lower;
94 } else if (m_position > m_upper) {
95 m_velocity = m_upper - m_position;
96 m_position = m_upper;
97 }
98
99 if (fabs(m_velocity) < 1 || (lastTime && fabs(m_position - lastPosition) < 1)) {
100 m_position = round(m_position);
101 m_velocity = 0;
102 }
103
104 return m_velocity;
105}
106
107ScrollAnimationKinetic::ScrollAnimationKinetic(ScrollableArea& scrollableArea, std::function<void(FloatPoint&&)>&& notifyPositionChangedFunction)
108 : ScrollAnimation(scrollableArea)
109 , m_notifyPositionChangedFunction(WTFMove(notifyPositionChangedFunction))
110 , m_animationTimer(*this, &ScrollAnimationKinetic::animationTimerFired)
111{
112}
113
114ScrollAnimationKinetic::~ScrollAnimationKinetic() = default;
115
116void ScrollAnimationKinetic::stop()
117{
118 m_animationTimer.stop();
119 m_horizontalData = WTF::nullopt;
120 m_verticalData = WTF::nullopt;
121}
122
123void ScrollAnimationKinetic::start(const FloatPoint& initialPosition, const FloatPoint& velocity, bool mayHScroll, bool mayVScroll)
124{
125 stop();
126
127 m_position = initialPosition;
128
129 if (!velocity.x() && !velocity.y())
130 return;
131
132 if (mayHScroll) {
133 m_horizontalData = PerAxisData(m_scrollableArea.minimumScrollPosition().x(),
134 m_scrollableArea.maximumScrollPosition().x(),
135 initialPosition.x(), velocity.x());
136 }
137 if (mayVScroll) {
138 m_verticalData = PerAxisData(m_scrollableArea.minimumScrollPosition().y(),
139 m_scrollableArea.maximumScrollPosition().y(),
140 initialPosition.y(), velocity.y());
141 }
142
143 m_startTime = MonotonicTime::now() - tickTime / 2.;
144 animationTimerFired();
145}
146
147void ScrollAnimationKinetic::animationTimerFired()
148{
149 MonotonicTime currentTime = MonotonicTime::now();
150 Seconds deltaToNextFrame = 1_s * ceil((currentTime - m_startTime).value() * frameRate) / frameRate - (currentTime - m_startTime);
151
152 if (m_horizontalData && !m_horizontalData.value().animateScroll(deltaToNextFrame))
153 m_horizontalData = WTF::nullopt;
154
155 if (m_verticalData && !m_verticalData.value().animateScroll(deltaToNextFrame))
156 m_verticalData = WTF::nullopt;
157
158 // If one of the axes didn't finish its animation we must continue it.
159 if (m_horizontalData || m_verticalData)
160 m_animationTimer.startOneShot(std::max(minimumTimerInterval, deltaToNextFrame));
161
162 double x = m_horizontalData ? m_horizontalData.value().position() : m_position.x();
163 double y = m_verticalData ? m_verticalData.value().position() : m_position.y();
164 m_position = FloatPoint(x, y);
165 m_notifyPositionChangedFunction(FloatPoint(m_position));
166}
167
168} // namespace WebCore
169