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