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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "TransactionTracing" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
| 21 | #include <android-base/stringprintf.h> |
| 22 | #include <log/log.h> |
| 23 | #include <utils/SystemClock.h> |
| 24 | #include <utils/Trace.h> |
| 25 | |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 26 | #include "ClientCache.h" |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 27 | #include "TransactionTracing.h" |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 28 | #include "renderengine/ExternalTexture.h" |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 32 | class FlingerDataMapper : public TransactionProtoParser::FlingerDataMapper { |
| 33 | std::unordered_map<BBinder* /* layerHandle */, int32_t /* layerId */>& mLayerHandles; |
| 34 | |
| 35 | public: |
| 36 | FlingerDataMapper(std::unordered_map<BBinder* /* handle */, int32_t /* id */>& layerHandles) |
| 37 | : mLayerHandles(layerHandles) {} |
| 38 | |
| 39 | int32_t getLayerId(const sp<IBinder>& layerHandle) const override { |
| 40 | if (layerHandle == nullptr) { |
| 41 | return -1; |
| 42 | } |
| 43 | auto it = mLayerHandles.find(layerHandle->localBinder()); |
| 44 | if (it == mLayerHandles.end()) { |
| 45 | ALOGW("Could not find layer handle %p", layerHandle->localBinder()); |
| 46 | return -1; |
| 47 | } |
| 48 | return it->second; |
| 49 | } |
| 50 | |
| 51 | void getGraphicBufferPropertiesFromCache(client_cache_t cachedBuffer, uint64_t* outBufferId, |
| 52 | uint32_t* outWidth, uint32_t* outHeight, |
| 53 | int32_t* outPixelFormat, |
| 54 | uint64_t* outUsage) const override { |
| 55 | std::shared_ptr<renderengine::ExternalTexture> buffer = |
| 56 | ClientCache::getInstance().get(cachedBuffer); |
| 57 | if (!buffer || !buffer->getBuffer()) { |
| 58 | *outBufferId = 0; |
| 59 | *outWidth = 0; |
| 60 | *outHeight = 0; |
| 61 | *outPixelFormat = 0; |
| 62 | *outUsage = 0; |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | *outBufferId = buffer->getId(); |
| 67 | *outWidth = buffer->getWidth(); |
| 68 | *outHeight = buffer->getHeight(); |
| 69 | *outPixelFormat = buffer->getPixelFormat(); |
| 70 | *outUsage = buffer->getUsage(); |
| 71 | return; |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | TransactionTracing::TransactionTracing() |
| 76 | : mProtoParser(std::make_unique<FlingerDataMapper>(mLayerHandles)) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 77 | std::scoped_lock lock(mTraceLock); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 78 | |
| 79 | mBuffer.setSize(mBufferSizeInBytes); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 80 | mStartingTimestamp = systemTime(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 81 | { |
| 82 | std::scoped_lock lock(mMainThreadLock); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 83 | mThread = std::thread(&TransactionTracing::loop, this); |
| 84 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 87 | TransactionTracing::~TransactionTracing() { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 88 | std::thread thread; |
| 89 | { |
| 90 | std::scoped_lock lock(mMainThreadLock); |
| 91 | mDone = true; |
| 92 | mTransactionsAvailableCv.notify_all(); |
| 93 | thread = std::move(mThread); |
| 94 | } |
| 95 | if (thread.joinable()) { |
| 96 | thread.join(); |
| 97 | } |
| 98 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 99 | writeToFile(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Vishnu Nair | d8f5e9f | 2022-02-03 10:23:28 -0800 | [diff] [blame^] | 102 | status_t TransactionTracing::writeToFile(std::string filename) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 103 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 104 | proto::TransactionTraceFile fileProto = createTraceFileProto(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 105 | addStartingStateToProtoLocked(fileProto); |
Vishnu Nair | d8f5e9f | 2022-02-03 10:23:28 -0800 | [diff] [blame^] | 106 | return mBuffer.writeToFile(fileProto, filename); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void TransactionTracing::setBufferSize(size_t bufferSizeInBytes) { |
| 110 | std::scoped_lock lock(mTraceLock); |
| 111 | mBufferSizeInBytes = bufferSizeInBytes; |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 112 | mBuffer.setSize(mBufferSizeInBytes); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | proto::TransactionTraceFile TransactionTracing::createTraceFileProto() const { |
| 116 | proto::TransactionTraceFile proto; |
| 117 | proto.set_magic_number(uint64_t(proto::TransactionTraceFile_MagicNumber_MAGIC_NUMBER_H) << 32 | |
| 118 | proto::TransactionTraceFile_MagicNumber_MAGIC_NUMBER_L); |
| 119 | return proto; |
| 120 | } |
| 121 | |
| 122 | void TransactionTracing::dump(std::string& result) const { |
| 123 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 124 | base::StringAppendF(&result, |
| 125 | " queued transactions=%zu created layers=%zu handles=%zu states=%zu\n", |
| 126 | mQueuedTransactions.size(), mCreatedLayers.size(), mLayerHandles.size(), |
| 127 | mStartingStates.size()); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 128 | mBuffer.dump(result); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void TransactionTracing::addQueuedTransaction(const TransactionState& transaction) { |
| 132 | std::scoped_lock lock(mTraceLock); |
| 133 | ATRACE_CALL(); |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 134 | mQueuedTransactions[transaction.id] = mProtoParser.toProto(transaction); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void TransactionTracing::addCommittedTransactions(std::vector<TransactionState>& transactions, |
| 138 | int64_t vsyncId) { |
| 139 | CommittedTransactions committedTransactions; |
| 140 | committedTransactions.vsyncId = vsyncId; |
| 141 | committedTransactions.timestamp = systemTime(); |
| 142 | committedTransactions.transactionIds.reserve(transactions.size()); |
| 143 | for (const auto& transaction : transactions) { |
| 144 | committedTransactions.transactionIds.emplace_back(transaction.id); |
| 145 | } |
| 146 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 147 | mPendingTransactions.emplace_back(committedTransactions); |
| 148 | tryPushToTracingThread(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void TransactionTracing::loop() { |
| 152 | while (true) { |
| 153 | std::vector<CommittedTransactions> committedTransactions; |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 154 | std::vector<int32_t> removedLayers; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 155 | { |
| 156 | std::unique_lock<std::mutex> lock(mMainThreadLock); |
| 157 | base::ScopedLockAssertion assumeLocked(mMainThreadLock); |
| 158 | mTransactionsAvailableCv.wait(lock, [&]() REQUIRES(mMainThreadLock) { |
| 159 | return mDone || !mCommittedTransactions.empty(); |
| 160 | }); |
| 161 | if (mDone) { |
| 162 | mCommittedTransactions.clear(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 163 | mRemovedLayers.clear(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 164 | break; |
| 165 | } |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 166 | |
| 167 | removedLayers = std::move(mRemovedLayers); |
| 168 | mRemovedLayers.clear(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 169 | committedTransactions = std::move(mCommittedTransactions); |
| 170 | mCommittedTransactions.clear(); |
| 171 | } // unlock mMainThreadLock |
| 172 | |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 173 | if (!committedTransactions.empty() || !removedLayers.empty()) { |
| 174 | addEntry(committedTransactions, removedLayers); |
| 175 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 179 | void TransactionTracing::addEntry(const std::vector<CommittedTransactions>& committedTransactions, |
| 180 | const std::vector<int32_t>& removedLayers) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 181 | ATRACE_CALL(); |
| 182 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 183 | std::vector<std::string> removedEntries; |
| 184 | proto::TransactionTraceEntry entryProto; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 185 | for (const CommittedTransactions& entry : committedTransactions) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 186 | entryProto.set_elapsed_realtime_nanos(entry.timestamp); |
| 187 | entryProto.set_vsync_id(entry.vsyncId); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 188 | entryProto.mutable_added_layers()->Reserve(static_cast<int32_t>(mCreatedLayers.size())); |
| 189 | for (auto& newLayer : mCreatedLayers) { |
| 190 | entryProto.mutable_added_layers()->Add(std::move(newLayer)); |
| 191 | } |
| 192 | entryProto.mutable_removed_layers()->Reserve(static_cast<int32_t>(removedLayers.size())); |
| 193 | for (auto& removedLayer : removedLayers) { |
| 194 | entryProto.mutable_removed_layers()->Add(removedLayer); |
| 195 | } |
| 196 | mCreatedLayers.clear(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 197 | entryProto.mutable_transactions()->Reserve( |
| 198 | static_cast<int32_t>(entry.transactionIds.size())); |
| 199 | for (const uint64_t& id : entry.transactionIds) { |
| 200 | auto it = mQueuedTransactions.find(id); |
| 201 | if (it != mQueuedTransactions.end()) { |
| 202 | entryProto.mutable_transactions()->Add(std::move(it->second)); |
| 203 | mQueuedTransactions.erase(it); |
| 204 | } else { |
Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 205 | ALOGW("Could not find transaction id %" PRIu64, id); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 208 | |
| 209 | std::string serializedProto; |
| 210 | entryProto.SerializeToString(&serializedProto); |
| 211 | entryProto.Clear(); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 212 | std::vector<std::string> entries = mBuffer.emplace(std::move(serializedProto)); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 213 | removedEntries.reserve(removedEntries.size() + entries.size()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 214 | removedEntries.insert(removedEntries.end(), std::make_move_iterator(entries.begin()), |
| 215 | std::make_move_iterator(entries.end())); |
Vishnu Nair | d37343b | 2022-01-12 16:18:56 -0800 | [diff] [blame] | 216 | |
| 217 | entryProto.mutable_removed_layer_handles()->Reserve( |
| 218 | static_cast<int32_t>(mRemovedLayerHandles.size())); |
| 219 | for (auto& handle : mRemovedLayerHandles) { |
| 220 | entryProto.mutable_removed_layer_handles()->Add(handle); |
| 221 | } |
| 222 | mRemovedLayerHandles.clear(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 225 | proto::TransactionTraceEntry removedEntryProto; |
| 226 | for (const std::string& removedEntry : removedEntries) { |
| 227 | removedEntryProto.ParseFromString(removedEntry); |
| 228 | updateStartingStateLocked(removedEntryProto); |
| 229 | removedEntryProto.Clear(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 230 | } |
| 231 | mTransactionsAddedToBufferCv.notify_one(); |
| 232 | } |
| 233 | |
| 234 | void TransactionTracing::flush(int64_t vsyncId) { |
| 235 | while (!mPendingTransactions.empty() || !mPendingRemovedLayers.empty()) { |
| 236 | tryPushToTracingThread(); |
| 237 | } |
| 238 | std::unique_lock<std::mutex> lock(mTraceLock); |
| 239 | base::ScopedLockAssertion assumeLocked(mTraceLock); |
| 240 | mTransactionsAddedToBufferCv.wait(lock, [&]() REQUIRES(mTraceLock) { |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 241 | proto::TransactionTraceEntry entry; |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 242 | if (mBuffer.used() > 0) { |
| 243 | entry.ParseFromString(mBuffer.back()); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 244 | } |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 245 | return mBuffer.used() > 0 && entry.vsync_id() >= vsyncId; |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 246 | }); |
| 247 | } |
| 248 | |
| 249 | void TransactionTracing::onLayerAdded(BBinder* layerHandle, int layerId, const std::string& name, |
| 250 | uint32_t flags, int parentId) { |
| 251 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 252 | TracingLayerCreationArgs args{layerId, name, flags, parentId, -1 /* mirrorFromId */}; |
Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 253 | if (mLayerHandles.find(layerHandle) != mLayerHandles.end()) { |
| 254 | ALOGW("Duplicate handles found. %p", layerHandle); |
| 255 | } |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 256 | mLayerHandles[layerHandle] = layerId; |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 257 | mCreatedLayers.push_back(mProtoParser.toProto(args)); |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void TransactionTracing::onMirrorLayerAdded(BBinder* layerHandle, int layerId, |
| 261 | const std::string& name, int mirrorFromId) { |
| 262 | std::scoped_lock lock(mTraceLock); |
| 263 | TracingLayerCreationArgs args{layerId, name, 0 /* flags */, -1 /* parentId */, mirrorFromId}; |
Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 264 | if (mLayerHandles.find(layerHandle) != mLayerHandles.end()) { |
| 265 | ALOGW("Duplicate handles found. %p", layerHandle); |
| 266 | } |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 267 | mLayerHandles[layerHandle] = layerId; |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 268 | mCreatedLayers.emplace_back(mProtoParser.toProto(args)); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void TransactionTracing::onLayerRemoved(int32_t layerId) { |
| 272 | mPendingRemovedLayers.emplace_back(layerId); |
| 273 | tryPushToTracingThread(); |
| 274 | } |
| 275 | |
Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 276 | void TransactionTracing::onHandleRemoved(BBinder* layerHandle) { |
| 277 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | d37343b | 2022-01-12 16:18:56 -0800 | [diff] [blame] | 278 | auto it = mLayerHandles.find(layerHandle); |
| 279 | if (it == mLayerHandles.end()) { |
| 280 | ALOGW("handle not found. %p", layerHandle); |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | mRemovedLayerHandles.push_back(it->second); |
| 285 | mLayerHandles.erase(it); |
Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 288 | void TransactionTracing::tryPushToTracingThread() { |
| 289 | // Try to acquire the lock from main thread. |
| 290 | if (mMainThreadLock.try_lock()) { |
| 291 | // We got the lock! Collect any pending transactions and continue. |
| 292 | mCommittedTransactions.insert(mCommittedTransactions.end(), |
| 293 | std::make_move_iterator(mPendingTransactions.begin()), |
| 294 | std::make_move_iterator(mPendingTransactions.end())); |
| 295 | mPendingTransactions.clear(); |
| 296 | mRemovedLayers.insert(mRemovedLayers.end(), mPendingRemovedLayers.begin(), |
| 297 | mPendingRemovedLayers.end()); |
| 298 | mPendingRemovedLayers.clear(); |
| 299 | mTransactionsAvailableCv.notify_one(); |
| 300 | mMainThreadLock.unlock(); |
| 301 | } else { |
| 302 | ALOGV("Couldn't get lock"); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 306 | void TransactionTracing::updateStartingStateLocked( |
| 307 | const proto::TransactionTraceEntry& removedEntry) { |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 308 | mStartingTimestamp = removedEntry.elapsed_realtime_nanos(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 309 | // Keep track of layer starting state so we can reconstruct the layer state as we purge |
| 310 | // transactions from the buffer. |
| 311 | for (const proto::LayerCreationArgs& addedLayer : removedEntry.added_layers()) { |
| 312 | TracingLayerState& startingState = mStartingStates[addedLayer.layer_id()]; |
| 313 | startingState.layerId = addedLayer.layer_id(); |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 314 | mProtoParser.fromProto(addedLayer, startingState.args); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // Merge layer states to starting transaction state. |
| 318 | for (const proto::TransactionState& transaction : removedEntry.transactions()) { |
| 319 | for (const proto::LayerState& layerState : transaction.layer_changes()) { |
| 320 | auto it = mStartingStates.find(layerState.layer_id()); |
| 321 | if (it == mStartingStates.end()) { |
Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 322 | ALOGW("Could not find layer id %d", layerState.layer_id()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 323 | continue; |
| 324 | } |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 325 | mProtoParser.mergeFromProto(layerState, it->second); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | // Clean up stale starting states since the layer has been removed and the buffer does not |
| 330 | // contain any references to the layer. |
| 331 | for (const int32_t removedLayerId : removedEntry.removed_layers()) { |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 332 | mStartingStates.erase(removedLayerId); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void TransactionTracing::addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) { |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 337 | if (mStartingStates.size() == 0) { |
| 338 | return; |
| 339 | } |
| 340 | |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 341 | proto::TransactionTraceEntry* entryProto = proto.add_entry(); |
| 342 | entryProto->set_elapsed_realtime_nanos(mStartingTimestamp); |
| 343 | entryProto->set_vsync_id(0); |
| 344 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 345 | entryProto->mutable_added_layers()->Reserve(static_cast<int32_t>(mStartingStates.size())); |
| 346 | for (auto& [layerId, state] : mStartingStates) { |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 347 | entryProto->mutable_added_layers()->Add(mProtoParser.toProto(state.args)); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 350 | proto::TransactionState transactionProto = mProtoParser.toProto(mStartingStates); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 351 | transactionProto.set_vsync_id(0); |
| 352 | transactionProto.set_post_time(mStartingTimestamp); |
| 353 | entryProto->mutable_transactions()->Add(std::move(transactionProto)); |
| 354 | } |
| 355 | |
| 356 | proto::TransactionTraceFile TransactionTracing::writeToProto() { |
| 357 | std::scoped_lock<std::mutex> lock(mTraceLock); |
| 358 | proto::TransactionTraceFile proto = createTraceFileProto(); |
| 359 | addStartingStateToProtoLocked(proto); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 360 | mBuffer.writeToProto(proto); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 361 | return proto; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | } // namespace android |