xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/Tracing/LayerTracing.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2021 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <layerproto/LayerProtoHeader.h>
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker #include <atomic>
22*38e8c45fSAndroid Build Coastguard Worker #include <functional>
23*38e8c45fSAndroid Build Coastguard Worker #include <optional>
24*38e8c45fSAndroid Build Coastguard Worker #include <ostream>
25*38e8c45fSAndroid Build Coastguard Worker 
26*38e8c45fSAndroid Build Coastguard Worker namespace android {
27*38e8c45fSAndroid Build Coastguard Worker 
28*38e8c45fSAndroid Build Coastguard Worker class TransactionTracing;
29*38e8c45fSAndroid Build Coastguard Worker 
30*38e8c45fSAndroid Build Coastguard Worker /*
31*38e8c45fSAndroid Build Coastguard Worker  * LayerTracing records layer states during surface flinging. Manages tracing state and
32*38e8c45fSAndroid Build Coastguard Worker  * configuration.
33*38e8c45fSAndroid Build Coastguard Worker  *
34*38e8c45fSAndroid Build Coastguard Worker  * The traced data can then be collected with Perfetto.
35*38e8c45fSAndroid Build Coastguard Worker  *
36*38e8c45fSAndroid Build Coastguard Worker  * The Perfetto custom data source LayerDataSource is registered with perfetto. The data source
37*38e8c45fSAndroid Build Coastguard Worker  * is used to listen to perfetto events (setup, start, stop, flush) and to write trace packets
38*38e8c45fSAndroid Build Coastguard Worker  * to perfetto.
39*38e8c45fSAndroid Build Coastguard Worker  *
40*38e8c45fSAndroid Build Coastguard Worker  * The user can configure/start/stop tracing via /system/bin/perfetto.
41*38e8c45fSAndroid Build Coastguard Worker  *
42*38e8c45fSAndroid Build Coastguard Worker  * Tracing can operate in the following modes.
43*38e8c45fSAndroid Build Coastguard Worker  *
44*38e8c45fSAndroid Build Coastguard Worker  * ACTIVE mode:
45*38e8c45fSAndroid Build Coastguard Worker  * A layers snapshot is taken and written to perfetto for each vsyncid commit.
46*38e8c45fSAndroid Build Coastguard Worker  *
47*38e8c45fSAndroid Build Coastguard Worker  * GENERATED mode:
48*38e8c45fSAndroid Build Coastguard Worker  * Listens to the perfetto 'flush' event (e.g. when a bugreport is taken).
49*38e8c45fSAndroid Build Coastguard Worker  * When a 'flush' event is received, the ring buffer of transactions (hold by TransactionTracing)
50*38e8c45fSAndroid Build Coastguard Worker  * is processed by LayerTraceGenerator, a sequence of layers snapshots is generated
51*38e8c45fSAndroid Build Coastguard Worker  * and written to perfetto.
52*38e8c45fSAndroid Build Coastguard Worker  *
53*38e8c45fSAndroid Build Coastguard Worker  * DUMP mode:
54*38e8c45fSAndroid Build Coastguard Worker  * When the 'start' event is received a single layers snapshot is taken
55*38e8c45fSAndroid Build Coastguard Worker  * and written to perfetto.
56*38e8c45fSAndroid Build Coastguard Worker  *
57*38e8c45fSAndroid Build Coastguard Worker  *
58*38e8c45fSAndroid Build Coastguard Worker  * E.g. start active mode tracing
59*38e8c45fSAndroid Build Coastguard Worker  * (replace mode value with MODE_DUMP, MODE_GENERATED or MODE_GENERATED_BUGREPORT_ONLY to enable
60*38e8c45fSAndroid Build Coastguard Worker  * different tracing modes):
61*38e8c45fSAndroid Build Coastguard Worker  *
62*38e8c45fSAndroid Build Coastguard Worker    adb shell -t perfetto \
63*38e8c45fSAndroid Build Coastguard Worker      -c - --txt \
64*38e8c45fSAndroid Build Coastguard Worker      -o /data/misc/perfetto-traces/trace \
65*38e8c45fSAndroid Build Coastguard Worker    <<EOF
66*38e8c45fSAndroid Build Coastguard Worker    unique_session_name: "surfaceflinger_layers_active"
67*38e8c45fSAndroid Build Coastguard Worker    buffers: {
68*38e8c45fSAndroid Build Coastguard Worker        size_kb: 63488
69*38e8c45fSAndroid Build Coastguard Worker        fill_policy: RING_BUFFER
70*38e8c45fSAndroid Build Coastguard Worker    }
71*38e8c45fSAndroid Build Coastguard Worker    data_sources: {
72*38e8c45fSAndroid Build Coastguard Worker        config {
73*38e8c45fSAndroid Build Coastguard Worker            name: "android.surfaceflinger.layers"
74*38e8c45fSAndroid Build Coastguard Worker            surfaceflinger_layers_config: {
75*38e8c45fSAndroid Build Coastguard Worker                mode: MODE_ACTIVE
76*38e8c45fSAndroid Build Coastguard Worker                trace_flags: TRACE_FLAG_INPUT
77*38e8c45fSAndroid Build Coastguard Worker                trace_flags: TRACE_FLAG_COMPOSITION
78*38e8c45fSAndroid Build Coastguard Worker                trace_flags: TRACE_FLAG_HWC
79*38e8c45fSAndroid Build Coastguard Worker                trace_flags: TRACE_FLAG_BUFFERS
80*38e8c45fSAndroid Build Coastguard Worker                trace_flags: TRACE_FLAG_VIRTUAL_DISPLAYS
81*38e8c45fSAndroid Build Coastguard Worker            }
82*38e8c45fSAndroid Build Coastguard Worker        }
83*38e8c45fSAndroid Build Coastguard Worker    }
84*38e8c45fSAndroid Build Coastguard Worker EOF
85*38e8c45fSAndroid Build Coastguard Worker  *
86*38e8c45fSAndroid Build Coastguard Worker  */
87*38e8c45fSAndroid Build Coastguard Worker class LayerTracing {
88*38e8c45fSAndroid Build Coastguard Worker public:
89*38e8c45fSAndroid Build Coastguard Worker     using Mode = perfetto::protos::pbzero::SurfaceFlingerLayersConfig::Mode;
90*38e8c45fSAndroid Build Coastguard Worker     using OnLayersSnapshotCallback = std::function<void(perfetto::protos::LayersSnapshotProto&&)>;
91*38e8c45fSAndroid Build Coastguard Worker 
92*38e8c45fSAndroid Build Coastguard Worker     enum Flag : uint32_t {
93*38e8c45fSAndroid Build Coastguard Worker         TRACE_INPUT = 1 << 1,
94*38e8c45fSAndroid Build Coastguard Worker         TRACE_COMPOSITION = 1 << 2,
95*38e8c45fSAndroid Build Coastguard Worker         TRACE_EXTRA = 1 << 3,
96*38e8c45fSAndroid Build Coastguard Worker         TRACE_HWC = 1 << 4,
97*38e8c45fSAndroid Build Coastguard Worker         TRACE_BUFFERS = 1 << 5,
98*38e8c45fSAndroid Build Coastguard Worker         TRACE_VIRTUAL_DISPLAYS = 1 << 6,
99*38e8c45fSAndroid Build Coastguard Worker         TRACE_ALL = TRACE_INPUT | TRACE_COMPOSITION | TRACE_EXTRA,
100*38e8c45fSAndroid Build Coastguard Worker     };
101*38e8c45fSAndroid Build Coastguard Worker 
102*38e8c45fSAndroid Build Coastguard Worker     LayerTracing();
103*38e8c45fSAndroid Build Coastguard Worker     LayerTracing(std::ostream&);
104*38e8c45fSAndroid Build Coastguard Worker     ~LayerTracing();
105*38e8c45fSAndroid Build Coastguard Worker     void setTakeLayersSnapshotProtoFunction(
106*38e8c45fSAndroid Build Coastguard Worker             const std::function<void(uint32_t, const OnLayersSnapshotCallback&)>&);
107*38e8c45fSAndroid Build Coastguard Worker     void setTransactionTracing(TransactionTracing&);
108*38e8c45fSAndroid Build Coastguard Worker 
109*38e8c45fSAndroid Build Coastguard Worker     // Start event from perfetto data source
110*38e8c45fSAndroid Build Coastguard Worker     void onStart(Mode mode, uint32_t flags);
111*38e8c45fSAndroid Build Coastguard Worker     // Flush event from perfetto data source
112*38e8c45fSAndroid Build Coastguard Worker     void onFlush(Mode mode, uint32_t flags, bool isBugreport);
113*38e8c45fSAndroid Build Coastguard Worker     // Stop event from perfetto data source
114*38e8c45fSAndroid Build Coastguard Worker     void onStop(Mode mode, uint32_t flags, std::function<void()>&& deferredStopDone);
115*38e8c45fSAndroid Build Coastguard Worker 
116*38e8c45fSAndroid Build Coastguard Worker     void addProtoSnapshotToOstream(perfetto::protos::LayersSnapshotProto&& snapshot, Mode mode);
117*38e8c45fSAndroid Build Coastguard Worker     bool isActiveTracingStarted() const;
118*38e8c45fSAndroid Build Coastguard Worker     uint32_t getActiveTracingFlags() const;
119*38e8c45fSAndroid Build Coastguard Worker     bool isActiveTracingFlagSet(Flag flag) const;
120*38e8c45fSAndroid Build Coastguard Worker     static perfetto::protos::LayersTraceFileProto createTraceFileProto();
121*38e8c45fSAndroid Build Coastguard Worker 
122*38e8c45fSAndroid Build Coastguard Worker private:
123*38e8c45fSAndroid Build Coastguard Worker     void writeSnapshotToStream(perfetto::protos::LayersSnapshotProto&& snapshot) const;
124*38e8c45fSAndroid Build Coastguard Worker     void writeSnapshotToPerfetto(const perfetto::protos::LayersSnapshotProto& snapshot, Mode mode);
125*38e8c45fSAndroid Build Coastguard Worker     bool checkAndUpdateLastVsyncIdWrittenToPerfetto(Mode mode, std::int64_t vsyncId);
126*38e8c45fSAndroid Build Coastguard Worker 
127*38e8c45fSAndroid Build Coastguard Worker     std::function<void(uint32_t, const OnLayersSnapshotCallback&)> mTakeLayersSnapshotProto;
128*38e8c45fSAndroid Build Coastguard Worker     TransactionTracing* mTransactionTracing;
129*38e8c45fSAndroid Build Coastguard Worker 
130*38e8c45fSAndroid Build Coastguard Worker     std::atomic<bool> mIsActiveTracingStarted{false};
131*38e8c45fSAndroid Build Coastguard Worker     std::atomic<uint32_t> mActiveTracingFlags{0};
132*38e8c45fSAndroid Build Coastguard Worker     std::atomic<std::int64_t> mLastVsyncIdWrittenToPerfetto{-1};
133*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::reference_wrapper<std::ostream>> mOutStream;
134*38e8c45fSAndroid Build Coastguard Worker };
135*38e8c45fSAndroid Build Coastguard Worker 
136*38e8c45fSAndroid Build Coastguard Worker } // namespace android
137