blob: 7a076347bb758fce6679fbcab18100ab06848b53 [file] [log] [blame]
Vishnu Nair8f371ed2022-02-03 19:15:36 -08001/*
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>
Vishnu Naird0183602023-03-16 18:52:15 +000023#include <unordered_map>
Vishnu Nair8f371ed2022-02-03 19:15:36 -080024
Vishnu Nair0d13b912023-03-27 22:06:38 +000025#include <LayerProtoHelper.h>
Vishnu Nair8f371ed2022-02-03 19:15:36 -080026#include <Tracing/TransactionProtoParser.h>
Kean Mariotti22cbec52023-04-20 12:06:29 +000027#include <Tracing/tools/LayerTraceGenerator.h>
Vishnu Nair8f371ed2022-02-03 19:15:36 -080028#include <layerproto/LayerProtoHeader.h>
29#include <log/log.h>
30
31using namespace android::surfaceflinger;
32
33namespace android {
34
35class TransactionTraceTestSuite : public testing::Test,
36 public testing::WithParamInterface<std::filesystem::path> {
37public:
38 static std::vector<std::filesystem::path> sTransactionTraces;
39 static constexpr std::string_view sTransactionTracePrefix = "transactions_trace_";
40 static constexpr std::string_view sLayersTracePrefix = "layers_trace_";
41 static constexpr std::string_view sTracePostfix = ".winscope";
42
Kean Mariotti22cbec52023-04-20 12:06:29 +000043 perfetto::protos::TransactionTraceFile mTransactionTrace;
44 perfetto::protos::LayersTraceFileProto mExpectedLayersTraceProto;
45 perfetto::protos::LayersTraceFileProto mActualLayersTraceProto;
Vishnu Nair8f371ed2022-02-03 19:15:36 -080046
47protected:
48 void SetUp() override {
49 std::filesystem::path transactionTracePath = GetParam();
50 parseTransactionTraceFromFile(transactionTracePath.c_str(), mTransactionTrace);
51
52 std::string expectedLayersFilename = std::string(sLayersTracePrefix) +
53 transactionTracePath.filename().string().substr(sTransactionTracePrefix.length());
54 std::string expectedLayersTracePath =
55 transactionTracePath.parent_path().string() + "/" + expectedLayersFilename;
56 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(expectedLayersTracePath)));
57 parseLayersTraceFromFile(expectedLayersTracePath.c_str(), mExpectedLayersTraceProto);
58 TemporaryDir temp_dir;
Kean Mariotti22cbec52023-04-20 12:06:29 +000059
Vishnu Nair8f371ed2022-02-03 19:15:36 -080060 std::string actualLayersTracePath =
61 std::string(temp_dir.path) + "/" + expectedLayersFilename + "_actual";
Kean Mariotti22cbec52023-04-20 12:06:29 +000062 {
63 auto traceFlags = LayerTracing::TRACE_INPUT | LayerTracing::TRACE_BUFFERS;
64 std::ofstream outStream{actualLayersTracePath, std::ios::binary | std::ios::app};
65 EXPECT_TRUE(LayerTraceGenerator().generate(mTransactionTrace, traceFlags, outStream,
66 /*onlyLastEntry=*/true))
67 << "Failed to generate layers trace from " << transactionTracePath;
68 }
Vishnu Nair8f371ed2022-02-03 19:15:36 -080069
Vishnu Nair8f371ed2022-02-03 19:15:36 -080070 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(actualLayersTracePath)));
71 parseLayersTraceFromFile(actualLayersTracePath.c_str(), mActualLayersTraceProto);
72 }
73
74 void parseTransactionTraceFromFile(const char* transactionTracePath,
Kean Mariotti22cbec52023-04-20 12:06:29 +000075 perfetto::protos::TransactionTraceFile& outProto) {
Vishnu Nair8f371ed2022-02-03 19:15:36 -080076 ALOGD("Parsing file %s...", transactionTracePath);
77 std::fstream input(transactionTracePath, std::ios::in | std::ios::binary);
78 EXPECT_TRUE(input) << "Error could not open " << transactionTracePath;
79 EXPECT_TRUE(outProto.ParseFromIstream(&input))
80 << "Failed to parse " << transactionTracePath;
81 }
82
Kean Mariotti22cbec52023-04-20 12:06:29 +000083 void parseLayersTraceFromFile(const char* layersTracePath,
84 perfetto::protos::LayersTraceFileProto& outProto) {
Vishnu Nair8f371ed2022-02-03 19:15:36 -080085 ALOGD("Parsing file %s...", layersTracePath);
86 std::fstream input(layersTracePath, std::ios::in | std::ios::binary);
87 EXPECT_TRUE(input) << "Error could not open " << layersTracePath;
88 EXPECT_TRUE(outProto.ParseFromIstream(&input)) << "Failed to parse " << layersTracePath;
89 }
90};
91
92std::vector<std::filesystem::path> TransactionTraceTestSuite::sTransactionTraces{};
93
Vishnu Nair286f4f92022-06-08 16:37:39 -070094struct LayerInfo {
Vishnu Nair52c4f252023-06-14 15:25:12 -070095 uint64_t id;
Vishnu Nair286f4f92022-06-08 16:37:39 -070096 std::string name;
Vishnu Nair52c4f252023-06-14 15:25:12 -070097 uint64_t parent;
Vishnu Nair286f4f92022-06-08 16:37:39 -070098 int z;
99 uint64_t curr_frame;
100 float x;
101 float y;
Vishnu Nair63a662a2023-02-22 20:17:18 +0000102 uint32_t bufferWidth;
103 uint32_t bufferHeight;
Vishnu Nair0d13b912023-03-27 22:06:38 +0000104 Rect touchableRegionBounds;
Vishnu Nair286f4f92022-06-08 16:37:39 -0700105};
106
107bool operator==(const LayerInfo& lh, const LayerInfo& rh) {
Vishnu Nair0d13b912023-03-27 22:06:38 +0000108 return std::make_tuple(lh.id, lh.name, lh.parent, lh.z, lh.curr_frame, lh.bufferWidth,
109 lh.bufferHeight, lh.touchableRegionBounds) ==
110 std::make_tuple(rh.id, rh.name, rh.parent, rh.z, rh.curr_frame, rh.bufferWidth,
111 rh.bufferHeight, rh.touchableRegionBounds);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700112}
113
114bool compareById(const LayerInfo& a, const LayerInfo& b) {
115 return a.id < b.id;
116}
117
118inline void PrintTo(const LayerInfo& info, ::std::ostream* os) {
119 *os << "Layer [" << info.id << "] name=" << info.name << " parent=" << info.parent
120 << " z=" << info.z << " curr_frame=" << info.curr_frame << " x=" << info.x
Vishnu Nair63a662a2023-02-22 20:17:18 +0000121 << " y=" << info.y << " bufferWidth=" << info.bufferWidth
Vishnu Nair0d13b912023-03-27 22:06:38 +0000122 << " bufferHeight=" << info.bufferHeight << "touchableRegionBounds={"
123 << info.touchableRegionBounds.left << "," << info.touchableRegionBounds.top << ","
124 << info.touchableRegionBounds.right << "," << info.touchableRegionBounds.bottom << "}";
Vishnu Nair286f4f92022-06-08 16:37:39 -0700125}
126
127struct find_id : std::unary_function<LayerInfo, bool> {
Vishnu Nair52c4f252023-06-14 15:25:12 -0700128 uint64_t id;
129 find_id(uint64_t id) : id(id) {}
Vishnu Nair286f4f92022-06-08 16:37:39 -0700130 bool operator()(LayerInfo const& m) const { return m.id == id; }
131};
132
Kean Mariotti22cbec52023-04-20 12:06:29 +0000133static LayerInfo getLayerInfoFromProto(perfetto::protos::LayerProto& proto) {
Vishnu Nair0d13b912023-03-27 22:06:38 +0000134 Rect touchableRegionBounds = Rect::INVALID_RECT;
135 // ignore touchable region for layers without buffers, the new fe aggressively avoids
136 // calculating state for layers that are not visible which could lead to mismatches
137 if (proto.has_input_window_info() && proto.input_window_info().has_touchable_region() &&
138 proto.has_active_buffer()) {
139 Region touchableRegion;
140 LayerProtoHelper::readFromProto(proto.input_window_info().touchable_region(),
141 touchableRegion);
142 touchableRegionBounds = touchableRegion.bounds();
143 }
144
Vishnu Nair52c4f252023-06-14 15:25:12 -0700145 return {static_cast<uint64_t>(proto.id()),
Vishnu Nair63a662a2023-02-22 20:17:18 +0000146 proto.name(),
Vishnu Nair52c4f252023-06-14 15:25:12 -0700147 static_cast<uint64_t>(proto.parent()),
Vishnu Nair63a662a2023-02-22 20:17:18 +0000148 proto.z(),
149 proto.curr_frame(),
150 proto.has_position() ? proto.position().x() : -1,
151 proto.has_position() ? proto.position().y() : -1,
152 proto.has_active_buffer() ? proto.active_buffer().width() : 0,
Vishnu Nair0d13b912023-03-27 22:06:38 +0000153 proto.has_active_buffer() ? proto.active_buffer().height() : 0,
154 touchableRegionBounds};
Vishnu Nair63a662a2023-02-22 20:17:18 +0000155}
156
Kean Mariotti22cbec52023-04-20 12:06:29 +0000157static std::vector<LayerInfo> getLayerInfosFromProto(perfetto::protos::LayersSnapshotProto& entry) {
Vishnu Nair52c4f252023-06-14 15:25:12 -0700158 std::unordered_map<uint64_t /* snapshotId*/, uint64_t /*layerId*/> snapshotIdToLayerId;
Vishnu Nair20e1f962023-03-29 15:58:34 -0700159 std::vector<LayerInfo> layers;
160 layers.reserve(static_cast<size_t>(entry.layers().layers_size()));
161 bool mapSnapshotIdToLayerId = false;
162 for (int i = 0; i < entry.layers().layers_size(); i++) {
163 auto layer = entry.layers().layers(i);
164 LayerInfo layerInfo = getLayerInfoFromProto(layer);
165
Vishnu Nair52c4f252023-06-14 15:25:12 -0700166 uint64_t layerId = layerInfo.name.find("(Mirror)") == std::string::npos
167 ? static_cast<uint64_t>(layer.original_id())
168 : static_cast<uint64_t>(layer.original_id()) | 1ull << 63;
169
170 snapshotIdToLayerId[layerInfo.id] = layerId;
171
Vishnu Nair20e1f962023-03-29 15:58:34 -0700172 if (layer.original_id() != 0) {
173 mapSnapshotIdToLayerId = true;
174 }
175 layers.push_back(layerInfo);
176 }
177 std::sort(layers.begin(), layers.end(), compareById);
178
179 if (!mapSnapshotIdToLayerId) {
180 return layers;
181 }
182 for (auto& layer : layers) {
183 layer.id = snapshotIdToLayerId[layer.id];
184 auto it = snapshotIdToLayerId.find(layer.parent);
Vishnu Nair52c4f252023-06-14 15:25:12 -0700185 layer.parent = it == snapshotIdToLayerId.end() ? static_cast<uint64_t>(-1) : it->second;
Vishnu Nair20e1f962023-03-29 15:58:34 -0700186 }
187 return layers;
188}
189
Vishnu Nair8f371ed2022-02-03 19:15:36 -0800190TEST_P(TransactionTraceTestSuite, validateEndState) {
191 ASSERT_GT(mActualLayersTraceProto.entry_size(), 0);
192 ASSERT_GT(mExpectedLayersTraceProto.entry_size(), 0);
193
194 auto expectedLastEntry =
195 mExpectedLayersTraceProto.entry(mExpectedLayersTraceProto.entry_size() - 1);
196 auto actualLastEntry = mActualLayersTraceProto.entry(mActualLayersTraceProto.entry_size() - 1);
197
198 EXPECT_EQ(expectedLastEntry.layers().layers_size(), actualLastEntry.layers().layers_size());
Vishnu Nair286f4f92022-06-08 16:37:39 -0700199
Vishnu Nair20e1f962023-03-29 15:58:34 -0700200 std::vector<LayerInfo> expectedLayers = getLayerInfosFromProto(expectedLastEntry);
201 std::vector<LayerInfo> actualLayers = getLayerInfosFromProto(actualLastEntry);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700202
203 size_t i = 0;
204 for (; i < actualLayers.size() && i < expectedLayers.size(); i++) {
205 auto it = std::find_if(actualLayers.begin(), actualLayers.end(),
206 find_id(expectedLayers[i].id));
207 EXPECT_NE(it, actualLayers.end());
208 EXPECT_EQ(expectedLayers[i], *it);
Vishnu Nair52c4f252023-06-14 15:25:12 -0700209 ALOGV("Validating %s[%" PRIu64 "] parent=%" PRIu64 " z=%d frame=%" PRIu64,
210 expectedLayers[i].name.c_str(), expectedLayers[i].id, expectedLayers[i].parent,
211 expectedLayers[i].z, expectedLayers[i].curr_frame);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700212 }
213
214 EXPECT_EQ(expectedLayers.size(), actualLayers.size());
215
216 if (i < actualLayers.size()) {
217 for (size_t j = 0; j < actualLayers.size(); j++) {
218 if (std::find_if(expectedLayers.begin(), expectedLayers.end(),
219 find_id(actualLayers[j].id)) == expectedLayers.end()) {
Vishnu Nair52c4f252023-06-14 15:25:12 -0700220 ALOGD("actualLayers [%" PRIu64 "]:%s parent=%" PRIu64 " z=%d frame=%" PRIu64,
221 actualLayers[j].id, actualLayers[j].name.c_str(), actualLayers[j].parent,
222 actualLayers[j].z, actualLayers[j].curr_frame);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700223 }
224 }
225 FAIL();
226 }
227
228 if (i < expectedLayers.size()) {
229 for (size_t j = 0; j < expectedLayers.size(); j++) {
230 if (std::find_if(actualLayers.begin(), actualLayers.end(),
231 find_id(expectedLayers[j].id)) == actualLayers.end()) {
Vishnu Nair52c4f252023-06-14 15:25:12 -0700232 ALOGD("expectedLayers [%" PRIu64 "]:%s parent=%" PRIu64 " z=%d frame=%" PRIu64,
233 expectedLayers[j].id, expectedLayers[j].name.c_str(),
234 expectedLayers[j].parent, expectedLayers[j].z, expectedLayers[j].curr_frame);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700235 }
236 }
237 FAIL();
Vishnu Nair8f371ed2022-02-03 19:15:36 -0800238 }
239}
240
241std::string PrintToStringParamName(const ::testing::TestParamInfo<std::filesystem::path>& info) {
242 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
243 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
244
245 const auto& filename = info.param.filename().string();
246 return filename.substr(prefix.length(), filename.length() - prefix.length() - postfix.length());
247}
248
249INSTANTIATE_TEST_CASE_P(TransactionTraceTestSuites, TransactionTraceTestSuite,
250 testing::ValuesIn(TransactionTraceTestSuite::sTransactionTraces),
251 PrintToStringParamName);
252
253} // namespace android
254
255int main(int argc, char** argv) {
256 for (const auto& entry : std::filesystem::directory_iterator(
257 android::base::GetExecutableDirectory() + "/testdata/")) {
258 if (!entry.is_regular_file()) {
259 continue;
260 }
261 const auto& filename = entry.path().filename().string();
262 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
263 if (filename.compare(0, prefix.length(), prefix)) {
264 continue;
265 }
266 const std::string& path = entry.path().string();
267 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
268 if (path.compare(path.length() - postfix.length(), postfix.length(), postfix)) {
269 continue;
270 }
271 android::TransactionTraceTestSuite::sTransactionTraces.push_back(path);
272 }
273 ::testing::InitGoogleTest(&argc, argv);
274 return RUN_ALL_TESTS();
Kean Mariottibec51fd2023-04-19 13:31:02 +0000275}