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

Commit 6cd42cbd authored by Arpit Singh's avatar Arpit Singh Committed by Android (Google) Code Review
Browse files

Merge changes I65b2b48d,I0d41ba77 into main

* changes:
  [20/n Dispatcher refactor] Allow access to all TouchedWindowHandles
  [19/n Dispatcher refactor] Remove findTouchStateWindowAndDisplay
parents 27663062 071afa05
Loading
Loading
Loading
Loading
+50 −40
Original line number Diff line number Diff line
@@ -4072,13 +4072,17 @@ void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked(
    // Generate cancellations for touched windows first. This is to avoid generating cancellations
    // through a non-touched window if there are more than one window for an input channel.
    if (cancelPointers) {
        for (const auto& [displayId, touchState] : mTouchStates.mTouchStatesByDisplay) {
            if (options.displayId.has_value() && options.displayId != displayId) {
                continue;
            }
            for (const auto& touchedWindow : touchState.windows) {
                synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options);
            }
        if (options.displayId.has_value()) {
            mTouchStates.forAllTouchedWindowsOnDisplay(
                    options.displayId.value(), [&](const sp<gui::WindowInfoHandle>& windowHandle) {
                        base::ScopedLockAssertion assumeLocked(mLock);
                        synthesizeCancelationEventsForWindowLocked(windowHandle, options);
                    });
        } else {
            mTouchStates.forAllTouchedWindows([&](const sp<gui::WindowInfoHandle>& windowHandle) {
                base::ScopedLockAssertion assumeLocked(mLock);
                synthesizeCancelationEventsForWindowLocked(windowHandle, options);
            });
        }
    }
    // Follow up by generating cancellations for all windows, because we don't explicitly track
@@ -4276,13 +4280,13 @@ void InputDispatcher::synthesizePointerDownEventsForConnectionLocked(
              connection->getInputChannelName().c_str(), downEvents.size());
    }

    const auto [_, touchedWindowState, displayId] =
            findTouchStateWindowAndDisplay(connection->getToken(),
                                           mTouchStates.mTouchStatesByDisplay);
    if (touchedWindowState == nullptr) {
    auto touchedWindowHandleAndDisplay =
            mTouchStates.findTouchedWindowHandleAndDisplay(connection->getToken());
    if (!touchedWindowHandleAndDisplay.has_value()) {
        LOG(FATAL) << __func__ << ": Touch state is out of sync: No touched window for token";
    }
    const auto& windowHandle = touchedWindowState->windowHandle;

    const auto [windowHandle, displayId] = touchedWindowHandleAndDisplay.value();

    const bool wasEmpty = connection->outboundQueue.empty();
    for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) {
@@ -5798,34 +5802,6 @@ void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) {
    mWindowInfos.setMaximumObscuringOpacityForTouch(opacity);
}

std::tuple<const TouchState*, const TouchedWindow*, ui::LogicalDisplayId>
InputDispatcher::findTouchStateWindowAndDisplay(
        const sp<IBinder>& token,
        const std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay) {
    for (auto& [displayId, state] : touchStatesByDisplay) {
        for (const TouchedWindow& w : state.windows) {
            if (w.windowHandle->getToken() == token) {
                return std::make_tuple(&state, &w, displayId);
            }
        }
    }
    return std::make_tuple(nullptr, nullptr, ui::LogicalDisplayId::DEFAULT);
}

std::tuple<TouchState*, TouchedWindow*, ui::LogicalDisplayId>
InputDispatcher::findTouchStateWindowAndDisplay(
        const sp<IBinder>& token,
        std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay) {
    auto [constTouchState, constTouchedWindow, displayId] = InputDispatcher::
            findTouchStateWindowAndDisplay(token,
                                           const_cast<const std::unordered_map<ui::LogicalDisplayId,
                                                                               TouchState>&>(
                                                   touchStatesByDisplay));

    return std::make_tuple(const_cast<TouchState*>(constTouchState),
                           const_cast<TouchedWindow*>(constTouchedWindow), displayId);
}

bool InputDispatcher::transferTouchGesture(const sp<IBinder>& fromToken, const sp<IBinder>& toToken,
                                           bool isDragDrop) {
    if (fromToken == toToken) {
@@ -7492,6 +7468,40 @@ bool InputDispatcher::DispatcherTouchState::isPointerInWindow(const sp<android::
    return false;
}

std::optional<std::tuple<const sp<gui::WindowInfoHandle>&, ui::LogicalDisplayId>>
InputDispatcher::DispatcherTouchState::findTouchedWindowHandleAndDisplay(
        const sp<android::IBinder>& token) const {
    for (const auto& [displayId, state] : mTouchStatesByDisplay) {
        for (const TouchedWindow& w : state.windows) {
            if (w.windowHandle->getToken() == token) {
                return std::make_tuple(std::ref(w.windowHandle), displayId);
            }
        }
    }
    return std::nullopt;
}

void InputDispatcher::DispatcherTouchState::forAllTouchedWindows(
        std::function<void(const sp<gui::WindowInfoHandle>&)> f) const {
    for (const auto& [_, state] : mTouchStatesByDisplay) {
        for (const TouchedWindow& window : state.windows) {
            f(window.windowHandle);
        }
    }
}

void InputDispatcher::DispatcherTouchState::forAllTouchedWindowsOnDisplay(
        ui::LogicalDisplayId displayId,
        std::function<void(const sp<gui::WindowInfoHandle>&)> f) const {
    const auto touchStateIt = mTouchStatesByDisplay.find(displayId);
    if (touchStateIt == mTouchStatesByDisplay.end()) {
        return;
    }
    for (const TouchedWindow& window : touchStateIt->second.windows) {
        f(window.windowHandle);
    }
}

std::string InputDispatcher::DispatcherTouchState::dump() const {
    std::string dump;
    if (!mTouchStatesByDisplay.empty()) {
+10 −11
Original line number Diff line number Diff line
@@ -392,6 +392,16 @@ private:
        bool isPointerInWindow(const sp<android::IBinder>& token, ui::LogicalDisplayId displayId,
                               DeviceId deviceId, int32_t pointerId) const;

        // Find touched windowHandle and display by token.
        std::optional<std::tuple<const sp<gui::WindowInfoHandle>&, ui::LogicalDisplayId>>
        findTouchedWindowHandleAndDisplay(const sp<IBinder>& token) const;

        void forAllTouchedWindows(std::function<void(const sp<gui::WindowInfoHandle>&)> f) const;

        void forAllTouchedWindowsOnDisplay(
                ui::LogicalDisplayId displayId,
                std::function<void(const sp<gui::WindowInfoHandle>&)> f) const;

        std::string dump() const;

        // Updates the touchState for display from WindowInfo,
@@ -855,17 +865,6 @@ private:
            const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry,
            bool handled) REQUIRES(mLock);

    // Find touched state and touched window by token.
    static std::tuple<TouchState*, TouchedWindow*, ui::LogicalDisplayId>
    findTouchStateWindowAndDisplay(
            const sp<IBinder>& token,
            std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay);

    static std::tuple<const TouchState*, const TouchedWindow*, ui::LogicalDisplayId>
    findTouchStateWindowAndDisplay(
            const sp<IBinder>& token,
            const std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay);

    // Statistics gathering.
    nsecs_t mLastStatisticPushTime = 0;
    std::unique_ptr<InputEventTimelineProcessor> mInputEventTimelineProcessor GUARDED_BY(mLock);