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> |
| 23 | |
| 24 | #include <LayerTraceGenerator.h> |
| 25 | #include <Tracing/TransactionProtoParser.h> |
| 26 | #include <layerproto/LayerProtoHeader.h> |
| 27 | #include <log/log.h> |
| 28 | |
| 29 | using namespace android::surfaceflinger; |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | class TransactionTraceTestSuite : public testing::Test, |
| 34 | public testing::WithParamInterface<std::filesystem::path> { |
| 35 | public: |
| 36 | static std::vector<std::filesystem::path> sTransactionTraces; |
| 37 | static constexpr std::string_view sTransactionTracePrefix = "transactions_trace_"; |
| 38 | static constexpr std::string_view sLayersTracePrefix = "layers_trace_"; |
| 39 | static constexpr std::string_view sTracePostfix = ".winscope"; |
| 40 | |
| 41 | proto::TransactionTraceFile mTransactionTrace; |
| 42 | LayersTraceFileProto mExpectedLayersTraceProto; |
| 43 | LayersTraceFileProto mActualLayersTraceProto; |
| 44 | |
| 45 | protected: |
| 46 | void SetUp() override { |
| 47 | std::filesystem::path transactionTracePath = GetParam(); |
| 48 | parseTransactionTraceFromFile(transactionTracePath.c_str(), mTransactionTrace); |
| 49 | |
| 50 | std::string expectedLayersFilename = std::string(sLayersTracePrefix) + |
| 51 | transactionTracePath.filename().string().substr(sTransactionTracePrefix.length()); |
| 52 | std::string expectedLayersTracePath = |
| 53 | transactionTracePath.parent_path().string() + "/" + expectedLayersFilename; |
| 54 | EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(expectedLayersTracePath))); |
| 55 | parseLayersTraceFromFile(expectedLayersTracePath.c_str(), mExpectedLayersTraceProto); |
| 56 | TemporaryDir temp_dir; |
| 57 | std::string actualLayersTracePath = |
| 58 | std::string(temp_dir.path) + "/" + expectedLayersFilename + "_actual"; |
| 59 | |
| 60 | EXPECT_TRUE( |
| 61 | LayerTraceGenerator().generate(mTransactionTrace, actualLayersTracePath.c_str())) |
| 62 | << "Failed to generate layers trace from " << transactionTracePath; |
| 63 | EXPECT_TRUE(std::filesystem::exists(std::filesystem::path(actualLayersTracePath))); |
| 64 | parseLayersTraceFromFile(actualLayersTracePath.c_str(), mActualLayersTraceProto); |
| 65 | } |
| 66 | |
| 67 | void parseTransactionTraceFromFile(const char* transactionTracePath, |
| 68 | proto::TransactionTraceFile& outProto) { |
| 69 | ALOGD("Parsing file %s...", transactionTracePath); |
| 70 | std::fstream input(transactionTracePath, std::ios::in | std::ios::binary); |
| 71 | EXPECT_TRUE(input) << "Error could not open " << transactionTracePath; |
| 72 | EXPECT_TRUE(outProto.ParseFromIstream(&input)) |
| 73 | << "Failed to parse " << transactionTracePath; |
| 74 | } |
| 75 | |
| 76 | void parseLayersTraceFromFile(const char* layersTracePath, LayersTraceFileProto& outProto) { |
| 77 | ALOGD("Parsing file %s...", layersTracePath); |
| 78 | std::fstream input(layersTracePath, std::ios::in | std::ios::binary); |
| 79 | EXPECT_TRUE(input) << "Error could not open " << layersTracePath; |
| 80 | EXPECT_TRUE(outProto.ParseFromIstream(&input)) << "Failed to parse " << layersTracePath; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | std::vector<std::filesystem::path> TransactionTraceTestSuite::sTransactionTraces{}; |
| 85 | |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 86 | struct LayerInfo { |
| 87 | int id; |
| 88 | std::string name; |
| 89 | int parent; |
| 90 | int z; |
| 91 | uint64_t curr_frame; |
| 92 | float x; |
| 93 | float y; |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame^] | 94 | uint32_t bufferWidth; |
| 95 | uint32_t bufferHeight; |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | bool operator==(const LayerInfo& lh, const LayerInfo& rh) { |
| 99 | return std::make_tuple(lh.id, lh.name, lh.parent, lh.z, lh.curr_frame) == |
| 100 | std::make_tuple(rh.id, rh.name, rh.parent, rh.z, rh.curr_frame); |
| 101 | } |
| 102 | |
| 103 | bool compareById(const LayerInfo& a, const LayerInfo& b) { |
| 104 | return a.id < b.id; |
| 105 | } |
| 106 | |
| 107 | inline void PrintTo(const LayerInfo& info, ::std::ostream* os) { |
| 108 | *os << "Layer [" << info.id << "] name=" << info.name << " parent=" << info.parent |
| 109 | << " z=" << info.z << " curr_frame=" << info.curr_frame << " x=" << info.x |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame^] | 110 | << " y=" << info.y << " bufferWidth=" << info.bufferWidth |
| 111 | << " bufferHeight=" << info.bufferHeight; |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | struct find_id : std::unary_function<LayerInfo, bool> { |
| 115 | int id; |
| 116 | find_id(int id) : id(id) {} |
| 117 | bool operator()(LayerInfo const& m) const { return m.id == id; } |
| 118 | }; |
| 119 | |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame^] | 120 | static LayerInfo getLayerInfoFromProto(::android::surfaceflinger::LayerProto& proto) { |
| 121 | return {proto.id(), |
| 122 | proto.name(), |
| 123 | proto.parent(), |
| 124 | proto.z(), |
| 125 | proto.curr_frame(), |
| 126 | proto.has_position() ? proto.position().x() : -1, |
| 127 | proto.has_position() ? proto.position().y() : -1, |
| 128 | proto.has_active_buffer() ? proto.active_buffer().width() : 0, |
| 129 | proto.has_active_buffer() ? proto.active_buffer().height() : 0}; |
| 130 | } |
| 131 | |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 132 | TEST_P(TransactionTraceTestSuite, validateEndState) { |
| 133 | ASSERT_GT(mActualLayersTraceProto.entry_size(), 0); |
| 134 | ASSERT_GT(mExpectedLayersTraceProto.entry_size(), 0); |
| 135 | |
| 136 | auto expectedLastEntry = |
| 137 | mExpectedLayersTraceProto.entry(mExpectedLayersTraceProto.entry_size() - 1); |
| 138 | auto actualLastEntry = mActualLayersTraceProto.entry(mActualLayersTraceProto.entry_size() - 1); |
| 139 | |
| 140 | EXPECT_EQ(expectedLastEntry.layers().layers_size(), actualLastEntry.layers().layers_size()); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 141 | |
| 142 | std::vector<LayerInfo> expectedLayers; |
| 143 | expectedLayers.reserve(static_cast<size_t>(expectedLastEntry.layers().layers_size())); |
| 144 | for (int i = 0; i < expectedLastEntry.layers().layers_size(); i++) { |
| 145 | auto layer = expectedLastEntry.layers().layers(i); |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame^] | 146 | expectedLayers.push_back(getLayerInfoFromProto(layer)); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 147 | } |
| 148 | std::sort(expectedLayers.begin(), expectedLayers.end(), compareById); |
| 149 | |
| 150 | std::vector<LayerInfo> actualLayers; |
| 151 | actualLayers.reserve(static_cast<size_t>(actualLastEntry.layers().layers_size())); |
| 152 | for (int i = 0; i < actualLastEntry.layers().layers_size(); i++) { |
| 153 | auto layer = actualLastEntry.layers().layers(i); |
Vishnu Nair | 63a662a | 2023-02-22 20:17:18 +0000 | [diff] [blame^] | 154 | actualLayers.push_back(getLayerInfoFromProto(layer)); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 155 | } |
| 156 | std::sort(actualLayers.begin(), actualLayers.end(), compareById); |
| 157 | |
| 158 | size_t i = 0; |
| 159 | for (; i < actualLayers.size() && i < expectedLayers.size(); i++) { |
| 160 | auto it = std::find_if(actualLayers.begin(), actualLayers.end(), |
| 161 | find_id(expectedLayers[i].id)); |
| 162 | EXPECT_NE(it, actualLayers.end()); |
| 163 | EXPECT_EQ(expectedLayers[i], *it); |
| 164 | ALOGV("Validating %s[%d] parent=%d z=%d frame=%" PRIu64, expectedLayers[i].name.c_str(), |
| 165 | expectedLayers[i].id, expectedLayers[i].parent, expectedLayers[i].z, |
| 166 | expectedLayers[i].curr_frame); |
| 167 | } |
| 168 | |
| 169 | EXPECT_EQ(expectedLayers.size(), actualLayers.size()); |
| 170 | |
| 171 | if (i < actualLayers.size()) { |
| 172 | for (size_t j = 0; j < actualLayers.size(); j++) { |
| 173 | if (std::find_if(expectedLayers.begin(), expectedLayers.end(), |
| 174 | find_id(actualLayers[j].id)) == expectedLayers.end()) { |
| 175 | ALOGD("actualLayers [%d]:%s parent=%d z=%d frame=%" PRIu64, actualLayers[j].id, |
| 176 | actualLayers[j].name.c_str(), actualLayers[j].parent, actualLayers[j].z, |
| 177 | actualLayers[j].curr_frame); |
| 178 | } |
| 179 | } |
| 180 | FAIL(); |
| 181 | } |
| 182 | |
| 183 | if (i < expectedLayers.size()) { |
| 184 | for (size_t j = 0; j < expectedLayers.size(); j++) { |
| 185 | if (std::find_if(actualLayers.begin(), actualLayers.end(), |
| 186 | find_id(expectedLayers[j].id)) == actualLayers.end()) { |
| 187 | ALOGD("expectedLayers [%d]:%s parent=%d z=%d frame=%" PRIu64, expectedLayers[j].id, |
| 188 | expectedLayers[j].name.c_str(), expectedLayers[j].parent, expectedLayers[j].z, |
| 189 | expectedLayers[j].curr_frame); |
| 190 | } |
| 191 | } |
| 192 | FAIL(); |
Vishnu Nair | 8f371ed | 2022-02-03 19:15:36 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | std::string PrintToStringParamName(const ::testing::TestParamInfo<std::filesystem::path>& info) { |
| 197 | const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix; |
| 198 | const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix; |
| 199 | |
| 200 | const auto& filename = info.param.filename().string(); |
| 201 | return filename.substr(prefix.length(), filename.length() - prefix.length() - postfix.length()); |
| 202 | } |
| 203 | |
| 204 | INSTANTIATE_TEST_CASE_P(TransactionTraceTestSuites, TransactionTraceTestSuite, |
| 205 | testing::ValuesIn(TransactionTraceTestSuite::sTransactionTraces), |
| 206 | PrintToStringParamName); |
| 207 | |
| 208 | } // namespace android |
| 209 | |
| 210 | int main(int argc, char** argv) { |
| 211 | for (const auto& entry : std::filesystem::directory_iterator( |
| 212 | android::base::GetExecutableDirectory() + "/testdata/")) { |
| 213 | if (!entry.is_regular_file()) { |
| 214 | continue; |
| 215 | } |
| 216 | const auto& filename = entry.path().filename().string(); |
| 217 | const auto& prefix = android::TransactionTraceTestSuite::sTransactionTracePrefix; |
| 218 | if (filename.compare(0, prefix.length(), prefix)) { |
| 219 | continue; |
| 220 | } |
| 221 | const std::string& path = entry.path().string(); |
| 222 | const auto& postfix = android::TransactionTraceTestSuite::sTracePostfix; |
| 223 | if (path.compare(path.length() - postfix.length(), postfix.length(), postfix)) { |
| 224 | continue; |
| 225 | } |
| 226 | android::TransactionTraceTestSuite::sTransactionTraces.push_back(path); |
| 227 | } |
| 228 | ::testing::InitGoogleTest(&argc, argv); |
| 229 | return RUN_ALL_TESTS(); |
| 230 | } |