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