Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 21 | //#define LOG_NDEBUG 0 |
| 22 | #undef LOG_TAG |
| 23 | #define LOG_TAG "TransactionCompletedThread" |
| 24 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 25 | |
| 26 | #include "TransactionCompletedThread.h" |
| 27 | |
| 28 | #include <cinttypes> |
| 29 | |
| 30 | #include <binder/IInterface.h> |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 31 | #include <utils/RefBase.h> |
| 32 | |
| 33 | namespace android { |
| 34 | |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 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 | // |
| 39 | // See CallbackIdsHash for a explaniation of why this works |
Greg Kaiser | a9e843a | 2019-04-01 06:23:09 -0700 | [diff] [blame] | 40 | static int compareCallbackIds(const std::vector<CallbackId>& c1, |
| 41 | const std::vector<CallbackId>& c2) { |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 42 | if (c1.empty()) { |
| 43 | return !c2.empty(); |
| 44 | } |
| 45 | return c1.front() - c2.front(); |
| 46 | } |
| 47 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 48 | TransactionCompletedThread::~TransactionCompletedThread() { |
Marissa Wall | 99343ba | 2018-11-13 10:39:08 -0800 | [diff] [blame] | 49 | std::lock_guard lockThread(mThreadMutex); |
| 50 | |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 51 | { |
| 52 | std::lock_guard lock(mMutex); |
| 53 | mKeepRunning = false; |
| 54 | mConditionVariable.notify_all(); |
| 55 | } |
| 56 | |
Marissa Wall | 05d9dd3 | 2018-11-13 10:05:14 -0800 | [diff] [blame] | 57 | if (mThread.joinable()) { |
| 58 | mThread.join(); |
| 59 | } |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 60 | |
| 61 | { |
| 62 | std::lock_guard lock(mMutex); |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 63 | for (const auto& [listener, transactionStats] : mCompletedTransactions) { |
Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 64 | listener->unlinkToDeath(mDeathRecipient); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void TransactionCompletedThread::run() { |
| 70 | std::lock_guard lock(mMutex); |
Marissa Wall | 99343ba | 2018-11-13 10:39:08 -0800 | [diff] [blame] | 71 | if (mRunning || !mKeepRunning) { |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | mDeathRecipient = new ThreadDeathRecipient(); |
| 75 | mRunning = true; |
Marissa Wall | 99343ba | 2018-11-13 10:39:08 -0800 | [diff] [blame] | 76 | |
| 77 | std::lock_guard lockThread(mThreadMutex); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 78 | mThread = std::thread(&TransactionCompletedThread::threadMain, this); |
| 79 | } |
| 80 | |
Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 81 | status_t TransactionCompletedThread::startRegistration(const ListenerCallbacks& listenerCallbacks) { |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 82 | // begin running if not already running |
| 83 | run(); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 84 | std::lock_guard lock(mMutex); |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 85 | if (!mRunning) { |
| 86 | ALOGE("cannot add callback because the callback thread isn't running"); |
| 87 | return BAD_VALUE; |
Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 90 | auto [itr, inserted] = mRegisteringTransactions.insert(listenerCallbacks); |
Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 91 | auto& [listener, callbackIds] = listenerCallbacks; |
| 92 | |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 93 | if (inserted) { |
| 94 | if (mCompletedTransactions.count(listener) == 0) { |
| 95 | status_t err = listener->linkToDeath(mDeathRecipient); |
| 96 | if (err != NO_ERROR) { |
| 97 | ALOGE("cannot add callback because linkToDeath failed, err: %d", err); |
| 98 | return err; |
| 99 | } |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 100 | } |
Valerie Hau | 9dab973 | 2019-08-20 09:29:25 -0700 | [diff] [blame] | 101 | auto& transactionStatsDeque = mCompletedTransactions[listener]; |
| 102 | transactionStatsDeque.emplace_back(callbackIds); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 105 | return NO_ERROR; |
| 106 | } |
| 107 | |
Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 108 | status_t TransactionCompletedThread::endRegistration(const ListenerCallbacks& listenerCallbacks) { |
| 109 | std::lock_guard lock(mMutex); |
| 110 | if (!mRunning) { |
| 111 | ALOGE("cannot add callback because the callback thread isn't running"); |
| 112 | return BAD_VALUE; |
| 113 | } |
| 114 | |
| 115 | auto itr = mRegisteringTransactions.find(listenerCallbacks); |
| 116 | if (itr == mRegisteringTransactions.end()) { |
| 117 | ALOGE("cannot end a registration that does not exist"); |
| 118 | return BAD_VALUE; |
| 119 | } |
| 120 | |
| 121 | mRegisteringTransactions.erase(itr); |
| 122 | |
| 123 | return NO_ERROR; |
| 124 | } |
| 125 | |
| 126 | bool TransactionCompletedThread::isRegisteringTransaction( |
Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 127 | const sp<IBinder>& transactionListener, const std::vector<CallbackId>& callbackIds) { |
Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 128 | ListenerCallbacks listenerCallbacks(transactionListener, callbackIds); |
| 129 | |
| 130 | auto itr = mRegisteringTransactions.find(listenerCallbacks); |
| 131 | return itr != mRegisteringTransactions.end(); |
| 132 | } |
| 133 | |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 134 | status_t TransactionCompletedThread::registerPendingCallbackHandle( |
| 135 | const sp<CallbackHandle>& handle) { |
| 136 | std::lock_guard lock(mMutex); |
| 137 | if (!mRunning) { |
| 138 | ALOGE("cannot register callback handle because the callback thread isn't running"); |
| 139 | return BAD_VALUE; |
| 140 | } |
| 141 | |
| 142 | // If we can't find the transaction stats something has gone wrong. The client should call |
Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 143 | // startRegistration before trying to register a pending callback handle. |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 144 | TransactionStats* transactionStats; |
| 145 | status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); |
| 146 | if (err != NO_ERROR) { |
| 147 | ALOGE("cannot find transaction stats"); |
| 148 | return err; |
| 149 | } |
| 150 | |
| 151 | mPendingTransactions[handle->listener][handle->callbackIds]++; |
| 152 | return NO_ERROR; |
| 153 | } |
| 154 | |
Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 155 | status_t TransactionCompletedThread::finalizePendingCallbackHandles( |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 156 | const std::deque<sp<CallbackHandle>>& handles) { |
| 157 | std::lock_guard lock(mMutex); |
| 158 | if (!mRunning) { |
| 159 | ALOGE("cannot add presented callback handle because the callback thread isn't running"); |
| 160 | return BAD_VALUE; |
| 161 | } |
| 162 | |
| 163 | for (const auto& handle : handles) { |
| 164 | auto listener = mPendingTransactions.find(handle->listener); |
| 165 | if (listener != mPendingTransactions.end()) { |
| 166 | auto& pendingCallbacks = listener->second; |
| 167 | auto pendingCallback = pendingCallbacks.find(handle->callbackIds); |
| 168 | |
| 169 | if (pendingCallback != pendingCallbacks.end()) { |
| 170 | auto& pendingCount = pendingCallback->second; |
| 171 | |
| 172 | // Decrease the pending count for this listener |
| 173 | if (--pendingCount == 0) { |
| 174 | pendingCallbacks.erase(pendingCallback); |
| 175 | } |
| 176 | } else { |
| 177 | ALOGW("there are more latched callbacks than there were registered callbacks"); |
| 178 | } |
Marissa Wall | b0022cc | 2019-04-16 14:19:55 -0700 | [diff] [blame] | 179 | if (listener->second.size() == 0) { |
| 180 | mPendingTransactions.erase(listener); |
| 181 | } |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 182 | } else { |
| 183 | ALOGW("cannot find listener in mPendingTransactions"); |
| 184 | } |
| 185 | |
| 186 | status_t err = addCallbackHandle(handle); |
| 187 | if (err != NO_ERROR) { |
| 188 | ALOGE("could not add callback handle"); |
| 189 | return err; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return NO_ERROR; |
| 194 | } |
| 195 | |
Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 196 | status_t TransactionCompletedThread::registerUnpresentedCallbackHandle( |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 197 | const sp<CallbackHandle>& handle) { |
| 198 | std::lock_guard lock(mMutex); |
| 199 | if (!mRunning) { |
| 200 | ALOGE("cannot add unpresented callback handle because the callback thread isn't running"); |
| 201 | return BAD_VALUE; |
| 202 | } |
| 203 | |
| 204 | return addCallbackHandle(handle); |
| 205 | } |
| 206 | |
| 207 | status_t TransactionCompletedThread::findTransactionStats( |
Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 208 | const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds, |
| 209 | TransactionStats** outTransactionStats) { |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 210 | auto& transactionStatsDeque = mCompletedTransactions[listener]; |
| 211 | |
| 212 | // Search back to front because the most recent transactions are at the back of the deque |
| 213 | auto itr = transactionStatsDeque.rbegin(); |
| 214 | for (; itr != transactionStatsDeque.rend(); itr++) { |
| 215 | if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) { |
| 216 | *outTransactionStats = &(*itr); |
| 217 | return NO_ERROR; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | ALOGE("could not find transaction stats"); |
| 222 | return BAD_VALUE; |
| 223 | } |
| 224 | |
| 225 | status_t TransactionCompletedThread::addCallbackHandle(const sp<CallbackHandle>& handle) { |
| 226 | // If we can't find the transaction stats something has gone wrong. The client should call |
Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 227 | // startRegistration before trying to add a callback handle. |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 228 | TransactionStats* transactionStats; |
| 229 | status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); |
| 230 | if (err != NO_ERROR) { |
| 231 | return err; |
| 232 | } |
| 233 | |
| 234 | transactionStats->latchTime = handle->latchTime; |
Marissa Wall | 0e24a83 | 2019-07-10 15:32:50 -0700 | [diff] [blame] | 235 | // If the layer has already been destroyed, don't add the SurfaceControl to the callback. |
| 236 | // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been |
| 237 | // destroyed the client side is dead and there won't be anyone to send the callback to. |
| 238 | sp<IBinder> surfaceControl = handle->surfaceControl.promote(); |
| 239 | if (surfaceControl) { |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame^] | 240 | FrameEventHistoryStats eventStats(handle->frameNumber, |
| 241 | handle->gpuCompositionDoneFence->getSnapshot().fence, |
| 242 | handle->compositorTiming, handle->refreshStartTime, |
| 243 | handle->dequeueReadyTime); |
Marissa Wall | 0e24a83 | 2019-07-10 15:32:50 -0700 | [diff] [blame] | 244 | transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime, |
Valerie Hau | 32cdc1f | 2019-10-21 14:45:54 -0700 | [diff] [blame] | 245 | handle->previousReleaseFence, |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame^] | 246 | handle->transformHint, eventStats); |
Marissa Wall | 0e24a83 | 2019-07-10 15:32:50 -0700 | [diff] [blame] | 247 | } |
Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 248 | return NO_ERROR; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void TransactionCompletedThread::addPresentFence(const sp<Fence>& presentFence) { |
| 252 | std::lock_guard<std::mutex> lock(mMutex); |
| 253 | mPresentFence = presentFence; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void TransactionCompletedThread::sendCallbacks() { |
| 257 | std::lock_guard lock(mMutex); |
| 258 | if (mRunning) { |
| 259 | mConditionVariable.notify_all(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void TransactionCompletedThread::threadMain() { |
| 264 | std::lock_guard lock(mMutex); |
| 265 | |
| 266 | while (mKeepRunning) { |
| 267 | mConditionVariable.wait(mMutex); |
Marissa Wall | caa83f5 | 2019-05-29 13:03:25 -0700 | [diff] [blame] | 268 | std::vector<ListenerStats> completedListenerStats; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 269 | |
| 270 | // For each listener |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 271 | auto completedTransactionsItr = mCompletedTransactions.begin(); |
| 272 | while (completedTransactionsItr != mCompletedTransactions.end()) { |
| 273 | auto& [listener, transactionStatsDeque] = *completedTransactionsItr; |
| 274 | ListenerStats listenerStats; |
| 275 | listenerStats.listener = listener; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 276 | |
| 277 | // For each transaction |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 278 | auto transactionStatsItr = transactionStatsDeque.begin(); |
| 279 | while (transactionStatsItr != transactionStatsDeque.end()) { |
| 280 | auto& transactionStats = *transactionStatsItr; |
| 281 | |
Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 282 | // If this transaction is still registering, it is not safe to send a callback |
| 283 | // because there could be surface controls that haven't been added to |
| 284 | // transaction stats or mPendingTransactions. |
| 285 | if (isRegisteringTransaction(listener, transactionStats.callbackIds)) { |
| 286 | break; |
| 287 | } |
| 288 | |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 289 | // If we are still waiting on the callback handles for this transaction, stop |
| 290 | // here because all transaction callbacks for the same listener must come in order |
Marissa Wall | 6110e84 | 2019-04-12 13:29:59 -0700 | [diff] [blame] | 291 | auto pendingTransactions = mPendingTransactions.find(listener); |
| 292 | if (pendingTransactions != mPendingTransactions.end() && |
| 293 | pendingTransactions->second.count(transactionStats.callbackIds) != 0) { |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 294 | break; |
| 295 | } |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 296 | |
| 297 | // If the transaction has been latched |
| 298 | if (transactionStats.latchTime >= 0) { |
Valerie Hau | 63258a1 | 2018-12-14 14:31:48 -0800 | [diff] [blame] | 299 | if (!mPresentFence) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 300 | break; |
| 301 | } |
Valerie Hau | 63258a1 | 2018-12-14 14:31:48 -0800 | [diff] [blame] | 302 | transactionStats.presentFence = mPresentFence; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 303 | } |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 304 | |
| 305 | // Remove the transaction from completed to the callback |
| 306 | listenerStats.transactionStats.push_back(std::move(transactionStats)); |
| 307 | transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 308 | } |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 309 | // If the listener has completed transactions |
| 310 | if (!listenerStats.transactionStats.empty()) { |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 311 | // If the listener is still alive |
Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 312 | if (listener->isBinderAlive()) { |
| 313 | // Send callback. The listener stored in listenerStats |
| 314 | // comes from the cross-process setTransactionState call to |
| 315 | // SF. This MUST be an ITransactionCompletedListener. We |
| 316 | // keep it as an IBinder due to consistency reasons: if we |
| 317 | // interface_cast at the IPC boundary when reading a Parcel, |
| 318 | // we get pointers that compare unequal in the SF process. |
| 319 | interface_cast<ITransactionCompletedListener>(listenerStats.listener) |
| 320 | ->onTransactionCompleted(listenerStats); |
Valerie Hau | dfca3ec | 2019-12-17 08:50:33 -0800 | [diff] [blame] | 321 | if (transactionStatsDeque.empty()) { |
Valerie Hau | 783ec68 | 2020-01-10 08:41:15 -0800 | [diff] [blame] | 322 | listener->unlinkToDeath(mDeathRecipient); |
Valerie Hau | dfca3ec | 2019-12-17 08:50:33 -0800 | [diff] [blame] | 323 | completedTransactionsItr = |
| 324 | mCompletedTransactions.erase(completedTransactionsItr); |
| 325 | } else { |
| 326 | completedTransactionsItr++; |
| 327 | } |
| 328 | } else { |
| 329 | completedTransactionsItr = |
| 330 | mCompletedTransactions.erase(completedTransactionsItr); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 331 | } |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 332 | } else { |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 333 | completedTransactionsItr++; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 334 | } |
Marissa Wall | caa83f5 | 2019-05-29 13:03:25 -0700 | [diff] [blame] | 335 | |
| 336 | completedListenerStats.push_back(std::move(listenerStats)); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 337 | } |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 338 | |
| 339 | if (mPresentFence) { |
| 340 | mPresentFence.clear(); |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 341 | } |
Marissa Wall | caa83f5 | 2019-05-29 13:03:25 -0700 | [diff] [blame] | 342 | |
| 343 | // If everyone else has dropped their reference to a layer and its listener is dead, |
| 344 | // we are about to cause the layer to be deleted. If this happens at the wrong time and |
| 345 | // we are holding mMutex, we will cause a deadlock. |
| 346 | // |
| 347 | // The deadlock happens because this thread is holding on to mMutex and when we delete |
| 348 | // the layer, it grabs SF's mStateLock. A different SF binder thread grabs mStateLock, |
| 349 | // then call's TransactionCompletedThread::run() which tries to grab mMutex. |
| 350 | // |
| 351 | // To avoid this deadlock, we need to unlock mMutex when dropping our last reference to |
| 352 | // to the layer. |
| 353 | mMutex.unlock(); |
| 354 | completedListenerStats.clear(); |
| 355 | mMutex.lock(); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | // ----------------------------------------------------------------------- |
| 360 | |
Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 361 | CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener, |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 362 | const std::vector<CallbackId>& ids, const sp<IBinder>& sc) |
| 363 | : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {} |
| 364 | |
| 365 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 366 | |
| 367 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 368 | #pragma clang diagnostic pop // ignored "-Wconversion" |