blob: b8a5e79a383afd4c1d8c0285deb9b0f9e49a39ff [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>
Diwas Sharma69758342023-09-01 00:40:19 +000026#include <LayerTraceGenerator.h>
Vishnu Nair8f371ed2022-02-03 19:15:36 -080027#include <Tracing/TransactionProtoParser.h>
28#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
Diwas Sharma69758342023-09-01 00:40:19 +000043 proto::TransactionTraceFile mTransactionTrace;
44 LayersTraceFileProto mExpectedLayersTraceProto;
45 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;
59 std::string actualLayersTracePath =
60 std::string(temp_dir.path) + "/" + expectedLayersFilename + "_actual";
61
Diwas Sharma69758342023-09-01 00:40:19 +000062 EXPECT_TRUE(LayerTraceGenerator().generate(mTransactionTrace, actualLayersTracePath.c_str(),
63 /*onlyLastEntry=*/true))
64 << "Failed to generate layers trace from " << transactionTracePath;
Vishnu Nair8f371ed2022-02-03 19:15:36 -080065 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(actualLayersTracePath)));
66 parseLayersTraceFromFile(actualLayersTracePath.c_str(), mActualLayersTraceProto);
67 }
68
69 void parseTransactionTraceFromFile(const char* transactionTracePath,
Diwas Sharma69758342023-09-01 00:40:19 +000070 proto::TransactionTraceFile& outProto) {
Vishnu Nair8f371ed2022-02-03 19:15:36 -080071 ALOGD("Parsing file %s...", transactionTracePath);
72 std::fstream input(transactionTracePath, std::ios::in | std::ios::binary);
73 EXPECT_TRUE(input) << "Error could not open " << transactionTracePath;
74 EXPECT_TRUE(outProto.ParseFromIstream(&input))
75 << "Failed to parse " << transactionTracePath;
76 }
77
Diwas Sharma69758342023-09-01 00:40:19 +000078 void parseLayersTraceFromFile(const char* layersTracePath, LayersTraceFileProto& outProto) {
Vishnu Nair8f371ed2022-02-03 19:15:36 -080079 ALOGD("Parsing file %s...", layersTracePath);
80 std::fstream input(layersTracePath, std::ios::in | std::ios::binary);
81 EXPECT_TRUE(input) << "Error could not open " << layersTracePath;
82 EXPECT_TRUE(outProto.ParseFromIstream(&input)) << "Failed to parse " << layersTracePath;
83 }
84};
85
86std::vector<std::filesystem::path> TransactionTraceTestSuite::sTransactionTraces{};
87
Vishnu Nair286f4f92022-06-08 16:37:39 -070088struct LayerInfo {
Vishnu Nair52c4f252023-06-14 15:25:12 -070089 uint64_t id;
Vishnu Nair286f4f92022-06-08 16:37:39 -070090 std::string name;
Vishnu Nair52c4f252023-06-14 15:25:12 -070091 uint64_t parent;
Vishnu Nair286f4f92022-06-08 16:37:39 -070092 int z;
93 uint64_t curr_frame;
94 float x;
95 float y;
Vishnu Nair63a662a2023-02-22 20:17:18 +000096 uint32_t bufferWidth;
97 uint32_t bufferHeight;
Vishnu Nair0d13b912023-03-27 22:06:38 +000098 Rect touchableRegionBounds;
Vishnu Nair286f4f92022-06-08 16:37:39 -070099};
100
101bool operator==(const LayerInfo& lh, const LayerInfo& rh) {
Vishnu Nair0d13b912023-03-27 22:06:38 +0000102 return std::make_tuple(lh.id, lh.name, lh.parent, lh.z, lh.curr_frame, lh.bufferWidth,
103 lh.bufferHeight, lh.touchableRegionBounds) ==
104 std::make_tuple(rh.id, rh.name, rh.parent, rh.z, rh.curr_frame, rh.bufferWidth,
105 rh.bufferHeight, rh.touchableRegionBounds);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700106}
107
108bool compareById(const LayerInfo& a, const LayerInfo& b) {
109 return a.id < b.id;
110}
111
112inline void PrintTo(const LayerInfo& info, ::std::ostream* os) {
113 *os << "Layer [" << info.id << "] name=" << info.name << " parent=" << info.parent
114 << " z=" << info.z << " curr_frame=" << info.curr_frame << " x=" << info.x
Vishnu Nair63a662a2023-02-22 20:17:18 +0000115 << " y=" << info.y << " bufferWidth=" << info.bufferWidth
Vishnu Nair0d13b912023-03-27 22:06:38 +0000116 << " bufferHeight=" << info.bufferHeight << "touchableRegionBounds={"
117 << info.touchableRegionBounds.left << "," << info.touchableRegionBounds.top << ","
118 << info.touchableRegionBounds.right << "," << info.touchableRegionBounds.bottom << "}";
Vishnu Nair286f4f92022-06-08 16:37:39 -0700119}
120
121struct find_id : std::unary_function<LayerInfo, bool> {
Vishnu Nair52c4f252023-06-14 15:25:12 -0700122 uint64_t id;
123 find_id(uint64_t id) : id(id) {}
Vishnu Nair286f4f92022-06-08 16:37:39 -0700124 bool operator()(LayerInfo const& m) const { return m.id == id; }
125};
126
Diwas Sharma69758342023-09-01 00:40:19 +0000127static LayerInfo getLayerInfoFromProto(::android::surfaceflinger::LayerProto& proto) {
Vishnu Nair0d13b912023-03-27 22:06:38 +0000128 Rect touchableRegionBounds = Rect::INVALID_RECT;
129 // ignore touchable region for layers without buffers, the new fe aggressively avoids
130 // calculating state for layers that are not visible which could lead to mismatches
131 if (proto.has_input_window_info() && proto.input_window_info().has_touchable_region() &&
132 proto.has_active_buffer()) {
133 Region touchableRegion;
134 LayerProtoHelper::readFromProto(proto.input_window_info().touchable_region(),
135 touchableRegion);
136 touchableRegionBounds = touchableRegion.bounds();
137 }
138
Vishnu Nair52c4f252023-06-14 15:25:12 -0700139 return {static_cast<uint64_t>(proto.id()),
Vishnu Nair63a662a2023-02-22 20:17:18 +0000140 proto.name(),
Vishnu Nair52c4f252023-06-14 15:25:12 -0700141 static_cast<uint64_t>(proto.parent()),
Vishnu Nair63a662a2023-02-22 20:17:18 +0000142 proto.z(),
143 proto.curr_frame(),
144 proto.has_position() ? proto.position().x() : -1,
145 proto.has_position() ? proto.position().y() : -1,
146 proto.has_active_buffer() ? proto.active_buffer().width() : 0,
Vishnu Nair0d13b912023-03-27 22:06:38 +0000147 proto.has_active_buffer() ? proto.active_buffer().height() : 0,
148 touchableRegionBounds};
Vishnu Nair63a662a2023-02-22 20:17:18 +0000149}
150
Diwas Sharma69758342023-09-01 00:40:19 +0000151static std::vector<LayerInfo> getLayerInfosFromProto(
Diwas Sharma2401daf2023-09-01 00:45:24 +0000152 android::surfaceflinger::LayersTraceProto& entry) {
Vishnu Nair52c4f252023-06-14 15:25:12 -0700153 std::unordered_map<uint64_t /* snapshotId*/, uint64_t /*layerId*/> snapshotIdToLayerId;
Vishnu Nair20e1f962023-03-29 15:58:34 -0700154 std::vector<LayerInfo> layers;
155 layers.reserve(static_cast<size_t>(entry.layers().layers_size()));
156 bool mapSnapshotIdToLayerId = false;
157 for (int i = 0; i < entry.layers().layers_size(); i++) {
158 auto layer = entry.layers().layers(i);
159 LayerInfo layerInfo = getLayerInfoFromProto(layer);
160
Vishnu Nair52c4f252023-06-14 15:25:12 -0700161 uint64_t layerId = layerInfo.name.find("(Mirror)") == std::string::npos
162 ? static_cast<uint64_t>(layer.original_id())
163 : static_cast<uint64_t>(layer.original_id()) | 1ull << 63;
164
165 snapshotIdToLayerId[layerInfo.id] = layerId;
166
Vishnu Nair20e1f962023-03-29 15:58:34 -0700167 if (layer.original_id() != 0) {
168 mapSnapshotIdToLayerId = true;
169 }
170 layers.push_back(layerInfo);
171 }
172 std::sort(layers.begin(), layers.end(), compareById);
173
174 if (!mapSnapshotIdToLayerId) {
175 return layers;
176 }
177 for (auto& layer : layers) {
178 layer.id = snapshotIdToLayerId[layer.id];
179 auto it = snapshotIdToLayerId.find(layer.parent);
Vishnu Nair52c4f252023-06-14 15:25:12 -0700180 layer.parent = it == snapshotIdToLayerId.end() ? static_cast<uint64_t>(-1) : it->second;
Vishnu Nair20e1f962023-03-29 15:58:34 -0700181 }
182 return layers;
183}
184
Vishnu Nair8f371ed2022-02-03 19:15:36 -0800185TEST_P(TransactionTraceTestSuite, validateEndState) {
186 ASSERT_GT(mActualLayersTraceProto.entry_size(), 0);
187 ASSERT_GT(mExpectedLayersTraceProto.entry_size(), 0);
188
189 auto expectedLastEntry =
190 mExpectedLayersTraceProto.entry(mExpectedLayersTraceProto.entry_size() - 1);
191 auto actualLastEntry = mActualLayersTraceProto.entry(mActualLayersTraceProto.entry_size() - 1);
192
193 EXPECT_EQ(expectedLastEntry.layers().layers_size(), actualLastEntry.layers().layers_size());
Vishnu Nair286f4f92022-06-08 16:37:39 -0700194
Vishnu Nair20e1f962023-03-29 15:58:34 -0700195 std::vector<LayerInfo> expectedLayers = getLayerInfosFromProto(expectedLastEntry);
196 std::vector<LayerInfo> actualLayers = getLayerInfosFromProto(actualLastEntry);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700197
198 size_t i = 0;
199 for (; i < actualLayers.size() && i < expectedLayers.size(); i++) {
200 auto it = std::find_if(actualLayers.begin(), actualLayers.end(),
201 find_id(expectedLayers[i].id));
202 EXPECT_NE(it, actualLayers.end());
203 EXPECT_EQ(expectedLayers[i], *it);
Vishnu Nair52c4f252023-06-14 15:25:12 -0700204 ALOGV("Validating %s[%" PRIu64 "] parent=%" PRIu64 " z=%d frame=%" PRIu64,
205 expectedLayers[i].name.c_str(), expectedLayers[i].id, expectedLayers[i].parent,
206 expectedLayers[i].z, expectedLayers[i].curr_frame);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700207 }
208
209 EXPECT_EQ(expectedLayers.size(), actualLayers.size());
210
211 if (i < actualLayers.size()) {
212 for (size_t j = 0; j < actualLayers.size(); j++) {
213 if (std::find_if(expectedLayers.begin(), expectedLayers.end(),
214 find_id(actualLayers[j].id)) == expectedLayers.end()) {
Vishnu Nair52c4f252023-06-14 15:25:12 -0700215 ALOGD("actualLayers [%" PRIu64 "]:%s parent=%" PRIu64 " z=%d frame=%" PRIu64,
216 actualLayers[j].id, actualLayers[j].name.c_str(), actualLayers[j].parent,
217 actualLayers[j].z, actualLayers[j].curr_frame);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700218 }
219 }
220 FAIL();
221 }
222
223 if (i < expectedLayers.size()) {
224 for (size_t j = 0; j < expectedLayers.size(); j++) {
225 if (std::find_if(actualLayers.begin(), actualLayers.end(),
226 find_id(expectedLayers[j].id)) == actualLayers.end()) {
Vishnu Nair52c4f252023-06-14 15:25:12 -0700227 ALOGD("expectedLayers [%" PRIu64 "]:%s parent=%" PRIu64 " z=%d frame=%" PRIu64,
228 expectedLayers[j].id, expectedLayers[j].name.c_str(),
229 expectedLayers[j].parent, expectedLayers[j].z, expectedLayers[j].curr_frame);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700230 }
231 }
232 FAIL();
Vishnu Nair8f371ed2022-02-03 19:15:36 -0800233 }
234}
235
236std::string PrintToStringParamName(const ::testing::TestParamInfo<std::filesystem::path>& info) {
237 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
238 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
239
240 const auto& filename = info.param.filename().string();
241 return filename.substr(prefix.length(), filename.length() - prefix.length() - postfix.length());
242}
243
244INSTANTIATE_TEST_CASE_P(TransactionTraceTestSuites, TransactionTraceTestSuite,
245 testing::ValuesIn(TransactionTraceTestSuite::sTransactionTraces),
246 PrintToStringParamName);
247
248} // namespace android
249
250int main(int argc, char** argv) {
251 for (const auto& entry : std::filesystem::directory_iterator(
252 android::base::GetExecutableDirectory() + "/testdata/")) {
253 if (!entry.is_regular_file()) {
254 continue;
255 }
256 const auto& filename = entry.path().filename().string();
257 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
258 if (filename.compare(0, prefix.length(), prefix)) {
259 continue;
260 }
261 const std::string& path = entry.path().string();
262 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
263 if (path.compare(path.length() - postfix.length(), postfix.length(), postfix)) {
264 continue;
265 }
266 android::TransactionTraceTestSuite::sTransactionTraces.push_back(path);
267 }
268 ::testing::InitGoogleTest(&argc, argv);
269 return RUN_ALL_TESTS();
Diwas Sharma2401daf2023-09-01 00:45:24 +0000270}