Skip to content

Commit a2db545

Browse files
Treehugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "Do not resample when frameTime is not available" into main
2 parents 3b49b34 + 37242c8 commit a2db545

4 files changed

Lines changed: 50 additions & 17 deletions

File tree

include/input/InputConsumerNoResampling.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,17 @@ class InputConsumerNoResampling final {
211211
* `consumeBatchedInputEvents`.
212212
*/
213213
std::map<DeviceId, std::queue<InputMessage>> mBatches;
214+
214215
/**
215-
* Creates a MotionEvent by consuming samples from the provided queue. If one message has
216-
* eventTime > adjustedFrameTime, all subsequent messages in the queue will be skipped. It is
217-
* assumed that messages are queued in chronological order. In other words, only events that
218-
* occurred prior to the adjustedFrameTime will be consumed.
219-
* @param requestedFrameTime the time up to which to consume events.
220-
* @param messages the queue of messages to consume from
216+
* Creates a MotionEvent by consuming samples from the provided queue. Consumes all messages
217+
* with eventTime <= requestedFrameTime - resampleLatency, where `resampleLatency` is latency
218+
* introduced by the resampler. Assumes that messages are queued in chronological order.
219+
* @param requestedFrameTime The time up to which consume messages, as given by the inequality
220+
* above. If std::nullopt, everything in messages will be consumed.
221+
* @param messages the queue of messages to consume from.
221222
*/
222223
std::pair<std::unique_ptr<MotionEvent>, std::optional<uint32_t>> createBatchedMotionEvent(
223-
const nsecs_t requestedFrameTime, std::queue<InputMessage>& messages);
224+
const std::optional<nsecs_t> requestedFrameTime, std::queue<InputMessage>& messages);
224225

225226
/**
226227
* Consumes the batched input events, optionally allowing the caller to specify a device id

libs/input/InputConsumerNoResampling.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ void InputConsumerNoResampling::handleMessages(std::vector<InputMessage>&& messa
357357
mBatches[deviceId].emplace(msg);
358358
} else {
359359
// consume all pending batches for this device immediately
360-
consumeBatchedInputEvents(deviceId, /*requestedFrameTime=*/std::nullopt);
360+
consumeBatchedInputEvents(deviceId, /*requestedFrameTime=*/
361+
std::numeric_limits<nsecs_t>::max());
361362
if (canResample &&
362363
(action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_CANCEL)) {
363364
LOG_IF(INFO, mResamplers.erase(deviceId) == 0)
@@ -480,7 +481,7 @@ void InputConsumerNoResampling::handleMessage(const InputMessage& msg) const {
480481
}
481482

482483
std::pair<std::unique_ptr<MotionEvent>, std::optional<uint32_t>>
483-
InputConsumerNoResampling::createBatchedMotionEvent(const nsecs_t requestedFrameTime,
484+
InputConsumerNoResampling::createBatchedMotionEvent(const std::optional<nsecs_t> requestedFrameTime,
484485
std::queue<InputMessage>& messages) {
485486
std::unique_ptr<MotionEvent> motionEvent;
486487
std::optional<uint32_t> firstSeqForBatch;
@@ -491,7 +492,11 @@ InputConsumerNoResampling::createBatchedMotionEvent(const nsecs_t requestedFrame
491492
const nanoseconds resampleLatency = (resampler != mResamplers.cend())
492493
? resampler->second->getResampleLatency()
493494
: nanoseconds{0};
494-
const nanoseconds adjustedFrameTime = nanoseconds{requestedFrameTime} - resampleLatency;
495+
// When batching is not enabled, we want to consume all events. That's equivalent to having an
496+
// infinite requestedFrameTime.
497+
const nanoseconds adjustedFrameTime = (requestedFrameTime.has_value())
498+
? (nanoseconds{*requestedFrameTime} - resampleLatency)
499+
: nanoseconds{std::numeric_limits<nsecs_t>::max()};
495500

496501
while (!messages.empty() &&
497502
(messages.front().body.motion.eventTime <= adjustedFrameTime.count())) {
@@ -513,8 +518,9 @@ InputConsumerNoResampling::createBatchedMotionEvent(const nsecs_t requestedFrame
513518
if (!messages.empty()) {
514519
futureSample = &messages.front();
515520
}
516-
if ((motionEvent != nullptr) && (resampler != mResamplers.cend())) {
517-
resampler->second->resampleMotionEvent(nanoseconds{requestedFrameTime}, *motionEvent,
521+
if ((motionEvent != nullptr) && (resampler != mResamplers.cend()) &&
522+
(requestedFrameTime.has_value())) {
523+
resampler->second->resampleMotionEvent(nanoseconds{*requestedFrameTime}, *motionEvent,
518524
futureSample);
519525
}
520526

@@ -524,16 +530,13 @@ InputConsumerNoResampling::createBatchedMotionEvent(const nsecs_t requestedFrame
524530
bool InputConsumerNoResampling::consumeBatchedInputEvents(
525531
std::optional<DeviceId> deviceId, std::optional<nsecs_t> requestedFrameTime) {
526532
ensureCalledOnLooperThread(__func__);
527-
// When batching is not enabled, we want to consume all events. That's equivalent to having an
528-
// infinite requestedFrameTime.
529-
requestedFrameTime = requestedFrameTime.value_or(std::numeric_limits<nsecs_t>::max());
530533
bool producedEvents = false;
531534

532535
for (auto deviceIdIter = (deviceId.has_value()) ? (mBatches.find(*deviceId))
533536
: (mBatches.begin());
534537
deviceIdIter != mBatches.cend(); ++deviceIdIter) {
535538
std::queue<InputMessage>& messages = deviceIdIter->second;
536-
auto [motion, firstSeqForBatch] = createBatchedMotionEvent(*requestedFrameTime, messages);
539+
auto [motion, firstSeqForBatch] = createBatchedMotionEvent(requestedFrameTime, messages);
537540
if (motion != nullptr) {
538541
LOG_ALWAYS_FATAL_IF(!firstSeqForBatch.has_value());
539542
mCallbacks.onMotionEvent(std::move(motion), *firstSeqForBatch);

libs/input/tests/InputConsumerResampling_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,32 @@ TEST_F(InputConsumerResamplingTest, OldEventReceivedAfterResampleOccurs) {
566566
mClientTestChannel->assertFinishMessage(/*seq=*/4, /*handled=*/true);
567567
}
568568

569+
TEST_F(InputConsumerResamplingTest, DoNotResampleWhenFrameTimeIsNotAvailable) {
570+
mClientTestChannel->enqueueMessage(nextPointerMessage(
571+
{0ms, {Pointer{.id = 0, .x = 10.0f, .y = 20.0f}}, AMOTION_EVENT_ACTION_DOWN}));
572+
573+
invokeLooperCallback();
574+
assertReceivedMotionEvent({InputEventEntry{0ms,
575+
{Pointer{.id = 0, .x = 10.0f, .y = 20.0f}},
576+
AMOTION_EVENT_ACTION_DOWN}});
577+
578+
mClientTestChannel->enqueueMessage(nextPointerMessage(
579+
{10ms, {Pointer{.id = 0, .x = 20.0f, .y = 30.0f}}, AMOTION_EVENT_ACTION_MOVE}));
580+
mClientTestChannel->enqueueMessage(nextPointerMessage(
581+
{20ms, {Pointer{.id = 0, .x = 30.0f, .y = 30.0f}}, AMOTION_EVENT_ACTION_MOVE}));
582+
583+
invokeLooperCallback();
584+
mConsumer->consumeBatchedInputEvents(std::nullopt);
585+
assertReceivedMotionEvent({InputEventEntry{10ms,
586+
{Pointer{.id = 0, .x = 20.0f, .y = 30.0f}},
587+
AMOTION_EVENT_ACTION_MOVE},
588+
InputEventEntry{20ms,
589+
{Pointer{.id = 0, .x = 30.0f, .y = 30.0f}},
590+
AMOTION_EVENT_ACTION_MOVE}});
591+
592+
mClientTestChannel->assertFinishMessage(/*seq=*/1, /*handled=*/true);
593+
mClientTestChannel->assertFinishMessage(/*seq=*/2, /*handled=*/true);
594+
mClientTestChannel->assertFinishMessage(/*seq=*/3, /*handled=*/true);
595+
}
596+
569597
} // namespace android

libs/input/tests/InputConsumer_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ TEST_F(InputConsumerTest, MessageStreamBatchedInMotionEvent) {
194194
std::unique_ptr<MotionEvent> moveMotionEvent =
195195
assertReceivedMotionEvent(WithMotionAction(ACTION_MOVE));
196196
ASSERT_NE(moveMotionEvent, nullptr);
197-
EXPECT_EQ(moveMotionEvent->getHistorySize() + 1, 3UL);
197+
EXPECT_EQ(moveMotionEvent->getHistorySize() + 1, 2UL);
198198

199199
mClientTestChannel->assertFinishMessage(/*seq=*/0, /*handled=*/true);
200200
mClientTestChannel->assertFinishMessage(/*seq=*/1, /*handled=*/true);
@@ -443,4 +443,5 @@ TEST_F(InputConsumerTest, MultiDeviceResampling) {
443443
mClientTestChannel->assertFinishMessage(/*seq=*/8, /*handled=*/true);
444444
mClientTestChannel->assertFinishMessage(/*seq=*/10, /*handled=*/true);
445445
}
446+
446447
} // namespace android

0 commit comments

Comments
 (0)