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 | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 68 | } // namespace |
| 69 | |
| 70 | // --- InputTracer --- |
| 71 | |
| 72 | InputTracer::InputTracer(std::unique_ptr<InputTracingBackendInterface> backend) |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 73 | : mBackend(std::move(backend)) {} |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 74 | |
| 75 | std::unique_ptr<EventTrackerInterface> InputTracer::traceInboundEvent(const EventEntry& entry) { |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 76 | // This is a newly traced inbound event. Create a new state to track it and its derived events. |
| 77 | auto eventState = std::make_shared<EventState>(*this); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 78 | |
| 79 | if (entry.type == EventEntry::Type::MOTION) { |
| 80 | const auto& motion = static_cast<const MotionEntry&>(entry); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 81 | eventState->events.emplace_back(createTracedEvent(motion)); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 82 | } else if (entry.type == EventEntry::Type::KEY) { |
| 83 | const auto& key = static_cast<const KeyEntry&>(entry); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 84 | eventState->events.emplace_back(createTracedEvent(key)); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 85 | } else { |
| 86 | LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type); |
| 87 | } |
| 88 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 89 | return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/false); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie, |
| 93 | const InputTarget& target) { |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 94 | if (isDerivedCookie(cookie)) { |
| 95 | LOG(FATAL) << "Event target cannot be updated from a derived cookie."; |
| 96 | } |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 97 | auto& eventState = getState(cookie); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 98 | if (eventState->isEventProcessingComplete) { |
| 99 | // TODO(b/210460522): Disallow adding new targets after eventProcessingComplete() is called. |
| 100 | return; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 101 | } |
| 102 | // TODO(b/210460522): Determine if the event is sensitive based on the target. |
| 103 | } |
| 104 | |
| 105 | void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) { |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 106 | if (isDerivedCookie(cookie)) { |
| 107 | LOG(FATAL) << "Event processing cannot be set from a derived cookie."; |
| 108 | } |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 109 | auto& eventState = getState(cookie); |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 110 | if (eventState->isEventProcessingComplete) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 111 | LOG(FATAL) << "Traced event was already logged. " |
| 112 | "eventProcessingComplete() was likely called more than once."; |
| 113 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 114 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 115 | for (const auto& event : eventState->events) { |
| 116 | writeEventToBackend(event, *mBackend); |
| 117 | } |
| 118 | eventState->isEventProcessingComplete = true; |
| 119 | } |
| 120 | |
| 121 | std::unique_ptr<EventTrackerInterface> InputTracer::traceDerivedEvent( |
| 122 | const EventEntry& entry, const EventTrackerInterface& originalEventCookie) { |
| 123 | // This is an event derived from an already-established event. Use the same state to track |
| 124 | // this event too. |
| 125 | auto eventState = getState(originalEventCookie); |
| 126 | |
| 127 | if (entry.type == EventEntry::Type::MOTION) { |
| 128 | const auto& motion = static_cast<const MotionEntry&>(entry); |
| 129 | eventState->events.emplace_back(createTracedEvent(motion)); |
| 130 | } else if (entry.type == EventEntry::Type::KEY) { |
| 131 | const auto& key = static_cast<const KeyEntry&>(entry); |
| 132 | eventState->events.emplace_back(createTracedEvent(key)); |
| 133 | } else { |
| 134 | LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type); |
| 135 | } |
| 136 | |
| 137 | if (eventState->isEventProcessingComplete) { |
| 138 | // It is possible for a derived event to be dispatched some time after the original event |
| 139 | // is dispatched, such as in the case of key fallback events. To account for these cases, |
| 140 | // derived events can be traced after the processing is complete for the original event. |
| 141 | writeEventToBackend(eventState->events.back(), *mBackend); |
| 142 | } |
| 143 | return std::make_unique<EventTrackerImpl>(std::move(eventState), /*isDerived=*/true); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 147 | const EventTrackerInterface* cookie) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 148 | const EventEntry& entry = *dispatchEntry.eventEntry; |
Prabir Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame] | 149 | // TODO(b/328618922): Remove resolved key repeats after making repeatCount non-mutable. |
| 150 | // The KeyEntry's repeatCount is mutable and can be modified after an event is initially traced, |
| 151 | // so we need to find the repeatCount at the time of dispatching to trace it accurately. |
| 152 | int32_t resolvedKeyRepeatCount = 0; |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 153 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 154 | TracedEvent traced; |
| 155 | if (entry.type == EventEntry::Type::MOTION) { |
| 156 | const auto& motion = static_cast<const MotionEntry&>(entry); |
| 157 | traced = createTracedEvent(motion); |
| 158 | } else if (entry.type == EventEntry::Type::KEY) { |
| 159 | const auto& key = static_cast<const KeyEntry&>(entry); |
Prabir Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame] | 160 | resolvedKeyRepeatCount = key.repeatCount; |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 161 | traced = createTracedEvent(key); |
| 162 | } else { |
| 163 | LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type); |
| 164 | } |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 165 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 166 | if (!cookie) { |
| 167 | // This event was not tracked as an inbound event, so trace it now. |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 168 | writeEventToBackend(traced, *mBackend); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 169 | } |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 170 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 171 | // The vsyncId only has meaning if the event is targeting a window. |
| 172 | const int32_t windowId = dispatchEntry.windowId.value_or(0); |
| 173 | const int32_t vsyncId = dispatchEntry.windowId.has_value() ? dispatchEntry.vsyncId : 0; |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 174 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 175 | mBackend->traceWindowDispatch({std::move(traced), dispatchEntry.deliveryTime, |
| 176 | dispatchEntry.resolvedFlags, dispatchEntry.targetUid, vsyncId, |
| 177 | windowId, dispatchEntry.transform, dispatchEntry.rawTransform, |
Prabir Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame] | 178 | /*hmac=*/{}, resolvedKeyRepeatCount}); |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 179 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 180 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 181 | std::shared_ptr<InputTracer::EventState>& InputTracer::getState( |
| 182 | const EventTrackerInterface& cookie) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 183 | return static_cast<const EventTrackerImpl&>(cookie).mState; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 186 | bool InputTracer::isDerivedCookie(const EventTrackerInterface& cookie) { |
| 187 | return static_cast<const EventTrackerImpl&>(cookie).mIsDerived; |
| 188 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 189 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 190 | // --- InputTracer::EventState --- |
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 | InputTracer::EventState::~EventState() { |
| 193 | if (isEventProcessingComplete) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 194 | // This event has already been written to the trace as expected. |
| 195 | return; |
| 196 | } |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 197 | // The event processing was never marked as complete, so do it now. |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 198 | // TODO(b/210460522): Determine why/where the event is being destroyed before |
| 199 | // eventProcessingComplete() is called. |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame^] | 200 | for (const auto& event : events) { |
| 201 | writeEventToBackend(event, *tracer.mBackend); |
| 202 | } |
| 203 | isEventProcessingComplete = true; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | } // namespace android::inputdispatcher::trace::impl |