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> |
Diwas Sharma | 6975834 | 2023-09-01 00:40:19 +0000 | [diff] [blame] | 26 | #include <LayerTraceGenerator.h> |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 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 | |
Diwas Sharma | 6975834 | 2023-09-01 00:40:19 +0000 | [diff] [blame] | 43 | proto::TransactionTraceFile mTransactionTrace; |
| 44 | LayersTraceFileProto mExpectedLayersTraceProto; |
| 45 | 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; |
| 59 | std::string actualLayersTracePath = |
| 60 | std::string(temp_dir.path) + "/" + expectedLayersFilename + "_actual"; |
| 61 | |
Diwas Sharma | 6975834 | 2023-09-01 00:40:19 +0000 | [diff] [blame] | 62 | EXPECT_TRUE(LayerTraceGenerator().generate(mTransactionTrace, actualLayersTracePath.c_str(), |
| 63 | /*onlyLastEntry=*/true)) |
| 64 | << "Failed to generate layers trace from " << transactionTracePath; |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 65 | EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(actualLayersTracePath))); |
| 66 | parseLayersTraceFromFile(actualLayersTracePath.c_str(), mActualLayersTraceProto); |
| 67 | } |
| 68 | |
| 69 | void parseTransactionTraceFromFile(const char* transactionTracePath, |
Diwas Sharma | 6975834 | 2023-09-01 00:40:19 +0000 | [diff] [blame] | 70 | proto::TransactionTraceFile& outProto) { |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 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 | |
Diwas Sharma | 6975834 | 2023-09-01 00:40:19 +0000 | [diff] [blame] | 78 | void parseLayersTraceFromFile(const char* layersTracePath, LayersTraceFileProto& outProto) { |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 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 | |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 88 | struct LayerInfo { |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 89 | uint64_t id; |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 90 | std::string name; |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 91 | uint64_t parent; |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 92 | int z; |
| 93 | uint64_t curr_frame; |
| 94 | float x; |
| 95 | float y; |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame] | 96 | uint32_t bufferWidth; |
| 97 | uint32_t bufferHeight; |
Vishnu Nair | 0d13b91 | 2023-03-27 22:06:38 +0000 | [diff] [blame] | 98 | Rect touchableRegionBounds; |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | bool operator==(const LayerInfo& lh, const LayerInfo& rh) { |
Vishnu Nair | 0d13b91 | 2023-03-27 22:06:38 +0000 | [diff] [blame] | 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); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | bool compareById(const LayerInfo& a, const LayerInfo& b) { |
| 109 | return a.id < b.id; |
| 110 | } |
| 111 | |
| 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 |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame] | 115 | << " y=" << info.y << " bufferWidth=" << info.bufferWidth |
Vishnu Nair | 0d13b91 | 2023-03-27 22:06:38 +0000 | [diff] [blame] | 116 | << " bufferHeight=" << info.bufferHeight << "touchableRegionBounds={" |
| 117 | << info.touchableRegionBounds.left << "," << info.touchableRegionBounds.top << "," |
| 118 | << info.touchableRegionBounds.right << "," << info.touchableRegionBounds.bottom << "}"; |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | struct find_id : std::unary_function<LayerInfo, bool> { |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 122 | uint64_t id; |
| 123 | find_id(uint64_t id) : id(id) {} |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 124 | bool operator()(LayerInfo const& m) const { return m.id == id; } |
| 125 | }; |
| 126 | |
Diwas Sharma | 6975834 | 2023-09-01 00:40:19 +0000 | [diff] [blame] | 127 | static LayerInfo getLayerInfoFromProto(::android::surfaceflinger::LayerProto& proto) { |
Vishnu Nair | 0d13b91 | 2023-03-27 22:06:38 +0000 | [diff] [blame] | 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 | |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 139 | return {static_cast<uint64_t>(proto.id()), |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame] | 140 | proto.name(), |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 141 | static_cast<uint64_t>(proto.parent()), |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame] | 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 Nair | 0d13b91 | 2023-03-27 22:06:38 +0000 | [diff] [blame] | 147 | proto.has_active_buffer() ? proto.active_buffer().height() : 0, |
| 148 | touchableRegionBounds}; |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Diwas Sharma | 6975834 | 2023-09-01 00:40:19 +0000 | [diff] [blame] | 151 | static std::vector<LayerInfo> getLayerInfosFromProto( |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame^] | 152 | android::surfaceflinger::LayersTraceProto& entry) { |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 153 | std::unordered_map<uint64_t /* snapshotId*/, uint64_t /*layerId*/> snapshotIdToLayerId; |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 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 | |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 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 | |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 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); |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 180 | layer.parent = it == snapshotIdToLayerId.end() ? static_cast<uint64_t>(-1) : it->second; |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 181 | } |
| 182 | return layers; |
| 183 | } |
| 184 | |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 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()); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 194 | |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 195 | std::vector<LayerInfo> expectedLayers = getLayerInfosFromProto(expectedLastEntry); |
| 196 | std::vector<LayerInfo> actualLayers = getLayerInfosFromProto(actualLastEntry); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 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); |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 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); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 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()) { |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 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); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 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()) { |
Vishnu Nair | 52c4f25 | 2023-06-14 15:25:12 -0700 | [diff] [blame] | 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); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | FAIL(); |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 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 | |
| 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(); |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame^] | 270 | } |