blob: 920ea600a9abfa2fddd421268ed060a3aa75a465 [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 Pradhan4c49aad2024-02-08 20:42:35 +000023#include <ftl/flags.h>
Prabir Pradhandae52792023-12-15 07:36:40 +000024#include <perfetto/tracing.h>
25#include <mutex>
26
27namespace android::inputdispatcher::trace::impl {
28
29/**
30 * The tracing backend that writes events into ongoing Perfetto traces.
31 *
32 * Example shell command to take an input trace from Perfetto:
33 *
34 * adb shell perfetto \
35 * -c - --txt \
36 * -o /data/misc/perfetto-traces/trace.input-trace \
37 * <<END
38 * buffers: {
39 * size_kb: 5000
40 * fill_policy: RING_BUFFER
41 * }
42 * data_sources: {
43 * config {
44 * name: "android.input.inputevent"
45 * }
46 * }
47 * END
48 */
49class PerfettoBackend : public InputTracingBackendInterface {
50public:
51 PerfettoBackend();
52 ~PerfettoBackend() override = default;
53
Prabir Pradhan8c3b1432024-02-09 23:34:16 +000054 void traceKeyEvent(const TracedKeyEvent&, const TracedEventArgs&) override;
55 void traceMotionEvent(const TracedMotionEvent&, const TracedEventArgs&) override;
56 void traceWindowDispatch(const WindowDispatchArgs&, const TracedEventArgs&) override;
Prabir Pradhandae52792023-12-15 07:36:40 +000057
Prabir Pradhan58f19f62024-03-07 21:54:34 +000058private:
59 // Implementation of the perfetto data source.
60 // Each instance of the InputEventDataSource represents a different tracing session.
Prabir Pradhandae52792023-12-15 07:36:40 +000061 class InputEventDataSource : public perfetto::DataSource<InputEventDataSource> {
62 public:
Prabir Pradhan58f19f62024-03-07 21:54:34 +000063 explicit InputEventDataSource();
64
65 void OnSetup(const SetupArgs&) override;
Prabir Pradhandae52792023-12-15 07:36:40 +000066 void OnStart(const StartArgs&) override;
67 void OnStop(const StopArgs&) override;
Prabir Pradhan58f19f62024-03-07 21:54:34 +000068
Prabir Pradhan4c49aad2024-02-08 20:42:35 +000069 bool shouldIgnoreTracedInputEvent(const EventType&) const;
70 inline ftl::Flags<TraceFlag> getFlags() const { return mConfig.flags; }
71
Prabir Pradhan58f19f62024-03-07 21:54:34 +000072 private:
73 const int32_t mInstanceId;
74 TraceConfig mConfig;
Prabir Pradhandae52792023-12-15 07:36:40 +000075 };
76
Prabir Pradhandae52792023-12-15 07:36:40 +000077 static std::once_flag sDataSourceRegistrationFlag;
Prabir Pradhan58f19f62024-03-07 21:54:34 +000078 static std::atomic<int32_t> sNextInstanceId;
Prabir Pradhandae52792023-12-15 07:36:40 +000079};
80
81} // namespace android::inputdispatcher::trace::impl