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