Use chrono for dispatching timeout
To prevent confusion about the units of dispatchingTimeout, convert to
chrono.
Bug: 155931890
Test: atest inputflinger_tests
Change-Id: Icfcb3bad7188052a6b047b3cbe836be8d928fad2
diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp
index 14f7caf..0fa2332 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.cpp
+++ b/services/inputflinger/dispatcher/InputDispatcher.cpp
@@ -159,6 +159,10 @@
}
}
+static int64_t millis(std::chrono::nanoseconds t) {
+ return std::chrono::duration_cast<std::chrono::milliseconds>(t).count();
+}
+
static bool validateMotionEvent(int32_t action, int32_t actionButton, size_t pointerCount,
const PointerProperties* pointerProperties) {
if (!isValidMotionAction(action, actionButton, pointerCount)) {
@@ -1473,13 +1477,13 @@
if (focusedWindowHandle == nullptr && focusedApplicationHandle != nullptr) {
if (!mNoFocusedWindowTimeoutTime.has_value()) {
// We just discovered that there's no focused window. Start the ANR timer
- const nsecs_t timeout = focusedApplicationHandle->getDispatchingTimeout(
- DEFAULT_INPUT_DISPATCHING_TIMEOUT.count());
- mNoFocusedWindowTimeoutTime = currentTime + timeout;
+ std::chrono::nanoseconds timeout = focusedApplicationHandle->getDispatchingTimeout(
+ DEFAULT_INPUT_DISPATCHING_TIMEOUT);
+ mNoFocusedWindowTimeoutTime = currentTime + timeout.count();
mAwaitedFocusedApplication = focusedApplicationHandle;
ALOGW("Waiting because no window has focus but %s may eventually add a "
"window when it finishes starting up. Will wait for %" PRId64 "ms",
- mAwaitedFocusedApplication->getName().c_str(), ns2ms(timeout));
+ mAwaitedFocusedApplication->getName().c_str(), millis(timeout));
*nextWakeupTime = *mNoFocusedWindowTimeoutTime;
return INPUT_EVENT_INJECTION_PENDING;
} else if (currentTime > *mNoFocusedWindowTimeoutTime) {
@@ -4063,13 +4067,11 @@
for (auto& it : mFocusedApplicationHandlesByDisplay) {
const int32_t displayId = it.first;
const sp<InputApplicationHandle>& applicationHandle = it.second;
+ const int64_t timeoutMillis = millis(
+ applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT));
dump += StringPrintf(INDENT2 "displayId=%" PRId32
", name='%s', dispatchingTimeout=%" PRId64 "ms\n",
- displayId, applicationHandle->getName().c_str(),
- ns2ms(applicationHandle
- ->getDispatchingTimeout(
- DEFAULT_INPUT_DISPATCHING_TIMEOUT)
- .count()));
+ displayId, applicationHandle->getName().c_str(), timeoutMillis);
}
} else {
dump += StringPrintf(INDENT "FocusedApplications: <none>\n");
@@ -4152,7 +4154,7 @@
dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64
"ms\n",
windowInfo->ownerPid, windowInfo->ownerUid,
- ns2ms(windowInfo->dispatchingTimeout));
+ millis(windowInfo->dispatchingTimeout));
dump += StringPrintf(INDENT4 " flags: %s\n",
inputWindowFlagsToString(windowInfo->layoutParamsFlags)
.c_str());
@@ -4653,12 +4655,12 @@
commandEntry->inputChannel ? commandEntry->inputChannel->getConnectionToken() : nullptr;
mLock.unlock();
- const nsecs_t timeoutExtension =
+ const std::chrono::nanoseconds timeoutExtension =
mPolicy->notifyAnr(commandEntry->inputApplicationHandle, token, commandEntry->reason);
mLock.lock();
- if (timeoutExtension > 0) {
+ if (timeoutExtension > 0s) {
extendAnrTimeoutsLocked(commandEntry->inputApplicationHandle, token, timeoutExtension);
} else {
// stop waking up for events in this connection, it is already not responding
@@ -4672,12 +4674,12 @@
void InputDispatcher::extendAnrTimeoutsLocked(const sp<InputApplicationHandle>& application,
const sp<IBinder>& connectionToken,
- nsecs_t timeoutExtension) {
+ std::chrono::nanoseconds timeoutExtension) {
sp<Connection> connection = getConnectionLocked(connectionToken);
if (connection == nullptr) {
if (mNoFocusedWindowTimeoutTime.has_value() && application != nullptr) {
// Maybe ANR happened because there's no focused window?
- mNoFocusedWindowTimeoutTime = now() + timeoutExtension;
+ mNoFocusedWindowTimeoutTime = now() + timeoutExtension.count();
mAwaitedFocusedApplication = application;
} else {
// It's also possible that the connection already disappeared. No action necessary.
@@ -4686,10 +4688,10 @@
}
ALOGI("Raised ANR, but the policy wants to keep waiting on %s for %" PRId64 "ms longer",
- connection->inputChannel->getName().c_str(), ns2ms(timeoutExtension));
+ connection->inputChannel->getName().c_str(), millis(timeoutExtension));
connection->responsive = true;
- const nsecs_t newTimeout = now() + timeoutExtension;
+ const nsecs_t newTimeout = now() + timeoutExtension.count();
for (DispatchEntry* entry : connection->waitQueue) {
if (newTimeout >= entry->timeoutTime) {
// Already removed old entries when connection was marked unresponsive
diff --git a/services/inputflinger/dispatcher/InputDispatcher.h b/services/inputflinger/dispatcher/InputDispatcher.h
index e679c6b..42583bc 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.h
+++ b/services/inputflinger/dispatcher/InputDispatcher.h
@@ -374,8 +374,8 @@
// prevent unneeded wakeups.
AnrTracker mAnrTracker GUARDED_BY(mLock);
void extendAnrTimeoutsLocked(const sp<InputApplicationHandle>& application,
- const sp<IBinder>& connectionToken, nsecs_t timeoutExtension)
- REQUIRES(mLock);
+ const sp<IBinder>& connectionToken,
+ std::chrono::nanoseconds timeoutExtension) REQUIRES(mLock);
// Contains the last window which received a hover event.
sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
diff --git a/services/inputflinger/dispatcher/include/InputDispatcherPolicyInterface.h b/services/inputflinger/dispatcher/include/InputDispatcherPolicyInterface.h
index 667af9b..21255dd 100644
--- a/services/inputflinger/dispatcher/include/InputDispatcherPolicyInterface.h
+++ b/services/inputflinger/dispatcher/include/InputDispatcherPolicyInterface.h
@@ -47,8 +47,9 @@
/* Notifies the system that an application is not responding.
* Returns a new timeout to continue waiting, or 0 to abort dispatch. */
- virtual nsecs_t notifyAnr(const sp<InputApplicationHandle>& inputApplicationHandle,
- const sp<IBinder>& token, const std::string& reason) = 0;
+ virtual std::chrono::nanoseconds notifyAnr(
+ const sp<InputApplicationHandle>& inputApplicationHandle, const sp<IBinder>& token,
+ const std::string& reason) = 0;
/* Notifies the system that an input channel is unrecoverably broken. */
virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0;