Merge "Provide prediction time from the application"
diff --git a/include/input/MotionPredictor.h b/include/input/MotionPredictor.h
index 6c07849..045e61b 100644
--- a/include/input/MotionPredictor.h
+++ b/include/input/MotionPredictor.h
@@ -63,14 +63,10 @@
MotionPredictor(nsecs_t predictionTimestampOffsetNanos,
std::function<bool()> checkEnableMotionPrediction = isMotionPredictionEnabled);
void record(const MotionEvent& event);
- std::vector<std::unique_ptr<MotionEvent>> predict();
+ std::vector<std::unique_ptr<MotionEvent>> predict(nsecs_t timestamp);
bool isPredictionAvailable(int32_t deviceId, int32_t source);
- void setExpectedPresentationTimeNanos(int64_t expectedPresentationTimeNanos);
private:
- std::mutex mLock;
- int64_t mExpectedPresentationTimeNanos GUARDED_BY(mLock) = 0;
- int64_t getExpectedPresentationTimeNanos();
std::vector<MotionEvent> mEvents;
const nsecs_t mPredictionTimestampOffsetNanos;
const std::function<bool()> mCheckMotionPredictionEnabled;
diff --git a/libs/input/MotionPredictor.cpp b/libs/input/MotionPredictor.cpp
index 0719fe5..5e2ffa6 100644
--- a/libs/input/MotionPredictor.cpp
+++ b/libs/input/MotionPredictor.cpp
@@ -55,7 +55,7 @@
* The returned event should not contain any of the real, existing data. It should only
* contain the predicted samples.
*/
-std::vector<std::unique_ptr<MotionEvent>> MotionPredictor::predict() {
+std::vector<std::unique_ptr<MotionEvent>> MotionPredictor::predict(nsecs_t timestamp) {
if (mEvents.size() < 2) {
return {};
}
@@ -67,7 +67,7 @@
std::unique_ptr<MotionEvent> prediction = std::make_unique<MotionEvent>();
std::vector<PointerCoords> futureCoords;
- const int64_t futureTime = getExpectedPresentationTimeNanos() + mPredictionTimestampOffsetNanos;
+ const nsecs_t futureTime = timestamp + mPredictionTimestampOffsetNanos;
const nsecs_t currentTime = event.getEventTime();
const MotionEvent& previous = mEvents.rbegin()[1];
const nsecs_t oldTime = previous.getEventTime();
@@ -143,14 +143,4 @@
return true;
}
-int64_t MotionPredictor::getExpectedPresentationTimeNanos() {
- std::scoped_lock lock(mLock);
- return mExpectedPresentationTimeNanos;
-}
-
-void MotionPredictor::setExpectedPresentationTimeNanos(int64_t expectedPresentationTimeNanos) {
- std::scoped_lock lock(mLock);
- mExpectedPresentationTimeNanos = expectedPresentationTimeNanos;
-}
-
} // namespace android
diff --git a/libs/input/tests/MotionPredictor_test.cpp b/libs/input/tests/MotionPredictor_test.cpp
index 0f39055..d2b59a1 100644
--- a/libs/input/tests/MotionPredictor_test.cpp
+++ b/libs/input/tests/MotionPredictor_test.cpp
@@ -63,8 +63,7 @@
predictor.record(getMotionEvent(MOVE, 1, 3, 10));
predictor.record(getMotionEvent(MOVE, 2, 5, 20));
predictor.record(getMotionEvent(MOVE, 3, 7, 30));
- predictor.setExpectedPresentationTimeNanos(40);
- std::vector<std::unique_ptr<MotionEvent>> predicted = predictor.predict();
+ std::vector<std::unique_ptr<MotionEvent>> predicted = predictor.predict(40);
ASSERT_EQ(1u, predicted.size());
ASSERT_EQ(predicted[0]->getX(0), 4);
ASSERT_EQ(predicted[0]->getY(0), 9);
@@ -81,8 +80,7 @@
predictor.record(getMotionEvent(MOVE, 0, 1, 10));
predictor.record(getMotionEvent(MOVE, 0, 1, 20));
predictor.record(getMotionEvent(MOVE, 0, 1, 30));
- predictor.setExpectedPresentationTimeNanos(40);
- std::vector<std::unique_ptr<MotionEvent>> predicted = predictor.predict();
+ std::vector<std::unique_ptr<MotionEvent>> predicted = predictor.predict(40);
ASSERT_EQ(1u, predicted.size());
ASSERT_EQ(predicted[0]->getX(0), 0);
ASSERT_EQ(predicted[0]->getY(0), 1);
@@ -98,21 +96,19 @@
TEST(MotionPredictorTest, Offset) {
MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/1,
[]() { return true /*enable prediction*/; });
- predictor.setExpectedPresentationTimeNanos(40);
predictor.record(getMotionEvent(DOWN, 0, 1, 30));
predictor.record(getMotionEvent(MOVE, 0, 1, 35));
- std::vector<std::unique_ptr<MotionEvent>> predicted = predictor.predict();
+ std::vector<std::unique_ptr<MotionEvent>> predicted = predictor.predict(40);
ASSERT_EQ(1u, predicted.size());
ASSERT_GE(predicted[0]->getEventTime(), 41);
}
-TEST(MotionPredictionTest, FlagDisablesPrediction) {
+TEST(MotionPredictorTest, FlagDisablesPrediction) {
MotionPredictor predictor(/*predictionTimestampOffsetNanos=*/0,
[]() { return false /*disable prediction*/; });
- predictor.setExpectedPresentationTimeNanos(40);
predictor.record(getMotionEvent(DOWN, 0, 1, 30));
predictor.record(getMotionEvent(MOVE, 0, 1, 35));
- std::vector<std::unique_ptr<MotionEvent>> predicted = predictor.predict();
+ std::vector<std::unique_ptr<MotionEvent>> predicted = predictor.predict(40);
ASSERT_EQ(0u, predicted.size());
ASSERT_FALSE(predictor.isPredictionAvailable(/*deviceId=*/1, AINPUT_SOURCE_STYLUS));
ASSERT_FALSE(predictor.isPredictionAvailable(/*deviceId=*/1, AINPUT_SOURCE_TOUCHSCREEN));