1 // Copyright (C) 2024 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Simple wrapper around Perfetto tracing that allows for building
16 // without tracing.
17 
18 #pragma once
19 
20 #include <stdint.h>
21 
22 #define GFXSTREAM_TRACE_DEFAULT_CATEGORY "gfxstream.default"
23 #define GFXSTREAM_TRACE_DECODER_CATEGORY "gfxstream.decoder"
24 #define GFXSTREAM_TRACE_STREAM_RENDERER_CATEGORY "gfxstream.stream_renderer"
25 #define GFXSTREAM_TRACE_VIRTIO_GPU_TIMELINE_CATEGORY "gfxstream.virtio_gpu_timeline"
26 
27 #ifdef GFXSTREAM_BUILD_WITH_TRACING
28 
29 #include <perfetto/tracing.h>
30 
31 PERFETTO_DEFINE_CATEGORIES(perfetto::Category(GFXSTREAM_TRACE_DEFAULT_CATEGORY)
32                                .SetDescription("Default events")
33                                .SetTags("default"),
34                            perfetto::Category(GFXSTREAM_TRACE_DECODER_CATEGORY)
35                                .SetDescription("Decoder events")
36                                .SetTags("decoder"),
37                            perfetto::Category(GFXSTREAM_TRACE_STREAM_RENDERER_CATEGORY)
38                                .SetDescription("Gfxstream frontend command events")
39                                .SetTags("stream-renderer"),
40                            perfetto::Category(GFXSTREAM_TRACE_VIRTIO_GPU_TIMELINE_CATEGORY)
41                                .SetDescription("Virtio GPU fence timeline events")
42                                .SetTags("virtio-gpu"));
43 
44 #define GFXSTREAM_TRACE_EVENT(...) TRACE_EVENT(__VA_ARGS__)
45 #define GFXSTREAM_TRACE_EVENT_INSTANT(...) TRACE_EVENT_INSTANT(__VA_ARGS__)
46 
47 #define GFXSTREAM_TRACE_FLOW(id) perfetto::Flow::ProcessScoped(id)
48 
49 #define GFXSTREAM_TRACE_TRACK_FOR_CURRENT_THREAD() perfetto::ThreadTrack::Current()
50 #define GFXSTREAM_TRACE_TRACK(id) perfetto::Track(id)
51 
52 #define GFXSTREAM_TRACE_NAME_TRACK(track, name)                           \
53     do {                                                                  \
54         auto trackDescriptor = track.Serialize();                         \
55         trackDescriptor.set_name(name);                                   \
56         perfetto::TrackEvent::SetTrackDescriptor(track, trackDescriptor); \
57     } while (0)
58 
59 #else
60 
61 #define GFXSTREAM_TRACE_EVENT(...)
62 #define GFXSTREAM_TRACE_EVENT_INSTANT(...)
63 
64 #define GFXSTREAM_TRACE_FLOW(id)
65 
66 #define GFXSTREAM_TRACE_TRACK_FOR_CURRENT_THREAD()
67 #define GFXSTREAM_TRACE_TRACK(id)
68 #define GFXSTREAM_TRACE_NAME_TRACK(track, name)
69 
70 #endif  // GFXSTREAM_BUILD_WITH_TRACING
71 
72 namespace gfxstream {
73 namespace host {
74 
75 uint64_t GetUniqueTracingId();
76 
77 void InitializeTracing();
78 
79 }  // namespace host
80 }  // namespace gfxstream
81