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
21 #include <algorithm>
22 #include <array>
23 #include <cinttypes>
24 #include <cmath>
25 #include <cstddef>
26 #include <cstdint>
27 #include <limits>
28 #include <optional>
29 #include <string>
30 #include <utility>
31 #include <vector>
32
33 #include <android-base/logging.h>
34 #include <android-base/strings.h>
35 #include <android/input.h>
36 #include <com_android_input_flags.h>
37
38 #include <attestation/HmacKeyManager.h>
39 #include <ftl/enum.h>
40 #include <input/TfLiteMotionPredictor.h>
41
42 namespace input_flags = com::android::input::flags;
43
44 namespace android {
45 namespace {
46
47 /**
48 * Log debug messages about predictions.
49 * Enable this via "adb shell setprop log.tag.MotionPredictor DEBUG"
50 */
isDebug()51 bool isDebug() {
52 return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO);
53 }
54
55 // Converts a prediction of some polar (r, phi) to Cartesian (x, y) when applied to an axis.
convertPrediction(const TfLiteMotionPredictorSample::Point & axisFrom,const TfLiteMotionPredictorSample::Point & axisTo,float r,float phi)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
normalizeRange(float x,float min,float max)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
71 } // namespace
72
73 // --- JerkTracker ---
74
JerkTracker(bool normalizedDt,float alpha)75 JerkTracker::JerkTracker(bool normalizedDt, float alpha)
76 : mNormalizedDt(normalizedDt), mAlpha(alpha) {}
77
pushSample(int64_t timestamp,float xPos,float yPos)78 void JerkTracker::pushSample(int64_t timestamp, float xPos, float yPos) {
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();
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
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) {
126 mJerkMagnitude = mJerkMagnitude + (mAlpha * (newJerkMagnitude - mJerkMagnitude));
127 } else {
128 mJerkMagnitude = newJerkMagnitude;
129 }
130 }
131
132 std::swap(newXDerivatives, mXDerivatives);
133 std::swap(newYDerivatives, mYDerivatives);
134 }
135
reset()136 void JerkTracker::reset() {
137 mTimestamps.clear();
138 }
139
jerkMagnitude() const140 std::optional<float> JerkTracker::jerkMagnitude() const {
141 if (mTimestamps.size() == mTimestamps.capacity()) {
142 return mJerkMagnitude;
143 }
144 return std::nullopt;
145 }
146
147 // --- MotionPredictor ---
148
MotionPredictor(nsecs_t predictionTimestampOffsetNanos,std::function<bool ()> checkMotionPredictionEnabled,ReportAtomFunction reportAtomFunction)149 MotionPredictor::MotionPredictor(nsecs_t predictionTimestampOffsetNanos,
150 std::function<bool()> checkMotionPredictionEnabled,
151 ReportAtomFunction reportAtomFunction)
152 : mPredictionTimestampOffsetNanos(predictionTimestampOffsetNanos),
153 mCheckMotionPredictionEnabled(std::move(checkMotionPredictionEnabled)),
154 mReportAtomFunction(reportAtomFunction) {}
155
initializeObjects()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
record(const MotionEvent & event)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
177 // consistent with the previous gesture.
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 }
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());
187 return {};
188 }
189
190 if (!mModel) {
191 initializeObjects();
192 }
193
194 // Pass input event to the MetricsManager.
195 mMetricsManager->onRecord(event);
196
197 const int32_t action = event.getActionMasked();
198 if (action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_CANCEL) {
199 ALOGD_IF(isDebug(), "End of event stream");
200 mBuffers->reset();
201 mJerkTracker->reset();
202 mLastEvent.reset();
203 return {};
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());
207 return {};
208 }
209
210 if (event.getPointerCount() != 1) {
211 ALOGD_IF(isDebug(), "Prediction not supported for multiple pointers");
212 return {};
213 }
214
215 const ToolType toolType = event.getPointerProperties(0)->toolType;
216 if (toolType != ToolType::STYLUS) {
217 ALOGD_IF(isDebug(), "Prediction not supported for non-stylus tool: %s",
218 ftl::enum_string(toolType).c_str());
219 return {};
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);
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 });
236 mJerkTracker->pushSample(event.getHistoricalEventTime(i),
237 coords->getAxisValue(AMOTION_EVENT_AXIS_X),
238 coords->getAxisValue(AMOTION_EVENT_AXIS_Y));
239 }
240
241 if (!mLastEvent) {
242 mLastEvent = MotionEvent();
243 }
244 mLastEvent->copyFrom(&event, /*keepHistory=*/false);
245
246 return {};
247 }
248
predict(nsecs_t timestamp)249 std::unique_ptr<MotionEvent> MotionPredictor::predict(nsecs_t timestamp) {
250 if (mBuffers == nullptr || !mBuffers->isReady()) {
251 return nullptr;
252 }
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
286 const float jerkMagnitude = mJerkTracker->jerkMagnitude().value_or(0);
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);
297 for (size_t i = 0; i < static_cast<size_t>(predictedR.size()) && predictionTime <= futureTime;
298 ++i) {
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 }
310 if (input_flags::enable_prediction_pruning_via_jerk_thresholding()) {
311 if (i >= static_cast<size_t>(maxNumPredictions)) {
312 break;
313 }
314 }
315 // TODO(b/266747654): Stop predictions if confidence is < some
316 // threshold. Currently predictions are pruned via jerk thresholding.
317
318 const TfLiteMotionPredictorSample::Point predictedPoint =
319 convertPrediction(axisFrom, axisTo, predictedR[i], predictedPhi[i]);
320
321 ALOGD_IF(isDebug(), "prediction %zu: %f, %f", i, predictedPoint.x, predictedPoint.y);
322 PointerCoords coords;
323 coords.clear();
324 coords.setAxisValue(AMOTION_EVENT_AXIS_X, predictedPoint.x);
325 coords.setAxisValue(AMOTION_EVENT_AXIS_Y, predictedPoint.y);
326 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, predictedPressure[i]);
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));
334
335 predictionTime += mModel->config().predictionInterval;
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 {
348 prediction->addSample(predictionTime, &coords, prediction->getId());
349 }
350
351 axisFrom = axisTo;
352 axisTo = predictedPoint;
353 }
354
355 if (!hasPredictions) {
356 return nullptr;
357 }
358
359 // Pass predictions to the MetricsManager.
360 LOG_ALWAYS_FATAL_IF(!mMetricsManager);
361 mMetricsManager->onPredict(*prediction);
362
363 return prediction;
364 }
365
isPredictionAvailable(int32_t,int32_t source)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
382 } // namespace android
383