Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 "MotionPredictor" |
| 18 | |
| 19 | #include <input/MotionPredictor.h> |
| 20 | |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 21 | #include <cinttypes> |
| 22 | #include <cmath> |
| 23 | #include <cstddef> |
| 24 | #include <cstdint> |
Derek Wu | ea36ee7 | 2024-03-25 13:17:51 -0700 | [diff] [blame^] | 25 | #include <limits> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
Yeabkal Wubshit | 64f090f | 2023-03-03 17:35:11 -0800 | [diff] [blame] | 29 | #include <android-base/logging.h> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 30 | #include <android-base/strings.h> |
| 31 | #include <android/input.h> |
Derek Wu | ea36ee7 | 2024-03-25 13:17:51 -0700 | [diff] [blame^] | 32 | #include <com_android_input_flags.h> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 33 | |
| 34 | #include <attestation/HmacKeyManager.h> |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 35 | #include <ftl/enum.h> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 36 | #include <input/TfLiteMotionPredictor.h> |
| 37 | |
Derek Wu | ea36ee7 | 2024-03-25 13:17:51 -0700 | [diff] [blame^] | 38 | namespace input_flags = com::android::input::flags; |
| 39 | |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 40 | namespace android { |
| 41 | namespace { |
| 42 | |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Log debug messages about predictions. |
| 45 | * Enable this via "adb shell setprop log.tag.MotionPredictor DEBUG" |
| 46 | */ |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 47 | bool isDebug() { |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 48 | return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO); |
| 49 | } |
| 50 | |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 51 | // Converts a prediction of some polar (r, phi) to Cartesian (x, y) when applied to an axis. |
| 52 | TfLiteMotionPredictorSample::Point convertPrediction( |
| 53 | const TfLiteMotionPredictorSample::Point& axisFrom, |
| 54 | const TfLiteMotionPredictorSample::Point& axisTo, float r, float phi) { |
| 55 | const TfLiteMotionPredictorSample::Point axis = axisTo - axisFrom; |
| 56 | const float axis_phi = std::atan2(axis.y, axis.x); |
| 57 | const float x_delta = r * std::cos(axis_phi + phi); |
| 58 | const float y_delta = r * std::sin(axis_phi + phi); |
| 59 | return {.x = axisTo.x + x_delta, .y = axisTo.y + y_delta}; |
| 60 | } |
| 61 | |
| 62 | } // namespace |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 63 | |
| 64 | // --- MotionPredictor --- |
| 65 | |
Siarhei Vishniakou | fd0a68e | 2023-02-28 13:25:36 -0800 | [diff] [blame] | 66 | MotionPredictor::MotionPredictor(nsecs_t predictionTimestampOffsetNanos, |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 67 | std::function<bool()> checkMotionPredictionEnabled, |
| 68 | ReportAtomFunction reportAtomFunction) |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 69 | : mPredictionTimestampOffsetNanos(predictionTimestampOffsetNanos), |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 70 | mCheckMotionPredictionEnabled(std::move(checkMotionPredictionEnabled)), |
| 71 | mReportAtomFunction(reportAtomFunction) {} |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 72 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 73 | android::base::Result<void> MotionPredictor::record(const MotionEvent& event) { |
| 74 | if (mLastEvent && mLastEvent->getDeviceId() != event.getDeviceId()) { |
| 75 | // We still have an active gesture for another device. The provided MotionEvent is not |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 76 | // consistent with the previous gesture. |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 77 | LOG(ERROR) << "Inconsistent event stream: last event is " << *mLastEvent << ", but " |
| 78 | << __func__ << " is called with " << event; |
| 79 | return android::base::Error() |
| 80 | << "Inconsistent event stream: still have an active gesture from device " |
| 81 | << mLastEvent->getDeviceId() << ", but received " << event; |
| 82 | } |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 83 | if (!isPredictionAvailable(event.getDeviceId(), event.getSource())) { |
| 84 | ALOGE("Prediction not supported for device %d's %s source", event.getDeviceId(), |
| 85 | inputEventSourceToString(event.getSource()).c_str()); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 86 | return {}; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 87 | } |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 88 | |
Philip Quinn | bd66e62 | 2023-02-10 11:45:01 -0800 | [diff] [blame] | 89 | // Initialise the model now that it's likely to be used. |
| 90 | if (!mModel) { |
Siarhei Vishniakou | fd0a68e | 2023-02-28 13:25:36 -0800 | [diff] [blame] | 91 | mModel = TfLiteMotionPredictorModel::create(); |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 92 | LOG_ALWAYS_FATAL_IF(!mModel); |
Philip Quinn | bd66e62 | 2023-02-10 11:45:01 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 95 | if (!mBuffers) { |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 96 | mBuffers = std::make_unique<TfLiteMotionPredictorBuffers>(mModel->inputLength()); |
| 97 | } |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 98 | |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 99 | // Pass input event to the MetricsManager. |
| 100 | if (!mMetricsManager) { |
| 101 | mMetricsManager.emplace(mModel->config().predictionInterval, mModel->outputLength(), |
| 102 | mReportAtomFunction); |
| 103 | } |
| 104 | mMetricsManager->onRecord(event); |
| 105 | |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 106 | const int32_t action = event.getActionMasked(); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 107 | if (action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_CANCEL) { |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 108 | ALOGD_IF(isDebug(), "End of event stream"); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 109 | mBuffers->reset(); |
| 110 | mLastEvent.reset(); |
| 111 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 112 | } else if (action != AMOTION_EVENT_ACTION_DOWN && action != AMOTION_EVENT_ACTION_MOVE) { |
| 113 | ALOGD_IF(isDebug(), "Skipping unsupported %s action", |
| 114 | MotionEvent::actionToString(action).c_str()); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 115 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (event.getPointerCount() != 1) { |
| 119 | ALOGD_IF(isDebug(), "Prediction not supported for multiple pointers"); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 120 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 123 | const ToolType toolType = event.getPointerProperties(0)->toolType; |
| 124 | if (toolType != ToolType::STYLUS) { |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 125 | ALOGD_IF(isDebug(), "Prediction not supported for non-stylus tool: %s", |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 126 | ftl::enum_string(toolType).c_str()); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 127 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | for (size_t i = 0; i <= event.getHistorySize(); ++i) { |
| 131 | if (event.isResampled(0, i)) { |
| 132 | continue; |
| 133 | } |
| 134 | const PointerCoords* coords = event.getHistoricalRawPointerCoords(0, i); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 135 | mBuffers->pushSample(event.getHistoricalEventTime(i), |
| 136 | { |
| 137 | .position.x = coords->getAxisValue(AMOTION_EVENT_AXIS_X), |
| 138 | .position.y = coords->getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 139 | .pressure = event.getHistoricalPressure(0, i), |
| 140 | .tilt = event.getHistoricalAxisValue(AMOTION_EVENT_AXIS_TILT, |
| 141 | 0, i), |
| 142 | .orientation = event.getHistoricalOrientation(0, i), |
| 143 | }); |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 146 | if (!mLastEvent) { |
| 147 | mLastEvent = MotionEvent(); |
| 148 | } |
| 149 | mLastEvent->copyFrom(&event, /*keepHistory=*/false); |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 150 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 151 | return {}; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 154 | std::unique_ptr<MotionEvent> MotionPredictor::predict(nsecs_t timestamp) { |
| 155 | if (mBuffers == nullptr || !mBuffers->isReady()) { |
| 156 | return nullptr; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 157 | } |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 158 | |
| 159 | LOG_ALWAYS_FATAL_IF(!mModel); |
| 160 | mBuffers->copyTo(*mModel); |
| 161 | LOG_ALWAYS_FATAL_IF(!mModel->invoke()); |
| 162 | |
| 163 | // Read out the predictions. |
| 164 | const std::span<const float> predictedR = mModel->outputR(); |
| 165 | const std::span<const float> predictedPhi = mModel->outputPhi(); |
| 166 | const std::span<const float> predictedPressure = mModel->outputPressure(); |
| 167 | |
| 168 | TfLiteMotionPredictorSample::Point axisFrom = mBuffers->axisFrom().position; |
| 169 | TfLiteMotionPredictorSample::Point axisTo = mBuffers->axisTo().position; |
| 170 | |
| 171 | if (isDebug()) { |
| 172 | ALOGD("axisFrom: %f, %f", axisFrom.x, axisFrom.y); |
| 173 | ALOGD("axisTo: %f, %f", axisTo.x, axisTo.y); |
| 174 | ALOGD("mInputR: %s", base::Join(mModel->inputR(), ", ").c_str()); |
| 175 | ALOGD("mInputPhi: %s", base::Join(mModel->inputPhi(), ", ").c_str()); |
| 176 | ALOGD("mInputPressure: %s", base::Join(mModel->inputPressure(), ", ").c_str()); |
| 177 | ALOGD("mInputTilt: %s", base::Join(mModel->inputTilt(), ", ").c_str()); |
| 178 | ALOGD("mInputOrientation: %s", base::Join(mModel->inputOrientation(), ", ").c_str()); |
| 179 | ALOGD("predictedR: %s", base::Join(predictedR, ", ").c_str()); |
| 180 | ALOGD("predictedPhi: %s", base::Join(predictedPhi, ", ").c_str()); |
| 181 | ALOGD("predictedPressure: %s", base::Join(predictedPressure, ", ").c_str()); |
| 182 | } |
| 183 | |
| 184 | LOG_ALWAYS_FATAL_IF(!mLastEvent); |
| 185 | const MotionEvent& event = *mLastEvent; |
| 186 | bool hasPredictions = false; |
| 187 | std::unique_ptr<MotionEvent> prediction = std::make_unique<MotionEvent>(); |
| 188 | int64_t predictionTime = mBuffers->lastTimestamp(); |
| 189 | const int64_t futureTime = timestamp + mPredictionTimestampOffsetNanos; |
| 190 | |
Ryan Prichard | 5a8af50 | 2023-08-31 00:00:47 -0700 | [diff] [blame] | 191 | for (size_t i = 0; i < static_cast<size_t>(predictedR.size()) && predictionTime <= futureTime; |
| 192 | ++i) { |
Philip Quinn | 107ce70 | 2023-07-14 13:07:13 -0700 | [diff] [blame] | 193 | if (predictedR[i] < mModel->config().distanceNoiseFloor) { |
| 194 | // Stop predicting when the predicted output is below the model's noise floor. |
| 195 | // |
| 196 | // We assume that all subsequent predictions in the batch are unreliable because later |
| 197 | // predictions are conditional on earlier predictions, and a state of noise is not a |
| 198 | // good basis for prediction. |
| 199 | // |
| 200 | // The UX trade-off is that this potentially sacrifices some predictions when the input |
| 201 | // device starts to speed up, but avoids producing noisy predictions as it slows down. |
| 202 | break; |
| 203 | } |
Derek Wu | ea36ee7 | 2024-03-25 13:17:51 -0700 | [diff] [blame^] | 204 | if (input_flags::enable_prediction_pruning_via_jerk_thresholding()) { |
| 205 | // TODO(b/266747654): Stop predictions if confidence is < some threshold |
| 206 | // Arbitrarily high pruning index, will correct once jerk thresholding is implemented. |
| 207 | const size_t upperBoundPredictionIndex = std::numeric_limits<size_t>::max(); |
| 208 | if (i > upperBoundPredictionIndex) { |
| 209 | break; |
| 210 | } |
| 211 | } |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 212 | |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 213 | const TfLiteMotionPredictorSample::Point predictedPoint = |
| 214 | convertPrediction(axisFrom, axisTo, predictedR[i], predictedPhi[i]); |
| 215 | |
Ryan Prichard | 5a8af50 | 2023-08-31 00:00:47 -0700 | [diff] [blame] | 216 | ALOGD_IF(isDebug(), "prediction %zu: %f, %f", i, predictedPoint.x, predictedPoint.y); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 217 | PointerCoords coords; |
| 218 | coords.clear(); |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 219 | coords.setAxisValue(AMOTION_EVENT_AXIS_X, predictedPoint.x); |
| 220 | coords.setAxisValue(AMOTION_EVENT_AXIS_Y, predictedPoint.y); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 221 | coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, predictedPressure[i]); |
Philip Quinn | 59fa912 | 2023-09-18 13:35:54 -0700 | [diff] [blame] | 222 | // Copy forward tilt and orientation from the last event until they are predicted |
| 223 | // (b/291789258). |
| 224 | coords.setAxisValue(AMOTION_EVENT_AXIS_TILT, |
| 225 | event.getAxisValue(AMOTION_EVENT_AXIS_TILT, 0)); |
| 226 | coords.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, |
| 227 | event.getRawPointerCoords(0)->getAxisValue( |
| 228 | AMOTION_EVENT_AXIS_ORIENTATION)); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 229 | |
Philip Quinn | 107ce70 | 2023-07-14 13:07:13 -0700 | [diff] [blame] | 230 | predictionTime += mModel->config().predictionInterval; |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 231 | if (i == 0) { |
| 232 | hasPredictions = true; |
| 233 | prediction->initialize(InputEvent::nextId(), event.getDeviceId(), event.getSource(), |
| 234 | event.getDisplayId(), INVALID_HMAC, AMOTION_EVENT_ACTION_MOVE, |
| 235 | event.getActionButton(), event.getFlags(), event.getEdgeFlags(), |
| 236 | event.getMetaState(), event.getButtonState(), |
| 237 | event.getClassification(), event.getTransform(), |
| 238 | event.getXPrecision(), event.getYPrecision(), |
| 239 | event.getRawXCursorPosition(), event.getRawYCursorPosition(), |
| 240 | event.getRawTransform(), event.getDownTime(), predictionTime, |
| 241 | event.getPointerCount(), event.getPointerProperties(), &coords); |
| 242 | } else { |
| 243 | prediction->addSample(predictionTime, &coords); |
| 244 | } |
| 245 | |
| 246 | axisFrom = axisTo; |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 247 | axisTo = predictedPoint; |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 248 | } |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 249 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 250 | if (!hasPredictions) { |
| 251 | return nullptr; |
| 252 | } |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 253 | |
| 254 | // Pass predictions to the MetricsManager. |
| 255 | LOG_ALWAYS_FATAL_IF(!mMetricsManager); |
| 256 | mMetricsManager->onPredict(*prediction); |
| 257 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 258 | return prediction; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | bool MotionPredictor::isPredictionAvailable(int32_t /*deviceId*/, int32_t source) { |
| 262 | // Global flag override |
| 263 | if (!mCheckMotionPredictionEnabled()) { |
| 264 | ALOGD_IF(isDebug(), "Prediction not available due to flag override"); |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | // Prediction is only supported for stylus sources. |
| 269 | if (!isFromSource(source, AINPUT_SOURCE_STYLUS)) { |
| 270 | ALOGD_IF(isDebug(), "Prediction not available for non-stylus source: %s", |
| 271 | inputEventSourceToString(source).c_str()); |
| 272 | return false; |
| 273 | } |
| 274 | return true; |
| 275 | } |
| 276 | |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 277 | } // namespace android |