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> |
Prabir Pradhan | 38ed544 | 2024-06-05 03:45:18 +0000 | [diff] [blame] | 25 | #include <perfetto/trace/android/winscope_extensions.pbzero.h> |
| 26 | #include <perfetto/trace/android/winscope_extensions_impl.pbzero.h> |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 27 | |
| 28 | namespace android::inputdispatcher::trace::impl { |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | constexpr auto INPUT_EVENT_TRACE_DATA_SOURCE_NAME = "android.input.inputevent"; |
| 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | // --- PerfettoBackend::InputEventDataSource --- |
| 37 | |
| 38 | void PerfettoBackend::InputEventDataSource::OnStart(const perfetto::DataSourceBase::StartArgs&) { |
| 39 | LOG(INFO) << "Starting perfetto trace for: " << INPUT_EVENT_TRACE_DATA_SOURCE_NAME; |
| 40 | } |
| 41 | |
| 42 | void PerfettoBackend::InputEventDataSource::OnStop(const perfetto::DataSourceBase::StopArgs&) { |
| 43 | LOG(INFO) << "Stopping perfetto trace for: " << INPUT_EVENT_TRACE_DATA_SOURCE_NAME; |
| 44 | InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) { ctx.Flush(); }); |
| 45 | } |
| 46 | |
| 47 | // --- PerfettoBackend --- |
| 48 | |
| 49 | std::once_flag PerfettoBackend::sDataSourceRegistrationFlag{}; |
| 50 | |
| 51 | PerfettoBackend::PerfettoBackend() { |
| 52 | // Use a once-flag to ensure that the data source is only registered once per boot, since |
| 53 | // we never unregister the InputEventDataSource. |
| 54 | std::call_once(sDataSourceRegistrationFlag, []() { |
| 55 | perfetto::TracingInitArgs args; |
| 56 | args.backends = perfetto::kSystemBackend; |
| 57 | perfetto::Tracing::Initialize(args); |
| 58 | |
| 59 | // Register our custom data source for input event tracing. |
| 60 | perfetto::DataSourceDescriptor dsd; |
| 61 | dsd.set_name(INPUT_EVENT_TRACE_DATA_SOURCE_NAME); |
| 62 | InputEventDataSource::Register(dsd); |
| 63 | LOG(INFO) << "InputTracer initialized for data source: " |
| 64 | << INPUT_EVENT_TRACE_DATA_SOURCE_NAME; |
| 65 | }); |
| 66 | } |
| 67 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 68 | void PerfettoBackend::traceMotionEvent(const TracedMotionEvent& event) { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 69 | InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) { |
| 70 | auto tracePacket = ctx.NewTracePacket(); |
Prabir Pradhan | 38ed544 | 2024-06-05 03:45:18 +0000 | [diff] [blame] | 71 | auto* winscopeExtensions = static_cast<perfetto::protos::pbzero::WinscopeExtensionsImpl*>( |
| 72 | tracePacket->set_winscope_extensions()); |
| 73 | auto* inputEvent = winscopeExtensions->set_android_input_event(); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 74 | auto* dispatchMotion = inputEvent->set_dispatcher_motion_event(); |
| 75 | AndroidInputEventProtoConverter::toProtoMotionEvent(event, *dispatchMotion); |
| 76 | }); |
| 77 | } |
| 78 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 79 | void PerfettoBackend::traceKeyEvent(const TracedKeyEvent& event) { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 80 | InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) { |
| 81 | auto tracePacket = ctx.NewTracePacket(); |
Prabir Pradhan | 38ed544 | 2024-06-05 03:45:18 +0000 | [diff] [blame] | 82 | auto* winscopeExtensions = static_cast<perfetto::protos::pbzero::WinscopeExtensionsImpl*>( |
| 83 | tracePacket->set_winscope_extensions()); |
| 84 | auto* inputEvent = winscopeExtensions->set_android_input_event(); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 85 | auto* dispatchKey = inputEvent->set_dispatcher_key_event(); |
| 86 | AndroidInputEventProtoConverter::toProtoKeyEvent(event, *dispatchKey); |
| 87 | }); |
| 88 | } |
| 89 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 90 | void PerfettoBackend::traceWindowDispatch(const WindowDispatchArgs& dispatchArgs) { |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 91 | InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) { |
| 92 | auto tracePacket = ctx.NewTracePacket(); |
Prabir Pradhan | 38ed544 | 2024-06-05 03:45:18 +0000 | [diff] [blame] | 93 | auto* winscopeExtensions = static_cast<perfetto::protos::pbzero::WinscopeExtensionsImpl*>( |
| 94 | tracePacket->set_winscope_extensions()); |
| 95 | auto* inputEventProto = winscopeExtensions->set_android_input_event(); |
Prabir Pradhan | adc59b4 | 2023-12-15 05:34:11 +0000 | [diff] [blame] | 96 | auto* dispatchEventProto = inputEventProto->set_dispatcher_window_dispatch_event(); |
| 97 | AndroidInputEventProtoConverter::toProtoWindowDispatchEvent(dispatchArgs, |
| 98 | *dispatchEventProto); |
| 99 | }); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | } // namespace android::inputdispatcher::trace::impl |