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