blob: 3e3d8f0206579dc7bca947b4c0ce6bcece656a50 [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 <LayerTraceGenerator.h>
27#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
43 proto::TransactionTraceFile mTransactionTrace;
44 LayersTraceFileProto mExpectedLayersTraceProto;
45 LayersTraceFileProto mActualLayersTraceProto;
46
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
62 EXPECT_TRUE(
63 LayerTraceGenerator().generate(mTransactionTrace, actualLayersTracePath.c_str()))
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
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
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
86std::vector<std::filesystem::path> TransactionTraceTestSuite::sTransactionTraces{};
87
Vishnu Nair286f4f92022-06-08 16:37:39 -070088struct LayerInfo {
Vishnu Naird0183602023-03-16 18:52:15 +000089 int32_t id;
Vishnu Nair286f4f92022-06-08 16:37:39 -070090 std::string name;
Vishnu Naird0183602023-03-16 18:52:15 +000091 int32_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> {
122 int id;
123 find_id(int id) : id(id) {}
124 bool operator()(LayerInfo const& m) const { return m.id == id; }
125};
126
Vishnu Nair63a662a2023-02-22 20:17:18 +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 Nair63a662a2023-02-22 20:17:18 +0000139 return {proto.id(),
140 proto.name(),
141 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,
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
Vishnu Nair8f371ed2022-02-03 19:15:36 -0800151TEST_P(TransactionTraceTestSuite, validateEndState) {
152 ASSERT_GT(mActualLayersTraceProto.entry_size(), 0);
153 ASSERT_GT(mExpectedLayersTraceProto.entry_size(), 0);
154
155 auto expectedLastEntry =
156 mExpectedLayersTraceProto.entry(mExpectedLayersTraceProto.entry_size() - 1);
157 auto actualLastEntry = mActualLayersTraceProto.entry(mActualLayersTraceProto.entry_size() - 1);
158
159 EXPECT_EQ(expectedLastEntry.layers().layers_size(), actualLastEntry.layers().layers_size());
Vishnu Nair286f4f92022-06-08 16:37:39 -0700160
161 std::vector<LayerInfo> expectedLayers;
162 expectedLayers.reserve(static_cast<size_t>(expectedLastEntry.layers().layers_size()));
163 for (int i = 0; i < expectedLastEntry.layers().layers_size(); i++) {
164 auto layer = expectedLastEntry.layers().layers(i);
Vishnu Naird0183602023-03-16 18:52:15 +0000165 LayerInfo layerInfo = getLayerInfoFromProto(layer);
166 expectedLayers.push_back(layerInfo);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700167 }
168 std::sort(expectedLayers.begin(), expectedLayers.end(), compareById);
169
Vishnu Naird0183602023-03-16 18:52:15 +0000170 std::unordered_map<int32_t /* snapshotId*/, int32_t /*layerId*/> snapshotIdToLayerId;
Vishnu Nair286f4f92022-06-08 16:37:39 -0700171 std::vector<LayerInfo> actualLayers;
172 actualLayers.reserve(static_cast<size_t>(actualLastEntry.layers().layers_size()));
173 for (int i = 0; i < actualLastEntry.layers().layers_size(); i++) {
174 auto layer = actualLastEntry.layers().layers(i);
Vishnu Naird0183602023-03-16 18:52:15 +0000175 LayerInfo layerInfo = getLayerInfoFromProto(layer);
176 snapshotIdToLayerId[layerInfo.id] = static_cast<int32_t>(layer.original_id());
177 actualLayers.push_back(layerInfo);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700178 }
Vishnu Naird0183602023-03-16 18:52:15 +0000179
180 for (auto& layer : actualLayers) {
181 layer.id = snapshotIdToLayerId[layer.id];
182 auto it = snapshotIdToLayerId.find(layer.parent);
183 layer.parent = it == snapshotIdToLayerId.end() ? -1 : it->second;
184 }
185
Vishnu Nair286f4f92022-06-08 16:37:39 -0700186 std::sort(actualLayers.begin(), actualLayers.end(), compareById);
187
188 size_t i = 0;
189 for (; i < actualLayers.size() && i < expectedLayers.size(); i++) {
190 auto it = std::find_if(actualLayers.begin(), actualLayers.end(),
191 find_id(expectedLayers[i].id));
192 EXPECT_NE(it, actualLayers.end());
193 EXPECT_EQ(expectedLayers[i], *it);
194 ALOGV("Validating %s[%d] parent=%d z=%d frame=%" PRIu64, expectedLayers[i].name.c_str(),
195 expectedLayers[i].id, expectedLayers[i].parent, expectedLayers[i].z,
196 expectedLayers[i].curr_frame);
197 }
198
199 EXPECT_EQ(expectedLayers.size(), actualLayers.size());
200
201 if (i < actualLayers.size()) {
202 for (size_t j = 0; j < actualLayers.size(); j++) {
203 if (std::find_if(expectedLayers.begin(), expectedLayers.end(),
204 find_id(actualLayers[j].id)) == expectedLayers.end()) {
205 ALOGD("actualLayers [%d]:%s parent=%d z=%d frame=%" PRIu64, actualLayers[j].id,
206 actualLayers[j].name.c_str(), actualLayers[j].parent, actualLayers[j].z,
207 actualLayers[j].curr_frame);
208 }
209 }
210 FAIL();
211 }
212
213 if (i < expectedLayers.size()) {
214 for (size_t j = 0; j < expectedLayers.size(); j++) {
215 if (std::find_if(actualLayers.begin(), actualLayers.end(),
216 find_id(expectedLayers[j].id)) == actualLayers.end()) {
217 ALOGD("expectedLayers [%d]:%s parent=%d z=%d frame=%" PRIu64, expectedLayers[j].id,
218 expectedLayers[j].name.c_str(), expectedLayers[j].parent, expectedLayers[j].z,
219 expectedLayers[j].curr_frame);
220 }
221 }
222 FAIL();
Vishnu Nair8f371ed2022-02-03 19:15:36 -0800223 }
224}
225
226std::string PrintToStringParamName(const ::testing::TestParamInfo<std::filesystem::path>& info) {
227 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
228 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
229
230 const auto& filename = info.param.filename().string();
231 return filename.substr(prefix.length(), filename.length() - prefix.length() - postfix.length());
232}
233
234INSTANTIATE_TEST_CASE_P(TransactionTraceTestSuites, TransactionTraceTestSuite,
235 testing::ValuesIn(TransactionTraceTestSuite::sTransactionTraces),
236 PrintToStringParamName);
237
238} // namespace android
239
240int main(int argc, char** argv) {
241 for (const auto& entry : std::filesystem::directory_iterator(
242 android::base::GetExecutableDirectory() + "/testdata/")) {
243 if (!entry.is_regular_file()) {
244 continue;
245 }
246 const auto& filename = entry.path().filename().string();
247 const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix;
248 if (filename.compare(0, prefix.length(), prefix)) {
249 continue;
250 }
251 const std::string& path = entry.path().string();
252 const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix;
253 if (path.compare(path.length() - postfix.length(), postfix.length(), postfix)) {
254 continue;
255 }
256 android::TransactionTraceTestSuite::sTransactionTraces.push_back(path);
257 }
258 ::testing::InitGoogleTest(&argc, argv);
259 return RUN_ALL_TESTS();
260}