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 | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 23 | #include <perfetto/tracing.h> |
| 24 | #include <mutex> |
| 25 | |
| 26 | namespace android::inputdispatcher::trace::impl { |
| 27 | |
| 28 | /** |
| 29 | * The tracing backend that writes events into ongoing Perfetto traces. |
| 30 | * |
| 31 | * Example shell command to take an input trace from Perfetto: |
| 32 | * |
| 33 | * adb shell perfetto \ |
| 34 | * -c - --txt \ |
| 35 | * -o /data/misc/perfetto-traces/trace.input-trace \ |
| 36 | * <<END |
| 37 | * buffers: { |
| 38 | * size_kb: 5000 |
| 39 | * fill_policy: RING_BUFFER |
| 40 | * } |
| 41 | * data_sources: { |
| 42 | * config { |
| 43 | * name: "android.input.inputevent" |
| 44 | * } |
| 45 | * } |
| 46 | * END |
| 47 | */ |
| 48 | class PerfettoBackend : public InputTracingBackendInterface { |
| 49 | public: |
| 50 | PerfettoBackend(); |
| 51 | ~PerfettoBackend() override = default; |
| 52 | |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 53 | void traceKeyEvent(const TracedKeyEvent&, const TracedEventArgs&) override; |
| 54 | void traceMotionEvent(const TracedMotionEvent&, const TracedEventArgs&) override; |
| 55 | void traceWindowDispatch(const WindowDispatchArgs&, const TracedEventArgs&) override; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 56 | |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame^] | 57 | private: |
| 58 | // Implementation of the perfetto data source. |
| 59 | // Each instance of the InputEventDataSource represents a different tracing session. |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 60 | class InputEventDataSource : public perfetto::DataSource<InputEventDataSource> { |
| 61 | public: |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame^] | 62 | explicit InputEventDataSource(); |
| 63 | |
| 64 | void OnSetup(const SetupArgs&) override; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 65 | void OnStart(const StartArgs&) override; |
| 66 | void OnStop(const StopArgs&) override; |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame^] | 67 | |
| 68 | private: |
| 69 | const int32_t mInstanceId; |
| 70 | TraceConfig mConfig; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 73 | static std::once_flag sDataSourceRegistrationFlag; |
Prabir Pradhan | 58f19f6 | 2024-03-07 21:54:34 +0000 | [diff] [blame^] | 74 | static std::atomic<int32_t> sNextInstanceId; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | } // namespace android::inputdispatcher::trace::impl |