From 4559c9d3c3ab33708be88a386b3a210c3a39c5a5 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Thu, 2 Mar 2023 21:05:27 -0800 Subject: [PATCH] Track all published motion events in InputEventSender "Received 'finished' signal for unknown seq number" messages occurred before this CL when the mouse is captured due to the below sequence of events. 1. ImeInputStage in the app uses InputEventSender to send these motion events to the IME. These events can be batched and for batched motion events, NativeInputEventSender::sendMotionEvent() only writes the highest published sequence number to mPublishedSeqMap here. 2. InputConsumer::consumeSamples() consumes these events in the IME. It takes care to create a SeqChain such that it can finish every intermediate event. 3. The IME handles the events and calls InputConsumer::sendFinishedSignal() which sends a finish event for each of the events 4. The app receives these input messages from the IME and handles them in NativeInputEventSender::notifyConsumerResponse. This function logs a "Received 'finished' signal for unknown seq number" warning since it only write the highest published sequence number in step 2) instead of all of the batched signal numbers This CL captures tracks published motion events, so that the log only occurs for truly unexpected sequence numbers. Bug: 268243026 Test: apps with mouse capture do not log this warning unnecessarily Change-Id: I35b64679c7af92f00e2d2ba7651cec4ed9142f26 --- core/jni/android_view_InputEventSender.cpp | 29 ++++++++++++++++------ 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/core/jni/android_view_InputEventSender.cpp b/core/jni/android_view_InputEventSender.cpp index 4bc567abf27ab..ad54004ec81cf 100644 --- a/core/jni/android_view_InputEventSender.cpp +++ b/core/jni/android_view_InputEventSender.cpp @@ -20,20 +20,21 @@ #include #include +#include #include #include #include #include + +#include +#include + #include "android_os_MessageQueue.h" #include "android_view_InputChannel.h" #include "android_view_KeyEvent.h" #include "android_view_MotionEvent.h" #include "core_jni_helpers.h" -#include -#include - - using android::base::Result; namespace android { @@ -67,7 +68,7 @@ private: jobject mSenderWeakGlobal; InputPublisher mInputPublisher; sp mMessageQueue; - std::unordered_map mPublishedSeqMap; + std::unordered_map> mPublishedSeqMap; uint32_t mNextPublishedSeq; @@ -165,8 +166,14 @@ status_t NativeInputEventSender::sendMotionEvent(uint32_t seq, const MotionEvent getInputChannelName().c_str(), status); return status; } + // mPublishedSeqMap tracks all sequences published from this sender. Only the last + // sequence number is used to signal this motion event is finished. + if (i == event->getHistorySize()) { + mPublishedSeqMap.emplace(publishedSeq, seq); + } else { + mPublishedSeqMap.emplace(publishedSeq, std::nullopt); + } } - mPublishedSeqMap.emplace(publishedSeq, seq); return OK; } @@ -277,8 +284,16 @@ bool NativeInputEventSender::notifyConsumerResponse( // does something wrong and sends bad data. Just ignore and process other events. return true; } - const uint32_t seq = it->second; + + const std::optional seqOptional = it->second; mPublishedSeqMap.erase(it); + // If this optional does not have a value, it means we are processing an event that had history + // and was split. There are more events coming, so we can't call 'dispatchInputEventFinished' + // yet. The final split event will have a valid sequence number. + if (!seqOptional.has_value()) { + return true; + } + const uint32_t seq = seqOptional.value(); if (kDebugDispatchCycle) { ALOGD("channel '%s' ~ Received finished signal, seq=%u, handled=%s, pendingEvents=%zu.",