blob: 3b6e40183fa4175846fdd110eecc270f0ca4d7b7 [file] [log] [blame]
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -08001/*
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#pragma once
18
Philip Quinn8f953ab2022-12-06 15:37:07 -080019#include <cstdint>
20#include <memory>
21#include <mutex>
Cody Heiner7b26dbe2023-11-14 14:47:10 -080022#include <optional>
Philip Quinnbd66e622023-02-10 11:45:01 -080023#include <string>
Philip Quinn8f953ab2022-12-06 15:37:07 -080024#include <unordered_map>
25
Siarhei Vishniakou33cb38b2023-02-23 18:52:34 -080026#include <android-base/result.h>
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080027#include <android-base/thread_annotations.h>
28#include <android/sysprop/InputProperties.sysprop.h>
29#include <input/Input.h>
Cody Heiner088c63e2023-06-15 12:06:09 -070030#include <input/MotionPredictorMetricsManager.h>
Philip Quinn8f953ab2022-12-06 15:37:07 -080031#include <input/TfLiteMotionPredictor.h>
Cody Heiner088c63e2023-06-15 12:06:09 -070032#include <utils/Timers.h> // for nsecs_t
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080033
34namespace android {
35
36static inline bool isMotionPredictionEnabled() {
37 return sysprop::InputProperties::enable_motion_prediction().value_or(true);
38}
39
40/**
41 * Given a set of MotionEvents for the current gesture, predict the motion. The returned MotionEvent
Philip Quinn8f953ab2022-12-06 15:37:07 -080042 * contains a set of samples in the future.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080043 *
44 * The typical usage is like this:
45 *
46 * MotionPredictor predictor(offset = MY_OFFSET);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080047 * predictor.record(DOWN_MOTION_EVENT);
48 * predictor.record(MOVE_MOTION_EVENT);
Philip Quinn8f953ab2022-12-06 15:37:07 -080049 * prediction = predictor.predict(futureTime);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080050 *
Philip Quinn8f953ab2022-12-06 15:37:07 -080051 * The resulting motion event will have eventTime <= (futureTime + MY_OFFSET). It might contain
52 * historical data, which are additional samples from the latest recorded MotionEvent's eventTime
53 * to the futureTime + MY_OFFSET.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080054 *
55 * The offset is used to provide additional flexibility to the caller, in case the default present
56 * time (typically provided by the choreographer) does not account for some delays, or to simply
Philip Quinn8f953ab2022-12-06 15:37:07 -080057 * reduce the aggressiveness of the prediction. Offset can be positive or negative.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080058 */
59class MotionPredictor {
60public:
Cody Heiner7b26dbe2023-11-14 14:47:10 -080061 using ReportAtomFunction = MotionPredictorMetricsManager::ReportAtomFunction;
62
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080063 /**
64 * Parameters:
65 * predictionTimestampOffsetNanos: additional, constant shift to apply to the target
Philip Quinn8f953ab2022-12-06 15:37:07 -080066 * prediction time. The prediction will target the time t=(prediction time +
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080067 * predictionTimestampOffsetNanos).
68 *
Cody Heiner7b26dbe2023-11-14 14:47:10 -080069 * checkEnableMotionPrediction: the function to check whether the prediction should run. Used to
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080070 * provide an additional way of turning prediction on and off. Can be toggled at runtime.
Cody Heiner7b26dbe2023-11-14 14:47:10 -080071 *
72 * reportAtomFunction: the function that will be called to report prediction metrics. If
73 * omitted, the implementation will choose a default metrics reporting mechanism.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080074 */
Siarhei Vishniakoufd0a68e2023-02-28 13:25:36 -080075 MotionPredictor(nsecs_t predictionTimestampOffsetNanos,
Cody Heiner7b26dbe2023-11-14 14:47:10 -080076 std::function<bool()> checkEnableMotionPrediction = isMotionPredictionEnabled,
77 ReportAtomFunction reportAtomFunction = {});
Cody Heiner088c63e2023-06-15 12:06:09 -070078
Siarhei Vishniakou33cb38b2023-02-23 18:52:34 -080079 /**
80 * Record the actual motion received by the view. This event will be used for calculating the
81 * predictions.
82 *
83 * @return empty result if the event was processed correctly, error if the event is not
84 * consistent with the previously recorded events.
85 */
86 android::base::Result<void> record(const MotionEvent& event);
Cody Heiner088c63e2023-06-15 12:06:09 -070087
Siarhei Vishniakou33cb38b2023-02-23 18:52:34 -080088 std::unique_ptr<MotionEvent> predict(nsecs_t timestamp);
Cody Heiner088c63e2023-06-15 12:06:09 -070089
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080090 bool isPredictionAvailable(int32_t deviceId, int32_t source);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080091
92private:
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080093 const nsecs_t mPredictionTimestampOffsetNanos;
94 const std::function<bool()> mCheckMotionPredictionEnabled;
Philip Quinn8f953ab2022-12-06 15:37:07 -080095
96 std::unique_ptr<TfLiteMotionPredictorModel> mModel;
Siarhei Vishniakou33cb38b2023-02-23 18:52:34 -080097
98 std::unique_ptr<TfLiteMotionPredictorBuffers> mBuffers;
99 std::optional<MotionEvent> mLastEvent;
Cody Heiner088c63e2023-06-15 12:06:09 -0700100
101 std::optional<MotionPredictorMetricsManager> mMetricsManager;
Cody Heiner7b26dbe2023-11-14 14:47:10 -0800102
103 const ReportAtomFunction mReportAtomFunction;
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -0800104};
105
106} // namespace android