blob: 47e27beff73f09143afe131be43fdf25e7d22aab [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 Pradhan8c3b1432024-02-09 23:34:16 +000062void writeEventToBackend(const TracedEvent& event, const TracedEventArgs args,
63 InputTracingBackendInterface& backend) {
64 std::visit(Visitor{[&](const TracedMotionEvent& e) { backend.traceMotionEvent(e, args); },
65 [&](const TracedKeyEvent& e) { backend.traceKeyEvent(e, args); }},
Prabir Pradhana67623c2024-02-21 06:57:36 +000066 event);
67}
68
Prabir Pradhand6b2b052024-02-21 23:25:15 +000069inline auto getId(const trace::TracedEvent& v) {
70 return std::visit([](const auto& event) { return event.id; }, v);
71}
72
Prabir Pradhandae52792023-12-15 07:36:40 +000073} // namespace
74
75// --- InputTracer ---
76
77InputTracer::InputTracer(std::unique_ptr<InputTracingBackendInterface> backend)
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000078 : mBackend(std::move(backend)) {}
Prabir Pradhandae52792023-12-15 07:36:40 +000079
80std::unique_ptr<EventTrackerInterface> InputTracer::traceInboundEvent(const EventEntry& entry) {
Prabir Pradhana67623c2024-02-21 06:57:36 +000081 // This is a newly traced inbound event. Create a new state to track it and its derived events.
82 auto eventState = std::make_shared<EventState>(*this);
Prabir Pradhandae52792023-12-15 07:36:40 +000083
84 if (entry.type == EventEntry::Type::MOTION) {
85 const auto& motion = static_cast<const MotionEntry&>(entry);
Prabir Pradhana67623c2024-02-21 06:57:36 +000086 eventState->events.emplace_back(createTracedEvent(motion));
Prabir Pradhandae52792023-12-15 07:36:40 +000087 } else if (entry.type == EventEntry::Type::KEY) {
88 const auto& key = static_cast<const KeyEntry&>(entry);
Prabir Pradhana67623c2024-02-21 06:57:36 +000089 eventState->events.emplace_back(createTracedEvent(key));
Prabir Pradhandae52792023-12-15 07:36:40 +000090 } else {
91 LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
92 }
93
Prabir Pradhana67623c2024-02-21 06:57:36 +000094 return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/false);
Prabir Pradhandae52792023-12-15 07:36:40 +000095}
96
Prabir Pradhand6b2b052024-02-21 23:25:15 +000097std::unique_ptr<EventTrackerInterface> InputTracer::createTrackerForSyntheticEvent() {
98 // Create a new EventState to track events derived from this tracker.
99 return std::make_unique<EventTrackerImpl>(std::make_shared<EventState>(*this),
100 /*isDerived=*/false);
101}
102
Prabir Pradhandae52792023-12-15 07:36:40 +0000103void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie,
104 const InputTarget& target) {
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000105 auto& eventState = getState(cookie);
Prabir Pradhana67623c2024-02-21 06:57:36 +0000106 if (eventState->isEventProcessingComplete) {
107 // TODO(b/210460522): Disallow adding new targets after eventProcessingComplete() is called.
108 return;
Prabir Pradhandae52792023-12-15 07:36:40 +0000109 }
Prabir Pradhanb95d4aa2024-03-11 22:48:04 +0000110 if (isDerivedCookie(cookie)) {
111 // TODO(b/210460522): Disallow adding new targets from a derived cookie.
112 return;
113 }
Prabir Pradhan8c3b1432024-02-09 23:34:16 +0000114 if (target.windowHandle != nullptr) {
115 eventState->isSecure |= target.windowHandle->getInfo()->layoutParamsFlags.test(
116 gui::WindowInfo::Flag::SECURE);
117 // TODO(b/210460522): Set events as sensitive when the IME connection is active.
118 }
Prabir Pradhandae52792023-12-15 07:36:40 +0000119}
120
121void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) {
Prabir Pradhana67623c2024-02-21 06:57:36 +0000122 if (isDerivedCookie(cookie)) {
123 LOG(FATAL) << "Event processing cannot be set from a derived cookie.";
124 }
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000125 auto& eventState = getState(cookie);
Prabir Pradhana67623c2024-02-21 06:57:36 +0000126 if (eventState->isEventProcessingComplete) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000127 LOG(FATAL) << "Traced event was already logged. "
128 "eventProcessingComplete() was likely called more than once.";
129 }
Prabir Pradhane7701642024-02-22 04:06:53 +0000130 eventState->onEventProcessingComplete();
Prabir Pradhana67623c2024-02-21 06:57:36 +0000131}
132
133std::unique_ptr<EventTrackerInterface> InputTracer::traceDerivedEvent(
134 const EventEntry& entry, const EventTrackerInterface& originalEventCookie) {
135 // This is an event derived from an already-established event. Use the same state to track
136 // this event too.
137 auto eventState = getState(originalEventCookie);
138
139 if (entry.type == EventEntry::Type::MOTION) {
140 const auto& motion = static_cast<const MotionEntry&>(entry);
141 eventState->events.emplace_back(createTracedEvent(motion));
142 } else if (entry.type == EventEntry::Type::KEY) {
143 const auto& key = static_cast<const KeyEntry&>(entry);
144 eventState->events.emplace_back(createTracedEvent(key));
145 } else {
146 LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
147 }
148
149 if (eventState->isEventProcessingComplete) {
150 // It is possible for a derived event to be dispatched some time after the original event
151 // is dispatched, such as in the case of key fallback events. To account for these cases,
152 // derived events can be traced after the processing is complete for the original event.
Prabir Pradhan8c3b1432024-02-09 23:34:16 +0000153 const TracedEventArgs traceArgs{.isSecure = eventState->isSecure};
154 writeEventToBackend(eventState->events.back(), traceArgs, *mBackend);
Prabir Pradhana67623c2024-02-21 06:57:36 +0000155 }
156 return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/true);
Prabir Pradhandae52792023-12-15 07:36:40 +0000157}
158
159void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry,
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000160 const EventTrackerInterface& cookie) {
161 auto& eventState = getState(cookie);
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000162 const EventEntry& entry = *dispatchEntry.eventEntry;
Prabir Pradhan560d0d12024-03-07 18:08:27 +0000163 // TODO(b/328618922): Remove resolved key repeats after making repeatCount non-mutable.
164 // The KeyEntry's repeatCount is mutable and can be modified after an event is initially traced,
165 // so we need to find the repeatCount at the time of dispatching to trace it accurately.
166 int32_t resolvedKeyRepeatCount = 0;
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000167
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000168 TracedEvent traced;
169 if (entry.type == EventEntry::Type::MOTION) {
170 const auto& motion = static_cast<const MotionEntry&>(entry);
171 traced = createTracedEvent(motion);
172 } else if (entry.type == EventEntry::Type::KEY) {
173 const auto& key = static_cast<const KeyEntry&>(entry);
Prabir Pradhan560d0d12024-03-07 18:08:27 +0000174 resolvedKeyRepeatCount = key.repeatCount;
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000175 traced = createTracedEvent(key);
176 } else {
177 LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
178 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000179
Prabir Pradhand6b2b052024-02-21 23:25:15 +0000180 auto tracedEventIt =
181 std::find_if(eventState->events.begin(), eventState->events.end(),
182 [&traced](const auto& event) { return getId(traced) == getId(event); });
183 if (tracedEventIt == eventState->events.end()) {
184 LOG(FATAL)
185 << __func__
186 << ": Failed to find a previously traced event that matches the dispatched event";
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000187 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000188
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000189 // The vsyncId only has meaning if the event is targeting a window.
190 const int32_t windowId = dispatchEntry.windowId.value_or(0);
191 const int32_t vsyncId = dispatchEntry.windowId.has_value() ? dispatchEntry.vsyncId : 0;
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000192
Prabir Pradhan8c3b1432024-02-09 23:34:16 +0000193 // TODO(b/210460522): Pass HMAC into traceEventDispatch.
Prabir Pradhan52ec3ff2024-02-22 03:26:38 +0000194 const WindowDispatchArgs windowDispatchArgs{std::move(traced),
195 dispatchEntry.deliveryTime,
196 dispatchEntry.resolvedFlags,
197 dispatchEntry.targetUid,
198 vsyncId,
199 windowId,
200 dispatchEntry.transform,
201 dispatchEntry.rawTransform,
202 /*hmac=*/{},
203 resolvedKeyRepeatCount};
204 if (eventState->isEventProcessingComplete) {
Prabir Pradhan8c3b1432024-02-09 23:34:16 +0000205 mBackend->traceWindowDispatch(std::move(windowDispatchArgs),
206 TracedEventArgs{.isSecure = eventState->isSecure});
Prabir Pradhan52ec3ff2024-02-22 03:26:38 +0000207 } else {
208 eventState->pendingDispatchArgs.emplace_back(std::move(windowDispatchArgs));
209 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000210}
Prabir Pradhandae52792023-12-15 07:36:40 +0000211
Prabir Pradhana67623c2024-02-21 06:57:36 +0000212std::shared_ptr<InputTracer::EventState>& InputTracer::getState(
213 const EventTrackerInterface& cookie) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000214 return static_cast<const EventTrackerImpl&>(cookie).mState;
Prabir Pradhandae52792023-12-15 07:36:40 +0000215}
216
Prabir Pradhana67623c2024-02-21 06:57:36 +0000217bool InputTracer::isDerivedCookie(const EventTrackerInterface& cookie) {
218 return static_cast<const EventTrackerImpl&>(cookie).mIsDerived;
219}
Prabir Pradhandae52792023-12-15 07:36:40 +0000220
Prabir Pradhana67623c2024-02-21 06:57:36 +0000221// --- InputTracer::EventState ---
Prabir Pradhandae52792023-12-15 07:36:40 +0000222
Prabir Pradhane7701642024-02-22 04:06:53 +0000223void InputTracer::EventState::onEventProcessingComplete() {
224 // Write all of the events known so far to the trace.
Prabir Pradhan8c3b1432024-02-09 23:34:16 +0000225 const TracedEventArgs traceArgs{.isSecure = isSecure};
Prabir Pradhane7701642024-02-22 04:06:53 +0000226 for (const auto& event : events) {
Prabir Pradhan8c3b1432024-02-09 23:34:16 +0000227 writeEventToBackend(event, traceArgs, *tracer.mBackend);
Prabir Pradhane7701642024-02-22 04:06:53 +0000228 }
Prabir Pradhan52ec3ff2024-02-22 03:26:38 +0000229 // Write all pending dispatch args to the trace.
230 for (const auto& windowDispatchArgs : pendingDispatchArgs) {
Prabir Pradhan8c3b1432024-02-09 23:34:16 +0000231 tracer.mBackend->traceWindowDispatch(windowDispatchArgs, traceArgs);
Prabir Pradhan52ec3ff2024-02-22 03:26:38 +0000232 }
233 pendingDispatchArgs.clear();
234
Prabir Pradhane7701642024-02-22 04:06:53 +0000235 isEventProcessingComplete = true;
236}
237
Prabir Pradhana67623c2024-02-21 06:57:36 +0000238InputTracer::EventState::~EventState() {
239 if (isEventProcessingComplete) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000240 // This event has already been written to the trace as expected.
241 return;
242 }
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000243 // The event processing was never marked as complete, so do it now.
Prabir Pradhane7701642024-02-22 04:06:53 +0000244 // We should never end up here in normal operation. However, in tests, it's possible that we
245 // stop and destroy InputDispatcher without waiting for it to finish processing events, at
246 // which point an event (and thus its EventState) may be destroyed before processing finishes.
247 onEventProcessingComplete();
Prabir Pradhandae52792023-12-15 07:36:40 +0000248}
249
250} // namespace android::inputdispatcher::trace::impl