blob: da4d877d0f9e66e9e6a2052ba2d4dd015d8aed86 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _LIBINPUT_VELOCITY_TRACKER_H
18#define _LIBINPUT_VELOCITY_TRACKER_H
19
20#include <input/Input.h>
Jeff Brown5912f952013-07-01 19:10:31 -070021#include <utils/BitSet.h>
Chris Yef8591482020-04-17 11:49:17 -070022#include <utils/Timers.h>
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000023#include <map>
24#include <set>
Jeff Brown5912f952013-07-01 19:10:31 -070025
26namespace android {
27
28class VelocityTrackerStrategy;
29
30/*
31 * Calculates the velocity of pointer movements over time.
32 */
33class VelocityTracker {
34public:
Chris Yef8591482020-04-17 11:49:17 -070035 enum class Strategy : int32_t {
36 DEFAULT = -1,
37 MIN = 0,
38 IMPULSE = 0,
39 LSQ1 = 1,
40 LSQ2 = 2,
41 LSQ3 = 3,
42 WLSQ2_DELTA = 4,
43 WLSQ2_CENTRAL = 5,
44 WLSQ2_RECENT = 6,
45 INT1 = 7,
46 INT2 = 8,
47 LEGACY = 9,
48 MAX = LEGACY,
49 };
50
Jeff Brown5912f952013-07-01 19:10:31 -070051 struct Estimator {
52 static const size_t MAX_DEGREE = 4;
53
54 // Estimator time base.
55 nsecs_t time;
56
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000057 // Polynomial coefficients describing motion.
58 float coeff[MAX_DEGREE + 1];
Jeff Brown5912f952013-07-01 19:10:31 -070059
60 // Polynomial degree (number of coefficients), or zero if no information is
61 // available.
62 uint32_t degree;
63
64 // Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit).
65 float confidence;
66
67 inline void clear() {
68 time = 0;
69 degree = 0;
70 confidence = 0;
71 for (size_t i = 0; i <= MAX_DEGREE; i++) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000072 coeff[i] = 0;
Jeff Brown5912f952013-07-01 19:10:31 -070073 }
74 }
75 };
76
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000077 /*
78 * Contains all available velocity data from a VelocityTracker.
79 */
80 struct ComputedVelocity {
81 inline std::optional<float> getVelocity(int32_t axis, uint32_t id) const {
82 const auto& axisVelocities = mVelocities.find(axis);
83 if (axisVelocities == mVelocities.end()) {
84 return {};
85 }
86
87 const auto& axisIdVelocity = axisVelocities->second.find(id);
88 if (axisIdVelocity == axisVelocities->second.end()) {
89 return {};
90 }
91
92 return axisIdVelocity->second;
93 }
94
95 inline void addVelocity(int32_t axis, uint32_t id, float velocity) {
96 mVelocities[axis][id] = velocity;
97 }
98
99 private:
100 std::map<int32_t /*axis*/, std::map<int32_t /*pointerId*/, float /*velocity*/>> mVelocities;
101 };
102
103 // Creates a velocity tracker using the specified strategy for each supported axis.
Chris Yef8591482020-04-17 11:49:17 -0700104 // If strategy is not provided, uses the default strategy for the platform.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000105 // TODO(b/32830165): support axis-specific strategies.
Chris Yef8591482020-04-17 11:49:17 -0700106 VelocityTracker(const Strategy strategy = Strategy::DEFAULT);
Jeff Brown5912f952013-07-01 19:10:31 -0700107
108 ~VelocityTracker();
109
110 // Resets the velocity tracker state.
111 void clear();
112
113 // Resets the velocity tracker state for specific pointers.
114 // Call this method when some pointers have changed and may be reusing
115 // an id that was assigned to a different pointer earlier.
116 void clearPointers(BitSet32 idBits);
117
118 // Adds movement information for a set of pointers.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000119 // The idBits bitfield specifies the pointer ids of the pointers whose data points
Jeff Brown5912f952013-07-01 19:10:31 -0700120 // are included in the movement.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000121 // The positions map contains a mapping of an axis to positions array.
122 // The positions arrays contain information for each pointer in order by increasing id.
123 // Each array's size should be equal to the number of one bits in idBits.
124 void addMovement(nsecs_t eventTime, BitSet32 idBits,
125 const std::map<int32_t, std::vector<float>>& positions);
Jeff Brown5912f952013-07-01 19:10:31 -0700126
127 // Adds movement information for all pointers in a MotionEvent, including historical samples.
128 void addMovement(const MotionEvent* event);
129
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000130 // Returns the velocity of the specified pointer id and axis in position units per second.
131 // Returns empty optional if there is insufficient movement information for the pointer, or if
132 // the given axis is not supported for velocity tracking.
133 std::optional<float> getVelocity(int32_t axis, uint32_t id) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700134
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000135 // Returns a ComputedVelocity instance with all available velocity data, using the given units
136 // (reference: units == 1 means "per millisecond"), and clamping each velocity between
137 // [-maxVelocity, maxVelocity], inclusive.
138 ComputedVelocity getComputedVelocity(int32_t units, float maxVelocity);
139
140 // Gets an estimator for the recent movements of the specified pointer id for the given axis.
Jeff Brown5912f952013-07-01 19:10:31 -0700141 // Returns false and clears the estimator if there is no information available
142 // about the pointer.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000143 bool getEstimator(int32_t axis, uint32_t id, Estimator* outEstimator) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700144
145 // Gets the active pointer id, or -1 if none.
146 inline int32_t getActivePointerId() const { return mActivePointerId; }
147
Jeff Brown5912f952013-07-01 19:10:31 -0700148private:
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700149 // All axes supported for velocity tracking, mapped to their default strategies.
Chris Yef8591482020-04-17 11:49:17 -0700150 // Although other strategies are available for testing and comparison purposes,
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700151 // the default strategy is the one that applications will actually use. Be very careful
Chris Yef8591482020-04-17 11:49:17 -0700152 // when adjusting the default strategy because it can dramatically affect
153 // (often in a bad way) the user experience.
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700154 static const std::map<int32_t, Strategy> DEFAULT_STRATEGY_BY_AXIS;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000155
156 // Axes specifying location on a 2D plane (i.e. X and Y).
157 static const std::set<int32_t> PLANAR_AXES;
158
Jeff Brown5912f952013-07-01 19:10:31 -0700159 nsecs_t mLastEventTime;
160 BitSet32 mCurrentPointerIdBits;
161 int32_t mActivePointerId;
Jeff Brown5912f952013-07-01 19:10:31 -0700162
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700163 // An override strategy passed in the constructor to be used for all axes.
164 // This strategy will apply to all axes, unless the default strategy is specified here.
165 // When default strategy is specified, then each axis will use a potentially different strategy
166 // based on a hardcoded mapping.
167 const Strategy mOverrideStrategy;
168 // Maps axes to their respective VelocityTrackerStrategy instances.
169 // Note that, only axes that have had MotionEvents (and not all supported axes) will be here.
170 std::map<int32_t /*axis*/, std::unique_ptr<VelocityTrackerStrategy>> mConfiguredStrategies;
171
172 void configureStrategy(int32_t axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700173
Chris Yef8591482020-04-17 11:49:17 -0700174 static std::unique_ptr<VelocityTrackerStrategy> createStrategy(const Strategy strategy);
Jeff Brown5912f952013-07-01 19:10:31 -0700175};
176
177
178/*
179 * Implements a particular velocity tracker algorithm.
180 */
181class VelocityTrackerStrategy {
182protected:
183 VelocityTrackerStrategy() { }
184
185public:
186 virtual ~VelocityTrackerStrategy() { }
187
Jeff Brown5912f952013-07-01 19:10:31 -0700188 virtual void clearPointers(BitSet32 idBits) = 0;
189 virtual void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000190 const std::vector<float>& positions) = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700191 virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const = 0;
192};
193
194
195/*
196 * Velocity tracker algorithm based on least-squares linear regression.
197 */
198class LeastSquaresVelocityTrackerStrategy : public VelocityTrackerStrategy {
199public:
200 enum Weighting {
201 // No weights applied. All data points are equally reliable.
202 WEIGHTING_NONE,
203
204 // Weight by time delta. Data points clustered together are weighted less.
205 WEIGHTING_DELTA,
206
207 // Weight such that points within a certain horizon are weighed more than those
208 // outside of that horizon.
209 WEIGHTING_CENTRAL,
210
211 // Weight such that points older than a certain amount are weighed less.
212 WEIGHTING_RECENT,
213 };
214
215 // Degree must be no greater than Estimator::MAX_DEGREE.
216 LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = WEIGHTING_NONE);
217 virtual ~LeastSquaresVelocityTrackerStrategy();
218
Jeff Brown5912f952013-07-01 19:10:31 -0700219 virtual void clearPointers(BitSet32 idBits);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500220 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000221 const std::vector<float>& positions) override;
Jeff Brown5912f952013-07-01 19:10:31 -0700222 virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
223
224private:
225 // Sample horizon.
226 // We don't use too much history by default since we want to react to quick
227 // changes in direction.
228 static const nsecs_t HORIZON = 100 * 1000000; // 100 ms
229
230 // Number of samples to keep.
231 static const uint32_t HISTORY_SIZE = 20;
232
233 struct Movement {
234 nsecs_t eventTime;
235 BitSet32 idBits;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000236 float positions[MAX_POINTERS];
Jeff Brown5912f952013-07-01 19:10:31 -0700237
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000238 inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
Jeff Brown5912f952013-07-01 19:10:31 -0700239 };
240
241 float chooseWeight(uint32_t index) const;
242
243 const uint32_t mDegree;
244 const Weighting mWeighting;
245 uint32_t mIndex;
246 Movement mMovements[HISTORY_SIZE];
247};
248
249
250/*
251 * Velocity tracker algorithm that uses an IIR filter.
252 */
253class IntegratingVelocityTrackerStrategy : public VelocityTrackerStrategy {
254public:
255 // Degree must be 1 or 2.
256 IntegratingVelocityTrackerStrategy(uint32_t degree);
257 ~IntegratingVelocityTrackerStrategy();
258
Jeff Brown5912f952013-07-01 19:10:31 -0700259 virtual void clearPointers(BitSet32 idBits);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500260 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000261 const std::vector<float>& positions) override;
Jeff Brown5912f952013-07-01 19:10:31 -0700262 virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
263
264private:
265 // Current state estimate for a particular pointer.
266 struct State {
267 nsecs_t updateTime;
268 uint32_t degree;
269
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000270 float pos, vel, accel;
Jeff Brown5912f952013-07-01 19:10:31 -0700271 };
272
273 const uint32_t mDegree;
274 BitSet32 mPointerIdBits;
275 State mPointerState[MAX_POINTER_ID + 1];
276
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000277 void initState(State& state, nsecs_t eventTime, float pos) const;
278 void updateState(State& state, nsecs_t eventTime, float pos) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700279 void populateEstimator(const State& state, VelocityTracker::Estimator* outEstimator) const;
280};
281
282
283/*
284 * Velocity tracker strategy used prior to ICS.
285 */
286class LegacyVelocityTrackerStrategy : public VelocityTrackerStrategy {
287public:
288 LegacyVelocityTrackerStrategy();
289 virtual ~LegacyVelocityTrackerStrategy();
290
Jeff Brown5912f952013-07-01 19:10:31 -0700291 virtual void clearPointers(BitSet32 idBits);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500292 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000293 const std::vector<float>& positions) override;
Jeff Brown5912f952013-07-01 19:10:31 -0700294 virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
295
296private:
297 // Oldest sample to consider when calculating the velocity.
298 static const nsecs_t HORIZON = 200 * 1000000; // 100 ms
299
300 // Number of samples to keep.
301 static const uint32_t HISTORY_SIZE = 20;
302
303 // The minimum duration between samples when estimating velocity.
304 static const nsecs_t MIN_DURATION = 10 * 1000000; // 10 ms
305
306 struct Movement {
307 nsecs_t eventTime;
308 BitSet32 idBits;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000309 float positions[MAX_POINTERS];
Jeff Brown5912f952013-07-01 19:10:31 -0700310
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000311 inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
Jeff Brown5912f952013-07-01 19:10:31 -0700312 };
313
314 uint32_t mIndex;
315 Movement mMovements[HISTORY_SIZE];
316};
317
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100318class ImpulseVelocityTrackerStrategy : public VelocityTrackerStrategy {
319public:
320 ImpulseVelocityTrackerStrategy();
321 virtual ~ImpulseVelocityTrackerStrategy();
322
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100323 virtual void clearPointers(BitSet32 idBits);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500324 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000325 const std::vector<float>& positions) override;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100326 virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
327
328private:
329 // Sample horizon.
330 // We don't use too much history by default since we want to react to quick
331 // changes in direction.
332 static constexpr nsecs_t HORIZON = 100 * 1000000; // 100 ms
333
334 // Number of samples to keep.
335 static constexpr size_t HISTORY_SIZE = 20;
336
337 struct Movement {
338 nsecs_t eventTime;
339 BitSet32 idBits;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000340 float positions[MAX_POINTERS];
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100341
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000342 inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100343 };
344
345 size_t mIndex;
346 Movement mMovements[HISTORY_SIZE];
347};
348
Jeff Brown5912f952013-07-01 19:10:31 -0700349} // namespace android
350
351#endif // _LIBINPUT_VELOCITY_TRACKER_H