blob: 6a155c17df0f1dc0614595d0a3b3c48f3f673dac [file] [log] [blame]
Robert Carr9a803c32021-01-14 16:57:58 -08001/*
2 * Copyright 2018 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// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
21//#define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "TransactionCallbackInvoker"
24#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
26#include "TransactionCallbackInvoker.h"
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080027#include "BackgroundExecutor.h"
Robert Carr9a803c32021-01-14 16:57:58 -080028
29#include <cinttypes>
30
31#include <binder/IInterface.h>
32#include <utils/RefBase.h>
33
34namespace android {
35
36// Returns 0 if they are equal
37// <0 if the first id that doesn't match is lower in c2 or all ids match but c2 is shorter
38// >0 if the first id that doesn't match is greater in c2 or all ids match but c2 is longer
39//
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070040// See CallbackIdsHash for a explanation of why this works
Robert Carr9a803c32021-01-14 16:57:58 -080041static int compareCallbackIds(const std::vector<CallbackId>& c1,
42 const std::vector<CallbackId>& c2) {
43 if (c1.empty()) {
44 return !c2.empty();
45 }
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070046 return c1.front().id - c2.front().id;
47}
48
49static bool containsOnCommitCallbacks(const std::vector<CallbackId>& callbacks) {
50 return !callbacks.empty() && callbacks.front().type == CallbackId::Type::ON_COMMIT;
Robert Carr9a803c32021-01-14 16:57:58 -080051}
52
Jiakai Zhanga5505cb2021-11-09 11:46:30 +000053void TransactionCallbackInvoker::addEmptyTransaction(const ListenerCallbacks& listenerCallbacks) {
Robert Carr9a803c32021-01-14 16:57:58 -080054 auto& [listener, callbackIds] = listenerCallbacks;
Jiakai Zhanga5505cb2021-11-09 11:46:30 +000055 auto& transactionStatsDeque = mCompletedTransactions[listener];
56 transactionStatsDeque.emplace_back(callbackIds);
Robert Carr9a803c32021-01-14 16:57:58 -080057}
58
Robert Carr3d1047b2021-09-20 18:22:32 -070059status_t TransactionCallbackInvoker::addOnCommitCallbackHandles(
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070060 const std::deque<sp<CallbackHandle>>& handles,
61 std::deque<sp<CallbackHandle>>& outRemainingHandles) {
62 if (handles.empty()) {
63 return NO_ERROR;
64 }
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070065 const std::vector<JankData>& jankData = std::vector<JankData>();
66 for (const auto& handle : handles) {
67 if (!containsOnCommitCallbacks(handle->callbackIds)) {
68 outRemainingHandles.push_back(handle);
69 continue;
70 }
Robert Carr3d1047b2021-09-20 18:22:32 -070071 status_t err = addCallbackHandle(handle, jankData);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070072 if (err != NO_ERROR) {
73 return err;
74 }
75 }
76
77 return NO_ERROR;
78}
79
Robert Carr3d1047b2021-09-20 18:22:32 -070080status_t TransactionCallbackInvoker::addCallbackHandles(
Robert Carr9a803c32021-01-14 16:57:58 -080081 const std::deque<sp<CallbackHandle>>& handles, const std::vector<JankData>& jankData) {
82 if (handles.empty()) {
83 return NO_ERROR;
84 }
Robert Carr9a803c32021-01-14 16:57:58 -080085 for (const auto& handle : handles) {
Robert Carr3d1047b2021-09-20 18:22:32 -070086 status_t err = addCallbackHandle(handle, jankData);
Robert Carr9a803c32021-01-14 16:57:58 -080087 if (err != NO_ERROR) {
Robert Carr9a803c32021-01-14 16:57:58 -080088 return err;
89 }
90 }
91
92 return NO_ERROR;
93}
94
Vishnu Naircd52e2d2021-10-18 08:42:46 -070095status_t TransactionCallbackInvoker::findOrCreateTransactionStats(
Robert Carr9a803c32021-01-14 16:57:58 -080096 const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds,
97 TransactionStats** outTransactionStats) {
98 auto& transactionStatsDeque = mCompletedTransactions[listener];
99
100 // Search back to front because the most recent transactions are at the back of the deque
101 auto itr = transactionStatsDeque.rbegin();
102 for (; itr != transactionStatsDeque.rend(); itr++) {
103 if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) {
104 *outTransactionStats = &(*itr);
105 return NO_ERROR;
106 }
107 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700108 *outTransactionStats = &transactionStatsDeque.emplace_back(callbackIds);
109 return NO_ERROR;
Robert Carr9a803c32021-01-14 16:57:58 -0800110}
111
112status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle,
113 const std::vector<JankData>& jankData) {
114 // If we can't find the transaction stats something has gone wrong. The client should call
115 // startRegistration before trying to add a callback handle.
116 TransactionStats* transactionStats;
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700117 status_t err =
118 findOrCreateTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
Robert Carr9a803c32021-01-14 16:57:58 -0800119 if (err != NO_ERROR) {
120 return err;
121 }
122
123 transactionStats->latchTime = handle->latchTime;
124 // If the layer has already been destroyed, don't add the SurfaceControl to the callback.
125 // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been
126 // destroyed the client side is dead and there won't be anyone to send the callback to.
127 sp<IBinder> surfaceControl = handle->surfaceControl.promote();
128 if (surfaceControl) {
Sally Qi59a9f502021-10-12 18:53:23 +0000129 sp<Fence> prevFence = nullptr;
130
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700131 for (const auto& future : handle->previousReleaseFences) {
132 sp<Fence> currentFence = future.get().value_or(Fence::NO_FENCE);
Sally Qi59a9f502021-10-12 18:53:23 +0000133 if (prevFence == nullptr && currentFence->getStatus() != Fence::Status::Invalid) {
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700134 prevFence = std::move(currentFence);
Sally Qi59a9f502021-10-12 18:53:23 +0000135 } else if (prevFence != nullptr) {
136 // If both fences are signaled or both are unsignaled, we need to merge
137 // them to get an accurate timestamp.
138 if (prevFence->getStatus() != Fence::Status::Invalid &&
139 prevFence->getStatus() == currentFence->getStatus()) {
140 char fenceName[32] = {};
141 snprintf(fenceName, 32, "%.28s", handle->name.c_str());
142 sp<Fence> mergedFence = Fence::merge(fenceName, prevFence, currentFence);
143 if (mergedFence->isValid()) {
Sally Qi4b9e7c42023-01-03 11:11:25 -0800144 prevFence = std::move(mergedFence);
Sally Qi59a9f502021-10-12 18:53:23 +0000145 }
146 } else if (currentFence->getStatus() == Fence::Status::Unsignaled) {
147 // If one fence has signaled and the other hasn't, the unsignaled
148 // fence will approximately correspond with the correct timestamp.
149 // There's a small race if both fences signal at about the same time
150 // and their statuses are retrieved with unfortunate timing. However,
151 // by this point, they will have both signaled and only the timestamp
152 // will be slightly off; any dependencies after this point will
153 // already have been met.
Sally Qi4b9e7c42023-01-03 11:11:25 -0800154 prevFence = std::move(currentFence);
Sally Qi59a9f502021-10-12 18:53:23 +0000155 }
156 }
157 }
Sally Qi4b9e7c42023-01-03 11:11:25 -0800158 handle->previousReleaseFence = prevFence;
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700159 handle->previousReleaseFences.clear();
160
Alec Mouri21d94322023-10-17 19:51:39 +0000161 FrameEventHistoryStats eventStats(handle->frameNumber, handle->previousFrameNumber,
Robert Carr9a803c32021-01-14 16:57:58 -0800162 handle->gpuCompositionDoneFence->getSnapshot().fence,
163 handle->compositorTiming, handle->refreshStartTime,
164 handle->dequeueReadyTime);
Ady Abraham461296a2022-01-21 11:11:31 -0800165 transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTimeOrFence,
Robert Carr9a803c32021-01-14 16:57:58 -0800166 handle->previousReleaseFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700167 handle->transformHint,
168 handle->currentMaxAcquiredBufferCount,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700169 eventStats, jankData,
170 handle->previousReleaseCallbackId);
Robert Carr9a803c32021-01-14 16:57:58 -0800171 }
172 return NO_ERROR;
173}
174
Dominik Laskowski8792c112022-07-12 09:03:39 -0700175void TransactionCallbackInvoker::addPresentFence(sp<Fence> presentFence) {
176 mPresentFence = std::move(presentFence);
Robert Carr9a803c32021-01-14 16:57:58 -0800177}
178
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700179void TransactionCallbackInvoker::sendCallbacks(bool onCommitOnly) {
Robert Carr9a803c32021-01-14 16:57:58 -0800180 // For each listener
181 auto completedTransactionsItr = mCompletedTransactions.begin();
Alec Mouri8d7d0f42022-05-10 23:33:40 +0000182 BackgroundExecutor::Callbacks callbacks;
Robert Carr9a803c32021-01-14 16:57:58 -0800183 while (completedTransactionsItr != mCompletedTransactions.end()) {
184 auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
185 ListenerStats listenerStats;
186 listenerStats.listener = listener;
187
188 // For each transaction
189 auto transactionStatsItr = transactionStatsDeque.begin();
190 while (transactionStatsItr != transactionStatsDeque.end()) {
191 auto& transactionStats = *transactionStatsItr;
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700192 if (onCommitOnly && !containsOnCommitCallbacks(transactionStats.callbackIds)) {
193 transactionStatsItr++;
194 continue;
195 }
Robert Carr9a803c32021-01-14 16:57:58 -0800196
Robert Carr9a803c32021-01-14 16:57:58 -0800197 // If the transaction has been latched
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700198 if (transactionStats.latchTime >= 0 &&
199 !containsOnCommitCallbacks(transactionStats.callbackIds)) {
Robert Carr9a803c32021-01-14 16:57:58 -0800200 transactionStats.presentFence = mPresentFence;
201 }
202
203 // Remove the transaction from completed to the callback
204 listenerStats.transactionStats.push_back(std::move(transactionStats));
205 transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
206 }
207 // If the listener has completed transactions
208 if (!listenerStats.transactionStats.empty()) {
209 // If the listener is still alive
210 if (listener->isBinderAlive()) {
211 // Send callback. The listener stored in listenerStats
212 // comes from the cross-process setTransactionState call to
213 // SF. This MUST be an ITransactionCompletedListener. We
214 // keep it as an IBinder due to consistency reasons: if we
215 // interface_cast at the IPC boundary when reading a Parcel,
216 // we get pointers that compare unequal in the SF process.
Alec Mouri8d7d0f42022-05-10 23:33:40 +0000217 callbacks.emplace_back([stats = std::move(listenerStats)]() {
Vishnu Nair34eb9ca2021-11-18 15:23:23 -0800218 interface_cast<ITransactionCompletedListener>(stats.listener)
219 ->onTransactionCompleted(stats);
220 });
Robert Carr9a803c32021-01-14 16:57:58 -0800221 }
Robert Carr9a803c32021-01-14 16:57:58 -0800222 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700223 completedTransactionsItr++;
Robert Carr9a803c32021-01-14 16:57:58 -0800224 }
225
226 if (mPresentFence) {
227 mPresentFence.clear();
228 }
Alec Mouri8d7d0f42022-05-10 23:33:40 +0000229
230 BackgroundExecutor::getInstance().sendCallbacks(std::move(callbacks));
Robert Carr9a803c32021-01-14 16:57:58 -0800231}
232
233// -----------------------------------------------------------------------
234
235CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
236 const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
237 : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
238
239} // namespace android
240
241// TODO(b/129481165): remove the #pragma below and fix conversion issues
242#pragma clang diagnostic pop // ignored "-Wconversion"