Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #ifndef _LIBINPUT_VELOCITY_TRACKER_H |
| 18 | #define _LIBINPUT_VELOCITY_TRACKER_H |
| 19 | |
| 20 | #include <input/Input.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 21 | #include <utils/BitSet.h> |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 22 | #include <utils/Timers.h> |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 23 | #include <map> |
| 24 | #include <set> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | class VelocityTrackerStrategy; |
| 29 | |
| 30 | /* |
| 31 | * Calculates the velocity of pointer movements over time. |
| 32 | */ |
| 33 | class VelocityTracker { |
| 34 | public: |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 35 | enum class Strategy : int32_t { |
| 36 | DEFAULT = -1, |
| 37 | MIN = 0, |
| 38 | IMPULSE = 0, |
| 39 | LSQ1 = 1, |
| 40 | LSQ2 = 2, |
| 41 | LSQ3 = 3, |
| 42 | WLSQ2_DELTA = 4, |
| 43 | WLSQ2_CENTRAL = 5, |
| 44 | WLSQ2_RECENT = 6, |
| 45 | INT1 = 7, |
| 46 | INT2 = 8, |
| 47 | LEGACY = 9, |
| 48 | MAX = LEGACY, |
| 49 | }; |
| 50 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 51 | struct Estimator { |
| 52 | static const size_t MAX_DEGREE = 4; |
| 53 | |
| 54 | // Estimator time base. |
| 55 | nsecs_t time; |
| 56 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 57 | // Polynomial coefficients describing motion. |
| 58 | float coeff[MAX_DEGREE + 1]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 59 | |
| 60 | // Polynomial degree (number of coefficients), or zero if no information is |
| 61 | // available. |
| 62 | uint32_t degree; |
| 63 | |
| 64 | // Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit). |
| 65 | float confidence; |
| 66 | |
| 67 | inline void clear() { |
| 68 | time = 0; |
| 69 | degree = 0; |
| 70 | confidence = 0; |
| 71 | for (size_t i = 0; i <= MAX_DEGREE; i++) { |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 72 | coeff[i] = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | }; |
| 76 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 77 | /* |
| 78 | * Contains all available velocity data from a VelocityTracker. |
| 79 | */ |
| 80 | struct ComputedVelocity { |
| 81 | inline std::optional<float> getVelocity(int32_t axis, uint32_t id) const { |
| 82 | const auto& axisVelocities = mVelocities.find(axis); |
| 83 | if (axisVelocities == mVelocities.end()) { |
| 84 | return {}; |
| 85 | } |
| 86 | |
| 87 | const auto& axisIdVelocity = axisVelocities->second.find(id); |
| 88 | if (axisIdVelocity == axisVelocities->second.end()) { |
| 89 | return {}; |
| 90 | } |
| 91 | |
| 92 | return axisIdVelocity->second; |
| 93 | } |
| 94 | |
| 95 | inline void addVelocity(int32_t axis, uint32_t id, float velocity) { |
| 96 | mVelocities[axis][id] = velocity; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | std::map<int32_t /*axis*/, std::map<int32_t /*pointerId*/, float /*velocity*/>> mVelocities; |
| 101 | }; |
| 102 | |
| 103 | // Creates a velocity tracker using the specified strategy for each supported axis. |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 104 | // If strategy is not provided, uses the default strategy for the platform. |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 105 | // TODO(b/32830165): support axis-specific strategies. |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 106 | VelocityTracker(const Strategy strategy = Strategy::DEFAULT); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 107 | |
| 108 | ~VelocityTracker(); |
| 109 | |
| 110 | // Resets the velocity tracker state. |
| 111 | void clear(); |
| 112 | |
| 113 | // Resets the velocity tracker state for specific pointers. |
| 114 | // Call this method when some pointers have changed and may be reusing |
| 115 | // an id that was assigned to a different pointer earlier. |
| 116 | void clearPointers(BitSet32 idBits); |
| 117 | |
| 118 | // Adds movement information for a set of pointers. |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 119 | // The idBits bitfield specifies the pointer ids of the pointers whose data points |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 120 | // are included in the movement. |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 121 | // The positions map contains a mapping of an axis to positions array. |
| 122 | // The positions arrays contain information for each pointer in order by increasing id. |
| 123 | // Each array's size should be equal to the number of one bits in idBits. |
| 124 | void addMovement(nsecs_t eventTime, BitSet32 idBits, |
| 125 | const std::map<int32_t, std::vector<float>>& positions); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 126 | |
| 127 | // Adds movement information for all pointers in a MotionEvent, including historical samples. |
| 128 | void addMovement(const MotionEvent* event); |
| 129 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 130 | // Returns the velocity of the specified pointer id and axis in position units per second. |
| 131 | // Returns empty optional if there is insufficient movement information for the pointer, or if |
| 132 | // the given axis is not supported for velocity tracking. |
| 133 | std::optional<float> getVelocity(int32_t axis, uint32_t id) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 134 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 135 | // Returns a ComputedVelocity instance with all available velocity data, using the given units |
| 136 | // (reference: units == 1 means "per millisecond"), and clamping each velocity between |
| 137 | // [-maxVelocity, maxVelocity], inclusive. |
| 138 | ComputedVelocity getComputedVelocity(int32_t units, float maxVelocity); |
| 139 | |
| 140 | // Gets an estimator for the recent movements of the specified pointer id for the given axis. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 141 | // Returns false and clears the estimator if there is no information available |
| 142 | // about the pointer. |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 143 | bool getEstimator(int32_t axis, uint32_t id, Estimator* outEstimator) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 144 | |
| 145 | // Gets the active pointer id, or -1 if none. |
| 146 | inline int32_t getActivePointerId() const { return mActivePointerId; } |
| 147 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 148 | private: |
Yeabkal Wubshit | 47ff708 | 2022-09-10 23:09:15 -0700 | [diff] [blame^] | 149 | // All axes supported for velocity tracking, mapped to their default strategies. |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 150 | // Although other strategies are available for testing and comparison purposes, |
Yeabkal Wubshit | 47ff708 | 2022-09-10 23:09:15 -0700 | [diff] [blame^] | 151 | // the default strategy is the one that applications will actually use. Be very careful |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 152 | // when adjusting the default strategy because it can dramatically affect |
| 153 | // (often in a bad way) the user experience. |
Yeabkal Wubshit | 47ff708 | 2022-09-10 23:09:15 -0700 | [diff] [blame^] | 154 | static const std::map<int32_t, Strategy> DEFAULT_STRATEGY_BY_AXIS; |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 155 | |
| 156 | // Axes specifying location on a 2D plane (i.e. X and Y). |
| 157 | static const std::set<int32_t> PLANAR_AXES; |
| 158 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 159 | nsecs_t mLastEventTime; |
| 160 | BitSet32 mCurrentPointerIdBits; |
| 161 | int32_t mActivePointerId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 162 | |
Yeabkal Wubshit | 47ff708 | 2022-09-10 23:09:15 -0700 | [diff] [blame^] | 163 | // An override strategy passed in the constructor to be used for all axes. |
| 164 | // This strategy will apply to all axes, unless the default strategy is specified here. |
| 165 | // When default strategy is specified, then each axis will use a potentially different strategy |
| 166 | // based on a hardcoded mapping. |
| 167 | const Strategy mOverrideStrategy; |
| 168 | // Maps axes to their respective VelocityTrackerStrategy instances. |
| 169 | // Note that, only axes that have had MotionEvents (and not all supported axes) will be here. |
| 170 | std::map<int32_t /*axis*/, std::unique_ptr<VelocityTrackerStrategy>> mConfiguredStrategies; |
| 171 | |
| 172 | void configureStrategy(int32_t axis); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 173 | |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 174 | static std::unique_ptr<VelocityTrackerStrategy> createStrategy(const Strategy strategy); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | |
| 178 | /* |
| 179 | * Implements a particular velocity tracker algorithm. |
| 180 | */ |
| 181 | class VelocityTrackerStrategy { |
| 182 | protected: |
| 183 | VelocityTrackerStrategy() { } |
| 184 | |
| 185 | public: |
| 186 | virtual ~VelocityTrackerStrategy() { } |
| 187 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 188 | virtual void clearPointers(BitSet32 idBits) = 0; |
| 189 | virtual void addMovement(nsecs_t eventTime, BitSet32 idBits, |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 190 | const std::vector<float>& positions) = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 191 | virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const = 0; |
| 192 | }; |
| 193 | |
| 194 | |
| 195 | /* |
| 196 | * Velocity tracker algorithm based on least-squares linear regression. |
| 197 | */ |
| 198 | class LeastSquaresVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 199 | public: |
| 200 | enum Weighting { |
| 201 | // No weights applied. All data points are equally reliable. |
| 202 | WEIGHTING_NONE, |
| 203 | |
| 204 | // Weight by time delta. Data points clustered together are weighted less. |
| 205 | WEIGHTING_DELTA, |
| 206 | |
| 207 | // Weight such that points within a certain horizon are weighed more than those |
| 208 | // outside of that horizon. |
| 209 | WEIGHTING_CENTRAL, |
| 210 | |
| 211 | // Weight such that points older than a certain amount are weighed less. |
| 212 | WEIGHTING_RECENT, |
| 213 | }; |
| 214 | |
| 215 | // Degree must be no greater than Estimator::MAX_DEGREE. |
| 216 | LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = WEIGHTING_NONE); |
| 217 | virtual ~LeastSquaresVelocityTrackerStrategy(); |
| 218 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 219 | virtual void clearPointers(BitSet32 idBits); |
Siarhei Vishniakou | ae0f990 | 2020-09-14 19:23:31 -0500 | [diff] [blame] | 220 | void addMovement(nsecs_t eventTime, BitSet32 idBits, |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 221 | const std::vector<float>& positions) override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 222 | virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const; |
| 223 | |
| 224 | private: |
| 225 | // Sample horizon. |
| 226 | // We don't use too much history by default since we want to react to quick |
| 227 | // changes in direction. |
| 228 | static const nsecs_t HORIZON = 100 * 1000000; // 100 ms |
| 229 | |
| 230 | // Number of samples to keep. |
| 231 | static const uint32_t HISTORY_SIZE = 20; |
| 232 | |
| 233 | struct Movement { |
| 234 | nsecs_t eventTime; |
| 235 | BitSet32 idBits; |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 236 | float positions[MAX_POINTERS]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 237 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 238 | inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | float chooseWeight(uint32_t index) const; |
| 242 | |
| 243 | const uint32_t mDegree; |
| 244 | const Weighting mWeighting; |
| 245 | uint32_t mIndex; |
| 246 | Movement mMovements[HISTORY_SIZE]; |
| 247 | }; |
| 248 | |
| 249 | |
| 250 | /* |
| 251 | * Velocity tracker algorithm that uses an IIR filter. |
| 252 | */ |
| 253 | class IntegratingVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 254 | public: |
| 255 | // Degree must be 1 or 2. |
| 256 | IntegratingVelocityTrackerStrategy(uint32_t degree); |
| 257 | ~IntegratingVelocityTrackerStrategy(); |
| 258 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 259 | virtual void clearPointers(BitSet32 idBits); |
Siarhei Vishniakou | ae0f990 | 2020-09-14 19:23:31 -0500 | [diff] [blame] | 260 | void addMovement(nsecs_t eventTime, BitSet32 idBits, |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 261 | const std::vector<float>& positions) override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 262 | virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const; |
| 263 | |
| 264 | private: |
| 265 | // Current state estimate for a particular pointer. |
| 266 | struct State { |
| 267 | nsecs_t updateTime; |
| 268 | uint32_t degree; |
| 269 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 270 | float pos, vel, accel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | const uint32_t mDegree; |
| 274 | BitSet32 mPointerIdBits; |
| 275 | State mPointerState[MAX_POINTER_ID + 1]; |
| 276 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 277 | void initState(State& state, nsecs_t eventTime, float pos) const; |
| 278 | void updateState(State& state, nsecs_t eventTime, float pos) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 279 | void populateEstimator(const State& state, VelocityTracker::Estimator* outEstimator) const; |
| 280 | }; |
| 281 | |
| 282 | |
| 283 | /* |
| 284 | * Velocity tracker strategy used prior to ICS. |
| 285 | */ |
| 286 | class LegacyVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 287 | public: |
| 288 | LegacyVelocityTrackerStrategy(); |
| 289 | virtual ~LegacyVelocityTrackerStrategy(); |
| 290 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 291 | virtual void clearPointers(BitSet32 idBits); |
Siarhei Vishniakou | ae0f990 | 2020-09-14 19:23:31 -0500 | [diff] [blame] | 292 | void addMovement(nsecs_t eventTime, BitSet32 idBits, |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 293 | const std::vector<float>& positions) override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 294 | virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const; |
| 295 | |
| 296 | private: |
| 297 | // Oldest sample to consider when calculating the velocity. |
| 298 | static const nsecs_t HORIZON = 200 * 1000000; // 100 ms |
| 299 | |
| 300 | // Number of samples to keep. |
| 301 | static const uint32_t HISTORY_SIZE = 20; |
| 302 | |
| 303 | // The minimum duration between samples when estimating velocity. |
| 304 | static const nsecs_t MIN_DURATION = 10 * 1000000; // 10 ms |
| 305 | |
| 306 | struct Movement { |
| 307 | nsecs_t eventTime; |
| 308 | BitSet32 idBits; |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 309 | float positions[MAX_POINTERS]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 310 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 311 | inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | uint32_t mIndex; |
| 315 | Movement mMovements[HISTORY_SIZE]; |
| 316 | }; |
| 317 | |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 318 | class ImpulseVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 319 | public: |
| 320 | ImpulseVelocityTrackerStrategy(); |
| 321 | virtual ~ImpulseVelocityTrackerStrategy(); |
| 322 | |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 323 | virtual void clearPointers(BitSet32 idBits); |
Siarhei Vishniakou | ae0f990 | 2020-09-14 19:23:31 -0500 | [diff] [blame] | 324 | void addMovement(nsecs_t eventTime, BitSet32 idBits, |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 325 | const std::vector<float>& positions) override; |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 326 | virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const; |
| 327 | |
| 328 | private: |
| 329 | // Sample horizon. |
| 330 | // We don't use too much history by default since we want to react to quick |
| 331 | // changes in direction. |
| 332 | static constexpr nsecs_t HORIZON = 100 * 1000000; // 100 ms |
| 333 | |
| 334 | // Number of samples to keep. |
| 335 | static constexpr size_t HISTORY_SIZE = 20; |
| 336 | |
| 337 | struct Movement { |
| 338 | nsecs_t eventTime; |
| 339 | BitSet32 idBits; |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 340 | float positions[MAX_POINTERS]; |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 341 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 342 | inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; } |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | size_t mIndex; |
| 346 | Movement mMovements[HISTORY_SIZE]; |
| 347 | }; |
| 348 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 349 | } // namespace android |
| 350 | |
| 351 | #endif // _LIBINPUT_VELOCITY_TRACKER_H |