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