blob: e99fe4c7f16e87f2885f08dd95315117ab7c216e [file] [log] [blame]
Vishnu Nair00b90132021-11-05 14:03:40 -07001/*
2 * Copyright 2021 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
Vishnu Nair00b90132021-11-05 14:03:40 -070019#include <layerproto/LayerProtoHeader.h>
Vishnu Nair00b90132021-11-05 14:03:40 -070020
Kean Mariotti639b54f2023-04-20 12:06:29 +000021#include <atomic>
22#include <functional>
23#include <optional>
24#include <ostream>
Vishnu Nair00b90132021-11-05 14:03:40 -070025
26namespace android {
27
Kean Mariotti639b54f2023-04-20 12:06:29 +000028class TransactionTracing;
Vishnu Nair00b90132021-11-05 14:03:40 -070029
30/*
31 * LayerTracing records layer states during surface flinging. Manages tracing state and
32 * configuration.
Kean Mariotti639b54f2023-04-20 12:06:29 +000033 *
34 * The traced data can then be collected with Perfetto.
35 *
36 * The Perfetto custom data source LayerDataSource is registered with perfetto. The data source
37 * is used to listen to perfetto events (setup, start, stop, flush) and to write trace packets
38 * to perfetto.
39 *
40 * The user can configure/start/stop tracing via /system/bin/perfetto.
41 *
42 * Tracing can operate in the following modes.
43 *
44 * ACTIVE mode:
45 * A layers snapshot is taken and written to perfetto for each vsyncid commit.
46 *
47 * GENERATED mode:
48 * Listens to the perfetto 'flush' event (e.g. when a bugreport is taken).
49 * When a 'flush' event is received, the ring buffer of transactions (hold by TransactionTracing)
50 * is processed by LayerTraceGenerator, a sequence of layers snapshots is generated
51 * and written to perfetto.
52 *
53 * DUMP mode:
54 * When the 'start' event is received a single layers snapshot is taken
55 * and written to perfetto.
56 *
57 *
Kean Mariotti8b5c6bd2023-09-20 06:50:41 +000058 * E.g. start active mode tracing
59 * (replace mode value with MODE_DUMP, MODE_GENERATED or MODE_GENERATED_BUGREPORT_ONLY to enable
60 * different tracing modes):
Kean Mariotti639b54f2023-04-20 12:06:29 +000061 *
62 adb shell -t perfetto \
63 -c - --txt \
64 -o /data/misc/perfetto-traces/trace \
65 <<EOF
66 unique_session_name: "surfaceflinger_layers_active"
67 buffers: {
68 size_kb: 63488
69 fill_policy: RING_BUFFER
70 }
71 data_sources: {
72 config {
73 name: "android.surfaceflinger.layers"
74 surfaceflinger_layers_config: {
75 mode: MODE_ACTIVE
76 trace_flags: TRACE_FLAG_INPUT
77 trace_flags: TRACE_FLAG_COMPOSITION
78 trace_flags: TRACE_FLAG_HWC
79 trace_flags: TRACE_FLAG_BUFFERS
80 trace_flags: TRACE_FLAG_VIRTUAL_DISPLAYS
81 }
82 }
83 }
Kean Mariotti8b5c6bd2023-09-20 06:50:41 +000084EOF
Kean Mariotti639b54f2023-04-20 12:06:29 +000085 *
Vishnu Nair00b90132021-11-05 14:03:40 -070086 */
87class LayerTracing {
88public:
Kean Mariotti639b54f2023-04-20 12:06:29 +000089 using Mode = perfetto::protos::pbzero::SurfaceFlingerLayersConfig::Mode;
Kean Mariottifc0cd252023-12-07 09:27:40 +000090 using OnLayersSnapshotCallback = std::function<void(perfetto::protos::LayersSnapshotProto&&)>;
Vishnu Nair00b90132021-11-05 14:03:40 -070091
Kean Mariotti639b54f2023-04-20 12:06:29 +000092 enum Flag : uint32_t {
Vishnu Nair00b90132021-11-05 14:03:40 -070093 TRACE_INPUT = 1 << 1,
94 TRACE_COMPOSITION = 1 << 2,
95 TRACE_EXTRA = 1 << 3,
96 TRACE_HWC = 1 << 4,
97 TRACE_BUFFERS = 1 << 5,
Nataniel Borges63b3c872022-11-03 16:22:24 +000098 TRACE_VIRTUAL_DISPLAYS = 1 << 6,
Vishnu Nair00b90132021-11-05 14:03:40 -070099 TRACE_ALL = TRACE_INPUT | TRACE_COMPOSITION | TRACE_EXTRA,
100 };
Kean Mariotti639b54f2023-04-20 12:06:29 +0000101
102 LayerTracing();
Kean Mariotti29aad4e2023-09-20 12:53:33 +0000103 LayerTracing(std::ostream&);
Kean Mariotti639b54f2023-04-20 12:06:29 +0000104 ~LayerTracing();
105 void setTakeLayersSnapshotProtoFunction(
Kean Mariottifc0cd252023-12-07 09:27:40 +0000106 const std::function<void(uint32_t, const OnLayersSnapshotCallback&)>&);
Kean Mariotti639b54f2023-04-20 12:06:29 +0000107 void setTransactionTracing(TransactionTracing&);
Kean Mariotti639b54f2023-04-20 12:06:29 +0000108
109 // Start event from perfetto data source
110 void onStart(Mode mode, uint32_t flags);
111 // Flush event from perfetto data source
Kean Mariotti8b5c6bd2023-09-20 06:50:41 +0000112 void onFlush(Mode mode, uint32_t flags, bool isBugreport);
Kean Mariotti639b54f2023-04-20 12:06:29 +0000113 // Stop event from perfetto data source
Kean Mariottifc0cd252023-12-07 09:27:40 +0000114 void onStop(Mode mode, uint32_t flags, std::function<void()>&& deferredStopDone);
Kean Mariotti639b54f2023-04-20 12:06:29 +0000115
116 void addProtoSnapshotToOstream(perfetto::protos::LayersSnapshotProto&& snapshot, Mode mode);
117 bool isActiveTracingStarted() const;
118 uint32_t getActiveTracingFlags() const;
119 bool isActiveTracingFlagSet(Flag flag) const;
120 static perfetto::protos::LayersTraceFileProto createTraceFileProto();
Vishnu Nair00b90132021-11-05 14:03:40 -0700121
122private:
Kean Mariotti639b54f2023-04-20 12:06:29 +0000123 void writeSnapshotToStream(perfetto::protos::LayersSnapshotProto&& snapshot) const;
124 void writeSnapshotToPerfetto(const perfetto::protos::LayersSnapshotProto& snapshot, Mode mode);
125 bool checkAndUpdateLastVsyncIdWrittenToPerfetto(Mode mode, std::int64_t vsyncId);
126
Kean Mariottifc0cd252023-12-07 09:27:40 +0000127 std::function<void(uint32_t, const OnLayersSnapshotCallback&)> mTakeLayersSnapshotProto;
Kean Mariotti639b54f2023-04-20 12:06:29 +0000128 TransactionTracing* mTransactionTracing;
129
130 std::atomic<bool> mIsActiveTracingStarted{false};
131 std::atomic<uint32_t> mActiveTracingFlags{0};
132 std::atomic<std::int64_t> mLastVsyncIdWrittenToPerfetto{-1};
133 std::optional<std::reference_wrapper<std::ostream>> mOutStream;
Vishnu Nair00b90132021-11-05 14:03:40 -0700134};
135
136} // namespace android