Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7f0a439e authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Reduce the usage of goto statements in dispatcher

Currently, there are a lot of uses of goto statements in
InputDispatcher, which makes the code hard to follow. Instead, remove
some unused code and reduce the goto usage.

Bug: 143459140
Test: presubmit
Change-Id: I0ef0b3a0820ce643486cec5d9cbeb0f593344ee6
Merged-In: I0ef0b3a0820ce643486cec5d9cbeb0f593344ee6
(cherry picked from commit 767917f0)
parent 7e838c23
Loading
Loading
Loading
Loading
+88 −124
Original line number Original line Diff line number Diff line
@@ -1152,14 +1152,16 @@ bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* ent
    }
    }


    setInjectionResult(entry, injectionResult);
    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_SUCCEEDED) {
        if (injectionResult != INPUT_EVENT_INJECTION_PERMISSION_DENIED) {
        CancelationOptions::Mode mode(isPointerEvent
        CancelationOptions::Mode mode(isPointerEvent
                                              ? CancelationOptions::CANCEL_POINTER_EVENTS
                                              ? CancelationOptions::CANCEL_POINTER_EVENTS
                                              : CancelationOptions::CANCEL_NON_POINTER_EVENTS);
                                              : CancelationOptions::CANCEL_NON_POINTER_EVENTS);
        CancelationOptions options(mode, "input event injection failed");
        CancelationOptions options(mode, "input event injection failed");
        synthesizeCancelationEventsForMonitorsLocked(options);
        synthesizeCancelationEventsForMonitorsLocked(options);
        }
        return true;
        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() {
void InputDispatcher::resetAnrTimeoutsLocked() {
    if (DEBUG_FOCUS) {
    if (DEBUG_FOCUS) {
        ALOGD("Resetting ANR timeouts.");
        ALOGD("Resetting ANR timeouts.");
@@ -1394,7 +1389,6 @@ int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime,
                                                        const EventEntry& entry,
                                                        const EventEntry& entry,
                                                        std::vector<InputTarget>& inputTargets,
                                                        std::vector<InputTarget>& inputTargets,
                                                        nsecs_t* nextWakeupTime) {
                                                        nsecs_t* nextWakeupTime) {
    int32_t injectionResult;
    std::string reason;
    std::string reason;


    int32_t displayId = getTargetDisplayId(entry);
    int32_t displayId = getTargetDisplayId(entry);
@@ -1407,54 +1401,38 @@ int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime,
    // then drop the event.
    // then drop the event.
    if (focusedWindowHandle == nullptr) {
    if (focusedWindowHandle == nullptr) {
        if (focusedApplicationHandle != nullptr) {
        if (focusedApplicationHandle != nullptr) {
            injectionResult =
            return handleTargetsNotReadyLocked(currentTime, entry, focusedApplicationHandle,
                    handleTargetsNotReadyLocked(currentTime, entry, focusedApplicationHandle,
                                               nullptr, nextWakeupTime,
                                               nullptr, nextWakeupTime,
                                               "Waiting because no window has focus but there is "
                                               "Waiting because no window has focus but there is "
                                               "a focused application that may eventually add a "
                                               "a focused application that may eventually add a "
                                               "window when it finishes starting up.");
                                               "window when it finishes starting up.");
            goto Unresponsive;
        }
        }


        ALOGI("Dropping event because there is no focused window or focused application in display "
        ALOGI("Dropping event because there is no focused window or focused application in display "
              "%" PRId32 ".",
              "%" PRId32 ".",
              displayId);
              displayId);
        injectionResult = INPUT_EVENT_INJECTION_FAILED;
        return INPUT_EVENT_INJECTION_FAILED;
        goto Failed;
    }
    }


    // Check permissions.
    // Check permissions.
    if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) {
    if (!checkInjectionPermission(focusedWindowHandle, entry.injectionState)) {
        injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED;
        return INPUT_EVENT_INJECTION_PERMISSION_DENIED;
        goto Failed;
    }
    }


    // Check whether the window is ready for more input.
    // Check whether the window is ready for more input.
    reason = checkWindowReadyForMoreInputLocked(currentTime, focusedWindowHandle, entry, "focused");
    reason = checkWindowReadyForMoreInputLocked(currentTime, focusedWindowHandle, entry, "focused");
    if (!reason.empty()) {
    if (!reason.empty()) {
        injectionResult =
        return handleTargetsNotReadyLocked(currentTime, entry, focusedApplicationHandle,
                handleTargetsNotReadyLocked(currentTime, entry, focusedApplicationHandle,
                                           focusedWindowHandle, nextWakeupTime, reason.c_str());
                                           focusedWindowHandle, nextWakeupTime, reason.c_str());
        goto Unresponsive;
    }
    }


    // Success!  Output targets.
    // Success!  Output targets.
    injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED;
    addWindowTargetLocked(focusedWindowHandle,
    addWindowTargetLocked(focusedWindowHandle,
                          InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS,
                          InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS,
                          BitSet32(0), inputTargets);
                          BitSet32(0), inputTargets);


    // Done.
    // Done.
Failed:
    return INPUT_EVENT_INJECTION_SUCCEEDED;
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;
}
}


int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime,
int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime,
@@ -1751,10 +1729,9 @@ int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime,
                    checkWindowReadyForMoreInputLocked(currentTime, touchedWindow.windowHandle,
                    checkWindowReadyForMoreInputLocked(currentTime, touchedWindow.windowHandle,
                                                       entry, "touched");
                                                       entry, "touched");
            if (!reason.empty()) {
            if (!reason.empty()) {
                injectionResult = handleTargetsNotReadyLocked(currentTime, entry, nullptr,
                return handleTargetsNotReadyLocked(currentTime, entry, nullptr,
                                                              touchedWindow.windowHandle,
                                                   touchedWindow.windowHandle, nextWakeupTime,
                                                              nextWakeupTime, reason.c_str());
                                                   reason.c_str());
                goto Unresponsive;
            }
            }
        }
        }
    }
    }
@@ -1814,8 +1791,11 @@ Failed:
        }
        }
    }
    }


    if (injectionPermission != INJECTION_PERMISSION_GRANTED) {
        return injectionResult;
    }

    // Update final pieces of touch state if the injector had permission.
    // Update final pieces of touch state if the injector had permission.
    if (injectionPermission == INJECTION_PERMISSION_GRANTED) {
    if (!wrongDevice) {
    if (!wrongDevice) {
        if (switchedDevice) {
        if (switchedDevice) {
            if (DEBUG_FOCUS) {
            if (DEBUG_FOCUS) {
@@ -1889,23 +1869,7 @@ Failed:
        // Update hover state.
        // Update hover state.
        mLastHoverWindowHandle = newHoverWindowHandle;
        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);
    }
    return injectionResult;
    return injectionResult;
}
}


@@ -4661,6 +4625,7 @@ void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* c
        dispatchEntry->eventEntry->appendDescription(msg);
        dispatchEntry->eventEntry->appendDescription(msg);
        ALOGI("%s", msg.c_str());
        ALOGI("%s", msg.c_str());
    }
    }
    reportDispatchStatistics(std::chrono::nanoseconds(eventDuration), *connection, handled);


    bool restartEvent;
    bool restartEvent;
    if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) {
    if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) {
@@ -4895,9 +4860,8 @@ KeyEvent InputDispatcher::createKeyEvent(const KeyEntry& entry) {
    return event;
    return event;
}
}


void InputDispatcher::updateDispatchStatistics(nsecs_t currentTime, const EventEntry& entry,
void InputDispatcher::reportDispatchStatistics(std::chrono::nanoseconds eventDuration,
                                               int32_t injectionResult,
                                               const Connection& connection, bool handled) {
                                               nsecs_t timeSpentWaitingForApplication) {
    // TODO Write some statistics about how long we spend waiting.
    // TODO Write some statistics about how long we spend waiting.
}
}


+2 −2
Original line number Original line Diff line number Diff line
@@ -497,8 +497,8 @@ private:
    LatencyStatistics mTouchStatistics{TOUCH_STATS_REPORT_PERIOD};
    LatencyStatistics mTouchStatistics{TOUCH_STATS_REPORT_PERIOD};


    void reportTouchEventForStatistics(const MotionEntry& entry);
    void reportTouchEventForStatistics(const MotionEntry& entry);
    void updateDispatchStatistics(nsecs_t currentTime, const EventEntry& entry,
    void reportDispatchStatistics(std::chrono::nanoseconds eventDuration,
                                  int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
                                  const Connection& connection, bool handled);
    void traceInboundQueueLengthLocked() REQUIRES(mLock);
    void traceInboundQueueLengthLocked() REQUIRES(mLock);
    void traceOutboundQueueLength(const sp<Connection>& connection);
    void traceOutboundQueueLength(const sp<Connection>& connection);
    void traceWaitQueueLength(const sp<Connection>& connection);
    void traceWaitQueueLength(const sp<Connection>& connection);