blob: b22ec668190f75cb6f4de5d2397ed41d7a2c74ef [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>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000031#include <common/trace.h>
Robert Carr9a803c32021-01-14 16:57:58 -080032#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 for (const auto& handle : handles) {
66 if (!containsOnCommitCallbacks(handle->callbackIds)) {
67 outRemainingHandles.push_back(handle);
68 continue;
69 }
Pascal Mütschardd56514e2024-05-24 17:37:13 +020070 status_t err = addCallbackHandle(handle);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070071 if (err != NO_ERROR) {
72 return err;
73 }
74 }
75
76 return NO_ERROR;
77}
78
Robert Carr3d1047b2021-09-20 18:22:32 -070079status_t TransactionCallbackInvoker::addCallbackHandles(
Pascal Mütschardd56514e2024-05-24 17:37:13 +020080 const std::deque<sp<CallbackHandle>>& handles) {
Robert Carr9a803c32021-01-14 16:57:58 -080081 if (handles.empty()) {
82 return NO_ERROR;
83 }
Robert Carr9a803c32021-01-14 16:57:58 -080084 for (const auto& handle : handles) {
Pascal Mütschardd56514e2024-05-24 17:37:13 +020085 status_t err = addCallbackHandle(handle);
Robert Carr9a803c32021-01-14 16:57:58 -080086 if (err != NO_ERROR) {
Robert Carr9a803c32021-01-14 16:57:58 -080087 return err;
88 }
89 }
90
91 return NO_ERROR;
92}
93
Vishnu Naircd52e2d2021-10-18 08:42:46 -070094status_t TransactionCallbackInvoker::findOrCreateTransactionStats(
Robert Carr9a803c32021-01-14 16:57:58 -080095 const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds,
96 TransactionStats** outTransactionStats) {
97 auto& transactionStatsDeque = mCompletedTransactions[listener];
98
99 // Search back to front because the most recent transactions are at the back of the deque
100 auto itr = transactionStatsDeque.rbegin();
101 for (; itr != transactionStatsDeque.rend(); itr++) {
102 if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) {
103 *outTransactionStats = &(*itr);
104 return NO_ERROR;
105 }
106 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700107 *outTransactionStats = &transactionStatsDeque.emplace_back(callbackIds);
108 return NO_ERROR;
Robert Carr9a803c32021-01-14 16:57:58 -0800109}
110
Pascal Mütschardd56514e2024-05-24 17:37:13 +0200111status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle) {
Robert Carr9a803c32021-01-14 16:57:58 -0800112 // If we can't find the transaction stats something has gone wrong. The client should call
113 // startRegistration before trying to add a callback handle.
114 TransactionStats* transactionStats;
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700115 status_t err =
116 findOrCreateTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
Robert Carr9a803c32021-01-14 16:57:58 -0800117 if (err != NO_ERROR) {
118 return err;
119 }
120
121 transactionStats->latchTime = handle->latchTime;
122 // If the layer has already been destroyed, don't add the SurfaceControl to the callback.
123 // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been
124 // destroyed the client side is dead and there won't be anyone to send the callback to.
125 sp<IBinder> surfaceControl = handle->surfaceControl.promote();
126 if (surfaceControl) {
Sally Qi59a9f502021-10-12 18:53:23 +0000127 sp<Fence> prevFence = nullptr;
Melody Hsu793f8362024-01-08 20:00:35 +0000128
Melody Hsu0077fde2024-10-17 21:42:50 +0000129 for (auto& future : handle->previousReleaseFences) {
130 mergeFence(handle->name.c_str(), future.get().value_or(Fence::NO_FENCE), prevFence);
Sally Qi59a9f502021-10-12 18:53:23 +0000131 }
Melody Hsu793f8362024-01-08 20:00:35 +0000132
Sally Qi4b9e7c42023-01-03 11:11:25 -0800133 handle->previousReleaseFence = prevFence;
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700134 handle->previousReleaseFences.clear();
135
Alec Mouri21d94322023-10-17 19:51:39 +0000136 FrameEventHistoryStats eventStats(handle->frameNumber, handle->previousFrameNumber,
Robert Carr9a803c32021-01-14 16:57:58 -0800137 handle->gpuCompositionDoneFence->getSnapshot().fence,
138 handle->compositorTiming, handle->refreshStartTime,
139 handle->dequeueReadyTime);
Ady Abraham461296a2022-01-21 11:11:31 -0800140 transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTimeOrFence,
Robert Carr9a803c32021-01-14 16:57:58 -0800141 handle->previousReleaseFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700142 handle->transformHint,
143 handle->currentMaxAcquiredBufferCount,
Pascal Mütschardd56514e2024-05-24 17:37:13 +0200144 eventStats, handle->previousReleaseCallbackId);
Patrick Williams7c9fa272024-08-30 12:38:43 +0000145 if (handle->bufferReleaseChannel &&
146 handle->previousReleaseCallbackId != ReleaseCallbackId::INVALID_ID) {
Patrick Williamsbb504472024-10-25 13:20:32 -0500147 mBufferReleases.emplace_back(handle->name, handle->bufferReleaseChannel,
Patrick Williams7c9fa272024-08-30 12:38:43 +0000148 handle->previousReleaseCallbackId,
149 handle->previousReleaseFence,
150 handle->currentMaxAcquiredBufferCount);
151 }
Robert Carr9a803c32021-01-14 16:57:58 -0800152 }
153 return NO_ERROR;
154}
155
Dominik Laskowski8792c112022-07-12 09:03:39 -0700156void TransactionCallbackInvoker::addPresentFence(sp<Fence> presentFence) {
157 mPresentFence = std::move(presentFence);
Robert Carr9a803c32021-01-14 16:57:58 -0800158}
159
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700160void TransactionCallbackInvoker::sendCallbacks(bool onCommitOnly) {
Patrick Williams7c9fa272024-08-30 12:38:43 +0000161 for (const auto& bufferRelease : mBufferReleases) {
Patrick Williamsbb504472024-10-25 13:20:32 -0500162 status_t status = bufferRelease.channel
163 ->writeReleaseFence(bufferRelease.callbackId, bufferRelease.fence,
164 bufferRelease.currentMaxAcquiredBufferCount);
165 if (status != OK) {
166 ALOGE("[%s] writeReleaseFence failed. error %d (%s)", bufferRelease.layerName.c_str(),
167 -status, strerror(-status));
168 }
Patrick Williams7c9fa272024-08-30 12:38:43 +0000169 }
170 mBufferReleases.clear();
171
Robert Carr9a803c32021-01-14 16:57:58 -0800172 // For each listener
173 auto completedTransactionsItr = mCompletedTransactions.begin();
Vishnu Nair5a93ccb2024-06-26 00:38:32 +0000174 ftl::SmallVector<ListenerStats, 10> listenerStatsToSend;
Robert Carr9a803c32021-01-14 16:57:58 -0800175 while (completedTransactionsItr != mCompletedTransactions.end()) {
176 auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
177 ListenerStats listenerStats;
178 listenerStats.listener = listener;
179
180 // For each transaction
181 auto transactionStatsItr = transactionStatsDeque.begin();
182 while (transactionStatsItr != transactionStatsDeque.end()) {
183 auto& transactionStats = *transactionStatsItr;
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700184 if (onCommitOnly && !containsOnCommitCallbacks(transactionStats.callbackIds)) {
185 transactionStatsItr++;
186 continue;
187 }
Robert Carr9a803c32021-01-14 16:57:58 -0800188
Robert Carr9a803c32021-01-14 16:57:58 -0800189 // If the transaction has been latched
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700190 if (transactionStats.latchTime >= 0 &&
191 !containsOnCommitCallbacks(transactionStats.callbackIds)) {
Robert Carr9a803c32021-01-14 16:57:58 -0800192 transactionStats.presentFence = mPresentFence;
193 }
194
195 // Remove the transaction from completed to the callback
196 listenerStats.transactionStats.push_back(std::move(transactionStats));
197 transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
198 }
199 // If the listener has completed transactions
200 if (!listenerStats.transactionStats.empty()) {
201 // If the listener is still alive
202 if (listener->isBinderAlive()) {
203 // Send callback. The listener stored in listenerStats
204 // comes from the cross-process setTransactionState call to
205 // SF. This MUST be an ITransactionCompletedListener. We
206 // keep it as an IBinder due to consistency reasons: if we
207 // interface_cast at the IPC boundary when reading a Parcel,
208 // we get pointers that compare unequal in the SF process.
Vishnu Nair5a93ccb2024-06-26 00:38:32 +0000209 listenerStatsToSend.emplace_back(std::move(listenerStats));
Robert Carr9a803c32021-01-14 16:57:58 -0800210 }
Robert Carr9a803c32021-01-14 16:57:58 -0800211 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700212 completedTransactionsItr++;
Robert Carr9a803c32021-01-14 16:57:58 -0800213 }
214
215 if (mPresentFence) {
216 mPresentFence.clear();
217 }
Alec Mouri8d7d0f42022-05-10 23:33:40 +0000218
Vishnu Nair5a93ccb2024-06-26 00:38:32 +0000219 BackgroundExecutor::getInstance().sendCallbacks(
220 {[listenerStatsToSend = std::move(listenerStatsToSend)]() {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000221 SFTRACE_NAME("TransactionCallbackInvoker::sendCallbacks");
Vishnu Nair5a93ccb2024-06-26 00:38:32 +0000222 for (auto& stats : listenerStatsToSend) {
223 interface_cast<ITransactionCompletedListener>(stats.listener)
224 ->onTransactionCompleted(stats);
225 }
226 }});
Robert Carr9a803c32021-01-14 16:57:58 -0800227}
228
229// -----------------------------------------------------------------------
230
231CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
232 const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
233 : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
234
235} // namespace android
236
237// TODO(b/129481165): remove the #pragma below and fix conversion issues
238#pragma clang diagnostic pop // ignored "-Wconversion"