blob: 8ef9ca504d16382191dd3b7ad17c4cb0e7ec1854 [file] [log] [blame]
Prabir Pradhandae52792023-12-15 07:36:40 +00001/*
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
26namespace android::inputdispatcher::trace::impl {
27
28namespace {
29
30constexpr auto INPUT_EVENT_TRACE_DATA_SOURCE_NAME = "android.input.inputevent";
31
32} // namespace
33
34// --- PerfettoBackend::InputEventDataSource ---
35
36void PerfettoBackend::InputEventDataSource::OnStart(const perfetto::DataSourceBase::StartArgs&) {
37 LOG(INFO) << "Starting perfetto trace for: " << INPUT_EVENT_TRACE_DATA_SOURCE_NAME;
38}
39
40void PerfettoBackend::InputEventDataSource::OnStop(const perfetto::DataSourceBase::StopArgs&) {
41 LOG(INFO) << "Stopping perfetto trace for: " << INPUT_EVENT_TRACE_DATA_SOURCE_NAME;
42 InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) { ctx.Flush(); });
43}
44
45// --- PerfettoBackend ---
46
47std::once_flag PerfettoBackend::sDataSourceRegistrationFlag{};
48
49PerfettoBackend::PerfettoBackend() {
50 // Use a once-flag to ensure that the data source is only registered once per boot, since
51 // we never unregister the InputEventDataSource.
52 std::call_once(sDataSourceRegistrationFlag, []() {
53 perfetto::TracingInitArgs args;
54 args.backends = perfetto::kSystemBackend;
55 perfetto::Tracing::Initialize(args);
56
57 // Register our custom data source for input event tracing.
58 perfetto::DataSourceDescriptor dsd;
59 dsd.set_name(INPUT_EVENT_TRACE_DATA_SOURCE_NAME);
60 InputEventDataSource::Register(dsd);
61 LOG(INFO) << "InputTracer initialized for data source: "
62 << INPUT_EVENT_TRACE_DATA_SOURCE_NAME;
63 });
64}
65
Prabir Pradhan8c3b1432024-02-09 23:34:16 +000066void PerfettoBackend::traceMotionEvent(const TracedMotionEvent& event,
67 const TracedEventArgs& args) {
68 if (args.isSecure) {
69 // For now, avoid tracing secure event entirely.
70 return;
71 }
Prabir Pradhandae52792023-12-15 07:36:40 +000072 InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) {
73 auto tracePacket = ctx.NewTracePacket();
74 auto* inputEvent = tracePacket->set_android_input_event();
75 auto* dispatchMotion = inputEvent->set_dispatcher_motion_event();
76 AndroidInputEventProtoConverter::toProtoMotionEvent(event, *dispatchMotion);
77 });
78}
79
Prabir Pradhan8c3b1432024-02-09 23:34:16 +000080void PerfettoBackend::traceKeyEvent(const TracedKeyEvent& event, const TracedEventArgs& args) {
81 if (args.isSecure) {
82 // For now, avoid tracing secure event entirely.
83 return;
84 }
Prabir Pradhandae52792023-12-15 07:36:40 +000085 InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) {
86 auto tracePacket = ctx.NewTracePacket();
87 auto* inputEvent = tracePacket->set_android_input_event();
88 auto* dispatchKey = inputEvent->set_dispatcher_key_event();
89 AndroidInputEventProtoConverter::toProtoKeyEvent(event, *dispatchKey);
90 });
91}
92
Prabir Pradhan8c3b1432024-02-09 23:34:16 +000093void PerfettoBackend::traceWindowDispatch(const WindowDispatchArgs& dispatchArgs,
94 const TracedEventArgs& args) {
95 if (args.isSecure) {
96 // For now, avoid tracing secure event entirely.
97 return;
98 }
Prabir Pradhanadc59b42023-12-15 05:34:11 +000099 InputEventDataSource::Trace([&](InputEventDataSource::TraceContext ctx) {
100 auto tracePacket = ctx.NewTracePacket();
Prabir Pradhan8c3b1432024-02-09 23:34:16 +0000101 auto* inputEvent = tracePacket->set_android_input_event();
102 auto* dispatchEvent = inputEvent->set_dispatcher_window_dispatch_event();
103 AndroidInputEventProtoConverter::toProtoWindowDispatchEvent(dispatchArgs, *dispatchEvent);
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000104 });
Prabir Pradhandae52792023-12-15 07:36:40 +0000105}
106
107} // namespace android::inputdispatcher::trace::impl