blob: 0be64e67ca8e02b99816d08bb48d7d5924f4e1b4 [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
62} // namespace
63
64// --- InputTracer ---
65
66InputTracer::InputTracer(std::unique_ptr<InputTracingBackendInterface> backend)
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000067 : mBackend(std::move(backend)) {}
Prabir Pradhandae52792023-12-15 07:36:40 +000068
69std::unique_ptr<EventTrackerInterface> InputTracer::traceInboundEvent(const EventEntry& entry) {
Prabir Pradhandae52792023-12-15 07:36:40 +000070 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
85void InputTracer::dispatchToTargetHint(const EventTrackerInterface& cookie,
86 const InputTarget& target) {
Prabir Pradhan1ea04a32024-02-10 03:02:59 +000087 auto& eventState = getState(cookie);
88 if (eventState.isEventProcessingComplete) {
Prabir Pradhandae52792023-12-15 07:36:40 +000089 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
94void InputTracer::eventProcessingComplete(const EventTrackerInterface& cookie) {
Prabir Pradhan1ea04a32024-02-10 03:02:59 +000095 auto& eventState = getState(cookie);
96 if (eventState.isEventProcessingComplete) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000097 LOG(FATAL) << "Traced event was already logged. "
98 "eventProcessingComplete() was likely called more than once.";
99 }
Prabir Pradhandae52792023-12-15 07:36:40 +0000100
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000101 std::visit(Visitor{[&](const TracedMotionEvent& e) { mBackend->traceMotionEvent(e); },
102 [&](const TracedKeyEvent& e) { mBackend->traceKeyEvent(e); }},
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000103 eventState.event);
104 eventState.isEventProcessingComplete = true;
Prabir Pradhandae52792023-12-15 07:36:40 +0000105}
106
107void InputTracer::traceEventDispatch(const DispatchEntry& dispatchEntry,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000108 const EventTrackerInterface* cookie) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000109 const EventEntry& entry = *dispatchEntry.eventEntry;
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000110
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000111 TracedEvent traced;
112 if (entry.type == EventEntry::Type::MOTION) {
113 const auto& motion = static_cast<const MotionEntry&>(entry);
114 traced = createTracedEvent(motion);
115 } else if (entry.type == EventEntry::Type::KEY) {
116 const auto& key = static_cast<const KeyEntry&>(entry);
117 traced = createTracedEvent(key);
118 } else {
119 LOG(FATAL) << "Cannot trace EventEntry of type: " << ftl::enum_string(entry.type);
120 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000121
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000122 if (!cookie) {
123 // This event was not tracked as an inbound event, so trace it now.
124 std::visit(Visitor{[&](const TracedMotionEvent& e) { mBackend->traceMotionEvent(e); },
125 [&](const TracedKeyEvent& e) { mBackend->traceKeyEvent(e); }},
126 traced);
127 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000128
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000129 // The vsyncId only has meaning if the event is targeting a window.
130 const int32_t windowId = dispatchEntry.windowId.value_or(0);
131 const int32_t vsyncId = dispatchEntry.windowId.has_value() ? dispatchEntry.vsyncId : 0;
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000132
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000133 mBackend->traceWindowDispatch({std::move(traced), dispatchEntry.deliveryTime,
134 dispatchEntry.resolvedFlags, dispatchEntry.targetUid, vsyncId,
135 windowId, dispatchEntry.transform, dispatchEntry.rawTransform,
136 /*hmac=*/{}});
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000137}
Prabir Pradhandae52792023-12-15 07:36:40 +0000138
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000139InputTracer::EventState& InputTracer::getState(const EventTrackerInterface& cookie) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000140 return static_cast<const EventTrackerImpl&>(cookie).mState;
Prabir Pradhandae52792023-12-15 07:36:40 +0000141}
142
143// --- InputTracer::EventTrackerImpl ---
144
145InputTracer::EventTrackerImpl::EventTrackerImpl(InputTracer& tracer, TracedEvent&& event)
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000146 : mTracer(tracer), mState(event) {}
Prabir Pradhandae52792023-12-15 07:36:40 +0000147
148InputTracer::EventTrackerImpl::~EventTrackerImpl() {
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000149 if (mState.isEventProcessingComplete) {
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000150 // This event has already been written to the trace as expected.
151 return;
152 }
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000153 // The event processing was never marked as complete, so do it now.
Prabir Pradhanbb7a0202024-02-10 02:09:01 +0000154 // TODO(b/210460522): Determine why/where the event is being destroyed before
155 // eventProcessingComplete() is called.
156 std::visit(Visitor{[&](const TracedMotionEvent& e) { mTracer.mBackend->traceMotionEvent(e); },
157 [&](const TracedKeyEvent& e) { mTracer.mBackend->traceKeyEvent(e); }},
Prabir Pradhan1ea04a32024-02-10 03:02:59 +0000158 mState.event);
159 mState.isEventProcessingComplete = true;
Prabir Pradhandae52792023-12-15 07:36:40 +0000160}
161
162} // namespace android::inputdispatcher::trace::impl