blob: 3fae4e6b68ddb4308a966e19a7383c3b91deae3a [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>
22#include <unordered_map>
23
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080024#include <android-base/thread_annotations.h>
25#include <android/sysprop/InputProperties.sysprop.h>
26#include <input/Input.h>
Philip Quinn8f953ab2022-12-06 15:37:07 -080027#include <input/TfLiteMotionPredictor.h>
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080028
29namespace android {
30
31static inline bool isMotionPredictionEnabled() {
32 return sysprop::InputProperties::enable_motion_prediction().value_or(true);
33}
34
35/**
36 * Given a set of MotionEvents for the current gesture, predict the motion. The returned MotionEvent
Philip Quinn8f953ab2022-12-06 15:37:07 -080037 * contains a set of samples in the future.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080038 *
39 * The typical usage is like this:
40 *
41 * MotionPredictor predictor(offset = MY_OFFSET);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080042 * predictor.record(DOWN_MOTION_EVENT);
43 * predictor.record(MOVE_MOTION_EVENT);
Philip Quinn8f953ab2022-12-06 15:37:07 -080044 * prediction = predictor.predict(futureTime);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080045 *
Philip Quinn8f953ab2022-12-06 15:37:07 -080046 * The resulting motion event will have eventTime <= (futureTime + MY_OFFSET). It might contain
47 * historical data, which are additional samples from the latest recorded MotionEvent's eventTime
48 * to the futureTime + MY_OFFSET.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080049 *
50 * The offset is used to provide additional flexibility to the caller, in case the default present
51 * time (typically provided by the choreographer) does not account for some delays, or to simply
Philip Quinn8f953ab2022-12-06 15:37:07 -080052 * reduce the aggressiveness of the prediction. Offset can be positive or negative.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080053 */
54class MotionPredictor {
55public:
56 /**
57 * Parameters:
58 * predictionTimestampOffsetNanos: additional, constant shift to apply to the target
Philip Quinn8f953ab2022-12-06 15:37:07 -080059 * prediction time. The prediction will target the time t=(prediction time +
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080060 * predictionTimestampOffsetNanos).
61 *
Philip Quinn8f953ab2022-12-06 15:37:07 -080062 * modelPath: filesystem path to a TfLiteMotionPredictorModel flatbuffer, or nullptr to use the
63 * default model path.
64 *
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080065 * checkEnableMotionPredition: the function to check whether the prediction should run. Used to
66 * provide an additional way of turning prediction on and off. Can be toggled at runtime.
67 */
Philip Quinn8f953ab2022-12-06 15:37:07 -080068 MotionPredictor(nsecs_t predictionTimestampOffsetNanos, const char* modelPath = nullptr,
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080069 std::function<bool()> checkEnableMotionPrediction = isMotionPredictionEnabled);
70 void record(const MotionEvent& event);
Siarhei Vishniakou0839bd62023-01-05 17:20:00 -080071 std::vector<std::unique_ptr<MotionEvent>> predict(nsecs_t timestamp);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080072 bool isPredictionAvailable(int32_t deviceId, int32_t source);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080073
74private:
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080075 const nsecs_t mPredictionTimestampOffsetNanos;
76 const std::function<bool()> mCheckMotionPredictionEnabled;
Philip Quinn8f953ab2022-12-06 15:37:07 -080077
78 std::unique_ptr<TfLiteMotionPredictorModel> mModel;
79 // Buffers/events for each device seen by record().
80 std::unordered_map</*deviceId*/ int32_t, TfLiteMotionPredictorBuffers> mDeviceBuffers;
81 std::unordered_map</*deviceId*/ int32_t, MotionEvent> mLastEvents;
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080082};
83
84} // namespace android