blob: 4cf6a89db6d922d79c49ebd6003184c3b97991e6 [file] [log] [blame]
Prabir Pradhandae52792023-12-15 07:36:40 +00001/*
2 * Copyright 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "InputTracer"
18
19#include "InputTracer.h"
20
21#include <android-base/logging.h>
22
23namespace android::inputdispatcher::trace::impl {
24
25namespace {
26
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000027// Helper to std::visit with lambdas.
28template <typename... V>
29struct Visitor : V... {
30 using V::operator()...;
31};
32
Prabir Pradhandae52792023-12-15 07:36:40 +000033TracedEvent createTracedEvent(const MotionEntry& e) {
34 return TracedMotionEvent{e.id,
35 e.eventTime,
36 e.policyFlags,
37 e.deviceId,
38 e.source,
39 e.displayId,
40 e.action,
41 e.actionButton,
42 e.flags,
43 e.metaState,
44 e.buttonState,
45 e.classification,
46 e.edgeFlags,
47 e.xPrecision,
48 e.yPrecision,
49 e.xCursorPosition,
50 e.yCursorPosition,
51 e.downTime,
52 e.pointerProperties,
53 e.pointerCoords};
54}
55
56TracedEvent createTracedEvent(const KeyEntry& e) {
57 return TracedKeyEvent{e.id, e.eventTime, e.policyFlags, e.deviceId, e.source,
58 e.displayId, e.action, e.keyCode, e.scanCode, e.metaState,
59 e.downTime, e.flags, e.repeatCount};
60}
61
Prabir Pradhana67623c2024-02-21 06:57:36 +000062void writeEventToBackend(const TracedEvent& event, InputTracingBackendInterface& backend) {
63 std::visit(Visitor{[&](const TracedMotionEvent& e) { backend.traceMotionEvent(e); },
64 [&](const TracedKeyEvent& e) { backend.traceKeyEvent(e); }},
65 event);
66}
67
Prabir Pradhand6b2b052024-02-21 23:25:15 +000068inline auto getId(const trace::TracedEvent& v) {
69 return std::visit([](const auto& event) { return event.id; }, v);
70}
71
Prabir Pradhandae52792023-12-15 07:36:40 +000072} // namespace
73
74// --- InputTracer ---
75
76InputTracer::InputTracer(std::unique_ptr<InputTracingBackendInterface> backend)
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000077 : mBackend(std::move(backend)) {}
Prabir Pradhandae52792023-12-15 07:36:40 +000078
79std::unique_ptr<EventTrackerInterface> InputTracer::traceInboundEvent(const EventEntry& entry) {
Prabir Pradhana67623c2024-02-21 06:57:36 +000080 // This is a newly traced inbound event. Create a new state to track it and its derived events.
81 auto eventState = std::make_shared<EventState>(*this);
Prabir Pradhandae52792023-12-15 07:36:40 +000082
83 if (entry.type == EventEntry::Type::MOTION) {
84 const auto& motion = static_cast<const MotionEntry&>(entry);
Prabir Pradhana67623c2024-02-21 06:57:36 +000085 eventState->events.emplace_back(createTracedEvent(motion));
Prabir Pradhandae52792023-12-15 07:36:40 +000086 } else if (entry.type == EventEntry::Type::KEY) {
87 const auto& key = static_cast<const KeyEntry&>(entry);
Prabir Pradhana67623c2024-02-21 06:57:36 +000088 eventState->events.emplace_back(createTracedEvent(key));
Prabir Pradhandae52792023-12-15 07:36:40 +000089 } else {
90 LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
91 }
92
Prabir Pradhana67623c2024-02-21 06:57:36 +000093 return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/false);
Prabir Pradhandae52792023-12-15 07:36:40 +000094}
95
Prabir Pradhand6b2b052024-02-21 23:25:15 +000096std::unique_ptr<EventTrackerInterface> InputTracer::createTrackerForSyntheticEvent() {
97 // Create a new EventState to track events derived from this tracker.
98 return std::make_unique<EventTrackerImpl>(std::make_shared<EventState>(*this),
99 /*isDerived=*/false);
100}
101
Prabir Pradhandae52792023-12-15 07:36:40 +0000102void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie,
103 const InputTarget& target) {
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000104 auto& eventState = getState(cookie);
Prabir Pradhana67623c2024-02-21 06:57:36 +0000105 if (eventState->isEventProcessingComplete) {
106 // TODO(b/210460522): Disallow adding new targets after eventProcessingComplete() is called.
107 return;
Prabir Pradhandae52792023-12-15 07:36:40 +0000108 }
Prabir Pradhanb95d4aa2024-03-11 22:48:04 +0000109 if (isDerivedCookie(cookie)) {
110 // TODO(b/210460522): Disallow adding new targets from a derived cookie.
111 return;
112 }
Prabir Pradhandae52792023-12-15 07:36:40 +0000113 // TODO(b/210460522): Determine if the event is sensitive based on the target.
114}
115
116void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) {
Prabir Pradhana67623c2024-02-21 06:57:36 +0000117 if (isDerivedCookie(cookie)) {
118 LOG(FATAL) << "Event processing cannot be set from a derived cookie.";
119 }
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000120 auto& eventState = getState(cookie);
Prabir Pradhana67623c2024-02-21 06:57:36 +0000121 if (eventState->isEventProcessingComplete) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000122 LOG(FATAL) << "Traced event was already logged. "
123 "eventProcessingComplete() was likely called more than once.";
124 }
Prabir Pradhane7701642024-02-22 04:06:53 +0000125 eventState->onEventProcessingComplete();
Prabir Pradhana67623c2024-02-21 06:57:36 +0000126}
127
128std::unique_ptr<EventTrackerInterface> InputTracer::traceDerivedEvent(
129 const EventEntry& entry, const EventTrackerInterface& originalEventCookie) {
130 // This is an event derived from an already-established event. Use the same state to track
131 // this event too.
132 auto eventState = getState(originalEventCookie);
133
134 if (entry.type == EventEntry::Type::MOTION) {
135 const auto& motion = static_cast<const MotionEntry&>(entry);
136 eventState->events.emplace_back(createTracedEvent(motion));
137 } else if (entry.type == EventEntry::Type::KEY) {
138 const auto& key = static_cast<const KeyEntry&>(entry);
139 eventState->events.emplace_back(createTracedEvent(key));
140 } else {
141 LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
142 }
143
144 if (eventState->isEventProcessingComplete) {
145 // It is possible for a derived event to be dispatched some time after the original event
146 // is dispatched, such as in the case of key fallback events. To account for these cases,
147 // derived events can be traced after the processing is complete for the original event.
148 writeEventToBackend(eventState->events.back(), *mBackend);
149 }
150 return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/true);
Prabir Pradhandae52792023-12-15 07:36:40 +0000151}
152
153void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry,
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000154 const EventTrackerInterface& cookie) {
155 auto& eventState = getState(cookie);
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000156 const EventEntry& entry = *dispatchEntry.eventEntry;
Prabir Pradhan560d0d12024-03-07 18:08:27 +0000157 // TODO(b/328618922): Remove resolved key repeats after making repeatCount non-mutable.
158 // The KeyEntry's repeatCount is mutable and can be modified after an event is initially traced,
159 // so we need to find the repeatCount at the time of dispatching to trace it accurately.
160 int32_t resolvedKeyRepeatCount = 0;
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000161
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000162 TracedEvent traced;
163 if (entry.type == EventEntry::Type::MOTION) {
164 const auto& motion = static_cast<const MotionEntry&>(entry);
165 traced = createTracedEvent(motion);
166 } else if (entry.type == EventEntry::Type::KEY) {
167 const auto& key = static_cast<const KeyEntry&>(entry);
Prabir Pradhan560d0d12024-03-07 18:08:27 +0000168 resolvedKeyRepeatCount = key.repeatCount;
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000169 traced = createTracedEvent(key);
170 } else {
171 LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
172 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000173
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000174 auto tracedEventIt =
175 std::find_if(eventState->events.begin(), eventState->events.end(),
176 [&traced](const auto& event) { return getId(traced) == getId(event); });
177 if (tracedEventIt == eventState->events.end()) {
178 LOG(FATAL)
179 << __func__
180 << ": Failed to find a previously traced event that matches the dispatched event";
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000181 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000182
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000183 // The vsyncId only has meaning if the event is targeting a window.
184 const int32_t windowId = dispatchEntry.windowId.value_or(0);
185 const int32_t vsyncId = dispatchEntry.windowId.has_value() ? dispatchEntry.vsyncId : 0;
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000186
Prabir Pradhan52ec3ff2024-02-22 03:26:38 +0000187 const WindowDispatchArgs windowDispatchArgs{std::move(traced),
188 dispatchEntry.deliveryTime,
189 dispatchEntry.resolvedFlags,
190 dispatchEntry.targetUid,
191 vsyncId,
192 windowId,
193 dispatchEntry.transform,
194 dispatchEntry.rawTransform,
195 /*hmac=*/{},
196 resolvedKeyRepeatCount};
197 if (eventState->isEventProcessingComplete) {
198 mBackend->traceWindowDispatch(std::move(windowDispatchArgs));
199 } else {
200 eventState->pendingDispatchArgs.emplace_back(std::move(windowDispatchArgs));
201 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000202}
Prabir Pradhandae52792023-12-15 07:36:40 +0000203
Prabir Pradhana67623c2024-02-21 06:57:36 +0000204std::shared_ptr<InputTracer::EventState>& InputTracer::getState(
205 const EventTrackerInterface& cookie) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000206 return static_cast<const EventTrackerImpl&>(cookie).mState;
Prabir Pradhandae52792023-12-15 07:36:40 +0000207}
208
Prabir Pradhana67623c2024-02-21 06:57:36 +0000209bool InputTracer::isDerivedCookie(const EventTrackerInterface& cookie) {
210 return static_cast<const EventTrackerImpl&>(cookie).mIsDerived;
211}
Prabir Pradhandae52792023-12-15 07:36:40 +0000212
Prabir Pradhana67623c2024-02-21 06:57:36 +0000213// --- InputTracer::EventState ---
Prabir Pradhandae52792023-12-15 07:36:40 +0000214
Prabir Pradhane7701642024-02-22 04:06:53 +0000215void InputTracer::EventState::onEventProcessingComplete() {
216 // Write all of the events known so far to the trace.
217 for (const auto& event : events) {
218 writeEventToBackend(event, *tracer.mBackend);
219 }
Prabir Pradhan52ec3ff2024-02-22 03:26:38 +0000220 // Write all pending dispatch args to the trace.
221 for (const auto& windowDispatchArgs : pendingDispatchArgs) {
222 tracer.mBackend->traceWindowDispatch(windowDispatchArgs);
223 }
224 pendingDispatchArgs.clear();
225
Prabir Pradhane7701642024-02-22 04:06:53 +0000226 isEventProcessingComplete = true;
227}
228
Prabir Pradhana67623c2024-02-21 06:57:36 +0000229InputTracer::EventState::~EventState() {
230 if (isEventProcessingComplete) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000231 // This event has already been written to the trace as expected.
232 return;
233 }
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000234 // The event processing was never marked as complete, so do it now.
Prabir Pradhane7701642024-02-22 04:06:53 +0000235 // We should never end up here in normal operation. However, in tests, it's possible that we
236 // stop and destroy InputDispatcher without waiting for it to finish processing events, at
237 // which point an event (and thus its EventState) may be destroyed before processing finishes.
238 onEventProcessingComplete();
Prabir Pradhandae52792023-12-15 07:36:40 +0000239}
240
241} // namespace android::inputdispatcher::trace::impl