blob: 0e214af7065780ef2cb871338b63d4b53f14c411 [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>
23
24#include <LayerTraceGenerator.h>
25#include <Tracing/TransactionProtoParser.h>
26#include <layerproto/LayerProtoHeader.h>
27#include <log/log.h>
28
29using namespace android::surfaceflinger;
30
31namespace android {
32
33class TransactionTraceTestSuite : public testing::Test,
34 public testing::WithParamInterface<std::filesystem::path> {
35public:
36 static std::vector<std::filesystem::path> sTransactionTraces;
37 static constexpr std::string_view sTransactionTracePrefix = "transactions_trace_";
38 static constexpr std::string_view sLayersTracePrefix = "layers_trace_";
39 static constexpr std::string_view sTracePostfix = ".winscope";
40
41 proto::TransactionTraceFile mTransactionTrace;
42 LayersTraceFileProto mExpectedLayersTraceProto;
43 LayersTraceFileProto mActualLayersTraceProto;
44
45protected:
46 void SetUp() override {
47 std::filesystem::path transactionTracePath = GetParam();
48 parseTransactionTraceFromFile(transactionTracePath.c_str(), mTransactionTrace);
49
50 std::string expectedLayersFilename = std::string(sLayersTracePrefix) +
51 transactionTracePath.filename().string().substr(sTransactionTracePrefix.length());
52 std::string expectedLayersTracePath =
53 transactionTracePath.parent_path().string() + "/" + expectedLayersFilename;
54 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(expectedLayersTracePath)));
55 parseLayersTraceFromFile(expectedLayersTracePath.c_str(), mExpectedLayersTraceProto);
56 TemporaryDir temp_dir;
57 std::string actualLayersTracePath =
58 std::string(temp_dir.path) + "/" + expectedLayersFilename + "_actual";
59
60 EXPECT_TRUE(
61 LayerTraceGenerator().generate(mTransactionTrace, actualLayersTracePath.c_str()))
62 << "Failed to generate layers trace from " << transactionTracePath;
63 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(actualLayersTracePath)));
64 parseLayersTraceFromFile(actualLayersTracePath.c_str(), mActualLayersTraceProto);
65 }
66
67 void parseTransactionTraceFromFile(const char* transactionTracePath,
68 proto::TransactionTraceFile& outProto) {
69 ALOGD("Parsing file %s...", transactionTracePath);
70 std::fstream input(transactionTracePath, std::ios::in | std::ios::binary);
71 EXPECT_TRUE(input) << "Error could not open " << transactionTracePath;
72 EXPECT_TRUE(outProto.ParseFromIstream(&input))
73 << "Failed to parse " << transactionTracePath;
74 }
75
76 void parseLayersTraceFromFile(const char* layersTracePath, LayersTraceFileProto& outProto) {
77 ALOGD("Parsing file %s...", layersTracePath);
78 std::fstream input(layersTracePath, std::ios::in | std::ios::binary);
79 EXPECT_TRUE(input) << "Error could not open " << layersTracePath;
80 EXPECT_TRUE(outProto.ParseFromIstream(&input)) << "Failed to parse " << layersTracePath;
81 }
82};
83
84std::vector<std::filesystem::path> TransactionTraceTestSuite::sTransactionTraces{};
85
Vishnu Nair286f4f92022-06-08 16:37:39 -070086struct LayerInfo {
87 int id;
88 std::string name;
89 int parent;
90 int z;
91 uint64_t curr_frame;
92 float x;
93 float y;
94};
95
96bool operator==(const LayerInfo& lh, const LayerInfo& rh) {
97 return std::make_tuple(lh.id, lh.name, lh.parent, lh.z, lh.curr_frame) ==
98 std::make_tuple(rh.id, rh.name, rh.parent, rh.z, rh.curr_frame);
99}
100
101bool compareById(const LayerInfo& a, const LayerInfo& b) {
102 return a.id < b.id;
103}
104
105inline void PrintTo(const LayerInfo& info, ::std::ostream* os) {
106 *os << "Layer [" << info.id << "] name=" << info.name << " parent=" << info.parent
107 << " z=" << info.z << " curr_frame=" << info.curr_frame << " x=" << info.x
108 << " y=" << info.y;
109}
110
111struct find_id : std::unary_function<LayerInfo, bool> {
112 int id;
113 find_id(int id) : id(id) {}
114 bool operator()(LayerInfo const& m) const { return m.id == id; }
115};
116
Vishnu Nair8f371ed2022-02-03 19:15:36 -0800117TEST_P(TransactionTraceTestSuite, validateEndState) {
118 ASSERT_GT(mActualLayersTraceProto.entry_size(), 0);
119 ASSERT_GT(mExpectedLayersTraceProto.entry_size(), 0);
120
121 auto expectedLastEntry =
122 mExpectedLayersTraceProto.entry(mExpectedLayersTraceProto.entry_size() - 1);
123 auto actualLastEntry = mActualLayersTraceProto.entry(mActualLayersTraceProto.entry_size() - 1);
124
125 EXPECT_EQ(expectedLastEntry.layers().layers_size(), actualLastEntry.layers().layers_size());
Vishnu Nair286f4f92022-06-08 16:37:39 -0700126
127 std::vector<LayerInfo> expectedLayers;
128 expectedLayers.reserve(static_cast<size_t>(expectedLastEntry.layers().layers_size()));
129 for (int i = 0; i < expectedLastEntry.layers().layers_size(); i++) {
130 auto layer = expectedLastEntry.layers().layers(i);
131 expectedLayers.push_back({layer.id(), layer.name(), layer.parent(), layer.z(),
132 layer.curr_frame(),
133 layer.has_position() ? layer.position().x() : -1,
134 layer.has_position() ? layer.position().y() : -1});
135 }
136 std::sort(expectedLayers.begin(), expectedLayers.end(), compareById);
137
138 std::vector<LayerInfo> actualLayers;
139 actualLayers.reserve(static_cast<size_t>(actualLastEntry.layers().layers_size()));
140 for (int i = 0; i < actualLastEntry.layers().layers_size(); i++) {
141 auto layer = actualLastEntry.layers().layers(i);
142 actualLayers.push_back({layer.id(), layer.name(), layer.parent(), layer.z(),
143 layer.curr_frame(),
144 layer.has_position() ? layer.position().x() : -1,
145 layer.has_position() ? layer.position().y() : -1});
146 }
147 std::sort(actualLayers.begin(), actualLayers.end(), compareById);
148
149 size_t i = 0;
150 for (; i < actualLayers.size() && i < expectedLayers.size(); i++) {
151 auto it = std::find_if(actualLayers.begin(), actualLayers.end(),
152 find_id(expectedLayers[i].id));
153 EXPECT_NE(it, actualLayers.end());
154 EXPECT_EQ(expectedLayers[i], *it);
155 ALOGV("Validating %s[%d] parent=%d z=%d frame=%" PRIu64, expectedLayers[i].name.c_str(),
156 expectedLayers[i].id, expectedLayers[i].parent, expectedLayers[i].z,
157 expectedLayers[i].curr_frame);
158 }
159
160 EXPECT_EQ(expectedLayers.size(), actualLayers.size());
161
162 if (i < actualLayers.size()) {
163 for (size_t j = 0; j < actualLayers.size(); j++) {
164 if (std::find_if(expectedLayers.begin(), expectedLayers.end(),
165 find_id(actualLayers[j].id)) == expectedLayers.end()) {
166 ALOGD("actualLayers [%d]:%s parent=%d z=%d frame=%" PRIu64, actualLayers[j].id,
167 actualLayers[j].name.c_str(), actualLayers[j].parent, actualLayers[j].z,
168 actualLayers[j].curr_frame);
169 }
170 }
171 FAIL();
172 }
173
174 if (i < expectedLayers.size()) {
175 for (size_t j = 0; j < expectedLayers.size(); j++) {
176 if (std::find_if(actualLayers.begin(), actualLayers.end(),
177 find_id(expectedLayers[j].id)) == actualLayers.end()) {
178 ALOGD("expectedLayers [%d]:%s parent=%d z=%d frame=%" PRIu64, expectedLayers[j].id,
179 expectedLayers[j].name.c_str(), expectedLayers[j].parent, expectedLayers[j].z,
180 expectedLayers[j].curr_frame);
181 }
182 }
183 FAIL();
Vishnu Nair8f371ed2022-02-03 19:15:36 -0800184 }
185}
186
187std::string PrintToStringParamName(const ::testing::TestParamInfo<std::filesystem::path>& info) {
188 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
189 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
190
191 const auto& filename = info.param.filename().string();
192 return filename.substr(prefix.length(), filename.length() - prefix.length() - postfix.length());
193}
194
195INSTANTIATE_TEST_CASE_P(TransactionTraceTestSuites, TransactionTraceTestSuite,
196 testing::ValuesIn(TransactionTraceTestSuite::sTransactionTraces),
197 PrintToStringParamName);
198
199} // namespace android
200
201int main(int argc, char** argv) {
202 for (const auto& entry : std::filesystem::directory_iterator(
203 android::base::GetExecutableDirectory() + "/testdata/")) {
204 if (!entry.is_regular_file()) {
205 continue;
206 }
207 const auto& filename = entry.path().filename().string();
208 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
209 if (filename.compare(0, prefix.length(), prefix)) {
210 continue;
211 }
212 const std::string& path = entry.path().string();
213 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
214 if (path.compare(path.length() - postfix.length(), postfix.length(), postfix)) {
215 continue;
216 }
217 android::TransactionTraceTestSuite::sTransactionTraces.push_back(path);
218 }
219 ::testing::InitGoogleTest(&argc, argv);
220 return RUN_ALL_TESTS();
221}