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 #include "AndroidInputEventProtoConverter.h"
18
19 #include <android-base/logging.h>
20 #include <perfetto/trace/android/android_input_event.pbzero.h>
21
22 namespace android::inputdispatcher::trace {
23
24 namespace {
25
26 using namespace ftl::flag_operators;
27
28 // The trace config to use for maximal tracing.
29 const impl::TraceConfig CONFIG_TRACE_ALL{
30 .flags = impl::TraceFlag::TRACE_DISPATCHER_INPUT_EVENTS |
31 impl::TraceFlag::TRACE_DISPATCHER_WINDOW_DISPATCH,
32 .rules = {impl::TraceRule{.level = impl::TraceLevel::TRACE_LEVEL_COMPLETE,
33 .matchAllPackages = {},
34 .matchAnyPackages = {},
35 .matchSecure{},
36 .matchImeConnectionActive = {}}},
37 };
38
39 } // namespace
40
toProtoMotionEvent(const TracedMotionEvent & event,proto::AndroidMotionEvent & outProto,bool isRedacted)41 void AndroidInputEventProtoConverter::toProtoMotionEvent(const TracedMotionEvent& event,
42 proto::AndroidMotionEvent& outProto,
43 bool isRedacted) {
44 outProto.set_event_id(event.id);
45 outProto.set_event_time_nanos(event.eventTime);
46 outProto.set_down_time_nanos(event.downTime);
47 outProto.set_source(event.source);
48 outProto.set_action(event.action);
49 outProto.set_device_id(event.deviceId);
50 outProto.set_display_id(event.displayId.val());
51 outProto.set_classification(static_cast<int32_t>(event.classification));
52 outProto.set_flags(event.flags);
53 outProto.set_policy_flags(event.policyFlags);
54
55 if (!isRedacted) {
56 outProto.set_cursor_position_x(event.xCursorPosition);
57 outProto.set_cursor_position_y(event.yCursorPosition);
58 outProto.set_meta_state(event.metaState);
59 }
60
61 for (uint32_t i = 0; i < event.pointerProperties.size(); i++) {
62 auto* pointer = outProto.add_pointer();
63
64 const auto& props = event.pointerProperties[i];
65 pointer->set_pointer_id(props.id);
66 pointer->set_tool_type(static_cast<int32_t>(props.toolType));
67
68 const auto& coords = event.pointerCoords[i];
69 auto bits = BitSet64(coords.bits);
70 for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) {
71 const auto axis = bits.clearFirstMarkedBit();
72 auto axisEntry = pointer->add_axis_value();
73 axisEntry->set_axis(axis);
74
75 if (!isRedacted) {
76 axisEntry->set_value(coords.values[axisIndex]);
77 }
78 }
79 }
80 }
81
toProtoKeyEvent(const TracedKeyEvent & event,proto::AndroidKeyEvent & outProto,bool isRedacted)82 void AndroidInputEventProtoConverter::toProtoKeyEvent(const TracedKeyEvent& event,
83 proto::AndroidKeyEvent& outProto,
84 bool isRedacted) {
85 outProto.set_event_id(event.id);
86 outProto.set_event_time_nanos(event.eventTime);
87 outProto.set_down_time_nanos(event.downTime);
88 outProto.set_source(event.source);
89 outProto.set_action(event.action);
90 outProto.set_device_id(event.deviceId);
91 outProto.set_display_id(event.displayId.val());
92 outProto.set_repeat_count(event.repeatCount);
93 outProto.set_flags(event.flags);
94 outProto.set_policy_flags(event.policyFlags);
95
96 if (!isRedacted) {
97 outProto.set_key_code(event.keyCode);
98 outProto.set_scan_code(event.scanCode);
99 outProto.set_meta_state(event.metaState);
100 }
101 }
102
toProtoWindowDispatchEvent(const WindowDispatchArgs & args,proto::AndroidWindowInputDispatchEvent & outProto,bool isRedacted)103 void AndroidInputEventProtoConverter::toProtoWindowDispatchEvent(
104 const WindowDispatchArgs& args, proto::AndroidWindowInputDispatchEvent& outProto,
105 bool isRedacted) {
106 std::visit([&](auto entry) { outProto.set_event_id(entry.id); }, args.eventEntry);
107 outProto.set_vsync_id(args.vsyncId);
108 outProto.set_window_id(args.windowId);
109 outProto.set_resolved_flags(args.resolvedFlags);
110
111 if (isRedacted) {
112 return;
113 }
114 if (auto* motion = std::get_if<TracedMotionEvent>(&args.eventEntry); motion != nullptr) {
115 for (size_t i = 0; i < motion->pointerProperties.size(); i++) {
116 auto* pointerProto = outProto.add_dispatched_pointer();
117 pointerProto->set_pointer_id(motion->pointerProperties[i].id);
118 const auto rawXY =
119 MotionEvent::calculateTransformedXY(motion->source, args.rawTransform,
120 motion->pointerCoords[i].getXYValue());
121 pointerProto->set_x_in_display(rawXY.x);
122 pointerProto->set_y_in_display(rawXY.y);
123
124 const auto& coords = motion->pointerCoords[i];
125 const auto coordsInWindow =
126 MotionEvent::calculateTransformedCoords(motion->source, motion->flags,
127 args.transform, coords);
128 auto bits = BitSet64(coords.bits);
129 for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) {
130 const uint32_t axis = bits.clearFirstMarkedBit();
131 const float axisValueInWindow = coordsInWindow.values[axisIndex];
132 if (coords.values[axisIndex] != axisValueInWindow) {
133 auto* axisEntry = pointerProto->add_axis_value_in_window();
134 axisEntry->set_axis(axis);
135 axisEntry->set_value(axisValueInWindow);
136 }
137 }
138 }
139 }
140 }
141
parseConfig(proto::AndroidInputEventConfig::Decoder & protoConfig)142 impl::TraceConfig AndroidInputEventProtoConverter::parseConfig(
143 proto::AndroidInputEventConfig::Decoder& protoConfig) {
144 if (protoConfig.has_mode() &&
145 protoConfig.mode() == proto::AndroidInputEventConfig::TRACE_MODE_TRACE_ALL) {
146 // User has requested the preset for maximal tracing
147 return CONFIG_TRACE_ALL;
148 }
149
150 impl::TraceConfig config;
151
152 // Parse trace flags
153 if (protoConfig.has_trace_dispatcher_input_events() &&
154 protoConfig.trace_dispatcher_input_events()) {
155 config.flags |= impl::TraceFlag::TRACE_DISPATCHER_INPUT_EVENTS;
156 }
157 if (protoConfig.has_trace_dispatcher_window_dispatch() &&
158 protoConfig.trace_dispatcher_window_dispatch()) {
159 config.flags |= impl::TraceFlag::TRACE_DISPATCHER_WINDOW_DISPATCH;
160 }
161
162 // Parse trace rules
163 auto rulesIt = protoConfig.rules();
164 while (rulesIt) {
165 proto::AndroidInputEventConfig::TraceRule::Decoder protoRule{rulesIt->as_bytes()};
166 config.rules.emplace_back();
167 auto& rule = config.rules.back();
168
169 rule.level = protoRule.has_trace_level()
170 ? static_cast<impl::TraceLevel>(protoRule.trace_level())
171 : impl::TraceLevel::TRACE_LEVEL_NONE;
172
173 if (protoRule.has_match_all_packages()) {
174 auto pkgIt = protoRule.match_all_packages();
175 while (pkgIt) {
176 rule.matchAllPackages.emplace_back(pkgIt->as_std_string());
177 pkgIt++;
178 }
179 }
180
181 if (protoRule.has_match_any_packages()) {
182 auto pkgIt = protoRule.match_any_packages();
183 while (pkgIt) {
184 rule.matchAnyPackages.emplace_back(pkgIt->as_std_string());
185 pkgIt++;
186 }
187 }
188
189 if (protoRule.has_match_secure()) {
190 rule.matchSecure = protoRule.match_secure();
191 }
192
193 if (protoRule.has_match_ime_connection_active()) {
194 rule.matchImeConnectionActive = protoRule.match_ime_connection_active();
195 }
196
197 rulesIt++;
198 }
199
200 return config;
201 }
202
203 } // namespace android::inputdispatcher::trace
204