Expose InputConsumer::probablyHasInput
Introduce InputConsumer::probablyHasInput() to check for availability of
input events that have not been consumed yet. It performs a poll on the
underlying file descriptor.
The return value is precise (modulo rare syscall interruptions), but
clients should handle it as a performance hint. This hint would allow to
prioritize input-related work, but only when input events are *likely*
pending.
Usage constraints for a future/possible API in the Android Framework:
0. This is experimental. The usefulness of the hint has not been
broadly proven, and the functionality can be removed soon if there is
no robust data to support the usefulness.
1. The high-level API visible to apps would be effectively opt-in: does
not create any state if not used.
2. An app could use this hint to yield from longer tasks on the UI
thread (which it is not supposed to create in the first place). This
is a very niche workaround for Chrome's complexity.
3. Using the Input Hint in WebView should probably be disabled: WebView
has less control over the embedding app than Chrome, and less
knowledge when to prioritize for input handling.
4. There is a good question whether probablyHasInput() should guarantee
to return true only when input is queued. While it is good to return
precise results, clients should only use this API as an optimization
hint without sacrificing correctness. Due to the growing complexity
of input handling on Android, it is not too hard to imagine that in
some not-too-distant future input would get filtered by the Framework
on the application side, potentially skipping the contents from the
ready-to-read file descriptor or an accumulated InputConsumer batch.
Bug: b/314973388
Test: atest 'InputChannelTest#ProbablyHasInput'
Test: atest InputPublisherAndConsumerTest
Change-Id: I9681a9ddd0249e3bfd5285c6570dee7a9f5de951
diff --git a/libs/input/InputTransport.cpp b/libs/input/InputTransport.cpp
index 30cedb0..83453af 100644
--- a/libs/input/InputTransport.cpp
+++ b/libs/input/InputTransport.cpp
@@ -10,6 +10,7 @@
#include <fcntl.h>
#include <inttypes.h>
#include <math.h>
+#include <poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
@@ -517,6 +518,22 @@
return OK;
}
+bool InputChannel::probablyHasInput() const {
+ struct pollfd pfds = {.fd = mFd, .events = POLLIN};
+ if (::poll(&pfds, /*nfds=*/1, /*timeout=*/0) <= 0) {
+ // This can be a false negative because EAGAIN and ENOMEM are not handled. The latter should
+ // be extremely rare. The EAGAIN is also unlikely because it happens only when the signal
+ // arrives while the syscall is executed, and the syscall is quick. Hitting EAGAIN too often
+ // would be a sign of having too many signals, which is a bigger performance problem. A
+ // common tradition is to repeat the syscall on each EAGAIN, but it is not necessary here.
+ // In other words, the missing one liner is replaced by a multiline explanation.
+ return false;
+ }
+ // From poll(2): The bits returned in |revents| can include any of those specified in |events|,
+ // or one of the values POLLERR, POLLHUP, or POLLNVAL.
+ return (pfds.revents & POLLIN) != 0;
+}
+
std::unique_ptr<InputChannel> InputChannel::dup() const {
base::unique_fd newFd(dupFd());
return InputChannel::create(getName(), std::move(newFd), getConnectionToken());
@@ -1406,6 +1423,10 @@
return head.body.motion.source;
}
+bool InputConsumer::probablyHasInput() const {
+ return hasPendingBatch() || mChannel->probablyHasInput();
+}
+
ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
for (size_t i = 0; i < mBatches.size(); i++) {
const Batch& batch = mBatches[i];