blob: 8797962886850043cc4bdf22bb51499b9b13e80e [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>
Philip Quinnbd66e622023-02-10 11:45:01 -080022#include <string>
Philip Quinn8f953ab2022-12-06 15:37:07 -080023#include <unordered_map>
24
Siarhei Vishniakou33cb38b2023-02-23 18:52:34 -080025#include <android-base/result.h>
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080026#include <android-base/thread_annotations.h>
27#include <android/sysprop/InputProperties.sysprop.h>
28#include <input/Input.h>
Cody Heiner088c63e2023-06-15 12:06:09 -070029#include <input/MotionPredictorMetricsManager.h>
Philip Quinn8f953ab2022-12-06 15:37:07 -080030#include <input/TfLiteMotionPredictor.h>
Cody Heiner088c63e2023-06-15 12:06:09 -070031#include <utils/Timers.h> // for nsecs_t
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080032
33namespace android {
34
35static inline bool isMotionPredictionEnabled() {
36 return sysprop::InputProperties::enable_motion_prediction().value_or(true);
37}
38
39/**
40 * Given a set of MotionEvents for the current gesture, predict the motion. The returned MotionEvent
Philip Quinn8f953ab2022-12-06 15:37:07 -080041 * contains a set of samples in the future.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080042 *
43 * The typical usage is like this:
44 *
45 * MotionPredictor predictor(offset = MY_OFFSET);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080046 * predictor.record(DOWN_MOTION_EVENT);
47 * predictor.record(MOVE_MOTION_EVENT);
Philip Quinn8f953ab2022-12-06 15:37:07 -080048 * prediction = predictor.predict(futureTime);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080049 *
Philip Quinn8f953ab2022-12-06 15:37:07 -080050 * The resulting motion event will have eventTime <= (futureTime + MY_OFFSET). It might contain
51 * historical data, which are additional samples from the latest recorded MotionEvent's eventTime
52 * to the futureTime + MY_OFFSET.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080053 *
54 * The offset is used to provide additional flexibility to the caller, in case the default present
55 * time (typically provided by the choreographer) does not account for some delays, or to simply
Philip Quinn8f953ab2022-12-06 15:37:07 -080056 * reduce the aggressiveness of the prediction. Offset can be positive or negative.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080057 */
58class MotionPredictor {
59public:
60 /**
61 * Parameters:
62 * predictionTimestampOffsetNanos: additional, constant shift to apply to the target
Philip Quinn8f953ab2022-12-06 15:37:07 -080063 * prediction time. The prediction will target the time t=(prediction time +
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080064 * predictionTimestampOffsetNanos).
65 *
Philip Quinn8f953ab2022-12-06 15:37:07 -080066 * modelPath: filesystem path to a TfLiteMotionPredictorModel flatbuffer, or nullptr to use the
67 * default model path.
68 *
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080069 * checkEnableMotionPredition: the function to check whether the prediction should run. Used to
70 * provide an additional way of turning prediction on and off. Can be toggled at runtime.
71 */
Siarhei Vishniakoufd0a68e2023-02-28 13:25:36 -080072 MotionPredictor(nsecs_t predictionTimestampOffsetNanos,
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080073 std::function<bool()> checkEnableMotionPrediction = isMotionPredictionEnabled);
Cody Heiner088c63e2023-06-15 12:06:09 -070074
Siarhei Vishniakou33cb38b2023-02-23 18:52:34 -080075 /**
76 * Record the actual motion received by the view. This event will be used for calculating the
77 * predictions.
78 *
79 * @return empty result if the event was processed correctly, error if the event is not
80 * consistent with the previously recorded events.
81 */
82 android::base::Result<void> record(const MotionEvent& event);
Cody Heiner088c63e2023-06-15 12:06:09 -070083
Siarhei Vishniakou33cb38b2023-02-23 18:52:34 -080084 std::unique_ptr<MotionEvent> predict(nsecs_t timestamp);
Cody Heiner088c63e2023-06-15 12:06:09 -070085
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080086 bool isPredictionAvailable(int32_t deviceId, int32_t source);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080087
88private:
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080089 const nsecs_t mPredictionTimestampOffsetNanos;
90 const std::function<bool()> mCheckMotionPredictionEnabled;
Philip Quinn8f953ab2022-12-06 15:37:07 -080091
92 std::unique_ptr<TfLiteMotionPredictorModel> mModel;
Siarhei Vishniakou33cb38b2023-02-23 18:52:34 -080093
94 std::unique_ptr<TfLiteMotionPredictorBuffers> mBuffers;
95 std::optional<MotionEvent> mLastEvent;
Cody Heiner088c63e2023-06-15 12:06:09 -070096
97 std::optional<MotionPredictorMetricsManager> mMetricsManager;
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080098};
99
100} // namespace android