blob: 70d503d782ba00de9bf16ea8d0ecc6ca0c1ccf78 [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>
Yeabkal Wubshit64f090f2023-03-03 17:35:11 -080020#include <input/RingBuffer.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:
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -080035 static const size_t MAX_DEGREE = 4;
36
Chris Yef8591482020-04-17 11:49:17 -070037 enum class Strategy : int32_t {
38 DEFAULT = -1,
39 MIN = 0,
40 IMPULSE = 0,
41 LSQ1 = 1,
42 LSQ2 = 2,
43 LSQ3 = 3,
44 WLSQ2_DELTA = 4,
45 WLSQ2_CENTRAL = 5,
46 WLSQ2_RECENT = 6,
47 INT1 = 7,
48 INT2 = 8,
49 LEGACY = 9,
50 MAX = LEGACY,
Siarhei Vishniakou8a2e5892023-08-14 13:59:40 -070051 ftl_last = LEGACY,
Chris Yef8591482020-04-17 11:49:17 -070052 };
53
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000054 /*
55 * Contains all available velocity data from a VelocityTracker.
56 */
57 struct ComputedVelocity {
Siarhei Vishniakou657a1732023-01-12 11:58:52 -080058 inline std::optional<float> getVelocity(int32_t axis, int32_t id) const {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000059 const auto& axisVelocities = mVelocities.find(axis);
60 if (axisVelocities == mVelocities.end()) {
61 return {};
62 }
63
64 const auto& axisIdVelocity = axisVelocities->second.find(id);
65 if (axisIdVelocity == axisVelocities->second.end()) {
66 return {};
67 }
68
69 return axisIdVelocity->second;
70 }
71
Siarhei Vishniakou657a1732023-01-12 11:58:52 -080072 inline void addVelocity(int32_t axis, int32_t id, float velocity) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000073 mVelocities[axis][id] = velocity;
74 }
75
76 private:
77 std::map<int32_t /*axis*/, std::map<int32_t /*pointerId*/, float /*velocity*/>> mVelocities;
78 };
79
80 // Creates a velocity tracker using the specified strategy for each supported axis.
Chris Yef8591482020-04-17 11:49:17 -070081 // If strategy is not provided, uses the default strategy for the platform.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000082 // TODO(b/32830165): support axis-specific strategies.
Chris Yef8591482020-04-17 11:49:17 -070083 VelocityTracker(const Strategy strategy = Strategy::DEFAULT);
Jeff Brown5912f952013-07-01 19:10:31 -070084
Yeabkal Wubshiteca273c2022-10-05 19:06:40 -070085 /** Return true if the axis is supported for velocity tracking, false otherwise. */
86 static bool isAxisSupported(int32_t axis);
87
Jeff Brown5912f952013-07-01 19:10:31 -070088 // Resets the velocity tracker state.
89 void clear();
90
Siarhei Vishniakou8d232032023-01-11 08:17:21 -080091 // Resets the velocity tracker state for a specific pointer.
Jeff Brown5912f952013-07-01 19:10:31 -070092 // Call this method when some pointers have changed and may be reusing
93 // an id that was assigned to a different pointer earlier.
Siarhei Vishniakou8d232032023-01-11 08:17:21 -080094 void clearPointer(int32_t pointerId);
Jeff Brown5912f952013-07-01 19:10:31 -070095
Siarhei Vishniakou8d232032023-01-11 08:17:21 -080096 // Adds movement information for a pointer for a specific axis
97 void addMovement(nsecs_t eventTime, int32_t pointerId, int32_t axis, float position);
Jeff Brown5912f952013-07-01 19:10:31 -070098
99 // Adds movement information for all pointers in a MotionEvent, including historical samples.
100 void addMovement(const MotionEvent* event);
101
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000102 // Returns the velocity of the specified pointer id and axis in position units per second.
103 // Returns empty optional if there is insufficient movement information for the pointer, or if
104 // the given axis is not supported for velocity tracking.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800105 std::optional<float> getVelocity(int32_t axis, int32_t pointerId) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700106
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000107 // Returns a ComputedVelocity instance with all available velocity data, using the given units
108 // (reference: units == 1 means "per millisecond"), and clamping each velocity between
109 // [-maxVelocity, maxVelocity], inclusive.
110 ComputedVelocity getComputedVelocity(int32_t units, float maxVelocity);
111
Jeff Brown5912f952013-07-01 19:10:31 -0700112 // Gets the active pointer id, or -1 if none.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800113 inline int32_t getActivePointerId() const { return mActivePointerId.value_or(-1); }
Jeff Brown5912f952013-07-01 19:10:31 -0700114
Jeff Brown5912f952013-07-01 19:10:31 -0700115private:
Jeff Brown5912f952013-07-01 19:10:31 -0700116 nsecs_t mLastEventTime;
117 BitSet32 mCurrentPointerIdBits;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800118 std::optional<int32_t> mActivePointerId;
Jeff Brown5912f952013-07-01 19:10:31 -0700119
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700120 // An override strategy passed in the constructor to be used for all axes.
121 // This strategy will apply to all axes, unless the default strategy is specified here.
122 // When default strategy is specified, then each axis will use a potentially different strategy
123 // based on a hardcoded mapping.
124 const Strategy mOverrideStrategy;
125 // Maps axes to their respective VelocityTrackerStrategy instances.
126 // Note that, only axes that have had MotionEvents (and not all supported axes) will be here.
127 std::map<int32_t /*axis*/, std::unique_ptr<VelocityTrackerStrategy>> mConfiguredStrategies;
128
129 void configureStrategy(int32_t axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700130
Yeabkal Wubshit73c95082022-09-22 10:12:33 +0000131 // Generates a VelocityTrackerStrategy instance for the given Strategy type.
132 // The `deltaValues` parameter indicates whether or not the created strategy should treat motion
133 // values as deltas (and not as absolute values). This the parameter is applicable only for
134 // strategies that support differential axes.
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700135 static std::unique_ptr<VelocityTrackerStrategy> createStrategy(const Strategy strategy,
Yeabkal Wubshit73c95082022-09-22 10:12:33 +0000136 bool deltaValues);
Jeff Brown5912f952013-07-01 19:10:31 -0700137};
138
139
140/*
141 * Implements a particular velocity tracker algorithm.
142 */
143class VelocityTrackerStrategy {
144protected:
145 VelocityTrackerStrategy() { }
146
147public:
148 virtual ~VelocityTrackerStrategy() { }
149
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800150 virtual void clearPointer(int32_t pointerId) = 0;
151 virtual void addMovement(nsecs_t eventTime, int32_t pointerId, float position) = 0;
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800152 virtual std::optional<float> getVelocity(int32_t pointerId) const = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700153};
154
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800155/**
156 * A `VelocityTrackerStrategy` that accumulates added data points and processes the accumulated data
157 * points when getting velocity.
158 */
159class AccumulatingVelocityTrackerStrategy : public VelocityTrackerStrategy {
160public:
Yeabkal Wubshit4a678b22023-02-23 17:03:40 -0800161 AccumulatingVelocityTrackerStrategy(nsecs_t horizonNanos, bool maintainHorizonDuringAdd);
162
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800163 void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override;
164 void clearPointer(int32_t pointerId) override;
165
166protected:
167 struct Movement {
168 nsecs_t eventTime;
169 float position;
170 };
171
172 // Number of samples to keep.
173 // If different strategies would like to maintain different history size, we can make this a
174 // protected const field.
175 static constexpr uint32_t HISTORY_SIZE = 20;
176
Yeabkal Wubshit4a678b22023-02-23 17:03:40 -0800177 /**
178 * Duration, in nanoseconds, since the latest movement where a movement may be considered for
179 * velocity calculation.
180 */
181 const nsecs_t mHorizonNanos;
182 /**
183 * If true, data points outside of horizon (see `mHorizonNanos`) will be cleared after each
184 * addition of a new movement.
185 */
186 const bool mMaintainHorizonDuringAdd;
Yeabkal Wubshit64f090f2023-03-03 17:35:11 -0800187 std::map<int32_t /*pointerId*/, RingBuffer<Movement>> mMovements;
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800188};
Jeff Brown5912f952013-07-01 19:10:31 -0700189
190/*
191 * Velocity tracker algorithm based on least-squares linear regression.
192 */
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800193class LeastSquaresVelocityTrackerStrategy : public AccumulatingVelocityTrackerStrategy {
Jeff Brown5912f952013-07-01 19:10:31 -0700194public:
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800195 enum class Weighting {
Jeff Brown5912f952013-07-01 19:10:31 -0700196 // No weights applied. All data points are equally reliable.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800197 NONE,
Jeff Brown5912f952013-07-01 19:10:31 -0700198
199 // Weight by time delta. Data points clustered together are weighted less.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800200 DELTA,
Jeff Brown5912f952013-07-01 19:10:31 -0700201
202 // Weight such that points within a certain horizon are weighed more than those
203 // outside of that horizon.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800204 CENTRAL,
Jeff Brown5912f952013-07-01 19:10:31 -0700205
206 // Weight such that points older than a certain amount are weighed less.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800207 RECENT,
Jeff Brown5912f952013-07-01 19:10:31 -0700208 };
209
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800210 // Degree must be no greater than VelocityTracker::MAX_DEGREE.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800211 LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = Weighting::NONE);
212 ~LeastSquaresVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700213
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800214 std::optional<float> getVelocity(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700215
216private:
217 // Sample horizon.
218 // We don't use too much history by default since we want to react to quick
219 // changes in direction.
220 static const nsecs_t HORIZON = 100 * 1000000; // 100 ms
221
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800222 float chooseWeight(int32_t pointerId, uint32_t index) const;
Yeabkal Wubshitfa806e42023-03-06 18:34:07 -0800223 /**
224 * An optimized least-squares solver for degree 2 and no weight (i.e. `Weighting.NONE`).
225 * The provided container of movements shall NOT be empty, and shall have the movements in
226 * chronological order.
227 */
228 std::optional<float> solveUnweightedLeastSquaresDeg2(
229 const RingBuffer<Movement>& movements) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700230
231 const uint32_t mDegree;
232 const Weighting mWeighting;
Jeff Brown5912f952013-07-01 19:10:31 -0700233};
234
Jeff Brown5912f952013-07-01 19:10:31 -0700235/*
236 * Velocity tracker algorithm that uses an IIR filter.
237 */
238class IntegratingVelocityTrackerStrategy : public VelocityTrackerStrategy {
239public:
240 // Degree must be 1 or 2.
241 IntegratingVelocityTrackerStrategy(uint32_t degree);
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800242 ~IntegratingVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700243
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800244 void clearPointer(int32_t pointerId) override;
245 void addMovement(nsecs_t eventTime, int32_t pointerId, float positions) override;
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800246 std::optional<float> getVelocity(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700247
248private:
249 // Current state estimate for a particular pointer.
250 struct State {
251 nsecs_t updateTime;
252 uint32_t degree;
253
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000254 float pos, vel, accel;
Jeff Brown5912f952013-07-01 19:10:31 -0700255 };
256
257 const uint32_t mDegree;
258 BitSet32 mPointerIdBits;
259 State mPointerState[MAX_POINTER_ID + 1];
260
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000261 void initState(State& state, nsecs_t eventTime, float pos) const;
262 void updateState(State& state, nsecs_t eventTime, float pos) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700263};
264
265
266/*
267 * Velocity tracker strategy used prior to ICS.
268 */
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800269class LegacyVelocityTrackerStrategy : public AccumulatingVelocityTrackerStrategy {
Jeff Brown5912f952013-07-01 19:10:31 -0700270public:
271 LegacyVelocityTrackerStrategy();
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800272 ~LegacyVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700273
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800274 std::optional<float> getVelocity(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700275
276private:
277 // Oldest sample to consider when calculating the velocity.
278 static const nsecs_t HORIZON = 200 * 1000000; // 100 ms
279
Jeff Brown5912f952013-07-01 19:10:31 -0700280 // The minimum duration between samples when estimating velocity.
281 static const nsecs_t MIN_DURATION = 10 * 1000000; // 10 ms
Jeff Brown5912f952013-07-01 19:10:31 -0700282};
283
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800284class ImpulseVelocityTrackerStrategy : public AccumulatingVelocityTrackerStrategy {
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100285public:
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700286 ImpulseVelocityTrackerStrategy(bool deltaValues);
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800287 ~ImpulseVelocityTrackerStrategy() override;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100288
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800289 std::optional<float> getVelocity(int32_t pointerId) const override;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100290
291private:
292 // Sample horizon.
293 // We don't use too much history by default since we want to react to quick
294 // changes in direction.
295 static constexpr nsecs_t HORIZON = 100 * 1000000; // 100 ms
296
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700297 // Whether or not the input movement values for the strategy come in the form of delta values.
298 // If the input values are not deltas, the strategy needs to calculate deltas as part of its
299 // velocity calculation.
300 const bool mDeltaValues;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100301};
302
Jeff Brown5912f952013-07-01 19:10:31 -0700303} // namespace android