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