Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | |
| 20 | #include <gui/SurfaceComposerClient.h> |
| 21 | |
| 22 | #include "Tracing/RingBuffer.h" |
| 23 | #include "Tracing/TransactionTracing.h" |
| 24 | |
| 25 | using namespace android::surfaceflinger; |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | class TransactionTracingTest : public testing::Test { |
| 30 | protected: |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 31 | static constexpr size_t SMALL_BUFFER_SIZE = 1024; |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 32 | TransactionTracing mTracing; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 33 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 34 | void flush(int64_t vsyncId) { mTracing.flush(vsyncId); } |
| 35 | proto::TransactionTraceFile writeToProto() { return mTracing.writeToProto(); } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 36 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 37 | proto::TransactionTraceEntry bufferFront() { |
| 38 | std::scoped_lock<std::mutex> lock(mTracing.mTraceLock); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 39 | proto::TransactionTraceEntry entry; |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 40 | entry.ParseFromString(mTracing.mBuffer.front()); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 41 | return entry; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 44 | void queueAndCommitTransaction(int64_t vsyncId) { |
| 45 | TransactionState transaction; |
| 46 | transaction.id = static_cast<uint64_t>(vsyncId * 3); |
| 47 | transaction.originUid = 1; |
| 48 | transaction.originPid = 2; |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 49 | mTracing.addQueuedTransaction(transaction); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 50 | std::vector<TransactionState> transactions; |
| 51 | transactions.emplace_back(transaction); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 52 | mTracing.addCommittedTransactions(transactions, vsyncId); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 53 | flush(vsyncId); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 56 | void verifyEntry(const proto::TransactionTraceEntry& actualProto, |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 57 | const std::vector<TransactionState>& expectedTransactions, |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 58 | int64_t expectedVsyncId) { |
| 59 | EXPECT_EQ(actualProto.vsync_id(), expectedVsyncId); |
| 60 | EXPECT_EQ(actualProto.transactions().size(), |
| 61 | static_cast<int32_t>(expectedTransactions.size())); |
| 62 | for (uint32_t i = 0; i < expectedTransactions.size(); i++) { |
| 63 | EXPECT_EQ(actualProto.transactions(static_cast<int32_t>(i)).pid(), |
| 64 | expectedTransactions[i].originPid); |
| 65 | } |
| 66 | } |
| 67 | }; |
| 68 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 69 | TEST_F(TransactionTracingTest, addTransactions) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 70 | std::vector<TransactionState> transactions; |
| 71 | transactions.reserve(100); |
| 72 | for (uint64_t i = 0; i < 100; i++) { |
| 73 | TransactionState transaction; |
| 74 | transaction.id = i; |
| 75 | transaction.originPid = static_cast<int32_t>(i); |
| 76 | transactions.emplace_back(transaction); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 77 | mTracing.addQueuedTransaction(transaction); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Split incoming transactions into two and commit them in reverse order to test out of order |
| 81 | // commits. |
| 82 | std::vector<TransactionState> firstTransactionSet = |
| 83 | std::vector<TransactionState>(transactions.begin() + 50, transactions.end()); |
| 84 | int64_t firstTransactionSetVsyncId = 42; |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 85 | mTracing.addCommittedTransactions(firstTransactionSet, firstTransactionSetVsyncId); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 86 | |
| 87 | int64_t secondTransactionSetVsyncId = 43; |
| 88 | std::vector<TransactionState> secondTransactionSet = |
| 89 | std::vector<TransactionState>(transactions.begin(), transactions.begin() + 50); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 90 | mTracing.addCommittedTransactions(secondTransactionSet, secondTransactionSetVsyncId); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 91 | flush(secondTransactionSetVsyncId); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 92 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 93 | proto::TransactionTraceFile proto = writeToProto(); |
| 94 | EXPECT_EQ(proto.entry().size(), 3); |
| 95 | // skip starting entry |
| 96 | verifyEntry(proto.entry(1), firstTransactionSet, firstTransactionSetVsyncId); |
| 97 | verifyEntry(proto.entry(2), secondTransactionSet, secondTransactionSetVsyncId); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 100 | class TransactionTracingLayerHandlingTest : public TransactionTracingTest { |
| 101 | protected: |
| 102 | void SetUp() override { |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 103 | // add layers |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 104 | mTracing.setBufferSize(SMALL_BUFFER_SIZE); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 105 | const sp<IBinder> fakeLayerHandle = new BBinder(); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 106 | mTracing.onLayerAdded(fakeLayerHandle->localBinder(), mParentLayerId, "parent", |
| 107 | 123 /* flags */, -1 /* parentId */); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 108 | const sp<IBinder> fakeChildLayerHandle = new BBinder(); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 109 | mTracing.onLayerAdded(fakeChildLayerHandle->localBinder(), mChildLayerId, "child", |
| 110 | 456 /* flags */, mParentLayerId); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 111 | |
| 112 | // add some layer transaction |
| 113 | { |
| 114 | TransactionState transaction; |
| 115 | transaction.id = 50; |
| 116 | ComposerState layerState; |
| 117 | layerState.state.surface = fakeLayerHandle; |
| 118 | layerState.state.what = layer_state_t::eLayerChanged; |
| 119 | layerState.state.z = 42; |
| 120 | transaction.states.add(layerState); |
| 121 | ComposerState childState; |
| 122 | childState.state.surface = fakeChildLayerHandle; |
Vishnu Nair | 473838d | 2021-12-08 09:46:02 -0800 | [diff] [blame] | 123 | childState.state.what = layer_state_t::eLayerChanged; |
| 124 | childState.state.z = 43; |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 125 | transaction.states.add(childState); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 126 | mTracing.addQueuedTransaction(transaction); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 127 | |
| 128 | std::vector<TransactionState> transactions; |
| 129 | transactions.emplace_back(transaction); |
| 130 | VSYNC_ID_FIRST_LAYER_CHANGE = ++mVsyncId; |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 131 | mTracing.addCommittedTransactions(transactions, VSYNC_ID_FIRST_LAYER_CHANGE); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 132 | flush(VSYNC_ID_FIRST_LAYER_CHANGE); |
| 133 | } |
| 134 | |
| 135 | // add transactions that modify the layer state further so we can test that layer state |
| 136 | // gets merged |
| 137 | { |
| 138 | TransactionState transaction; |
| 139 | transaction.id = 51; |
| 140 | ComposerState layerState; |
| 141 | layerState.state.surface = fakeLayerHandle; |
| 142 | layerState.state.what = layer_state_t::eLayerChanged | layer_state_t::ePositionChanged; |
| 143 | layerState.state.z = 41; |
| 144 | layerState.state.x = 22; |
| 145 | transaction.states.add(layerState); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 146 | mTracing.addQueuedTransaction(transaction); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 147 | |
| 148 | std::vector<TransactionState> transactions; |
| 149 | transactions.emplace_back(transaction); |
| 150 | VSYNC_ID_SECOND_LAYER_CHANGE = ++mVsyncId; |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 151 | mTracing.addCommittedTransactions(transactions, VSYNC_ID_SECOND_LAYER_CHANGE); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 152 | flush(VSYNC_ID_SECOND_LAYER_CHANGE); |
| 153 | } |
| 154 | |
| 155 | // remove child layer |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 156 | mTracing.onLayerRemoved(2); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 157 | VSYNC_ID_CHILD_LAYER_REMOVED = ++mVsyncId; |
| 158 | queueAndCommitTransaction(VSYNC_ID_CHILD_LAYER_REMOVED); |
| 159 | |
| 160 | // remove layer |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 161 | mTracing.onLayerRemoved(1); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 162 | queueAndCommitTransaction(++mVsyncId); |
| 163 | } |
| 164 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 165 | int mParentLayerId = 1; |
Vishnu Nair | 473838d | 2021-12-08 09:46:02 -0800 | [diff] [blame] | 166 | int mChildLayerId = 2; |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 167 | int64_t mVsyncId = 0; |
| 168 | int64_t VSYNC_ID_FIRST_LAYER_CHANGE; |
| 169 | int64_t VSYNC_ID_SECOND_LAYER_CHANGE; |
| 170 | int64_t VSYNC_ID_CHILD_LAYER_REMOVED; |
| 171 | }; |
| 172 | |
| 173 | TEST_F(TransactionTracingLayerHandlingTest, addStartingState) { |
| 174 | // add transactions until we drop the transaction with the first layer change |
| 175 | while (bufferFront().vsync_id() <= VSYNC_ID_FIRST_LAYER_CHANGE) { |
| 176 | queueAndCommitTransaction(++mVsyncId); |
| 177 | } |
| 178 | proto::TransactionTraceFile proto = writeToProto(); |
| 179 | // verify we can still retrieve the layer change from the first entry containing starting |
| 180 | // states. |
| 181 | EXPECT_GT(proto.entry().size(), 0); |
| 182 | EXPECT_GT(proto.entry(0).transactions().size(), 0); |
| 183 | EXPECT_GT(proto.entry(0).added_layers().size(), 0); |
Vishnu Nair | 473838d | 2021-12-08 09:46:02 -0800 | [diff] [blame] | 184 | EXPECT_EQ(proto.entry(0).transactions(0).layer_changes().size(), 2); |
| 185 | EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(0).layer_id(), mParentLayerId); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 186 | EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(0).z(), 42); |
Vishnu Nair | 473838d | 2021-12-08 09:46:02 -0800 | [diff] [blame] | 187 | EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(1).layer_id(), mChildLayerId); |
| 188 | EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(1).z(), 43); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | TEST_F(TransactionTracingLayerHandlingTest, updateStartingState) { |
| 192 | // add transactions until we drop the transaction with the second layer change |
| 193 | while (bufferFront().vsync_id() <= VSYNC_ID_SECOND_LAYER_CHANGE) { |
| 194 | queueAndCommitTransaction(++mVsyncId); |
| 195 | } |
| 196 | proto::TransactionTraceFile proto = writeToProto(); |
| 197 | // verify starting states are updated correctly |
| 198 | EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(0).z(), 41); |
| 199 | } |
| 200 | |
| 201 | TEST_F(TransactionTracingLayerHandlingTest, removeStartingState) { |
| 202 | // add transactions until we drop the transaction which removes the child layer |
| 203 | while (bufferFront().vsync_id() <= VSYNC_ID_CHILD_LAYER_REMOVED) { |
| 204 | queueAndCommitTransaction(++mVsyncId); |
| 205 | } |
| 206 | proto::TransactionTraceFile proto = writeToProto(); |
| 207 | // verify the child layer has been removed from the trace |
| 208 | EXPECT_EQ(proto.entry(0).transactions(0).layer_changes().size(), 1); |
| 209 | EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(0).layer_id(), mParentLayerId); |
| 210 | } |
| 211 | |
| 212 | TEST_F(TransactionTracingLayerHandlingTest, startingStateSurvivesBufferFlush) { |
| 213 | // add transactions until we drop the transaction with the second layer change |
| 214 | while (bufferFront().vsync_id() <= VSYNC_ID_SECOND_LAYER_CHANGE) { |
| 215 | queueAndCommitTransaction(++mVsyncId); |
| 216 | } |
| 217 | proto::TransactionTraceFile proto = writeToProto(); |
| 218 | // verify we have two starting states |
| 219 | EXPECT_EQ(proto.entry(0).transactions(0).layer_changes().size(), 2); |
| 220 | |
| 221 | // Continue adding transactions until child layer is removed |
| 222 | while (bufferFront().vsync_id() <= VSYNC_ID_CHILD_LAYER_REMOVED) { |
| 223 | queueAndCommitTransaction(++mVsyncId); |
| 224 | } |
| 225 | proto = writeToProto(); |
| 226 | // verify we still have the parent layer state |
| 227 | EXPECT_EQ(proto.entry(0).transactions(0).layer_changes().size(), 1); |
| 228 | EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(0).layer_id(), mParentLayerId); |
| 229 | } |
| 230 | |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 231 | class TransactionTracingMirrorLayerTest : public TransactionTracingTest { |
| 232 | protected: |
| 233 | void SetUp() override { |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 234 | // add layers |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 235 | mTracing.setBufferSize(SMALL_BUFFER_SIZE); |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 236 | const sp<IBinder> fakeLayerHandle = new BBinder(); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 237 | mTracing.onLayerAdded(fakeLayerHandle->localBinder(), mLayerId, "Test Layer", |
| 238 | 123 /* flags */, -1 /* parentId */); |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 239 | const sp<IBinder> fakeMirrorLayerHandle = new BBinder(); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 240 | mTracing.onMirrorLayerAdded(fakeMirrorLayerHandle->localBinder(), mMirrorLayerId, "Mirror", |
| 241 | mLayerId); |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 242 | |
| 243 | // add some layer transaction |
| 244 | { |
| 245 | TransactionState transaction; |
| 246 | transaction.id = 50; |
| 247 | ComposerState layerState; |
| 248 | layerState.state.surface = fakeLayerHandle; |
| 249 | layerState.state.what = layer_state_t::eLayerChanged; |
| 250 | layerState.state.z = 42; |
| 251 | transaction.states.add(layerState); |
| 252 | ComposerState mirrorState; |
| 253 | mirrorState.state.surface = fakeMirrorLayerHandle; |
| 254 | mirrorState.state.what = layer_state_t::eLayerChanged; |
| 255 | mirrorState.state.z = 43; |
| 256 | transaction.states.add(mirrorState); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 257 | mTracing.addQueuedTransaction(transaction); |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 258 | |
| 259 | std::vector<TransactionState> transactions; |
| 260 | transactions.emplace_back(transaction); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame^] | 261 | mTracing.addCommittedTransactions(transactions, ++mVsyncId); |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 262 | flush(mVsyncId); |
| 263 | } |
| 264 | } |
| 265 | |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 266 | int mLayerId = 5; |
| 267 | int mMirrorLayerId = 55; |
| 268 | int64_t mVsyncId = 0; |
| 269 | int64_t VSYNC_ID_FIRST_LAYER_CHANGE; |
| 270 | int64_t VSYNC_ID_SECOND_LAYER_CHANGE; |
| 271 | int64_t VSYNC_ID_CHILD_LAYER_REMOVED; |
| 272 | }; |
| 273 | |
| 274 | TEST_F(TransactionTracingMirrorLayerTest, canAddMirrorLayers) { |
| 275 | proto::TransactionTraceFile proto = writeToProto(); |
| 276 | // We don't have any starting states since no layer was removed from. |
| 277 | EXPECT_EQ(proto.entry().size(), 2); |
| 278 | EXPECT_EQ(proto.entry(0).transactions().size(), 0); |
| 279 | EXPECT_EQ(proto.entry(0).added_layers().size(), 0); |
| 280 | |
| 281 | // Verify the mirror layer was added |
| 282 | EXPECT_EQ(proto.entry(1).transactions().size(), 1); |
| 283 | EXPECT_EQ(proto.entry(1).added_layers().size(), 2); |
| 284 | EXPECT_EQ(proto.entry(1).added_layers(1).layer_id(), mMirrorLayerId); |
| 285 | EXPECT_EQ(proto.entry(1).transactions(0).layer_changes().size(), 2); |
| 286 | EXPECT_EQ(proto.entry(1).transactions(0).layer_changes(1).z(), 43); |
| 287 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 288 | } // namespace android |