From 0c25e864701a6c408b90709493dd30de9b929e2e Mon Sep 17 00:00:00 2001 From: Paul Ramirez Date: Tue, 18 Jun 2024 21:33:33 +0000 Subject: Return message wrapped in Result from receiveMessage Changed receivedMessaged return type from status_t to android::base::Result<>. Enforces checking valid state before using the object. Flag: EXEMPT refactor Bug: 297226446 Test: TEST=libinput_tests; m $TEST && $ANDROID_HOST_OUT/nativetest64/$TEST/$TEST Change-Id: Ic2285d38a2d0d2227c1fae92379270a5f52586c7 --- libs/input/InputConsumer.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'libs/input/InputConsumer.cpp') diff --git a/libs/input/InputConsumer.cpp b/libs/input/InputConsumer.cpp index fcf490d5f9..dce528f763 100644 --- a/libs/input/InputConsumer.cpp +++ b/libs/input/InputConsumer.cpp @@ -235,8 +235,9 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consum mMsgDeferred = false; } else { // Receive a fresh message. - status_t result = mChannel->receiveMessage(&mMsg); - if (result == OK) { + android::base::Result result = mChannel->receiveMessage(); + if (result.ok()) { + mMsg = std::move(result.value()); const auto [_, inserted] = mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC)); LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32, @@ -244,11 +245,11 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consum // Trace the event processing timeline - event was just read from the socket ATRACE_ASYNC_BEGIN(mProcessingTraceTag.c_str(), /*cookie=*/mMsg.header.seq); - } - if (result) { + } else { // Consume the next batched event unless batches are being held for later. - if (consumeBatches || result != WOULD_BLOCK) { - result = consumeBatch(factory, frameTime, outSeq, outEvent); + if (consumeBatches || result.error().code() != WOULD_BLOCK) { + result = android::base::Error( + consumeBatch(factory, frameTime, outSeq, outEvent)); if (*outEvent) { ALOGD_IF(DEBUG_TRANSPORT_CONSUMER, "channel '%s' consumer ~ consumed batch event, seq=%u", @@ -256,7 +257,7 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consum break; } } - return result; + return result.error().code(); } } -- cgit v1.2.3-59-g8ed1b From 71c6f73ada569787a7064af4ea2a4c01adb75a85 Mon Sep 17 00:00:00 2001 From: jioana Date: Tue, 16 Jul 2024 15:42:56 +0000 Subject: Update MotionEvent's id when a new sample is added. Until now the MotionEvent's id was set during the "initialize" call, and it was equal to the first sample's eventId. We want to use the latest sample's eventId. Bug: b/350907221 Test: atest libinput_tests inputflinger_tests Flag: EXEMPT bugfix Change-Id: I0d967e7fe644fc2b2c09d044c3ee8ed13276d787 --- include/input/Input.h | 4 +--- libs/input/Input.cpp | 8 ++++---- libs/input/InputConsumer.cpp | 4 ++-- libs/input/InputConsumerNoResampling.cpp | 2 +- libs/input/MotionPredictor.cpp | 2 +- libs/input/tests/InputEvent_test.cpp | 20 ++++++++++++++++++-- .../tests/MotionPredictorMetricsManager_test.cpp | 3 ++- 7 files changed, 29 insertions(+), 14 deletions(-) (limited to 'libs/input/InputConsumer.cpp') diff --git a/include/input/Input.h b/include/input/Input.h index 77d7448db3..1a3cb6a884 100644 --- a/include/input/Input.h +++ b/include/input/Input.h @@ -900,9 +900,7 @@ public: void splitFrom(const MotionEvent& other, std::bitset splitPointerIds, int32_t newEventId); - void addSample( - nsecs_t eventTime, - const PointerCoords* pointerCoords); + void addSample(nsecs_t eventTime, const PointerCoords* pointerCoords, int32_t eventId); void offsetLocation(float xOffset, float yOffset); diff --git a/libs/input/Input.cpp b/libs/input/Input.cpp index b09814797f..a2bb3453fe 100644 --- a/libs/input/Input.cpp +++ b/libs/input/Input.cpp @@ -580,7 +580,7 @@ void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, &pointerProperties[pointerCount]); mSampleEventTimes.clear(); mSamplePointerCoords.clear(); - addSample(eventTime, pointerCoords); + addSample(eventTime, pointerCoords, mId); } void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) { @@ -640,9 +640,9 @@ void MotionEvent::splitFrom(const android::MotionEvent& other, mSampleEventTimes = other.mSampleEventTimes; } -void MotionEvent::addSample( - int64_t eventTime, - const PointerCoords* pointerCoords) { +void MotionEvent::addSample(int64_t eventTime, const PointerCoords* pointerCoords, + int32_t eventId) { + mId = eventId; mSampleEventTimes.push_back(eventTime); mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0], &pointerCoords[getPointerCount()]); diff --git a/libs/input/InputConsumer.cpp b/libs/input/InputConsumer.cpp index dce528f763..1eeb4e678c 100644 --- a/libs/input/InputConsumer.cpp +++ b/libs/input/InputConsumer.cpp @@ -135,7 +135,7 @@ void addSample(MotionEvent& event, const InputMessage& msg) { } event.setMetaState(event.getMetaState() | msg.body.motion.metaState); - event.addSample(msg.body.motion.eventTime, pointerCoords); + event.addSample(msg.body.motion.eventTime, pointerCoords, msg.body.motion.eventId); } void initializeTouchModeEvent(TouchModeEvent& event, const InputMessage& msg) { @@ -697,7 +697,7 @@ void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event, currentCoords.getY(), otherCoords.getX(), otherCoords.getY(), alpha); } - event->addSample(sampleTime, touchState.lastResample.pointers); + event->addSample(sampleTime, touchState.lastResample.pointers, event->getId()); } status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) { diff --git a/libs/input/InputConsumerNoResampling.cpp b/libs/input/InputConsumerNoResampling.cpp index da494b5ba3..c145d5c286 100644 --- a/libs/input/InputConsumerNoResampling.cpp +++ b/libs/input/InputConsumerNoResampling.cpp @@ -114,7 +114,7 @@ void addSample(MotionEvent& event, const InputMessage& msg) { // TODO(b/329770983): figure out if it's safe to combine events with mismatching metaState event.setMetaState(event.getMetaState() | msg.body.motion.metaState); - event.addSample(msg.body.motion.eventTime, pointerCoords.data()); + event.addSample(msg.body.motion.eventTime, pointerCoords.data(), msg.body.motion.eventId); } std::unique_ptr createTouchModeEvent(const InputMessage& msg) { diff --git a/libs/input/MotionPredictor.cpp b/libs/input/MotionPredictor.cpp index 9204b95745..9c70535ef5 100644 --- a/libs/input/MotionPredictor.cpp +++ b/libs/input/MotionPredictor.cpp @@ -345,7 +345,7 @@ std::unique_ptr MotionPredictor::predict(nsecs_t timestamp) { event.getRawTransform(), event.getDownTime(), predictionTime, event.getPointerCount(), event.getPointerProperties(), &coords); } else { - prediction->addSample(predictionTime, &coords); + prediction->addSample(predictionTime, &coords, prediction->getId()); } axisFrom = axisTo; diff --git a/libs/input/tests/InputEvent_test.cpp b/libs/input/tests/InputEvent_test.cpp index 3717f49fef..a67e1ef472 100644 --- a/libs/input/tests/InputEvent_test.cpp +++ b/libs/input/tests/InputEvent_test.cpp @@ -371,8 +371,8 @@ void MotionEventTest::initializeEventWithHistory(MotionEvent* event) { mTransform, 2.0f, 2.1f, AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, mRawTransform, ARBITRARY_DOWN_TIME, ARBITRARY_EVENT_TIME, 2, mPointerProperties, mSamples[0].pointerCoords); - event->addSample(ARBITRARY_EVENT_TIME + 1, mSamples[1].pointerCoords); - event->addSample(ARBITRARY_EVENT_TIME + 2, mSamples[2].pointerCoords); + event->addSample(ARBITRARY_EVENT_TIME + 1, mSamples[1].pointerCoords, event->getId()); + event->addSample(ARBITRARY_EVENT_TIME + 2, mSamples[2].pointerCoords, event->getId()); } void MotionEventTest::assertEqualsEventWithHistory(const MotionEvent* event) { @@ -591,6 +591,22 @@ TEST_F(MotionEventTest, CopyFrom_DoNotKeepHistory) { ASSERT_EQ(event.getX(0), copy.getX(0)); } +TEST_F(MotionEventTest, CheckEventIdWithHistoryIsIncremented) { + MotionEvent event; + constexpr int32_t ARBITRARY_ID = 42; + event.initialize(ARBITRARY_ID, 2, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, INVALID_HMAC, + AMOTION_EVENT_ACTION_MOVE, 0, 0, AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, + AMOTION_EVENT_BUTTON_PRIMARY, MotionClassification::NONE, mTransform, 0, 0, + AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, + mRawTransform, ARBITRARY_DOWN_TIME, ARBITRARY_EVENT_TIME, 2, + mPointerProperties, mSamples[0].pointerCoords); + ASSERT_EQ(event.getId(), ARBITRARY_ID); + event.addSample(ARBITRARY_EVENT_TIME + 1, mSamples[1].pointerCoords, ARBITRARY_ID + 1); + ASSERT_EQ(event.getId(), ARBITRARY_ID + 1); + event.addSample(ARBITRARY_EVENT_TIME + 2, mSamples[2].pointerCoords, ARBITRARY_ID + 2); + ASSERT_EQ(event.getId(), ARBITRARY_ID + 2); +} + TEST_F(MotionEventTest, SplitPointerDown) { MotionEvent event = MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) .downTime(ARBITRARY_DOWN_TIME) diff --git a/libs/input/tests/MotionPredictorMetricsManager_test.cpp b/libs/input/tests/MotionPredictorMetricsManager_test.cpp index cc41eeb5e7..0542f39f6d 100644 --- a/libs/input/tests/MotionPredictorMetricsManager_test.cpp +++ b/libs/input/tests/MotionPredictorMetricsManager_test.cpp @@ -167,7 +167,8 @@ MotionEvent makeMotionEvent(const std::vector& predictionPoints .y(predictionPoints[i].position[0]) .axis(AMOTION_EVENT_AXIS_PRESSURE, predictionPoints[i].pressure) .buildCoords(); - predictionEvent.addSample(predictionPoints[i].targetTimestamp, &coords); + predictionEvent.addSample(predictionPoints[i].targetTimestamp, &coords, + predictionEvent.getId()); } return predictionEvent; } -- cgit v1.2.3-59-g8ed1b