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 | #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 | |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame] | 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 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 41 | void AndroidInputEventProtoConverter::toProtoMotionEvent(const TracedMotionEvent& event, |
Prabir Pradhan | b568238 | 2024-02-23 22:52:31 +0000 | [diff] [blame] | 42 | proto::AndroidMotionEvent& outProto, |
| 43 | bool isRedacted) { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 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); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 50 | outProto.set_display_id(event.displayId.val()); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 51 | outProto.set_classification(static_cast<int32_t>(event.classification)); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 52 | outProto.set_flags(event.flags); |
| 53 | outProto.set_policy_flags(event.policyFlags); |
| 54 | |
Prabir Pradhan | b568238 | 2024-02-23 22:52:31 +0000 | [diff] [blame] | 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 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 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); |
Prabir Pradhan | b568238 | 2024-02-23 22:52:31 +0000 | [diff] [blame] | 74 | |
| 75 | if (!isRedacted) { |
| 76 | axisEntry->set_value(coords.values[axisIndex]); |
| 77 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void AndroidInputEventProtoConverter::toProtoKeyEvent(const TracedKeyEvent& event, |
Prabir Pradhan | b568238 | 2024-02-23 22:52:31 +0000 | [diff] [blame] | 83 | proto::AndroidKeyEvent& outProto, |
| 84 | bool isRedacted) { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 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); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame^] | 91 | outProto.set_display_id(event.displayId.val()); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 92 | outProto.set_repeat_count(event.repeatCount); |
| 93 | outProto.set_flags(event.flags); |
| 94 | outProto.set_policy_flags(event.policyFlags); |
Prabir Pradhan | b568238 | 2024-02-23 22:52:31 +0000 | [diff] [blame] | 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 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 103 | void AndroidInputEventProtoConverter::toProtoWindowDispatchEvent( |
Prabir Pradhan | b568238 | 2024-02-23 22:52:31 +0000 | [diff] [blame] | 104 | const WindowDispatchArgs& args, proto::AndroidWindowInputDispatchEvent& outProto, |
| 105 | bool isRedacted) { |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 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 | |
Prabir Pradhan | b568238 | 2024-02-23 22:52:31 +0000 | [diff] [blame] | 111 | if (isRedacted) { |
| 112 | return; |
| 113 | } |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 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, args.transform, coords); |
| 127 | auto bits = BitSet64(coords.bits); |
| 128 | for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) { |
| 129 | const uint32_t axis = bits.clearFirstMarkedBit(); |
| 130 | const float axisValueInWindow = coordsInWindow.values[axisIndex]; |
| 131 | if (coords.values[axisIndex] != axisValueInWindow) { |
| 132 | auto* axisEntry = pointerProto->add_axis_value_in_window(); |
| 133 | axisEntry->set_axis(axis); |
| 134 | axisEntry->set_value(axisValueInWindow); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame] | 141 | impl::TraceConfig AndroidInputEventProtoConverter::parseConfig( |
| 142 | proto::AndroidInputEventConfig::Decoder& protoConfig) { |
| 143 | if (protoConfig.has_mode() && |
| 144 | protoConfig.mode() == proto::AndroidInputEventConfig::TRACE_MODE_TRACE_ALL) { |
| 145 | // User has requested the preset for maximal tracing |
| 146 | return CONFIG_TRACE_ALL; |
| 147 | } |
| 148 | |
| 149 | impl::TraceConfig config; |
| 150 | |
| 151 | // Parse trace flags |
| 152 | if (protoConfig.has_trace_dispatcher_input_events() && |
| 153 | protoConfig.trace_dispatcher_input_events()) { |
| 154 | config.flags |= impl::TraceFlag::TRACE_DISPATCHER_INPUT_EVENTS; |
| 155 | } |
| 156 | if (protoConfig.has_trace_dispatcher_window_dispatch() && |
| 157 | protoConfig.trace_dispatcher_window_dispatch()) { |
| 158 | config.flags |= impl::TraceFlag::TRACE_DISPATCHER_WINDOW_DISPATCH; |
| 159 | } |
| 160 | |
| 161 | // Parse trace rules |
| 162 | auto rulesIt = protoConfig.rules(); |
| 163 | while (rulesIt) { |
| 164 | proto::AndroidInputEventConfig::TraceRule::Decoder protoRule{rulesIt->as_bytes()}; |
| 165 | config.rules.emplace_back(); |
| 166 | auto& rule = config.rules.back(); |
| 167 | |
| 168 | rule.level = protoRule.has_trace_level() |
| 169 | ? static_cast<impl::TraceLevel>(protoRule.trace_level()) |
| 170 | : impl::TraceLevel::TRACE_LEVEL_NONE; |
| 171 | |
| 172 | if (protoRule.has_match_all_packages()) { |
| 173 | auto pkgIt = protoRule.match_all_packages(); |
| 174 | while (pkgIt) { |
| 175 | rule.matchAllPackages.emplace_back(pkgIt->as_std_string()); |
| 176 | pkgIt++; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (protoRule.has_match_any_packages()) { |
| 181 | auto pkgIt = protoRule.match_any_packages(); |
| 182 | while (pkgIt) { |
| 183 | rule.matchAnyPackages.emplace_back(pkgIt->as_std_string()); |
| 184 | pkgIt++; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (protoRule.has_match_secure()) { |
| 189 | rule.matchSecure = protoRule.match_secure(); |
| 190 | } |
| 191 | |
| 192 | if (protoRule.has_match_ime_connection_active()) { |
| 193 | rule.matchImeConnectionActive = protoRule.match_ime_connection_active(); |
| 194 | } |
| 195 | |
| 196 | rulesIt++; |
| 197 | } |
| 198 | |
| 199 | return config; |
| 200 | } |
| 201 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 202 | } // namespace android::inputdispatcher::trace |