Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace android::inputdispatcher::trace::impl { |
| 24 | |
| 25 | namespace { |
| 26 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 27 | // Helper to std::visit with lambdas. |
| 28 | template <typename... V> |
| 29 | struct Visitor : V... { |
| 30 | using V::operator()...; |
| 31 | }; |
| 32 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 33 | TracedEvent 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 | |
| 56 | TracedEvent 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 Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 62 | void 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 Pradhan | d6b2b05 | 2024-02-21 23:25:15 +0000 | [diff] [blame^] | 68 | inline auto getId(const trace::TracedEvent& v) { |
| 69 | return std::visit([](const auto& event) { return event.id; }, v); |
| 70 | } |
| 71 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 72 | } // namespace |
| 73 | |
| 74 | // --- InputTracer --- |
| 75 | |
| 76 | InputTracer::InputTracer(std::unique_ptr<InputTracingBackendInterface> backend) |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 77 | : mBackend(std::move(backend)) {} |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 78 | |
| 79 | std::unique_ptr<EventTrackerInterface> InputTracer::traceInboundEvent(const EventEntry& entry) { |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 80 | // 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 Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 82 | |
| 83 | if (entry.type == EventEntry::Type::MOTION) { |
| 84 | const auto& motion = static_cast<const MotionEntry&>(entry); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 85 | eventState->events.emplace_back(createTracedEvent(motion)); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 86 | } else if (entry.type == EventEntry::Type::KEY) { |
| 87 | const auto& key = static_cast<const KeyEntry&>(entry); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 88 | eventState->events.emplace_back(createTracedEvent(key)); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 89 | } else { |
| 90 | LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type); |
| 91 | } |
| 92 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 93 | return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/false); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Prabir Pradhan | d6b2b05 | 2024-02-21 23:25:15 +0000 | [diff] [blame^] | 96 | std::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 Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 102 | void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie, |
| 103 | const InputTarget& target) { |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 104 | if (isDerivedCookie(cookie)) { |
| 105 | LOG(FATAL) << "Event target cannot be updated from a derived cookie."; |
| 106 | } |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 107 | auto& eventState = getState(cookie); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 108 | if (eventState->isEventProcessingComplete) { |
| 109 | // TODO(b/210460522): Disallow adding new targets after eventProcessingComplete() is called. |
| 110 | return; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 111 | } |
| 112 | // TODO(b/210460522): Determine if the event is sensitive based on the target. |
| 113 | } |
| 114 | |
| 115 | void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) { |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 116 | if (isDerivedCookie(cookie)) { |
| 117 | LOG(FATAL) << "Event processing cannot be set from a derived cookie."; |
| 118 | } |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 119 | auto& eventState = getState(cookie); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 120 | if (eventState->isEventProcessingComplete) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 121 | LOG(FATAL) << "Traced event was already logged. " |
| 122 | "eventProcessingComplete() was likely called more than once."; |
| 123 | } |
Prabir Pradhan | e770164 | 2024-02-22 04:06:53 +0000 | [diff] [blame] | 124 | eventState->onEventProcessingComplete(); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | std::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 Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry, |
Prabir Pradhan | d6b2b05 | 2024-02-21 23:25:15 +0000 | [diff] [blame^] | 153 | const EventTrackerInterface& cookie) { |
| 154 | auto& eventState = getState(cookie); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 155 | const EventEntry& entry = *dispatchEntry.eventEntry; |
Prabir Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame] | 156 | // 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 Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 160 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 161 | 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 Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame] | 167 | resolvedKeyRepeatCount = key.repeatCount; |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 168 | traced = createTracedEvent(key); |
| 169 | } else { |
| 170 | LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type); |
| 171 | } |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 172 | |
Prabir Pradhan | d6b2b05 | 2024-02-21 23:25:15 +0000 | [diff] [blame^] | 173 | 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 Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 180 | } |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 181 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 182 | // 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 Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 185 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 186 | mBackend->traceWindowDispatch({std::move(traced), dispatchEntry.deliveryTime, |
| 187 | dispatchEntry.resolvedFlags, dispatchEntry.targetUid, vsyncId, |
| 188 | windowId, dispatchEntry.transform, dispatchEntry.rawTransform, |
Prabir Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame] | 189 | /*hmac=*/{}, resolvedKeyRepeatCount}); |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 190 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 191 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 192 | std::shared_ptr<InputTracer::EventState>& InputTracer::getState( |
| 193 | const EventTrackerInterface& cookie) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 194 | return static_cast<const EventTrackerImpl&>(cookie).mState; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 197 | bool InputTracer::isDerivedCookie(const EventTrackerInterface& cookie) { |
| 198 | return static_cast<const EventTrackerImpl&>(cookie).mIsDerived; |
| 199 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 200 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 201 | // --- InputTracer::EventState --- |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 202 | |
Prabir Pradhan | e770164 | 2024-02-22 04:06:53 +0000 | [diff] [blame] | 203 | void InputTracer::EventState::onEventProcessingComplete() { |
| 204 | // Write all of the events known so far to the trace. |
| 205 | for (const auto& event : events) { |
| 206 | writeEventToBackend(event, *tracer.mBackend); |
| 207 | } |
| 208 | isEventProcessingComplete = true; |
| 209 | } |
| 210 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 211 | InputTracer::EventState::~EventState() { |
| 212 | if (isEventProcessingComplete) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 213 | // This event has already been written to the trace as expected. |
| 214 | return; |
| 215 | } |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 216 | // The event processing was never marked as complete, so do it now. |
Prabir Pradhan | e770164 | 2024-02-22 04:06:53 +0000 | [diff] [blame] | 217 | // We should never end up here in normal operation. However, in tests, it's possible that we |
| 218 | // stop and destroy InputDispatcher without waiting for it to finish processing events, at |
| 219 | // which point an event (and thus its EventState) may be destroyed before processing finishes. |
| 220 | onEventProcessingComplete(); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | } // namespace android::inputdispatcher::trace::impl |