1 /*
2 * Copyright 2022 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 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <filesystem>
20 #include <fstream>
21 #include <iostream>
22 #include <string>
23 #include <unordered_map>
24
25 #include <LayerProtoHelper.h>
26 #include <Tracing/LayerTracing.h>
27 #include <Tracing/TransactionProtoParser.h>
28 #include <Tracing/tools/LayerTraceGenerator.h>
29 #include <layerproto/LayerProtoHeader.h>
30 #include <log/log.h>
31
32 using namespace android::surfaceflinger;
33
34 namespace android {
35
36 class TransactionTraceTestSuite : public testing::Test,
37 public testing::WithParamInterface<std::filesystem::path> {
38 public:
39 static std::vector<std::filesystem::path> sTransactionTraces;
40 static constexpr std::string_view sTransactionTracePrefix = "transactions_trace_";
41 static constexpr std::string_view sLayersTracePrefix = "layers_trace_";
42 static constexpr std::string_view sTracePostfix = ".winscope";
43
44 perfetto::protos::TransactionTraceFile mTransactionTrace;
45 perfetto::protos::LayersTraceFileProto mExpectedLayersTraceProto;
46 perfetto::protos::LayersTraceFileProto mActualLayersTraceProto;
47
48 protected:
SetUp()49 void SetUp() override {
50 std::filesystem::path transactionTracePath = GetParam();
51 parseTransactionTraceFromFile(transactionTracePath.c_str(), mTransactionTrace);
52
53 std::string expectedLayersFilename = std::string(sLayersTracePrefix) +
54 transactionTracePath.filename().string().substr(sTransactionTracePrefix.length());
55 std::string expectedLayersTracePath =
56 transactionTracePath.parent_path().string() + "/" + expectedLayersFilename;
57 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(expectedLayersTracePath)));
58 parseLayersTraceFromFile(expectedLayersTracePath.c_str(), mExpectedLayersTraceProto);
59 TemporaryDir temp_dir;
60
61 std::string actualLayersTracePath =
62 std::string(temp_dir.path) + "/" + expectedLayersFilename + "_actual";
63 {
64 auto traceFlags = LayerTracing::TRACE_INPUT | LayerTracing::TRACE_BUFFERS;
65 std::ofstream outStream{actualLayersTracePath, std::ios::binary | std::ios::app};
66 auto layerTracing = LayerTracing{outStream};
67 EXPECT_TRUE(LayerTraceGenerator().generate(mTransactionTrace, traceFlags, layerTracing,
68 /*onlyLastEntry=*/true))
69 << "Failed to generate layers trace from " << transactionTracePath;
70 }
71
72 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(actualLayersTracePath)));
73 parseLayersTraceFromFile(actualLayersTracePath.c_str(), mActualLayersTraceProto);
74 }
75
parseTransactionTraceFromFile(const char * transactionTracePath,perfetto::protos::TransactionTraceFile & outProto)76 void parseTransactionTraceFromFile(const char* transactionTracePath,
77 perfetto::protos::TransactionTraceFile& outProto) {
78 ALOGD("Parsing file %s...", transactionTracePath);
79 std::fstream input(transactionTracePath, std::ios::in | std::ios::binary);
80 EXPECT_TRUE(input) << "Error could not open " << transactionTracePath;
81 EXPECT_TRUE(outProto.ParseFromIstream(&input))
82 << "Failed to parse " << transactionTracePath;
83 }
84
parseLayersTraceFromFile(const char * layersTracePath,perfetto::protos::LayersTraceFileProto & outProto)85 void parseLayersTraceFromFile(const char* layersTracePath,
86 perfetto::protos::LayersTraceFileProto& outProto) {
87 ALOGD("Parsing file %s...", layersTracePath);
88 std::fstream input(layersTracePath, std::ios::in | std::ios::binary);
89 EXPECT_TRUE(input) << "Error could not open " << layersTracePath;
90 EXPECT_TRUE(outProto.ParseFromIstream(&input)) << "Failed to parse " << layersTracePath;
91 }
92 };
93
94 std::vector<std::filesystem::path> TransactionTraceTestSuite::sTransactionTraces{};
95
96 struct LayerInfo {
97 uint64_t id;
98 std::string name;
99 uint64_t parent;
100 int z;
101 uint64_t curr_frame;
102 float x;
103 float y;
104 uint32_t bufferWidth;
105 uint32_t bufferHeight;
106 Rect touchableRegionBounds;
107 };
108
operator ==(const LayerInfo & lh,const LayerInfo & rh)109 bool operator==(const LayerInfo& lh, const LayerInfo& rh) {
110 return std::make_tuple(lh.id, lh.name, lh.parent, lh.z, lh.curr_frame, lh.bufferWidth,
111 lh.bufferHeight, lh.touchableRegionBounds) ==
112 std::make_tuple(rh.id, rh.name, rh.parent, rh.z, rh.curr_frame, rh.bufferWidth,
113 rh.bufferHeight, rh.touchableRegionBounds);
114 }
115
compareById(const LayerInfo & a,const LayerInfo & b)116 bool compareById(const LayerInfo& a, const LayerInfo& b) {
117 return a.id < b.id;
118 }
119
PrintTo(const LayerInfo & info,::std::ostream * os)120 inline void PrintTo(const LayerInfo& info, ::std::ostream* os) {
121 *os << "Layer [" << info.id << "] name=" << info.name << " parent=" << info.parent
122 << " z=" << info.z << " curr_frame=" << info.curr_frame << " x=" << info.x
123 << " y=" << info.y << " bufferWidth=" << info.bufferWidth
124 << " bufferHeight=" << info.bufferHeight << "touchableRegionBounds={"
125 << info.touchableRegionBounds.left << "," << info.touchableRegionBounds.top << ","
126 << info.touchableRegionBounds.right << "," << info.touchableRegionBounds.bottom << "}";
127 }
128
129 struct find_id {
130 uint64_t id;
find_idandroid::find_id131 find_id(uint64_t id) : id(id) {}
operator ()android::find_id132 bool operator()(LayerInfo const& m) const { return m.id == id; }
133 };
134
getLayerInfoFromProto(perfetto::protos::LayerProto & proto)135 static LayerInfo getLayerInfoFromProto(perfetto::protos::LayerProto& proto) {
136 Rect touchableRegionBounds = Rect::INVALID_RECT;
137 // ignore touchable region for layers without buffers, the new fe aggressively avoids
138 // calculating state for layers that are not visible which could lead to mismatches
139 if (proto.has_input_window_info() && proto.input_window_info().has_touchable_region() &&
140 proto.has_active_buffer()) {
141 Region touchableRegion;
142 LayerProtoHelper::readFromProto(proto.input_window_info().touchable_region(),
143 touchableRegion);
144 touchableRegionBounds = touchableRegion.bounds();
145 }
146
147 return {static_cast<uint64_t>(proto.id()),
148 proto.name(),
149 static_cast<uint64_t>(proto.parent()),
150 proto.z(),
151 proto.curr_frame(),
152 proto.has_position() ? proto.position().x() : -1,
153 proto.has_position() ? proto.position().y() : -1,
154 proto.has_active_buffer() ? proto.active_buffer().width() : 0,
155 proto.has_active_buffer() ? proto.active_buffer().height() : 0,
156 touchableRegionBounds};
157 }
158
getLayerInfosFromProto(perfetto::protos::LayersSnapshotProto & entry)159 static std::vector<LayerInfo> getLayerInfosFromProto(perfetto::protos::LayersSnapshotProto& entry) {
160 std::unordered_map<uint64_t /* snapshotId*/, uint64_t /*layerId*/> snapshotIdToLayerId;
161 std::vector<LayerInfo> layers;
162 layers.reserve(static_cast<size_t>(entry.layers().layers_size()));
163 bool mapSnapshotIdToLayerId = false;
164 for (int i = 0; i < entry.layers().layers_size(); i++) {
165 auto layer = entry.layers().layers(i);
166 LayerInfo layerInfo = getLayerInfoFromProto(layer);
167
168 uint64_t layerId = layerInfo.name.find("(Mirror)") == std::string::npos
169 ? static_cast<uint64_t>(layer.original_id())
170 : static_cast<uint64_t>(layer.original_id()) | 1ull << 63;
171
172 snapshotIdToLayerId[layerInfo.id] = layerId;
173
174 if (layer.original_id() != 0) {
175 mapSnapshotIdToLayerId = true;
176 }
177 layers.push_back(layerInfo);
178 }
179 std::sort(layers.begin(), layers.end(), compareById);
180
181 if (!mapSnapshotIdToLayerId) {
182 return layers;
183 }
184 for (auto& layer : layers) {
185 layer.id = snapshotIdToLayerId[layer.id];
186 auto it = snapshotIdToLayerId.find(layer.parent);
187 layer.parent = it == snapshotIdToLayerId.end() ? static_cast<uint64_t>(-1) : it->second;
188 }
189 return layers;
190 }
191
TEST_P(TransactionTraceTestSuite,validateEndState)192 TEST_P(TransactionTraceTestSuite, validateEndState) {
193 ASSERT_GT(mActualLayersTraceProto.entry_size(), 0);
194 ASSERT_GT(mExpectedLayersTraceProto.entry_size(), 0);
195
196 auto expectedLastEntry =
197 mExpectedLayersTraceProto.entry(mExpectedLayersTraceProto.entry_size() - 1);
198 auto actualLastEntry = mActualLayersTraceProto.entry(mActualLayersTraceProto.entry_size() - 1);
199
200 EXPECT_EQ(expectedLastEntry.layers().layers_size(), actualLastEntry.layers().layers_size());
201
202 std::vector<LayerInfo> expectedLayers = getLayerInfosFromProto(expectedLastEntry);
203 std::vector<LayerInfo> actualLayers = getLayerInfosFromProto(actualLastEntry);
204
205 size_t i = 0;
206 for (; i < actualLayers.size() && i < expectedLayers.size(); i++) {
207 auto it = std::find_if(actualLayers.begin(), actualLayers.end(),
208 find_id(expectedLayers[i].id));
209 EXPECT_NE(it, actualLayers.end());
210 EXPECT_EQ(expectedLayers[i], *it);
211 ALOGV("Validating %s[%" PRIu64 "] parent=%" PRIu64 " z=%d frame=%" PRIu64,
212 expectedLayers[i].name.c_str(), expectedLayers[i].id, expectedLayers[i].parent,
213 expectedLayers[i].z, expectedLayers[i].curr_frame);
214 }
215
216 EXPECT_EQ(expectedLayers.size(), actualLayers.size());
217
218 if (i < actualLayers.size()) {
219 for (size_t j = 0; j < actualLayers.size(); j++) {
220 if (std::find_if(expectedLayers.begin(), expectedLayers.end(),
221 find_id(actualLayers[j].id)) == expectedLayers.end()) {
222 ALOGD("actualLayers [%" PRIu64 "]:%s parent=%" PRIu64 " z=%d frame=%" PRIu64,
223 actualLayers[j].id, actualLayers[j].name.c_str(), actualLayers[j].parent,
224 actualLayers[j].z, actualLayers[j].curr_frame);
225 }
226 }
227 FAIL();
228 }
229
230 if (i < expectedLayers.size()) {
231 for (size_t j = 0; j < expectedLayers.size(); j++) {
232 if (std::find_if(actualLayers.begin(), actualLayers.end(),
233 find_id(expectedLayers[j].id)) == actualLayers.end()) {
234 ALOGD("expectedLayers [%" PRIu64 "]:%s parent=%" PRIu64 " z=%d frame=%" PRIu64,
235 expectedLayers[j].id, expectedLayers[j].name.c_str(),
236 expectedLayers[j].parent, expectedLayers[j].z, expectedLayers[j].curr_frame);
237 }
238 }
239 FAIL();
240 }
241 }
242
PrintToStringParamName(const::testing::TestParamInfo<std::filesystem::path> & info)243 std::string PrintToStringParamName(const ::testing::TestParamInfo<std::filesystem::path>& info) {
244 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
245 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
246
247 const auto& filename = info.param.filename().string();
248 return filename.substr(prefix.length(), filename.length() - prefix.length() - postfix.length());
249 }
250
251 INSTANTIATE_TEST_CASE_P(TransactionTraceTestSuites, TransactionTraceTestSuite,
252 testing::ValuesIn(TransactionTraceTestSuite::sTransactionTraces),
253 PrintToStringParamName);
254
255 } // namespace android
256
main(int argc,char ** argv)257 int main(int argc, char** argv) {
258 for (const auto& entry : std::filesystem::directory_iterator(
259 android::base::GetExecutableDirectory() + "/testdata/")) {
260 if (!entry.is_regular_file()) {
261 continue;
262 }
263 const auto& filename = entry.path().filename().string();
264 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
265 if (filename.compare(0, prefix.length(), prefix)) {
266 continue;
267 }
268 const std::string& path = entry.path().string();
269 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
270 if (path.compare(path.length() - postfix.length(), postfix.length(), postfix)) {
271 continue;
272 }
273 android::TransactionTraceTestSuite::sTransactionTraces.push_back(path);
274 }
275 ::testing::InitGoogleTest(&argc, argv);
276 return RUN_ALL_TESTS();
277 }
278