blob: ee7445544b89933cace9b26be5ef28a29d7425b8 [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
Siarhei Vishniakouf7436a12023-08-14 15:17:11 -070019#include <android/os/IInputConstants.h>
Jeff Brown5912f952013-07-01 19:10:31 -070020#include <input/Input.h>
Yeabkal Wubshit64f090f2023-03-03 17:35:11 -080021#include <input/RingBuffer.h>
Jeff Brown5912f952013-07-01 19:10:31 -070022#include <utils/BitSet.h>
Chris Yef8591482020-04-17 11:49:17 -070023#include <utils/Timers.h>
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000024#include <map>
25#include <set>
Jeff Brown5912f952013-07-01 19:10:31 -070026
27namespace android {
28
29class VelocityTrackerStrategy;
30
31/*
32 * Calculates the velocity of pointer movements over time.
33 */
34class VelocityTracker {
35public:
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -080036 static const size_t MAX_DEGREE = 4;
37
Chris Yef8591482020-04-17 11:49:17 -070038 enum class Strategy : int32_t {
Siarhei Vishniakouf7436a12023-08-14 15:17:11 -070039 DEFAULT = android::os::IInputConstants::VELOCITY_TRACKER_STRATEGY_DEFAULT,
40 IMPULSE = android::os::IInputConstants::VELOCITY_TRACKER_STRATEGY_IMPULSE,
41 LSQ1 = android::os::IInputConstants::VELOCITY_TRACKER_STRATEGY_LSQ1,
42 LSQ2 = android::os::IInputConstants::VELOCITY_TRACKER_STRATEGY_LSQ2,
43 LSQ3 = android::os::IInputConstants::VELOCITY_TRACKER_STRATEGY_LSQ3,
44 WLSQ2_DELTA = android::os::IInputConstants::VELOCITY_TRACKER_STRATEGY_WLSQ2_DELTA,
45 WLSQ2_CENTRAL = android::os::IInputConstants::VELOCITY_TRACKER_STRATEGY_WLSQ2_CENTRAL,
46 WLSQ2_RECENT = android::os::IInputConstants::VELOCITY_TRACKER_STRATEGY_WLSQ2_RECENT,
47 INT1 = android::os::IInputConstants::VELOCITY_TRACKER_STRATEGY_INT1,
48 INT2 = android::os::IInputConstants::VELOCITY_TRACKER_STRATEGY_INT2,
49 LEGACY = android::os::IInputConstants::VELOCITY_TRACKER_STRATEGY_LEGACY,
50 MIN = IMPULSE,
Chris Yef8591482020-04-17 11:49:17 -070051 MAX = LEGACY,
Siarhei Vishniakou8a2e5892023-08-14 13:59:40 -070052 ftl_last = LEGACY,
Chris Yef8591482020-04-17 11:49:17 -070053 };
54
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000055 /*
56 * Contains all available velocity data from a VelocityTracker.
57 */
58 struct ComputedVelocity {
Siarhei Vishniakou657a1732023-01-12 11:58:52 -080059 inline std::optional<float> getVelocity(int32_t axis, int32_t id) const {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000060 const auto& axisVelocities = mVelocities.find(axis);
61 if (axisVelocities == mVelocities.end()) {
62 return {};
63 }
64
65 const auto& axisIdVelocity = axisVelocities->second.find(id);
66 if (axisIdVelocity == axisVelocities->second.end()) {
67 return {};
68 }
69
70 return axisIdVelocity->second;
71 }
72
Siarhei Vishniakou657a1732023-01-12 11:58:52 -080073 inline void addVelocity(int32_t axis, int32_t id, float velocity) {
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000074 mVelocities[axis][id] = velocity;
75 }
76
77 private:
78 std::map<int32_t /*axis*/, std::map<int32_t /*pointerId*/, float /*velocity*/>> mVelocities;
79 };
80
81 // Creates a velocity tracker using the specified strategy for each supported axis.
Chris Yef8591482020-04-17 11:49:17 -070082 // If strategy is not provided, uses the default strategy for the platform.
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +000083 // TODO(b/32830165): support axis-specific strategies.
Chris Yef8591482020-04-17 11:49:17 -070084 VelocityTracker(const Strategy strategy = Strategy::DEFAULT);
Jeff Brown5912f952013-07-01 19:10:31 -070085
Yeabkal Wubshiteca273c2022-10-05 19:06:40 -070086 /** Return true if the axis is supported for velocity tracking, false otherwise. */
87 static bool isAxisSupported(int32_t axis);
88
Jeff Brown5912f952013-07-01 19:10:31 -070089 // Resets the velocity tracker state.
90 void clear();
91
Siarhei Vishniakou8d232032023-01-11 08:17:21 -080092 // Resets the velocity tracker state for a specific pointer.
Jeff Brown5912f952013-07-01 19:10:31 -070093 // Call this method when some pointers have changed and may be reusing
94 // an id that was assigned to a different pointer earlier.
Siarhei Vishniakou8d232032023-01-11 08:17:21 -080095 void clearPointer(int32_t pointerId);
Jeff Brown5912f952013-07-01 19:10:31 -070096
Siarhei Vishniakou8d232032023-01-11 08:17:21 -080097 // Adds movement information for a pointer for a specific axis
98 void addMovement(nsecs_t eventTime, int32_t pointerId, int32_t axis, float position);
Jeff Brown5912f952013-07-01 19:10:31 -070099
100 // Adds movement information for all pointers in a MotionEvent, including historical samples.
Siarhei Vishniakou318005c2023-10-24 18:05:59 -0700101 void addMovement(const MotionEvent& event);
Jeff Brown5912f952013-07-01 19:10:31 -0700102
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000103 // Returns the velocity of the specified pointer id and axis in position units per second.
104 // Returns empty optional if there is insufficient movement information for the pointer, or if
105 // the given axis is not supported for velocity tracking.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800106 std::optional<float> getVelocity(int32_t axis, int32_t pointerId) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700107
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000108 // Returns a ComputedVelocity instance with all available velocity data, using the given units
109 // (reference: units == 1 means "per millisecond"), and clamping each velocity between
110 // [-maxVelocity, maxVelocity], inclusive.
111 ComputedVelocity getComputedVelocity(int32_t units, float maxVelocity);
112
Jeff Brown5912f952013-07-01 19:10:31 -0700113 // Gets the active pointer id, or -1 if none.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800114 inline int32_t getActivePointerId() const { return mActivePointerId.value_or(-1); }
Jeff Brown5912f952013-07-01 19:10:31 -0700115
Jeff Brown5912f952013-07-01 19:10:31 -0700116private:
Jeff Brown5912f952013-07-01 19:10:31 -0700117 nsecs_t mLastEventTime;
118 BitSet32 mCurrentPointerIdBits;
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800119 std::optional<int32_t> mActivePointerId;
Jeff Brown5912f952013-07-01 19:10:31 -0700120
Yeabkal Wubshit47ff7082022-09-10 23:09:15 -0700121 // An override strategy passed in the constructor to be used for all axes.
122 // This strategy will apply to all axes, unless the default strategy is specified here.
123 // When default strategy is specified, then each axis will use a potentially different strategy
124 // based on a hardcoded mapping.
125 const Strategy mOverrideStrategy;
126 // Maps axes to their respective VelocityTrackerStrategy instances.
127 // Note that, only axes that have had MotionEvents (and not all supported axes) will be here.
128 std::map<int32_t /*axis*/, std::unique_ptr<VelocityTrackerStrategy>> mConfiguredStrategies;
129
130 void configureStrategy(int32_t axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700131
Yeabkal Wubshit73c95082022-09-22 10:12:33 +0000132 // Generates a VelocityTrackerStrategy instance for the given Strategy type.
133 // The `deltaValues` parameter indicates whether or not the created strategy should treat motion
134 // values as deltas (and not as absolute values). This the parameter is applicable only for
135 // strategies that support differential axes.
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700136 static std::unique_ptr<VelocityTrackerStrategy> createStrategy(const Strategy strategy,
Yeabkal Wubshit73c95082022-09-22 10:12:33 +0000137 bool deltaValues);
Jeff Brown5912f952013-07-01 19:10:31 -0700138};
139
140
141/*
142 * Implements a particular velocity tracker algorithm.
143 */
144class VelocityTrackerStrategy {
145protected:
146 VelocityTrackerStrategy() { }
147
148public:
149 virtual ~VelocityTrackerStrategy() { }
150
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800151 virtual void clearPointer(int32_t pointerId) = 0;
152 virtual void addMovement(nsecs_t eventTime, int32_t pointerId, float position) = 0;
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800153 virtual std::optional<float> getVelocity(int32_t pointerId) const = 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700154};
155
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800156/**
157 * A `VelocityTrackerStrategy` that accumulates added data points and processes the accumulated data
158 * points when getting velocity.
159 */
160class AccumulatingVelocityTrackerStrategy : public VelocityTrackerStrategy {
161public:
Yeabkal Wubshit4a678b22023-02-23 17:03:40 -0800162 AccumulatingVelocityTrackerStrategy(nsecs_t horizonNanos, bool maintainHorizonDuringAdd);
163
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800164 void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override;
165 void clearPointer(int32_t pointerId) override;
166
167protected:
168 struct Movement {
169 nsecs_t eventTime;
170 float position;
171 };
172
173 // Number of samples to keep.
174 // If different strategies would like to maintain different history size, we can make this a
175 // protected const field.
176 static constexpr uint32_t HISTORY_SIZE = 20;
177
Yeabkal Wubshit4a678b22023-02-23 17:03:40 -0800178 /**
179 * Duration, in nanoseconds, since the latest movement where a movement may be considered for
180 * velocity calculation.
181 */
182 const nsecs_t mHorizonNanos;
183 /**
184 * If true, data points outside of horizon (see `mHorizonNanos`) will be cleared after each
185 * addition of a new movement.
186 */
187 const bool mMaintainHorizonDuringAdd;
Yeabkal Wubshit64f090f2023-03-03 17:35:11 -0800188 std::map<int32_t /*pointerId*/, RingBuffer<Movement>> mMovements;
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800189};
Jeff Brown5912f952013-07-01 19:10:31 -0700190
191/*
192 * Velocity tracker algorithm based on least-squares linear regression.
193 */
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800194class LeastSquaresVelocityTrackerStrategy : public AccumulatingVelocityTrackerStrategy {
Jeff Brown5912f952013-07-01 19:10:31 -0700195public:
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800196 enum class Weighting {
Jeff Brown5912f952013-07-01 19:10:31 -0700197 // No weights applied. All data points are equally reliable.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800198 NONE,
Jeff Brown5912f952013-07-01 19:10:31 -0700199
200 // Weight by time delta. Data points clustered together are weighted less.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800201 DELTA,
Jeff Brown5912f952013-07-01 19:10:31 -0700202
203 // Weight such that points within a certain horizon are weighed more than those
204 // outside of that horizon.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800205 CENTRAL,
Jeff Brown5912f952013-07-01 19:10:31 -0700206
207 // Weight such that points older than a certain amount are weighed less.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800208 RECENT,
Jeff Brown5912f952013-07-01 19:10:31 -0700209 };
210
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800211 // Degree must be no greater than VelocityTracker::MAX_DEGREE.
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800212 LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = Weighting::NONE);
213 ~LeastSquaresVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700214
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800215 std::optional<float> getVelocity(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700216
217private:
218 // Sample horizon.
219 // We don't use too much history by default since we want to react to quick
220 // changes in direction.
221 static const nsecs_t HORIZON = 100 * 1000000; // 100 ms
222
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800223 float chooseWeight(int32_t pointerId, uint32_t index) const;
Yeabkal Wubshitfa806e42023-03-06 18:34:07 -0800224 /**
225 * An optimized least-squares solver for degree 2 and no weight (i.e. `Weighting.NONE`).
226 * The provided container of movements shall NOT be empty, and shall have the movements in
227 * chronological order.
228 */
229 std::optional<float> solveUnweightedLeastSquaresDeg2(
230 const RingBuffer<Movement>& movements) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700231
232 const uint32_t mDegree;
233 const Weighting mWeighting;
Jeff Brown5912f952013-07-01 19:10:31 -0700234};
235
Jeff Brown5912f952013-07-01 19:10:31 -0700236/*
237 * Velocity tracker algorithm that uses an IIR filter.
238 */
239class IntegratingVelocityTrackerStrategy : public VelocityTrackerStrategy {
240public:
241 // Degree must be 1 or 2.
242 IntegratingVelocityTrackerStrategy(uint32_t degree);
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800243 ~IntegratingVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700244
Siarhei Vishniakou8d232032023-01-11 08:17:21 -0800245 void clearPointer(int32_t pointerId) override;
246 void addMovement(nsecs_t eventTime, int32_t pointerId, float positions) override;
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800247 std::optional<float> getVelocity(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700248
249private:
250 // Current state estimate for a particular pointer.
251 struct State {
252 nsecs_t updateTime;
253 uint32_t degree;
254
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000255 float pos, vel, accel;
Jeff Brown5912f952013-07-01 19:10:31 -0700256 };
257
258 const uint32_t mDegree;
259 BitSet32 mPointerIdBits;
260 State mPointerState[MAX_POINTER_ID + 1];
261
Yeabkal Wubshit384ab0f2022-09-09 16:39:18 +0000262 void initState(State& state, nsecs_t eventTime, float pos) const;
263 void updateState(State& state, nsecs_t eventTime, float pos) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700264};
265
266
267/*
268 * Velocity tracker strategy used prior to ICS.
269 */
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800270class LegacyVelocityTrackerStrategy : public AccumulatingVelocityTrackerStrategy {
Jeff Brown5912f952013-07-01 19:10:31 -0700271public:
272 LegacyVelocityTrackerStrategy();
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800273 ~LegacyVelocityTrackerStrategy() override;
Jeff Brown5912f952013-07-01 19:10:31 -0700274
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800275 std::optional<float> getVelocity(int32_t pointerId) const override;
Jeff Brown5912f952013-07-01 19:10:31 -0700276
277private:
278 // Oldest sample to consider when calculating the velocity.
279 static const nsecs_t HORIZON = 200 * 1000000; // 100 ms
280
Jeff Brown5912f952013-07-01 19:10:31 -0700281 // The minimum duration between samples when estimating velocity.
282 static const nsecs_t MIN_DURATION = 10 * 1000000; // 10 ms
Jeff Brown5912f952013-07-01 19:10:31 -0700283};
284
Yeabkal Wubshita0e573c2023-03-02 21:08:14 -0800285class ImpulseVelocityTrackerStrategy : public AccumulatingVelocityTrackerStrategy {
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100286public:
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700287 ImpulseVelocityTrackerStrategy(bool deltaValues);
Siarhei Vishniakou657a1732023-01-12 11:58:52 -0800288 ~ImpulseVelocityTrackerStrategy() override;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100289
Yeabkal Wubshit9b4443f2023-02-23 23:35:07 -0800290 std::optional<float> getVelocity(int32_t pointerId) const override;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100291
292private:
293 // Sample horizon.
294 // We don't use too much history by default since we want to react to quick
295 // changes in direction.
296 static constexpr nsecs_t HORIZON = 100 * 1000000; // 100 ms
297
Yeabkal Wubshit0bb5e592022-09-14 01:22:28 -0700298 // Whether or not the input movement values for the strategy come in the form of delta values.
299 // If the input values are not deltas, the strategy needs to calculate deltas as part of its
300 // velocity calculation.
301 const bool mDeltaValues;
Siarhei Vishniakou00a4ea92017-06-08 21:43:20 +0100302};
303
Jeff Brown5912f952013-07-01 19:10:31 -0700304} // namespace android