blob: c6856aea75d4dde8e811415e3c6ee14115926442 [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"
Alec Mouri9892aac2023-12-11 21:16:59 +000028#include "Utils/FenceUtils.h"
Robert Carr9a803c32021-01-14 16:57:58 -080029
30#include <binder/IInterface.h>
Melody Hsu793f8362024-01-08 20:00:35 +000031#include <common/FlagManager.h>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000032#include <common/trace.h>
Robert Carr9a803c32021-01-14 16:57:58 -080033#include <utils/RefBase.h>
34
35namespace android {
36
37// Returns 0 if they are equal
38// <0 if the first id that doesn't match is lower in c2 or all ids match but c2 is shorter
39// >0 if the first id that doesn't match is greater in c2 or all ids match but c2 is longer
40//
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070041// See CallbackIdsHash for a explanation of why this works
Robert Carr9a803c32021-01-14 16:57:58 -080042static int compareCallbackIds(const std::vector<CallbackId>& c1,
43 const std::vector<CallbackId>& c2) {
44 if (c1.empty()) {
45 return !c2.empty();
46 }
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070047 return c1.front().id - c2.front().id;
48}
49
50static bool containsOnCommitCallbacks(const std::vector<CallbackId>& callbacks) {
51 return !callbacks.empty() && callbacks.front().type == CallbackId::Type::ON_COMMIT;
Robert Carr9a803c32021-01-14 16:57:58 -080052}
53
Jiakai Zhanga5505cb2021-11-09 11:46:30 +000054void TransactionCallbackInvoker::addEmptyTransaction(const ListenerCallbacks& listenerCallbacks) {
Robert Carr9a803c32021-01-14 16:57:58 -080055 auto& [listener, callbackIds] = listenerCallbacks;
Jiakai Zhanga5505cb2021-11-09 11:46:30 +000056 auto& transactionStatsDeque = mCompletedTransactions[listener];
57 transactionStatsDeque.emplace_back(callbackIds);
Robert Carr9a803c32021-01-14 16:57:58 -080058}
59
Robert Carr3d1047b2021-09-20 18:22:32 -070060status_t TransactionCallbackInvoker::addOnCommitCallbackHandles(
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070061 const std::deque<sp<CallbackHandle>>& handles,
62 std::deque<sp<CallbackHandle>>& outRemainingHandles) {
63 if (handles.empty()) {
64 return NO_ERROR;
65 }
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070066 for (const auto& handle : handles) {
67 if (!containsOnCommitCallbacks(handle->callbackIds)) {
68 outRemainingHandles.push_back(handle);
69 continue;
70 }
Pascal Mütschardd56514e2024-05-24 17:37:13 +020071 status_t err = addCallbackHandle(handle);
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(
Pascal Mütschardd56514e2024-05-24 17:37:13 +020081 const std::deque<sp<CallbackHandle>>& handles) {
Robert Carr9a803c32021-01-14 16:57:58 -080082 if (handles.empty()) {
83 return NO_ERROR;
84 }
Robert Carr9a803c32021-01-14 16:57:58 -080085 for (const auto& handle : handles) {
Pascal Mütschardd56514e2024-05-24 17:37:13 +020086 status_t err = addCallbackHandle(handle);
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
Pascal Mütschardd56514e2024-05-24 17:37:13 +0200112status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle) {
Robert Carr9a803c32021-01-14 16:57:58 -0800113 // If we can't find the transaction stats something has gone wrong. The client should call
114 // startRegistration before trying to add a callback handle.
115 TransactionStats* transactionStats;
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700116 status_t err =
117 findOrCreateTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
Robert Carr9a803c32021-01-14 16:57:58 -0800118 if (err != NO_ERROR) {
119 return err;
120 }
121
122 transactionStats->latchTime = handle->latchTime;
123 // If the layer has already been destroyed, don't add the SurfaceControl to the callback.
124 // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been
125 // destroyed the client side is dead and there won't be anyone to send the callback to.
126 sp<IBinder> surfaceControl = handle->surfaceControl.promote();
127 if (surfaceControl) {
Sally Qi59a9f502021-10-12 18:53:23 +0000128 sp<Fence> prevFence = nullptr;
Melody Hsu793f8362024-01-08 20:00:35 +0000129
130 if (FlagManager::getInstance().ce_fence_promise()) {
131 for (auto& future : handle->previousReleaseFences) {
132 mergeFence(handle->name.c_str(), future.get().value_or(Fence::NO_FENCE), prevFence);
133 }
134 } else {
135 for (const auto& future : handle->previousSharedReleaseFences) {
136 mergeFence(handle->name.c_str(), future.get().value_or(Fence::NO_FENCE), prevFence);
137 }
Sally Qi59a9f502021-10-12 18:53:23 +0000138 }
Melody Hsu793f8362024-01-08 20:00:35 +0000139
Sally Qi4b9e7c42023-01-03 11:11:25 -0800140 handle->previousReleaseFence = prevFence;
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700141 handle->previousReleaseFences.clear();
142
Alec Mouri21d94322023-10-17 19:51:39 +0000143 FrameEventHistoryStats eventStats(handle->frameNumber, handle->previousFrameNumber,
Robert Carr9a803c32021-01-14 16:57:58 -0800144 handle->gpuCompositionDoneFence->getSnapshot().fence,
145 handle->compositorTiming, handle->refreshStartTime,
146 handle->dequeueReadyTime);
Ady Abraham461296a2022-01-21 11:11:31 -0800147 transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTimeOrFence,
Robert Carr9a803c32021-01-14 16:57:58 -0800148 handle->previousReleaseFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700149 handle->transformHint,
150 handle->currentMaxAcquiredBufferCount,
Pascal Mütschardd56514e2024-05-24 17:37:13 +0200151 eventStats, handle->previousReleaseCallbackId);
Patrick Williams7c9fa272024-08-30 12:38:43 +0000152 if (handle->bufferReleaseChannel &&
153 handle->previousReleaseCallbackId != ReleaseCallbackId::INVALID_ID) {
154 mBufferReleases.emplace_back(handle->bufferReleaseChannel,
155 handle->previousReleaseCallbackId,
156 handle->previousReleaseFence,
157 handle->currentMaxAcquiredBufferCount);
158 }
Robert Carr9a803c32021-01-14 16:57:58 -0800159 }
160 return NO_ERROR;
161}
162
Dominik Laskowski8792c112022-07-12 09:03:39 -0700163void TransactionCallbackInvoker::addPresentFence(sp<Fence> presentFence) {
164 mPresentFence = std::move(presentFence);
Robert Carr9a803c32021-01-14 16:57:58 -0800165}
166
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700167void TransactionCallbackInvoker::sendCallbacks(bool onCommitOnly) {
Patrick Williams7c9fa272024-08-30 12:38:43 +0000168 for (const auto& bufferRelease : mBufferReleases) {
169 bufferRelease.channel->writeReleaseFence(bufferRelease.callbackId, bufferRelease.fence,
170 bufferRelease.currentMaxAcquiredBufferCount);
171 }
172 mBufferReleases.clear();
173
Robert Carr9a803c32021-01-14 16:57:58 -0800174 // For each listener
175 auto completedTransactionsItr = mCompletedTransactions.begin();
Vishnu Nair5a93ccb2024-06-26 00:38:32 +0000176 ftl::SmallVector<ListenerStats, 10> listenerStatsToSend;
Robert Carr9a803c32021-01-14 16:57:58 -0800177 while (completedTransactionsItr != mCompletedTransactions.end()) {
178 auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
179 ListenerStats listenerStats;
180 listenerStats.listener = listener;
181
182 // For each transaction
183 auto transactionStatsItr = transactionStatsDeque.begin();
184 while (transactionStatsItr != transactionStatsDeque.end()) {
185 auto& transactionStats = *transactionStatsItr;
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700186 if (onCommitOnly && !containsOnCommitCallbacks(transactionStats.callbackIds)) {
187 transactionStatsItr++;
188 continue;
189 }
Robert Carr9a803c32021-01-14 16:57:58 -0800190
Robert Carr9a803c32021-01-14 16:57:58 -0800191 // If the transaction has been latched
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700192 if (transactionStats.latchTime >= 0 &&
193 !containsOnCommitCallbacks(transactionStats.callbackIds)) {
Robert Carr9a803c32021-01-14 16:57:58 -0800194 transactionStats.presentFence = mPresentFence;
195 }
196
197 // Remove the transaction from completed to the callback
198 listenerStats.transactionStats.push_back(std::move(transactionStats));
199 transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
200 }
201 // If the listener has completed transactions
202 if (!listenerStats.transactionStats.empty()) {
203 // If the listener is still alive
204 if (listener->isBinderAlive()) {
205 // Send callback. The listener stored in listenerStats
206 // comes from the cross-process setTransactionState call to
207 // SF. This MUST be an ITransactionCompletedListener. We
208 // keep it as an IBinder due to consistency reasons: if we
209 // interface_cast at the IPC boundary when reading a Parcel,
210 // we get pointers that compare unequal in the SF process.
Vishnu Nair5a93ccb2024-06-26 00:38:32 +0000211 listenerStatsToSend.emplace_back(std::move(listenerStats));
Robert Carr9a803c32021-01-14 16:57:58 -0800212 }
Robert Carr9a803c32021-01-14 16:57:58 -0800213 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700214 completedTransactionsItr++;
Robert Carr9a803c32021-01-14 16:57:58 -0800215 }
216
217 if (mPresentFence) {
218 mPresentFence.clear();
219 }
Alec Mouri8d7d0f42022-05-10 23:33:40 +0000220
Vishnu Nair5a93ccb2024-06-26 00:38:32 +0000221 BackgroundExecutor::getInstance().sendCallbacks(
222 {[listenerStatsToSend = std::move(listenerStatsToSend)]() {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000223 SFTRACE_NAME("TransactionCallbackInvoker::sendCallbacks");
Vishnu Nair5a93ccb2024-06-26 00:38:32 +0000224 for (auto& stats : listenerStatsToSend) {
225 interface_cast<ITransactionCompletedListener>(stats.listener)
226 ->onTransactionCompleted(stats);
227 }
228 }});
Robert Carr9a803c32021-01-14 16:57:58 -0800229}
230
231// -----------------------------------------------------------------------
232
233CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
234 const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
235 : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
236
237} // namespace android
238
239// TODO(b/129481165): remove the #pragma below and fix conversion issues
240#pragma clang diagnostic pop // ignored "-Wconversion"