LayerTraceGenerator: Fix duplicate layer ids
Layers can be created from API calls on the binder thread or
by surfaceflinger on the main thread for mirroring and
BSL background layers. Transaction traces keep track of
layer creation API calls to recreate layers when generating
the layer trace.
However, there was a possibility that new layers would be
recorded with a set of transactions that were applied with
an earlier vsync. This is harmless except we could end up
creating layers with duplicate layer ids since layers created
via binder would get an injected sequence id while main
thread created layers would use the global counter.
Test: atest transactiontrace_testsuite
Bug: 235376060
Change-Id: Ia11839d1880bc131ab5314779281c93393d6742f
diff --git a/services/surfaceflinger/Tracing/TransactionTracing.cpp b/services/surfaceflinger/Tracing/TransactionTracing.cpp
index 6381758..e53feca 100644
--- a/services/surfaceflinger/Tracing/TransactionTracing.cpp
+++ b/services/surfaceflinger/Tracing/TransactionTracing.cpp
@@ -152,17 +152,33 @@
mTransactionQueue.push(state);
}
-void TransactionTracing::addCommittedTransactions(std::vector<TransactionState>& transactions,
- int64_t vsyncId) {
+TransactionTracing::CommittedTransactions&
+TransactionTracing::findOrCreateCommittedTransactionRecord(int64_t vsyncId) {
+ for (auto& pendingTransaction : mPendingTransactions) {
+ if (pendingTransaction.vsyncId == vsyncId) {
+ return pendingTransaction;
+ }
+ }
+
CommittedTransactions committedTransactions;
committedTransactions.vsyncId = vsyncId;
committedTransactions.timestamp = systemTime();
+ mPendingTransactions.emplace_back(committedTransactions);
+ return mPendingTransactions.back();
+}
+
+void TransactionTracing::onLayerAddedToDrawingState(int layerId, int64_t vsyncId) {
+ CommittedTransactions& committedTransactions = findOrCreateCommittedTransactionRecord(vsyncId);
+ committedTransactions.createdLayerIds.emplace_back(layerId);
+}
+
+void TransactionTracing::addCommittedTransactions(std::vector<TransactionState>& transactions,
+ int64_t vsyncId) {
+ CommittedTransactions& committedTransactions = findOrCreateCommittedTransactionRecord(vsyncId);
committedTransactions.transactionIds.reserve(transactions.size());
for (const auto& transaction : transactions) {
committedTransactions.transactionIds.emplace_back(transaction.id);
}
-
- mPendingTransactions.emplace_back(committedTransactions);
tryPushToTracingThread();
}
@@ -235,15 +251,24 @@
for (const CommittedTransactions& entry : committedTransactions) {
entryProto.set_elapsed_realtime_nanos(entry.timestamp);
entryProto.set_vsync_id(entry.vsyncId);
- entryProto.mutable_added_layers()->Reserve(static_cast<int32_t>(mCreatedLayers.size()));
- for (auto& newLayer : mCreatedLayers) {
- entryProto.mutable_added_layers()->Add(std::move(newLayer));
+ entryProto.mutable_added_layers()->Reserve(
+ static_cast<int32_t>(entry.createdLayerIds.size()));
+
+ for (const int32_t& id : entry.createdLayerIds) {
+ auto it = mCreatedLayers.find(id);
+ if (it != mCreatedLayers.end()) {
+ entryProto.mutable_added_layers()->Add(std::move(it->second));
+ mCreatedLayers.erase(it);
+ } else {
+ ALOGW("Could not created layer with id %d", id);
+ }
}
+
entryProto.mutable_removed_layers()->Reserve(static_cast<int32_t>(removedLayers.size()));
for (auto& removedLayer : removedLayers) {
entryProto.mutable_removed_layers()->Add(removedLayer);
+ mCreatedLayers.erase(removedLayer);
}
- mCreatedLayers.clear();
entryProto.mutable_transactions()->Reserve(
static_cast<int32_t>(entry.transactionIds.size()));
for (const uint64_t& id : entry.transactionIds) {
@@ -304,7 +329,7 @@
ALOGW("Duplicate handles found. %p", layerHandle);
}
mLayerHandles[layerHandle] = layerId;
- mCreatedLayers.push_back(mProtoParser.toProto(args));
+ mCreatedLayers[layerId] = mProtoParser.toProto(args);
}
void TransactionTracing::onMirrorLayerAdded(BBinder* layerHandle, int layerId,
@@ -315,7 +340,7 @@
ALOGW("Duplicate handles found. %p", layerHandle);
}
mLayerHandles[layerHandle] = layerId;
- mCreatedLayers.emplace_back(mProtoParser.toProto(args));
+ mCreatedLayers[layerId] = mProtoParser.toProto(args);
}
void TransactionTracing::onLayerRemoved(int32_t layerId) {