Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 1 | /* |
| 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 Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 27 | #include "BackgroundExecutor.h" |
Alec Mouri | 9892aac | 2023-12-11 21:16:59 +0000 | [diff] [blame] | 28 | #include "Utils/FenceUtils.h" |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 29 | |
| 30 | #include <binder/IInterface.h> |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 31 | #include <common/trace.h> |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 32 | #include <utils/RefBase.h> |
| 33 | |
| 34 | namespace 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 Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 40 | // See CallbackIdsHash for a explanation of why this works |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 41 | static int compareCallbackIds(const std::vector<CallbackId>& c1, |
| 42 | const std::vector<CallbackId>& c2) { |
| 43 | if (c1.empty()) { |
| 44 | return !c2.empty(); |
| 45 | } |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 46 | return c1.front().id - c2.front().id; |
| 47 | } |
| 48 | |
| 49 | static bool containsOnCommitCallbacks(const std::vector<CallbackId>& callbacks) { |
| 50 | return !callbacks.empty() && callbacks.front().type == CallbackId::Type::ON_COMMIT; |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Jiakai Zhang | a5505cb | 2021-11-09 11:46:30 +0000 | [diff] [blame] | 53 | void TransactionCallbackInvoker::addEmptyTransaction(const ListenerCallbacks& listenerCallbacks) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 54 | auto& [listener, callbackIds] = listenerCallbacks; |
Jiakai Zhang | a5505cb | 2021-11-09 11:46:30 +0000 | [diff] [blame] | 55 | auto& transactionStatsDeque = mCompletedTransactions[listener]; |
| 56 | transactionStatsDeque.emplace_back(callbackIds); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame] | 59 | status_t TransactionCallbackInvoker::addOnCommitCallbackHandles( |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 60 | const std::deque<sp<CallbackHandle>>& handles, |
| 61 | std::deque<sp<CallbackHandle>>& outRemainingHandles) { |
| 62 | if (handles.empty()) { |
| 63 | return NO_ERROR; |
| 64 | } |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 65 | for (const auto& handle : handles) { |
| 66 | if (!containsOnCommitCallbacks(handle->callbackIds)) { |
| 67 | outRemainingHandles.push_back(handle); |
| 68 | continue; |
| 69 | } |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 70 | status_t err = addCallbackHandle(handle); |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 71 | if (err != NO_ERROR) { |
| 72 | return err; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return NO_ERROR; |
| 77 | } |
| 78 | |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame] | 79 | status_t TransactionCallbackInvoker::addCallbackHandles( |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 80 | const std::deque<sp<CallbackHandle>>& handles) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 81 | if (handles.empty()) { |
| 82 | return NO_ERROR; |
| 83 | } |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 84 | for (const auto& handle : handles) { |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 85 | status_t err = addCallbackHandle(handle); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 86 | if (err != NO_ERROR) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 87 | return err; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return NO_ERROR; |
| 92 | } |
| 93 | |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 94 | status_t TransactionCallbackInvoker::findOrCreateTransactionStats( |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 95 | 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 Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame] | 107 | *outTransactionStats = &transactionStatsDeque.emplace_back(callbackIds); |
| 108 | return NO_ERROR; |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 111 | status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 112 | // 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 Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 115 | status_t err = |
| 116 | findOrCreateTransactionStats(handle->listener, handle->callbackIds, &transactionStats); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 117 | 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 Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 127 | sp<Fence> prevFence = nullptr; |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 128 | |
Melody Hsu | 0077fde | 2024-10-17 21:42:50 +0000 | [diff] [blame^] | 129 | for (auto& future : handle->previousReleaseFences) { |
| 130 | mergeFence(handle->name.c_str(), future.get().value_or(Fence::NO_FENCE), prevFence); |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 131 | } |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 132 | |
Sally Qi | 4b9e7c4 | 2023-01-03 11:11:25 -0800 | [diff] [blame] | 133 | handle->previousReleaseFence = prevFence; |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 134 | handle->previousReleaseFences.clear(); |
| 135 | |
Alec Mouri | 21d9432 | 2023-10-17 19:51:39 +0000 | [diff] [blame] | 136 | FrameEventHistoryStats eventStats(handle->frameNumber, handle->previousFrameNumber, |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 137 | handle->gpuCompositionDoneFence->getSnapshot().fence, |
| 138 | handle->compositorTiming, handle->refreshStartTime, |
| 139 | handle->dequeueReadyTime); |
Ady Abraham | 461296a | 2022-01-21 11:11:31 -0800 | [diff] [blame] | 140 | transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTimeOrFence, |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 141 | handle->previousReleaseFence, |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 142 | handle->transformHint, |
| 143 | handle->currentMaxAcquiredBufferCount, |
Pascal Mütschard | d56514e | 2024-05-24 17:37:13 +0200 | [diff] [blame] | 144 | eventStats, handle->previousReleaseCallbackId); |
Patrick Williams | 7c9fa27 | 2024-08-30 12:38:43 +0000 | [diff] [blame] | 145 | if (handle->bufferReleaseChannel && |
| 146 | handle->previousReleaseCallbackId != ReleaseCallbackId::INVALID_ID) { |
| 147 | mBufferReleases.emplace_back(handle->bufferReleaseChannel, |
| 148 | handle->previousReleaseCallbackId, |
| 149 | handle->previousReleaseFence, |
| 150 | handle->currentMaxAcquiredBufferCount); |
| 151 | } |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 152 | } |
| 153 | return NO_ERROR; |
| 154 | } |
| 155 | |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 156 | void TransactionCallbackInvoker::addPresentFence(sp<Fence> presentFence) { |
| 157 | mPresentFence = std::move(presentFence); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 160 | void TransactionCallbackInvoker::sendCallbacks(bool onCommitOnly) { |
Patrick Williams | 7c9fa27 | 2024-08-30 12:38:43 +0000 | [diff] [blame] | 161 | for (const auto& bufferRelease : mBufferReleases) { |
| 162 | bufferRelease.channel->writeReleaseFence(bufferRelease.callbackId, bufferRelease.fence, |
| 163 | bufferRelease.currentMaxAcquiredBufferCount); |
| 164 | } |
| 165 | mBufferReleases.clear(); |
| 166 | |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 167 | // For each listener |
| 168 | auto completedTransactionsItr = mCompletedTransactions.begin(); |
Vishnu Nair | 5a93ccb | 2024-06-26 00:38:32 +0000 | [diff] [blame] | 169 | ftl::SmallVector<ListenerStats, 10> listenerStatsToSend; |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 170 | while (completedTransactionsItr != mCompletedTransactions.end()) { |
| 171 | auto& [listener, transactionStatsDeque] = *completedTransactionsItr; |
| 172 | ListenerStats listenerStats; |
| 173 | listenerStats.listener = listener; |
| 174 | |
| 175 | // For each transaction |
| 176 | auto transactionStatsItr = transactionStatsDeque.begin(); |
| 177 | while (transactionStatsItr != transactionStatsDeque.end()) { |
| 178 | auto& transactionStats = *transactionStatsItr; |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 179 | if (onCommitOnly && !containsOnCommitCallbacks(transactionStats.callbackIds)) { |
| 180 | transactionStatsItr++; |
| 181 | continue; |
| 182 | } |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 183 | |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 184 | // If the transaction has been latched |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 185 | if (transactionStats.latchTime >= 0 && |
| 186 | !containsOnCommitCallbacks(transactionStats.callbackIds)) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 187 | transactionStats.presentFence = mPresentFence; |
| 188 | } |
| 189 | |
| 190 | // Remove the transaction from completed to the callback |
| 191 | listenerStats.transactionStats.push_back(std::move(transactionStats)); |
| 192 | transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr); |
| 193 | } |
| 194 | // If the listener has completed transactions |
| 195 | if (!listenerStats.transactionStats.empty()) { |
| 196 | // If the listener is still alive |
| 197 | if (listener->isBinderAlive()) { |
| 198 | // Send callback. The listener stored in listenerStats |
| 199 | // comes from the cross-process setTransactionState call to |
| 200 | // SF. This MUST be an ITransactionCompletedListener. We |
| 201 | // keep it as an IBinder due to consistency reasons: if we |
| 202 | // interface_cast at the IPC boundary when reading a Parcel, |
| 203 | // we get pointers that compare unequal in the SF process. |
Vishnu Nair | 5a93ccb | 2024-06-26 00:38:32 +0000 | [diff] [blame] | 204 | listenerStatsToSend.emplace_back(std::move(listenerStats)); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 205 | } |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 206 | } |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame] | 207 | completedTransactionsItr++; |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if (mPresentFence) { |
| 211 | mPresentFence.clear(); |
| 212 | } |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 213 | |
Vishnu Nair | 5a93ccb | 2024-06-26 00:38:32 +0000 | [diff] [blame] | 214 | BackgroundExecutor::getInstance().sendCallbacks( |
| 215 | {[listenerStatsToSend = std::move(listenerStatsToSend)]() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 216 | SFTRACE_NAME("TransactionCallbackInvoker::sendCallbacks"); |
Vishnu Nair | 5a93ccb | 2024-06-26 00:38:32 +0000 | [diff] [blame] | 217 | for (auto& stats : listenerStatsToSend) { |
| 218 | interface_cast<ITransactionCompletedListener>(stats.listener) |
| 219 | ->onTransactionCompleted(stats); |
| 220 | } |
| 221 | }}); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // ----------------------------------------------------------------------- |
| 225 | |
| 226 | CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener, |
| 227 | const std::vector<CallbackId>& ids, const sp<IBinder>& sc) |
| 228 | : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {} |
| 229 | |
| 230 | } // namespace android |
| 231 | |
| 232 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 233 | #pragma clang diagnostic pop // ignored "-Wconversion" |