blob: 6c07849c15a716897c84e4106c1889cad3c83125 [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
19#include <android-base/thread_annotations.h>
20#include <android/sysprop/InputProperties.sysprop.h>
21#include <input/Input.h>
22
23namespace android {
24
25static inline bool isMotionPredictionEnabled() {
26 return sysprop::InputProperties::enable_motion_prediction().value_or(true);
27}
28
29/**
30 * Given a set of MotionEvents for the current gesture, predict the motion. The returned MotionEvent
31 * contains a set of samples in the future, up to "presentation time + offset".
32 *
33 * The typical usage is like this:
34 *
35 * MotionPredictor predictor(offset = MY_OFFSET);
36 * predictor.setExpectedPresentationTimeNanos(NEXT_PRESENT_TIME);
37 * predictor.record(DOWN_MOTION_EVENT);
38 * predictor.record(MOVE_MOTION_EVENT);
39 * prediction = predictor.predict();
40 *
41 * The presentation time should be set some time before calling .predict(). It could be set before
42 * or after the recorded motion events. Must be done on every frame.
43 *
44 * The resulting motion event will have eventTime <= (NEXT_PRESENT_TIME + MY_OFFSET). It might
45 * contain historical data, which are additional samples from the latest recorded MotionEvent's
46 * eventTime to the NEXT_PRESENT_TIME + MY_OFFSET.
47 *
48 * The offset is used to provide additional flexibility to the caller, in case the default present
49 * time (typically provided by the choreographer) does not account for some delays, or to simply
50 * reduce the aggressiveness of the prediction. Offset can be both positive and negative.
51 */
52class MotionPredictor {
53public:
54 /**
55 * Parameters:
56 * predictionTimestampOffsetNanos: additional, constant shift to apply to the target
57 * presentation time. The prediction will target the time t=(presentationTime +
58 * predictionTimestampOffsetNanos).
59 *
60 * checkEnableMotionPredition: the function to check whether the prediction should run. Used to
61 * provide an additional way of turning prediction on and off. Can be toggled at runtime.
62 */
63 MotionPredictor(nsecs_t predictionTimestampOffsetNanos,
64 std::function<bool()> checkEnableMotionPrediction = isMotionPredictionEnabled);
65 void record(const MotionEvent& event);
66 std::vector<std::unique_ptr<MotionEvent>> predict();
67 bool isPredictionAvailable(int32_t deviceId, int32_t source);
68 void setExpectedPresentationTimeNanos(int64_t expectedPresentationTimeNanos);
69
70private:
71 std::mutex mLock;
72 int64_t mExpectedPresentationTimeNanos GUARDED_BY(mLock) = 0;
73 int64_t getExpectedPresentationTimeNanos();
74 std::vector<MotionEvent> mEvents;
75 const nsecs_t mPredictionTimestampOffsetNanos;
76 const std::function<bool()> mCheckMotionPredictionEnabled;
77};
78
79} // namespace android