Introduce ASurfaceTransaction_setOnCommit api
Introduce a new callback for SurfaceControl transactions that
fire after we commit a transaction in SurfaceFlinger. This
will help some clients pace when they should apply the next
transaction so it get applied on the next vsync. If they wait for
the existing transaction complete callback, there may not be
enough time between when the client applies the transaction
and surface flinger waking up and apply it on the new vsync.
This would mean the update would arrive a frame late.
Bug: 185843251
Test: atest ASurfaceControlTest
Change-Id: If0d5d01a1d5c2029eb81667356e666d7297376d4
diff --git a/native/android/surface_control.cpp b/native/android/surface_control.cpp
index 3d14c42..087837a 100644
--- a/native/android/surface_control.cpp
+++ b/native/android/surface_control.cpp
@@ -260,6 +260,7 @@
std::unordered_map<ASurfaceControl*, ASurfaceControlStats> aSurfaceControlStats;
int64_t latchTime;
sp<Fence> presentFence;
+ bool transactionCompleted;
};
int64_t ASurfaceTransactionStats_getLatchTime(ASurfaceTransactionStats* aSurfaceTransactionStats) {
@@ -269,6 +270,9 @@
int ASurfaceTransactionStats_getPresentFenceFd(ASurfaceTransactionStats* aSurfaceTransactionStats) {
CHECK_NOT_NULL(aSurfaceTransactionStats);
+ LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted,
+ "ASurfaceTransactionStats queried from an incomplete transaction callback");
+
auto& presentFence = aSurfaceTransactionStats->presentFence;
return (presentFence) ? presentFence->dup() : -1;
}
@@ -313,6 +317,8 @@
ASurfaceTransactionStats* aSurfaceTransactionStats, ASurfaceControl* aSurfaceControl) {
CHECK_NOT_NULL(aSurfaceTransactionStats);
CHECK_NOT_NULL(aSurfaceControl);
+ LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted,
+ "ASurfaceTransactionStats queried from an incomplete transaction callback");
const auto& aSurfaceControlStats =
aSurfaceTransactionStats->aSurfaceControlStats.find(aSurfaceControl);
@@ -334,7 +340,6 @@
void ASurfaceTransaction_setOnComplete(ASurfaceTransaction* aSurfaceTransaction, void* context,
ASurfaceTransaction_OnComplete func) {
CHECK_NOT_NULL(aSurfaceTransaction);
- CHECK_NOT_NULL(context);
CHECK_NOT_NULL(func);
TransactionCompletedCallbackTakesContext callback = [func](void* callback_context,
@@ -345,6 +350,7 @@
aSurfaceTransactionStats.latchTime = latchTime;
aSurfaceTransactionStats.presentFence = presentFence;
+ aSurfaceTransactionStats.transactionCompleted = true;
auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats;
@@ -695,3 +701,33 @@
layer_state_t::eEnableBackpressure : 0;
transaction->setFlags(surfaceControl, flags, layer_state_t::eEnableBackpressure);
}
+
+void ASurfaceTransaction_setOnCommit(ASurfaceTransaction* aSurfaceTransaction, void* context,
+ ASurfaceTransaction_OnCommit func) {
+ CHECK_NOT_NULL(aSurfaceTransaction);
+ CHECK_NOT_NULL(func);
+
+ TransactionCompletedCallbackTakesContext callback =
+ [func](void* callback_context, nsecs_t latchTime, const sp<Fence>& /* presentFence */,
+ const std::vector<SurfaceControlStats>& surfaceControlStats) {
+ ASurfaceTransactionStats aSurfaceTransactionStats;
+ aSurfaceTransactionStats.latchTime = latchTime;
+ aSurfaceTransactionStats.transactionCompleted = false;
+
+ auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats;
+ for (const auto&
+ [surfaceControl, latchTime, acquireTime, presentFence,
+ previousReleaseFence, transformHint,
+ frameEvents] : surfaceControlStats) {
+ ASurfaceControl* aSurfaceControl =
+ reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
+ aSurfaceControlStats[aSurfaceControl].acquireTime = acquireTime;
+ }
+
+ (*func)(callback_context, &aSurfaceTransactionStats);
+ };
+
+ Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
+
+ transaction->addTransactionCommittedCallback(callback, context);
+}
\ No newline at end of file