blob: 37543ba17149a588fef5ee8b7046aef7420f9845 [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"
Vishnu Nair5a93ccb2024-06-26 00:38:32 +000029#include "utils/Trace.h"
Robert Carr9a803c32021-01-14 16:57:58 -080030
31#include <cinttypes>
32
33#include <binder/IInterface.h>
Melody Hsu793f8362024-01-08 20:00:35 +000034#include <common/FlagManager.h>
Robert Carr9a803c32021-01-14 16:57:58 -080035#include <utils/RefBase.h>
36
37namespace android {
38
39// Returns 0 if they are equal
40// <0 if the first id that doesn't match is lower in c2 or all ids match but c2 is shorter
41// >0 if the first id that doesn't match is greater in c2 or all ids match but c2 is longer
42//
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070043// See CallbackIdsHash for a explanation of why this works
Robert Carr9a803c32021-01-14 16:57:58 -080044static int compareCallbackIds(const std::vector<CallbackId>& c1,
45 const std::vector<CallbackId>& c2) {
46 if (c1.empty()) {
47 return !c2.empty();
48 }
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070049 return c1.front().id - c2.front().id;
50}
51
52static bool containsOnCommitCallbacks(const std::vector<CallbackId>& callbacks) {
53 return !callbacks.empty() && callbacks.front().type == CallbackId::Type::ON_COMMIT;
Robert Carr9a803c32021-01-14 16:57:58 -080054}
55
Jiakai Zhanga5505cb2021-11-09 11:46:30 +000056void TransactionCallbackInvoker::addEmptyTransaction(const ListenerCallbacks& listenerCallbacks) {
Robert Carr9a803c32021-01-14 16:57:58 -080057 auto& [listener, callbackIds] = listenerCallbacks;
Jiakai Zhanga5505cb2021-11-09 11:46:30 +000058 auto& transactionStatsDeque = mCompletedTransactions[listener];
59 transactionStatsDeque.emplace_back(callbackIds);
Robert Carr9a803c32021-01-14 16:57:58 -080060}
61
Robert Carr3d1047b2021-09-20 18:22:32 -070062status_t TransactionCallbackInvoker::addOnCommitCallbackHandles(
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070063 const std::deque<sp<CallbackHandle>>& handles,
64 std::deque<sp<CallbackHandle>>& outRemainingHandles) {
65 if (handles.empty()) {
66 return NO_ERROR;
67 }
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070068 for (const auto& handle : handles) {
69 if (!containsOnCommitCallbacks(handle->callbackIds)) {
70 outRemainingHandles.push_back(handle);
71 continue;
72 }
Pascal Mütschardd56514e2024-05-24 17:37:13 +020073 status_t err = addCallbackHandle(handle);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070074 if (err != NO_ERROR) {
75 return err;
76 }
77 }
78
79 return NO_ERROR;
80}
81
Robert Carr3d1047b2021-09-20 18:22:32 -070082status_t TransactionCallbackInvoker::addCallbackHandles(
Pascal Mütschardd56514e2024-05-24 17:37:13 +020083 const std::deque<sp<CallbackHandle>>& handles) {
Robert Carr9a803c32021-01-14 16:57:58 -080084 if (handles.empty()) {
85 return NO_ERROR;
86 }
Robert Carr9a803c32021-01-14 16:57:58 -080087 for (const auto& handle : handles) {
Pascal Mütschardd56514e2024-05-24 17:37:13 +020088 status_t err = addCallbackHandle(handle);
Robert Carr9a803c32021-01-14 16:57:58 -080089 if (err != NO_ERROR) {
Robert Carr9a803c32021-01-14 16:57:58 -080090 return err;
91 }
92 }
93
94 return NO_ERROR;
95}
96
Vishnu Naircd52e2d2021-10-18 08:42:46 -070097status_t TransactionCallbackInvoker::findOrCreateTransactionStats(
Robert Carr9a803c32021-01-14 16:57:58 -080098 const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds,
99 TransactionStats** outTransactionStats) {
100 auto& transactionStatsDeque = mCompletedTransactions[listener];
101
102 // Search back to front because the most recent transactions are at the back of the deque
103 auto itr = transactionStatsDeque.rbegin();
104 for (; itr != transactionStatsDeque.rend(); itr++) {
105 if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) {
106 *outTransactionStats = &(*itr);
107 return NO_ERROR;
108 }
109 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700110 *outTransactionStats = &transactionStatsDeque.emplace_back(callbackIds);
111 return NO_ERROR;
Robert Carr9a803c32021-01-14 16:57:58 -0800112}
113
Pascal Mütschardd56514e2024-05-24 17:37:13 +0200114status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle) {
Robert Carr9a803c32021-01-14 16:57:58 -0800115 // If we can't find the transaction stats something has gone wrong. The client should call
116 // startRegistration before trying to add a callback handle.
117 TransactionStats* transactionStats;
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700118 status_t err =
119 findOrCreateTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
Robert Carr9a803c32021-01-14 16:57:58 -0800120 if (err != NO_ERROR) {
121 return err;
122 }
123
124 transactionStats->latchTime = handle->latchTime;
125 // If the layer has already been destroyed, don't add the SurfaceControl to the callback.
126 // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been
127 // destroyed the client side is dead and there won't be anyone to send the callback to.
128 sp<IBinder> surfaceControl = handle->surfaceControl.promote();
129 if (surfaceControl) {
Sally Qi59a9f502021-10-12 18:53:23 +0000130 sp<Fence> prevFence = nullptr;
Melody Hsu793f8362024-01-08 20:00:35 +0000131
132 if (FlagManager::getInstance().ce_fence_promise()) {
133 for (auto& future : handle->previousReleaseFences) {
134 mergeFence(handle->name.c_str(), future.get().value_or(Fence::NO_FENCE), prevFence);
135 }
136 } else {
137 for (const auto& future : handle->previousSharedReleaseFences) {
138 mergeFence(handle->name.c_str(), future.get().value_or(Fence::NO_FENCE), prevFence);
139 }
Sally Qi59a9f502021-10-12 18:53:23 +0000140 }
Melody Hsu793f8362024-01-08 20:00:35 +0000141
Sally Qi4b9e7c42023-01-03 11:11:25 -0800142 handle->previousReleaseFence = prevFence;
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700143 handle->previousReleaseFences.clear();
144
Alec Mouri21d94322023-10-17 19:51:39 +0000145 FrameEventHistoryStats eventStats(handle->frameNumber, handle->previousFrameNumber,
Robert Carr9a803c32021-01-14 16:57:58 -0800146 handle->gpuCompositionDoneFence->getSnapshot().fence,
147 handle->compositorTiming, handle->refreshStartTime,
148 handle->dequeueReadyTime);
Ady Abraham461296a2022-01-21 11:11:31 -0800149 transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTimeOrFence,
Robert Carr9a803c32021-01-14 16:57:58 -0800150 handle->previousReleaseFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700151 handle->transformHint,
152 handle->currentMaxAcquiredBufferCount,
Pascal Mütschardd56514e2024-05-24 17:37:13 +0200153 eventStats, handle->previousReleaseCallbackId);
Robert Carr9a803c32021-01-14 16:57:58 -0800154 }
155 return NO_ERROR;
156}
157
Dominik Laskowski8792c112022-07-12 09:03:39 -0700158void TransactionCallbackInvoker::addPresentFence(sp<Fence> presentFence) {
159 mPresentFence = std::move(presentFence);
Robert Carr9a803c32021-01-14 16:57:58 -0800160}
161
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700162void TransactionCallbackInvoker::sendCallbacks(bool onCommitOnly) {
Robert Carr9a803c32021-01-14 16:57:58 -0800163 // For each listener
164 auto completedTransactionsItr = mCompletedTransactions.begin();
Vishnu Nair5a93ccb2024-06-26 00:38:32 +0000165 ftl::SmallVector<ListenerStats, 10> listenerStatsToSend;
Robert Carr9a803c32021-01-14 16:57:58 -0800166 while (completedTransactionsItr != mCompletedTransactions.end()) {
167 auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
168 ListenerStats listenerStats;
169 listenerStats.listener = listener;
170
171 // For each transaction
172 auto transactionStatsItr = transactionStatsDeque.begin();
173 while (transactionStatsItr != transactionStatsDeque.end()) {
174 auto& transactionStats = *transactionStatsItr;
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700175 if (onCommitOnly && !containsOnCommitCallbacks(transactionStats.callbackIds)) {
176 transactionStatsItr++;
177 continue;
178 }
Robert Carr9a803c32021-01-14 16:57:58 -0800179
Robert Carr9a803c32021-01-14 16:57:58 -0800180 // If the transaction has been latched
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700181 if (transactionStats.latchTime >= 0 &&
182 !containsOnCommitCallbacks(transactionStats.callbackIds)) {
Robert Carr9a803c32021-01-14 16:57:58 -0800183 transactionStats.presentFence = mPresentFence;
184 }
185
186 // Remove the transaction from completed to the callback
187 listenerStats.transactionStats.push_back(std::move(transactionStats));
188 transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
189 }
190 // If the listener has completed transactions
191 if (!listenerStats.transactionStats.empty()) {
192 // If the listener is still alive
193 if (listener->isBinderAlive()) {
194 // Send callback. The listener stored in listenerStats
195 // comes from the cross-process setTransactionState call to
196 // SF. This MUST be an ITransactionCompletedListener. We
197 // keep it as an IBinder due to consistency reasons: if we
198 // interface_cast at the IPC boundary when reading a Parcel,
199 // we get pointers that compare unequal in the SF process.
Vishnu Nair5a93ccb2024-06-26 00:38:32 +0000200 listenerStatsToSend.emplace_back(std::move(listenerStats));
Robert Carr9a803c32021-01-14 16:57:58 -0800201 }
Robert Carr9a803c32021-01-14 16:57:58 -0800202 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700203 completedTransactionsItr++;
Robert Carr9a803c32021-01-14 16:57:58 -0800204 }
205
206 if (mPresentFence) {
207 mPresentFence.clear();
208 }
Alec Mouri8d7d0f42022-05-10 23:33:40 +0000209
Vishnu Nair5a93ccb2024-06-26 00:38:32 +0000210 BackgroundExecutor::getInstance().sendCallbacks(
211 {[listenerStatsToSend = std::move(listenerStatsToSend)]() {
212 ATRACE_NAME("TransactionCallbackInvoker::sendCallbacks");
213 for (auto& stats : listenerStatsToSend) {
214 interface_cast<ITransactionCompletedListener>(stats.listener)
215 ->onTransactionCompleted(stats);
216 }
217 }});
Robert Carr9a803c32021-01-14 16:57:58 -0800218}
219
220// -----------------------------------------------------------------------
221
222CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
223 const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
224 : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
225
226} // namespace android
227
228// TODO(b/129481165): remove the #pragma below and fix conversion issues
229#pragma clang diagnostic pop // ignored "-Wconversion"