blob: 7b5298c82e4adce09b9a1a2263f583687738dddb [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 <cinttypes>
31
32#include <binder/IInterface.h>
33#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 const std::vector<JankData>& jankData = std::vector<JankData>();
67 for (const auto& handle : handles) {
68 if (!containsOnCommitCallbacks(handle->callbackIds)) {
69 outRemainingHandles.push_back(handle);
70 continue;
71 }
Robert Carr3d1047b2021-09-20 18:22:32 -070072 status_t err = addCallbackHandle(handle, jankData);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070073 if (err != NO_ERROR) {
74 return err;
75 }
76 }
77
78 return NO_ERROR;
79}
80
Robert Carr3d1047b2021-09-20 18:22:32 -070081status_t TransactionCallbackInvoker::addCallbackHandles(
Robert Carr9a803c32021-01-14 16:57:58 -080082 const std::deque<sp<CallbackHandle>>& handles, const std::vector<JankData>& jankData) {
83 if (handles.empty()) {
84 return NO_ERROR;
85 }
Robert Carr9a803c32021-01-14 16:57:58 -080086 for (const auto& handle : handles) {
Robert Carr3d1047b2021-09-20 18:22:32 -070087 status_t err = addCallbackHandle(handle, jankData);
Robert Carr9a803c32021-01-14 16:57:58 -080088 if (err != NO_ERROR) {
Robert Carr9a803c32021-01-14 16:57:58 -080089 return err;
90 }
91 }
92
93 return NO_ERROR;
94}
95
Vishnu Naircd52e2d2021-10-18 08:42:46 -070096status_t TransactionCallbackInvoker::findOrCreateTransactionStats(
Robert Carr9a803c32021-01-14 16:57:58 -080097 const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds,
98 TransactionStats** outTransactionStats) {
99 auto& transactionStatsDeque = mCompletedTransactions[listener];
100
101 // Search back to front because the most recent transactions are at the back of the deque
102 auto itr = transactionStatsDeque.rbegin();
103 for (; itr != transactionStatsDeque.rend(); itr++) {
104 if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) {
105 *outTransactionStats = &(*itr);
106 return NO_ERROR;
107 }
108 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700109 *outTransactionStats = &transactionStatsDeque.emplace_back(callbackIds);
110 return NO_ERROR;
Robert Carr9a803c32021-01-14 16:57:58 -0800111}
112
113status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle,
114 const std::vector<JankData>& jankData) {
115 // 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;
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700131 for (const auto& future : handle->previousReleaseFences) {
Alec Mouri9892aac2023-12-11 21:16:59 +0000132 mergeFence(handle->name.c_str(), future.get().value_or(Fence::NO_FENCE), prevFence);
Sally Qi59a9f502021-10-12 18:53:23 +0000133 }
Sally Qi4b9e7c42023-01-03 11:11:25 -0800134 handle->previousReleaseFence = prevFence;
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700135 handle->previousReleaseFences.clear();
136
Alec Mouri21d94322023-10-17 19:51:39 +0000137 FrameEventHistoryStats eventStats(handle->frameNumber, handle->previousFrameNumber,
Robert Carr9a803c32021-01-14 16:57:58 -0800138 handle->gpuCompositionDoneFence->getSnapshot().fence,
139 handle->compositorTiming, handle->refreshStartTime,
140 handle->dequeueReadyTime);
Ady Abraham461296a2022-01-21 11:11:31 -0800141 transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTimeOrFence,
Robert Carr9a803c32021-01-14 16:57:58 -0800142 handle->previousReleaseFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700143 handle->transformHint,
144 handle->currentMaxAcquiredBufferCount,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700145 eventStats, jankData,
146 handle->previousReleaseCallbackId);
Robert Carr9a803c32021-01-14 16:57:58 -0800147 }
148 return NO_ERROR;
149}
150
Dominik Laskowski8792c112022-07-12 09:03:39 -0700151void TransactionCallbackInvoker::addPresentFence(sp<Fence> presentFence) {
152 mPresentFence = std::move(presentFence);
Robert Carr9a803c32021-01-14 16:57:58 -0800153}
154
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700155void TransactionCallbackInvoker::sendCallbacks(bool onCommitOnly) {
Robert Carr9a803c32021-01-14 16:57:58 -0800156 // For each listener
157 auto completedTransactionsItr = mCompletedTransactions.begin();
Alec Mouri8d7d0f42022-05-10 23:33:40 +0000158 BackgroundExecutor::Callbacks callbacks;
Robert Carr9a803c32021-01-14 16:57:58 -0800159 while (completedTransactionsItr != mCompletedTransactions.end()) {
160 auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
161 ListenerStats listenerStats;
162 listenerStats.listener = listener;
163
164 // For each transaction
165 auto transactionStatsItr = transactionStatsDeque.begin();
166 while (transactionStatsItr != transactionStatsDeque.end()) {
167 auto& transactionStats = *transactionStatsItr;
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700168 if (onCommitOnly && !containsOnCommitCallbacks(transactionStats.callbackIds)) {
169 transactionStatsItr++;
170 continue;
171 }
Robert Carr9a803c32021-01-14 16:57:58 -0800172
Robert Carr9a803c32021-01-14 16:57:58 -0800173 // If the transaction has been latched
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700174 if (transactionStats.latchTime >= 0 &&
175 !containsOnCommitCallbacks(transactionStats.callbackIds)) {
Robert Carr9a803c32021-01-14 16:57:58 -0800176 transactionStats.presentFence = mPresentFence;
177 }
178
179 // Remove the transaction from completed to the callback
180 listenerStats.transactionStats.push_back(std::move(transactionStats));
181 transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
182 }
183 // If the listener has completed transactions
184 if (!listenerStats.transactionStats.empty()) {
185 // If the listener is still alive
186 if (listener->isBinderAlive()) {
187 // Send callback. The listener stored in listenerStats
188 // comes from the cross-process setTransactionState call to
189 // SF. This MUST be an ITransactionCompletedListener. We
190 // keep it as an IBinder due to consistency reasons: if we
191 // interface_cast at the IPC boundary when reading a Parcel,
192 // we get pointers that compare unequal in the SF process.
Alec Mouri8d7d0f42022-05-10 23:33:40 +0000193 callbacks.emplace_back([stats = std::move(listenerStats)]() {
Vishnu Nair34eb9ca2021-11-18 15:23:23 -0800194 interface_cast<ITransactionCompletedListener>(stats.listener)
195 ->onTransactionCompleted(stats);
196 });
Robert Carr9a803c32021-01-14 16:57:58 -0800197 }
Robert Carr9a803c32021-01-14 16:57:58 -0800198 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700199 completedTransactionsItr++;
Robert Carr9a803c32021-01-14 16:57:58 -0800200 }
201
202 if (mPresentFence) {
203 mPresentFence.clear();
204 }
Alec Mouri8d7d0f42022-05-10 23:33:40 +0000205
206 BackgroundExecutor::getInstance().sendCallbacks(std::move(callbacks));
Robert Carr9a803c32021-01-14 16:57:58 -0800207}
208
209// -----------------------------------------------------------------------
210
211CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
212 const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
213 : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
214
215} // namespace android
216
217// TODO(b/129481165): remove the #pragma below and fix conversion issues
218#pragma clang diagnostic pop // ignored "-Wconversion"