| 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) { | 
| Valerie Hau | 109ad71 | 2020-06-18 17:00:58 -0700 | [diff] [blame] | 157 | if (handles.empty()) { | 
|  | 158 | return NO_ERROR; | 
|  | 159 | } | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 160 | std::lock_guard lock(mMutex); | 
|  | 161 | if (!mRunning) { | 
|  | 162 | ALOGE("cannot add presented callback handle because the callback thread isn't running"); | 
|  | 163 | return BAD_VALUE; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | for (const auto& handle : handles) { | 
|  | 167 | auto listener = mPendingTransactions.find(handle->listener); | 
|  | 168 | if (listener != mPendingTransactions.end()) { | 
|  | 169 | auto& pendingCallbacks = listener->second; | 
|  | 170 | auto pendingCallback = pendingCallbacks.find(handle->callbackIds); | 
|  | 171 |  | 
|  | 172 | if (pendingCallback != pendingCallbacks.end()) { | 
|  | 173 | auto& pendingCount = pendingCallback->second; | 
|  | 174 |  | 
|  | 175 | // Decrease the pending count for this listener | 
|  | 176 | if (--pendingCount == 0) { | 
|  | 177 | pendingCallbacks.erase(pendingCallback); | 
|  | 178 | } | 
|  | 179 | } else { | 
|  | 180 | ALOGW("there are more latched callbacks than there were registered callbacks"); | 
|  | 181 | } | 
| Marissa Wall | b0022cc | 2019-04-16 14:19:55 -0700 | [diff] [blame] | 182 | if (listener->second.size() == 0) { | 
|  | 183 | mPendingTransactions.erase(listener); | 
|  | 184 | } | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 185 | } else { | 
|  | 186 | ALOGW("cannot find listener in mPendingTransactions"); | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | status_t err = addCallbackHandle(handle); | 
|  | 190 | if (err != NO_ERROR) { | 
|  | 191 | ALOGE("could not add callback handle"); | 
|  | 192 | return err; | 
|  | 193 | } | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | return NO_ERROR; | 
|  | 197 | } | 
|  | 198 |  | 
| Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 199 | status_t TransactionCompletedThread::registerUnpresentedCallbackHandle( | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 200 | const sp<CallbackHandle>& handle) { | 
|  | 201 | std::lock_guard lock(mMutex); | 
|  | 202 | if (!mRunning) { | 
|  | 203 | ALOGE("cannot add unpresented callback handle because the callback thread isn't running"); | 
|  | 204 | return BAD_VALUE; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | return addCallbackHandle(handle); | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | status_t TransactionCompletedThread::findTransactionStats( | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 211 | const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds, | 
|  | 212 | TransactionStats** outTransactionStats) { | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 213 | auto& transactionStatsDeque = mCompletedTransactions[listener]; | 
|  | 214 |  | 
|  | 215 | // Search back to front because the most recent transactions are at the back of the deque | 
|  | 216 | auto itr = transactionStatsDeque.rbegin(); | 
|  | 217 | for (; itr != transactionStatsDeque.rend(); itr++) { | 
|  | 218 | if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) { | 
|  | 219 | *outTransactionStats = &(*itr); | 
|  | 220 | return NO_ERROR; | 
|  | 221 | } | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | ALOGE("could not find transaction stats"); | 
|  | 225 | return BAD_VALUE; | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | status_t TransactionCompletedThread::addCallbackHandle(const sp<CallbackHandle>& handle) { | 
|  | 229 | // 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] | 230 | // startRegistration before trying to add a callback handle. | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 231 | TransactionStats* transactionStats; | 
|  | 232 | status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); | 
|  | 233 | if (err != NO_ERROR) { | 
|  | 234 | return err; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | transactionStats->latchTime = handle->latchTime; | 
| Marissa Wall | 0e24a83 | 2019-07-10 15:32:50 -0700 | [diff] [blame] | 238 | // If the layer has already been destroyed, don't add the SurfaceControl to the callback. | 
|  | 239 | // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been | 
|  | 240 | // destroyed the client side is dead and there won't be anyone to send the callback to. | 
|  | 241 | sp<IBinder> surfaceControl = handle->surfaceControl.promote(); | 
|  | 242 | if (surfaceControl) { | 
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 243 | FrameEventHistoryStats eventStats(handle->frameNumber, | 
|  | 244 | handle->gpuCompositionDoneFence->getSnapshot().fence, | 
|  | 245 | handle->compositorTiming, handle->refreshStartTime, | 
|  | 246 | handle->dequeueReadyTime); | 
| Marissa Wall | 0e24a83 | 2019-07-10 15:32:50 -0700 | [diff] [blame] | 247 | transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime, | 
| Valerie Hau | 32cdc1f | 2019-10-21 14:45:54 -0700 | [diff] [blame] | 248 | handle->previousReleaseFence, | 
| Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 249 | handle->transformHint, eventStats); | 
| Marissa Wall | 0e24a83 | 2019-07-10 15:32:50 -0700 | [diff] [blame] | 250 | } | 
| Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 251 | return NO_ERROR; | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 252 | } | 
|  | 253 |  | 
|  | 254 | void TransactionCompletedThread::addPresentFence(const sp<Fence>& presentFence) { | 
|  | 255 | std::lock_guard<std::mutex> lock(mMutex); | 
|  | 256 | mPresentFence = presentFence; | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
|  | 259 | void TransactionCompletedThread::sendCallbacks() { | 
|  | 260 | std::lock_guard lock(mMutex); | 
|  | 261 | if (mRunning) { | 
|  | 262 | mConditionVariable.notify_all(); | 
|  | 263 | } | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | void TransactionCompletedThread::threadMain() { | 
|  | 267 | std::lock_guard lock(mMutex); | 
|  | 268 |  | 
|  | 269 | while (mKeepRunning) { | 
|  | 270 | mConditionVariable.wait(mMutex); | 
| Marissa Wall | caa83f5 | 2019-05-29 13:03:25 -0700 | [diff] [blame] | 271 | std::vector<ListenerStats> completedListenerStats; | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 272 |  | 
|  | 273 | // For each listener | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 274 | auto completedTransactionsItr = mCompletedTransactions.begin(); | 
|  | 275 | while (completedTransactionsItr != mCompletedTransactions.end()) { | 
|  | 276 | auto& [listener, transactionStatsDeque] = *completedTransactionsItr; | 
|  | 277 | ListenerStats listenerStats; | 
|  | 278 | listenerStats.listener = listener; | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 279 |  | 
|  | 280 | // For each transaction | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 281 | auto transactionStatsItr = transactionStatsDeque.begin(); | 
|  | 282 | while (transactionStatsItr != transactionStatsDeque.end()) { | 
|  | 283 | auto& transactionStats = *transactionStatsItr; | 
|  | 284 |  | 
| Marissa Wall | efb71af | 2019-06-27 14:45:53 -0700 | [diff] [blame] | 285 | // If this transaction is still registering, it is not safe to send a callback | 
|  | 286 | // because there could be surface controls that haven't been added to | 
|  | 287 | // transaction stats or mPendingTransactions. | 
|  | 288 | if (isRegisteringTransaction(listener, transactionStats.callbackIds)) { | 
|  | 289 | break; | 
|  | 290 | } | 
|  | 291 |  | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 292 | // If we are still waiting on the callback handles for this transaction, stop | 
|  | 293 | // 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] | 294 | auto pendingTransactions = mPendingTransactions.find(listener); | 
|  | 295 | if (pendingTransactions != mPendingTransactions.end() && | 
|  | 296 | pendingTransactions->second.count(transactionStats.callbackIds) != 0) { | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 297 | break; | 
|  | 298 | } | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 299 |  | 
|  | 300 | // If the transaction has been latched | 
|  | 301 | if (transactionStats.latchTime >= 0) { | 
| Valerie Hau | 63258a1 | 2018-12-14 14:31:48 -0800 | [diff] [blame] | 302 | if (!mPresentFence) { | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 303 | break; | 
|  | 304 | } | 
| Valerie Hau | 63258a1 | 2018-12-14 14:31:48 -0800 | [diff] [blame] | 305 | transactionStats.presentFence = mPresentFence; | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 306 | } | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 307 |  | 
|  | 308 | // Remove the transaction from completed to the callback | 
|  | 309 | listenerStats.transactionStats.push_back(std::move(transactionStats)); | 
|  | 310 | transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 311 | } | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 312 | // If the listener has completed transactions | 
|  | 313 | if (!listenerStats.transactionStats.empty()) { | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 314 | // If the listener is still alive | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 315 | if (listener->isBinderAlive()) { | 
|  | 316 | // Send callback.  The listener stored in listenerStats | 
|  | 317 | // comes from the cross-process setTransactionState call to | 
|  | 318 | // SF.  This MUST be an ITransactionCompletedListener.  We | 
|  | 319 | // keep it as an IBinder due to consistency reasons: if we | 
|  | 320 | // interface_cast at the IPC boundary when reading a Parcel, | 
|  | 321 | // we get pointers that compare unequal in the SF process. | 
|  | 322 | interface_cast<ITransactionCompletedListener>(listenerStats.listener) | 
|  | 323 | ->onTransactionCompleted(listenerStats); | 
| Valerie Hau | dfca3ec | 2019-12-17 08:50:33 -0800 | [diff] [blame] | 324 | if (transactionStatsDeque.empty()) { | 
| Valerie Hau | 783ec68 | 2020-01-10 08:41:15 -0800 | [diff] [blame] | 325 | listener->unlinkToDeath(mDeathRecipient); | 
| Valerie Hau | dfca3ec | 2019-12-17 08:50:33 -0800 | [diff] [blame] | 326 | completedTransactionsItr = | 
|  | 327 | mCompletedTransactions.erase(completedTransactionsItr); | 
|  | 328 | } else { | 
|  | 329 | completedTransactionsItr++; | 
|  | 330 | } | 
|  | 331 | } else { | 
|  | 332 | completedTransactionsItr = | 
|  | 333 | mCompletedTransactions.erase(completedTransactionsItr); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 334 | } | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 335 | } else { | 
| Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 336 | completedTransactionsItr++; | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 337 | } | 
| Marissa Wall | caa83f5 | 2019-05-29 13:03:25 -0700 | [diff] [blame] | 338 |  | 
|  | 339 | completedListenerStats.push_back(std::move(listenerStats)); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 340 | } | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 341 |  | 
|  | 342 | if (mPresentFence) { | 
|  | 343 | mPresentFence.clear(); | 
| Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 344 | } | 
| Marissa Wall | caa83f5 | 2019-05-29 13:03:25 -0700 | [diff] [blame] | 345 |  | 
|  | 346 | // If everyone else has dropped their reference to a layer and its listener is dead, | 
|  | 347 | // we are about to cause the layer to be deleted. If this happens at the wrong time and | 
|  | 348 | // we are holding mMutex, we will cause a deadlock. | 
|  | 349 | // | 
|  | 350 | // The deadlock happens because this thread is holding on to mMutex and when we delete | 
|  | 351 | // the layer, it grabs SF's mStateLock. A different SF binder thread grabs mStateLock, | 
|  | 352 | // then call's TransactionCompletedThread::run() which tries to grab mMutex. | 
|  | 353 | // | 
|  | 354 | // To avoid this deadlock, we need to unlock mMutex when dropping our last reference to | 
|  | 355 | // to the layer. | 
|  | 356 | mMutex.unlock(); | 
|  | 357 | completedListenerStats.clear(); | 
|  | 358 | mMutex.lock(); | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 359 | } | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | // ----------------------------------------------------------------------- | 
|  | 363 |  | 
| Valerie Hau | 5de3ad2 | 2019-08-20 07:47:43 -0700 | [diff] [blame] | 364 | CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener, | 
| Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 365 | const std::vector<CallbackId>& ids, const sp<IBinder>& sc) | 
|  | 366 | : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {} | 
|  | 367 |  | 
|  | 368 | } // namespace android | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 369 |  | 
|  | 370 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 371 | #pragma clang diagnostic pop // ignored "-Wconversion" |