blob: da97c3e85562ddeeb4c3bed8c0f4163c90b05c41 [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
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800106 // Resets the velocity tracker state for a specific pointer.
Jeff Brown5912f952013-07-01 19:10:31 -0700107 // Call this method when some pointers have changed and may be reusing
108 // an id that was assigned to a different pointer earlier.
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800109 void clearPointer(int32_t pointerId);
Jeff Brown5912f952013-07-01 19:10:31 -0700110
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800111 // Adds movement information for a pointer for a specific axis
112 void addMovement(nsecs_t eventTime, int32_t pointerId, int32_t axis, float position);
Jeff Brown5912f952013-07-01 19:10:31 -0700113
114 // Adds movement information for all pointers in a MotionEvent, including historical samples.
115 void addMovement(const MotionEvent* event);
116
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000117 // Returns the velocity of the specified pointer id and axis in position units per second.
118 // Returns empty optional if there is insufficient movement information for the pointer, or if
119 // the given axis is not supported for velocity tracking.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800120 std::optional<float> getVelocity(int32_t axis, int32_t pointerId) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700121
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000122 // Returns a ComputedVelocity instance with all available velocity data, using the given units
123 // (reference: units == 1 means "per millisecond"), and clamping each velocity between
124 // [-maxVelocity, maxVelocity], inclusive.
125 ComputedVelocity getComputedVelocity(int32_t units, float maxVelocity);
126
127 // Gets an estimator for the recent movements of the specified pointer id for the given axis.
Jeff Brown5912f952013-07-01 19:10:31 -0700128 // Returns false and clears the estimator if there is no information available
129 // about the pointer.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800130 std::optional<Estimator> getEstimator(int32_t axis, int32_t pointerId) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700131
132 // Gets the active pointer id, or -1 if none.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800133 inline int32_t getActivePointerId() const { return mActivePointerId.value_or(-1); }
Jeff Brown5912f952013-07-01 19:10:31 -0700134
Jeff Brown5912f952013-07-01 19:10:31 -0700135private:
Jeff Brown5912f952013-07-01 19:10:31 -0700136 nsecs_t mLastEventTime;
137 BitSet32 mCurrentPointerIdBits;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800138 std::optional<int32_t> mActivePointerId;
Jeff Brown5912f952013-07-01 19:10:31 -0700139
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700140 // An override strategy passed in the constructor to be used for all axes.
141 // This strategy will apply to all axes, unless the default strategy is specified here.
142 // When default strategy is specified, then each axis will use a potentially different strategy
143 // based on a hardcoded mapping.
144 const Strategy mOverrideStrategy;
145 // Maps axes to their respective VelocityTrackerStrategy instances.
146 // Note that, only axes that have had MotionEvents (and not all supported axes) will be here.
147 std::map<int32_t /*axis*/, std::unique_ptr<VelocityTrackerStrategy>> mConfiguredStrategies;
148
149 void configureStrategy(int32_t axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700150
Yeabkal Wubshit73c95082022-09-22 10:12:33 +0000151 // Generates a VelocityTrackerStrategy instance for the given Strategy type.
152 // The `deltaValues` parameter indicates whether or not the created strategy should treat motion
153 // values as deltas (and not as absolute values). This the parameter is applicable only for
154 // strategies that support differential axes.
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700155 static std::unique_ptr<VelocityTrackerStrategy> createStrategy(const Strategy strategy,
Yeabkal Wubshit73c95082022-09-22 10:12:33 +0000156 bool deltaValues);
Jeff Brown5912f952013-07-01 19:10:31 -0700157};
158
159
160/*
161 * Implements a particular velocity tracker algorithm.
162 */
163class VelocityTrackerStrategy {
164protected:
165 VelocityTrackerStrategy() { }
166
167public:
168 virtual ~VelocityTrackerStrategy() { }
169
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800170 virtual void clearPointer(int32_t pointerId) = 0;
171 virtual void addMovement(nsecs_t eventTime, int32_t pointerId, float position) = 0;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800172 virtual std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700173};
174
175
176/*
177 * Velocity tracker algorithm based on least-squares linear regression.
178 */
179class LeastSquaresVelocityTrackerStrategy : public VelocityTrackerStrategy {
180public:
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800181 enum class Weighting {
Jeff Brown5912f952013-07-01 19:10:31 -0700182 // No weights applied. All data points are equally reliable.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800183 NONE,
Jeff Brown5912f952013-07-01 19:10:31 -0700184
185 // Weight by time delta. Data points clustered together are weighted less.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800186 DELTA,
Jeff Brown5912f952013-07-01 19:10:31 -0700187
188 // Weight such that points within a certain horizon are weighed more than those
189 // outside of that horizon.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800190 CENTRAL,
Jeff Brown5912f952013-07-01 19:10:31 -0700191
192 // Weight such that points older than a certain amount are weighed less.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800193 RECENT,
Jeff Brown5912f952013-07-01 19:10:31 -0700194 };
195
196 // Degree must be no greater than Estimator::MAX_DEGREE.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800197 LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = Weighting::NONE);
198 ~LeastSquaresVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700199
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800200 void clearPointer(int32_t pointerId) override;
201 void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800202 std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700203
204private:
205 // Sample horizon.
206 // We don't use too much history by default since we want to react to quick
207 // changes in direction.
208 static const nsecs_t HORIZON = 100 * 1000000; // 100 ms
209
210 // Number of samples to keep.
211 static const uint32_t HISTORY_SIZE = 20;
212
213 struct Movement {
214 nsecs_t eventTime;
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800215 float position;
Jeff Brown5912f952013-07-01 19:10:31 -0700216 };
217
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800218 float chooseWeight(int32_t pointerId, uint32_t index) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700219
220 const uint32_t mDegree;
221 const Weighting mWeighting;
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800222 std::map<int32_t /*pointerId*/, size_t /*positionInArray*/> mIndex;
223 std::map<int32_t /*pointerId*/, std::array<Movement, HISTORY_SIZE>> mMovements;
Jeff Brown5912f952013-07-01 19:10:31 -0700224};
225
226
227/*
228 * Velocity tracker algorithm that uses an IIR filter.
229 */
230class IntegratingVelocityTrackerStrategy : public VelocityTrackerStrategy {
231public:
232 // Degree must be 1 or 2.
233 IntegratingVelocityTrackerStrategy(uint32_t degree);
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800234 ~IntegratingVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700235
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800236 void clearPointer(int32_t pointerId) override;
237 void addMovement(nsecs_t eventTime, int32_t pointerId, float positions) override;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800238 std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700239
240private:
241 // Current state estimate for a particular pointer.
242 struct State {
243 nsecs_t updateTime;
244 uint32_t degree;
245
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000246 float pos, vel, accel;
Jeff Brown5912f952013-07-01 19:10:31 -0700247 };
248
249 const uint32_t mDegree;
250 BitSet32 mPointerIdBits;
251 State mPointerState[MAX_POINTER_ID + 1];
252
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000253 void initState(State& state, nsecs_t eventTime, float pos) const;
254 void updateState(State& state, nsecs_t eventTime, float pos) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700255 void populateEstimator(const State& state, VelocityTracker::Estimator* outEstimator) const;
256};
257
258
259/*
260 * Velocity tracker strategy used prior to ICS.
261 */
262class LegacyVelocityTrackerStrategy : public VelocityTrackerStrategy {
263public:
264 LegacyVelocityTrackerStrategy();
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800265 ~LegacyVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700266
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800267 void clearPointer(int32_t pointerId) override;
268 void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800269 std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700270
271private:
272 // Oldest sample to consider when calculating the velocity.
273 static const nsecs_t HORIZON = 200 * 1000000; // 100 ms
274
275 // Number of samples to keep.
276 static const uint32_t HISTORY_SIZE = 20;
277
278 // The minimum duration between samples when estimating velocity.
279 static const nsecs_t MIN_DURATION = 10 * 1000000; // 10 ms
280
281 struct Movement {
282 nsecs_t eventTime;
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800283 float position;
Jeff Brown5912f952013-07-01 19:10:31 -0700284 };
285
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800286 std::map<int32_t /*pointerId*/, size_t /*positionInArray*/> mIndex;
287 std::map<int32_t /*pointerId*/, std::array<Movement, HISTORY_SIZE>> mMovements;
Jeff Brown5912f952013-07-01 19:10:31 -0700288};
289
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100290class ImpulseVelocityTrackerStrategy : public VelocityTrackerStrategy {
291public:
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700292 ImpulseVelocityTrackerStrategy(bool deltaValues);
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800293 ~ImpulseVelocityTrackerStrategy() override;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100294
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800295 void clearPointer(int32_t pointerId) override;
296 void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800297 std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100298
299private:
300 // Sample horizon.
301 // We don't use too much history by default since we want to react to quick
302 // changes in direction.
303 static constexpr nsecs_t HORIZON = 100 * 1000000; // 100 ms
304
305 // Number of samples to keep.
306 static constexpr size_t HISTORY_SIZE = 20;
307
308 struct Movement {
309 nsecs_t eventTime;
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800310 float position;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100311 };
312
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700313 // Whether or not the input movement values for the strategy come in the form of delta values.
314 // If the input values are not deltas, the strategy needs to calculate deltas as part of its
315 // velocity calculation.
316 const bool mDeltaValues;
317
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800318 std::map<int32_t /*pointerId*/, size_t /*positionInArray*/> mIndex;
319 std::map<int32_t /*pointerId*/, std::array<Movement, HISTORY_SIZE>> mMovements;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100320};
321
Jeff Brown5912f952013-07-01 19:10:31 -0700322} // namespace android