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

Commit a7333114 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Move stale event check into dispatcher policy

This cleans up the dispatcher interface a bit, but also, it allows us to
change the timeout more easily for tests.

Test: TEST=inputflinger_tests; m $TEST && $ANDROID_HOST_OUT/nativetest64/$TEST/$TEST
Bug: 308153460
Change-Id: Ieb4c7a9d53b62b0e0a73c4f43038db949a821993
parent 5d7faea1
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -756,10 +756,6 @@ bool shouldSplitTouch(const TouchState& touchState, const MotionEntry& entry) {
// --- InputDispatcher ---

InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy)
      : InputDispatcher(policy, STALE_EVENT_TIMEOUT) {}

InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
                                 std::chrono::nanoseconds staleEventTimeout)
      : mPolicy(policy),
        mPendingEvent(nullptr),
        mLastDropReason(DropReason::NOT_DROPPED),
@@ -774,7 +770,6 @@ InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
        mMaximumObscuringOpacityForTouch(1.0f),
        mFocusedDisplayId(ADISPLAY_ID_DEFAULT),
        mWindowTokenWithPointerCapture(nullptr),
        mStaleEventTimeout(staleEventTimeout),
        mLatencyAggregator(),
        mLatencyTracker(&mLatencyAggregator) {
    mLooper = sp<Looper>::make(false);
@@ -1131,7 +1126,7 @@ void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) {
}

bool InputDispatcher::isStaleEvent(nsecs_t currentTime, const EventEntry& entry) {
    return std::chrono::nanoseconds(currentTime - entry.eventTime) >= mStaleEventTimeout;
    return mPolicy.isStaleEvent(currentTime, entry.eventTime);
}

/**
+0 −5
Original line number Diff line number Diff line
@@ -82,8 +82,6 @@ public:
    static constexpr bool kDefaultInTouchMode = true;

    explicit InputDispatcher(InputDispatcherPolicyInterface& policy);
    explicit InputDispatcher(InputDispatcherPolicyInterface& policy,
                             std::chrono::nanoseconds staleEventTimeout);
    ~InputDispatcher() override;

    void dump(std::string& dump) const override;
@@ -461,9 +459,6 @@ private:
     */
    std::optional<nsecs_t> mNoFocusedWindowTimeoutTime GUARDED_BY(mLock);

    // Amount of time to allow for an event to be dispatched (measured since its eventTime)
    // before considering it stale and dropping it.
    const std::chrono::nanoseconds mStaleEventTimeout;
    bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry);

    bool shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) REQUIRES(mLock);
+11 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#include "InputDispatcherConfiguration.h"

#include <android-base/properties.h>
#include <binder/IBinder.h>
#include <gui/InputApplication.h>
#include <input/Input.h>
@@ -118,6 +119,16 @@ public:
    /* Poke user activity for an event dispatched to a window. */
    virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType, int32_t displayId) = 0;

    /*
     * Return true if the provided event is stale, and false otherwise. Used for determining
     * whether the dispatcher should drop the event.
     */
    virtual bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) {
        static const std::chrono::duration STALE_EVENT_TIMEOUT =
                std::chrono::seconds(10) * android::base::HwTimeoutMultiplier();
        return std::chrono::nanoseconds(currentTime - eventTime) >= STALE_EVENT_TIMEOUT;
    }

    /* Notifies the policy that a pointer down event has occurred outside the current focused
     * window.
     *
+12 −3
Original line number Diff line number Diff line
@@ -112,8 +112,6 @@ static constexpr gui::Uid SECONDARY_WINDOW_UID{1012};
// An arbitrary pid of the gesture monitor window
static constexpr gui::Pid MONITOR_PID{2001};
static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
/**
 * If we expect to receive the event, the timeout can be made very long. When the test are running
 * correctly, we will actually never wait until the end of the timeout because the wait will end
@@ -348,6 +346,8 @@ public:
        mInterceptKeyTimeout = timeout;
    }
    void setStaleEventTimeout(std::chrono::nanoseconds timeout) { mStaleEventTimeout = timeout; }
    void assertUserActivityPoked() {
        std::scoped_lock lock(mLock);
        ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked";
@@ -391,6 +391,8 @@ private:
    std::chrono::milliseconds mInterceptKeyTimeout = 0ms;
    std::chrono::nanoseconds mStaleEventTimeout = 1000ms;
    BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
    // All three ANR-related callbacks behave the same way, so we use this generic function to wait
@@ -545,6 +547,10 @@ private:
        mPokedUserActivity = true;
    }
    bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override {
        return std::chrono::nanoseconds(currentTime - eventTime) >= mStaleEventTimeout;
    }
    void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {
        std::scoped_lock lock(mLock);
        mOnPointerDownToken = newToken;
@@ -586,7 +592,8 @@ protected:
    void SetUp() override {
        mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>();
        mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT);
        mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy);
        mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false);
        // Start InputDispatcher thread
        ASSERT_EQ(OK, mDispatcher->start());
@@ -7529,6 +7536,8 @@ TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) {
    mWindow->consumeFocusEvent(false);
    KeyEvent event;
    static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms;
    mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT);
    const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) -
            std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count();