Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2024 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 | #define LOG_TAG "LegacyResampler" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <chrono> |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 21 | #include <ostream> |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | #include <android-base/properties.h> |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 25 | #include <ftl/enum.h> |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 26 | |
| 27 | #include <input/Resampler.h> |
| 28 | #include <utils/Timers.h> |
| 29 | |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 30 | namespace android { |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | const bool IS_DEBUGGABLE_BUILD = |
| 34 | #if defined(__ANDROID__) |
| 35 | android::base::GetBoolProperty("ro.debuggable", false); |
| 36 | #else |
| 37 | true; |
| 38 | #endif |
| 39 | |
| 40 | bool debugResampling() { |
| 41 | if (!IS_DEBUGGABLE_BUILD) { |
| 42 | static const bool DEBUG_TRANSPORT_RESAMPLING = |
| 43 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", |
| 44 | ANDROID_LOG_INFO); |
| 45 | return DEBUG_TRANSPORT_RESAMPLING; |
| 46 | } |
| 47 | return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", ANDROID_LOG_INFO); |
| 48 | } |
| 49 | |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 50 | using std::chrono::nanoseconds; |
| 51 | |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 52 | constexpr std::chrono::milliseconds RESAMPLE_LATENCY{5}; |
| 53 | |
| 54 | constexpr std::chrono::milliseconds RESAMPLE_MIN_DELTA{2}; |
| 55 | |
| 56 | constexpr std::chrono::milliseconds RESAMPLE_MAX_DELTA{20}; |
| 57 | |
| 58 | constexpr std::chrono::milliseconds RESAMPLE_MAX_PREDICTION{8}; |
| 59 | |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 60 | bool canResampleTool(ToolType toolType) { |
| 61 | return toolType == ToolType::FINGER || toolType == ToolType::MOUSE || |
| 62 | toolType == ToolType::STYLUS || toolType == ToolType::UNKNOWN; |
| 63 | } |
| 64 | |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 65 | inline float lerp(float a, float b, float alpha) { |
| 66 | return a + alpha * (b - a); |
| 67 | } |
| 68 | |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 69 | PointerCoords calculateResampledCoords(const PointerCoords& a, const PointerCoords& b, |
| 70 | float alpha) { |
Paul Ramirez | 68ca3d1 | 2024-08-12 23:00:50 +0000 | [diff] [blame] | 71 | // We use the value of alpha to initialize resampledCoords with the latest sample information. |
| 72 | PointerCoords resampledCoords = (alpha < 1.0f) ? a : b; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 73 | resampledCoords.isResampled = true; |
| 74 | resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X, lerp(a.getX(), b.getX(), alpha)); |
| 75 | resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, lerp(a.getY(), b.getY(), alpha)); |
| 76 | return resampledCoords; |
| 77 | } |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 78 | |
| 79 | bool equalXY(const PointerCoords& a, const PointerCoords& b) { |
| 80 | return (a.getX() == b.getX()) && (a.getY() == b.getY()); |
| 81 | } |
| 82 | |
| 83 | void setMotionEventPointerCoords(MotionEvent& motionEvent, size_t sampleIndex, size_t pointerIndex, |
| 84 | const PointerCoords& pointerCoords) { |
| 85 | // Ideally, we should not cast away const. In this particular case, it's safe to cast away const |
| 86 | // and dereference getHistoricalRawPointerCoords returned pointer because motionEvent is a |
| 87 | // nonconst reference to a MotionEvent object, so mutating the object should not be undefined |
| 88 | // behavior; moreover, the invoked method guarantees to return a valid pointer. Otherwise, it |
| 89 | // fatally logs. Alternatively, we could've created a new MotionEvent from scratch, but this |
| 90 | // approach is simpler and more efficient. |
| 91 | PointerCoords& motionEventCoords = const_cast<PointerCoords&>( |
| 92 | *(motionEvent.getHistoricalRawPointerCoords(pointerIndex, sampleIndex))); |
| 93 | motionEventCoords.setAxisValue(AMOTION_EVENT_AXIS_X, pointerCoords.getX()); |
| 94 | motionEventCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, pointerCoords.getY()); |
| 95 | motionEventCoords.isResampled = pointerCoords.isResampled; |
| 96 | } |
| 97 | |
| 98 | std::ostream& operator<<(std::ostream& os, const PointerCoords& pointerCoords) { |
| 99 | os << "(" << pointerCoords.getX() << ", " << pointerCoords.getY() << ")"; |
| 100 | return os; |
| 101 | } |
| 102 | |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 103 | } // namespace |
| 104 | |
| 105 | void LegacyResampler::updateLatestSamples(const MotionEvent& motionEvent) { |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 106 | const size_t numSamples = motionEvent.getHistorySize() + 1; |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 107 | const size_t latestIndex = numSamples - 1; |
| 108 | const size_t secondToLatestIndex = (latestIndex > 0) ? (latestIndex - 1) : 0; |
| 109 | for (size_t sampleIndex = secondToLatestIndex; sampleIndex < numSamples; ++sampleIndex) { |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 110 | PointerMap pointerMap; |
| 111 | for (size_t pointerIndex = 0; pointerIndex < motionEvent.getPointerCount(); |
| 112 | ++pointerIndex) { |
| 113 | pointerMap.insert(Pointer{*(motionEvent.getPointerProperties(pointerIndex)), |
| 114 | *(motionEvent.getHistoricalRawPointerCoords(pointerIndex, |
| 115 | sampleIndex))}); |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 116 | } |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 117 | mLatestSamples.pushBack( |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 118 | Sample{nanoseconds{motionEvent.getHistoricalEventTime(sampleIndex)}, pointerMap}); |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 122 | LegacyResampler::Sample LegacyResampler::messageToSample(const InputMessage& message) { |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 123 | PointerMap pointerMap; |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 124 | for (uint32_t i = 0; i < message.body.motion.pointerCount; ++i) { |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 125 | pointerMap.insert(Pointer{message.body.motion.pointers[i].properties, |
| 126 | message.body.motion.pointers[i].coords}); |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 127 | } |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 128 | return Sample{nanoseconds{message.body.motion.eventTime}, pointerMap}; |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | bool LegacyResampler::pointerPropertiesResampleable(const Sample& target, const Sample& auxiliary) { |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 132 | for (const Pointer& pointer : target.pointerMap) { |
| 133 | const std::optional<Pointer> auxiliaryPointer = |
| 134 | auxiliary.pointerMap.find(PointerMap::PointerId{pointer.properties.id}); |
| 135 | if (!auxiliaryPointer.has_value()) { |
| 136 | LOG_IF(INFO, debugResampling()) |
| 137 | << "Not resampled. Auxiliary sample does not contain all pointers from target."; |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 138 | return false; |
| 139 | } |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 140 | if (pointer.properties.toolType != auxiliaryPointer->properties.toolType) { |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 141 | LOG_IF(INFO, debugResampling()) << "Not resampled. Pointer ToolType mismatch."; |
| 142 | return false; |
| 143 | } |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 144 | if (!canResampleTool(pointer.properties.toolType)) { |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 145 | LOG_IF(INFO, debugResampling()) |
| 146 | << "Not resampled. Cannot resample " |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 147 | << ftl::enum_string(pointer.properties.toolType) << " ToolType."; |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | bool LegacyResampler::canInterpolate(const InputMessage& message) const { |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 155 | LOG_IF(FATAL, mLatestSamples.empty()) |
| 156 | << "Not resampled. mLatestSamples must not be empty to interpolate."; |
| 157 | |
| 158 | const Sample& pastSample = *(mLatestSamples.end() - 1); |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 159 | const Sample& futureSample = messageToSample(message); |
| 160 | |
| 161 | if (!pointerPropertiesResampleable(pastSample, futureSample)) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | const nanoseconds delta = futureSample.eventTime - pastSample.eventTime; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 166 | if (delta < RESAMPLE_MIN_DELTA) { |
| 167 | LOG_IF(INFO, debugResampling()) << "Not resampled. Delta is too small: " << delta << "ns."; |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 168 | return false; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 169 | } |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 170 | return true; |
| 171 | } |
| 172 | |
| 173 | std::optional<LegacyResampler::Sample> LegacyResampler::attemptInterpolation( |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 174 | nanoseconds resampleTime, const InputMessage& futureMessage) const { |
| 175 | if (!canInterpolate(futureMessage)) { |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 176 | return std::nullopt; |
| 177 | } |
| 178 | LOG_IF(FATAL, mLatestSamples.empty()) |
| 179 | << "Not resampled. mLatestSamples must not be empty to interpolate."; |
| 180 | |
| 181 | const Sample& pastSample = *(mLatestSamples.end() - 1); |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 182 | const Sample& futureSample = messageToSample(futureMessage); |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 183 | |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 184 | const nanoseconds delta = nanoseconds{futureSample.eventTime} - pastSample.eventTime; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 185 | const float alpha = |
| 186 | std::chrono::duration<float, std::milli>(resampleTime - pastSample.eventTime) / delta; |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 187 | |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 188 | PointerMap resampledPointerMap; |
| 189 | for (const Pointer& pointer : pastSample.pointerMap) { |
| 190 | if (std::optional<Pointer> futureSamplePointer = |
| 191 | futureSample.pointerMap.find(PointerMap::PointerId{pointer.properties.id}); |
| 192 | futureSamplePointer.has_value()) { |
| 193 | const PointerCoords& resampledCoords = |
| 194 | calculateResampledCoords(pointer.coords, futureSamplePointer->coords, alpha); |
| 195 | resampledPointerMap.insert(Pointer{pointer.properties, resampledCoords}); |
| 196 | } |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 197 | } |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 198 | return Sample{resampleTime, resampledPointerMap}; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 201 | bool LegacyResampler::canExtrapolate() const { |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 202 | if (mLatestSamples.size() < 2) { |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 203 | LOG_IF(INFO, debugResampling()) << "Not resampled. Not enough data."; |
| 204 | return false; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 205 | } |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 206 | |
| 207 | const Sample& pastSample = *(mLatestSamples.end() - 2); |
| 208 | const Sample& presentSample = *(mLatestSamples.end() - 1); |
| 209 | |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 210 | if (!pointerPropertiesResampleable(presentSample, pastSample)) { |
| 211 | return false; |
| 212 | } |
| 213 | |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 214 | const nanoseconds delta = presentSample.eventTime - pastSample.eventTime; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 215 | if (delta < RESAMPLE_MIN_DELTA) { |
| 216 | LOG_IF(INFO, debugResampling()) << "Not resampled. Delta is too small: " << delta << "ns."; |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 217 | return false; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 218 | } else if (delta > RESAMPLE_MAX_DELTA) { |
| 219 | LOG_IF(INFO, debugResampling()) << "Not resampled. Delta is too large: " << delta << "ns."; |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 220 | return false; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 221 | } |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | |
| 225 | std::optional<LegacyResampler::Sample> LegacyResampler::attemptExtrapolation( |
| 226 | nanoseconds resampleTime) const { |
| 227 | if (!canExtrapolate()) { |
| 228 | return std::nullopt; |
| 229 | } |
| 230 | LOG_IF(FATAL, mLatestSamples.size() < 2) |
| 231 | << "Not resampled. mLatestSamples must have at least two samples to extrapolate."; |
| 232 | |
| 233 | const Sample& pastSample = *(mLatestSamples.end() - 2); |
| 234 | const Sample& presentSample = *(mLatestSamples.end() - 1); |
| 235 | |
| 236 | const nanoseconds delta = presentSample.eventTime - pastSample.eventTime; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 237 | // The farthest future time to which we can extrapolate. If the given resampleTime exceeds this, |
| 238 | // we use this value as the resample time target. |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 239 | const nanoseconds farthestPrediction = |
| 240 | presentSample.eventTime + std::min<nanoseconds>(delta / 2, RESAMPLE_MAX_PREDICTION); |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 241 | const nanoseconds newResampleTime = |
| 242 | (resampleTime > farthestPrediction) ? (farthestPrediction) : (resampleTime); |
| 243 | LOG_IF(INFO, debugResampling() && newResampleTime == farthestPrediction) |
| 244 | << "Resample time is too far in the future. Adjusting prediction from " |
| 245 | << (resampleTime - presentSample.eventTime) << " to " |
| 246 | << (farthestPrediction - presentSample.eventTime) << "ns."; |
| 247 | const float alpha = |
| 248 | std::chrono::duration<float, std::milli>(newResampleTime - pastSample.eventTime) / |
| 249 | delta; |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 250 | |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 251 | PointerMap resampledPointerMap; |
| 252 | for (const Pointer& pointer : presentSample.pointerMap) { |
| 253 | if (std::optional<Pointer> pastSamplePointer = |
| 254 | pastSample.pointerMap.find(PointerMap::PointerId{pointer.properties.id}); |
| 255 | pastSamplePointer.has_value()) { |
| 256 | const PointerCoords& resampledCoords = |
| 257 | calculateResampledCoords(pastSamplePointer->coords, pointer.coords, alpha); |
| 258 | resampledPointerMap.insert(Pointer{pointer.properties, resampledCoords}); |
| 259 | } |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 260 | } |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 261 | return Sample{newResampleTime, resampledPointerMap}; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 264 | inline void LegacyResampler::addSampleToMotionEvent(const Sample& sample, |
| 265 | MotionEvent& motionEvent) { |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 266 | motionEvent.addSample(sample.eventTime.count(), sample.asPointerCoords().data(), |
| 267 | motionEvent.getId()); |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Paul Ramirez | cd7488c | 2024-09-13 23:01:12 +0000 | [diff] [blame] | 270 | nanoseconds LegacyResampler::getResampleLatency() const { |
| 271 | return RESAMPLE_LATENCY; |
| 272 | } |
| 273 | |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 274 | /** |
| 275 | * The resampler is unaware of ACTION_DOWN. Thus, it needs to constantly check for pointer IDs |
| 276 | * occurrences. This problem could be fixed if the resampler has access to the entire stream of |
| 277 | * MotionEvent actions. That way, both ACTION_DOWN and ACTION_UP will be visible; therefore, |
| 278 | * facilitating pointer tracking between samples. |
| 279 | */ |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 280 | void LegacyResampler::overwriteMotionEventSamples(MotionEvent& motionEvent) const { |
| 281 | const size_t numSamples = motionEvent.getHistorySize() + 1; |
| 282 | for (size_t sampleIndex = 0; sampleIndex < numSamples; ++sampleIndex) { |
| 283 | overwriteStillPointers(motionEvent, sampleIndex); |
Paul Ramirez | 4679e55 | 2024-10-01 01:17:39 +0000 | [diff] [blame] | 284 | overwriteOldPointers(motionEvent, sampleIndex); |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | void LegacyResampler::overwriteStillPointers(MotionEvent& motionEvent, size_t sampleIndex) const { |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 289 | if (!mLastRealSample.has_value() || !mPreviousPrediction.has_value()) { |
| 290 | LOG_IF(INFO, debugResampling()) << "Still pointers not overwritten. Not enough data."; |
| 291 | return; |
| 292 | } |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 293 | for (size_t pointerIndex = 0; pointerIndex < motionEvent.getPointerCount(); ++pointerIndex) { |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 294 | const std::optional<Pointer> lastRealPointer = mLastRealSample->pointerMap.find( |
| 295 | PointerMap::PointerId{motionEvent.getPointerId(pointerIndex)}); |
| 296 | const std::optional<Pointer> previousPointer = mPreviousPrediction->pointerMap.find( |
| 297 | PointerMap::PointerId{motionEvent.getPointerId(pointerIndex)}); |
| 298 | // This could happen because resampler only receives ACTION_MOVE events. |
| 299 | if (!lastRealPointer.has_value() || !previousPointer.has_value()) { |
| 300 | continue; |
| 301 | } |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 302 | const PointerCoords& pointerCoords = |
| 303 | *(motionEvent.getHistoricalRawPointerCoords(pointerIndex, sampleIndex)); |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 304 | if (equalXY(pointerCoords, lastRealPointer->coords)) { |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 305 | LOG_IF(INFO, debugResampling()) |
| 306 | << "Pointer ID: " << motionEvent.getPointerId(pointerIndex) |
| 307 | << " did not move. Overwriting its coordinates from " << pointerCoords << " to " |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 308 | << previousPointer->coords; |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 309 | setMotionEventPointerCoords(motionEvent, sampleIndex, pointerIndex, |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 310 | previousPointer->coords); |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Paul Ramirez | 4679e55 | 2024-10-01 01:17:39 +0000 | [diff] [blame] | 315 | void LegacyResampler::overwriteOldPointers(MotionEvent& motionEvent, size_t sampleIndex) const { |
| 316 | if (!mPreviousPrediction.has_value()) { |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 317 | LOG_IF(INFO, debugResampling()) << "Old sample not overwritten. Not enough data."; |
Paul Ramirez | 4679e55 | 2024-10-01 01:17:39 +0000 | [diff] [blame] | 318 | return; |
| 319 | } |
| 320 | if (nanoseconds{motionEvent.getHistoricalEventTime(sampleIndex)} < |
| 321 | mPreviousPrediction->eventTime) { |
| 322 | LOG_IF(INFO, debugResampling()) |
| 323 | << "Motion event sample older than predicted sample. Overwriting event time from " |
| 324 | << motionEvent.getHistoricalEventTime(sampleIndex) << "ns to " |
| 325 | << mPreviousPrediction->eventTime.count() << "ns."; |
| 326 | for (size_t pointerIndex = 0; pointerIndex < motionEvent.getPointerCount(); |
| 327 | ++pointerIndex) { |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 328 | const std::optional<Pointer> previousPointer = mPreviousPrediction->pointerMap.find( |
| 329 | PointerMap::PointerId{motionEvent.getPointerId(pointerIndex)}); |
| 330 | // This could happen because resampler only receives ACTION_MOVE events. |
| 331 | if (!previousPointer.has_value()) { |
| 332 | continue; |
| 333 | } |
Paul Ramirez | 4679e55 | 2024-10-01 01:17:39 +0000 | [diff] [blame] | 334 | setMotionEventPointerCoords(motionEvent, sampleIndex, pointerIndex, |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 335 | previousPointer->coords); |
Paul Ramirez | 4679e55 | 2024-10-01 01:17:39 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
Paul Ramirez | 6affbdb | 2024-09-11 22:20:26 +0000 | [diff] [blame] | 340 | void LegacyResampler::resampleMotionEvent(nanoseconds frameTime, MotionEvent& motionEvent, |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 341 | const InputMessage* futureSample) { |
Paul Ramirez | 6affbdb | 2024-09-11 22:20:26 +0000 | [diff] [blame] | 342 | const nanoseconds resampleTime = frameTime - RESAMPLE_LATENCY; |
| 343 | |
Paul Ramirez | 7f1efed | 2024-09-29 23:55:23 +0000 | [diff] [blame] | 344 | if (resampleTime.count() == motionEvent.getEventTime()) { |
| 345 | LOG_IF(INFO, debugResampling()) << "Not resampled. Resample time equals motion event time."; |
| 346 | return; |
| 347 | } |
| 348 | |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 349 | updateLatestSamples(motionEvent); |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 350 | |
| 351 | const std::optional<Sample> sample = (futureSample != nullptr) |
| 352 | ? (attemptInterpolation(resampleTime, *futureSample)) |
| 353 | : (attemptExtrapolation(resampleTime)); |
| 354 | if (sample.has_value()) { |
| 355 | addSampleToMotionEvent(*sample, motionEvent); |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 356 | if (mPreviousPrediction.has_value()) { |
| 357 | overwriteMotionEventSamples(motionEvent); |
| 358 | } |
| 359 | // mPreviousPrediction is only updated whenever extrapolation occurs because extrapolation |
| 360 | // is about predicting upcoming scenarios. |
| 361 | if (futureSample == nullptr) { |
| 362 | mPreviousPrediction = sample; |
| 363 | } |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 364 | } |
Paul Ramirez | 29ee27c | 2024-10-02 08:18:53 +0000 | [diff] [blame^] | 365 | LOG_IF(FATAL, mLatestSamples.empty()) << "mLatestSamples must contain at least one sample."; |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 366 | mLastRealSample = *(mLatestSamples.end() - 1); |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 367 | } |
Paul Ramirez | 4d3b03a | 2024-09-30 01:39:00 +0000 | [diff] [blame] | 368 | |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 369 | } // namespace android |