blob: 49e6e21828c1fe4de207964b854bb0accae867a2 [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 Pradhana67623c2024-02-21 06:57:36 +0000104 if (isDerivedCookie(cookie)) {
105 LOG(FATAL) << "Event target cannot be updated from a derived cookie.";
106 }
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000107 auto& eventState = getState(cookie);
Prabir Pradhana67623c2024-02-21 06:57:36 +0000108 if (eventState->isEventProcessingComplete) {
109 // TODO(b/210460522): Disallow adding new targets after eventProcessingComplete() is called.
110 return;
Prabir Pradhandae52792023-12-15 07:36:40 +0000111 }
112 // TODO(b/210460522): Determine if the event is sensitive based on the target.
113}
114
115void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) {
Prabir Pradhana67623c2024-02-21 06:57:36 +0000116 if (isDerivedCookie(cookie)) {
117 LOG(FATAL) << "Event processing cannot be set from a derived cookie.";
118 }
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000119 auto& eventState = getState(cookie);
Prabir Pradhana67623c2024-02-21 06:57:36 +0000120 if (eventState->isEventProcessingComplete) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000121 LOG(FATAL) << "Traced event was already logged. "
122 "eventProcessingComplete() was likely called more than once.";
123 }
Prabir Pradhane7701642024-02-22 04:06:53 +0000124 eventState->onEventProcessingComplete();
Prabir Pradhana67623c2024-02-21 06:57:36 +0000125}
126
127std::unique_ptr<EventTrackerInterface> InputTracer::traceDerivedEvent(
128 const EventEntry& entry, const EventTrackerInterface& originalEventCookie) {
129 // This is an event derived from an already-established event. Use the same state to track
130 // this event too.
131 auto eventState = getState(originalEventCookie);
132
133 if (entry.type == EventEntry::Type::MOTION) {
134 const auto& motion = static_cast<const MotionEntry&>(entry);
135 eventState->events.emplace_back(createTracedEvent(motion));
136 } else if (entry.type == EventEntry::Type::KEY) {
137 const auto& key = static_cast<const KeyEntry&>(entry);
138 eventState->events.emplace_back(createTracedEvent(key));
139 } else {
140 LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
141 }
142
143 if (eventState->isEventProcessingComplete) {
144 // It is possible for a derived event to be dispatched some time after the original event
145 // is dispatched, such as in the case of key fallback events. To account for these cases,
146 // derived events can be traced after the processing is complete for the original event.
147 writeEventToBackend(eventState->events.back(), *mBackend);
148 }
149 return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/true);
Prabir Pradhandae52792023-12-15 07:36:40 +0000150}
151
152void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry,
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000153 const EventTrackerInterface& cookie) {
154 auto& eventState = getState(cookie);
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000155 const EventEntry& entry = *dispatchEntry.eventEntry;
Prabir Pradhan560d0d12024-03-07 18:08:27 +0000156 // TODO(b/328618922): Remove resolved key repeats after making repeatCount non-mutable.
157 // The KeyEntry's repeatCount is mutable and can be modified after an event is initially traced,
158 // so we need to find the repeatCount at the time of dispatching to trace it accurately.
159 int32_t resolvedKeyRepeatCount = 0;
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000160
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000161 TracedEvent traced;
162 if (entry.type == EventEntry::Type::MOTION) {
163 const auto& motion = static_cast<const MotionEntry&>(entry);
164 traced = createTracedEvent(motion);
165 } else if (entry.type == EventEntry::Type::KEY) {
166 const auto& key = static_cast<const KeyEntry&>(entry);
Prabir Pradhan560d0d12024-03-07 18:08:27 +0000167 resolvedKeyRepeatCount = key.repeatCount;
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000168 traced = createTracedEvent(key);
169 } else {
170 LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
171 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000172
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000173 auto tracedEventIt =
174 std::find_if(eventState->events.begin(), eventState->events.end(),
175 [&traced](const auto& event) { return getId(traced) == getId(event); });
176 if (tracedEventIt == eventState->events.end()) {
177 LOG(FATAL)
178 << __func__
179 << ": Failed to find a previously traced event that matches the dispatched event";
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000180 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000181
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000182 // The vsyncId only has meaning if the event is targeting a window.
183 const int32_t windowId = dispatchEntry.windowId.value_or(0);
184 const int32_t vsyncId = dispatchEntry.windowId.has_value() ? dispatchEntry.vsyncId : 0;
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000185
Prabir Pradhan52ec3ff2024-02-22 03:26:38 +0000186 const WindowDispatchArgs windowDispatchArgs{std::move(traced),
187 dispatchEntry.deliveryTime,
188 dispatchEntry.resolvedFlags,
189 dispatchEntry.targetUid,
190 vsyncId,
191 windowId,
192 dispatchEntry.transform,
193 dispatchEntry.rawTransform,
194 /*hmac=*/{},
195 resolvedKeyRepeatCount};
196 if (eventState->isEventProcessingComplete) {
197 mBackend->traceWindowDispatch(std::move(windowDispatchArgs));
198 } else {
199 eventState->pendingDispatchArgs.emplace_back(std::move(windowDispatchArgs));
200 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000201}
Prabir Pradhandae52792023-12-15 07:36:40 +0000202
Prabir Pradhana67623c2024-02-21 06:57:36 +0000203std::shared_ptr<InputTracer::EventState>& InputTracer::getState(
204 const EventTrackerInterface& cookie) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000205 return static_cast<const EventTrackerImpl&>(cookie).mState;
Prabir Pradhandae52792023-12-15 07:36:40 +0000206}
207
Prabir Pradhana67623c2024-02-21 06:57:36 +0000208bool InputTracer::isDerivedCookie(const EventTrackerInterface& cookie) {
209 return static_cast<const EventTrackerImpl&>(cookie).mIsDerived;
210}
Prabir Pradhandae52792023-12-15 07:36:40 +0000211
Prabir Pradhana67623c2024-02-21 06:57:36 +0000212// --- InputTracer::EventState ---
Prabir Pradhandae52792023-12-15 07:36:40 +0000213
Prabir Pradhane7701642024-02-22 04:06:53 +0000214void InputTracer::EventState::onEventProcessingComplete() {
215 // Write all of the events known so far to the trace.
216 for (const auto& event : events) {
217 writeEventToBackend(event, *tracer.mBackend);
218 }
Prabir Pradhan52ec3ff2024-02-22 03:26:38 +0000219 // Write all pending dispatch args to the trace.
220 for (const auto& windowDispatchArgs : pendingDispatchArgs) {
221 tracer.mBackend->traceWindowDispatch(windowDispatchArgs);
222 }
223 pendingDispatchArgs.clear();
224
Prabir Pradhane7701642024-02-22 04:06:53 +0000225 isEventProcessingComplete = true;
226}
227
Prabir Pradhana67623c2024-02-21 06:57:36 +0000228InputTracer::EventState::~EventState() {
229 if (isEventProcessingComplete) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000230 // This event has already been written to the trace as expected.
231 return;
232 }
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000233 // The event processing was never marked as complete, so do it now.
Prabir Pradhane7701642024-02-22 04:06:53 +0000234 // We should never end up here in normal operation. However, in tests, it's possible that we
235 // stop and destroy InputDispatcher without waiting for it to finish processing events, at
236 // which point an event (and thus its EventState) may be destroyed before processing finishes.
237 onEventProcessingComplete();
Prabir Pradhandae52792023-12-15 07:36:40 +0000238}
239
240} // namespace android::inputdispatcher::trace::impl