Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 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 | |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame] | 21 | #include "InputTracingPerfettoBackendConfig.h" |
| 22 | |
Prabir Pradhan | 4c49aad | 2024-02-08 20:42:35 +0000 | [diff] [blame^] | 23 | #include <ftl/flags.h> |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 24 | #include <perfetto/tracing.h> |
| 25 | #include <mutex> |
| 26 | |
| 27 | namespace 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 | */ |
| 49 | class PerfettoBackend : public InputTracingBackendInterface { |
| 50 | public: |
| 51 | PerfettoBackend(); |
| 52 | ~PerfettoBackend() override = default; |
| 53 | |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 54 | void traceKeyEvent(const TracedKeyEvent&, const TracedEventArgs&) override; |
| 55 | void traceMotionEvent(const TracedMotionEvent&, const TracedEventArgs&) override; |
| 56 | void traceWindowDispatch(const WindowDispatchArgs&, const TracedEventArgs&) override; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 57 | |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame] | 58 | private: |
| 59 | // Implementation of the perfetto data source. |
| 60 | // Each instance of the InputEventDataSource represents a different tracing session. |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 61 | class InputEventDataSource : public perfetto::DataSource<InputEventDataSource> { |
| 62 | public: |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame] | 63 | explicit InputEventDataSource(); |
| 64 | |
| 65 | void OnSetup(const SetupArgs&) override; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 66 | void OnStart(const StartArgs&) override; |
| 67 | void OnStop(const StopArgs&) override; |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame] | 68 | |
Prabir Pradhan | 4c49aad | 2024-02-08 20:42:35 +0000 | [diff] [blame^] | 69 | bool shouldIgnoreTracedInputEvent(const EventType&) const; |
| 70 | inline ftl::Flags<TraceFlag> getFlags() const { return mConfig.flags; } |
| 71 | |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame] | 72 | private: |
| 73 | const int32_t mInstanceId; |
| 74 | TraceConfig mConfig; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 77 | static std::once_flag sDataSourceRegistrationFlag; |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame] | 78 | static std::atomic<int32_t> sNextInstanceId; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace android::inputdispatcher::trace::impl |