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 <LayerTraceGenerator.h>
27 #include <Tracing/TransactionProtoParser.h>
28 #include <layerproto/LayerProtoHeader.h>
29 #include <log/log.h>
30
31 using namespace android::surfaceflinger;
32
33 namespace android {
34
35 class TransactionTraceTestSuite : public testing::Test,
36 public testing::WithParamInterface<std::filesystem::path> {
37 public:
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
43 proto::TransactionTraceFile mTransactionTrace;
44 LayersTraceFileProto mExpectedLayersTraceProto;
45 LayersTraceFileProto mActualLayersTraceProto;
46
47 protected:
SetUp()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
62 EXPECT_TRUE(LayerTraceGenerator().generate(mTransactionTrace, actualLayersTracePath.c_str(),
63 /*onlyLastEntry=*/true))
64 << "Failed to generate layers trace from " << transactionTracePath;
65 EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(actualLayersTracePath)));
66 parseLayersTraceFromFile(actualLayersTracePath.c_str(), mActualLayersTraceProto);
67 }
68
parseTransactionTraceFromFile(const char * transactionTracePath,proto::TransactionTraceFile & outProto)69 void parseTransactionTraceFromFile(const char* transactionTracePath,
70 proto::TransactionTraceFile& outProto) {
71 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
parseLayersTraceFromFile(const char * layersTracePath,LayersTraceFileProto & outProto)78 void parseLayersTraceFromFile(const char* layersTracePath, LayersTraceFileProto& outProto) {
79 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
86 std::vector<std::filesystem::path> TransactionTraceTestSuite::sTransactionTraces{};
87
88 struct LayerInfo {
89 uint64_t id;
90 std::string name;
91 uint64_t parent;
92 int z;
93 uint64_t curr_frame;
94 float x;
95 float y;
96 uint32_t bufferWidth;
97 uint32_t bufferHeight;
98 Rect touchableRegionBounds;
99 };
100
operator ==(const LayerInfo & lh,const LayerInfo & rh)101 bool operator==(const LayerInfo& lh, const LayerInfo& rh) {
102 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);
106 }
107
compareById(const LayerInfo & a,const LayerInfo & b)108 bool compareById(const LayerInfo& a, const LayerInfo& b) {
109 return a.id < b.id;
110 }
111
PrintTo(const LayerInfo & info,::std::ostream * os)112 inline 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
115 << " y=" << info.y << " bufferWidth=" << info.bufferWidth
116 << " bufferHeight=" << info.bufferHeight << "touchableRegionBounds={"
117 << info.touchableRegionBounds.left << "," << info.touchableRegionBounds.top << ","
118 << info.touchableRegionBounds.right << "," << info.touchableRegionBounds.bottom << "}";
119 }
120
121 struct find_id : std::unary_function<LayerInfo, bool> {
122 uint64_t id;
find_idandroid::find_id123 find_id(uint64_t id) : id(id) {}
operator ()android::find_id124 bool operator()(LayerInfo const& m) const { return m.id == id; }
125 };
126
getLayerInfoFromProto(::android::surfaceflinger::LayerProto & proto)127 static LayerInfo getLayerInfoFromProto(::android::surfaceflinger::LayerProto& proto) {
128 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
139 return {static_cast<uint64_t>(proto.id()),
140 proto.name(),
141 static_cast<uint64_t>(proto.parent()),
142 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,
147 proto.has_active_buffer() ? proto.active_buffer().height() : 0,
148 touchableRegionBounds};
149 }
150
getLayerInfosFromProto(android::surfaceflinger::LayersTraceProto & entry)151 static std::vector<LayerInfo> getLayerInfosFromProto(
152 android::surfaceflinger::LayersTraceProto& entry) {
153 std::unordered_map<uint64_t /* snapshotId*/, uint64_t /*layerId*/> snapshotIdToLayerId;
154 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
161 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
167 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);
180 layer.parent = it == snapshotIdToLayerId.end() ? static_cast<uint64_t>(-1) : it->second;
181 }
182 return layers;
183 }
184
TEST_P(TransactionTraceTestSuite,validateEndState)185 TEST_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());
194
195 std::vector<LayerInfo> expectedLayers = getLayerInfosFromProto(expectedLastEntry);
196 std::vector<LayerInfo> actualLayers = getLayerInfosFromProto(actualLastEntry);
197
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);
204 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);
207 }
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()) {
215 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);
218 }
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()) {
227 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);
230 }
231 }
232 FAIL();
233 }
234 }
235
PrintToStringParamName(const::testing::TestParamInfo<std::filesystem::path> & info)236 std::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
244 INSTANTIATE_TEST_CASE_P(TransactionTraceTestSuites, TransactionTraceTestSuite,
245 testing::ValuesIn(TransactionTraceTestSuite::sTransactionTraces),
246 PrintToStringParamName);
247
248 } // namespace android
249
main(int argc,char ** argv)250 int 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();
270 }