blob: 68ebf75fc685cde3c57c1c100b74b1e1514335dd [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 Vishniakou39147ce2022-11-15 12:13:04 -080025#include <android-base/thread_annotations.h>
26#include <android/sysprop/InputProperties.sysprop.h>
27#include <input/Input.h>
Philip Quinn8f953ab2022-12-06 15:37:07 -080028#include <input/TfLiteMotionPredictor.h>
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080029
30namespace android {
31
32static inline bool isMotionPredictionEnabled() {
33 return sysprop::InputProperties::enable_motion_prediction().value_or(true);
34}
35
36/**
37 * Given a set of MotionEvents for the current gesture, predict the motion. The returned MotionEvent
Philip Quinn8f953ab2022-12-06 15:37:07 -080038 * contains a set of samples in the future.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080039 *
40 * The typical usage is like this:
41 *
42 * MotionPredictor predictor(offset = MY_OFFSET);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080043 * predictor.record(DOWN_MOTION_EVENT);
44 * predictor.record(MOVE_MOTION_EVENT);
Philip Quinn8f953ab2022-12-06 15:37:07 -080045 * prediction = predictor.predict(futureTime);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080046 *
Philip Quinn8f953ab2022-12-06 15:37:07 -080047 * The resulting motion event will have eventTime <= (futureTime + MY_OFFSET). It might contain
48 * historical data, which are additional samples from the latest recorded MotionEvent's eventTime
49 * to the futureTime + MY_OFFSET.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080050 *
51 * The offset is used to provide additional flexibility to the caller, in case the default present
52 * time (typically provided by the choreographer) does not account for some delays, or to simply
Philip Quinn8f953ab2022-12-06 15:37:07 -080053 * reduce the aggressiveness of the prediction. Offset can be positive or negative.
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080054 */
55class MotionPredictor {
56public:
57 /**
58 * Parameters:
59 * predictionTimestampOffsetNanos: additional, constant shift to apply to the target
Philip Quinn8f953ab2022-12-06 15:37:07 -080060 * prediction time. The prediction will target the time t=(prediction time +
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080061 * predictionTimestampOffsetNanos).
62 *
Philip Quinn8f953ab2022-12-06 15:37:07 -080063 * modelPath: filesystem path to a TfLiteMotionPredictorModel flatbuffer, or nullptr to use the
64 * default model path.
65 *
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080066 * checkEnableMotionPredition: the function to check whether the prediction should run. Used to
67 * provide an additional way of turning prediction on and off. Can be toggled at runtime.
68 */
Philip Quinn8f953ab2022-12-06 15:37:07 -080069 MotionPredictor(nsecs_t predictionTimestampOffsetNanos, const char* modelPath = nullptr,
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080070 std::function<bool()> checkEnableMotionPrediction = isMotionPredictionEnabled);
71 void record(const MotionEvent& event);
Siarhei Vishniakou0839bd62023-01-05 17:20:00 -080072 std::vector<std::unique_ptr<MotionEvent>> predict(nsecs_t timestamp);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080073 bool isPredictionAvailable(int32_t deviceId, int32_t source);
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080074
75private:
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080076 const nsecs_t mPredictionTimestampOffsetNanos;
Philip Quinnbd66e622023-02-10 11:45:01 -080077 const std::string mModelPath;
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080078 const std::function<bool()> mCheckMotionPredictionEnabled;
Philip Quinn8f953ab2022-12-06 15:37:07 -080079
80 std::unique_ptr<TfLiteMotionPredictorModel> mModel;
81 // Buffers/events for each device seen by record().
82 std::unordered_map</*deviceId*/ int32_t, TfLiteMotionPredictorBuffers> mDeviceBuffers;
83 std::unordered_map</*deviceId*/ int32_t, MotionEvent> mLastEvents;
Siarhei Vishniakou39147ce2022-11-15 12:13:04 -080084};
85
86} // namespace android