| 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) { | 
|  | 240 | transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime, | 
| Valerie Hau | 32cdc1f | 2019-10-21 14:45:54 -0700 | [diff] [blame] | 241 | handle->previousReleaseFence, | 
|  | 242 | handle->transformHint); | 
| Marissa Wall | 0e24a83 | 2019-07-10 15:32:50 -0700 | [diff] [blame] | 243 | } | 
| Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 244 | return NO_ERROR; | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 245 | } | 
|  | 246 |  | 
|  | 247 | void TransactionCompletedThread::addPresentFence(const sp<Fence>& presentFence) { | 
|  | 248 | std::lock_guard<std::mutex> lock(mMutex); | 
|  | 249 | mPresentFence = presentFence; | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 250 | } | 
|  | 251 |  | 
|  | 252 | void TransactionCompletedThread::sendCallbacks() { | 
|  | 253 | std::lock_guard lock(mMutex); | 
|  | 254 | if (mRunning) { | 
|  | 255 | mConditionVariable.notify_all(); | 
|  | 256 | } | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | void TransactionCompletedThread::threadMain() { | 
|  | 260 | std::lock_guard lock(mMutex); | 
|  | 261 |  | 
|  | 262 | while (mKeepRunning) { | 
|  | 263 | mConditionVariable.wait(mMutex); | 
| Marissa Wall | caa83f5 | 2019-05-29 13:03:25 -0700 | [diff] [blame] | 264 | std::vector<ListenerStats> completedListenerStats; | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 265 |  | 
|  | 266 | // For each listener | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 267 | auto completedTransactionsItr = mCompletedTransactions.begin(); | 
|  | 268 | while (completedTransactionsItr != mCompletedTransactions.end()) { | 
|  | 269 | auto& [listener, transactionStatsDeque] = *completedTransactionsItr; | 
|  | 270 | ListenerStats listenerStats; | 
|  | 271 | listenerStats.listener = listener; | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 272 |  | 
|  | 273 | // For each transaction | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 274 | auto transactionStatsItr = transactionStatsDeque.begin(); | 
|  | 275 | while (transactionStatsItr != transactionStatsDeque.end()) { | 
|  | 276 | auto& transactionStats = *transactionStatsItr; | 
|  | 277 |  | 
| Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 278 | // If this transaction is still registering, it is not safe to send a callback | 
|  | 279 | // because there could be surface controls that haven't been added to | 
|  | 280 | // transaction stats or mPendingTransactions. | 
|  | 281 | if (isRegisteringTransaction(listener, transactionStats.callbackIds)) { | 
|  | 282 | break; | 
|  | 283 | } | 
|  | 284 |  | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 285 | // If we are still waiting on the callback handles for this transaction, stop | 
|  | 286 | // 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] | 287 | auto pendingTransactions = mPendingTransactions.find(listener); | 
|  | 288 | if (pendingTransactions != mPendingTransactions.end() && | 
|  | 289 | pendingTransactions->second.count(transactionStats.callbackIds) != 0) { | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 290 | break; | 
|  | 291 | } | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 292 |  | 
|  | 293 | // If the transaction has been latched | 
|  | 294 | if (transactionStats.latchTime >= 0) { | 
| Valerie Hau | 63258a1 | 2018-12-14 14:31:48 -0800 | [diff] [blame] | 295 | if (!mPresentFence) { | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 296 | break; | 
|  | 297 | } | 
| Valerie Hau | 63258a1 | 2018-12-14 14:31:48 -0800 | [diff] [blame] | 298 | transactionStats.presentFence = mPresentFence; | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 299 | } | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 300 |  | 
|  | 301 | // Remove the transaction from completed to the callback | 
|  | 302 | listenerStats.transactionStats.push_back(std::move(transactionStats)); | 
|  | 303 | transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 304 | } | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 305 | // If the listener has completed transactions | 
|  | 306 | if (!listenerStats.transactionStats.empty()) { | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 307 | // If the listener is still alive | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 308 | if (listener->isBinderAlive()) { | 
|  | 309 | // Send callback.  The listener stored in listenerStats | 
|  | 310 | // comes from the cross-process setTransactionState call to | 
|  | 311 | // SF.  This MUST be an ITransactionCompletedListener.  We | 
|  | 312 | // keep it as an IBinder due to consistency reasons: if we | 
|  | 313 | // interface_cast at the IPC boundary when reading a Parcel, | 
|  | 314 | // we get pointers that compare unequal in the SF process. | 
|  | 315 | interface_cast<ITransactionCompletedListener>(listenerStats.listener) | 
|  | 316 | ->onTransactionCompleted(listenerStats); | 
| Valerie Hau | dfca3ec | 2019-12-17 08:50:33 -0800 | [diff] [blame] | 317 | if (transactionStatsDeque.empty()) { | 
| Valerie Hau | 783ec68 | 2020-01-10 08:41:15 -0800 | [diff] [blame] | 318 | listener->unlinkToDeath(mDeathRecipient); | 
| Valerie Hau | dfca3ec | 2019-12-17 08:50:33 -0800 | [diff] [blame] | 319 | completedTransactionsItr = | 
|  | 320 | mCompletedTransactions.erase(completedTransactionsItr); | 
|  | 321 | } else { | 
|  | 322 | completedTransactionsItr++; | 
|  | 323 | } | 
|  | 324 | } else { | 
|  | 325 | completedTransactionsItr = | 
|  | 326 | mCompletedTransactions.erase(completedTransactionsItr); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 327 | } | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 328 | } else { | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 329 | completedTransactionsItr++; | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 330 | } | 
| Marissa Wall | caa83f5 | 2019-05-29 13:03:25 -0700 | [diff] [blame] | 331 |  | 
|  | 332 | completedListenerStats.push_back(std::move(listenerStats)); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 333 | } | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 334 |  | 
|  | 335 | if (mPresentFence) { | 
|  | 336 | mPresentFence.clear(); | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 337 | } | 
| Marissa Wall | caa83f5 | 2019-05-29 13:03:25 -0700 | [diff] [blame] | 338 |  | 
|  | 339 | // If everyone else has dropped their reference to a layer and its listener is dead, | 
|  | 340 | // we are about to cause the layer to be deleted. If this happens at the wrong time and | 
|  | 341 | // we are holding mMutex, we will cause a deadlock. | 
|  | 342 | // | 
|  | 343 | // The deadlock happens because this thread is holding on to mMutex and when we delete | 
|  | 344 | // the layer, it grabs SF's mStateLock. A different SF binder thread grabs mStateLock, | 
|  | 345 | // then call's TransactionCompletedThread::run() which tries to grab mMutex. | 
|  | 346 | // | 
|  | 347 | // To avoid this deadlock, we need to unlock mMutex when dropping our last reference to | 
|  | 348 | // to the layer. | 
|  | 349 | mMutex.unlock(); | 
|  | 350 | completedListenerStats.clear(); | 
|  | 351 | mMutex.lock(); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 352 | } | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | // ----------------------------------------------------------------------- | 
|  | 356 |  | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 357 | CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener, | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 358 | const std::vector<CallbackId>& ids, const sp<IBinder>& sc) | 
|  | 359 | : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {} | 
|  | 360 |  | 
|  | 361 | } // namespace android | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 362 |  | 
|  | 363 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 364 | #pragma clang diagnostic pop // ignored "-Wconversion" |