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 | |
| 28 | #include <android-base/strings.h> |
| 29 | #include <android/input.h> |
| 30 | #include <log/log.h> |
| 31 | |
| 32 | #include <attestation/HmacKeyManager.h> |
Siarhei Vishniakou | 6d73f83 | 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 |
| 70 | // consistent the previous gesture. |
| 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(); |
Philip Quinn | bd66e62 | 2023-02-10 11:45:01 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 88 | if (mBuffers == nullptr) { |
| 89 | mBuffers = std::make_unique<TfLiteMotionPredictorBuffers>(mModel->inputLength()); |
| 90 | } |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 91 | |
| 92 | const int32_t action = event.getActionMasked(); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 93 | if (action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_CANCEL) { |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 94 | ALOGD_IF(isDebug(), "End of event stream"); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 95 | mBuffers->reset(); |
| 96 | mLastEvent.reset(); |
| 97 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 98 | } else if (action != AMOTION_EVENT_ACTION_DOWN && action != AMOTION_EVENT_ACTION_MOVE) { |
| 99 | ALOGD_IF(isDebug(), "Skipping unsupported %s action", |
| 100 | MotionEvent::actionToString(action).c_str()); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 101 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | if (event.getPointerCount() != 1) { |
| 105 | ALOGD_IF(isDebug(), "Prediction not supported for multiple pointers"); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 106 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 109 | const ToolType toolType = event.getPointerProperties(0)->toolType; |
| 110 | if (toolType != ToolType::STYLUS) { |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 111 | ALOGD_IF(isDebug(), "Prediction not supported for non-stylus tool: %s", |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 112 | ftl::enum_string(toolType).c_str()); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 113 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | for (size_t i = 0; i <= event.getHistorySize(); ++i) { |
| 117 | if (event.isResampled(0, i)) { |
| 118 | continue; |
| 119 | } |
| 120 | const PointerCoords* coords = event.getHistoricalRawPointerCoords(0, i); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 121 | mBuffers->pushSample(event.getHistoricalEventTime(i), |
| 122 | { |
| 123 | .position.x = coords->getAxisValue(AMOTION_EVENT_AXIS_X), |
| 124 | .position.y = coords->getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 125 | .pressure = event.getHistoricalPressure(0, i), |
| 126 | .tilt = event.getHistoricalAxisValue(AMOTION_EVENT_AXIS_TILT, |
| 127 | 0, i), |
| 128 | .orientation = event.getHistoricalOrientation(0, i), |
| 129 | }); |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 132 | if (!mLastEvent) { |
| 133 | mLastEvent = MotionEvent(); |
| 134 | } |
| 135 | mLastEvent->copyFrom(&event, /*keepHistory=*/false); |
| 136 | return {}; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 139 | std::unique_ptr<MotionEvent> MotionPredictor::predict(nsecs_t timestamp) { |
| 140 | if (mBuffers == nullptr || !mBuffers->isReady()) { |
| 141 | return nullptr; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 142 | } |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 143 | |
| 144 | LOG_ALWAYS_FATAL_IF(!mModel); |
| 145 | mBuffers->copyTo(*mModel); |
| 146 | LOG_ALWAYS_FATAL_IF(!mModel->invoke()); |
| 147 | |
| 148 | // Read out the predictions. |
| 149 | const std::span<const float> predictedR = mModel->outputR(); |
| 150 | const std::span<const float> predictedPhi = mModel->outputPhi(); |
| 151 | const std::span<const float> predictedPressure = mModel->outputPressure(); |
| 152 | |
| 153 | TfLiteMotionPredictorSample::Point axisFrom = mBuffers->axisFrom().position; |
| 154 | TfLiteMotionPredictorSample::Point axisTo = mBuffers->axisTo().position; |
| 155 | |
| 156 | if (isDebug()) { |
| 157 | ALOGD("axisFrom: %f, %f", axisFrom.x, axisFrom.y); |
| 158 | ALOGD("axisTo: %f, %f", axisTo.x, axisTo.y); |
| 159 | ALOGD("mInputR: %s", base::Join(mModel->inputR(), ", ").c_str()); |
| 160 | ALOGD("mInputPhi: %s", base::Join(mModel->inputPhi(), ", ").c_str()); |
| 161 | ALOGD("mInputPressure: %s", base::Join(mModel->inputPressure(), ", ").c_str()); |
| 162 | ALOGD("mInputTilt: %s", base::Join(mModel->inputTilt(), ", ").c_str()); |
| 163 | ALOGD("mInputOrientation: %s", base::Join(mModel->inputOrientation(), ", ").c_str()); |
| 164 | ALOGD("predictedR: %s", base::Join(predictedR, ", ").c_str()); |
| 165 | ALOGD("predictedPhi: %s", base::Join(predictedPhi, ", ").c_str()); |
| 166 | ALOGD("predictedPressure: %s", base::Join(predictedPressure, ", ").c_str()); |
| 167 | } |
| 168 | |
| 169 | LOG_ALWAYS_FATAL_IF(!mLastEvent); |
| 170 | const MotionEvent& event = *mLastEvent; |
| 171 | bool hasPredictions = false; |
| 172 | std::unique_ptr<MotionEvent> prediction = std::make_unique<MotionEvent>(); |
| 173 | int64_t predictionTime = mBuffers->lastTimestamp(); |
| 174 | const int64_t futureTime = timestamp + mPredictionTimestampOffsetNanos; |
| 175 | |
| 176 | for (int i = 0; i < predictedR.size() && predictionTime <= futureTime; ++i) { |
| 177 | const TfLiteMotionPredictorSample::Point point = |
| 178 | convertPrediction(axisFrom, axisTo, predictedR[i], predictedPhi[i]); |
| 179 | // TODO(b/266747654): Stop predictions if confidence is < some threshold. |
| 180 | |
| 181 | ALOGD_IF(isDebug(), "prediction %d: %f, %f", i, point.x, point.y); |
| 182 | PointerCoords coords; |
| 183 | coords.clear(); |
| 184 | coords.setAxisValue(AMOTION_EVENT_AXIS_X, point.x); |
| 185 | coords.setAxisValue(AMOTION_EVENT_AXIS_Y, point.y); |
| 186 | // TODO(b/266747654): Stop predictions if predicted pressure is < some threshold. |
| 187 | coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, predictedPressure[i]); |
| 188 | |
Philip Quinn | f84fa49 | 2023-06-26 14:15:15 -0700 | [diff] [blame^] | 189 | predictionTime += mModel->predictionInterval(); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 190 | if (i == 0) { |
| 191 | hasPredictions = true; |
| 192 | prediction->initialize(InputEvent::nextId(), event.getDeviceId(), event.getSource(), |
| 193 | event.getDisplayId(), INVALID_HMAC, AMOTION_EVENT_ACTION_MOVE, |
| 194 | event.getActionButton(), event.getFlags(), event.getEdgeFlags(), |
| 195 | event.getMetaState(), event.getButtonState(), |
| 196 | event.getClassification(), event.getTransform(), |
| 197 | event.getXPrecision(), event.getYPrecision(), |
| 198 | event.getRawXCursorPosition(), event.getRawYCursorPosition(), |
| 199 | event.getRawTransform(), event.getDownTime(), predictionTime, |
| 200 | event.getPointerCount(), event.getPointerProperties(), &coords); |
| 201 | } else { |
| 202 | prediction->addSample(predictionTime, &coords); |
| 203 | } |
| 204 | |
| 205 | axisFrom = axisTo; |
| 206 | axisTo = point; |
| 207 | } |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 208 | if (!hasPredictions) { |
| 209 | return nullptr; |
| 210 | } |
| 211 | return prediction; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | bool MotionPredictor::isPredictionAvailable(int32_t /*deviceId*/, int32_t source) { |
| 215 | // Global flag override |
| 216 | if (!mCheckMotionPredictionEnabled()) { |
| 217 | ALOGD_IF(isDebug(), "Prediction not available due to flag override"); |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | // Prediction is only supported for stylus sources. |
| 222 | if (!isFromSource(source, AINPUT_SOURCE_STYLUS)) { |
| 223 | ALOGD_IF(isDebug(), "Prediction not available for non-stylus source: %s", |
| 224 | inputEventSourceToString(source).c_str()); |
| 225 | return false; |
| 226 | } |
| 227 | return true; |
| 228 | } |
| 229 | |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 230 | } // namespace android |