diff options
| author | 2024-03-05 22:23:57 +0000 | |
|---|---|---|
| committer | 2024-03-05 22:23:57 +0000 | |
| commit | eaa9fde906b02a87155f54963eb659b6a70830ae (patch) | |
| tree | 81a628069d6b6a8c266187758ab6e9efaa3b8587 | |
| parent | 31dafa390a7b7aa399e8554fc1be7f9ab1b5cd30 (diff) | |
| parent | 052fb0b3f235249578ca4352d2b08e70bfbd8fe1 (diff) | |
Merge changes I4b61ef85,I8f97648d into main
* changes:
InputTracer: Ensure eventProcessingComplete called after dropping events
InputTracer: Minor readability improvements
| -rw-r--r-- | services/inputflinger/dispatcher/InputDispatcher.cpp | 34 | ||||
| -rw-r--r-- | services/inputflinger/dispatcher/trace/InputTracer.cpp | 23 | ||||
| -rw-r--r-- | services/inputflinger/dispatcher/trace/InputTracer.h | 11 |
3 files changed, 43 insertions, 25 deletions
diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp index 9c04a1dc0b..057b996a23 100644 --- a/services/inputflinger/dispatcher/InputDispatcher.cpp +++ b/services/inputflinger/dispatcher/InputDispatcher.cpp @@ -103,6 +103,26 @@ void ensureEventTraced(const Entry& entry) { } } +// Helper to get a trace tracker from a traced key or motion entry. +const std::unique_ptr<trace::EventTrackerInterface>& getTraceTracker(const EventEntry& entry) { + switch (entry.type) { + case EventEntry::Type::MOTION: { + const auto& motion = static_cast<const MotionEntry&>(entry); + ensureEventTraced(motion); + return motion.traceTracker; + } + case EventEntry::Type::KEY: { + const auto& key = static_cast<const KeyEntry&>(entry); + ensureEventTraced(key); + return key.traceTracker; + } + default: { + const static std::unique_ptr<trace::EventTrackerInterface> kNullTracker; + return kNullTracker; + } + } +} + // Temporarily releases a held mutex for the lifetime of the instance. // Named to match std::scoped_lock class scoped_unlock { @@ -1147,10 +1167,6 @@ void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) { dropReason = DropReason::BLOCKED; } done = dispatchKeyLocked(currentTime, keyEntry, &dropReason, nextWakeupTime); - if (done && mTracer) { - ensureEventTraced(*keyEntry); - mTracer->eventProcessingComplete(*keyEntry->traceTracker); - } break; } @@ -1176,10 +1192,6 @@ void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) { } } done = dispatchMotionLocked(currentTime, motionEntry, &dropReason, nextWakeupTime); - if (done && mTracer) { - ensureEventTraced(*motionEntry); - mTracer->eventProcessingComplete(*motionEntry->traceTracker); - } break; } @@ -1205,6 +1217,12 @@ void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) { } mLastDropReason = dropReason; + if (mTracer) { + if (auto& traceTracker = getTraceTracker(*mPendingEvent); traceTracker != nullptr) { + mTracer->eventProcessingComplete(*traceTracker); + } + } + releasePendingEventLocked(); nextWakeupTime = LLONG_MIN; // force next poll to wake up immediately } diff --git a/services/inputflinger/dispatcher/trace/InputTracer.cpp b/services/inputflinger/dispatcher/trace/InputTracer.cpp index be09013a83..0be64e67ca 100644 --- a/services/inputflinger/dispatcher/trace/InputTracer.cpp +++ b/services/inputflinger/dispatcher/trace/InputTracer.cpp @@ -84,24 +84,24 @@ std::unique_ptr<EventTrackerInterface> InputTracer::traceInboundEvent(const Even void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie, const InputTarget& target) { - auto& cookieState = getState(cookie); - if (!cookieState) { + auto& eventState = getState(cookie); + if (eventState.isEventProcessingComplete) { LOG(FATAL) << "dispatchToTargetHint() should not be called after eventProcessingComplete()"; } // TODO(b/210460522): Determine if the event is sensitive based on the target. } void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) { - auto& cookieState = getState(cookie); - if (!cookieState) { + auto& eventState = getState(cookie); + if (eventState.isEventProcessingComplete) { LOG(FATAL) << "Traced event was already logged. " "eventProcessingComplete() was likely called more than once."; } std::visit(Visitor{[&](const TracedMotionEvent& e) { mBackend->traceMotionEvent(e); }, [&](const TracedKeyEvent& e) { mBackend->traceKeyEvent(e); }}, - cookieState->event); - cookieState.reset(); + eventState.event); + eventState.isEventProcessingComplete = true; } void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry, @@ -136,7 +136,7 @@ void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry, /*hmac=*/{}}); } -std::optional<InputTracer::EventState>& InputTracer::getState(const EventTrackerInterface& cookie) { +InputTracer::EventState& InputTracer::getState(const EventTrackerInterface& cookie) { return static_cast<const EventTrackerImpl&>(cookie).mState; } @@ -146,18 +146,17 @@ InputTracer::EventTrackerImpl::EventTrackerImpl(InputTracer& tracer, TracedEvent : mTracer(tracer), mState(event) {} InputTracer::EventTrackerImpl::~EventTrackerImpl() { - if (!mState) { + if (mState.isEventProcessingComplete) { // This event has already been written to the trace as expected. return; } - // We're still holding on to the state, which means it hasn't yet been written to the trace. - // Write it to the trace now. + // The event processing was never marked as complete, so do it now. // TODO(b/210460522): Determine why/where the event is being destroyed before // eventProcessingComplete() is called. std::visit(Visitor{[&](const TracedMotionEvent& e) { mTracer.mBackend->traceMotionEvent(e); }, [&](const TracedKeyEvent& e) { mTracer.mBackend->traceKeyEvent(e); }}, - mState->event); - mState.reset(); + mState.event); + mState.isEventProcessingComplete = true; } } // namespace android::inputdispatcher::trace::impl diff --git a/services/inputflinger/dispatcher/trace/InputTracer.h b/services/inputflinger/dispatcher/trace/InputTracer.h index c8b25c9961..1acac6df20 100644 --- a/services/inputflinger/dispatcher/trace/InputTracer.h +++ b/services/inputflinger/dispatcher/trace/InputTracer.h @@ -51,13 +51,16 @@ private: // The state of a tracked event. struct EventState { + explicit inline EventState(TracedEvent event) : event(std::move(event)){}; + const TracedEvent event; + bool isEventProcessingComplete{false}; // TODO(b/210460522): Add additional args for tracking event sensitivity and // dispatch target UIDs. }; // Get the event state associated with a tracking cookie. - std::optional<EventState>& getState(const EventTrackerInterface&); + EventState& getState(const EventTrackerInterface&); // Implementation of the event tracker cookie. The cookie holds the event state directly for // convenience to avoid the overhead of tracking the state separately in InputTracer. @@ -68,11 +71,9 @@ private: private: InputTracer& mTracer; - // This event tracker cookie will only hold the state as long as it has not been written - // to the trace. The state is released when the event is written to the trace. - mutable std::optional<EventState> mState; + mutable EventState mState; - friend std::optional<EventState>& InputTracer::getState(const EventTrackerInterface&); + friend EventState& InputTracer::getState(const EventTrackerInterface&); }; }; |