1 /*
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 #undef LOG_TAG
18 #define LOG_TAG "LayerTracing"
19 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
21 #include <filesystem>
22
23 #include <SurfaceFlinger.h>
24 #include <android-base/stringprintf.h>
25 #include <log/log.h>
26 #include <utils/SystemClock.h>
27 #include <utils/Trace.h>
28
29 #include "LayerTracing.h"
30 #include "RingBuffer.h"
31
32 namespace android {
33
LayerTracing()34 LayerTracing::LayerTracing()
35 : mBuffer(std::make_unique<RingBuffer<LayersTraceFileProto, LayersTraceProto>>()) {}
36
37 LayerTracing::~LayerTracing() = default;
38
enable()39 bool LayerTracing::enable() {
40 std::scoped_lock lock(mTraceLock);
41 if (mEnabled) {
42 return false;
43 }
44 mBuffer->setSize(mBufferSizeInBytes);
45 mEnabled = true;
46 return true;
47 }
48
disable(std::string filename,bool writeToFile)49 bool LayerTracing::disable(std::string filename, bool writeToFile) {
50 std::scoped_lock lock(mTraceLock);
51 if (!mEnabled) {
52 return false;
53 }
54 mEnabled = false;
55 if (writeToFile) {
56 LayersTraceFileProto fileProto = createTraceFileProto();
57 mBuffer->writeToFile(fileProto, filename);
58 }
59 mBuffer->reset();
60 return true;
61 }
62
appendToStream(std::ofstream & out)63 void LayerTracing::appendToStream(std::ofstream& out) {
64 std::scoped_lock lock(mTraceLock);
65 LayersTraceFileProto fileProto = createTraceFileProto();
66 mBuffer->appendToStream(fileProto, out);
67 mBuffer->reset();
68 }
69
isEnabled() const70 bool LayerTracing::isEnabled() const {
71 std::scoped_lock lock(mTraceLock);
72 return mEnabled;
73 }
74
writeToFile(std::string filename)75 status_t LayerTracing::writeToFile(std::string filename) {
76 std::scoped_lock lock(mTraceLock);
77 if (!mEnabled) {
78 return STATUS_OK;
79 }
80 LayersTraceFileProto fileProto = createTraceFileProto();
81 return mBuffer->writeToFile(fileProto, filename);
82 }
83
setTraceFlags(uint32_t flags)84 void LayerTracing::setTraceFlags(uint32_t flags) {
85 std::scoped_lock lock(mTraceLock);
86 mFlags = flags;
87 }
88
setBufferSize(size_t bufferSizeInBytes)89 void LayerTracing::setBufferSize(size_t bufferSizeInBytes) {
90 std::scoped_lock lock(mTraceLock);
91 mBufferSizeInBytes = bufferSizeInBytes;
92 }
93
flagIsSet(uint32_t flags) const94 bool LayerTracing::flagIsSet(uint32_t flags) const {
95 return (mFlags & flags) == flags;
96 }
getFlags() const97 uint32_t LayerTracing::getFlags() const {
98 return mFlags;
99 }
100
createTraceFileProto()101 LayersTraceFileProto LayerTracing::createTraceFileProto() {
102 LayersTraceFileProto fileProto;
103 fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
104 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
105 auto timeOffsetNs = static_cast<std::uint64_t>(systemTime(SYSTEM_TIME_REALTIME) -
106 systemTime(SYSTEM_TIME_MONOTONIC));
107 fileProto.set_real_to_elapsed_time_offset_nanos(timeOffsetNs);
108 return fileProto;
109 }
110
dump(std::string & result) const111 void LayerTracing::dump(std::string& result) const {
112 std::scoped_lock lock(mTraceLock);
113 base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
114 mBuffer->dump(result);
115 }
116
notify(bool visibleRegionDirty,int64_t time,int64_t vsyncId,LayersProto * layers,std::string hwcDump,google::protobuf::RepeatedPtrField<DisplayProto> * displays)117 void LayerTracing::notify(bool visibleRegionDirty, int64_t time, int64_t vsyncId,
118 LayersProto* layers, std::string hwcDump,
119 google::protobuf::RepeatedPtrField<DisplayProto>* displays) {
120 std::scoped_lock lock(mTraceLock);
121 if (!mEnabled) {
122 return;
123 }
124
125 if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) {
126 return;
127 }
128
129 ATRACE_CALL();
130 LayersTraceProto entry;
131 entry.set_elapsed_realtime_nanos(time);
132 const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched";
133 entry.set_where(where);
134 entry.mutable_layers()->Swap(layers);
135
136 if (flagIsSet(LayerTracing::TRACE_HWC)) {
137 entry.set_hwc_blob(hwcDump);
138 }
139 if (!flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
140 entry.set_excludes_composition_state(true);
141 }
142 entry.mutable_displays()->Swap(displays);
143 entry.set_vsync_id(vsyncId);
144 mBuffer->emplace(std::move(entry));
145 }
146
147 } // namespace android
148