Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 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 | |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 19 | #include <cstdint> |
| 20 | #include <memory> |
| 21 | #include <mutex> |
Philip Quinn | bd66e62 | 2023-02-10 11:45:01 -0800 | [diff] [blame^] | 22 | #include <string> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 23 | #include <unordered_map> |
| 24 | |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 25 | #include <android-base/thread_annotations.h> |
| 26 | #include <android/sysprop/InputProperties.sysprop.h> |
| 27 | #include <input/Input.h> |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 28 | #include <input/TfLiteMotionPredictor.h> |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | static 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 Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 38 | * contains a set of samples in the future. |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 39 | * |
| 40 | * The typical usage is like this: |
| 41 | * |
| 42 | * MotionPredictor predictor(offset = MY_OFFSET); |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 43 | * predictor.record(DOWN_MOTION_EVENT); |
| 44 | * predictor.record(MOVE_MOTION_EVENT); |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 45 | * prediction = predictor.predict(futureTime); |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 46 | * |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 47 | * 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 Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 50 | * |
| 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 Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 53 | * reduce the aggressiveness of the prediction. Offset can be positive or negative. |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 54 | */ |
| 55 | class MotionPredictor { |
| 56 | public: |
| 57 | /** |
| 58 | * Parameters: |
| 59 | * predictionTimestampOffsetNanos: additional, constant shift to apply to the target |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 60 | * prediction time. The prediction will target the time t=(prediction time + |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 61 | * predictionTimestampOffsetNanos). |
| 62 | * |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 63 | * modelPath: filesystem path to a TfLiteMotionPredictorModel flatbuffer, or nullptr to use the |
| 64 | * default model path. |
| 65 | * |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 66 | * 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 Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 69 | MotionPredictor(nsecs_t predictionTimestampOffsetNanos, const char* modelPath = nullptr, |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 70 | std::function<bool()> checkEnableMotionPrediction = isMotionPredictionEnabled); |
| 71 | void record(const MotionEvent& event); |
Siarhei Vishniakou | 0839bd6 | 2023-01-05 17:20:00 -0800 | [diff] [blame] | 72 | std::vector<std::unique_ptr<MotionEvent>> predict(nsecs_t timestamp); |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 73 | bool isPredictionAvailable(int32_t deviceId, int32_t source); |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 74 | |
| 75 | private: |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 76 | const nsecs_t mPredictionTimestampOffsetNanos; |
Philip Quinn | bd66e62 | 2023-02-10 11:45:01 -0800 | [diff] [blame^] | 77 | const std::string mModelPath; |
Siarhei Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 78 | const std::function<bool()> mCheckMotionPredictionEnabled; |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 79 | |
| 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 Vishniakou | 39147ce | 2022-11-15 12:13:04 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace android |