• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <cstdint>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <unordered_map>
24 
25 #include <android-base/result.h>
26 #include <android-base/thread_annotations.h>
27 #include <android/sysprop/InputProperties.sysprop.h>
28 #include <input/Input.h>
29 #include <input/MotionPredictorMetricsManager.h>
30 #include <input/TfLiteMotionPredictor.h>
31 #include <utils/Timers.h> // for nsecs_t
32 
33 namespace android {
34 
isMotionPredictionEnabled()35 static 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
41  * contains a set of samples in the future.
42  *
43  * The typical usage is like this:
44  *
45  * MotionPredictor predictor(offset = MY_OFFSET);
46  * predictor.record(DOWN_MOTION_EVENT);
47  * predictor.record(MOVE_MOTION_EVENT);
48  * prediction = predictor.predict(futureTime);
49  *
50  * 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.
53  *
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
56  * reduce the aggressiveness of the prediction. Offset can be positive or negative.
57  */
58 class MotionPredictor {
59 public:
60     /**
61      * Parameters:
62      * predictionTimestampOffsetNanos: additional, constant shift to apply to the target
63      * prediction time. The prediction will target the time t=(prediction time +
64      * predictionTimestampOffsetNanos).
65      *
66      * modelPath: filesystem path to a TfLiteMotionPredictorModel flatbuffer, or nullptr to use the
67      * default model path.
68      *
69      * 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      */
72     MotionPredictor(nsecs_t predictionTimestampOffsetNanos,
73                     std::function<bool()> checkEnableMotionPrediction = isMotionPredictionEnabled);
74 
75     /**
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);
83 
84     std::unique_ptr<MotionEvent> predict(nsecs_t timestamp);
85 
86     bool isPredictionAvailable(int32_t deviceId, int32_t source);
87 
88 private:
89     const nsecs_t mPredictionTimestampOffsetNanos;
90     const std::function<bool()> mCheckMotionPredictionEnabled;
91 
92     std::unique_ptr<TfLiteMotionPredictorModel> mModel;
93 
94     std::unique_ptr<TfLiteMotionPredictorBuffers> mBuffers;
95     std::optional<MotionEvent> mLastEvent;
96 
97     std::optional<MotionPredictorMetricsManager> mMetricsManager;
98 };
99 
100 } // namespace android
101