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 | |
| 24 | void AndroidInputEventProtoConverter::toProtoMotionEvent(const TracedMotionEvent& event, |
| 25 | proto::AndroidMotionEvent& outProto) { |
| 26 | outProto.set_event_id(event.id); |
| 27 | outProto.set_event_time_nanos(event.eventTime); |
| 28 | outProto.set_down_time_nanos(event.downTime); |
| 29 | outProto.set_source(event.source); |
| 30 | outProto.set_action(event.action); |
| 31 | outProto.set_device_id(event.deviceId); |
| 32 | outProto.set_display_id(event.displayId); |
| 33 | outProto.set_classification(static_cast<int32_t>(event.classification)); |
| 34 | outProto.set_cursor_position_x(event.xCursorPosition); |
| 35 | outProto.set_cursor_position_y(event.yCursorPosition); |
| 36 | outProto.set_flags(event.flags); |
| 37 | outProto.set_policy_flags(event.policyFlags); |
| 38 | |
| 39 | for (uint32_t i = 0; i < event.pointerProperties.size(); i++) { |
| 40 | auto* pointer = outProto.add_pointer(); |
| 41 | |
| 42 | const auto& props = event.pointerProperties[i]; |
| 43 | pointer->set_pointer_id(props.id); |
| 44 | pointer->set_tool_type(static_cast<int32_t>(props.toolType)); |
| 45 | |
| 46 | const auto& coords = event.pointerCoords[i]; |
| 47 | auto bits = BitSet64(coords.bits); |
| 48 | for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) { |
| 49 | const auto axis = bits.clearFirstMarkedBit(); |
| 50 | auto axisEntry = pointer->add_axis_value(); |
| 51 | axisEntry->set_axis(axis); |
| 52 | axisEntry->set_value(coords.values[axisIndex]); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void AndroidInputEventProtoConverter::toProtoKeyEvent(const TracedKeyEvent& event, |
| 58 | proto::AndroidKeyEvent& outProto) { |
| 59 | outProto.set_event_id(event.id); |
| 60 | outProto.set_event_time_nanos(event.eventTime); |
| 61 | outProto.set_down_time_nanos(event.downTime); |
| 62 | outProto.set_source(event.source); |
| 63 | outProto.set_action(event.action); |
| 64 | outProto.set_device_id(event.deviceId); |
| 65 | outProto.set_display_id(event.displayId); |
| 66 | outProto.set_key_code(event.keyCode); |
| 67 | outProto.set_scan_code(event.scanCode); |
| 68 | outProto.set_meta_state(event.metaState); |
| 69 | outProto.set_repeat_count(event.repeatCount); |
| 70 | outProto.set_flags(event.flags); |
| 71 | outProto.set_policy_flags(event.policyFlags); |
| 72 | } |
| 73 | |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 74 | void AndroidInputEventProtoConverter::toProtoWindowDispatchEvent( |
| 75 | const InputTracingBackendInterface::WindowDispatchArgs& args, |
| 76 | proto::AndroidWindowInputDispatchEvent& outProto) { |
| 77 | std::visit([&](auto entry) { outProto.set_event_id(entry.id); }, args.eventEntry); |
| 78 | outProto.set_vsync_id(args.vsyncId); |
| 79 | outProto.set_window_id(args.windowId); |
| 80 | outProto.set_resolved_flags(args.resolvedFlags); |
| 81 | |
| 82 | if (auto* motion = std::get_if<TracedMotionEvent>(&args.eventEntry); motion != nullptr) { |
| 83 | for (size_t i = 0; i < motion->pointerProperties.size(); i++) { |
| 84 | auto* pointerProto = outProto.add_dispatched_pointer(); |
| 85 | pointerProto->set_pointer_id(motion->pointerProperties[i].id); |
| 86 | const auto rawXY = |
| 87 | MotionEvent::calculateTransformedXY(motion->source, args.rawTransform, |
| 88 | motion->pointerCoords[i].getXYValue()); |
| 89 | pointerProto->set_x_in_display(rawXY.x); |
| 90 | pointerProto->set_y_in_display(rawXY.y); |
| 91 | |
| 92 | const auto& coords = motion->pointerCoords[i]; |
| 93 | const auto coordsInWindow = |
| 94 | MotionEvent::calculateTransformedCoords(motion->source, args.transform, coords); |
| 95 | auto bits = BitSet64(coords.bits); |
| 96 | for (int32_t axisIndex = 0; !bits.isEmpty(); axisIndex++) { |
| 97 | const uint32_t axis = bits.clearFirstMarkedBit(); |
| 98 | const float axisValueInWindow = coordsInWindow.values[axisIndex]; |
| 99 | if (coords.values[axisIndex] != axisValueInWindow) { |
| 100 | auto* axisEntry = pointerProto->add_axis_value_in_window(); |
| 101 | axisEntry->set_axis(axis); |
| 102 | axisEntry->set_value(axisValueInWindow); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 109 | } // namespace android::inputdispatcher::trace |