| 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 | #define LOG_TAG "InputTracer" |
| 18 | |
| 19 | #include "InputTracingPerfettoBackend.h" |
| 20 | |
| 21 | #include "AndroidInputEventProtoConverter.h" |
| 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | #include <perfetto/trace/android/android_input_event.pbzero.h> |
| 25 | |
| 26 | namespace android::inputdispatcher::trace::impl { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | constexpr auto INPUT_EVENT_TRACE_DATA_SOURCE_NAME = "android.input.inputevent"; |
| 31 | |
| 32 | } // namespace |
| 33 | |
| 34 | // --- PerfettoBackend::InputEventDataSource --- |
| 35 | |
| Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame^] | 36 | PerfettoBackend::InputEventDataSource::InputEventDataSource() : mInstanceId(sNextInstanceId++) {} |
| 37 | |
| 38 | void PerfettoBackend::InputEventDataSource::OnSetup(const InputEventDataSource::SetupArgs& args) { |
| 39 | LOG(INFO) << "Setting up perfetto trace for: " << INPUT_EVENT_TRACE_DATA_SOURCE_NAME |
| 40 | << ", instanceId: " << mInstanceId; |
| 41 | const auto rawConfig = args.config->android_input_event_config_raw(); |
| 42 | auto protoConfig = perfetto::protos::pbzero::AndroidInputEventConfig::Decoder{rawConfig}; |
| 43 | |
| 44 | mConfig = AndroidInputEventProtoConverter::parseConfig(protoConfig); |
| Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame^] | 47 | void PerfettoBackend::InputEventDataSource::OnStart(const InputEventDataSource::StartArgs&) { |
| 48 | LOG(INFO) << "Starting perfetto trace for: " << INPUT_EVENT_TRACE_DATA_SOURCE_NAME |
| 49 | << ", instanceId: " << mInstanceId; |
| 50 | } |
| 51 | |
| 52 | void PerfettoBackend::InputEventDataSource::OnStop(const InputEventDataSource::StopArgs&) { |
| 53 | LOG(INFO) << "Stopping perfetto trace for: " << INPUT_EVENT_TRACE_DATA_SOURCE_NAME |
| 54 | << ", instanceId: " << mInstanceId; |
| Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 55 | InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) { ctx.Flush(); }); |
| 56 | } |
| 57 | |
| 58 | // --- PerfettoBackend --- |
| 59 | |
| 60 | std::once_flag PerfettoBackend::sDataSourceRegistrationFlag{}; |
| 61 | |
| Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame^] | 62 | std::atomic<int32_t> PerfettoBackend::sNextInstanceId{1}; |
| 63 | |
| Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 64 | PerfettoBackend::PerfettoBackend() { |
| 65 | // Use a once-flag to ensure that the data source is only registered once per boot, since |
| 66 | // we never unregister the InputEventDataSource. |
| 67 | std::call_once(sDataSourceRegistrationFlag, []() { |
| 68 | perfetto::TracingInitArgs args; |
| 69 | args.backends = perfetto::kSystemBackend; |
| 70 | perfetto::Tracing::Initialize(args); |
| 71 | |
| 72 | // Register our custom data source for input event tracing. |
| 73 | perfetto::DataSourceDescriptor dsd; |
| 74 | dsd.set_name(INPUT_EVENT_TRACE_DATA_SOURCE_NAME); |
| 75 | InputEventDataSource::Register(dsd); |
| 76 | LOG(INFO) << "InputTracer initialized for data source: " |
| 77 | << INPUT_EVENT_TRACE_DATA_SOURCE_NAME; |
| 78 | }); |
| 79 | } |
| 80 | |
| Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 81 | void PerfettoBackend::traceMotionEvent(const TracedMotionEvent& event, |
| 82 | const TracedEventArgs& args) { |
| 83 | if (args.isSecure) { |
| 84 | // For now, avoid tracing secure event entirely. |
| 85 | return; |
| 86 | } |
| Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 87 | InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) { |
| 88 | auto tracePacket = ctx.NewTracePacket(); |
| 89 | auto* inputEvent = tracePacket->set_android_input_event(); |
| 90 | auto* dispatchMotion = inputEvent->set_dispatcher_motion_event(); |
| 91 | AndroidInputEventProtoConverter::toProtoMotionEvent(event, *dispatchMotion); |
| 92 | }); |
| 93 | } |
| 94 | |
| Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 95 | void PerfettoBackend::traceKeyEvent(const TracedKeyEvent& event, const TracedEventArgs& args) { |
| 96 | if (args.isSecure) { |
| 97 | // For now, avoid tracing secure event entirely. |
| 98 | return; |
| 99 | } |
| Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 100 | InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) { |
| 101 | auto tracePacket = ctx.NewTracePacket(); |
| 102 | auto* inputEvent = tracePacket->set_android_input_event(); |
| 103 | auto* dispatchKey = inputEvent->set_dispatcher_key_event(); |
| 104 | AndroidInputEventProtoConverter::toProtoKeyEvent(event, *dispatchKey); |
| 105 | }); |
| 106 | } |
| 107 | |
| Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 108 | void PerfettoBackend::traceWindowDispatch(const WindowDispatchArgs& dispatchArgs, |
| 109 | const TracedEventArgs& args) { |
| 110 | if (args.isSecure) { |
| 111 | // For now, avoid tracing secure event entirely. |
| 112 | return; |
| 113 | } |
| Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 114 | InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) { |
| 115 | auto tracePacket = ctx.NewTracePacket(); |
| Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 116 | auto* inputEvent = tracePacket->set_android_input_event(); |
| 117 | auto* dispatchEvent = inputEvent->set_dispatcher_window_dispatch_event(); |
| 118 | AndroidInputEventProtoConverter::toProtoWindowDispatchEvent(dispatchArgs, *dispatchEvent); |
| Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 119 | }); |
| Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | } // namespace android::inputdispatcher::trace::impl |