Skip to content

Commit a17032e

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "Accurately track the sequence numbers of batched events."
2 parents 7a29d84 + 2d34e0c commit a17032e

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

include/ui/InputTransport.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,23 @@ class InputConsumer {
306306

307307
// Batched motion events per device and source.
308308
struct Batch {
309-
uint32_t seq;
309+
uint32_t seq; // sequence number of last input message batched in the event
310310
MotionEvent event;
311311
};
312312
Vector<Batch> mBatches;
313313

314+
// Chain of batched sequence numbers. When multiple input messages are combined into
315+
// a batch, we append a record here that associates the last sequence number in the
316+
// batch with the previous one. When the finished signal is sent, we traverse the
317+
// chain to individually finish all input messages that were part of the batch.
318+
struct SeqChain {
319+
uint32_t seq; // sequence number of batched input message
320+
uint32_t chain; // sequence number of previous batched input message
321+
};
322+
Vector<SeqChain> mSeqChains;
323+
314324
ssize_t findBatch(int32_t deviceId, int32_t source) const;
325+
status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled);
315326

316327
static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg);
317328
static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);

libs/ui/InputTransport.cpp

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,17 +401,16 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory,
401401
if (batchIndex >= 0) {
402402
Batch& batch = mBatches.editItemAt(batchIndex);
403403
if (canAppendSamples(&batch.event, &mMsg)) {
404-
// Send finished message for the earlier part of the batch.
405-
// Claim that we handled the event. (The dispatcher doesn't care either
406-
// way at the moment.)
407-
status_t status = sendFinishedSignal(batch.seq, true);
408-
if (status) {
409-
return status;
410-
}
411-
412404
// Append to the batch and save the new sequence number for the tail end.
405+
uint32_t chain = batch.seq;
413406
appendSamples(&batch.event, &mMsg);
414407
batch.seq = mMsg.body.motion.seq;
408+
409+
// Update the sequence number chain.
410+
SeqChain seqChain;
411+
seqChain.seq = batch.seq;
412+
seqChain.chain = chain;
413+
mSeqChains.push(seqChain);
415414
#if DEBUG_TRANSPORT_ACTIONS
416415
ALOGD("channel '%s' consumer ~ appended to batch event",
417416
mChannel->getName().string());
@@ -486,6 +485,41 @@ status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
486485
return BAD_VALUE;
487486
}
488487

488+
// Send finished signals for the batch sequence chain first.
489+
size_t seqChainCount = mSeqChains.size();
490+
if (seqChainCount) {
491+
uint32_t currentSeq = seq;
492+
uint32_t chainSeqs[seqChainCount];
493+
size_t chainIndex = 0;
494+
for (size_t i = seqChainCount; i-- > 0; ) {
495+
const SeqChain& seqChain = mSeqChains.itemAt(i);
496+
if (seqChain.seq == currentSeq) {
497+
currentSeq = seqChain.chain;
498+
chainSeqs[chainIndex++] = currentSeq;
499+
mSeqChains.removeAt(i);
500+
}
501+
}
502+
status_t status = OK;
503+
while (!status && chainIndex-- > 0) {
504+
status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
505+
}
506+
if (status) {
507+
// An error occurred so at least one signal was not sent, reconstruct the chain.
508+
do {
509+
SeqChain seqChain;
510+
seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
511+
seqChain.chain = chainSeqs[chainIndex];
512+
mSeqChains.push(seqChain);
513+
} while (chainIndex-- > 0);
514+
return status;
515+
}
516+
}
517+
518+
// Send finished signal for the last message in the batch.
519+
return sendUnchainedFinishedSignal(seq, handled);
520+
}
521+
522+
status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
489523
InputMessage msg;
490524
msg.header.type = InputMessage::TYPE_FINISHED;
491525
msg.body.finished.seq = seq;

0 commit comments

Comments
 (0)