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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "AndroidFlingPhysics.h"
#include <cmath>
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/StaticPrefs_apz.h"
#include "mozilla/StaticPtr.h"
namespace mozilla {
namespace layers {
// The fling physics calculations implemented here are adapted from
// Chrome's implementation of fling physics on Android:
// https://cs.chromium.org/chromium/src/ui/events/android/scroller.cc?rcl=3ae3aaff927038a5c644926842cb0c31dea60c79
static double ComputeDeceleration(float aDPI) {
const float kFriction = 0.84f;
const float kGravityEarth = 9.80665f;
return kGravityEarth // g (m/s^2)
* 39.37f // inch/meter
* aDPI // pixels/inch
* kFriction;
}
// == std::log(0.78f) / std::log(0.9f)
const float kDecelerationRate = 2.3582018f;
// Default friction constant in android.view.ViewConfiguration.
static float GetFlingFriction() {
return StaticPrefs::apz_android_chrome_fling_physics_friction();
}
// Tension lines cross at (GetInflexion(), 1).
static float GetInflexion() {
// Clamp the inflexion to the range [0,1]. Values outside of this range
// do not make sense in the physics model, and for negative values the
// approximation used to compute the spline curve does not converge.
const float inflexion =
StaticPrefs::apz_android_chrome_fling_physics_inflexion();
if (inflexion < 0.0f) {
return 0.0f;
}
if (inflexion > 1.0f) {
return 1.0f;
}
return inflexion;
}
// Fling scroll is stopped when the scroll position is |kThresholdForFlingEnd|
// pixels or closer from the end.
static float GetThresholdForFlingEnd() {
return StaticPrefs::apz_android_chrome_fling_physics_stop_threshold();
}
static double ComputeSplineDeceleration(ParentLayerCoord aVelocity,
double aTuningCoeff) {
float velocityPerSec = aVelocity * 1000.0f;
return std::log(GetInflexion() * velocityPerSec /
(GetFlingFriction() * aTuningCoeff));
}
static TimeDuration ComputeFlingDuration(ParentLayerCoord aVelocity,
double aTuningCoeff) {
const double splineDecel = ComputeSplineDeceleration(aVelocity, aTuningCoeff);
const double timeSeconds = std::exp(splineDecel / (kDecelerationRate - 1.0));
return TimeDuration::FromSeconds(timeSeconds);
}
static ParentLayerCoord ComputeFlingDistance(ParentLayerCoord aVelocity,
double aTuningCoeff) {
const double splineDecel = ComputeSplineDeceleration(aVelocity, aTuningCoeff);
return GetFlingFriction() * aTuningCoeff *
std::exp(kDecelerationRate / (kDecelerationRate - 1.0) * splineDecel);
}
struct SplineConstants {
public:
SplineConstants() {
const float kStartTension = 0.5f;
const float kEndTension = 1.0f;
const float kP1 = kStartTension * GetInflexion();
const float kP2 = 1.0f - kEndTension * (1.0f - GetInflexion());
float xMin = 0.0f;
for (int i = 0; i < kNumSamples; i++) {
const float alpha = static_cast<float>(i) / kNumSamples;
float xMax = 1.0f;
float x, tx, coef;
// While the inflexion can be overridden by the user, it's clamped to
// [0,1]. For values in this range, the approximation algorithm below
// should converge in < 20 iterations. For good measure, we impose an
// iteration limit as well.
static const int sIterationLimit = 100;
int iterations = 0;
while (iterations++ < sIterationLimit) {
x = xMin + (xMax - xMin) / 2.0f;
coef = 3.0f * x * (1.0f - x);
tx = coef * ((1.0f - x) * kP1 + x * kP2) + x * x * x;
if (FuzzyEqualsAdditive(tx, alpha)) {
break;
}
if (tx > alpha) {
xMax = x;
} else {
xMin = x;
}
}
mSplinePositions[i] = coef * ((1.0f - x) * kStartTension + x) + x * x * x;
}
mSplinePositions[kNumSamples] = 1.0f;
}
void CalculateCoefficients(float aTime, float* aOutDistanceCoef,
float* aOutVelocityCoef) {
*aOutDistanceCoef = 1.0f;
*aOutVelocityCoef = 0.0f;
const int index = static_cast<int>(kNumSamples * aTime);
if (index < kNumSamples) {
const float tInf = static_cast<float>(index) / kNumSamples;
const float dInf = mSplinePositions[index];
const float tSup = static_cast<float>(index + 1) / kNumSamples;
const float dSup = mSplinePositions[index + 1];
*aOutVelocityCoef = (dSup - dInf) / (tSup - tInf);
*aOutDistanceCoef = dInf + (aTime - tInf) * *aOutVelocityCoef;
}
}
private:
static const int kNumSamples = 100;
float mSplinePositions[kNumSamples + 1];
};
StaticAutoPtr<SplineConstants> gSplineConstants;
/* static */
void AndroidFlingPhysics::InitializeGlobalState() {
gSplineConstants = new SplineConstants();
ClearOnShutdown(&gSplineConstants);
}
void AndroidFlingPhysics::Init(const ParentLayerPoint& aStartingVelocity,
float aPLPPI) {
mVelocity = aStartingVelocity.Length();
// We should not have created a fling animation if there is no velocity.
MOZ_ASSERT(mVelocity != 0.0f);
const double tuningCoeff = ComputeDeceleration(aPLPPI);
mTargetDuration = ComputeFlingDuration(mVelocity, tuningCoeff);
MOZ_ASSERT(!mTargetDuration.IsZero());
mDurationSoFar = TimeDuration();
mLastPos = ParentLayerPoint();
mCurrentPos = ParentLayerPoint();
float coeffX =
mVelocity == 0 ? 1.0f : aStartingVelocity.x.value / mVelocity.value;
float coeffY =
mVelocity == 0 ? 1.0f : aStartingVelocity.y.value / mVelocity.value;
mTargetDistance = ComputeFlingDistance(mVelocity, tuningCoeff);
mTargetPos =
ParentLayerPoint(mTargetDistance * coeffX, mTargetDistance * coeffY);
const float hyp = mTargetPos.Length();
if (FuzzyEqualsAdditive(hyp, 0.0f)) {
mDeltaNorm = ParentLayerPoint(1, 1);
} else {
mDeltaNorm = ParentLayerPoint(mTargetPos.x / hyp, mTargetPos.y / hyp);
}
}
void AndroidFlingPhysics::Sample(const TimeDuration& aDelta,
ParentLayerPoint* aOutVelocity,
ParentLayerPoint* aOutOffset) {
float newVelocity;
if (SampleImpl(aDelta, &newVelocity)) {
*aOutOffset = (mCurrentPos - mLastPos);
*aOutVelocity = ParentLayerPoint(mDeltaNorm.x * newVelocity,
mDeltaNorm.y * newVelocity);
mLastPos = mCurrentPos;
} else {
*aOutOffset = (mTargetPos - mLastPos);
*aOutVelocity = ParentLayerPoint();
}
}
bool AndroidFlingPhysics::SampleImpl(const TimeDuration& aDelta,
float* aOutVelocity) {
mDurationSoFar += aDelta;
if (mDurationSoFar >= mTargetDuration) {
return false;
}
const float timeRatio =
mDurationSoFar.ToSeconds() / mTargetDuration.ToSeconds();
float distanceCoef = 1.0f;
float velocityCoef = 0.0f;
gSplineConstants->CalculateCoefficients(timeRatio, &distanceCoef,
&velocityCoef);
// The caller expects the velocity in pixels per _millisecond_.
*aOutVelocity =
velocityCoef * mTargetDistance / mTargetDuration.ToMilliseconds();
mCurrentPos = mTargetPos * distanceCoef;
ParentLayerPoint remainder = mTargetPos - mCurrentPos;
const float threshold = GetThresholdForFlingEnd();
if (fabsf(remainder.x) < threshold && fabsf(remainder.y) < threshold) {
return false;
}
return true;
}
} // namespace layers
} // namespace mozilla
|