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 | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 78 | status_t TransactionCompletedThread::addCallback(const sp<ITransactionCompletedListener>& listener, |
| 79 | const std::vector<CallbackId>& callbackIds) { |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 80 | std::lock_guard lock(mMutex); |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 81 | if (!mRunning) { |
| 82 | ALOGE("cannot add callback because the callback thread isn't running"); |
| 83 | return BAD_VALUE; |
Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 86 | if (mCompletedTransactions.count(listener) == 0) { |
| 87 | status_t err = IInterface::asBinder(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 | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 94 | auto& transactionStatsDeque = mCompletedTransactions[listener]; |
| 95 | transactionStatsDeque.emplace_back(callbackIds); |
| 96 | return NO_ERROR; |
| 97 | } |
| 98 | |
| 99 | status_t TransactionCompletedThread::registerPendingCallbackHandle( |
| 100 | const sp<CallbackHandle>& handle) { |
| 101 | std::lock_guard lock(mMutex); |
| 102 | if (!mRunning) { |
| 103 | ALOGE("cannot register callback handle because the callback thread isn't running"); |
| 104 | return BAD_VALUE; |
| 105 | } |
| 106 | |
| 107 | // If we can't find the transaction stats something has gone wrong. The client should call |
| 108 | // addCallback before trying to register a pending callback handle. |
| 109 | TransactionStats* transactionStats; |
| 110 | status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); |
| 111 | if (err != NO_ERROR) { |
| 112 | ALOGE("cannot find transaction stats"); |
| 113 | return err; |
| 114 | } |
| 115 | |
| 116 | mPendingTransactions[handle->listener][handle->callbackIds]++; |
| 117 | return NO_ERROR; |
| 118 | } |
| 119 | |
| 120 | status_t TransactionCompletedThread::addPresentedCallbackHandles( |
| 121 | const std::deque<sp<CallbackHandle>>& handles) { |
| 122 | std::lock_guard lock(mMutex); |
| 123 | if (!mRunning) { |
| 124 | ALOGE("cannot add presented callback handle because the callback thread isn't running"); |
| 125 | return BAD_VALUE; |
| 126 | } |
| 127 | |
| 128 | for (const auto& handle : handles) { |
| 129 | auto listener = mPendingTransactions.find(handle->listener); |
| 130 | if (listener != mPendingTransactions.end()) { |
| 131 | auto& pendingCallbacks = listener->second; |
| 132 | auto pendingCallback = pendingCallbacks.find(handle->callbackIds); |
| 133 | |
| 134 | if (pendingCallback != pendingCallbacks.end()) { |
| 135 | auto& pendingCount = pendingCallback->second; |
| 136 | |
| 137 | // Decrease the pending count for this listener |
| 138 | if (--pendingCount == 0) { |
| 139 | pendingCallbacks.erase(pendingCallback); |
| 140 | } |
| 141 | } else { |
| 142 | ALOGW("there are more latched callbacks than there were registered callbacks"); |
| 143 | } |
| 144 | } else { |
| 145 | ALOGW("cannot find listener in mPendingTransactions"); |
| 146 | } |
| 147 | |
| 148 | status_t err = addCallbackHandle(handle); |
| 149 | if (err != NO_ERROR) { |
| 150 | ALOGE("could not add callback handle"); |
| 151 | return err; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return NO_ERROR; |
| 156 | } |
| 157 | |
| 158 | status_t TransactionCompletedThread::addUnpresentedCallbackHandle( |
| 159 | const sp<CallbackHandle>& handle) { |
| 160 | std::lock_guard lock(mMutex); |
| 161 | if (!mRunning) { |
| 162 | ALOGE("cannot add unpresented callback handle because the callback thread isn't running"); |
| 163 | return BAD_VALUE; |
| 164 | } |
| 165 | |
| 166 | return addCallbackHandle(handle); |
| 167 | } |
| 168 | |
| 169 | status_t TransactionCompletedThread::findTransactionStats( |
| 170 | const sp<ITransactionCompletedListener>& listener, |
| 171 | const std::vector<CallbackId>& callbackIds, TransactionStats** outTransactionStats) { |
| 172 | auto& transactionStatsDeque = mCompletedTransactions[listener]; |
| 173 | |
| 174 | // Search back to front because the most recent transactions are at the back of the deque |
| 175 | auto itr = transactionStatsDeque.rbegin(); |
| 176 | for (; itr != transactionStatsDeque.rend(); itr++) { |
| 177 | if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) { |
| 178 | *outTransactionStats = &(*itr); |
| 179 | return NO_ERROR; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | ALOGE("could not find transaction stats"); |
| 184 | return BAD_VALUE; |
| 185 | } |
| 186 | |
| 187 | status_t TransactionCompletedThread::addCallbackHandle(const sp<CallbackHandle>& handle) { |
| 188 | // If we can't find the transaction stats something has gone wrong. The client should call |
| 189 | // addCallback before trying to add a presnted callback handle. |
| 190 | TransactionStats* transactionStats; |
| 191 | status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); |
| 192 | if (err != NO_ERROR) { |
| 193 | return err; |
| 194 | } |
| 195 | |
| 196 | transactionStats->latchTime = handle->latchTime; |
| 197 | transactionStats->surfaceStats.emplace_back(handle->surfaceControl, handle->acquireTime, |
| 198 | handle->previousReleaseFence); |
Marissa Wall | 3dad52d | 2019-03-22 14:03:19 -0700 | [diff] [blame] | 199 | return NO_ERROR; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void TransactionCompletedThread::addPresentFence(const sp<Fence>& presentFence) { |
| 203 | std::lock_guard<std::mutex> lock(mMutex); |
| 204 | mPresentFence = presentFence; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void TransactionCompletedThread::sendCallbacks() { |
| 208 | std::lock_guard lock(mMutex); |
| 209 | if (mRunning) { |
| 210 | mConditionVariable.notify_all(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void TransactionCompletedThread::threadMain() { |
| 215 | std::lock_guard lock(mMutex); |
| 216 | |
| 217 | while (mKeepRunning) { |
| 218 | mConditionVariable.wait(mMutex); |
| 219 | |
| 220 | // For each listener |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 221 | auto completedTransactionsItr = mCompletedTransactions.begin(); |
| 222 | while (completedTransactionsItr != mCompletedTransactions.end()) { |
| 223 | auto& [listener, transactionStatsDeque] = *completedTransactionsItr; |
| 224 | ListenerStats listenerStats; |
| 225 | listenerStats.listener = listener; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 226 | |
| 227 | // For each transaction |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 228 | auto transactionStatsItr = transactionStatsDeque.begin(); |
| 229 | while (transactionStatsItr != transactionStatsDeque.end()) { |
| 230 | auto& transactionStats = *transactionStatsItr; |
| 231 | |
| 232 | // If we are still waiting on the callback handles for this transaction, stop |
| 233 | // here because all transaction callbacks for the same listener must come in order |
| 234 | if (mPendingTransactions[listener].count(transactionStats.callbackIds) != 0) { |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 235 | break; |
| 236 | } |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 237 | |
| 238 | // If the transaction has been latched |
| 239 | if (transactionStats.latchTime >= 0) { |
Valerie Hau | 63258a1 | 2018-12-14 14:31:48 -0800 | [diff] [blame] | 240 | if (!mPresentFence) { |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 241 | break; |
| 242 | } |
Valerie Hau | 63258a1 | 2018-12-14 14:31:48 -0800 | [diff] [blame] | 243 | transactionStats.presentFence = mPresentFence; |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 244 | } |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 245 | |
| 246 | // Remove the transaction from completed to the callback |
| 247 | listenerStats.transactionStats.push_back(std::move(transactionStats)); |
| 248 | transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 249 | } |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 250 | // If the listener has completed transactions |
| 251 | if (!listenerStats.transactionStats.empty()) { |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 252 | // If the listener is still alive |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 253 | if (IInterface::asBinder(listener)->isBinderAlive()) { |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 254 | // Send callback |
| 255 | listenerStats.listener->onTransactionCompleted(listenerStats); |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 256 | IInterface::asBinder(listener)->unlinkToDeath(mDeathRecipient); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 257 | } |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 258 | completedTransactionsItr = mCompletedTransactions.erase(completedTransactionsItr); |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 259 | } else { |
Marissa Wall | d600d57 | 2019-03-26 15:38:50 -0700 | [diff] [blame] | 260 | completedTransactionsItr++; |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 263 | |
| 264 | if (mPresentFence) { |
| 265 | mPresentFence.clear(); |
Marissa Wall | fda30bb | 2018-10-12 11:34:28 -0700 | [diff] [blame] | 266 | } |
Marissa Wall | e2ffb42 | 2018-10-12 11:33:52 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | // ----------------------------------------------------------------------- |
| 271 | |
| 272 | CallbackHandle::CallbackHandle(const sp<ITransactionCompletedListener>& transactionListener, |
| 273 | const std::vector<CallbackId>& ids, const sp<IBinder>& sc) |
| 274 | : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {} |
| 275 | |
| 276 | } // namespace android |