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 | |
Siarhei Vishniakou | f7436a1 | 2023-08-14 15:17:11 -0700 | [diff] [blame] | 19 | #include <android/os/IInputConstants.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 20 | #include <input/Input.h> |
Yeabkal Wubshit | 64f090f | 2023-03-03 17:35:11 -0800 | [diff] [blame] | 21 | #include <input/RingBuffer.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 22 | #include <utils/BitSet.h> |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 23 | #include <utils/Timers.h> |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 24 | #include <map> |
| 25 | #include <set> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | class VelocityTrackerStrategy; |
| 30 | |
| 31 | /* |
| 32 | * Calculates the velocity of pointer movements over time. |
| 33 | */ |
| 34 | class VelocityTracker { |
| 35 | public: |
Yeabkal Wubshit | 9b4443f | 2023-02-23 23:35:07 -0800 | [diff] [blame] | 36 | static const size_t MAX_DEGREE = 4; |
| 37 | |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 38 | enum class Strategy : int32_t { |
Siarhei Vishniakou | f7436a1 | 2023-08-14 15:17:11 -0700 | [diff] [blame] | 39 | 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 Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 51 | MAX = LEGACY, |
Siarhei Vishniakou | 8a2e589 | 2023-08-14 13:59:40 -0700 | [diff] [blame] | 52 | ftl_last = LEGACY, |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 55 | /* |
| 56 | * Contains all available velocity data from a VelocityTracker. |
| 57 | */ |
| 58 | struct ComputedVelocity { |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 59 | inline std::optional<float> getVelocity(int32_t axis, int32_t id) const { |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 60 | 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 Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 73 | inline void addVelocity(int32_t axis, int32_t id, float velocity) { |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 74 | 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 Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 82 | // If strategy is not provided, uses the default strategy for the platform. |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 83 | // TODO(b/32830165): support axis-specific strategies. |
Chris Ye | f859148 | 2020-04-17 11:49:17 -0700 | [diff] [blame] | 84 | VelocityTracker(const Strategy strategy = Strategy::DEFAULT); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 85 | |
Yeabkal Wubshit | eca273c | 2022-10-05 19:06:40 -0700 | [diff] [blame] | 86 | /** Return true if the axis is supported for velocity tracking, false otherwise. */ |
| 87 | static bool isAxisSupported(int32_t axis); |
| 88 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 89 | // Resets the velocity tracker state. |
| 90 | void clear(); |
| 91 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame] | 92 | // Resets the velocity tracker state for a specific pointer. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 93 | // Call this method when some pointers have changed and may be reusing |
| 94 | // an id that was assigned to a different pointer earlier. |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame] | 95 | void clearPointer(int32_t pointerId); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 96 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame] | 97 | // 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 Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 99 | |
| 100 | // Adds movement information for all pointers in a MotionEvent, including historical samples. |
Siarhei Vishniakou | 318005c | 2023-10-24 18:05:59 -0700 | [diff] [blame] | 101 | void addMovement(const MotionEvent& event); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 102 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 103 | // 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 Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 106 | std::optional<float> getVelocity(int32_t axis, int32_t pointerId) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 107 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 108 | // 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 Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 113 | // Gets the active pointer id, or -1 if none. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 114 | inline int32_t getActivePointerId() const { return mActivePointerId.value_or(-1); } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 115 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 116 | private: |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 117 | nsecs_t mLastEventTime; |
| 118 | BitSet32 mCurrentPointerIdBits; |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 119 | std::optional<int32_t> mActivePointerId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 120 | |
Yeabkal Wubshit | 47ff708 | 2022-09-10 23:09:15 -0700 | [diff] [blame] | 121 | // 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 Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 131 | |
Yeabkal Wubshit | 73c9508 | 2022-09-22 10:12:33 +0000 | [diff] [blame] | 132 | // 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 Wubshit | 0bb5e59 | 2022-09-14 01:22:28 -0700 | [diff] [blame] | 136 | static std::unique_ptr<VelocityTrackerStrategy> createStrategy(const Strategy strategy, |
Yeabkal Wubshit | 73c9508 | 2022-09-22 10:12:33 +0000 | [diff] [blame] | 137 | bool deltaValues); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | |
| 141 | /* |
| 142 | * Implements a particular velocity tracker algorithm. |
| 143 | */ |
| 144 | class VelocityTrackerStrategy { |
| 145 | protected: |
| 146 | VelocityTrackerStrategy() { } |
| 147 | |
| 148 | public: |
| 149 | virtual ~VelocityTrackerStrategy() { } |
| 150 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame] | 151 | virtual void clearPointer(int32_t pointerId) = 0; |
| 152 | virtual void addMovement(nsecs_t eventTime, int32_t pointerId, float position) = 0; |
Yeabkal Wubshit | 9b4443f | 2023-02-23 23:35:07 -0800 | [diff] [blame] | 153 | virtual std::optional<float> getVelocity(int32_t pointerId) const = 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Yeabkal Wubshit | a0e573c | 2023-03-02 21:08:14 -0800 | [diff] [blame] | 156 | /** |
| 157 | * A `VelocityTrackerStrategy` that accumulates added data points and processes the accumulated data |
| 158 | * points when getting velocity. |
| 159 | */ |
| 160 | class AccumulatingVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 161 | public: |
Yeabkal Wubshit | 4a678b2 | 2023-02-23 17:03:40 -0800 | [diff] [blame] | 162 | AccumulatingVelocityTrackerStrategy(nsecs_t horizonNanos, bool maintainHorizonDuringAdd); |
| 163 | |
Yeabkal Wubshit | a0e573c | 2023-03-02 21:08:14 -0800 | [diff] [blame] | 164 | void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override; |
| 165 | void clearPointer(int32_t pointerId) override; |
| 166 | |
| 167 | protected: |
| 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 Wubshit | 4a678b2 | 2023-02-23 17:03:40 -0800 | [diff] [blame] | 178 | /** |
| 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 Wubshit | 64f090f | 2023-03-03 17:35:11 -0800 | [diff] [blame] | 188 | std::map<int32_t /*pointerId*/, RingBuffer<Movement>> mMovements; |
Yeabkal Wubshit | a0e573c | 2023-03-02 21:08:14 -0800 | [diff] [blame] | 189 | }; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | * Velocity tracker algorithm based on least-squares linear regression. |
| 193 | */ |
Yeabkal Wubshit | a0e573c | 2023-03-02 21:08:14 -0800 | [diff] [blame] | 194 | class LeastSquaresVelocityTrackerStrategy : public AccumulatingVelocityTrackerStrategy { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 195 | public: |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 196 | enum class Weighting { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 197 | // No weights applied. All data points are equally reliable. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 198 | NONE, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 199 | |
| 200 | // Weight by time delta. Data points clustered together are weighted less. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 201 | DELTA, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 202 | |
| 203 | // Weight such that points within a certain horizon are weighed more than those |
| 204 | // outside of that horizon. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 205 | CENTRAL, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 206 | |
| 207 | // Weight such that points older than a certain amount are weighed less. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 208 | RECENT, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
Yeabkal Wubshit | 9b4443f | 2023-02-23 23:35:07 -0800 | [diff] [blame] | 211 | // Degree must be no greater than VelocityTracker::MAX_DEGREE. |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 212 | LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = Weighting::NONE); |
| 213 | ~LeastSquaresVelocityTrackerStrategy() override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 214 | |
Yeabkal Wubshit | 9b4443f | 2023-02-23 23:35:07 -0800 | [diff] [blame] | 215 | std::optional<float> getVelocity(int32_t pointerId) const override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 216 | |
| 217 | private: |
| 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 Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame] | 223 | float chooseWeight(int32_t pointerId, uint32_t index) const; |
Yeabkal Wubshit | fa806e4 | 2023-03-06 18:34:07 -0800 | [diff] [blame] | 224 | /** |
| 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 Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 231 | |
| 232 | const uint32_t mDegree; |
| 233 | const Weighting mWeighting; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 236 | /* |
| 237 | * Velocity tracker algorithm that uses an IIR filter. |
| 238 | */ |
| 239 | class IntegratingVelocityTrackerStrategy : public VelocityTrackerStrategy { |
| 240 | public: |
| 241 | // Degree must be 1 or 2. |
| 242 | IntegratingVelocityTrackerStrategy(uint32_t degree); |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 243 | ~IntegratingVelocityTrackerStrategy() override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 244 | |
Siarhei Vishniakou | 8d23203 | 2023-01-11 08:17:21 -0800 | [diff] [blame] | 245 | void clearPointer(int32_t pointerId) override; |
| 246 | void addMovement(nsecs_t eventTime, int32_t pointerId, float positions) override; |
Yeabkal Wubshit | 9b4443f | 2023-02-23 23:35:07 -0800 | [diff] [blame] | 247 | std::optional<float> getVelocity(int32_t pointerId) const override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 248 | |
| 249 | private: |
| 250 | // Current state estimate for a particular pointer. |
| 251 | struct State { |
| 252 | nsecs_t updateTime; |
| 253 | uint32_t degree; |
| 254 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 255 | float pos, vel, accel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | const uint32_t mDegree; |
| 259 | BitSet32 mPointerIdBits; |
| 260 | State mPointerState[MAX_POINTER_ID + 1]; |
| 261 | |
Yeabkal Wubshit | 384ab0f | 2022-09-09 16:39:18 +0000 | [diff] [blame] | 262 | void initState(State& state, nsecs_t eventTime, float pos) const; |
| 263 | void updateState(State& state, nsecs_t eventTime, float pos) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | |
| 267 | /* |
| 268 | * Velocity tracker strategy used prior to ICS. |
| 269 | */ |
Yeabkal Wubshit | a0e573c | 2023-03-02 21:08:14 -0800 | [diff] [blame] | 270 | class LegacyVelocityTrackerStrategy : public AccumulatingVelocityTrackerStrategy { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 271 | public: |
| 272 | LegacyVelocityTrackerStrategy(); |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 273 | ~LegacyVelocityTrackerStrategy() override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 274 | |
Yeabkal Wubshit | 9b4443f | 2023-02-23 23:35:07 -0800 | [diff] [blame] | 275 | std::optional<float> getVelocity(int32_t pointerId) const override; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 276 | |
| 277 | private: |
| 278 | // Oldest sample to consider when calculating the velocity. |
| 279 | static const nsecs_t HORIZON = 200 * 1000000; // 100 ms |
| 280 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 281 | // The minimum duration between samples when estimating velocity. |
| 282 | static const nsecs_t MIN_DURATION = 10 * 1000000; // 10 ms |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 283 | }; |
| 284 | |
Yeabkal Wubshit | a0e573c | 2023-03-02 21:08:14 -0800 | [diff] [blame] | 285 | class ImpulseVelocityTrackerStrategy : public AccumulatingVelocityTrackerStrategy { |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 286 | public: |
Yeabkal Wubshit | 0bb5e59 | 2022-09-14 01:22:28 -0700 | [diff] [blame] | 287 | ImpulseVelocityTrackerStrategy(bool deltaValues); |
Siarhei Vishniakou | 657a173 | 2023-01-12 11:58:52 -0800 | [diff] [blame] | 288 | ~ImpulseVelocityTrackerStrategy() override; |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 289 | |
Yeabkal Wubshit | 9b4443f | 2023-02-23 23:35:07 -0800 | [diff] [blame] | 290 | std::optional<float> getVelocity(int32_t pointerId) const override; |
Siarhei Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 291 | |
| 292 | private: |
| 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 Wubshit | 0bb5e59 | 2022-09-14 01:22:28 -0700 | [diff] [blame] | 298 | // 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 Vishniakou | 00a4ea9 | 2017-06-08 21:43:20 +0100 | [diff] [blame] | 302 | }; |
| 303 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 304 | } // namespace android |