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 | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 26 | #include "Client.h" |
| 27 | #include "FrontEnd/LayerCreationArgs.h" |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 28 | #include "TransactionTracing.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 32 | TransactionTracing::TransactionTracing() |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 33 | : mProtoParser(std::make_unique<TransactionProtoParser::FlingerDataMapper>()) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 34 | std::scoped_lock lock(mTraceLock); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 35 | |
| 36 | mBuffer.setSize(mBufferSizeInBytes); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 37 | mStartingTimestamp = systemTime(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 38 | { |
| 39 | std::scoped_lock lock(mMainThreadLock); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 40 | mThread = std::thread(&TransactionTracing::loop, this); |
| 41 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 44 | TransactionTracing::~TransactionTracing() { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 45 | std::thread thread; |
| 46 | { |
| 47 | std::scoped_lock lock(mMainThreadLock); |
| 48 | mDone = true; |
| 49 | mTransactionsAvailableCv.notify_all(); |
| 50 | thread = std::move(mThread); |
| 51 | } |
| 52 | if (thread.joinable()) { |
| 53 | thread.join(); |
| 54 | } |
| 55 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 56 | writeToFile(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Vishnu Nair | d8f5e9f | 2022-02-03 10:23:28 -0800 | [diff] [blame] | 59 | status_t TransactionTracing::writeToFile(std::string filename) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 60 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 61 | proto::TransactionTraceFile fileProto = createTraceFileProto(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 62 | addStartingStateToProtoLocked(fileProto); |
Vishnu Nair | d8f5e9f | 2022-02-03 10:23:28 -0800 | [diff] [blame] | 63 | return mBuffer.writeToFile(fileProto, filename); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void TransactionTracing::setBufferSize(size_t bufferSizeInBytes) { |
| 67 | std::scoped_lock lock(mTraceLock); |
| 68 | mBufferSizeInBytes = bufferSizeInBytes; |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 69 | mBuffer.setSize(mBufferSizeInBytes); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | proto::TransactionTraceFile TransactionTracing::createTraceFileProto() const { |
| 73 | proto::TransactionTraceFile proto; |
| 74 | proto.set_magic_number(uint64_t(proto::TransactionTraceFile_MagicNumber_MAGIC_NUMBER_H) << 32 | |
| 75 | proto::TransactionTraceFile_MagicNumber_MAGIC_NUMBER_L); |
Kean Mariotti | c44fdaf | 2022-07-29 14:20:39 +0000 | [diff] [blame] | 76 | auto timeOffsetNs = static_cast<std::uint64_t>(systemTime(SYSTEM_TIME_REALTIME) - |
| 77 | systemTime(SYSTEM_TIME_MONOTONIC)); |
| 78 | proto.set_real_to_elapsed_time_offset_nanos(timeOffsetNs); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 79 | proto.set_version(TRACING_VERSION); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 80 | return proto; |
| 81 | } |
| 82 | |
| 83 | void TransactionTracing::dump(std::string& result) const { |
| 84 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 85 | base::StringAppendF(&result, " queued transactions=%zu created layers=%zu states=%zu\n", |
| 86 | mQueuedTransactions.size(), mCreatedLayers.size(), mStartingStates.size()); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 87 | mBuffer.dump(result); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void TransactionTracing::addQueuedTransaction(const TransactionState& transaction) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 91 | proto::TransactionState* state = new proto::TransactionState(mProtoParser.toProto(transaction)); |
Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 92 | mTransactionQueue.push(state); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 95 | void TransactionTracing::addCommittedTransactions(int64_t vsyncId, nsecs_t commitTime, |
| 96 | frontend::Update& newUpdate, |
| 97 | const frontend::DisplayInfos& displayInfos, |
| 98 | bool displayInfoChanged) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 99 | CommittedUpdates update; |
| 100 | update.vsyncId = vsyncId; |
| 101 | update.timestamp = commitTime; |
| 102 | update.transactionIds.reserve(newUpdate.transactions.size()); |
| 103 | for (const auto& transaction : newUpdate.transactions) { |
| 104 | update.transactionIds.emplace_back(transaction.id); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 105 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 106 | update.displayInfoChanged = displayInfoChanged; |
| 107 | if (displayInfoChanged) { |
| 108 | update.displayInfos = displayInfos; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 109 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 110 | update.createdLayers = std::move(newUpdate.layerCreationArgs); |
| 111 | newUpdate.layerCreationArgs.clear(); |
| 112 | update.destroyedLayerHandles.reserve(newUpdate.destroyedHandles.size()); |
| 113 | for (uint32_t handle : newUpdate.destroyedHandles) { |
| 114 | update.destroyedLayerHandles.push_back(handle); |
| 115 | } |
| 116 | mPendingUpdates.emplace_back(update); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 117 | tryPushToTracingThread(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void TransactionTracing::loop() { |
| 121 | while (true) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 122 | std::vector<CommittedUpdates> committedUpdates; |
| 123 | std::vector<uint32_t> destroyedLayers; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 124 | { |
| 125 | std::unique_lock<std::mutex> lock(mMainThreadLock); |
| 126 | base::ScopedLockAssertion assumeLocked(mMainThreadLock); |
| 127 | mTransactionsAvailableCv.wait(lock, [&]() REQUIRES(mMainThreadLock) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 128 | return mDone || !mUpdates.empty(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 129 | }); |
| 130 | if (mDone) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 131 | mUpdates.clear(); |
| 132 | mDestroyedLayers.clear(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 133 | break; |
| 134 | } |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 135 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 136 | destroyedLayers = std::move(mDestroyedLayers); |
| 137 | mDestroyedLayers.clear(); |
| 138 | committedUpdates = std::move(mUpdates); |
| 139 | mUpdates.clear(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 140 | } // unlock mMainThreadLock |
| 141 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 142 | if (!committedUpdates.empty() || !destroyedLayers.empty()) { |
| 143 | addEntry(committedUpdates, destroyedLayers); |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 144 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 148 | void TransactionTracing::addEntry(const std::vector<CommittedUpdates>& committedUpdates, |
| 149 | const std::vector<uint32_t>& destroyedLayers) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 150 | ATRACE_CALL(); |
| 151 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 152 | std::vector<std::string> removedEntries; |
| 153 | proto::TransactionTraceEntry entryProto; |
Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 154 | |
| 155 | while (auto incomingTransaction = mTransactionQueue.pop()) { |
| 156 | auto transaction = *incomingTransaction; |
Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 157 | mQueuedTransactions[incomingTransaction->transaction_id()] = transaction; |
| 158 | delete incomingTransaction; |
| 159 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 160 | for (const CommittedUpdates& update : committedUpdates) { |
| 161 | entryProto.set_elapsed_realtime_nanos(update.timestamp); |
| 162 | entryProto.set_vsync_id(update.vsyncId); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 163 | entryProto.mutable_added_layers()->Reserve( |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 164 | static_cast<int32_t>(update.createdLayers.size())); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 165 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 166 | for (const auto& args : update.createdLayers) { |
| 167 | entryProto.mutable_added_layers()->Add(std::move(mProtoParser.toProto(args))); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 168 | } |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 169 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 170 | entryProto.mutable_destroyed_layers()->Reserve( |
| 171 | static_cast<int32_t>(destroyedLayers.size())); |
| 172 | for (auto& destroyedLayer : destroyedLayers) { |
| 173 | entryProto.mutable_destroyed_layers()->Add(destroyedLayer); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 174 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 175 | entryProto.mutable_transactions()->Reserve( |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 176 | static_cast<int32_t>(update.transactionIds.size())); |
| 177 | for (const uint64_t& id : update.transactionIds) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 178 | auto it = mQueuedTransactions.find(id); |
| 179 | if (it != mQueuedTransactions.end()) { |
| 180 | entryProto.mutable_transactions()->Add(std::move(it->second)); |
| 181 | mQueuedTransactions.erase(it); |
| 182 | } else { |
Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 183 | ALOGW("Could not find transaction id %" PRIu64, id); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 186 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 187 | entryProto.mutable_destroyed_layer_handles()->Reserve( |
| 188 | static_cast<int32_t>(update.destroyedLayerHandles.size())); |
| 189 | for (auto layerId : update.destroyedLayerHandles) { |
| 190 | entryProto.mutable_destroyed_layer_handles()->Add(layerId); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 191 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 192 | |
| 193 | entryProto.set_displays_changed(update.displayInfoChanged); |
| 194 | if (update.displayInfoChanged) { |
| 195 | entryProto.mutable_displays()->Reserve( |
| 196 | static_cast<int32_t>(update.displayInfos.size())); |
| 197 | for (auto& [layerStack, displayInfo] : update.displayInfos) { |
| 198 | entryProto.mutable_displays()->Add( |
| 199 | std::move(mProtoParser.toProto(displayInfo, layerStack.id))); |
| 200 | } |
| 201 | } |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 202 | |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 203 | std::string serializedProto; |
| 204 | entryProto.SerializeToString(&serializedProto); |
| 205 | entryProto.Clear(); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 206 | std::vector<std::string> entries = mBuffer.emplace(std::move(serializedProto)); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 207 | removedEntries.reserve(removedEntries.size() + entries.size()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 208 | removedEntries.insert(removedEntries.end(), std::make_move_iterator(entries.begin()), |
| 209 | std::make_move_iterator(entries.end())); |
| 210 | } |
| 211 | |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 212 | proto::TransactionTraceEntry removedEntryProto; |
| 213 | for (const std::string& removedEntry : removedEntries) { |
| 214 | removedEntryProto.ParseFromString(removedEntry); |
| 215 | updateStartingStateLocked(removedEntryProto); |
| 216 | removedEntryProto.Clear(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 217 | } |
| 218 | mTransactionsAddedToBufferCv.notify_one(); |
| 219 | } |
| 220 | |
| 221 | void TransactionTracing::flush(int64_t vsyncId) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 222 | while (!mPendingUpdates.empty() || !mPendingDestroyedLayers.empty()) { |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 223 | tryPushToTracingThread(); |
| 224 | } |
| 225 | std::unique_lock<std::mutex> lock(mTraceLock); |
| 226 | base::ScopedLockAssertion assumeLocked(mTraceLock); |
| 227 | mTransactionsAddedToBufferCv.wait(lock, [&]() REQUIRES(mTraceLock) { |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 228 | proto::TransactionTraceEntry entry; |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 229 | if (mBuffer.used() > 0) { |
| 230 | entry.ParseFromString(mBuffer.back()); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 231 | } |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 232 | return mBuffer.used() > 0 && entry.vsync_id() >= vsyncId; |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 233 | }); |
| 234 | } |
| 235 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 236 | void TransactionTracing::onLayerRemoved(int32_t layerId) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 237 | mPendingDestroyedLayers.emplace_back(layerId); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 238 | tryPushToTracingThread(); |
| 239 | } |
| 240 | |
| 241 | void TransactionTracing::tryPushToTracingThread() { |
| 242 | // Try to acquire the lock from main thread. |
| 243 | if (mMainThreadLock.try_lock()) { |
| 244 | // We got the lock! Collect any pending transactions and continue. |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 245 | mUpdates.insert(mUpdates.end(), std::make_move_iterator(mPendingUpdates.begin()), |
| 246 | std::make_move_iterator(mPendingUpdates.end())); |
| 247 | mPendingUpdates.clear(); |
| 248 | mDestroyedLayers.insert(mDestroyedLayers.end(), mPendingDestroyedLayers.begin(), |
| 249 | mPendingDestroyedLayers.end()); |
| 250 | mPendingDestroyedLayers.clear(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 251 | mTransactionsAvailableCv.notify_one(); |
| 252 | mMainThreadLock.unlock(); |
| 253 | } else { |
| 254 | ALOGV("Couldn't get lock"); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 258 | void TransactionTracing::updateStartingStateLocked( |
| 259 | const proto::TransactionTraceEntry& removedEntry) { |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 260 | mStartingTimestamp = removedEntry.elapsed_realtime_nanos(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 261 | // Keep track of layer starting state so we can reconstruct the layer state as we purge |
| 262 | // transactions from the buffer. |
| 263 | for (const proto::LayerCreationArgs& addedLayer : removedEntry.added_layers()) { |
| 264 | TracingLayerState& startingState = mStartingStates[addedLayer.layer_id()]; |
| 265 | startingState.layerId = addedLayer.layer_id(); |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 266 | mProtoParser.fromProto(addedLayer, startingState.args); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Merge layer states to starting transaction state. |
| 270 | for (const proto::TransactionState& transaction : removedEntry.transactions()) { |
| 271 | for (const proto::LayerState& layerState : transaction.layer_changes()) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 272 | auto it = mStartingStates.find(layerState.layer_id()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 273 | if (it == mStartingStates.end()) { |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 274 | // TODO(b/238781169) make this log fatal when we switch over to using new fe |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 275 | ALOGW("Could not find layer id %d", layerState.layer_id()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 276 | continue; |
| 277 | } |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 278 | mProtoParser.mergeFromProto(layerState, it->second); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 282 | for (const uint32_t destroyedLayerHandleId : removedEntry.destroyed_layer_handles()) { |
| 283 | mRemovedLayerHandlesAtStart.insert(destroyedLayerHandleId); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 286 | // Clean up stale starting states since the layer has been removed and the buffer does not |
| 287 | // contain any references to the layer. |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 288 | for (const uint32_t destroyedLayerId : removedEntry.destroyed_layers()) { |
| 289 | mStartingStates.erase(destroyedLayerId); |
| 290 | mRemovedLayerHandlesAtStart.erase(destroyedLayerId); |
| 291 | } |
| 292 | |
| 293 | if (removedEntry.displays_changed()) { |
| 294 | mProtoParser.fromProto(removedEntry.displays(), mStartingDisplayInfos); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | void TransactionTracing::addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) { |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 299 | if (mStartingStates.size() == 0) { |
| 300 | return; |
| 301 | } |
| 302 | |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 303 | proto::TransactionTraceEntry* entryProto = proto.add_entry(); |
| 304 | entryProto->set_elapsed_realtime_nanos(mStartingTimestamp); |
| 305 | entryProto->set_vsync_id(0); |
| 306 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 307 | entryProto->mutable_added_layers()->Reserve(static_cast<int32_t>(mStartingStates.size())); |
| 308 | for (auto& [layerId, state] : mStartingStates) { |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 309 | entryProto->mutable_added_layers()->Add(mProtoParser.toProto(state.args)); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 312 | proto::TransactionState transactionProto = mProtoParser.toProto(mStartingStates); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 313 | transactionProto.set_vsync_id(0); |
| 314 | transactionProto.set_post_time(mStartingTimestamp); |
| 315 | entryProto->mutable_transactions()->Add(std::move(transactionProto)); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 316 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 317 | entryProto->mutable_destroyed_layer_handles()->Reserve( |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 318 | static_cast<int32_t>(mRemovedLayerHandlesAtStart.size())); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 319 | for (const uint32_t destroyedLayerHandleId : mRemovedLayerHandlesAtStart) { |
| 320 | entryProto->mutable_destroyed_layer_handles()->Add(destroyedLayerHandleId); |
| 321 | } |
| 322 | |
| 323 | entryProto->mutable_displays()->Reserve(static_cast<int32_t>(mStartingDisplayInfos.size())); |
| 324 | for (auto& [layerStack, displayInfo] : mStartingDisplayInfos) { |
| 325 | entryProto->mutable_displays()->Add(mProtoParser.toProto(displayInfo, layerStack.id)); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 326 | } |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | proto::TransactionTraceFile TransactionTracing::writeToProto() { |
| 330 | std::scoped_lock<std::mutex> lock(mTraceLock); |
| 331 | proto::TransactionTraceFile proto = createTraceFileProto(); |
| 332 | addStartingStateToProtoLocked(proto); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 333 | mBuffer.writeToProto(proto); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 334 | return proto; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | } // namespace android |