blob: 62c3ae15ced802879ddcbcfeafe93aac5fad109a [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
Prabir Pradhanc08b0db2022-09-10 00:57:15 +000017#pragma once
Jeff Brown5912f952013-07-01 19:10:31 -070018
19#include <input/Input.h>
Jeff Brown5912f952013-07-01 19:10:31 -070020#include <utils/BitSet.h>
Chris Yef8591482020-04-17 11:49:17 -070021#include <utils/Timers.h>
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000022#include <map>
23#include <set>
Jeff Brown5912f952013-07-01 19:10:31 -070024
25namespace android {
26
27class VelocityTrackerStrategy;
28
29/*
30 * Calculates the velocity of pointer movements over time.
31 */
32class VelocityTracker {
33public:
Chris Yef8591482020-04-17 11:49:17 -070034 enum class Strategy : int32_t {
35 DEFAULT = -1,
36 MIN = 0,
37 IMPULSE = 0,
38 LSQ1 = 1,
39 LSQ2 = 2,
40 LSQ3 = 3,
41 WLSQ2_DELTA = 4,
42 WLSQ2_CENTRAL = 5,
43 WLSQ2_RECENT = 6,
44 INT1 = 7,
45 INT2 = 8,
46 LEGACY = 9,
47 MAX = LEGACY,
48 };
49
Jeff Brown5912f952013-07-01 19:10:31 -070050 struct Estimator {
51 static const size_t MAX_DEGREE = 4;
52
53 // Estimator time base.
54 nsecs_t time;
55
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000056 // Polynomial coefficients describing motion.
57 float coeff[MAX_DEGREE + 1];
Jeff Brown5912f952013-07-01 19:10:31 -070058
59 // Polynomial degree (number of coefficients), or zero if no information is
60 // available.
61 uint32_t degree;
62
63 // Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit).
64 float confidence;
65
66 inline void clear() {
67 time = 0;
68 degree = 0;
69 confidence = 0;
70 for (size_t i = 0; i <= MAX_DEGREE; i++) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000071 coeff[i] = 0;
Jeff Brown5912f952013-07-01 19:10:31 -070072 }
73 }
74 };
75
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000076 /*
77 * Contains all available velocity data from a VelocityTracker.
78 */
79 struct ComputedVelocity {
80 inline std::optional<float> getVelocity(int32_t axis, uint32_t id) const {
81 const auto& axisVelocities = mVelocities.find(axis);
82 if (axisVelocities == mVelocities.end()) {
83 return {};
84 }
85
86 const auto& axisIdVelocity = axisVelocities->second.find(id);
87 if (axisIdVelocity == axisVelocities->second.end()) {
88 return {};
89 }
90
91 return axisIdVelocity->second;
92 }
93
94 inline void addVelocity(int32_t axis, uint32_t id, float velocity) {
95 mVelocities[axis][id] = velocity;
96 }
97
98 private:
99 std::map<int32_t /*axis*/, std::map<int32_t /*pointerId*/, float /*velocity*/>> mVelocities;
100 };
101
102 // Creates a velocity tracker using the specified strategy for each supported axis.
Chris Yef8591482020-04-17 11:49:17 -0700103 // If strategy is not provided, uses the default strategy for the platform.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000104 // TODO(b/32830165): support axis-specific strategies.
Chris Yef8591482020-04-17 11:49:17 -0700105 VelocityTracker(const Strategy strategy = Strategy::DEFAULT);
Jeff Brown5912f952013-07-01 19:10:31 -0700106
107 ~VelocityTracker();
108
Yeabkal Wubshiteca273c2022-10-05 19:06:40 -0700109 /** Return true if the axis is supported for velocity tracking, false otherwise. */
110 static bool isAxisSupported(int32_t axis);
111
Jeff Brown5912f952013-07-01 19:10:31 -0700112 // Resets the velocity tracker state.
113 void clear();
114
115 // Resets the velocity tracker state for specific pointers.
116 // Call this method when some pointers have changed and may be reusing
117 // an id that was assigned to a different pointer earlier.
118 void clearPointers(BitSet32 idBits);
119
120 // Adds movement information for a set of pointers.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000121 // The idBits bitfield specifies the pointer ids of the pointers whose data points
Jeff Brown5912f952013-07-01 19:10:31 -0700122 // are included in the movement.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000123 // The positions map contains a mapping of an axis to positions array.
124 // The positions arrays contain information for each pointer in order by increasing id.
125 // Each array's size should be equal to the number of one bits in idBits.
126 void addMovement(nsecs_t eventTime, BitSet32 idBits,
127 const std::map<int32_t, std::vector<float>>& positions);
Jeff Brown5912f952013-07-01 19:10:31 -0700128
129 // Adds movement information for all pointers in a MotionEvent, including historical samples.
130 void addMovement(const MotionEvent* event);
131
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000132 // Returns the velocity of the specified pointer id and axis in position units per second.
133 // Returns empty optional if there is insufficient movement information for the pointer, or if
134 // the given axis is not supported for velocity tracking.
135 std::optional<float> getVelocity(int32_t axis, uint32_t id) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700136
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000137 // Returns a ComputedVelocity instance with all available velocity data, using the given units
138 // (reference: units == 1 means "per millisecond"), and clamping each velocity between
139 // [-maxVelocity, maxVelocity], inclusive.
140 ComputedVelocity getComputedVelocity(int32_t units, float maxVelocity);
141
142 // Gets an estimator for the recent movements of the specified pointer id for the given axis.
Jeff Brown5912f952013-07-01 19:10:31 -0700143 // Returns false and clears the estimator if there is no information available
144 // about the pointer.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000145 bool getEstimator(int32_t axis, uint32_t id, Estimator* outEstimator) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700146
147 // Gets the active pointer id, or -1 if none.
148 inline int32_t getActivePointerId() const { return mActivePointerId; }
149
Jeff Brown5912f952013-07-01 19:10:31 -0700150private:
Jeff Brown5912f952013-07-01 19:10:31 -0700151 nsecs_t mLastEventTime;
152 BitSet32 mCurrentPointerIdBits;
153 int32_t mActivePointerId;
Jeff Brown5912f952013-07-01 19:10:31 -0700154
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700155 // An override strategy passed in the constructor to be used for all axes.
156 // This strategy will apply to all axes, unless the default strategy is specified here.
157 // When default strategy is specified, then each axis will use a potentially different strategy
158 // based on a hardcoded mapping.
159 const Strategy mOverrideStrategy;
160 // Maps axes to their respective VelocityTrackerStrategy instances.
161 // Note that, only axes that have had MotionEvents (and not all supported axes) will be here.
162 std::map<int32_t /*axis*/, std::unique_ptr<VelocityTrackerStrategy>> mConfiguredStrategies;
163
164 void configureStrategy(int32_t axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700165
Yeabkal Wubshit73c95082022-09-22 10:12:33 +0000166 // Generates a VelocityTrackerStrategy instance for the given Strategy type.
167 // The `deltaValues` parameter indicates whether or not the created strategy should treat motion
168 // values as deltas (and not as absolute values). This the parameter is applicable only for
169 // strategies that support differential axes.
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700170 static std::unique_ptr<VelocityTrackerStrategy> createStrategy(const Strategy strategy,
Yeabkal Wubshit73c95082022-09-22 10:12:33 +0000171 bool deltaValues);
Jeff Brown5912f952013-07-01 19:10:31 -0700172};
173
174
175/*
176 * Implements a particular velocity tracker algorithm.
177 */
178class VelocityTrackerStrategy {
179protected:
180 VelocityTrackerStrategy() { }
181
182public:
183 virtual ~VelocityTrackerStrategy() { }
184
Jeff Brown5912f952013-07-01 19:10:31 -0700185 virtual void clearPointers(BitSet32 idBits) = 0;
186 virtual void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000187 const std::vector<float>& positions) = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700188 virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const = 0;
189};
190
191
192/*
193 * Velocity tracker algorithm based on least-squares linear regression.
194 */
195class LeastSquaresVelocityTrackerStrategy : public VelocityTrackerStrategy {
196public:
197 enum Weighting {
198 // No weights applied. All data points are equally reliable.
199 WEIGHTING_NONE,
200
201 // Weight by time delta. Data points clustered together are weighted less.
202 WEIGHTING_DELTA,
203
204 // Weight such that points within a certain horizon are weighed more than those
205 // outside of that horizon.
206 WEIGHTING_CENTRAL,
207
208 // Weight such that points older than a certain amount are weighed less.
209 WEIGHTING_RECENT,
210 };
211
212 // Degree must be no greater than Estimator::MAX_DEGREE.
213 LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = WEIGHTING_NONE);
214 virtual ~LeastSquaresVelocityTrackerStrategy();
215
Jeff Brown5912f952013-07-01 19:10:31 -0700216 virtual void clearPointers(BitSet32 idBits);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500217 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000218 const std::vector<float>& positions) override;
Jeff Brown5912f952013-07-01 19:10:31 -0700219 virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
220
221private:
222 // Sample horizon.
223 // We don't use too much history by default since we want to react to quick
224 // changes in direction.
225 static const nsecs_t HORIZON = 100 * 1000000; // 100 ms
226
227 // Number of samples to keep.
228 static const uint32_t HISTORY_SIZE = 20;
229
230 struct Movement {
231 nsecs_t eventTime;
232 BitSet32 idBits;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000233 float positions[MAX_POINTERS];
Jeff Brown5912f952013-07-01 19:10:31 -0700234
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000235 inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
Jeff Brown5912f952013-07-01 19:10:31 -0700236 };
237
238 float chooseWeight(uint32_t index) const;
239
240 const uint32_t mDegree;
241 const Weighting mWeighting;
242 uint32_t mIndex;
243 Movement mMovements[HISTORY_SIZE];
244};
245
246
247/*
248 * Velocity tracker algorithm that uses an IIR filter.
249 */
250class IntegratingVelocityTrackerStrategy : public VelocityTrackerStrategy {
251public:
252 // Degree must be 1 or 2.
253 IntegratingVelocityTrackerStrategy(uint32_t degree);
254 ~IntegratingVelocityTrackerStrategy();
255
Jeff Brown5912f952013-07-01 19:10:31 -0700256 virtual void clearPointers(BitSet32 idBits);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500257 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000258 const std::vector<float>& positions) override;
Jeff Brown5912f952013-07-01 19:10:31 -0700259 virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
260
261private:
262 // Current state estimate for a particular pointer.
263 struct State {
264 nsecs_t updateTime;
265 uint32_t degree;
266
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000267 float pos, vel, accel;
Jeff Brown5912f952013-07-01 19:10:31 -0700268 };
269
270 const uint32_t mDegree;
271 BitSet32 mPointerIdBits;
272 State mPointerState[MAX_POINTER_ID + 1];
273
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000274 void initState(State& state, nsecs_t eventTime, float pos) const;
275 void updateState(State& state, nsecs_t eventTime, float pos) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700276 void populateEstimator(const State& state, VelocityTracker::Estimator* outEstimator) const;
277};
278
279
280/*
281 * Velocity tracker strategy used prior to ICS.
282 */
283class LegacyVelocityTrackerStrategy : public VelocityTrackerStrategy {
284public:
285 LegacyVelocityTrackerStrategy();
286 virtual ~LegacyVelocityTrackerStrategy();
287
Jeff Brown5912f952013-07-01 19:10:31 -0700288 virtual void clearPointers(BitSet32 idBits);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500289 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000290 const std::vector<float>& positions) override;
Jeff Brown5912f952013-07-01 19:10:31 -0700291 virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
292
293private:
294 // Oldest sample to consider when calculating the velocity.
295 static const nsecs_t HORIZON = 200 * 1000000; // 100 ms
296
297 // Number of samples to keep.
298 static const uint32_t HISTORY_SIZE = 20;
299
300 // The minimum duration between samples when estimating velocity.
301 static const nsecs_t MIN_DURATION = 10 * 1000000; // 10 ms
302
303 struct Movement {
304 nsecs_t eventTime;
305 BitSet32 idBits;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000306 float positions[MAX_POINTERS];
Jeff Brown5912f952013-07-01 19:10:31 -0700307
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000308 inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
Jeff Brown5912f952013-07-01 19:10:31 -0700309 };
310
311 uint32_t mIndex;
312 Movement mMovements[HISTORY_SIZE];
313};
314
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100315class ImpulseVelocityTrackerStrategy : public VelocityTrackerStrategy {
316public:
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700317 ImpulseVelocityTrackerStrategy(bool deltaValues);
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100318 virtual ~ImpulseVelocityTrackerStrategy();
319
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100320 virtual void clearPointers(BitSet32 idBits);
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500321 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000322 const std::vector<float>& positions) override;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100323 virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
324
325private:
326 // Sample horizon.
327 // We don't use too much history by default since we want to react to quick
328 // changes in direction.
329 static constexpr nsecs_t HORIZON = 100 * 1000000; // 100 ms
330
331 // Number of samples to keep.
332 static constexpr size_t HISTORY_SIZE = 20;
333
334 struct Movement {
335 nsecs_t eventTime;
336 BitSet32 idBits;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000337 float positions[MAX_POINTERS];
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100338
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000339 inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100340 };
341
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700342 // Whether or not the input movement values for the strategy come in the form of delta values.
343 // If the input values are not deltas, the strategy needs to calculate deltas as part of its
344 // velocity calculation.
345 const bool mDeltaValues;
346
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100347 size_t mIndex;
348 Movement mMovements[HISTORY_SIZE];
349};
350
Jeff Brown5912f952013-07-01 19:10:31 -0700351} // namespace android