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

Commit d6b2b05f authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

InputTracer: Create tracker for tracing synthetic events

Allow the creation of a trace tracker for a synthetic event that does
not stem from an inbound input event. This is used for any dispatching
cycle that has a non-input event root, such as ANR timers, window
removals, API interactions (e.g. pilfer pointers), etc.

Any key or motion events generated for this synthetic event should be
traced as a derived event. We achieve this by passing the trace tracker
through the dispatching pipeline, and tracing all of the synthesized
events for that root using the same tracker.

Since all synthetic events can now be traced, we can now enforce that
all dispatched events have been previously traced as either an inbound
or derived event. This makes the event cookie non-nullable.

Bug: 210460522
Test: atest inputflinger_tests
Change-Id: I3417aee300edc251e2f7cb76c1f719502a5f5b8b
parent e7701644
Loading
Loading
Loading
Loading
+9 −1
Original line number Original line Diff line number Diff line
@@ -16,6 +16,8 @@


#pragma once
#pragma once


#include "trace/EventTrackerInterface.h"

#include <input/Input.h>
#include <input/Input.h>
#include <bitset>
#include <bitset>
#include <optional>
#include <optional>
@@ -51,7 +53,13 @@ struct CancelationOptions {
    // The specific pointers to cancel, or nullopt to cancel all pointer events
    // The specific pointers to cancel, or nullopt to cancel all pointer events
    std::optional<std::bitset<MAX_POINTER_ID + 1>> pointerIds = std::nullopt;
    std::optional<std::bitset<MAX_POINTER_ID + 1>> pointerIds = std::nullopt;


    CancelationOptions(Mode mode, const char* reason) : mode(mode), reason(reason) {}
    const std::unique_ptr<trace::EventTrackerInterface>& traceTracker;

    explicit CancelationOptions(Mode mode, const char* reason,
                                const std::unique_ptr<trace::EventTrackerInterface>& traceTracker)
          : mode(mode), reason(reason), traceTracker(traceTracker) {}
    CancelationOptions(const CancelationOptions&) = delete;
    CancelationOptions operator=(const CancelationOptions&) = delete;
};
};


} // namespace inputdispatcher
} // namespace inputdispatcher
+103 −32

File changed.

Preview size limit exceeded, changes collapsed.

+6 −2
Original line number Original line Diff line number Diff line
@@ -628,7 +628,8 @@ private:


    void synthesizePointerDownEventsForConnectionLocked(
    void synthesizePointerDownEventsForConnectionLocked(
            const nsecs_t downTime, const std::shared_ptr<Connection>& connection,
            const nsecs_t downTime, const std::shared_ptr<Connection>& connection,
            ftl::Flags<InputTarget::Flags> targetFlags) REQUIRES(mLock);
            ftl::Flags<InputTarget::Flags> targetFlags,
            const std::unique_ptr<trace::EventTrackerInterface>& traceTracker) REQUIRES(mLock);


    // Splitting motion events across windows. When splitting motion event for a target,
    // Splitting motion events across windows. When splitting motion event for a target,
    // splitDownTime refers to the time of first 'down' event on that particular target
    // splitDownTime refers to the time of first 'down' event on that particular target
@@ -657,6 +658,7 @@ private:
    void doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken,
    void doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken,
                                                const KeyEntry& entry) REQUIRES(mLock);
                                                const KeyEntry& entry) REQUIRES(mLock);
    void onFocusChangedLocked(const FocusResolver::FocusChanges& changes,
    void onFocusChangedLocked(const FocusResolver::FocusChanges& changes,
                              const std::unique_ptr<trace::EventTrackerInterface>& traceTracker,
                              const sp<gui::WindowInfoHandle> removedFocusedWindowHandle = nullptr)
                              const sp<gui::WindowInfoHandle> removedFocusedWindowHandle = nullptr)
            REQUIRES(mLock);
            REQUIRES(mLock);
    void sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, const sp<IBinder>& newToken)
    void sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, const sp<IBinder>& newToken)
@@ -704,7 +706,9 @@ private:
                                const sp<android::gui::WindowInfoHandle> fromWindowHandle,
                                const sp<android::gui::WindowInfoHandle> fromWindowHandle,
                                const sp<android::gui::WindowInfoHandle> toWindowHandle,
                                const sp<android::gui::WindowInfoHandle> toWindowHandle,
                                TouchState& state, int32_t deviceId,
                                TouchState& state, int32_t deviceId,
                                const std::vector<PointerProperties>& pointers) REQUIRES(mLock);
                                const std::vector<PointerProperties>& pointers,
                                const std::unique_ptr<trace::EventTrackerInterface>& traceTracker)
            REQUIRES(mLock);


    sp<android::gui::WindowInfoHandle> findWallpaperWindowBelow(
    sp<android::gui::WindowInfoHandle> findWallpaperWindowBelow(
            const sp<android::gui::WindowInfoHandle>& windowHandle) const REQUIRES(mLock);
            const sp<android::gui::WindowInfoHandle>& windowHandle) const REQUIRES(mLock);
+19 −4
Original line number Original line Diff line number Diff line
@@ -65,6 +65,10 @@ void writeEventToBackend(const TracedEvent& event, InputTracingBackendInterface&
               event);
               event);
}
}


inline auto getId(const trace::TracedEvent& v) {
    return std::visit([](const auto& event) { return event.id; }, v);
}

} // namespace
} // namespace


// --- InputTracer ---
// --- InputTracer ---
@@ -89,6 +93,12 @@ std::unique_ptr<EventTrackerInterface> InputTracer::traceInboundEvent(const Even
    return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/false);
    return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/false);
}
}


std::unique_ptr<EventTrackerInterface> InputTracer::createTrackerForSyntheticEvent() {
    // Create a new EventState to track events derived from this tracker.
    return std::make_unique<EventTrackerImpl>(std::make_shared<EventState>(*this),
                                              /*isDerived=*/false);
}

void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie,
void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie,
                                       const InputTarget& target) {
                                       const InputTarget& target) {
    if (isDerivedCookie(cookie)) {
    if (isDerivedCookie(cookie)) {
@@ -140,7 +150,8 @@ std::unique_ptr<EventTrackerInterface> InputTracer::traceDerivedEvent(
}
}


void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry,
void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry,
                                     const EventTrackerInterface* cookie) {
                                     const EventTrackerInterface& cookie) {
    auto& eventState = getState(cookie);
    const EventEntry& entry = *dispatchEntry.eventEntry;
    const EventEntry& entry = *dispatchEntry.eventEntry;
    // TODO(b/328618922): Remove resolved key repeats after making repeatCount non-mutable.
    // TODO(b/328618922): Remove resolved key repeats after making repeatCount non-mutable.
    // The KeyEntry's repeatCount is mutable and can be modified after an event is initially traced,
    // The KeyEntry's repeatCount is mutable and can be modified after an event is initially traced,
@@ -159,9 +170,13 @@ void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry,
        LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
        LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
    }
    }


    if (!cookie) {
    auto tracedEventIt =
        // This event was not tracked as an inbound event, so trace it now.
            std::find_if(eventState->events.begin(), eventState->events.end(),
        writeEventToBackend(traced, *mBackend);
                         [&traced](const auto& event) { return getId(traced) == getId(event); });
    if (tracedEventIt == eventState->events.end()) {
        LOG(FATAL)
                << __func__
                << ": Failed to find a previously traced event that matches the dispatched event";
    }
    }


    // The vsyncId only has meaning if the event is targeting a window.
    // The vsyncId only has meaning if the event is targeting a window.
+2 −1
Original line number Original line Diff line number Diff line
@@ -42,11 +42,12 @@ public:
    InputTracer& operator=(const InputTracer&) = delete;
    InputTracer& operator=(const InputTracer&) = delete;


    std::unique_ptr<EventTrackerInterface> traceInboundEvent(const EventEntry&) override;
    std::unique_ptr<EventTrackerInterface> traceInboundEvent(const EventEntry&) override;
    std::unique_ptr<EventTrackerInterface> createTrackerForSyntheticEvent() override;
    void dispatchToTargetHint(const EventTrackerInterface&, const InputTarget&) override;
    void dispatchToTargetHint(const EventTrackerInterface&, const InputTarget&) override;
    void eventProcessingComplete(const EventTrackerInterface&) override;
    void eventProcessingComplete(const EventTrackerInterface&) override;
    std::unique_ptr<EventTrackerInterface> traceDerivedEvent(const EventEntry&,
    std::unique_ptr<EventTrackerInterface> traceDerivedEvent(const EventEntry&,
                                                             const EventTrackerInterface&) override;
                                                             const EventTrackerInterface&) override;
    void traceEventDispatch(const DispatchEntry&, const EventTrackerInterface*) override;
    void traceEventDispatch(const DispatchEntry&, const EventTrackerInterface&) override;


private:
private:
    std::unique_ptr<InputTracingBackendInterface> mBackend;
    std::unique_ptr<InputTracingBackendInterface> mBackend;
Loading