blob: 7e1e36aba0b5f3459042ea194734d79105e9ea19 [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#pragma once
18
19#include "InputTracingBackendInterface.h"
20
Prabir Pradhan58f19f62024-03-07 21:54:34 +000021#include "InputTracingPerfettoBackendConfig.h"
22
Prabir Pradhandae52792023-12-15 07:36:40 +000023#include <perfetto/tracing.h>
24#include <mutex>
25
26namespace android::inputdispatcher::trace::impl {
27
28/**
29 * The tracing backend that writes events into ongoing Perfetto traces.
30 *
31 * Example shell command to take an input trace from Perfetto:
32 *
33 * adb shell perfetto \
34 * -c - --txt \
35 * -o /data/misc/perfetto-traces/trace.input-trace \
36 * <<END
37 * buffers: {
38 * size_kb: 5000
39 * fill_policy: RING_BUFFER
40 * }
41 * data_sources: {
42 * config {
43 * name: "android.input.inputevent"
44 * }
45 * }
46 * END
47 */
48class PerfettoBackend : public InputTracingBackendInterface {
49public:
50 PerfettoBackend();
51 ~PerfettoBackend() override = default;
52
Prabir Pradhan8c3b1432024-02-09 23:34:16 +000053 void traceKeyEvent(const TracedKeyEvent&, const TracedEventArgs&) override;
54 void traceMotionEvent(const TracedMotionEvent&, const TracedEventArgs&) override;
55 void traceWindowDispatch(const WindowDispatchArgs&, const TracedEventArgs&) override;
Prabir Pradhandae52792023-12-15 07:36:40 +000056
Prabir Pradhan58f19f62024-03-07 21:54:34 +000057private:
58 // Implementation of the perfetto data source.
59 // Each instance of the InputEventDataSource represents a different tracing session.
Prabir Pradhandae52792023-12-15 07:36:40 +000060 class InputEventDataSource : public perfetto::DataSource<InputEventDataSource> {
61 public:
Prabir Pradhan58f19f62024-03-07 21:54:34 +000062 explicit InputEventDataSource();
63
64 void OnSetup(const SetupArgs&) override;
Prabir Pradhandae52792023-12-15 07:36:40 +000065 void OnStart(const StartArgs&) override;
66 void OnStop(const StopArgs&) override;
Prabir Pradhan58f19f62024-03-07 21:54:34 +000067
68 private:
69 const int32_t mInstanceId;
70 TraceConfig mConfig;
Prabir Pradhandae52792023-12-15 07:36:40 +000071 };
72
Prabir Pradhandae52792023-12-15 07:36:40 +000073 static std::once_flag sDataSourceRegistrationFlag;
Prabir Pradhan58f19f62024-03-07 21:54:34 +000074 static std::atomic<int32_t> sNextInstanceId;
Prabir Pradhandae52792023-12-15 07:36:40 +000075};
76
77} // namespace android::inputdispatcher::trace::impl