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

Commit ed1244cb authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Move InputApplication to use std::shared_ptr."

parents 6278ece8 a209fdef
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ namespace android {
 * Used by the native input dispatcher as a handle for the window manager objects
 * that describe an application.
 */
class InputApplicationHandle : public RefBase {
class InputApplicationHandle {
public:
    inline const InputApplicationInfo* getInfo() const {
        return &mInfo;
@@ -55,6 +55,12 @@ public:
        return mInfo.token;
    }

    bool operator==(const InputApplicationHandle& other) const {
        return getName() == other.getName() && getApplicationToken() == other.getApplicationToken();
    }

    bool operator!=(const InputApplicationHandle& other) const { return !(*this == other); }

    /**
     * Requests that the state of this object be updated to reflect
     * the most current available information about the application.
+5 −5
Original line number Diff line number Diff line
@@ -47,8 +47,8 @@ protected:
private:
    void notifyConfigurationChanged(nsecs_t) override {}

    std::chrono::nanoseconds notifyAnr(const sp<InputApplicationHandle>&, const sp<IBinder>&,
                                       const std::string& name) override {
    std::chrono::nanoseconds notifyAnr(const std::shared_ptr<InputApplicationHandle>&,
                                       const sp<IBinder>&, const std::string& name) override {
        ALOGE("The window is not responding : %s", name.c_str());
        return 0s;
    }
@@ -148,7 +148,7 @@ public:
    static const int32_t WIDTH = 200;
    static const int32_t HEIGHT = 200;

    FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
    FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
                     const sp<InputDispatcher>& dispatcher, const std::string name)
          : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
        mDispatcher->registerInputChannel(mServerChannel);
@@ -246,7 +246,7 @@ static void benchmarkNotifyMotion(benchmark::State& state) {
    dispatcher->start();

    // Create a window that will receive motion events
    sp<FakeApplicationHandle> application = new FakeApplicationHandle();
    std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
    sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");

    dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
@@ -282,7 +282,7 @@ static void benchmarkInjectMotion(benchmark::State& state) {
    dispatcher->start();

    // Create a window that will receive motion events
    sp<FakeApplicationHandle> application = new FakeApplicationHandle();
    std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
    sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");

    dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
+1 −1
Original line number Diff line number Diff line
@@ -254,7 +254,7 @@ struct CommandEntry {
    sp<Connection> connection;
    nsecs_t eventTime;
    KeyEntry* keyEntry;
    sp<InputApplicationHandle> inputApplicationHandle;
    std::shared_ptr<InputApplicationHandle> inputApplicationHandle;
    std::string reason;
    int32_t userActivityEventType;
    uint32_t seq;
+30 −24
Original line number Diff line number Diff line
@@ -496,7 +496,7 @@ nsecs_t InputDispatcher::processAnrsLocked() {
    if (mNoFocusedWindowTimeoutTime.has_value() && mAwaitedFocusedApplication != nullptr) {
        if (currentTime >= *mNoFocusedWindowTimeoutTime) {
            onAnrLocked(mAwaitedFocusedApplication);
            mAwaitedFocusedApplication.clear();
            mAwaitedFocusedApplication.reset();
            return LONG_LONG_MIN;
        } else {
            // Keep waiting
@@ -1389,7 +1389,7 @@ void InputDispatcher::resetNoFocusedWindowTimeoutLocked() {

    // Reset input target wait timeout.
    mNoFocusedWindowTimeoutTime = std::nullopt;
    mAwaitedFocusedApplication.clear();
    mAwaitedFocusedApplication.reset();
}

/**
@@ -1459,7 +1459,7 @@ int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime,
    int32_t displayId = getTargetDisplayId(entry);
    sp<InputWindowHandle> focusedWindowHandle =
            getValueByKey(mFocusedWindowHandlesByDisplay, displayId);
    sp<InputApplicationHandle> focusedApplicationHandle =
    std::shared_ptr<InputApplicationHandle> focusedApplicationHandle =
            getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);

    // If there is no currently focused window and no focused application
@@ -2149,7 +2149,7 @@ bool InputDispatcher::isWindowObscuredLocked(const sp<InputWindowHandle>& window
}

std::string InputDispatcher::getApplicationWindowLabel(
        const sp<InputApplicationHandle>& applicationHandle,
        const std::shared_ptr<InputApplicationHandle>& applicationHandle,
        const sp<InputWindowHandle>& windowHandle) {
    if (applicationHandle != nullptr) {
        if (windowHandle != nullptr) {
@@ -3868,30 +3868,36 @@ void InputDispatcher::setInputWindowsLocked(
}

void InputDispatcher::setFocusedApplication(
        int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) {
        int32_t displayId, const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) {
    if (DEBUG_FOCUS) {
        ALOGD("setFocusedApplication displayId=%" PRId32 " %s", displayId,
              inputApplicationHandle ? inputApplicationHandle->getName().c_str() : "<nullptr>");
    }
    { // acquire lock
    if (inputApplicationHandle != nullptr &&
        inputApplicationHandle->getApplicationToken() != nullptr) {
        // acquire lock
        std::scoped_lock _l(mLock);

        sp<InputApplicationHandle> oldFocusedApplicationHandle =
        std::shared_ptr<InputApplicationHandle> oldFocusedApplicationHandle =
                getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);

        if (oldFocusedApplicationHandle == mAwaitedFocusedApplication &&
            inputApplicationHandle != oldFocusedApplicationHandle) {
        // If oldFocusedApplicationHandle already exists
        if (oldFocusedApplicationHandle != nullptr) {
            // If a new focused application handle is different from the old one and
            // old focus application info is awaited focused application info.
            if (*oldFocusedApplicationHandle != *inputApplicationHandle &&
                mAwaitedFocusedApplication != nullptr &&
                *oldFocusedApplicationHandle == *mAwaitedFocusedApplication) {
                resetNoFocusedWindowTimeoutLocked();
            }

        if (inputApplicationHandle != nullptr && inputApplicationHandle->updateInfo()) {
            if (oldFocusedApplicationHandle != inputApplicationHandle) {
                mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle;
            }
        } else if (oldFocusedApplicationHandle != nullptr) {
            oldFocusedApplicationHandle.clear();
            // Erase the old application from container first
            mFocusedApplicationHandlesByDisplay.erase(displayId);
            // Should already get freed after removed from container but just double check.
            oldFocusedApplicationHandle.reset();
        }

        // Set the new application handle.
        mFocusedApplicationHandlesByDisplay[displayId] = inputApplicationHandle;
    } // release lock

    // Wake up poll loop since it may need to make new input dispatching choices.
@@ -4138,7 +4144,7 @@ void InputDispatcher::dumpDispatchStateLocked(std::string& dump) {
        dump += StringPrintf(INDENT "FocusedApplications:\n");
        for (auto& it : mFocusedApplicationHandlesByDisplay) {
            const int32_t displayId = it.first;
            const sp<InputApplicationHandle>& applicationHandle = it.second;
            const std::shared_ptr<InputApplicationHandle>& applicationHandle = it.second;
            const int64_t timeoutMillis = millis(
                    applicationHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT));
            dump += StringPrintf(INDENT2 "displayId=%" PRId32
@@ -4652,7 +4658,7 @@ void InputDispatcher::onAnrLocked(const sp<Connection>& connection) {
    postCommandLocked(std::move(commandEntry));
}

void InputDispatcher::onAnrLocked(const sp<InputApplicationHandle>& application) {
void InputDispatcher::onAnrLocked(const std::shared_ptr<InputApplicationHandle>& application) {
    std::string reason = android::base::StringPrintf("%s does not have a focused window",
                                                     application->getName().c_str());

@@ -4672,8 +4678,8 @@ void InputDispatcher::updateLastAnrStateLocked(const sp<InputWindowHandle>& wind
    updateLastAnrStateLocked(windowLabel, reason);
}

void InputDispatcher::updateLastAnrStateLocked(const sp<InputApplicationHandle>& application,
                                               const std::string& reason) {
void InputDispatcher::updateLastAnrStateLocked(
        const std::shared_ptr<InputApplicationHandle>& application, const std::string& reason) {
    const std::string windowLabel = getApplicationWindowLabel(application, nullptr);
    updateLastAnrStateLocked(windowLabel, reason);
}
@@ -4744,9 +4750,9 @@ void InputDispatcher::doNotifyAnrLockedInterruptible(CommandEntry* commandEntry)
    }
}

void InputDispatcher::extendAnrTimeoutsLocked(const sp<InputApplicationHandle>& application,
                                              const sp<IBinder>& connectionToken,
                                              std::chrono::nanoseconds timeoutExtension) {
void InputDispatcher::extendAnrTimeoutsLocked(
        const std::shared_ptr<InputApplicationHandle>& application,
        const sp<IBinder>& connectionToken, std::chrono::nanoseconds timeoutExtension) {
    sp<Connection> connection = getConnectionLocked(connectionToken);
    if (connection == nullptr) {
        if (mNoFocusedWindowTimeoutTime.has_value() && application != nullptr) {
+11 −9
Original line number Diff line number Diff line
@@ -115,7 +115,8 @@ public:
            const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>&
                    handlesPerDisplay) override;
    virtual void setFocusedApplication(
            int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) override;
            int32_t displayId,
            const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) override;
    virtual void setFocusedDisplay(int32_t displayId) override;
    virtual void setInputDispatchMode(bool enabled, bool frozen) override;
    virtual void setInputFilterEnabled(bool enabled) override;
@@ -321,8 +322,8 @@ private:
    std::unordered_map<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);

    // Focused applications.
    std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay
            GUARDED_BY(mLock);
    std::unordered_map<int32_t, std::shared_ptr<InputApplicationHandle>>
            mFocusedApplicationHandlesByDisplay GUARDED_BY(mLock);

    // Top focused display.
    int32_t mFocusedDisplayId GUARDED_BY(mLock);
@@ -374,7 +375,7 @@ private:
     * The focused application at the time when no focused window was present.
     * Used to raise an ANR when we have no focused window.
     */
    sp<InputApplicationHandle> mAwaitedFocusedApplication GUARDED_BY(mLock);
    std::shared_ptr<InputApplicationHandle> mAwaitedFocusedApplication GUARDED_BY(mLock);

    // Optimization: AnrTracker is used to quickly find which connection is due for a timeout next.
    // AnrTracker must be kept in-sync with all responsive connection.waitQueues.
@@ -382,7 +383,7 @@ private:
    // Once a connection becomes unresponsive, its entries are removed from AnrTracker to
    // prevent unneeded wakeups.
    AnrTracker mAnrTracker GUARDED_BY(mLock);
    void extendAnrTimeoutsLocked(const sp<InputApplicationHandle>& application,
    void extendAnrTimeoutsLocked(const std::shared_ptr<InputApplicationHandle>& application,
                                 const sp<IBinder>& connectionToken,
                                 std::chrono::nanoseconds timeoutExtension) REQUIRES(mLock);

@@ -426,7 +427,8 @@ private:
    bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, int32_t x,
                                       int32_t y) const REQUIRES(mLock);
    bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
    std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle,
    std::string getApplicationWindowLabel(
            const std::shared_ptr<InputApplicationHandle>& applicationHandle,
            const sp<InputWindowHandle>& windowHandle);

    // Manage the dispatch cycle for a single connection.
@@ -499,10 +501,10 @@ private:
    void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
                              const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
    void onAnrLocked(const sp<Connection>& connection) REQUIRES(mLock);
    void onAnrLocked(const sp<InputApplicationHandle>& application) REQUIRES(mLock);
    void onAnrLocked(const std::shared_ptr<InputApplicationHandle>& application) REQUIRES(mLock);
    void updateLastAnrStateLocked(const sp<InputWindowHandle>& window, const std::string& reason)
            REQUIRES(mLock);
    void updateLastAnrStateLocked(const sp<InputApplicationHandle>& application,
    void updateLastAnrStateLocked(const std::shared_ptr<InputApplicationHandle>& application,
                                  const std::string& reason) REQUIRES(mLock);
    void updateLastAnrStateLocked(const std::string& windowLabel, const std::string& reason)
            REQUIRES(mLock);
Loading