blob: 7355c35131abda8cae5a384a2cc1a11966072866 [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
25#include <LayerTraceGenerator.h>
26#include <Tracing/TransactionProtoParser.h>
27#include <layerproto/LayerProtoHeader.h>
28#include <log/log.h>
29
30using namespace android::surfaceflinger;
31
32namespace android {
33
34class TransactionTraceTestSuite : public testing::Test,
35 public testing::WithParamInterface<std::filesystem::path> {
36public:
37 static std::vector<std::filesystem::path> sTransactionTraces;
38 static constexpr std::string_view sTransactionTracePrefix = "transactions_trace_";
39 static constexpr std::string_view sLayersTracePrefix = "layers_trace_";
40 static constexpr std::string_view sTracePostfix = ".winscope";
41
42 proto::TransactionTraceFile mTransactionTrace;
43 LayersTraceFileProto mExpectedLayersTraceProto;
44 LayersTraceFileProto mActualLayersTraceProto;
45
46protected:
47 void SetUp() override {
48 std::filesystem::path transactionTracePath = GetParam();
49 parseTransactionTraceFromFile(transactionTracePath.c_str(), mTransactionTrace);
50
51 std::string expectedLayersFilename = std::string(sLayersTracePrefix) +
52 transactionTracePath.filename().string().substr(sTransactionTracePrefix.length());
53 std::string expectedLayersTracePath =
54 transactionTracePath.parent_path().string() + "/" + expectedLayersFilename;
55 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(expectedLayersTracePath)));
56 parseLayersTraceFromFile(expectedLayersTracePath.c_str(), mExpectedLayersTraceProto);
57 TemporaryDir temp_dir;
58 std::string actualLayersTracePath =
59 std::string(temp_dir.path) + "/" + expectedLayersFilename + "_actual";
60
61 EXPECT_TRUE(
62 LayerTraceGenerator().generate(mTransactionTrace, actualLayersTracePath.c_str()))
63 << "Failed to generate layers trace from " << transactionTracePath;
64 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(actualLayersTracePath)));
65 parseLayersTraceFromFile(actualLayersTracePath.c_str(), mActualLayersTraceProto);
66 }
67
68 void parseTransactionTraceFromFile(const char* transactionTracePath,
69 proto::TransactionTraceFile& outProto) {
70 ALOGD("Parsing file %s...", transactionTracePath);
71 std::fstream input(transactionTracePath, std::ios::in | std::ios::binary);
72 EXPECT_TRUE(input) << "Error could not open " << transactionTracePath;
73 EXPECT_TRUE(outProto.ParseFromIstream(&input))
74 << "Failed to parse " << transactionTracePath;
75 }
76
77 void parseLayersTraceFromFile(const char* layersTracePath, LayersTraceFileProto& outProto) {
78 ALOGD("Parsing file %s...", layersTracePath);
79 std::fstream input(layersTracePath, std::ios::in | std::ios::binary);
80 EXPECT_TRUE(input) << "Error could not open " << layersTracePath;
81 EXPECT_TRUE(outProto.ParseFromIstream(&input)) << "Failed to parse " << layersTracePath;
82 }
83};
84
85std::vector<std::filesystem::path> TransactionTraceTestSuite::sTransactionTraces{};
86
Vishnu Nair286f4f92022-06-08 16:37:39 -070087struct LayerInfo {
Vishnu Naird0183602023-03-16 18:52:15 +000088 int32_t id;
Vishnu Nair286f4f92022-06-08 16:37:39 -070089 std::string name;
Vishnu Naird0183602023-03-16 18:52:15 +000090 int32_t parent;
Vishnu Nair286f4f92022-06-08 16:37:39 -070091 int z;
92 uint64_t curr_frame;
93 float x;
94 float y;
Vishnu Nair63a662a2023-02-22 20:17:18 +000095 uint32_t bufferWidth;
96 uint32_t bufferHeight;
Vishnu Nair286f4f92022-06-08 16:37:39 -070097};
98
99bool operator==(const LayerInfo& lh, const LayerInfo& rh) {
100 return std::make_tuple(lh.id, lh.name, lh.parent, lh.z, lh.curr_frame) ==
101 std::make_tuple(rh.id, rh.name, rh.parent, rh.z, rh.curr_frame);
102}
103
104bool compareById(const LayerInfo& a, const LayerInfo& b) {
105 return a.id < b.id;
106}
107
108inline void PrintTo(const LayerInfo& info, ::std::ostream* os) {
109 *os << "Layer [" << info.id << "] name=" << info.name << " parent=" << info.parent
110 << " z=" << info.z << " curr_frame=" << info.curr_frame << " x=" << info.x
Vishnu Nair63a662a2023-02-22 20:17:18 +0000111 << " y=" << info.y << " bufferWidth=" << info.bufferWidth
112 << " bufferHeight=" << info.bufferHeight;
Vishnu Nair286f4f92022-06-08 16:37:39 -0700113}
114
115struct find_id : std::unary_function<LayerInfo, bool> {
116 int id;
117 find_id(int id) : id(id) {}
118 bool operator()(LayerInfo const& m) const { return m.id == id; }
119};
120
Vishnu Nair63a662a2023-02-22 20:17:18 +0000121static LayerInfo getLayerInfoFromProto(::android::surfaceflinger::LayerProto& proto) {
122 return {proto.id(),
123 proto.name(),
124 proto.parent(),
125 proto.z(),
126 proto.curr_frame(),
127 proto.has_position() ? proto.position().x() : -1,
128 proto.has_position() ? proto.position().y() : -1,
129 proto.has_active_buffer() ? proto.active_buffer().width() : 0,
130 proto.has_active_buffer() ? proto.active_buffer().height() : 0};
131}
132
Vishnu Nair8f371ed2022-02-03 19:15:36 -0800133TEST_P(TransactionTraceTestSuite, validateEndState) {
134 ASSERT_GT(mActualLayersTraceProto.entry_size(), 0);
135 ASSERT_GT(mExpectedLayersTraceProto.entry_size(), 0);
136
137 auto expectedLastEntry =
138 mExpectedLayersTraceProto.entry(mExpectedLayersTraceProto.entry_size() - 1);
139 auto actualLastEntry = mActualLayersTraceProto.entry(mActualLayersTraceProto.entry_size() - 1);
140
141 EXPECT_EQ(expectedLastEntry.layers().layers_size(), actualLastEntry.layers().layers_size());
Vishnu Nair286f4f92022-06-08 16:37:39 -0700142
143 std::vector<LayerInfo> expectedLayers;
144 expectedLayers.reserve(static_cast<size_t>(expectedLastEntry.layers().layers_size()));
145 for (int i = 0; i < expectedLastEntry.layers().layers_size(); i++) {
146 auto layer = expectedLastEntry.layers().layers(i);
Vishnu Naird0183602023-03-16 18:52:15 +0000147 LayerInfo layerInfo = getLayerInfoFromProto(layer);
148 expectedLayers.push_back(layerInfo);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700149 }
150 std::sort(expectedLayers.begin(), expectedLayers.end(), compareById);
151
Vishnu Naird0183602023-03-16 18:52:15 +0000152 std::unordered_map<int32_t /* snapshotId*/, int32_t /*layerId*/> snapshotIdToLayerId;
Vishnu Nair286f4f92022-06-08 16:37:39 -0700153 std::vector<LayerInfo> actualLayers;
154 actualLayers.reserve(static_cast<size_t>(actualLastEntry.layers().layers_size()));
155 for (int i = 0; i < actualLastEntry.layers().layers_size(); i++) {
156 auto layer = actualLastEntry.layers().layers(i);
Vishnu Naird0183602023-03-16 18:52:15 +0000157 LayerInfo layerInfo = getLayerInfoFromProto(layer);
158 snapshotIdToLayerId[layerInfo.id] = static_cast<int32_t>(layer.original_id());
159 actualLayers.push_back(layerInfo);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700160 }
Vishnu Naird0183602023-03-16 18:52:15 +0000161
162 for (auto& layer : actualLayers) {
163 layer.id = snapshotIdToLayerId[layer.id];
164 auto it = snapshotIdToLayerId.find(layer.parent);
165 layer.parent = it == snapshotIdToLayerId.end() ? -1 : it->second;
166 }
167
Vishnu Nair286f4f92022-06-08 16:37:39 -0700168 std::sort(actualLayers.begin(), actualLayers.end(), compareById);
169
170 size_t i = 0;
171 for (; i < actualLayers.size() && i < expectedLayers.size(); i++) {
172 auto it = std::find_if(actualLayers.begin(), actualLayers.end(),
173 find_id(expectedLayers[i].id));
174 EXPECT_NE(it, actualLayers.end());
175 EXPECT_EQ(expectedLayers[i], *it);
176 ALOGV("Validating %s[%d] parent=%d z=%d frame=%" PRIu64, expectedLayers[i].name.c_str(),
177 expectedLayers[i].id, expectedLayers[i].parent, expectedLayers[i].z,
178 expectedLayers[i].curr_frame);
179 }
180
181 EXPECT_EQ(expectedLayers.size(), actualLayers.size());
182
183 if (i < actualLayers.size()) {
184 for (size_t j = 0; j < actualLayers.size(); j++) {
185 if (std::find_if(expectedLayers.begin(), expectedLayers.end(),
186 find_id(actualLayers[j].id)) == expectedLayers.end()) {
187 ALOGD("actualLayers [%d]:%s parent=%d z=%d frame=%" PRIu64, actualLayers[j].id,
188 actualLayers[j].name.c_str(), actualLayers[j].parent, actualLayers[j].z,
189 actualLayers[j].curr_frame);
190 }
191 }
192 FAIL();
193 }
194
195 if (i < expectedLayers.size()) {
196 for (size_t j = 0; j < expectedLayers.size(); j++) {
197 if (std::find_if(actualLayers.begin(), actualLayers.end(),
198 find_id(expectedLayers[j].id)) == actualLayers.end()) {
199 ALOGD("expectedLayers [%d]:%s parent=%d z=%d frame=%" PRIu64, expectedLayers[j].id,
200 expectedLayers[j].name.c_str(), expectedLayers[j].parent, expectedLayers[j].z,
201 expectedLayers[j].curr_frame);
202 }
203 }
204 FAIL();
Vishnu Nair8f371ed2022-02-03 19:15:36 -0800205 }
206}
207
208std::string PrintToStringParamName(const ::testing::TestParamInfo<std::filesystem::path>& info) {
209 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
210 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
211
212 const auto& filename = info.param.filename().string();
213 return filename.substr(prefix.length(), filename.length() - prefix.length() - postfix.length());
214}
215
216INSTANTIATE_TEST_CASE_P(TransactionTraceTestSuites, TransactionTraceTestSuite,
217 testing::ValuesIn(TransactionTraceTestSuite::sTransactionTraces),
218 PrintToStringParamName);
219
220} // namespace android
221
222int main(int argc, char** argv) {
223 for (const auto& entry : std::filesystem::directory_iterator(
224 android::base::GetExecutableDirectory() + "/testdata/")) {
225 if (!entry.is_regular_file()) {
226 continue;
227 }
228 const auto& filename = entry.path().filename().string();
229 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
230 if (filename.compare(0, prefix.length(), prefix)) {
231 continue;
232 }
233 const std::string& path = entry.path().string();
234 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
235 if (path.compare(path.length() - postfix.length(), postfix.length(), postfix)) {
236 continue;
237 }
238 android::TransactionTraceTestSuite::sTransactionTraces.push_back(path);
239 }
240 ::testing::InitGoogleTest(&argc, argv);
241 return RUN_ALL_TESTS();
242}