diff options
| author | 2020-05-15 23:16:34 +0000 | |
|---|---|---|
| committer | 2020-05-15 23:16:34 +0000 | |
| commit | 418a9e4235ead6c3b8a6ad812cdcd21de7471d8b (patch) | |
| tree | 73db2d22b1351dd9982dbcd7f915d97f66a42aa7 | |
| parent | 289e402dac34772a6e863d785b8ba0476fa8f4bc (diff) | |
| parent | 7f0a439ef705d36182a36e07351decf124e7c5f3 (diff) | |
Merge "Reduce the usage of goto statements in dispatcher" into rvc-dev
| -rw-r--r-- | services/inputflinger/dispatcher/InputDispatcher.cpp | 212 | ||||
| -rw-r--r-- | services/inputflinger/dispatcher/InputDispatcher.h | 4 |
2 files changed, 90 insertions, 126 deletions
diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp index 1dbcf98652..a239723652 100644 --- a/services/inputflinger/dispatcher/InputDispatcher.cpp +++ b/services/inputflinger/dispatcher/InputDispatcher.cpp @@ -1152,14 +1152,16 @@ bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* ent } setInjectionResult(entry, injectionResult); + if (injectionResult == INPUT_EVENT_INJECTION_PERMISSION_DENIED) { + ALOGW("Permission denied, dropping the motion (isPointer=%s)", toString(isPointerEvent)); + return true; + } if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { - if (injectionResult != INPUT_EVENT_INJECTION_PERMISSION_DENIED) { - CancelationOptions::Mode mode(isPointerEvent - ? CancelationOptions::CANCEL_POINTER_EVENTS - : CancelationOptions::CANCEL_NON_POINTER_EVENTS); - CancelationOptions options(mode, "input event injection failed"); - synthesizeCancelationEventsForMonitorsLocked(options); - } + CancelationOptions::Mode mode(isPointerEvent + ? CancelationOptions::CANCEL_POINTER_EVENTS + : CancelationOptions::CANCEL_NON_POINTER_EVENTS); + CancelationOptions options(mode, "input event injection failed"); + synthesizeCancelationEventsForMonitorsLocked(options); return true; } @@ -1345,13 +1347,6 @@ void InputDispatcher::resumeAfterTargetsNotReadyTimeoutLocked( } } -nsecs_t InputDispatcher::getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) { - if (mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { - return currentTime - mInputTargetWaitStartTime; - } - return 0; -} - void InputDispatcher::resetAnrTimeoutsLocked() { if (DEBUG_FOCUS) { ALOGD("Resetting ANR timeouts."); @@ -1394,7 +1389,6 @@ int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) { - int32_t injectionResult; std::string reason; int32_t displayId = getTargetDisplayId(entry); @@ -1407,54 +1401,38 @@ int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, // then drop the event. if (focusedWindowHandle == nullptr) { if (focusedApplicationHandle != nullptr) { - injectionResult = - handleTargetsNotReadyLocked(currentTime, entry, focusedApplicationHandle, - nullptr, nextWakeupTime, - "Waiting because no window has focus but there is " - "a focused application that may eventually add a " - "window when it finishes starting up."); - goto Unresponsive; + return handleTargetsNotReadyLocked(currentTime, entry, focusedApplicationHandle, + nullptr, nextWakeupTime, + "Waiting because no window has focus but there is " + "a focused application that may eventually add a " + "window when it finishes starting up."); } ALOGI("Dropping event because there is no focused window or focused application in display " "%" PRId32 ".", displayId); - injectionResult = INPUT_EVENT_INJECTION_FAILED; - goto Failed; + return INPUT_EVENT_INJECTION_FAILED; } // Check permissions. if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) { - injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; - goto Failed; + return INPUT_EVENT_INJECTION_PERMISSION_DENIED; } // Check whether the window is ready for more input. reason = checkWindowReadyForMoreInputLocked(currentTime, focusedWindowHandle, entry, "focused"); if (!reason.empty()) { - injectionResult = - handleTargetsNotReadyLocked(currentTime, entry, focusedApplicationHandle, - focusedWindowHandle, nextWakeupTime, reason.c_str()); - goto Unresponsive; + return handleTargetsNotReadyLocked(currentTime, entry, focusedApplicationHandle, + focusedWindowHandle, nextWakeupTime, reason.c_str()); } // Success! Output targets. - injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; addWindowTargetLocked(focusedWindowHandle, InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, BitSet32(0), inputTargets); // Done. -Failed: -Unresponsive: - nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); - updateDispatchStatistics(currentTime, entry, injectionResult, timeSpentWaitingForApplication); - if (DEBUG_FOCUS) { - ALOGD("findFocusedWindow finished: injectionResult=%d, " - "timeSpentWaitingForApplication=%0.1fms", - injectionResult, timeSpentWaitingForApplication / 1000000.0); - } - return injectionResult; + return INPUT_EVENT_INJECTION_SUCCEEDED; } int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, @@ -1751,10 +1729,9 @@ int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, checkWindowReadyForMoreInputLocked(currentTime, touchedWindow.windowHandle, entry, "touched"); if (!reason.empty()) { - injectionResult = handleTargetsNotReadyLocked(currentTime, entry, nullptr, - touchedWindow.windowHandle, - nextWakeupTime, reason.c_str()); - goto Unresponsive; + return handleTargetsNotReadyLocked(currentTime, entry, nullptr, + touchedWindow.windowHandle, nextWakeupTime, + reason.c_str()); } } } @@ -1814,98 +1791,85 @@ Failed: } } + if (injectionPermission != INJECTION_PERMISSION_GRANTED) { + return injectionResult; + } + // Update final pieces of touch state if the injector had permission. - if (injectionPermission == INJECTION_PERMISSION_GRANTED) { - if (!wrongDevice) { - if (switchedDevice) { + if (!wrongDevice) { + if (switchedDevice) { + if (DEBUG_FOCUS) { + ALOGD("Conflicting pointer actions: Switched to a different device."); + } + *outConflictingPointerActions = true; + } + + if (isHoverAction) { + // Started hovering, therefore no longer down. + if (oldState && oldState->down) { if (DEBUG_FOCUS) { - ALOGD("Conflicting pointer actions: Switched to a different device."); + ALOGD("Conflicting pointer actions: Hover received while pointer was " + "down."); } *outConflictingPointerActions = true; } - - if (isHoverAction) { - // Started hovering, therefore no longer down. - if (oldState && oldState->down) { - if (DEBUG_FOCUS) { - ALOGD("Conflicting pointer actions: Hover received while pointer was " - "down."); - } - *outConflictingPointerActions = true; - } - mTempTouchState.reset(); - if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || - maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { - mTempTouchState.deviceId = entry.deviceId; - mTempTouchState.source = entry.source; - mTempTouchState.displayId = displayId; - } - } else if (maskedAction == AMOTION_EVENT_ACTION_UP || - maskedAction == AMOTION_EVENT_ACTION_CANCEL) { - // All pointers up or canceled. - mTempTouchState.reset(); - } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { - // First pointer went down. - if (oldState && oldState->down) { - if (DEBUG_FOCUS) { - ALOGD("Conflicting pointer actions: Down received while already down."); - } - *outConflictingPointerActions = true; + mTempTouchState.reset(); + if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER || + maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { + mTempTouchState.deviceId = entry.deviceId; + mTempTouchState.source = entry.source; + mTempTouchState.displayId = displayId; + } + } else if (maskedAction == AMOTION_EVENT_ACTION_UP || + maskedAction == AMOTION_EVENT_ACTION_CANCEL) { + // All pointers up or canceled. + mTempTouchState.reset(); + } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { + // First pointer went down. + if (oldState && oldState->down) { + if (DEBUG_FOCUS) { + ALOGD("Conflicting pointer actions: Down received while already down."); } - } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { - // One pointer went up. - if (isSplit) { - int32_t pointerIndex = getMotionEventActionPointerIndex(action); - uint32_t pointerId = entry.pointerProperties[pointerIndex].id; - - for (size_t i = 0; i < mTempTouchState.windows.size();) { - TouchedWindow& touchedWindow = mTempTouchState.windows[i]; - if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { - touchedWindow.pointerIds.clearBit(pointerId); - if (touchedWindow.pointerIds.isEmpty()) { - mTempTouchState.windows.erase(mTempTouchState.windows.begin() + i); - continue; - } + *outConflictingPointerActions = true; + } + } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { + // One pointer went up. + if (isSplit) { + int32_t pointerIndex = getMotionEventActionPointerIndex(action); + uint32_t pointerId = entry.pointerProperties[pointerIndex].id; + + for (size_t i = 0; i < mTempTouchState.windows.size();) { + TouchedWindow& touchedWindow = mTempTouchState.windows[i]; + if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { + touchedWindow.pointerIds.clearBit(pointerId); + if (touchedWindow.pointerIds.isEmpty()) { + mTempTouchState.windows.erase(mTempTouchState.windows.begin() + i); + continue; } - i += 1; } + i += 1; } } + } - // Save changes unless the action was scroll in which case the temporary touch - // state was only valid for this one action. - if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { - if (mTempTouchState.displayId >= 0) { - if (oldStateIndex >= 0) { - mTouchStatesByDisplay.editValueAt(oldStateIndex).copyFrom(mTempTouchState); - } else { - mTouchStatesByDisplay.add(displayId, mTempTouchState); - } - } else if (oldStateIndex >= 0) { - mTouchStatesByDisplay.removeItemsAt(oldStateIndex); + // Save changes unless the action was scroll in which case the temporary touch + // state was only valid for this one action. + if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) { + if (mTempTouchState.displayId >= 0) { + if (oldStateIndex >= 0) { + mTouchStatesByDisplay.editValueAt(oldStateIndex).copyFrom(mTempTouchState); + } else { + mTouchStatesByDisplay.add(displayId, mTempTouchState); } + } else if (oldStateIndex >= 0) { + mTouchStatesByDisplay.removeItemsAt(oldStateIndex); } - - // Update hover state. - mLastHoverWindowHandle = newHoverWindowHandle; - } - } else { - if (DEBUG_FOCUS) { - ALOGD("Not updating touch focus because injection was denied."); } - } -Unresponsive: - // Reset temporary touch state to ensure we release unnecessary references to input channels. - mTempTouchState.reset(); - - nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); - updateDispatchStatistics(currentTime, entry, injectionResult, timeSpentWaitingForApplication); - if (DEBUG_FOCUS) { - ALOGD("findTouchedWindow finished: injectionResult=%d, injectionPermission=%d, " - "timeSpentWaitingForApplication=%0.1fms", - injectionResult, injectionPermission, timeSpentWaitingForApplication / 1000000.0); + // Update hover state. + mLastHoverWindowHandle = newHoverWindowHandle; } + return injectionResult; } @@ -4664,6 +4628,7 @@ void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* c dispatchEntry->eventEntry->appendDescription(msg); ALOGI("%s", msg.c_str()); } + reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled); bool restartEvent; if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { @@ -4898,9 +4863,8 @@ KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) { return event; } -void InputDispatcher::updateDispatchStatistics(nsecs_t currentTime, const EventEntry& entry, - int32_t injectionResult, - nsecs_t timeSpentWaitingForApplication) { +void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration, + const Connection& connection, bool handled) { // TODO Write some statistics about how long we spend waiting. } diff --git a/services/inputflinger/dispatcher/InputDispatcher.h b/services/inputflinger/dispatcher/InputDispatcher.h index 670d0e1c96..d122282fbe 100644 --- a/services/inputflinger/dispatcher/InputDispatcher.h +++ b/services/inputflinger/dispatcher/InputDispatcher.h @@ -497,8 +497,8 @@ private: LatencyStatistics mTouchStatistics{TOUCH_STATS_REPORT_PERIOD}; void reportTouchEventForStatistics(const MotionEntry& entry); - void updateDispatchStatistics(nsecs_t currentTime, const EventEntry& entry, - int32_t injectionResult, nsecs_t timeSpentWaitingForApplication); + void reportDispatchStatistics(std::chrono::nanoseconds eventDuration, + const Connection& connection, bool handled); void traceInboundQueueLengthLocked() REQUIRES(mLock); void traceOutboundQueueLength(const sp<Connection>& connection); void traceWaitQueueLength(const sp<Connection>& connection); |