blob: e945066dffacc184cd5650b147f1b7196aaab456 [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>
Prabir Pradhanbf3c8322024-02-23 02:38:36 +000026#include <set>
Prabir Pradhandae52792023-12-15 07:36:40 +000027
28namespace android::inputdispatcher::trace::impl {
29
30/**
31 * The tracing backend that writes events into ongoing Perfetto traces.
32 *
33 * Example shell command to take an input trace from Perfetto:
34 *
35 * adb shell perfetto \
36 * -c - --txt \
37 * -o /data/misc/perfetto-traces/trace.input-trace \
38 * <<END
39 * buffers: {
40 * size_kb: 5000
41 * fill_policy: RING_BUFFER
42 * }
43 * data_sources: {
44 * config {
45 * name: "android.input.inputevent"
46 * }
47 * }
48 * END
49 */
50class PerfettoBackend : public InputTracingBackendInterface {
51public:
Prabir Pradhanbf3c8322024-02-23 02:38:36 +000052 using GetPackageUid = std::function<gui::Uid(std::string)>;
53
54 explicit PerfettoBackend(GetPackageUid);
Prabir Pradhandae52792023-12-15 07:36:40 +000055 ~PerfettoBackend() override = default;
56
Prabir Pradhanc7edaaa2024-03-15 15:31:02 +000057 void traceKeyEvent(const TracedKeyEvent&, const TracedEventMetadata&) override;
58 void traceMotionEvent(const TracedMotionEvent&, const TracedEventMetadata&) override;
59 void traceWindowDispatch(const WindowDispatchArgs&, const TracedEventMetadata&) override;
Prabir Pradhandae52792023-12-15 07:36:40 +000060
Prabir Pradhan58f19f62024-03-07 21:54:34 +000061private:
62 // Implementation of the perfetto data source.
63 // Each instance of the InputEventDataSource represents a different tracing session.
Prabir Pradhandae52792023-12-15 07:36:40 +000064 class InputEventDataSource : public perfetto::DataSource<InputEventDataSource> {
65 public:
Prabir Pradhan58f19f62024-03-07 21:54:34 +000066 explicit InputEventDataSource();
67
68 void OnSetup(const SetupArgs&) override;
Prabir Pradhandae52792023-12-15 07:36:40 +000069 void OnStart(const StartArgs&) override;
70 void OnStop(const StopArgs&) override;
Prabir Pradhan58f19f62024-03-07 21:54:34 +000071
Prabir Pradhanbf3c8322024-02-23 02:38:36 +000072 void initializeUidMap(GetPackageUid);
Prabir Pradhan4c49aad2024-02-08 20:42:35 +000073 bool shouldIgnoreTracedInputEvent(const EventType&) const;
74 inline ftl::Flags<TraceFlag> getFlags() const { return mConfig.flags; }
Prabir Pradhanc7edaaa2024-03-15 15:31:02 +000075 TraceLevel resolveTraceLevel(const TracedEventMetadata&) const;
Prabir Pradhan4c49aad2024-02-08 20:42:35 +000076
Prabir Pradhan58f19f62024-03-07 21:54:34 +000077 private:
78 const int32_t mInstanceId;
79 TraceConfig mConfig;
Prabir Pradhanb5682382024-02-23 22:52:31 +000080
Prabir Pradhanc7edaaa2024-03-15 15:31:02 +000081 bool ruleMatches(const TraceRule&, const TracedEventMetadata&) const;
Prabir Pradhanbf3c8322024-02-23 02:38:36 +000082
83 std::optional<std::map<std::string, gui::Uid>> mUidMap;
Prabir Pradhandae52792023-12-15 07:36:40 +000084 };
85
Prabir Pradhanbf3c8322024-02-23 02:38:36 +000086 // TODO(b/330360505): Query the native package manager directly from the data source,
87 // and remove this.
88 GetPackageUid mGetPackageUid;
89
Prabir Pradhandae52792023-12-15 07:36:40 +000090 static std::once_flag sDataSourceRegistrationFlag;
Prabir Pradhan58f19f62024-03-07 21:54:34 +000091 static std::atomic<int32_t> sNextInstanceId;
Prabir Pradhandae52792023-12-15 07:36:40 +000092};
93
94} // namespace android::inputdispatcher::trace::impl