1 /* 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 21 #include "InputTracingPerfettoBackendConfig.h" 22 23 #include <android/content/pm/IPackageManagerNative.h> 24 #include <ftl/flags.h> 25 #include <perfetto/tracing.h> 26 #include <mutex> 27 #include <set> 28 29 namespace 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 */ 51 class PerfettoBackend : public InputTracingBackendInterface { 52 public: 53 static bool sUseInProcessBackendForTest; 54 static std::function<sp<content::pm::IPackageManagerNative>()> sPackageManagerProvider; 55 56 explicit PerfettoBackend(); 57 ~PerfettoBackend() override = default; 58 59 void traceKeyEvent(const TracedKeyEvent&, const TracedEventMetadata&) override; 60 void traceMotionEvent(const TracedMotionEvent&, const TracedEventMetadata&) override; 61 void traceWindowDispatch(const WindowDispatchArgs&, const TracedEventMetadata&) override; 62 63 private: 64 // Implementation of the perfetto data source. 65 // Each instance of the InputEventDataSource represents a different tracing session. 66 // Its lifecycle is controlled by perfetto. 67 class InputEventDataSource : public perfetto::DataSource<InputEventDataSource> { 68 public: 69 explicit InputEventDataSource(); 70 71 void OnSetup(const SetupArgs&) override; 72 void OnStart(const StartArgs&) override; 73 void OnStop(const StopArgs&) override; 74 75 void initializeUidMap(); 76 bool shouldIgnoreTracedInputEvent(const EventType&) const; getFlags()77 inline ftl::Flags<TraceFlag> getFlags() const { return mConfig.flags; } 78 TraceLevel resolveTraceLevel(const TracedEventMetadata&) const; 79 80 private: 81 const int32_t mInstanceId; 82 TraceConfig mConfig; 83 84 bool ruleMatches(const TraceRule&, const TracedEventMetadata&) const; 85 86 std::optional<std::map<std::string, gui::Uid>> mUidMap; 87 }; 88 89 static std::once_flag sDataSourceRegistrationFlag; 90 static std::atomic<int32_t> sNextInstanceId; 91 }; 92 93 } // namespace android::inputdispatcher::trace::impl 94