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. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 54 | nsecs_t time = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 55 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 56 | // Polynomial coefficients describing motion. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 57 | std::array<float, MAX_DEGREE + 1> coeff{}; |
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. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 61 | uint32_t degree = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 62 | |
| 63 | // Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit). |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 64 | float confidence = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 67 | /* |
| 68 | * Contains all available velocity data from a VelocityTracker. |
| 69 | */ |
| 70 | struct ComputedVelocity { |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 71 | inline std::optional<float> getVelocity(int32_t axis, int32_t id) const { |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 72 | 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 Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 85 | inline void addVelocity(int32_t axis, int32_t id, float velocity) { |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 86 | 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 Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 94 | // If strategy is not provided, uses the default strategy for the platform. |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 95 | // TODO(b/32830165): support axis-specific strategies. |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 96 | VelocityTracker(const Strategy strategy = Strategy::DEFAULT); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 97 | |
| 98 | ~VelocityTracker(); |
| 99 | |
Yeabkal Wubshit | eca273c | 2022-10-05 19:06:40 -0700 | [diff] [blame] | 100 | /** Return true if the axis is supported for velocity tracking, false otherwise. */ |
| 101 | static bool isAxisSupported(int32_t axis); |
| 102 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 103 | // Resets the velocity tracker state. |
| 104 | void clear(); |
| 105 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 106 | // Resets the velocity tracker state for a specific pointer. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 107 | // Call this method when some pointers have changed and may be reusing |
| 108 | // an id that was assigned to a different pointer earlier. |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 109 | void clearPointer(int32_t pointerId); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 110 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 111 | // 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 Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 113 | |
| 114 | // Adds movement information for all pointers in a MotionEvent, including historical samples. |
| 115 | void addMovement(const MotionEvent* event); |
| 116 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 117 | // 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 Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 120 | std::optional<float> getVelocity(int32_t axis, int32_t pointerId) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 121 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 122 | // 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 Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 128 | // Returns false and clears the estimator if there is no information available |
| 129 | // about the pointer. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 130 | std::optional<Estimator> getEstimator(int32_t axis, int32_t pointerId) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 131 | |
| 132 | // Gets the active pointer id, or -1 if none. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 133 | inline int32_t getActivePointerId() const { return mActivePointerId.value_or(-1); } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 134 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 135 | private: |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 136 | nsecs_t mLastEventTime; |
| 137 | BitSet32 mCurrentPointerIdBits; |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 138 | std::optional<int32_t> mActivePointerId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 139 | |
Yeabkal Wubshit | 47ff708 | 2022-09-10 23:09:15 -0700 | [diff] [blame] | 140 | // 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 Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 150 | |
Yeabkal Wubshit | 73c9508 | 2022-09-22 10:12:33 +0000 | [diff] [blame] | 151 | // 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 Wubshit | 0bb5e59 | 2022-09-14 01:22:28 -0700 | [diff] [blame] | 155 | static std::unique_ptr<VelocityTrackerStrategy> createStrategy(const Strategy strategy, |
Yeabkal Wubshit | 73c9508 | 2022-09-22 10:12:33 +0000 | [diff] [blame] | 156 | bool deltaValues); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | |
| 160 | /* |
| 161 | * Implements a particular velocity tracker algorithm. |
| 162 | */ |
| 163 | class VelocityTrackerStrategy { |
| 164 | protected: |
| 165 | VelocityTrackerStrategy() { } |
| 166 | |
| 167 | public: |
| 168 | virtual ~VelocityTrackerStrategy() { } |
| 169 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 170 | virtual void clearPointer(int32_t pointerId) = 0; |
| 171 | virtual void addMovement(nsecs_t eventTime, int32_t pointerId, float position) = 0; |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 172 | virtual std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | * Velocity tracker algorithm based on least-squares linear regression. |
| 178 | */ |
| 179 | class LeastSquaresVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 180 | public: |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 181 | enum class Weighting { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 182 | // No weights applied. All data points are equally reliable. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 183 | NONE, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 184 | |
| 185 | // Weight by time delta. Data points clustered together are weighted less. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 186 | DELTA, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 187 | |
| 188 | // Weight such that points within a certain horizon are weighed more than those |
| 189 | // outside of that horizon. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 190 | CENTRAL, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 191 | |
| 192 | // Weight such that points older than a certain amount are weighed less. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 193 | RECENT, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | // Degree must be no greater than Estimator::MAX_DEGREE. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 197 | LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = Weighting::NONE); |
| 198 | ~LeastSquaresVelocityTrackerStrategy() override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 199 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 200 | void clearPointer(int32_t pointerId) override; |
| 201 | void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override; |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 202 | std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 203 | |
| 204 | private: |
| 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 Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 215 | float position; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 218 | float chooseWeight(int32_t pointerId, uint32_t index) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 219 | |
| 220 | const uint32_t mDegree; |
| 221 | const Weighting mWeighting; |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 222 | std::map<int32_t /*pointerId*/, size_t /*positionInArray*/> mIndex; |
| 223 | std::map<int32_t /*pointerId*/, std::array<Movement, HISTORY_SIZE>> mMovements; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | |
| 227 | /* |
| 228 | * Velocity tracker algorithm that uses an IIR filter. |
| 229 | */ |
| 230 | class IntegratingVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 231 | public: |
| 232 | // Degree must be 1 or 2. |
| 233 | IntegratingVelocityTrackerStrategy(uint32_t degree); |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 234 | ~IntegratingVelocityTrackerStrategy() override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 235 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 236 | void clearPointer(int32_t pointerId) override; |
| 237 | void addMovement(nsecs_t eventTime, int32_t pointerId, float positions) override; |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 238 | std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 239 | |
| 240 | private: |
| 241 | // Current state estimate for a particular pointer. |
| 242 | struct State { |
| 243 | nsecs_t updateTime; |
| 244 | uint32_t degree; |
| 245 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 246 | float pos, vel, accel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | const uint32_t mDegree; |
| 250 | BitSet32 mPointerIdBits; |
| 251 | State mPointerState[MAX_POINTER_ID + 1]; |
| 252 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 253 | void initState(State& state, nsecs_t eventTime, float pos) const; |
| 254 | void updateState(State& state, nsecs_t eventTime, float pos) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 255 | void populateEstimator(const State& state, VelocityTracker::Estimator* outEstimator) const; |
| 256 | }; |
| 257 | |
| 258 | |
| 259 | /* |
| 260 | * Velocity tracker strategy used prior to ICS. |
| 261 | */ |
| 262 | class LegacyVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 263 | public: |
| 264 | LegacyVelocityTrackerStrategy(); |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 265 | ~LegacyVelocityTrackerStrategy() override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 266 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 267 | void clearPointer(int32_t pointerId) override; |
| 268 | void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override; |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 269 | std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 270 | |
| 271 | private: |
| 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 Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 283 | float position; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 284 | }; |
| 285 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 286 | std::map<int32_t /*pointerId*/, size_t /*positionInArray*/> mIndex; |
| 287 | std::map<int32_t /*pointerId*/, std::array<Movement, HISTORY_SIZE>> mMovements; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 288 | }; |
| 289 | |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 290 | class ImpulseVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 291 | public: |
Yeabkal Wubshit | 0bb5e59 | 2022-09-14 01:22:28 -0700 | [diff] [blame] | 292 | ImpulseVelocityTrackerStrategy(bool deltaValues); |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 293 | ~ImpulseVelocityTrackerStrategy() override; |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 294 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 295 | void clearPointer(int32_t pointerId) override; |
| 296 | void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override; |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 297 | std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override; |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 298 | |
| 299 | private: |
| 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 Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 310 | float position; |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 311 | }; |
| 312 | |
Yeabkal Wubshit | 0bb5e59 | 2022-09-14 01:22:28 -0700 | [diff] [blame] | 313 | // 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 Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame^] | 318 | std::map<int32_t /*pointerId*/, size_t /*positionInArray*/> mIndex; |
| 319 | std::map<int32_t /*pointerId*/, std::array<Movement, HISTORY_SIZE>> mMovements; |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 320 | }; |
| 321 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 322 | } // namespace android |