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" |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 28 | |
| 29 | #include <cinttypes> |
| 30 | |
| 31 | #include <binder/IInterface.h> |
| 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 | const std::vector<JankData>& jankData = std::vector<JankData>(); |
| 66 | for (const auto& handle : handles) { |
| 67 | if (!containsOnCommitCallbacks(handle->callbackIds)) { |
| 68 | outRemainingHandles.push_back(handle); |
| 69 | continue; |
| 70 | } |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame] | 71 | status_t err = addCallbackHandle(handle, jankData); |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 72 | if (err != NO_ERROR) { |
| 73 | return err; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return NO_ERROR; |
| 78 | } |
| 79 | |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame] | 80 | status_t TransactionCallbackInvoker::addCallbackHandles( |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 81 | const std::deque<sp<CallbackHandle>>& handles, const std::vector<JankData>& jankData) { |
| 82 | if (handles.empty()) { |
| 83 | return NO_ERROR; |
| 84 | } |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 85 | for (const auto& handle : handles) { |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame] | 86 | status_t err = addCallbackHandle(handle, jankData); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 87 | if (err != NO_ERROR) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 88 | return err; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return NO_ERROR; |
| 93 | } |
| 94 | |
Jiakai Zhang | a5505cb | 2021-11-09 11:46:30 +0000 | [diff] [blame] | 95 | status_t TransactionCallbackInvoker::registerUnpresentedCallbackHandle( |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 96 | const sp<CallbackHandle>& handle) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 97 | return addCallbackHandle(handle, std::vector<JankData>()); |
| 98 | } |
| 99 | |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 100 | status_t TransactionCallbackInvoker::findOrCreateTransactionStats( |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 101 | const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds, |
| 102 | TransactionStats** outTransactionStats) { |
| 103 | auto& transactionStatsDeque = mCompletedTransactions[listener]; |
| 104 | |
| 105 | // Search back to front because the most recent transactions are at the back of the deque |
| 106 | auto itr = transactionStatsDeque.rbegin(); |
| 107 | for (; itr != transactionStatsDeque.rend(); itr++) { |
| 108 | if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) { |
| 109 | *outTransactionStats = &(*itr); |
| 110 | return NO_ERROR; |
| 111 | } |
| 112 | } |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame] | 113 | *outTransactionStats = &transactionStatsDeque.emplace_back(callbackIds); |
| 114 | return NO_ERROR; |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle, |
| 118 | const std::vector<JankData>& jankData) { |
| 119 | // If we can't find the transaction stats something has gone wrong. The client should call |
| 120 | // startRegistration before trying to add a callback handle. |
| 121 | TransactionStats* transactionStats; |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 122 | status_t err = |
| 123 | findOrCreateTransactionStats(handle->listener, handle->callbackIds, &transactionStats); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 124 | if (err != NO_ERROR) { |
| 125 | return err; |
| 126 | } |
| 127 | |
| 128 | transactionStats->latchTime = handle->latchTime; |
| 129 | // If the layer has already been destroyed, don't add the SurfaceControl to the callback. |
| 130 | // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been |
| 131 | // destroyed the client side is dead and there won't be anyone to send the callback to. |
| 132 | sp<IBinder> surfaceControl = handle->surfaceControl.promote(); |
| 133 | if (surfaceControl) { |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 134 | sp<Fence> prevFence = nullptr; |
| 135 | |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 136 | for (const auto& future : handle->previousReleaseFences) { |
| 137 | sp<Fence> currentFence = future.get().value_or(Fence::NO_FENCE); |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 138 | if (prevFence == nullptr && currentFence->getStatus() != Fence::Status::Invalid) { |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 139 | prevFence = std::move(currentFence); |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 140 | } else if (prevFence != nullptr) { |
| 141 | // If both fences are signaled or both are unsignaled, we need to merge |
| 142 | // them to get an accurate timestamp. |
| 143 | if (prevFence->getStatus() != Fence::Status::Invalid && |
| 144 | prevFence->getStatus() == currentFence->getStatus()) { |
| 145 | char fenceName[32] = {}; |
| 146 | snprintf(fenceName, 32, "%.28s", handle->name.c_str()); |
| 147 | sp<Fence> mergedFence = Fence::merge(fenceName, prevFence, currentFence); |
| 148 | if (mergedFence->isValid()) { |
Sally Qi | 4b9e7c4 | 2023-01-03 11:11:25 -0800 | [diff] [blame^] | 149 | prevFence = std::move(mergedFence); |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 150 | } |
| 151 | } else if (currentFence->getStatus() == Fence::Status::Unsignaled) { |
| 152 | // If one fence has signaled and the other hasn't, the unsignaled |
| 153 | // fence will approximately correspond with the correct timestamp. |
| 154 | // There's a small race if both fences signal at about the same time |
| 155 | // and their statuses are retrieved with unfortunate timing. However, |
| 156 | // by this point, they will have both signaled and only the timestamp |
| 157 | // will be slightly off; any dependencies after this point will |
| 158 | // already have been met. |
Sally Qi | 4b9e7c4 | 2023-01-03 11:11:25 -0800 | [diff] [blame^] | 159 | prevFence = std::move(currentFence); |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |
Sally Qi | 4b9e7c4 | 2023-01-03 11:11:25 -0800 | [diff] [blame^] | 163 | handle->previousReleaseFence = prevFence; |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame] | 164 | handle->previousReleaseFences.clear(); |
| 165 | |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 166 | FrameEventHistoryStats eventStats(handle->frameNumber, |
| 167 | handle->gpuCompositionDoneFence->getSnapshot().fence, |
| 168 | handle->compositorTiming, handle->refreshStartTime, |
| 169 | handle->dequeueReadyTime); |
Ady Abraham | 461296a | 2022-01-21 11:11:31 -0800 | [diff] [blame] | 170 | transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTimeOrFence, |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 171 | handle->previousReleaseFence, |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 172 | handle->transformHint, |
| 173 | handle->currentMaxAcquiredBufferCount, |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 174 | eventStats, jankData, |
| 175 | handle->previousReleaseCallbackId); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 176 | } |
| 177 | return NO_ERROR; |
| 178 | } |
| 179 | |
Dominik Laskowski | 8792c11 | 2022-07-12 09:03:39 -0700 | [diff] [blame] | 180 | void TransactionCallbackInvoker::addPresentFence(sp<Fence> presentFence) { |
| 181 | mPresentFence = std::move(presentFence); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 184 | void TransactionCallbackInvoker::sendCallbacks(bool onCommitOnly) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 185 | // For each listener |
| 186 | auto completedTransactionsItr = mCompletedTransactions.begin(); |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 187 | BackgroundExecutor::Callbacks callbacks; |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 188 | while (completedTransactionsItr != mCompletedTransactions.end()) { |
| 189 | auto& [listener, transactionStatsDeque] = *completedTransactionsItr; |
| 190 | ListenerStats listenerStats; |
| 191 | listenerStats.listener = listener; |
| 192 | |
| 193 | // For each transaction |
| 194 | auto transactionStatsItr = transactionStatsDeque.begin(); |
| 195 | while (transactionStatsItr != transactionStatsDeque.end()) { |
| 196 | auto& transactionStats = *transactionStatsItr; |
Vishnu Nair | cd52e2d | 2021-10-18 08:42:46 -0700 | [diff] [blame] | 197 | if (onCommitOnly && !containsOnCommitCallbacks(transactionStats.callbackIds)) { |
| 198 | transactionStatsItr++; |
| 199 | continue; |
| 200 | } |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 201 | |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 202 | // If the transaction has been latched |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 203 | if (transactionStats.latchTime >= 0 && |
| 204 | !containsOnCommitCallbacks(transactionStats.callbackIds)) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 205 | transactionStats.presentFence = mPresentFence; |
| 206 | } |
| 207 | |
| 208 | // Remove the transaction from completed to the callback |
| 209 | listenerStats.transactionStats.push_back(std::move(transactionStats)); |
| 210 | transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr); |
| 211 | } |
| 212 | // If the listener has completed transactions |
| 213 | if (!listenerStats.transactionStats.empty()) { |
| 214 | // If the listener is still alive |
| 215 | if (listener->isBinderAlive()) { |
| 216 | // Send callback. The listener stored in listenerStats |
| 217 | // comes from the cross-process setTransactionState call to |
| 218 | // SF. This MUST be an ITransactionCompletedListener. We |
| 219 | // keep it as an IBinder due to consistency reasons: if we |
| 220 | // interface_cast at the IPC boundary when reading a Parcel, |
| 221 | // we get pointers that compare unequal in the SF process. |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 222 | callbacks.emplace_back([stats = std::move(listenerStats)]() { |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 223 | interface_cast<ITransactionCompletedListener>(stats.listener) |
| 224 | ->onTransactionCompleted(stats); |
| 225 | }); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 226 | } |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 227 | } |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame] | 228 | completedTransactionsItr++; |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | if (mPresentFence) { |
| 232 | mPresentFence.clear(); |
| 233 | } |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 234 | |
| 235 | BackgroundExecutor::getInstance().sendCallbacks(std::move(callbacks)); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // ----------------------------------------------------------------------- |
| 239 | |
| 240 | CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener, |
| 241 | const std::vector<CallbackId>& ids, const sp<IBinder>& sc) |
| 242 | : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {} |
| 243 | |
| 244 | } // namespace android |
| 245 | |
| 246 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 247 | #pragma clang diagnostic pop // ignored "-Wconversion" |