blob: fdfe495c457fc16fd6ac0849c54f90104b2534da [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
Prabir Pradhan9a9897d2024-03-21 21:52:57 +000054 static bool sUseInProcessBackendForTest;
55
Prabir Pradhanbf3c8322024-02-23 02:38:36 +000056 explicit PerfettoBackend(GetPackageUid);
Prabir Pradhandae52792023-12-15 07:36:40 +000057 ~PerfettoBackend() override = default;
58
Prabir Pradhanc7edaaa2024-03-15 15:31:02 +000059 void traceKeyEvent(const TracedKeyEvent&, const TracedEventMetadata&) override;
60 void traceMotionEvent(const TracedMotionEvent&, const TracedEventMetadata&) override;
61 void traceWindowDispatch(const WindowDispatchArgs&, const TracedEventMetadata&) override;
Prabir Pradhandae52792023-12-15 07:36:40 +000062
Prabir Pradhan58f19f62024-03-07 21:54:34 +000063private:
64 // Implementation of the perfetto data source.
65 // Each instance of the InputEventDataSource represents a different tracing session.
Prabir Pradhan9a9897d2024-03-21 21:52:57 +000066 // Its lifecycle is controlled by perfetto.
Prabir Pradhandae52792023-12-15 07:36:40 +000067 class InputEventDataSource : public perfetto::DataSource<InputEventDataSource> {
68 public:
Prabir Pradhan58f19f62024-03-07 21:54:34 +000069 explicit InputEventDataSource();
70
71 void OnSetup(const SetupArgs&) override;
Prabir Pradhandae52792023-12-15 07:36:40 +000072 void OnStart(const StartArgs&) override;
73 void OnStop(const StopArgs&) override;
Prabir Pradhan58f19f62024-03-07 21:54:34 +000074
Prabir Pradhanbf3c8322024-02-23 02:38:36 +000075 void initializeUidMap(GetPackageUid);
Prabir Pradhan4c49aad2024-02-08 20:42:35 +000076 bool shouldIgnoreTracedInputEvent(const EventType&) const;
77 inline ftl::Flags<TraceFlag> getFlags() const { return mConfig.flags; }
Prabir Pradhanc7edaaa2024-03-15 15:31:02 +000078 TraceLevel resolveTraceLevel(const TracedEventMetadata&) const;
Prabir Pradhan4c49aad2024-02-08 20:42:35 +000079
Prabir Pradhan58f19f62024-03-07 21:54:34 +000080 private:
81 const int32_t mInstanceId;
82 TraceConfig mConfig;
Prabir Pradhanb5682382024-02-23 22:52:31 +000083
Prabir Pradhanc7edaaa2024-03-15 15:31:02 +000084 bool ruleMatches(const TraceRule&, const TracedEventMetadata&) const;
Prabir Pradhanbf3c8322024-02-23 02:38:36 +000085
86 std::optional<std::map<std::string, gui::Uid>> mUidMap;
Prabir Pradhandae52792023-12-15 07:36:40 +000087 };
88
Prabir Pradhanbf3c8322024-02-23 02:38:36 +000089 // TODO(b/330360505): Query the native package manager directly from the data source,
90 // and remove this.
91 GetPackageUid mGetPackageUid;
92
Prabir Pradhandae52792023-12-15 07:36:40 +000093 static std::once_flag sDataSourceRegistrationFlag;
Prabir Pradhan58f19f62024-03-07 21:54:34 +000094 static std::atomic<int32_t> sNextInstanceId;
Prabir Pradhandae52792023-12-15 07:36:40 +000095};
96
97} // namespace android::inputdispatcher::trace::impl