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> |
| 21 | |
| 22 | #include <android-base/logging.h> |
| 23 | #include <android-base/properties.h> |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 24 | #include <ftl/enum.h> |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 25 | |
| 26 | #include <input/Resampler.h> |
| 27 | #include <utils/Timers.h> |
| 28 | |
| 29 | using std::chrono::nanoseconds; |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | const bool IS_DEBUGGABLE_BUILD = |
| 36 | #if defined(__ANDROID__) |
| 37 | android::base::GetBoolProperty("ro.debuggable", false); |
| 38 | #else |
| 39 | true; |
| 40 | #endif |
| 41 | |
| 42 | bool debugResampling() { |
| 43 | if (!IS_DEBUGGABLE_BUILD) { |
| 44 | static const bool DEBUG_TRANSPORT_RESAMPLING = |
| 45 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", |
| 46 | ANDROID_LOG_INFO); |
| 47 | return DEBUG_TRANSPORT_RESAMPLING; |
| 48 | } |
| 49 | return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", ANDROID_LOG_INFO); |
| 50 | } |
| 51 | |
| 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 | } |
| 78 | } // namespace |
| 79 | |
| 80 | void LegacyResampler::updateLatestSamples(const MotionEvent& motionEvent) { |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 81 | const size_t numSamples = motionEvent.getHistorySize() + 1; |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 82 | const size_t latestIndex = numSamples - 1; |
| 83 | const size_t secondToLatestIndex = (latestIndex > 0) ? (latestIndex - 1) : 0; |
| 84 | for (size_t sampleIndex = secondToLatestIndex; sampleIndex < numSamples; ++sampleIndex) { |
| 85 | std::vector<Pointer> pointers; |
| 86 | const size_t numPointers = motionEvent.getPointerCount(); |
| 87 | for (size_t pointerIndex = 0; pointerIndex < numPointers; ++pointerIndex) { |
| 88 | // getSamplePointerCoords is the vector representation of a getHistorySize by |
| 89 | // getPointerCount matrix. |
| 90 | const PointerCoords& pointerCoords = |
| 91 | motionEvent.getSamplePointerCoords()[sampleIndex * numPointers + pointerIndex]; |
| 92 | pointers.push_back( |
| 93 | Pointer{*motionEvent.getPointerProperties(pointerIndex), pointerCoords}); |
| 94 | } |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 95 | mLatestSamples.pushBack( |
Paul Ramirez | 698344a | 2024-08-20 00:58:55 +0000 | [diff] [blame] | 96 | Sample{nanoseconds{motionEvent.getHistoricalEventTime(sampleIndex)}, pointers}); |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 100 | LegacyResampler::Sample LegacyResampler::messageToSample(const InputMessage& message) { |
| 101 | std::vector<Pointer> pointers; |
| 102 | for (uint32_t i = 0; i < message.body.motion.pointerCount; ++i) { |
| 103 | pointers.push_back(Pointer{message.body.motion.pointers[i].properties, |
| 104 | message.body.motion.pointers[i].coords}); |
| 105 | } |
Paul Ramirez | 698344a | 2024-08-20 00:58:55 +0000 | [diff] [blame] | 106 | return Sample{nanoseconds{message.body.motion.eventTime}, pointers}; |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool LegacyResampler::pointerPropertiesResampleable(const Sample& target, const Sample& auxiliary) { |
| 110 | if (target.pointers.size() > auxiliary.pointers.size()) { |
| 111 | LOG_IF(INFO, debugResampling()) |
| 112 | << "Not resampled. Auxiliary sample has fewer pointers than target sample."; |
| 113 | return false; |
| 114 | } |
| 115 | for (size_t i = 0; i < target.pointers.size(); ++i) { |
| 116 | if (target.pointers[i].properties.id != auxiliary.pointers[i].properties.id) { |
| 117 | LOG_IF(INFO, debugResampling()) << "Not resampled. Pointer ID mismatch."; |
| 118 | return false; |
| 119 | } |
| 120 | if (target.pointers[i].properties.toolType != auxiliary.pointers[i].properties.toolType) { |
| 121 | LOG_IF(INFO, debugResampling()) << "Not resampled. Pointer ToolType mismatch."; |
| 122 | return false; |
| 123 | } |
| 124 | if (!canResampleTool(target.pointers[i].properties.toolType)) { |
| 125 | LOG_IF(INFO, debugResampling()) |
| 126 | << "Not resampled. Cannot resample " |
| 127 | << ftl::enum_string(target.pointers[i].properties.toolType) << " ToolType."; |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | bool LegacyResampler::canInterpolate(const InputMessage& message) const { |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 135 | LOG_IF(FATAL, mLatestSamples.empty()) |
| 136 | << "Not resampled. mLatestSamples must not be empty to interpolate."; |
| 137 | |
| 138 | const Sample& pastSample = *(mLatestSamples.end() - 1); |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 139 | const Sample& futureSample = messageToSample(message); |
| 140 | |
| 141 | if (!pointerPropertiesResampleable(pastSample, futureSample)) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | const nanoseconds delta = futureSample.eventTime - pastSample.eventTime; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 146 | if (delta < RESAMPLE_MIN_DELTA) { |
| 147 | LOG_IF(INFO, debugResampling()) << "Not resampled. Delta is too small: " << delta << "ns."; |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 148 | return false; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 149 | } |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | |
| 153 | std::optional<LegacyResampler::Sample> LegacyResampler::attemptInterpolation( |
| 154 | nanoseconds resampleTime, const InputMessage& futureSample) const { |
| 155 | if (!canInterpolate(futureSample)) { |
| 156 | return std::nullopt; |
| 157 | } |
| 158 | LOG_IF(FATAL, mLatestSamples.empty()) |
| 159 | << "Not resampled. mLatestSamples must not be empty to interpolate."; |
| 160 | |
| 161 | const Sample& pastSample = *(mLatestSamples.end() - 1); |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 162 | |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 163 | const nanoseconds delta = |
Paul Ramirez | 698344a | 2024-08-20 00:58:55 +0000 | [diff] [blame] | 164 | nanoseconds{futureSample.body.motion.eventTime} - pastSample.eventTime; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 165 | const float alpha = |
| 166 | std::chrono::duration<float, std::milli>(resampleTime - pastSample.eventTime) / delta; |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 167 | |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 168 | std::vector<Pointer> resampledPointers; |
| 169 | for (size_t i = 0; i < pastSample.pointers.size(); ++i) { |
| 170 | const PointerCoords& resampledCoords = |
| 171 | calculateResampledCoords(pastSample.pointers[i].coords, |
| 172 | futureSample.body.motion.pointers[i].coords, alpha); |
| 173 | resampledPointers.push_back(Pointer{pastSample.pointers[i].properties, resampledCoords}); |
| 174 | } |
| 175 | return Sample{resampleTime, resampledPointers}; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 178 | bool LegacyResampler::canExtrapolate() const { |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 179 | if (mLatestSamples.size() < 2) { |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 180 | LOG_IF(INFO, debugResampling()) << "Not resampled. Not enough data."; |
| 181 | return false; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 182 | } |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 183 | |
| 184 | const Sample& pastSample = *(mLatestSamples.end() - 2); |
| 185 | const Sample& presentSample = *(mLatestSamples.end() - 1); |
| 186 | |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 187 | if (!pointerPropertiesResampleable(presentSample, pastSample)) { |
| 188 | return false; |
| 189 | } |
| 190 | |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 191 | const nanoseconds delta = presentSample.eventTime - pastSample.eventTime; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 192 | if (delta < RESAMPLE_MIN_DELTA) { |
| 193 | LOG_IF(INFO, debugResampling()) << "Not resampled. Delta is too small: " << delta << "ns."; |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 194 | return false; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 195 | } else if (delta > RESAMPLE_MAX_DELTA) { |
| 196 | LOG_IF(INFO, debugResampling()) << "Not resampled. Delta is too large: " << delta << "ns."; |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 197 | return false; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 198 | } |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 199 | return true; |
| 200 | } |
| 201 | |
| 202 | std::optional<LegacyResampler::Sample> LegacyResampler::attemptExtrapolation( |
| 203 | nanoseconds resampleTime) const { |
| 204 | if (!canExtrapolate()) { |
| 205 | return std::nullopt; |
| 206 | } |
| 207 | LOG_IF(FATAL, mLatestSamples.size() < 2) |
| 208 | << "Not resampled. mLatestSamples must have at least two samples to extrapolate."; |
| 209 | |
| 210 | const Sample& pastSample = *(mLatestSamples.end() - 2); |
| 211 | const Sample& presentSample = *(mLatestSamples.end() - 1); |
| 212 | |
| 213 | const nanoseconds delta = presentSample.eventTime - pastSample.eventTime; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 214 | // The farthest future time to which we can extrapolate. If the given resampleTime exceeds this, |
| 215 | // we use this value as the resample time target. |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 216 | const nanoseconds farthestPrediction = |
| 217 | presentSample.eventTime + std::min<nanoseconds>(delta / 2, RESAMPLE_MAX_PREDICTION); |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 218 | const nanoseconds newResampleTime = |
| 219 | (resampleTime > farthestPrediction) ? (farthestPrediction) : (resampleTime); |
| 220 | LOG_IF(INFO, debugResampling() && newResampleTime == farthestPrediction) |
| 221 | << "Resample time is too far in the future. Adjusting prediction from " |
| 222 | << (resampleTime - presentSample.eventTime) << " to " |
| 223 | << (farthestPrediction - presentSample.eventTime) << "ns."; |
| 224 | const float alpha = |
| 225 | std::chrono::duration<float, std::milli>(newResampleTime - pastSample.eventTime) / |
| 226 | delta; |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 227 | |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 228 | std::vector<Pointer> resampledPointers; |
| 229 | for (size_t i = 0; i < presentSample.pointers.size(); ++i) { |
| 230 | const PointerCoords& resampledCoords = |
| 231 | calculateResampledCoords(pastSample.pointers[i].coords, |
| 232 | presentSample.pointers[i].coords, alpha); |
| 233 | resampledPointers.push_back(Pointer{presentSample.pointers[i].properties, resampledCoords}); |
| 234 | } |
| 235 | return Sample{newResampleTime, resampledPointers}; |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 238 | inline void LegacyResampler::addSampleToMotionEvent(const Sample& sample, |
| 239 | MotionEvent& motionEvent) { |
Paul Ramirez | cf1b06e | 2024-08-01 17:11:58 +0000 | [diff] [blame] | 240 | motionEvent.addSample(sample.eventTime.count(), sample.asPointerCoords().data(), |
| 241 | motionEvent.getId()); |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Paul Ramirez | cd7488c | 2024-09-13 23:01:12 +0000 | [diff] [blame^] | 244 | nanoseconds LegacyResampler::getResampleLatency() const { |
| 245 | return RESAMPLE_LATENCY; |
| 246 | } |
| 247 | |
Paul Ramirez | 6affbdb | 2024-09-11 22:20:26 +0000 | [diff] [blame] | 248 | void LegacyResampler::resampleMotionEvent(nanoseconds frameTime, MotionEvent& motionEvent, |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 249 | const InputMessage* futureSample) { |
| 250 | if (mPreviousDeviceId && *mPreviousDeviceId != motionEvent.getDeviceId()) { |
| 251 | mLatestSamples.clear(); |
| 252 | } |
| 253 | mPreviousDeviceId = motionEvent.getDeviceId(); |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 254 | |
Paul Ramirez | 6affbdb | 2024-09-11 22:20:26 +0000 | [diff] [blame] | 255 | const nanoseconds resampleTime = frameTime - RESAMPLE_LATENCY; |
| 256 | |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 257 | updateLatestSamples(motionEvent); |
Paul Ramirez | 486ca6d | 2024-08-12 14:37:02 +0000 | [diff] [blame] | 258 | |
| 259 | const std::optional<Sample> sample = (futureSample != nullptr) |
| 260 | ? (attemptInterpolation(resampleTime, *futureSample)) |
| 261 | : (attemptExtrapolation(resampleTime)); |
| 262 | if (sample.has_value()) { |
| 263 | addSampleToMotionEvent(*sample, motionEvent); |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 264 | } |
Paul Ramirez | be9c544 | 2024-07-10 00:12:41 +0000 | [diff] [blame] | 265 | } |
| 266 | } // namespace android |