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