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

Commit 93ee540f authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Use a single trackListener method in LatencyTracker

This simplifies the code for the caller (InputDispatcher) and reduces the API surface of LatencyTracker.

Bug: 270049345
Change-Id: Ia25e95a24b5e89abcfce8060893ec3cce0c7892e
Flag: EXEMPT refactor
Test: atest inputflinger_tests
parent b7e4701c
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -50,6 +50,18 @@ InputDeviceUsageSource getUsageSourceForKeyArgs(int32_t keyboardType,
    return InputDeviceUsageSource::BUTTONS;
}

std::set<InputDeviceUsageSource> getUsageSourcesForKeyArgs(
        const NotifyKeyArgs& args, const std::vector<InputDeviceInfo>& inputDevices) {
    int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NONE;
    for (const InputDeviceInfo& inputDevice : inputDevices) {
        if (args.deviceId == inputDevice.getId()) {
            keyboardType = inputDevice.getKeyboardType();
            break;
        }
    }
    return std::set{getUsageSourceForKeyArgs(keyboardType, args)};
}

std::set<InputDeviceUsageSource> getUsageSourcesForMotionArgs(const NotifyMotionArgs& motionArgs) {
    LOG_ALWAYS_FATAL_IF(motionArgs.getPointerCount() < 1, "Received motion args without pointers");
    std::set<InputDeviceUsageSource> sources;
+4 −0
Original line number Diff line number Diff line
@@ -54,6 +54,10 @@ enum class InputDeviceUsageSource : int32_t {
/** Returns the InputDeviceUsageSource that corresponds to the key event. */
InputDeviceUsageSource getUsageSourceForKeyArgs(int32_t keyboardType, const NotifyKeyArgs&);

/** Returns the InputDeviceUsageSources that correspond to the key event. */
std::set<InputDeviceUsageSource> getUsageSourcesForKeyArgs(
        const NotifyKeyArgs&, const std::vector<InputDeviceInfo>& inputDevices);

/** Returns the InputDeviceUsageSources that correspond to the motion event. */
std::set<InputDeviceUsageSource> getUsageSourcesForMotionArgs(const NotifyMotionArgs&);

+2 −2
Original line number Diff line number Diff line
@@ -4551,7 +4551,7 @@ void InputDispatcher::notifyKey(const NotifyKeyArgs& args) {
            if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID &&
                IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER &&
                !mInputFilterEnabled) {
                mLatencyTracker.trackNotifyKey(args);
                mLatencyTracker.trackListener(args);
            }
        }

@@ -4687,7 +4687,7 @@ void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) {
        if (args.id != android::os::IInputConstants::INVALID_INPUT_EVENT_ID &&
            IdGenerator::getSource(args.id) == IdGenerator::Source::INPUT_READER &&
            !mInputFilterEnabled) {
            mLatencyTracker.trackNotifyMotion(args);
            mLatencyTracker.trackListener(args);
        }

        needWake = enqueueInboundEventLocked(std::move(newEntry));
+18 −17
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@

#include <inttypes.h>

#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android/os/IInputConstants.h>
@@ -32,6 +33,8 @@ using android::base::StringPrintf;

namespace android::inputdispatcher {

namespace {

/**
 * Events that are older than this time will be considered mature, at which point we will stop
 * waiting for the apps to provide further information about them.
@@ -62,27 +65,25 @@ static void eraseByValue(std::multimap<K, V>& map, const V& value) {
    }
}

} // namespace

LatencyTracker::LatencyTracker(InputEventTimelineProcessor& processor)
      : mTimelineProcessor(&processor) {}

void LatencyTracker::trackNotifyMotion(const NotifyMotionArgs& args) {
    std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(args);
    trackListener(args.id, args.eventTime, args.readTime, args.deviceId, sources, args.action,
                  InputEventType::MOTION);
}

void LatencyTracker::trackNotifyKey(const NotifyKeyArgs& args) {
    int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NONE;
    for (auto& inputDevice : mInputDevices) {
        if (args.deviceId == inputDevice.getId()) {
            keyboardType = inputDevice.getKeyboardType();
            break;
        }
    }
void LatencyTracker::trackListener(const NotifyArgs& args) {
    if (const NotifyKeyArgs* keyArgs = std::get_if<NotifyKeyArgs>(&args)) {
        std::set<InputDeviceUsageSource> sources =
            std::set{getUsageSourceForKeyArgs(keyboardType, args)};
    trackListener(args.id, args.eventTime, args.readTime, args.deviceId, sources, args.action,
                  InputEventType::KEY);
                getUsageSourcesForKeyArgs(*keyArgs, mInputDevices);
        trackListener(keyArgs->id, keyArgs->eventTime, keyArgs->readTime, keyArgs->deviceId,
                      sources, keyArgs->action, InputEventType::KEY);

    } else if (const NotifyMotionArgs* motionArgs = std::get_if<NotifyMotionArgs>(&args)) {
        std::set<InputDeviceUsageSource> sources = getUsageSourcesForMotionArgs(*motionArgs);
        trackListener(motionArgs->id, motionArgs->eventTime, motionArgs->readTime,
                      motionArgs->deviceId, sources, motionArgs->action, InputEventType::MOTION);
    } else {
        LOG(FATAL) << "Unexpected NotifyArgs type: " << args.index();
    }
}

void LatencyTracker::trackListener(int32_t inputEventId, nsecs_t eventTime, nsecs_t readTime,
+12 −16
Original line number Diff line number Diff line
@@ -44,28 +44,20 @@ public:
     */
    LatencyTracker(InputEventTimelineProcessor& processor);
    /**
     * Start keeping track of an event identified by inputEventId. This must be called first.
     * Start keeping track of an event identified by the args. This must be called first.
     * If duplicate events are encountered (events that have the same eventId), none of them will be
     * tracked. This is because there is not enough information to correctly track them. The api's
     * 'trackFinishedEvent' and 'trackGraphicsLatency' only contain the inputEventId, and not the
     * eventTime. Even if eventTime was provided, there would still be a possibility of having
     * duplicate events that happen to have the same eventTime and inputEventId. Therefore, we
     * must drop all duplicate data.
     * tracked. This is because there is not enough information to correctly track them. It is
     * always possible that two different events are generated with the same inputEventId and the
     * same eventTime, so there aren't ways to distinguish those. Therefore, we must drop all
     * duplicate data.
     * For that reason, the APIs 'trackFinishedEvent' and 'trackGraphicsLatency' only receive the
     * inputEventId as input.
     */
    void trackListener(int32_t inputEventId, nsecs_t eventTime, nsecs_t readTime, DeviceId deviceId,
                       const std::set<InputDeviceUsageSource>& sources, int32_t inputEventAction,
                       InputEventType inputEventType);
    void trackListener(const NotifyArgs& args);
    void trackFinishedEvent(int32_t inputEventId, const sp<IBinder>& connectionToken,
                            nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime);
    void trackGraphicsLatency(int32_t inputEventId, const sp<IBinder>& connectionToken,
                              std::array<nsecs_t, GraphicsTimeline::SIZE> timeline);
    /**
     * trackNotifyMotion and trackNotifyKeys are intermediates between InputDispatcher and
     * trackListener. They compute the InputDeviceUsageSource set and call trackListener with
     * the relevant parameters for latency computation.
     */
    void trackNotifyMotion(const NotifyMotionArgs& args);
    void trackNotifyKey(const NotifyKeyArgs& args);

    std::string dump(const char* prefix) const;
    void setInputDevices(const std::vector<InputDeviceInfo>& inputDevices);
@@ -90,6 +82,10 @@ private:

    InputEventTimelineProcessor* mTimelineProcessor;
    std::vector<InputDeviceInfo> mInputDevices;

    void trackListener(int32_t inputEventId, nsecs_t eventTime, nsecs_t readTime, DeviceId deviceId,
                       const std::set<InputDeviceUsageSource>& sources, int32_t inputEventAction,
                       InputEventType inputEventType);
    void reportAndPruneMatureRecords(nsecs_t newEventTime);
};

Loading