blob: d733d3fb6591f8e9f9279bd478cb829b13a9eedd [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.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -080054 nsecs_t time = 0;
Jeff Brown5912f952013-07-01 19:10:31 -070055
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000056 // Polynomial coefficients describing motion.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -080057 std::array<float, MAX_DEGREE + 1> coeff{};
Jeff Brown5912f952013-07-01 19:10:31 -070058
59 // Polynomial degree (number of coefficients), or zero if no information is
60 // available.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -080061 uint32_t degree = 0;
Jeff Brown5912f952013-07-01 19:10:31 -070062
63 // Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit).
Siarhei Vishniakou657a1732023-01-12 11:58:52 -080064 float confidence = 0;
Jeff Brown5912f952013-07-01 19:10:31 -070065 };
66
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000067 /*
68 * Contains all available velocity data from a VelocityTracker.
69 */
70 struct ComputedVelocity {
Siarhei Vishniakou657a1732023-01-12 11:58:52 -080071 inline std::optional<float> getVelocity(int32_t axis, int32_t id) const {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000072 const auto& axisVelocities = mVelocities.find(axis);
73 if (axisVelocities == mVelocities.end()) {
74 return {};
75 }
76
77 const auto& axisIdVelocity = axisVelocities->second.find(id);
78 if (axisIdVelocity == axisVelocities->second.end()) {
79 return {};
80 }
81
82 return axisIdVelocity->second;
83 }
84
Siarhei Vishniakou657a1732023-01-12 11:58:52 -080085 inline void addVelocity(int32_t axis, int32_t id, float velocity) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000086 mVelocities[axis][id] = velocity;
87 }
88
89 private:
90 std::map<int32_t /*axis*/, std::map<int32_t /*pointerId*/, float /*velocity*/>> mVelocities;
91 };
92
93 // Creates a velocity tracker using the specified strategy for each supported axis.
Chris Yef8591482020-04-17 11:49:17 -070094 // If strategy is not provided, uses the default strategy for the platform.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000095 // TODO(b/32830165): support axis-specific strategies.
Chris Yef8591482020-04-17 11:49:17 -070096 VelocityTracker(const Strategy strategy = Strategy::DEFAULT);
Jeff Brown5912f952013-07-01 19:10:31 -070097
98 ~VelocityTracker();
99
Yeabkal Wubshiteca273c2022-10-05 19:06:40 -0700100 /** Return true if the axis is supported for velocity tracking, false otherwise. */
101 static bool isAxisSupported(int32_t axis);
102
Jeff Brown5912f952013-07-01 19:10:31 -0700103 // Resets the velocity tracker state.
104 void clear();
105
106 // Resets the velocity tracker state for specific pointers.
107 // Call this method when some pointers have changed and may be reusing
108 // an id that was assigned to a different pointer earlier.
109 void clearPointers(BitSet32 idBits);
110
111 // Adds movement information for a set of pointers.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000112 // The idBits bitfield specifies the pointer ids of the pointers whose data points
Jeff Brown5912f952013-07-01 19:10:31 -0700113 // are included in the movement.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000114 // The positions map contains a mapping of an axis to positions array.
115 // The positions arrays contain information for each pointer in order by increasing id.
116 // Each array's size should be equal to the number of one bits in idBits.
117 void addMovement(nsecs_t eventTime, BitSet32 idBits,
118 const std::map<int32_t, std::vector<float>>& positions);
Jeff Brown5912f952013-07-01 19:10:31 -0700119
120 // Adds movement information for all pointers in a MotionEvent, including historical samples.
121 void addMovement(const MotionEvent* event);
122
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000123 // Returns the velocity of the specified pointer id and axis in position units per second.
124 // Returns empty optional if there is insufficient movement information for the pointer, or if
125 // the given axis is not supported for velocity tracking.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800126 std::optional<float> getVelocity(int32_t axis, int32_t pointerId) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700127
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000128 // Returns a ComputedVelocity instance with all available velocity data, using the given units
129 // (reference: units == 1 means "per millisecond"), and clamping each velocity between
130 // [-maxVelocity, maxVelocity], inclusive.
131 ComputedVelocity getComputedVelocity(int32_t units, float maxVelocity);
132
133 // Gets an estimator for the recent movements of the specified pointer id for the given axis.
Jeff Brown5912f952013-07-01 19:10:31 -0700134 // Returns false and clears the estimator if there is no information available
135 // about the pointer.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800136 std::optional<Estimator> getEstimator(int32_t axis, int32_t pointerId) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700137
138 // Gets the active pointer id, or -1 if none.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800139 inline int32_t getActivePointerId() const { return mActivePointerId.value_or(-1); }
Jeff Brown5912f952013-07-01 19:10:31 -0700140
Jeff Brown5912f952013-07-01 19:10:31 -0700141private:
Jeff Brown5912f952013-07-01 19:10:31 -0700142 nsecs_t mLastEventTime;
143 BitSet32 mCurrentPointerIdBits;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800144 std::optional<int32_t> mActivePointerId;
Jeff Brown5912f952013-07-01 19:10:31 -0700145
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700146 // An override strategy passed in the constructor to be used for all axes.
147 // This strategy will apply to all axes, unless the default strategy is specified here.
148 // When default strategy is specified, then each axis will use a potentially different strategy
149 // based on a hardcoded mapping.
150 const Strategy mOverrideStrategy;
151 // Maps axes to their respective VelocityTrackerStrategy instances.
152 // Note that, only axes that have had MotionEvents (and not all supported axes) will be here.
153 std::map<int32_t /*axis*/, std::unique_ptr<VelocityTrackerStrategy>> mConfiguredStrategies;
154
155 void configureStrategy(int32_t axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700156
Yeabkal Wubshit73c95082022-09-22 10:12:33 +0000157 // Generates a VelocityTrackerStrategy instance for the given Strategy type.
158 // The `deltaValues` parameter indicates whether or not the created strategy should treat motion
159 // values as deltas (and not as absolute values). This the parameter is applicable only for
160 // strategies that support differential axes.
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700161 static std::unique_ptr<VelocityTrackerStrategy> createStrategy(const Strategy strategy,
Yeabkal Wubshit73c95082022-09-22 10:12:33 +0000162 bool deltaValues);
Jeff Brown5912f952013-07-01 19:10:31 -0700163};
164
165
166/*
167 * Implements a particular velocity tracker algorithm.
168 */
169class VelocityTrackerStrategy {
170protected:
171 VelocityTrackerStrategy() { }
172
173public:
174 virtual ~VelocityTrackerStrategy() { }
175
Jeff Brown5912f952013-07-01 19:10:31 -0700176 virtual void clearPointers(BitSet32 idBits) = 0;
177 virtual void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000178 const std::vector<float>& positions) = 0;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800179 virtual std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700180};
181
182
183/*
184 * Velocity tracker algorithm based on least-squares linear regression.
185 */
186class LeastSquaresVelocityTrackerStrategy : public VelocityTrackerStrategy {
187public:
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800188 enum class Weighting {
Jeff Brown5912f952013-07-01 19:10:31 -0700189 // No weights applied. All data points are equally reliable.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800190 NONE,
Jeff Brown5912f952013-07-01 19:10:31 -0700191
192 // Weight by time delta. Data points clustered together are weighted less.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800193 DELTA,
Jeff Brown5912f952013-07-01 19:10:31 -0700194
195 // Weight such that points within a certain horizon are weighed more than those
196 // outside of that horizon.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800197 CENTRAL,
Jeff Brown5912f952013-07-01 19:10:31 -0700198
199 // Weight such that points older than a certain amount are weighed less.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800200 RECENT,
Jeff Brown5912f952013-07-01 19:10:31 -0700201 };
202
203 // Degree must be no greater than Estimator::MAX_DEGREE.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800204 LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = Weighting::NONE);
205 ~LeastSquaresVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700206
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800207 void clearPointers(BitSet32 idBits) override;
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500208 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000209 const std::vector<float>& positions) override;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800210 std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700211
212private:
213 // Sample horizon.
214 // We don't use too much history by default since we want to react to quick
215 // changes in direction.
216 static const nsecs_t HORIZON = 100 * 1000000; // 100 ms
217
218 // Number of samples to keep.
219 static const uint32_t HISTORY_SIZE = 20;
220
221 struct Movement {
222 nsecs_t eventTime;
223 BitSet32 idBits;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000224 float positions[MAX_POINTERS];
Jeff Brown5912f952013-07-01 19:10:31 -0700225
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800226 inline float getPosition(int32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
Jeff Brown5912f952013-07-01 19:10:31 -0700227 };
228
229 float chooseWeight(uint32_t index) const;
230
231 const uint32_t mDegree;
232 const Weighting mWeighting;
233 uint32_t mIndex;
234 Movement mMovements[HISTORY_SIZE];
235};
236
237
238/*
239 * Velocity tracker algorithm that uses an IIR filter.
240 */
241class IntegratingVelocityTrackerStrategy : public VelocityTrackerStrategy {
242public:
243 // Degree must be 1 or 2.
244 IntegratingVelocityTrackerStrategy(uint32_t degree);
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800245 ~IntegratingVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700246
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800247 void clearPointers(BitSet32 idBits) override;
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500248 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000249 const std::vector<float>& positions) override;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800250 std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700251
252private:
253 // Current state estimate for a particular pointer.
254 struct State {
255 nsecs_t updateTime;
256 uint32_t degree;
257
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000258 float pos, vel, accel;
Jeff Brown5912f952013-07-01 19:10:31 -0700259 };
260
261 const uint32_t mDegree;
262 BitSet32 mPointerIdBits;
263 State mPointerState[MAX_POINTER_ID + 1];
264
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000265 void initState(State& state, nsecs_t eventTime, float pos) const;
266 void updateState(State& state, nsecs_t eventTime, float pos) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700267 void populateEstimator(const State& state, VelocityTracker::Estimator* outEstimator) const;
268};
269
270
271/*
272 * Velocity tracker strategy used prior to ICS.
273 */
274class LegacyVelocityTrackerStrategy : public VelocityTrackerStrategy {
275public:
276 LegacyVelocityTrackerStrategy();
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800277 ~LegacyVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700278
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800279 void clearPointers(BitSet32 idBits) override;
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500280 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000281 const std::vector<float>& positions) override;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800282 std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700283
284private:
285 // Oldest sample to consider when calculating the velocity.
286 static const nsecs_t HORIZON = 200 * 1000000; // 100 ms
287
288 // Number of samples to keep.
289 static const uint32_t HISTORY_SIZE = 20;
290
291 // The minimum duration between samples when estimating velocity.
292 static const nsecs_t MIN_DURATION = 10 * 1000000; // 10 ms
293
294 struct Movement {
295 nsecs_t eventTime;
296 BitSet32 idBits;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000297 float positions[MAX_POINTERS];
Jeff Brown5912f952013-07-01 19:10:31 -0700298
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800299 inline float getPosition(int32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
Jeff Brown5912f952013-07-01 19:10:31 -0700300 };
301
302 uint32_t mIndex;
303 Movement mMovements[HISTORY_SIZE];
304};
305
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100306class ImpulseVelocityTrackerStrategy : public VelocityTrackerStrategy {
307public:
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700308 ImpulseVelocityTrackerStrategy(bool deltaValues);
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800309 ~ImpulseVelocityTrackerStrategy() override;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100310
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800311 void clearPointers(BitSet32 idBits) override;
Siarhei Vishniakouae0f9902020-09-14 19:23:31 -0500312 void addMovement(nsecs_t eventTime, BitSet32 idBits,
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000313 const std::vector<float>& positions) override;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800314 std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100315
316private:
317 // Sample horizon.
318 // We don't use too much history by default since we want to react to quick
319 // changes in direction.
320 static constexpr nsecs_t HORIZON = 100 * 1000000; // 100 ms
321
322 // Number of samples to keep.
323 static constexpr size_t HISTORY_SIZE = 20;
324
325 struct Movement {
326 nsecs_t eventTime;
327 BitSet32 idBits;
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000328 float positions[MAX_POINTERS];
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100329
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800330 inline float getPosition(int32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100331 };
332
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700333 // Whether or not the input movement values for the strategy come in the form of delta values.
334 // If the input values are not deltas, the strategy needs to calculate deltas as part of its
335 // velocity calculation.
336 const bool mDeltaValues;
337
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100338 size_t mIndex;
339 Movement mMovements[HISTORY_SIZE];
340};
341
Jeff Brown5912f952013-07-01 19:10:31 -0700342} // namespace android