| 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 <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: | 
|  | 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 |  | 
|  | 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 | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 89 | int32_t id; | 
| Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 90 | std::string name; | 
| Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 91 | int32_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> { | 
|  | 122 | int id; | 
|  | 123 | find_id(int id) : id(id) {} | 
|  | 124 | bool operator()(LayerInfo const& m) const { return m.id == id; } | 
|  | 125 | }; | 
|  | 126 |  | 
| Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +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 | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame] | 139 | 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 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 |  | 
| Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 151 | static std::vector<LayerInfo> getLayerInfosFromProto( | 
|  | 152 | android::surfaceflinger::LayersTraceProto& entry) { | 
|  | 153 | std::unordered_map<int32_t /* snapshotId*/, int32_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 | snapshotIdToLayerId[layerInfo.id] = static_cast<int32_t>(layer.original_id()); | 
|  | 162 | if (layer.original_id() != 0) { | 
|  | 163 | mapSnapshotIdToLayerId = true; | 
|  | 164 | } | 
|  | 165 | layers.push_back(layerInfo); | 
|  | 166 | } | 
|  | 167 | std::sort(layers.begin(), layers.end(), compareById); | 
|  | 168 |  | 
|  | 169 | if (!mapSnapshotIdToLayerId) { | 
|  | 170 | return layers; | 
|  | 171 | } | 
|  | 172 | for (auto& layer : layers) { | 
|  | 173 | layer.id = snapshotIdToLayerId[layer.id]; | 
|  | 174 | auto it = snapshotIdToLayerId.find(layer.parent); | 
|  | 175 | layer.parent = it == snapshotIdToLayerId.end() ? -1 : it->second; | 
|  | 176 | } | 
|  | 177 | return layers; | 
|  | 178 | } | 
|  | 179 |  | 
| Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 180 | TEST_P(TransactionTraceTestSuite, validateEndState) { | 
|  | 181 | ASSERT_GT(mActualLayersTraceProto.entry_size(), 0); | 
|  | 182 | ASSERT_GT(mExpectedLayersTraceProto.entry_size(), 0); | 
|  | 183 |  | 
|  | 184 | auto expectedLastEntry = | 
|  | 185 | mExpectedLayersTraceProto.entry(mExpectedLayersTraceProto.entry_size() - 1); | 
|  | 186 | auto actualLastEntry = mActualLayersTraceProto.entry(mActualLayersTraceProto.entry_size() - 1); | 
|  | 187 |  | 
|  | 188 | EXPECT_EQ(expectedLastEntry.layers().layers_size(), actualLastEntry.layers().layers_size()); | 
| Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 189 |  | 
| Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 190 | std::vector<LayerInfo> expectedLayers = getLayerInfosFromProto(expectedLastEntry); | 
|  | 191 | std::vector<LayerInfo> actualLayers = getLayerInfosFromProto(actualLastEntry); | 
|  | 192 | ; | 
| Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 193 |  | 
|  | 194 | size_t i = 0; | 
|  | 195 | for (; i < actualLayers.size() && i < expectedLayers.size(); i++) { | 
|  | 196 | auto it = std::find_if(actualLayers.begin(), actualLayers.end(), | 
|  | 197 | find_id(expectedLayers[i].id)); | 
|  | 198 | EXPECT_NE(it, actualLayers.end()); | 
|  | 199 | EXPECT_EQ(expectedLayers[i], *it); | 
|  | 200 | ALOGV("Validating %s[%d] parent=%d z=%d frame=%" PRIu64, expectedLayers[i].name.c_str(), | 
|  | 201 | expectedLayers[i].id, expectedLayers[i].parent, expectedLayers[i].z, | 
|  | 202 | expectedLayers[i].curr_frame); | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | EXPECT_EQ(expectedLayers.size(), actualLayers.size()); | 
|  | 206 |  | 
|  | 207 | if (i < actualLayers.size()) { | 
|  | 208 | for (size_t j = 0; j < actualLayers.size(); j++) { | 
|  | 209 | if (std::find_if(expectedLayers.begin(), expectedLayers.end(), | 
|  | 210 | find_id(actualLayers[j].id)) == expectedLayers.end()) { | 
|  | 211 | ALOGD("actualLayers [%d]:%s parent=%d z=%d frame=%" PRIu64, actualLayers[j].id, | 
|  | 212 | actualLayers[j].name.c_str(), actualLayers[j].parent, actualLayers[j].z, | 
|  | 213 | actualLayers[j].curr_frame); | 
|  | 214 | } | 
|  | 215 | } | 
|  | 216 | FAIL(); | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | if (i < expectedLayers.size()) { | 
|  | 220 | for (size_t j = 0; j < expectedLayers.size(); j++) { | 
|  | 221 | if (std::find_if(actualLayers.begin(), actualLayers.end(), | 
|  | 222 | find_id(expectedLayers[j].id)) == actualLayers.end()) { | 
|  | 223 | ALOGD("expectedLayers [%d]:%s parent=%d z=%d frame=%" PRIu64, expectedLayers[j].id, | 
|  | 224 | expectedLayers[j].name.c_str(), expectedLayers[j].parent, expectedLayers[j].z, | 
|  | 225 | expectedLayers[j].curr_frame); | 
|  | 226 | } | 
|  | 227 | } | 
|  | 228 | FAIL(); | 
| Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 229 | } | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | std::string PrintToStringParamName(const ::testing::TestParamInfo<std::filesystem::path>& info) { | 
|  | 233 | const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix; | 
|  | 234 | const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix; | 
|  | 235 |  | 
|  | 236 | const auto& filename = info.param.filename().string(); | 
|  | 237 | return filename.substr(prefix.length(), filename.length() - prefix.length() - postfix.length()); | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | INSTANTIATE_TEST_CASE_P(TransactionTraceTestSuites, TransactionTraceTestSuite, | 
|  | 241 | testing::ValuesIn(TransactionTraceTestSuite::sTransactionTraces), | 
|  | 242 | PrintToStringParamName); | 
|  | 243 |  | 
|  | 244 | } // namespace android | 
|  | 245 |  | 
|  | 246 | int main(int argc, char** argv) { | 
|  | 247 | for (const auto& entry : std::filesystem::directory_iterator( | 
|  | 248 | android::base::GetExecutableDirectory() + "/testdata/")) { | 
|  | 249 | if (!entry.is_regular_file()) { | 
|  | 250 | continue; | 
|  | 251 | } | 
|  | 252 | const auto& filename = entry.path().filename().string(); | 
|  | 253 | const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix; | 
|  | 254 | if (filename.compare(0, prefix.length(), prefix)) { | 
|  | 255 | continue; | 
|  | 256 | } | 
|  | 257 | const std::string& path = entry.path().string(); | 
|  | 258 | const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix; | 
|  | 259 | if (path.compare(path.length() - postfix.length(), postfix.length(), postfix)) { | 
|  | 260 | continue; | 
|  | 261 | } | 
|  | 262 | android::TransactionTraceTestSuite::sTransactionTraces.push_back(path); | 
|  | 263 | } | 
|  | 264 | ::testing::InitGoogleTest(&argc, argv); | 
|  | 265 | return RUN_ALL_TESTS(); | 
|  | 266 | } |