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