blob: d0bab061f4d93e6c92035a1fa455d1918af69f03 [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 Pradhanf4099452024-04-12 18:34:14 +000023#include <android/content/pm/IPackageManagerNative.h>
Prabir Pradhan4c49aad2024-02-08 20:42:35 +000024#include <ftl/flags.h>
Prabir Pradhandae52792023-12-15 07:36:40 +000025#include <perfetto/tracing.h>
26#include <mutex>
Prabir Pradhanbf3c8322024-02-23 02:38:36 +000027#include <set>
Prabir Pradhandae52792023-12-15 07:36:40 +000028
29namespace android::inputdispatcher::trace::impl {
30
31/**
32 * The tracing backend that writes events into ongoing Perfetto traces.
33 *
34 * Example shell command to take an input trace from Perfetto:
35 *
36 * adb shell perfetto \
37 * -c - --txt \
38 * -o /data/misc/perfetto-traces/trace.input-trace \
39 * <<END
40 * buffers: {
41 * size_kb: 5000
42 * fill_policy: RING_BUFFER
43 * }
44 * data_sources: {
45 * config {
46 * name: "android.input.inputevent"
47 * }
48 * }
49 * END
50 */
51class PerfettoBackend : public InputTracingBackendInterface {
52public:
Prabir Pradhan9a9897d2024-03-21 21:52:57 +000053 static bool sUseInProcessBackendForTest;
Prabir Pradhanf4099452024-04-12 18:34:14 +000054 static std::function<sp<content::pm::IPackageManagerNative>()> sPackageManagerProvider;
Prabir Pradhan9a9897d2024-03-21 21:52:57 +000055
Prabir Pradhanf4099452024-04-12 18:34:14 +000056 explicit PerfettoBackend();
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 Pradhanf4099452024-04-12 18:34:14 +000075 void initializeUidMap();
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 Pradhandae52792023-12-15 07:36:40 +000089 static std::once_flag sDataSourceRegistrationFlag;
Prabir Pradhan58f19f62024-03-07 21:54:34 +000090 static std::atomic<int32_t> sNextInstanceId;
Prabir Pradhandae52792023-12-15 07:36:40 +000091};
92
93} // namespace android::inputdispatcher::trace::impl