Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 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> |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 23 | #include <unordered_map> |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 24 | |
Vishnu Nair | 0d13b91 | 2023-03-27 22:06:38 +0000 | [diff] [blame] | 25 | #include <LayerProtoHelper.h> |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 26 | #include <Tracing/TransactionProtoParser.h> |
Kean Mariotti | 22cbec5 | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 27 | #include <Tracing/tools/LayerTraceGenerator.h> |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 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 | |
Kean Mariotti | 22cbec5 | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 43 | perfetto::protos::TransactionTraceFile mTransactionTrace; |
| 44 | perfetto::protos::LayersTraceFileProto mExpectedLayersTraceProto; |
| 45 | perfetto::protos::LayersTraceFileProto mActualLayersTraceProto; |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 46 | |
| 47 | protected: |
| 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; |
Kean Mariotti | 22cbec5 | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 59 | |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 60 | std::string actualLayersTracePath = |
| 61 | std::string(temp_dir.path) + "/" + expectedLayersFilename + "_actual"; |
Kean Mariotti | 22cbec5 | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 62 | { |
| 63 | auto traceFlags = LayerTracing::TRACE_INPUT | LayerTracing::TRACE_BUFFERS; |
| 64 | std::ofstream outStream{actualLayersTracePath, std::ios::binary | std::ios::app}; |
| 65 | EXPECT_TRUE(LayerTraceGenerator().generate(mTransactionTrace, traceFlags, outStream, |
| 66 | /*onlyLastEntry=*/true)) |
| 67 | << "Failed to generate layers trace from " << transactionTracePath; |
| 68 | } |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 69 | |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 70 | EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(actualLayersTracePath))); |
| 71 | parseLayersTraceFromFile(actualLayersTracePath.c_str(), mActualLayersTraceProto); |
| 72 | } |
| 73 | |
| 74 | void parseTransactionTraceFromFile(const char* transactionTracePath, |
Kean Mariotti | 22cbec5 | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 75 | perfetto::protos::TransactionTraceFile& outProto) { |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 76 | ALOGD("Parsing file %s...", transactionTracePath); |
| 77 | std::fstream input(transactionTracePath, std::ios::in | std::ios::binary); |
| 78 | EXPECT_TRUE(input) << "Error could not open " << transactionTracePath; |
| 79 | EXPECT_TRUE(outProto.ParseFromIstream(&input)) |
| 80 | << "Failed to parse " << transactionTracePath; |
| 81 | } |
| 82 | |
Kean Mariotti | 22cbec5 | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 83 | void parseLayersTraceFromFile(const char* layersTracePath, |
| 84 | perfetto::protos::LayersTraceFileProto& outProto) { |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 85 | ALOGD("Parsing file %s...", layersTracePath); |
| 86 | std::fstream input(layersTracePath, std::ios::in | std::ios::binary); |
| 87 | EXPECT_TRUE(input) << "Error could not open " << layersTracePath; |
| 88 | EXPECT_TRUE(outProto.ParseFromIstream(&input)) << "Failed to parse " << layersTracePath; |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | std::vector<std::filesystem::path> TransactionTraceTestSuite::sTransactionTraces{}; |
| 93 | |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 94 | struct LayerInfo { |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 95 | uint64_t id; |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 96 | std::string name; |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 97 | uint64_t parent; |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 98 | int z; |
| 99 | uint64_t curr_frame; |
| 100 | float x; |
| 101 | float y; |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame] | 102 | uint32_t bufferWidth; |
| 103 | uint32_t bufferHeight; |
Vishnu Nair | 0d13b91 | 2023-03-27 22:06:38 +0000 | [diff] [blame] | 104 | Rect touchableRegionBounds; |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | bool operator==(const LayerInfo& lh, const LayerInfo& rh) { |
Vishnu Nair | 0d13b91 | 2023-03-27 22:06:38 +0000 | [diff] [blame] | 108 | return std::make_tuple(lh.id, lh.name, lh.parent, lh.z, lh.curr_frame, lh.bufferWidth, |
| 109 | lh.bufferHeight, lh.touchableRegionBounds) == |
| 110 | std::make_tuple(rh.id, rh.name, rh.parent, rh.z, rh.curr_frame, rh.bufferWidth, |
| 111 | rh.bufferHeight, rh.touchableRegionBounds); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | bool compareById(const LayerInfo& a, const LayerInfo& b) { |
| 115 | return a.id < b.id; |
| 116 | } |
| 117 | |
| 118 | inline void PrintTo(const LayerInfo& info, ::std::ostream* os) { |
| 119 | *os << "Layer [" << info.id << "] name=" << info.name << " parent=" << info.parent |
| 120 | << " z=" << info.z << " curr_frame=" << info.curr_frame << " x=" << info.x |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame] | 121 | << " y=" << info.y << " bufferWidth=" << info.bufferWidth |
Vishnu Nair | 0d13b91 | 2023-03-27 22:06:38 +0000 | [diff] [blame] | 122 | << " bufferHeight=" << info.bufferHeight << "touchableRegionBounds={" |
| 123 | << info.touchableRegionBounds.left << "," << info.touchableRegionBounds.top << "," |
| 124 | << info.touchableRegionBounds.right << "," << info.touchableRegionBounds.bottom << "}"; |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | struct find_id : std::unary_function<LayerInfo, bool> { |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 128 | uint64_t id; |
| 129 | find_id(uint64_t id) : id(id) {} |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 130 | bool operator()(LayerInfo const& m) const { return m.id == id; } |
| 131 | }; |
| 132 | |
Kean Mariotti | 22cbec5 | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 133 | static LayerInfo getLayerInfoFromProto(perfetto::protos::LayerProto& proto) { |
Vishnu Nair | 0d13b91 | 2023-03-27 22:06:38 +0000 | [diff] [blame] | 134 | Rect touchableRegionBounds = Rect::INVALID_RECT; |
| 135 | // ignore touchable region for layers without buffers, the new fe aggressively avoids |
| 136 | // calculating state for layers that are not visible which could lead to mismatches |
| 137 | if (proto.has_input_window_info() && proto.input_window_info().has_touchable_region() && |
| 138 | proto.has_active_buffer()) { |
| 139 | Region touchableRegion; |
| 140 | LayerProtoHelper::readFromProto(proto.input_window_info().touchable_region(), |
| 141 | touchableRegion); |
| 142 | touchableRegionBounds = touchableRegion.bounds(); |
| 143 | } |
| 144 | |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 145 | return {static_cast<uint64_t>(proto.id()), |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame] | 146 | proto.name(), |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 147 | static_cast<uint64_t>(proto.parent()), |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame] | 148 | proto.z(), |
| 149 | proto.curr_frame(), |
| 150 | proto.has_position() ? proto.position().x() : -1, |
| 151 | proto.has_position() ? proto.position().y() : -1, |
| 152 | proto.has_active_buffer() ? proto.active_buffer().width() : 0, |
Vishnu Nair | 0d13b91 | 2023-03-27 22:06:38 +0000 | [diff] [blame] | 153 | proto.has_active_buffer() ? proto.active_buffer().height() : 0, |
| 154 | touchableRegionBounds}; |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Kean Mariotti | 22cbec5 | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 157 | static std::vector<LayerInfo> getLayerInfosFromProto(perfetto::protos::LayersSnapshotProto& entry) { |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 158 | std::unordered_map<uint64_t /* snapshotId*/, uint64_t /*layerId*/> snapshotIdToLayerId; |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 159 | std::vector<LayerInfo> layers; |
| 160 | layers.reserve(static_cast<size_t>(entry.layers().layers_size())); |
| 161 | bool mapSnapshotIdToLayerId = false; |
| 162 | for (int i = 0; i < entry.layers().layers_size(); i++) { |
| 163 | auto layer = entry.layers().layers(i); |
| 164 | LayerInfo layerInfo = getLayerInfoFromProto(layer); |
| 165 | |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 166 | uint64_t layerId = layerInfo.name.find("(Mirror)") == std::string::npos |
| 167 | ? static_cast<uint64_t>(layer.original_id()) |
| 168 | : static_cast<uint64_t>(layer.original_id()) | 1ull << 63; |
| 169 | |
| 170 | snapshotIdToLayerId[layerInfo.id] = layerId; |
| 171 | |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 172 | if (layer.original_id() != 0) { |
| 173 | mapSnapshotIdToLayerId = true; |
| 174 | } |
| 175 | layers.push_back(layerInfo); |
| 176 | } |
| 177 | std::sort(layers.begin(), layers.end(), compareById); |
| 178 | |
| 179 | if (!mapSnapshotIdToLayerId) { |
| 180 | return layers; |
| 181 | } |
| 182 | for (auto& layer : layers) { |
| 183 | layer.id = snapshotIdToLayerId[layer.id]; |
| 184 | auto it = snapshotIdToLayerId.find(layer.parent); |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 185 | layer.parent = it == snapshotIdToLayerId.end() ? static_cast<uint64_t>(-1) : it->second; |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 186 | } |
| 187 | return layers; |
| 188 | } |
| 189 | |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 190 | TEST_P(TransactionTraceTestSuite, validateEndState) { |
| 191 | ASSERT_GT(mActualLayersTraceProto.entry_size(), 0); |
| 192 | ASSERT_GT(mExpectedLayersTraceProto.entry_size(), 0); |
| 193 | |
| 194 | auto expectedLastEntry = |
| 195 | mExpectedLayersTraceProto.entry(mExpectedLayersTraceProto.entry_size() - 1); |
| 196 | auto actualLastEntry = mActualLayersTraceProto.entry(mActualLayersTraceProto.entry_size() - 1); |
| 197 | |
| 198 | EXPECT_EQ(expectedLastEntry.layers().layers_size(), actualLastEntry.layers().layers_size()); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 199 | |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 200 | std::vector<LayerInfo> expectedLayers = getLayerInfosFromProto(expectedLastEntry); |
| 201 | std::vector<LayerInfo> actualLayers = getLayerInfosFromProto(actualLastEntry); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 202 | |
| 203 | size_t i = 0; |
| 204 | for (; i < actualLayers.size() && i < expectedLayers.size(); i++) { |
| 205 | auto it = std::find_if(actualLayers.begin(), actualLayers.end(), |
| 206 | find_id(expectedLayers[i].id)); |
| 207 | EXPECT_NE(it, actualLayers.end()); |
| 208 | EXPECT_EQ(expectedLayers[i], *it); |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 209 | ALOGV("Validating %s[%" PRIu64 "] parent=%" PRIu64 " z=%d frame=%" PRIu64, |
| 210 | expectedLayers[i].name.c_str(), expectedLayers[i].id, expectedLayers[i].parent, |
| 211 | expectedLayers[i].z, expectedLayers[i].curr_frame); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | EXPECT_EQ(expectedLayers.size(), actualLayers.size()); |
| 215 | |
| 216 | if (i < actualLayers.size()) { |
| 217 | for (size_t j = 0; j < actualLayers.size(); j++) { |
| 218 | if (std::find_if(expectedLayers.begin(), expectedLayers.end(), |
| 219 | find_id(actualLayers[j].id)) == expectedLayers.end()) { |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 220 | ALOGD("actualLayers [%" PRIu64 "]:%s parent=%" PRIu64 " z=%d frame=%" PRIu64, |
| 221 | actualLayers[j].id, actualLayers[j].name.c_str(), actualLayers[j].parent, |
| 222 | actualLayers[j].z, actualLayers[j].curr_frame); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | FAIL(); |
| 226 | } |
| 227 | |
| 228 | if (i < expectedLayers.size()) { |
| 229 | for (size_t j = 0; j < expectedLayers.size(); j++) { |
| 230 | if (std::find_if(actualLayers.begin(), actualLayers.end(), |
| 231 | find_id(expectedLayers[j].id)) == actualLayers.end()) { |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 232 | ALOGD("expectedLayers [%" PRIu64 "]:%s parent=%" PRIu64 " z=%d frame=%" PRIu64, |
| 233 | expectedLayers[j].id, expectedLayers[j].name.c_str(), |
| 234 | expectedLayers[j].parent, expectedLayers[j].z, expectedLayers[j].curr_frame); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | FAIL(); |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | std::string PrintToStringParamName(const ::testing::TestParamInfo<std::filesystem::path>& info) { |
| 242 | const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix; |
| 243 | const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix; |
| 244 | |
| 245 | const auto& filename = info.param.filename().string(); |
| 246 | return filename.substr(prefix.length(), filename.length() - prefix.length() - postfix.length()); |
| 247 | } |
| 248 | |
| 249 | INSTANTIATE_TEST_CASE_P(TransactionTraceTestSuites, TransactionTraceTestSuite, |
| 250 | testing::ValuesIn(TransactionTraceTestSuite::sTransactionTraces), |
| 251 | PrintToStringParamName); |
| 252 | |
| 253 | } // namespace android |
| 254 | |
| 255 | int main(int argc, char** argv) { |
| 256 | for (const auto& entry : std::filesystem::directory_iterator( |
| 257 | android::base::GetExecutableDirectory() + "/testdata/")) { |
| 258 | if (!entry.is_regular_file()) { |
| 259 | continue; |
| 260 | } |
| 261 | const auto& filename = entry.path().filename().string(); |
| 262 | const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix; |
| 263 | if (filename.compare(0, prefix.length(), prefix)) { |
| 264 | continue; |
| 265 | } |
| 266 | const std::string& path = entry.path().string(); |
| 267 | const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix; |
| 268 | if (path.compare(path.length() - postfix.length(), postfix.length(), postfix)) { |
| 269 | continue; |
| 270 | } |
| 271 | android::TransactionTraceTestSuite::sTransactionTraces.push_back(path); |
| 272 | } |
| 273 | ::testing::InitGoogleTest(&argc, argv); |
| 274 | return RUN_ALL_TESTS(); |
Kean Mariotti | bec51fd | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 275 | } |