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 | |
Derek Wu | aaa4731 | 2024-03-26 15:53:44 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 22 | #include <array> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 23 | #include <cinttypes> |
| 24 | #include <cmath> |
| 25 | #include <cstddef> |
| 26 | #include <cstdint> |
Derek Wu | ea36ee7 | 2024-03-25 13:17:51 -0700 | [diff] [blame] | 27 | #include <limits> |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 28 | #include <optional> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 29 | #include <string> |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 30 | #include <utility> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Yeabkal Wubshit | 64f090f | 2023-03-03 17:35:11 -0800 | [diff] [blame] | 33 | #include <android-base/logging.h> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 34 | #include <android-base/strings.h> |
| 35 | #include <android/input.h> |
Derek Wu | ea36ee7 | 2024-03-25 13:17:51 -0700 | [diff] [blame] | 36 | #include <com_android_input_flags.h> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 37 | |
| 38 | #include <attestation/HmacKeyManager.h> |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 39 | #include <ftl/enum.h> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 40 | #include <input/TfLiteMotionPredictor.h> |
| 41 | |
Derek Wu | ea36ee7 | 2024-03-25 13:17:51 -0700 | [diff] [blame] | 42 | namespace input_flags = com::android::input::flags; |
| 43 | |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 44 | namespace android { |
| 45 | namespace { |
| 46 | |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 47 | /** |
| 48 | * Log debug messages about predictions. |
| 49 | * Enable this via "adb shell setprop log.tag.MotionPredictor DEBUG" |
| 50 | */ |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 51 | bool isDebug() { |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 52 | return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO); |
| 53 | } |
| 54 | |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 55 | // Converts a prediction of some polar (r, phi) to Cartesian (x, y) when applied to an axis. |
| 56 | TfLiteMotionPredictorSample::Point convertPrediction( |
| 57 | const TfLiteMotionPredictorSample::Point& axisFrom, |
| 58 | const TfLiteMotionPredictorSample::Point& axisTo, float r, float phi) { |
| 59 | const TfLiteMotionPredictorSample::Point axis = axisTo - axisFrom; |
| 60 | const float axis_phi = std::atan2(axis.y, axis.x); |
| 61 | const float x_delta = r * std::cos(axis_phi + phi); |
| 62 | const float y_delta = r * std::sin(axis_phi + phi); |
| 63 | return {.x = axisTo.x + x_delta, .y = axisTo.y + y_delta}; |
| 64 | } |
| 65 | |
Derek Wu | aaa4731 | 2024-03-26 15:53:44 -0700 | [diff] [blame] | 66 | float normalizeRange(float x, float min, float max) { |
| 67 | const float normalized = (x - min) / (max - min); |
| 68 | return std::min(1.0f, std::max(0.0f, normalized)); |
| 69 | } |
| 70 | |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 71 | } // namespace |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 72 | |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 73 | // --- JerkTracker --- |
| 74 | |
Derek Wu | cc6aec5 | 2024-07-30 11:59:56 +0000 | [diff] [blame] | 75 | JerkTracker::JerkTracker(bool normalizedDt, float alpha) |
| 76 | : mNormalizedDt(normalizedDt), mAlpha(alpha) {} |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 77 | |
| 78 | void JerkTracker::pushSample(int64_t timestamp, float xPos, float yPos) { |
Derek Wu | 5e0e7cf | 2024-07-04 11:14:18 +0000 | [diff] [blame] | 79 | // If we previously had full samples, we have a previous jerk calculation |
| 80 | // to do weighted smoothing. |
| 81 | const bool applySmoothing = mTimestamps.size() == mTimestamps.capacity(); |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 82 | mTimestamps.pushBack(timestamp); |
| 83 | const int numSamples = mTimestamps.size(); |
| 84 | |
| 85 | std::array<float, 4> newXDerivatives; |
| 86 | std::array<float, 4> newYDerivatives; |
| 87 | |
| 88 | /** |
| 89 | * Diagram showing the calculation of higher order derivatives of sample x3 |
| 90 | * collected at time=t3. |
| 91 | * Terms in parentheses are not stored (and not needed for calculations) |
| 92 | * t0 ----- t1 ----- t2 ----- t3 |
| 93 | * (x0)-----(x1) ----- x2 ----- x3 |
| 94 | * (x'0) --- x'1 --- x'2 |
| 95 | * x''0 - x''1 |
| 96 | * x'''0 |
| 97 | * |
| 98 | * In this example: |
| 99 | * x'2 = (x3 - x2) / (t3 - t2) |
| 100 | * x''1 = (x'2 - x'1) / (t2 - t1) |
| 101 | * x'''0 = (x''1 - x''0) / (t1 - t0) |
| 102 | * Therefore, timestamp history is needed to calculate higher order derivatives, |
| 103 | * compared to just the last calculated derivative sample. |
| 104 | * |
| 105 | * If mNormalizedDt = true, then dt = 1 and the division is moot. |
| 106 | */ |
| 107 | for (int i = 0; i < numSamples; ++i) { |
| 108 | if (i == 0) { |
| 109 | newXDerivatives[i] = xPos; |
| 110 | newYDerivatives[i] = yPos; |
| 111 | } else { |
| 112 | newXDerivatives[i] = newXDerivatives[i - 1] - mXDerivatives[i - 1]; |
| 113 | newYDerivatives[i] = newYDerivatives[i - 1] - mYDerivatives[i - 1]; |
| 114 | if (!mNormalizedDt) { |
| 115 | const float dt = mTimestamps[numSamples - i] - mTimestamps[numSamples - i - 1]; |
| 116 | newXDerivatives[i] = newXDerivatives[i] / dt; |
| 117 | newYDerivatives[i] = newYDerivatives[i] / dt; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Derek Wu | 5e0e7cf | 2024-07-04 11:14:18 +0000 | [diff] [blame] | 122 | if (numSamples == static_cast<int>(mTimestamps.capacity())) { |
| 123 | float newJerkMagnitude = std::hypot(newXDerivatives[3], newYDerivatives[3]); |
| 124 | ALOGD_IF(isDebug(), "raw jerk: %f", newJerkMagnitude); |
| 125 | if (applySmoothing) { |
Derek Wu | cc6aec5 | 2024-07-30 11:59:56 +0000 | [diff] [blame] | 126 | mJerkMagnitude = mJerkMagnitude + (mAlpha * (newJerkMagnitude - mJerkMagnitude)); |
Derek Wu | 5e0e7cf | 2024-07-04 11:14:18 +0000 | [diff] [blame] | 127 | } else { |
| 128 | mJerkMagnitude = newJerkMagnitude; |
| 129 | } |
| 130 | } |
| 131 | |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 132 | std::swap(newXDerivatives, mXDerivatives); |
| 133 | std::swap(newYDerivatives, mYDerivatives); |
| 134 | } |
| 135 | |
| 136 | void JerkTracker::reset() { |
| 137 | mTimestamps.clear(); |
| 138 | } |
| 139 | |
| 140 | std::optional<float> JerkTracker::jerkMagnitude() const { |
| 141 | if (mTimestamps.size() == mTimestamps.capacity()) { |
Derek Wu | 5e0e7cf | 2024-07-04 11:14:18 +0000 | [diff] [blame] | 142 | return mJerkMagnitude; |
Derek Wu | 705068d | 2024-03-20 10:41:37 -0700 | [diff] [blame] | 143 | } |
| 144 | return std::nullopt; |
| 145 | } |
| 146 | |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 147 | // --- MotionPredictor --- |
| 148 | |
Siarhei Vishniakou | fd0a68e | 2023-02-28 13:25:36 -0800 | [diff] [blame] | 149 | MotionPredictor::MotionPredictor(nsecs_t predictionTimestampOffsetNanos, |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 150 | std::function<bool()> checkMotionPredictionEnabled, |
| 151 | ReportAtomFunction reportAtomFunction) |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 152 | : mPredictionTimestampOffsetNanos(predictionTimestampOffsetNanos), |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 153 | mCheckMotionPredictionEnabled(std::move(checkMotionPredictionEnabled)), |
| 154 | mReportAtomFunction(reportAtomFunction) {} |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 155 | |
Derek Wu | cc6aec5 | 2024-07-30 11:59:56 +0000 | [diff] [blame] | 156 | void MotionPredictor::initializeObjects() { |
| 157 | mModel = TfLiteMotionPredictorModel::create(); |
| 158 | LOG_ALWAYS_FATAL_IF(!mModel); |
| 159 | |
| 160 | // mJerkTracker assumes normalized dt = 1 between recorded samples because |
| 161 | // the underlying mModel input also assumes fixed-interval samples. |
| 162 | // Normalized dt as 1 is also used to correspond with the similar Jank |
| 163 | // implementation from the JetPack MotionPredictor implementation. |
| 164 | mJerkTracker = std::make_unique<JerkTracker>(/*normalizedDt=*/true, mModel->config().jerkAlpha); |
| 165 | |
| 166 | mBuffers = std::make_unique<TfLiteMotionPredictorBuffers>(mModel->inputLength()); |
| 167 | |
| 168 | mMetricsManager = |
| 169 | std::make_unique<MotionPredictorMetricsManager>(mModel->config().predictionInterval, |
| 170 | mModel->outputLength(), |
| 171 | mReportAtomFunction); |
| 172 | } |
| 173 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 174 | android::base::Result<void> MotionPredictor::record(const MotionEvent& event) { |
| 175 | if (mLastEvent && mLastEvent->getDeviceId() != event.getDeviceId()) { |
| 176 | // 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] | 177 | // consistent with the previous gesture. |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 178 | LOG(ERROR) << "Inconsistent event stream: last event is " << *mLastEvent << ", but " |
| 179 | << __func__ << " is called with " << event; |
| 180 | return android::base::Error() |
| 181 | << "Inconsistent event stream: still have an active gesture from device " |
| 182 | << mLastEvent->getDeviceId() << ", but received " << event; |
| 183 | } |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 184 | if (!isPredictionAvailable(event.getDeviceId(), event.getSource())) { |
| 185 | ALOGE("Prediction not supported for device %d's %s source", event.getDeviceId(), |
| 186 | inputEventSourceToString(event.getSource()).c_str()); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 187 | return {}; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 188 | } |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 189 | |
Philip Quinn | bd66e62 | 2023-02-10 11:45:01 -0800 | [diff] [blame] | 190 | if (!mModel) { |
Derek Wu | cc6aec5 | 2024-07-30 11:59:56 +0000 | [diff] [blame] | 191 | initializeObjects(); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 192 | } |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 193 | |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 194 | // Pass input event to the MetricsManager. |
Cody Heiner | 7b26dbe | 2023-11-14 14:47:10 -0800 | [diff] [blame] | 195 | mMetricsManager->onRecord(event); |
| 196 | |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 197 | const int32_t action = event.getActionMasked(); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 198 | if (action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_CANCEL) { |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 199 | ALOGD_IF(isDebug(), "End of event stream"); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 200 | mBuffers->reset(); |
Derek Wu | cc6aec5 | 2024-07-30 11:59:56 +0000 | [diff] [blame] | 201 | mJerkTracker->reset(); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 202 | mLastEvent.reset(); |
| 203 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 204 | } else if (action != AMOTION_EVENT_ACTION_DOWN && action != AMOTION_EVENT_ACTION_MOVE) { |
| 205 | ALOGD_IF(isDebug(), "Skipping unsupported %s action", |
| 206 | MotionEvent::actionToString(action).c_str()); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 207 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if (event.getPointerCount() != 1) { |
| 211 | ALOGD_IF(isDebug(), "Prediction not supported for multiple pointers"); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 212 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 215 | const ToolType toolType = event.getPointerProperties(0)->toolType; |
| 216 | if (toolType != ToolType::STYLUS) { |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 217 | ALOGD_IF(isDebug(), "Prediction not supported for non-stylus tool: %s", |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 218 | ftl::enum_string(toolType).c_str()); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 219 | return {}; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | for (size_t i = 0; i <= event.getHistorySize(); ++i) { |
| 223 | if (event.isResampled(0, i)) { |
| 224 | continue; |
| 225 | } |
| 226 | const PointerCoords* coords = event.getHistoricalRawPointerCoords(0, i); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 227 | mBuffers->pushSample(event.getHistoricalEventTime(i), |
| 228 | { |
| 229 | .position.x = coords->getAxisValue(AMOTION_EVENT_AXIS_X), |
| 230 | .position.y = coords->getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 231 | .pressure = event.getHistoricalPressure(0, i), |
| 232 | .tilt = event.getHistoricalAxisValue(AMOTION_EVENT_AXIS_TILT, |
| 233 | 0, i), |
| 234 | .orientation = event.getHistoricalOrientation(0, i), |
| 235 | }); |
Derek Wu | cc6aec5 | 2024-07-30 11:59:56 +0000 | [diff] [blame] | 236 | mJerkTracker->pushSample(event.getHistoricalEventTime(i), |
| 237 | coords->getAxisValue(AMOTION_EVENT_AXIS_X), |
| 238 | coords->getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 241 | if (!mLastEvent) { |
| 242 | mLastEvent = MotionEvent(); |
| 243 | } |
| 244 | mLastEvent->copyFrom(&event, /*keepHistory=*/false); |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 245 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 246 | return {}; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 249 | std::unique_ptr<MotionEvent> MotionPredictor::predict(nsecs_t timestamp) { |
| 250 | if (mBuffers == nullptr || !mBuffers->isReady()) { |
| 251 | return nullptr; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 252 | } |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 253 | |
| 254 | LOG_ALWAYS_FATAL_IF(!mModel); |
| 255 | mBuffers->copyTo(*mModel); |
| 256 | LOG_ALWAYS_FATAL_IF(!mModel->invoke()); |
| 257 | |
| 258 | // Read out the predictions. |
| 259 | const std::span<const float> predictedR = mModel->outputR(); |
| 260 | const std::span<const float> predictedPhi = mModel->outputPhi(); |
| 261 | const std::span<const float> predictedPressure = mModel->outputPressure(); |
| 262 | |
| 263 | TfLiteMotionPredictorSample::Point axisFrom = mBuffers->axisFrom().position; |
| 264 | TfLiteMotionPredictorSample::Point axisTo = mBuffers->axisTo().position; |
| 265 | |
| 266 | if (isDebug()) { |
| 267 | ALOGD("axisFrom: %f, %f", axisFrom.x, axisFrom.y); |
| 268 | ALOGD("axisTo: %f, %f", axisTo.x, axisTo.y); |
| 269 | ALOGD("mInputR: %s", base::Join(mModel->inputR(), ", ").c_str()); |
| 270 | ALOGD("mInputPhi: %s", base::Join(mModel->inputPhi(), ", ").c_str()); |
| 271 | ALOGD("mInputPressure: %s", base::Join(mModel->inputPressure(), ", ").c_str()); |
| 272 | ALOGD("mInputTilt: %s", base::Join(mModel->inputTilt(), ", ").c_str()); |
| 273 | ALOGD("mInputOrientation: %s", base::Join(mModel->inputOrientation(), ", ").c_str()); |
| 274 | ALOGD("predictedR: %s", base::Join(predictedR, ", ").c_str()); |
| 275 | ALOGD("predictedPhi: %s", base::Join(predictedPhi, ", ").c_str()); |
| 276 | ALOGD("predictedPressure: %s", base::Join(predictedPressure, ", ").c_str()); |
| 277 | } |
| 278 | |
| 279 | LOG_ALWAYS_FATAL_IF(!mLastEvent); |
| 280 | const MotionEvent& event = *mLastEvent; |
| 281 | bool hasPredictions = false; |
| 282 | std::unique_ptr<MotionEvent> prediction = std::make_unique<MotionEvent>(); |
| 283 | int64_t predictionTime = mBuffers->lastTimestamp(); |
| 284 | const int64_t futureTime = timestamp + mPredictionTimestampOffsetNanos; |
| 285 | |
Derek Wu | cc6aec5 | 2024-07-30 11:59:56 +0000 | [diff] [blame] | 286 | const float jerkMagnitude = mJerkTracker->jerkMagnitude().value_or(0); |
Derek Wu | aaa4731 | 2024-03-26 15:53:44 -0700 | [diff] [blame] | 287 | const float fractionKept = |
| 288 | 1 - normalizeRange(jerkMagnitude, mModel->config().lowJerk, mModel->config().highJerk); |
| 289 | // float to ensure proper division below. |
| 290 | const float predictionTimeWindow = futureTime - predictionTime; |
| 291 | const int maxNumPredictions = static_cast<int>( |
| 292 | std::ceil(predictionTimeWindow / mModel->config().predictionInterval * fractionKept)); |
| 293 | ALOGD_IF(isDebug(), |
| 294 | "jerk (d^3p/normalizedDt^3): %f, fraction of prediction window pruned: %f, max number " |
| 295 | "of predictions: %d", |
| 296 | jerkMagnitude, 1 - fractionKept, maxNumPredictions); |
Ryan Prichard | 5a8af50 | 2023-08-31 00:00:47 -0700 | [diff] [blame] | 297 | for (size_t i = 0; i < static_cast<size_t>(predictedR.size()) && predictionTime <= futureTime; |
| 298 | ++i) { |
Philip Quinn | 107ce70 | 2023-07-14 13:07:13 -0700 | [diff] [blame] | 299 | if (predictedR[i] < mModel->config().distanceNoiseFloor) { |
| 300 | // Stop predicting when the predicted output is below the model's noise floor. |
| 301 | // |
| 302 | // We assume that all subsequent predictions in the batch are unreliable because later |
| 303 | // predictions are conditional on earlier predictions, and a state of noise is not a |
| 304 | // good basis for prediction. |
| 305 | // |
| 306 | // The UX trade-off is that this potentially sacrifices some predictions when the input |
| 307 | // device starts to speed up, but avoids producing noisy predictions as it slows down. |
| 308 | break; |
| 309 | } |
Derek Wu | ea36ee7 | 2024-03-25 13:17:51 -0700 | [diff] [blame] | 310 | if (input_flags::enable_prediction_pruning_via_jerk_thresholding()) { |
Derek Wu | aaa4731 | 2024-03-26 15:53:44 -0700 | [diff] [blame] | 311 | if (i >= static_cast<size_t>(maxNumPredictions)) { |
Derek Wu | ea36ee7 | 2024-03-25 13:17:51 -0700 | [diff] [blame] | 312 | break; |
| 313 | } |
| 314 | } |
Derek Wu | aaa4731 | 2024-03-26 15:53:44 -0700 | [diff] [blame] | 315 | // TODO(b/266747654): Stop predictions if confidence is < some |
| 316 | // threshold. Currently predictions are pruned via jerk thresholding. |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 317 | |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 318 | const TfLiteMotionPredictorSample::Point predictedPoint = |
| 319 | convertPrediction(axisFrom, axisTo, predictedR[i], predictedPhi[i]); |
| 320 | |
Ryan Prichard | 5a8af50 | 2023-08-31 00:00:47 -0700 | [diff] [blame] | 321 | ALOGD_IF(isDebug(), "prediction %zu: %f, %f", i, predictedPoint.x, predictedPoint.y); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 322 | PointerCoords coords; |
| 323 | coords.clear(); |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 324 | coords.setAxisValue(AMOTION_EVENT_AXIS_X, predictedPoint.x); |
| 325 | coords.setAxisValue(AMOTION_EVENT_AXIS_Y, predictedPoint.y); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 326 | coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, predictedPressure[i]); |
Philip Quinn | 59fa912 | 2023-09-18 13:35:54 -0700 | [diff] [blame] | 327 | // Copy forward tilt and orientation from the last event until they are predicted |
| 328 | // (b/291789258). |
| 329 | coords.setAxisValue(AMOTION_EVENT_AXIS_TILT, |
| 330 | event.getAxisValue(AMOTION_EVENT_AXIS_TILT, 0)); |
| 331 | coords.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, |
| 332 | event.getRawPointerCoords(0)->getAxisValue( |
| 333 | AMOTION_EVENT_AXIS_ORIENTATION)); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 334 | |
Philip Quinn | 107ce70 | 2023-07-14 13:07:13 -0700 | [diff] [blame] | 335 | predictionTime += mModel->config().predictionInterval; |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 336 | if (i == 0) { |
| 337 | hasPredictions = true; |
| 338 | prediction->initialize(InputEvent::nextId(), event.getDeviceId(), event.getSource(), |
| 339 | event.getDisplayId(), INVALID_HMAC, AMOTION_EVENT_ACTION_MOVE, |
| 340 | event.getActionButton(), event.getFlags(), event.getEdgeFlags(), |
| 341 | event.getMetaState(), event.getButtonState(), |
| 342 | event.getClassification(), event.getTransform(), |
| 343 | event.getXPrecision(), event.getYPrecision(), |
| 344 | event.getRawXCursorPosition(), event.getRawYCursorPosition(), |
| 345 | event.getRawTransform(), event.getDownTime(), predictionTime, |
| 346 | event.getPointerCount(), event.getPointerProperties(), &coords); |
| 347 | } else { |
jioana | 71c6f73 | 2024-07-16 15:42:56 +0000 | [diff] [blame] | 348 | prediction->addSample(predictionTime, &coords, prediction->getId()); |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | axisFrom = axisTo; |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 352 | axisTo = predictedPoint; |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 353 | } |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 354 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 355 | if (!hasPredictions) { |
| 356 | return nullptr; |
| 357 | } |
Cody Heiner | 088c63e | 2023-06-15 12:06:09 -0700 | [diff] [blame] | 358 | |
| 359 | // Pass predictions to the MetricsManager. |
| 360 | LOG_ALWAYS_FATAL_IF(!mMetricsManager); |
| 361 | mMetricsManager->onPredict(*prediction); |
| 362 | |
Siarhei Vishniakou | 33cb38b | 2023-02-23 18:52:34 -0800 | [diff] [blame] | 363 | return prediction; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | bool MotionPredictor::isPredictionAvailable(int32_t /*deviceId*/, int32_t source) { |
| 367 | // Global flag override |
| 368 | if (!mCheckMotionPredictionEnabled()) { |
| 369 | ALOGD_IF(isDebug(), "Prediction not available due to flag override"); |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | // Prediction is only supported for stylus sources. |
| 374 | if (!isFromSource(source, AINPUT_SOURCE_STYLUS)) { |
| 375 | ALOGD_IF(isDebug(), "Prediction not available for non-stylus source: %s", |
| 376 | inputEventSourceToString(source).c_str()); |
| 377 | return false; |
| 378 | } |
| 379 | return true; |
| 380 | } |
| 381 | |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 382 | } // namespace android |