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

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

Merge "NativeInputManager: Cleanup after PointerChoreographer refactor" into main

parents cc53ee04 1dca4eb8
Loading
Loading
Loading
Loading
+0 −14
Original line number Diff line number Diff line
@@ -142,7 +142,6 @@ PointerController::PointerController(const sp<PointerControllerPolicyInterface>&
PointerController::~PointerController() {
    mDisplayInfoListener->onPointerControllerDestroyed();
    mUnregisterWindowInfosListener(mDisplayInfoListener);
    mContext.getPolicy()->onPointerDisplayIdChanged(ADISPLAY_ID_NONE, FloatPoint{0, 0});
}

std::mutex& PointerController::getLock() const {
@@ -313,12 +312,6 @@ void PointerController::reloadPointerResources() {
}

void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
    struct PointerDisplayChangeArgs {
        int32_t displayId;
        FloatPoint cursorPosition;
    };
    std::optional<PointerDisplayChangeArgs> pointerDisplayChanged;

    { // acquire lock
        std::scoped_lock lock(getLock());

@@ -330,15 +323,8 @@ void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
        mCursorController.setDisplayViewport(viewport, getAdditionalMouseResources);
        if (viewport.displayId != mLocked.pointerDisplayId) {
            mLocked.pointerDisplayId = viewport.displayId;
            pointerDisplayChanged = {viewport.displayId, mCursorController.getPosition()};
        }
    } // release lock

    if (pointerDisplayChanged) {
        // Notify the policy without holding the pointer controller lock.
        mContext.getPolicy()->onPointerDisplayIdChanged(pointerDisplayChanged->displayId,
                                                        pointerDisplayChanged->cursorPosition);
    }
}

void PointerController::updatePointerIcon(PointerIconStyle iconId) {
+0 −1
Original line number Diff line number Diff line
@@ -81,7 +81,6 @@ public:
    virtual PointerIconStyle getDefaultPointerIconId() = 0;
    virtual PointerIconStyle getDefaultStylusIconId() = 0;
    virtual PointerIconStyle getCustomPointerIconId() = 0;
    virtual void onPointerDisplayIdChanged(int32_t displayId, const FloatPoint& position) = 0;
};

/*
+0 −33
Original line number Diff line number Diff line
@@ -64,11 +64,9 @@ public:
    virtual PointerIconStyle getDefaultPointerIconId() override;
    virtual PointerIconStyle getDefaultStylusIconId() override;
    virtual PointerIconStyle getCustomPointerIconId() override;
    virtual void onPointerDisplayIdChanged(int32_t displayId, const FloatPoint& position) override;

    bool allResourcesAreLoaded();
    bool noResourcesAreLoaded();
    std::optional<int32_t> getLastReportedPointerDisplayId() { return latestPointerDisplayId; }

private:
    void loadPointerIconForType(SpriteIcon* icon, int32_t cursorType);
@@ -76,7 +74,6 @@ private:
    bool pointerIconLoaded{false};
    bool pointerResourcesLoaded{false};
    bool additionalMouseResourcesLoaded{false};
    std::optional<int32_t /*displayId*/> latestPointerDisplayId;
};

void MockPointerControllerPolicyInterface::loadPointerIcon(SpriteIcon* icon, int32_t) {
@@ -146,12 +143,6 @@ void MockPointerControllerPolicyInterface::loadPointerIconForType(SpriteIcon* ic
    icon->hotSpotY = hotSpot.second;
}

void MockPointerControllerPolicyInterface::onPointerDisplayIdChanged(int32_t displayId,
                                                                     const FloatPoint& /*position*/
) {
    latestPointerDisplayId = displayId;
}

class TestPointerController : public PointerController {
public:
    TestPointerController(sp<android::gui::WindowInfosListener>& registeredListener,
@@ -348,30 +339,6 @@ TEST_F(PointerControllerTest, doesNotGetResourcesBeforeSettingViewport) {
    ensureDisplayViewportIsSet();
}

TEST_F(PointerControllerTest, notifiesPolicyWhenPointerDisplayChanges) {
    EXPECT_FALSE(mPolicy->getLastReportedPointerDisplayId())
            << "A pointer display change does not occur when PointerController is created.";

    ensureDisplayViewportIsSet(ADISPLAY_ID_DEFAULT);

    const auto lastReportedPointerDisplayId = mPolicy->getLastReportedPointerDisplayId();
    ASSERT_TRUE(lastReportedPointerDisplayId)
            << "The policy is notified of a pointer display change when the viewport is first set.";
    EXPECT_EQ(ADISPLAY_ID_DEFAULT, *lastReportedPointerDisplayId)
            << "Incorrect pointer display notified.";

    ensureDisplayViewportIsSet(42);

    EXPECT_EQ(42, *mPolicy->getLastReportedPointerDisplayId())
            << "The policy is notified when the pointer display changes.";

    // Release the PointerController.
    mPointerController = nullptr;

    EXPECT_EQ(ADISPLAY_ID_NONE, *mPolicy->getLastReportedPointerDisplayId())
            << "The pointer display changes to invalid when PointerController is destroyed.";
}

TEST_F(PointerControllerTest, updatesSkipScreenshotFlagForTouchSpots) {
    ensureDisplayViewportIsSet();

+9 −149
Original line number Diff line number Diff line
@@ -88,7 +88,6 @@ namespace input_flags = com::android::input::flags;

namespace android {

static const bool ENABLE_POINTER_CHOREOGRAPHER = input_flags::enable_pointer_choreographer();
static const bool ENABLE_INPUT_FILTER_RUST = input_flags::enable_input_filter_rust_impl();

// The exponent used to calculate the pointer speed scaling factor.
@@ -298,10 +297,8 @@ public:
    void setShowTouches(bool enabled);
    void setInteractive(bool interactive);
    void reloadCalibration();
    void setPointerIconType(PointerIconStyle iconId);
    void reloadPointerIcons();
    void requestPointerCapture(const sp<IBinder>& windowToken, bool enabled);
    void setCustomPointerIcon(const SpriteIcon& icon);
    bool setPointerIcon(std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon,
                        int32_t displayId, DeviceId deviceId, int32_t pointerId,
                        const sp<IBinder>& inputToken);
@@ -316,7 +313,6 @@ public:
    /* --- InputReaderPolicyInterface implementation --- */

    void getReaderConfiguration(InputReaderConfiguration* outConfig) override;
    std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override;
    void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override;
    std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
            const InputDeviceIdentifier& identifier,
@@ -375,7 +371,6 @@ public:
    virtual PointerIconStyle getDefaultPointerIconId();
    virtual PointerIconStyle getDefaultStylusIconId();
    virtual PointerIconStyle getCustomPointerIconId();
    virtual void onPointerDisplayIdChanged(int32_t displayId, const FloatPoint& position);

    /* --- PointerChoreographerPolicyInterface implementation --- */
    std::shared_ptr<PointerControllerInterface> createPointerController(
@@ -409,19 +404,12 @@ private:
        // True if pointer gestures are enabled.
        bool pointerGesturesEnabled{true};

        // Show touches feature enable/disable.
        bool showTouches{false};

        // The latest request to enable or disable Pointer Capture.
        PointerCaptureRequest pointerCaptureRequest{};

        // Sprite controller singleton, created on first use.
        std::shared_ptr<SpriteController> spriteController{};

        // TODO(b/293587049): Remove when the PointerChoreographer refactoring is complete.
        // Pointer controller singleton, created and destroyed as needed.
        std::weak_ptr<PointerController> legacyPointerController{};

        // The list of PointerControllers created and managed by the PointerChoreographer.
        std::list<std::weak_ptr<PointerController>> pointerControllers{};

@@ -505,13 +493,9 @@ void NativeInputManager::dump(std::string& dump) {
                             dumpSet(mLocked.displaysWithMousePointerAccelerationDisabled).c_str());
        dump += StringPrintf(INDENT "Pointer Gestures Enabled: %s\n",
                             toString(mLocked.pointerGesturesEnabled));
        dump += StringPrintf(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
        dump += StringPrintf(INDENT "Pointer Capture: %s, seq=%" PRIu32 "\n",
                             mLocked.pointerCaptureRequest.isEnable() ? "Enabled" : "Disabled",
                             mLocked.pointerCaptureRequest.seq);
        if (auto pc = mLocked.legacyPointerController.lock(); pc) {
            dump += pc->dump();
        }
    } // release lock
    dump += "\n";

@@ -556,9 +540,7 @@ void NativeInputManager::setDisplayViewports(JNIEnv* env, jobjectArray viewportO
                [&viewports](PointerController& pc) { pc.onDisplayViewportsUpdated(viewports); });
    } // release lock

    if (ENABLE_POINTER_CHOREOGRAPHER) {
    mInputManager->getChoreographer().setDisplayViewports(viewports);
    }
    mInputManager->getReader().requestRefreshConfiguration(
            InputReaderConfiguration::Change::DISPLAY_INFO);
}
@@ -700,8 +682,6 @@ void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outCon
                : 1;
        outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;

        outConfig->showTouches = mLocked.showTouches;

        outConfig->pointerCaptureRequest = mLocked.pointerCaptureRequest;

        outConfig->setDisplayViewports(mLocked.viewports);
@@ -743,10 +723,6 @@ std::unordered_map<std::string, T> NativeInputManager::readMapFromInterleavedJav

void NativeInputManager::forEachPointerControllerLocked(
        std::function<void(PointerController&)> apply) {
    if (auto pc = mLocked.legacyPointerController.lock(); pc) {
        apply(*pc);
    }

    auto it = mLocked.pointerControllers.begin();
    while (it != mLocked.pointerControllers.end()) {
        auto pc = it->lock();
@@ -780,28 +756,6 @@ PointerIcon NativeInputManager::loadPointerIcon(JNIEnv* env, int32_t displayId,
    return android_view_PointerIcon_toNative(env, pointerIconObj.get());
}

// TODO(b/293587049): Remove the old way of obtaining PointerController when the
//  PointerChoreographer refactoring is complete.
std::shared_ptr<PointerControllerInterface> NativeInputManager::obtainPointerController(
        int32_t /* deviceId */) {
    ATRACE_CALL();
    std::scoped_lock _l(mLock);

    std::shared_ptr<PointerController> controller = mLocked.legacyPointerController.lock();
    if (controller == nullptr) {
        ensureSpriteControllerLocked();

        // Disable the functionality of the legacy PointerController if PointerChoreographer is
        // enabled.
        controller = PointerController::create(this, mLooper, *mLocked.spriteController,
                                               /*enabled=*/!ENABLE_POINTER_CHOREOGRAPHER);
        mLocked.legacyPointerController = controller;
        updateInactivityTimeoutLocked();
    }

    return controller;
}

std::shared_ptr<PointerControllerInterface> NativeInputManager::createPointerController(
        PointerControllerInterface::ControllerType type) {
    std::scoped_lock _l(mLock);
@@ -813,17 +767,6 @@ std::shared_ptr<PointerControllerInterface> NativeInputManager::createPointerCon
    return pc;
}

void NativeInputManager::onPointerDisplayIdChanged(int32_t pointerDisplayId,
                                                   const FloatPoint& position) {
    if (ENABLE_POINTER_CHOREOGRAPHER) {
        return;
    }
    JNIEnv* env = jniEnv();
    env->CallVoidMethod(mServiceObj, gServiceClassInfo.onPointerDisplayIdChanged, pointerDisplayId,
                        position.x, position.y);
    checkAndClearExceptionFromCallback(env, "onPointerDisplayIdChanged");
}

void NativeInputManager::notifyPointerDisplayIdChanged(int32_t pointerDisplayId,
                                                       const FloatPoint& position) {
    // Notify the Reader so that devices can be reconfigured.
@@ -1210,23 +1153,7 @@ void NativeInputManager::updateInactivityTimeoutLocked() REQUIRES(mLock) {
}

void NativeInputManager::setPointerDisplayId(int32_t displayId) {
    if (ENABLE_POINTER_CHOREOGRAPHER) {
    mInputManager->getChoreographer().setDefaultMouseDisplayId(displayId);
    } else {
        { // acquire lock
            std::scoped_lock _l(mLock);

            if (mLocked.pointerDisplayId == displayId) {
                return;
            }

            ALOGI("Setting pointer display id to %d.", displayId);
            mLocked.pointerDisplayId = displayId;
        } // release lock

        mInputManager->getReader().requestRefreshConfiguration(
                InputReaderConfiguration::Change::DISPLAY_INFO);
    }
}

int32_t NativeInputManager::getMousePointerSpeed() {
@@ -1378,24 +1305,7 @@ void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled)
}

void NativeInputManager::setShowTouches(bool enabled) {
    if (ENABLE_POINTER_CHOREOGRAPHER) {
    mInputManager->getChoreographer().setShowTouchesEnabled(enabled);
        return;
    }

    { // acquire lock
        std::scoped_lock _l(mLock);

        if (mLocked.showTouches == enabled) {
            return;
        }

        ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
        mLocked.showTouches = enabled;
    } // release lock

    mInputManager->getReader().requestRefreshConfiguration(
            InputReaderConfiguration::Change::SHOW_TOUCHES);
}

void NativeInputManager::requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) {
@@ -1411,27 +1321,11 @@ void NativeInputManager::reloadCalibration() {
            InputReaderConfiguration::Change::TOUCH_AFFINE_TRANSFORMATION);
}

void NativeInputManager::setPointerIconType(PointerIconStyle iconId) {
    std::scoped_lock _l(mLock);
    std::shared_ptr<PointerController> controller = mLocked.legacyPointerController.lock();
    if (controller != nullptr) {
        controller->updatePointerIcon(iconId);
    }
}

void NativeInputManager::reloadPointerIcons() {
    std::scoped_lock _l(mLock);
    forEachPointerControllerLocked([](PointerController& pc) { pc.reloadPointerResources(); });
}

void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
    std::scoped_lock _l(mLock);
    std::shared_ptr<PointerController> controller = mLocked.legacyPointerController.lock();
    if (controller != nullptr) {
        controller->setCustomPointerIcon(icon);
    }
}

bool NativeInputManager::setPointerIcon(
        std::variant<std::unique_ptr<SpriteIcon>, PointerIconStyle> icon, int32_t displayId,
        DeviceId deviceId, int32_t pointerId, const sp<IBinder>& inputToken) {
@@ -1447,9 +1341,6 @@ bool NativeInputManager::setPointerIcon(
}

void NativeInputManager::setPointerIconVisibility(int32_t displayId, bool visible) {
    if (!ENABLE_POINTER_CHOREOGRAPHER) {
        return;
    }
    mInputManager->getChoreographer().setPointerIconVisibility(displayId, visible);
}

@@ -1819,38 +1710,14 @@ void NativeInputManager::setStylusButtonMotionEventsEnabled(bool enabled) {
}

FloatPoint NativeInputManager::getMouseCursorPosition(int32_t displayId) {
    if (ENABLE_POINTER_CHOREOGRAPHER) {
    return mInputManager->getChoreographer().getMouseCursorPosition(displayId);
}
    // To maintain the status-quo, the displayId parameter (used when PointerChoreographer is
    // enabled) is ignored in the old pipeline.
    std::scoped_lock _l(mLock);
    const auto pc = mLocked.legacyPointerController.lock();
    if (!pc) return {AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION};

    return pc->getPosition();
}

void NativeInputManager::setStylusPointerIconEnabled(bool enabled) {
    if (ENABLE_POINTER_CHOREOGRAPHER) {
    mInputManager->getChoreographer().setStylusPointerIconEnabled(enabled);
    return;
}

    { // acquire lock
        std::scoped_lock _l(mLock);

        if (mLocked.stylusPointerIconEnabled == enabled) {
            return;
        }

        mLocked.stylusPointerIconEnabled = enabled;
    } // release lock

    mInputManager->getReader().requestRefreshConfiguration(
            InputReaderConfiguration::Change::DISPLAY_INFO);
}

void NativeInputManager::setInputMethodConnectionIsActive(bool isActive) {
    { // acquire lock
        std::scoped_lock _l(mLock);
@@ -2598,13 +2465,7 @@ static void nativeDisableInputDevice(JNIEnv* env, jobject nativeImplObj, jint de
}

static void nativeSetPointerIconType(JNIEnv* env, jobject nativeImplObj, jint iconId) {
    // iconId is set in java from from frameworks/base/core/java/android/view/PointerIcon.java,
    // where the definition in <input/Input.h> is duplicated as a sealed class (type safe enum
    // equivalent in Java).

    NativeInputManager* im = getNativeInputManager(env, nativeImplObj);

    im->setPointerIconType(static_cast<PointerIconStyle>(iconId));
    // TODO(b/311416205): Remove
}

static void nativeReloadPointerIcons(JNIEnv* env, jobject nativeImplObj) {
@@ -2614,8 +2475,7 @@ static void nativeReloadPointerIcons(JNIEnv* env, jobject nativeImplObj) {
}

static void nativeSetCustomPointerIcon(JNIEnv* env, jobject nativeImplObj, jobject iconObj) {
    NativeInputManager* im = getNativeInputManager(env, nativeImplObj);
    im->setCustomPointerIcon(toSpriteIcon(android_view_PointerIcon_toNative(env, iconObj)));
    // TODO(b/311416205): Remove
}

static bool nativeSetPointerIcon(JNIEnv* env, jobject nativeImplObj, jobject iconObj,