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 | |
| 62 | } // namespace |
| 63 | |
| 64 | // --- InputTracer --- |
| 65 | |
| 66 | InputTracer::InputTracer(std::unique_ptr<InputTracingBackendInterface> backend) |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 67 | : mBackend(std::move(backend)) {} |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 68 | |
| 69 | std::unique_ptr<EventTrackerInterface> InputTracer::traceInboundEvent(const EventEntry& entry) { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 70 | TracedEvent traced; |
| 71 | |
| 72 | if (entry.type == EventEntry::Type::MOTION) { |
| 73 | const auto& motion = static_cast<const MotionEntry&>(entry); |
| 74 | traced = createTracedEvent(motion); |
| 75 | } else if (entry.type == EventEntry::Type::KEY) { |
| 76 | const auto& key = static_cast<const KeyEntry&>(entry); |
| 77 | traced = createTracedEvent(key); |
| 78 | } else { |
| 79 | LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type); |
| 80 | } |
| 81 | |
| 82 | return std::make_unique<EventTrackerImpl>(*this, std::move(traced)); |
| 83 | } |
| 84 | |
| 85 | void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie, |
| 86 | const InputTarget& target) { |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 87 | auto& eventState = getState(cookie); |
| 88 | if (eventState.isEventProcessingComplete) { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 89 | LOG(FATAL) << "dispatchToTargetHint() should not be called after eventProcessingComplete()"; |
| 90 | } |
| 91 | // TODO(b/210460522): Determine if the event is sensitive based on the target. |
| 92 | } |
| 93 | |
| 94 | void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) { |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 95 | auto& eventState = getState(cookie); |
| 96 | if (eventState.isEventProcessingComplete) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 97 | LOG(FATAL) << "Traced event was already logged. " |
| 98 | "eventProcessingComplete() was likely called more than once."; |
| 99 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 100 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 101 | std::visit(Visitor{[&](const TracedMotionEvent& e) { mBackend->traceMotionEvent(e); }, |
| 102 | [&](const TracedKeyEvent& e) { mBackend->traceKeyEvent(e); }}, |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 103 | eventState.event); |
| 104 | eventState.isEventProcessingComplete = true; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry, |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 108 | const EventTrackerInterface* cookie) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 109 | const EventEntry& entry = *dispatchEntry.eventEntry; |
Prabir Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame^] | 110 | // TODO(b/328618922): Remove resolved key repeats after making repeatCount non-mutable. |
| 111 | // The KeyEntry's repeatCount is mutable and can be modified after an event is initially traced, |
| 112 | // so we need to find the repeatCount at the time of dispatching to trace it accurately. |
| 113 | int32_t resolvedKeyRepeatCount = 0; |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 114 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 115 | TracedEvent traced; |
| 116 | if (entry.type == EventEntry::Type::MOTION) { |
| 117 | const auto& motion = static_cast<const MotionEntry&>(entry); |
| 118 | traced = createTracedEvent(motion); |
| 119 | } else if (entry.type == EventEntry::Type::KEY) { |
| 120 | const auto& key = static_cast<const KeyEntry&>(entry); |
Prabir Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame^] | 121 | resolvedKeyRepeatCount = key.repeatCount; |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 122 | traced = createTracedEvent(key); |
| 123 | } else { |
| 124 | LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type); |
| 125 | } |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 126 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 127 | if (!cookie) { |
| 128 | // This event was not tracked as an inbound event, so trace it now. |
| 129 | std::visit(Visitor{[&](const TracedMotionEvent& e) { mBackend->traceMotionEvent(e); }, |
| 130 | [&](const TracedKeyEvent& e) { mBackend->traceKeyEvent(e); }}, |
| 131 | traced); |
| 132 | } |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 133 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 134 | // The vsyncId only has meaning if the event is targeting a window. |
| 135 | const int32_t windowId = dispatchEntry.windowId.value_or(0); |
| 136 | const int32_t vsyncId = dispatchEntry.windowId.has_value() ? dispatchEntry.vsyncId : 0; |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 137 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 138 | mBackend->traceWindowDispatch({std::move(traced), dispatchEntry.deliveryTime, |
| 139 | dispatchEntry.resolvedFlags, dispatchEntry.targetUid, vsyncId, |
| 140 | windowId, dispatchEntry.transform, dispatchEntry.rawTransform, |
Prabir Pradhan | 560d0d1 | 2024-03-07 18:08:27 +0000 | [diff] [blame^] | 141 | /*hmac=*/{}, resolvedKeyRepeatCount}); |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 142 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 143 | |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 144 | InputTracer::EventState& InputTracer::getState(const EventTrackerInterface& cookie) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 145 | return static_cast<const EventTrackerImpl&>(cookie).mState; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // --- InputTracer::EventTrackerImpl --- |
| 149 | |
| 150 | InputTracer::EventTrackerImpl::EventTrackerImpl(InputTracer& tracer, TracedEvent&& event) |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 151 | : mTracer(tracer), mState(event) {} |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 152 | |
| 153 | InputTracer::EventTrackerImpl::~EventTrackerImpl() { |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 154 | if (mState.isEventProcessingComplete) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 155 | // This event has already been written to the trace as expected. |
| 156 | return; |
| 157 | } |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 158 | // The event processing was never marked as complete, so do it now. |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 159 | // TODO(b/210460522): Determine why/where the event is being destroyed before |
| 160 | // eventProcessingComplete() is called. |
| 161 | std::visit(Visitor{[&](const TracedMotionEvent& e) { mTracer.mBackend->traceMotionEvent(e); }, |
| 162 | [&](const TracedKeyEvent& e) { mTracer.mBackend->traceKeyEvent(e); }}, |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 163 | mState.event); |
| 164 | mState.isEventProcessingComplete = true; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | } // namespace android::inputdispatcher::trace::impl |