1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2012 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "VelocityControl"
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker // Log debug messages about acceleration.
20*38e8c45fSAndroid Build Coastguard Worker static constexpr bool DEBUG_ACCELERATION = false;
21*38e8c45fSAndroid Build Coastguard Worker
22*38e8c45fSAndroid Build Coastguard Worker #include <math.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <limits.h>
24*38e8c45fSAndroid Build Coastguard Worker
25*38e8c45fSAndroid Build Coastguard Worker #include <android-base/logging.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <input/VelocityControl.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <utils/BitSet.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <utils/Timers.h>
29*38e8c45fSAndroid Build Coastguard Worker
30*38e8c45fSAndroid Build Coastguard Worker namespace android {
31*38e8c45fSAndroid Build Coastguard Worker
32*38e8c45fSAndroid Build Coastguard Worker // --- VelocityControl ---
33*38e8c45fSAndroid Build Coastguard Worker
34*38e8c45fSAndroid Build Coastguard Worker const nsecs_t VelocityControl::STOP_TIME;
35*38e8c45fSAndroid Build Coastguard Worker
VelocityControl()36*38e8c45fSAndroid Build Coastguard Worker VelocityControl::VelocityControl() {
37*38e8c45fSAndroid Build Coastguard Worker reset();
38*38e8c45fSAndroid Build Coastguard Worker }
39*38e8c45fSAndroid Build Coastguard Worker
reset()40*38e8c45fSAndroid Build Coastguard Worker void VelocityControl::reset() {
41*38e8c45fSAndroid Build Coastguard Worker mLastMovementTime = LLONG_MIN;
42*38e8c45fSAndroid Build Coastguard Worker mRawPositionX = 0;
43*38e8c45fSAndroid Build Coastguard Worker mRawPositionY = 0;
44*38e8c45fSAndroid Build Coastguard Worker mVelocityTracker.clear();
45*38e8c45fSAndroid Build Coastguard Worker }
46*38e8c45fSAndroid Build Coastguard Worker
move(nsecs_t eventTime,float * deltaX,float * deltaY)47*38e8c45fSAndroid Build Coastguard Worker void VelocityControl::move(nsecs_t eventTime, float* deltaX, float* deltaY) {
48*38e8c45fSAndroid Build Coastguard Worker if ((deltaX == nullptr || *deltaX == 0) && (deltaY == nullptr || *deltaY == 0)) {
49*38e8c45fSAndroid Build Coastguard Worker return;
50*38e8c45fSAndroid Build Coastguard Worker }
51*38e8c45fSAndroid Build Coastguard Worker if (eventTime >= mLastMovementTime + STOP_TIME) {
52*38e8c45fSAndroid Build Coastguard Worker ALOGD_IF(DEBUG_ACCELERATION && mLastMovementTime != LLONG_MIN,
53*38e8c45fSAndroid Build Coastguard Worker "VelocityControl: stopped, last movement was %0.3fms ago",
54*38e8c45fSAndroid Build Coastguard Worker (eventTime - mLastMovementTime) * 0.000001f);
55*38e8c45fSAndroid Build Coastguard Worker reset();
56*38e8c45fSAndroid Build Coastguard Worker }
57*38e8c45fSAndroid Build Coastguard Worker
58*38e8c45fSAndroid Build Coastguard Worker mLastMovementTime = eventTime;
59*38e8c45fSAndroid Build Coastguard Worker if (deltaX) {
60*38e8c45fSAndroid Build Coastguard Worker mRawPositionX += *deltaX;
61*38e8c45fSAndroid Build Coastguard Worker }
62*38e8c45fSAndroid Build Coastguard Worker if (deltaY) {
63*38e8c45fSAndroid Build Coastguard Worker mRawPositionY += *deltaY;
64*38e8c45fSAndroid Build Coastguard Worker }
65*38e8c45fSAndroid Build Coastguard Worker mVelocityTracker.addMovement(eventTime, /*pointerId=*/0, AMOTION_EVENT_AXIS_X, mRawPositionX);
66*38e8c45fSAndroid Build Coastguard Worker mVelocityTracker.addMovement(eventTime, /*pointerId=*/0, AMOTION_EVENT_AXIS_Y, mRawPositionY);
67*38e8c45fSAndroid Build Coastguard Worker scaleDeltas(deltaX, deltaY);
68*38e8c45fSAndroid Build Coastguard Worker }
69*38e8c45fSAndroid Build Coastguard Worker
70*38e8c45fSAndroid Build Coastguard Worker // --- SimpleVelocityControl ---
71*38e8c45fSAndroid Build Coastguard Worker
getParameters() const72*38e8c45fSAndroid Build Coastguard Worker const VelocityControlParameters& SimpleVelocityControl::getParameters() const {
73*38e8c45fSAndroid Build Coastguard Worker return mParameters;
74*38e8c45fSAndroid Build Coastguard Worker }
75*38e8c45fSAndroid Build Coastguard Worker
setParameters(const VelocityControlParameters & parameters)76*38e8c45fSAndroid Build Coastguard Worker void SimpleVelocityControl::setParameters(const VelocityControlParameters& parameters) {
77*38e8c45fSAndroid Build Coastguard Worker mParameters = parameters;
78*38e8c45fSAndroid Build Coastguard Worker reset();
79*38e8c45fSAndroid Build Coastguard Worker }
80*38e8c45fSAndroid Build Coastguard Worker
scaleDeltas(float * deltaX,float * deltaY)81*38e8c45fSAndroid Build Coastguard Worker void SimpleVelocityControl::scaleDeltas(float* deltaX, float* deltaY) {
82*38e8c45fSAndroid Build Coastguard Worker std::optional<float> vx = mVelocityTracker.getVelocity(AMOTION_EVENT_AXIS_X, 0);
83*38e8c45fSAndroid Build Coastguard Worker std::optional<float> vy = mVelocityTracker.getVelocity(AMOTION_EVENT_AXIS_Y, 0);
84*38e8c45fSAndroid Build Coastguard Worker float scale = mParameters.scale;
85*38e8c45fSAndroid Build Coastguard Worker if (vx.has_value() && vy.has_value()) {
86*38e8c45fSAndroid Build Coastguard Worker float speed = hypotf(*vx, *vy) * scale;
87*38e8c45fSAndroid Build Coastguard Worker if (speed >= mParameters.highThreshold) {
88*38e8c45fSAndroid Build Coastguard Worker // Apply full acceleration above the high speed threshold.
89*38e8c45fSAndroid Build Coastguard Worker scale *= mParameters.acceleration;
90*38e8c45fSAndroid Build Coastguard Worker } else if (speed > mParameters.lowThreshold) {
91*38e8c45fSAndroid Build Coastguard Worker // Linearly interpolate the acceleration to apply between the low and high
92*38e8c45fSAndroid Build Coastguard Worker // speed thresholds.
93*38e8c45fSAndroid Build Coastguard Worker scale *= 1 +
94*38e8c45fSAndroid Build Coastguard Worker (speed - mParameters.lowThreshold) /
95*38e8c45fSAndroid Build Coastguard Worker (mParameters.highThreshold - mParameters.lowThreshold) *
96*38e8c45fSAndroid Build Coastguard Worker (mParameters.acceleration - 1);
97*38e8c45fSAndroid Build Coastguard Worker }
98*38e8c45fSAndroid Build Coastguard Worker
99*38e8c45fSAndroid Build Coastguard Worker ALOGD_IF(DEBUG_ACCELERATION,
100*38e8c45fSAndroid Build Coastguard Worker "SimpleVelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): "
101*38e8c45fSAndroid Build Coastguard Worker "vx=%0.3f, vy=%0.3f, speed=%0.3f, accel=%0.3f",
102*38e8c45fSAndroid Build Coastguard Worker mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold,
103*38e8c45fSAndroid Build Coastguard Worker mParameters.acceleration, *vx, *vy, speed, scale / mParameters.scale);
104*38e8c45fSAndroid Build Coastguard Worker
105*38e8c45fSAndroid Build Coastguard Worker } else {
106*38e8c45fSAndroid Build Coastguard Worker ALOGD_IF(DEBUG_ACCELERATION,
107*38e8c45fSAndroid Build Coastguard Worker "SimpleVelocityControl(%0.3f, %0.3f, %0.3f, %0.3f): unknown velocity",
108*38e8c45fSAndroid Build Coastguard Worker mParameters.scale, mParameters.lowThreshold, mParameters.highThreshold,
109*38e8c45fSAndroid Build Coastguard Worker mParameters.acceleration);
110*38e8c45fSAndroid Build Coastguard Worker }
111*38e8c45fSAndroid Build Coastguard Worker
112*38e8c45fSAndroid Build Coastguard Worker if (deltaX != nullptr) {
113*38e8c45fSAndroid Build Coastguard Worker *deltaX *= scale;
114*38e8c45fSAndroid Build Coastguard Worker }
115*38e8c45fSAndroid Build Coastguard Worker if (deltaY != nullptr) {
116*38e8c45fSAndroid Build Coastguard Worker *deltaY *= scale;
117*38e8c45fSAndroid Build Coastguard Worker }
118*38e8c45fSAndroid Build Coastguard Worker }
119*38e8c45fSAndroid Build Coastguard Worker
120*38e8c45fSAndroid Build Coastguard Worker // --- CurvedVelocityControl ---
121*38e8c45fSAndroid Build Coastguard Worker
122*38e8c45fSAndroid Build Coastguard Worker namespace {
123*38e8c45fSAndroid Build Coastguard Worker
124*38e8c45fSAndroid Build Coastguard Worker /**
125*38e8c45fSAndroid Build Coastguard Worker * The resolution that we assume a mouse to have, in counts per inch.
126*38e8c45fSAndroid Build Coastguard Worker *
127*38e8c45fSAndroid Build Coastguard Worker * Mouse resolutions vary wildly, but 800 CPI is probably the most common. There should be enough
128*38e8c45fSAndroid Build Coastguard Worker * range in the available sensitivity settings to accommodate users of mice with other resolutions.
129*38e8c45fSAndroid Build Coastguard Worker */
130*38e8c45fSAndroid Build Coastguard Worker constexpr int32_t MOUSE_CPI = 800;
131*38e8c45fSAndroid Build Coastguard Worker
countsToMm(float counts)132*38e8c45fSAndroid Build Coastguard Worker float countsToMm(float counts) {
133*38e8c45fSAndroid Build Coastguard Worker return counts / MOUSE_CPI * 25.4;
134*38e8c45fSAndroid Build Coastguard Worker }
135*38e8c45fSAndroid Build Coastguard Worker
136*38e8c45fSAndroid Build Coastguard Worker } // namespace
137*38e8c45fSAndroid Build Coastguard Worker
CurvedVelocityControl()138*38e8c45fSAndroid Build Coastguard Worker CurvedVelocityControl::CurvedVelocityControl()
139*38e8c45fSAndroid Build Coastguard Worker : mCurveSegments(createAccelerationCurveForPointerSensitivity(0)) {}
140*38e8c45fSAndroid Build Coastguard Worker
setCurve(const std::vector<AccelerationCurveSegment> & curve)141*38e8c45fSAndroid Build Coastguard Worker void CurvedVelocityControl::setCurve(const std::vector<AccelerationCurveSegment>& curve) {
142*38e8c45fSAndroid Build Coastguard Worker mCurveSegments = curve;
143*38e8c45fSAndroid Build Coastguard Worker }
144*38e8c45fSAndroid Build Coastguard Worker
setAccelerationEnabled(bool enabled)145*38e8c45fSAndroid Build Coastguard Worker void CurvedVelocityControl::setAccelerationEnabled(bool enabled) {
146*38e8c45fSAndroid Build Coastguard Worker mAccelerationEnabled = enabled;
147*38e8c45fSAndroid Build Coastguard Worker }
148*38e8c45fSAndroid Build Coastguard Worker
scaleDeltas(float * deltaX,float * deltaY)149*38e8c45fSAndroid Build Coastguard Worker void CurvedVelocityControl::scaleDeltas(float* deltaX, float* deltaY) {
150*38e8c45fSAndroid Build Coastguard Worker if (!mAccelerationEnabled) {
151*38e8c45fSAndroid Build Coastguard Worker ALOGD_IF(DEBUG_ACCELERATION, "CurvedVelocityControl: acceleration disabled");
152*38e8c45fSAndroid Build Coastguard Worker return;
153*38e8c45fSAndroid Build Coastguard Worker }
154*38e8c45fSAndroid Build Coastguard Worker
155*38e8c45fSAndroid Build Coastguard Worker std::optional<float> vx = mVelocityTracker.getVelocity(AMOTION_EVENT_AXIS_X, 0);
156*38e8c45fSAndroid Build Coastguard Worker std::optional<float> vy = mVelocityTracker.getVelocity(AMOTION_EVENT_AXIS_Y, 0);
157*38e8c45fSAndroid Build Coastguard Worker
158*38e8c45fSAndroid Build Coastguard Worker float ratio;
159*38e8c45fSAndroid Build Coastguard Worker if (vx.has_value() && vy.has_value()) {
160*38e8c45fSAndroid Build Coastguard Worker float vxMmPerS = countsToMm(*vx);
161*38e8c45fSAndroid Build Coastguard Worker float vyMmPerS = countsToMm(*vy);
162*38e8c45fSAndroid Build Coastguard Worker float speedMmPerS = sqrtf(vxMmPerS * vxMmPerS + vyMmPerS * vyMmPerS);
163*38e8c45fSAndroid Build Coastguard Worker
164*38e8c45fSAndroid Build Coastguard Worker const AccelerationCurveSegment& seg = segmentForSpeed(speedMmPerS);
165*38e8c45fSAndroid Build Coastguard Worker ratio = seg.baseGain + seg.reciprocal / speedMmPerS;
166*38e8c45fSAndroid Build Coastguard Worker ALOGD_IF(DEBUG_ACCELERATION,
167*38e8c45fSAndroid Build Coastguard Worker "CurvedVelocityControl: velocities (%0.3f, %0.3f) → speed %0.3f → ratio %0.3f",
168*38e8c45fSAndroid Build Coastguard Worker vxMmPerS, vyMmPerS, speedMmPerS, ratio);
169*38e8c45fSAndroid Build Coastguard Worker } else {
170*38e8c45fSAndroid Build Coastguard Worker // We don't have enough data to compute a velocity yet. This happens early in the movement,
171*38e8c45fSAndroid Build Coastguard Worker // when the speed is presumably low, so use the base gain of the first segment of the curve.
172*38e8c45fSAndroid Build Coastguard Worker // (This would behave oddly for curves with a reciprocal term on the first segment, but we
173*38e8c45fSAndroid Build Coastguard Worker // don't have any of those, and they'd be very strange at velocities close to zero anyway.)
174*38e8c45fSAndroid Build Coastguard Worker ratio = mCurveSegments[0].baseGain;
175*38e8c45fSAndroid Build Coastguard Worker ALOGD_IF(DEBUG_ACCELERATION,
176*38e8c45fSAndroid Build Coastguard Worker "CurvedVelocityControl: unknown velocity, using base gain of first segment (%.3f)",
177*38e8c45fSAndroid Build Coastguard Worker ratio);
178*38e8c45fSAndroid Build Coastguard Worker }
179*38e8c45fSAndroid Build Coastguard Worker
180*38e8c45fSAndroid Build Coastguard Worker if (deltaX != nullptr) {
181*38e8c45fSAndroid Build Coastguard Worker *deltaX *= ratio;
182*38e8c45fSAndroid Build Coastguard Worker }
183*38e8c45fSAndroid Build Coastguard Worker if (deltaY != nullptr) {
184*38e8c45fSAndroid Build Coastguard Worker *deltaY *= ratio;
185*38e8c45fSAndroid Build Coastguard Worker }
186*38e8c45fSAndroid Build Coastguard Worker }
187*38e8c45fSAndroid Build Coastguard Worker
segmentForSpeed(float speedMmPerS)188*38e8c45fSAndroid Build Coastguard Worker const AccelerationCurveSegment& CurvedVelocityControl::segmentForSpeed(float speedMmPerS) {
189*38e8c45fSAndroid Build Coastguard Worker for (const AccelerationCurveSegment& seg : mCurveSegments) {
190*38e8c45fSAndroid Build Coastguard Worker if (speedMmPerS <= seg.maxPointerSpeedMmPerS) {
191*38e8c45fSAndroid Build Coastguard Worker return seg;
192*38e8c45fSAndroid Build Coastguard Worker }
193*38e8c45fSAndroid Build Coastguard Worker }
194*38e8c45fSAndroid Build Coastguard Worker ALOGE("CurvedVelocityControl: No segment found for speed %.3f; last segment should always have "
195*38e8c45fSAndroid Build Coastguard Worker "a max speed of infinity.",
196*38e8c45fSAndroid Build Coastguard Worker speedMmPerS);
197*38e8c45fSAndroid Build Coastguard Worker return mCurveSegments.back();
198*38e8c45fSAndroid Build Coastguard Worker }
199*38e8c45fSAndroid Build Coastguard Worker
200*38e8c45fSAndroid Build Coastguard Worker } // namespace android
201