| Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [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 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 18 | #pragma clang diagnostic push | 
|  | 19 | #pragma clang diagnostic ignored "-Wconversion" | 
|  | 20 |  | 
|  | 21 | //#define LOG_NDEBUG 0 | 
|  | 22 | #undef LOG_TAG | 
|  | 23 | #define LOG_TAG "TransactionCallbackInvoker" | 
|  | 24 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 25 |  | 
|  | 26 | #include "TransactionCallbackInvoker.h" | 
|  | 27 |  | 
|  | 28 | #include <cinttypes> | 
|  | 29 |  | 
|  | 30 | #include <binder/IInterface.h> | 
|  | 31 | #include <utils/RefBase.h> | 
|  | 32 |  | 
|  | 33 | namespace android { | 
|  | 34 |  | 
|  | 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 | 
|  | 40 | static int compareCallbackIds(const std::vector<CallbackId>& c1, | 
|  | 41 | const std::vector<CallbackId>& c2) { | 
|  | 42 | if (c1.empty()) { | 
|  | 43 | return !c2.empty(); | 
|  | 44 | } | 
|  | 45 | return c1.front() - c2.front(); | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | TransactionCallbackInvoker::~TransactionCallbackInvoker() { | 
|  | 49 | { | 
|  | 50 | std::lock_guard lock(mMutex); | 
|  | 51 | for (const auto& [listener, transactionStats] : mCompletedTransactions) { | 
|  | 52 | listener->unlinkToDeath(mDeathRecipient); | 
|  | 53 | } | 
|  | 54 | } | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | status_t TransactionCallbackInvoker::startRegistration(const ListenerCallbacks& listenerCallbacks) { | 
|  | 58 | std::lock_guard lock(mMutex); | 
|  | 59 |  | 
|  | 60 | auto [itr, inserted] = mRegisteringTransactions.insert(listenerCallbacks); | 
|  | 61 | auto& [listener, callbackIds] = listenerCallbacks; | 
|  | 62 |  | 
|  | 63 | if (inserted) { | 
|  | 64 | if (mCompletedTransactions.count(listener) == 0) { | 
|  | 65 | status_t err = listener->linkToDeath(mDeathRecipient); | 
|  | 66 | if (err != NO_ERROR) { | 
|  | 67 | ALOGE("cannot add callback because linkToDeath failed, err: %d", err); | 
|  | 68 | return err; | 
|  | 69 | } | 
|  | 70 | } | 
|  | 71 | auto& transactionStatsDeque = mCompletedTransactions[listener]; | 
|  | 72 | transactionStatsDeque.emplace_back(callbackIds); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | return NO_ERROR; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | status_t TransactionCallbackInvoker::endRegistration(const ListenerCallbacks& listenerCallbacks) { | 
|  | 79 | std::lock_guard lock(mMutex); | 
|  | 80 |  | 
|  | 81 | auto itr = mRegisteringTransactions.find(listenerCallbacks); | 
|  | 82 | if (itr == mRegisteringTransactions.end()) { | 
|  | 83 | ALOGE("cannot end a registration that does not exist"); | 
|  | 84 | return BAD_VALUE; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | mRegisteringTransactions.erase(itr); | 
|  | 88 |  | 
|  | 89 | return NO_ERROR; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | bool TransactionCallbackInvoker::isRegisteringTransaction( | 
|  | 93 | const sp<IBinder>& transactionListener, const std::vector<CallbackId>& callbackIds) { | 
|  | 94 | ListenerCallbacks listenerCallbacks(transactionListener, callbackIds); | 
|  | 95 |  | 
|  | 96 | auto itr = mRegisteringTransactions.find(listenerCallbacks); | 
|  | 97 | return itr != mRegisteringTransactions.end(); | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | status_t TransactionCallbackInvoker::registerPendingCallbackHandle( | 
|  | 101 | const sp<CallbackHandle>& handle) { | 
|  | 102 | std::lock_guard lock(mMutex); | 
|  | 103 |  | 
|  | 104 | // If we can't find the transaction stats something has gone wrong. The client should call | 
|  | 105 | // startRegistration before trying to register a pending callback handle. | 
|  | 106 | TransactionStats* transactionStats; | 
|  | 107 | status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); | 
|  | 108 | if (err != NO_ERROR) { | 
|  | 109 | ALOGE("cannot find transaction stats"); | 
|  | 110 | return err; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | mPendingTransactions[handle->listener][handle->callbackIds]++; | 
|  | 114 | return NO_ERROR; | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | status_t TransactionCallbackInvoker::finalizePendingCallbackHandles( | 
|  | 118 | const std::deque<sp<CallbackHandle>>& handles, const std::vector<JankData>& jankData) { | 
|  | 119 | if (handles.empty()) { | 
|  | 120 | return NO_ERROR; | 
|  | 121 | } | 
|  | 122 | std::lock_guard lock(mMutex); | 
|  | 123 |  | 
|  | 124 | for (const auto& handle : handles) { | 
|  | 125 | auto listener = mPendingTransactions.find(handle->listener); | 
|  | 126 | if (listener != mPendingTransactions.end()) { | 
|  | 127 | auto& pendingCallbacks = listener->second; | 
|  | 128 | auto pendingCallback = pendingCallbacks.find(handle->callbackIds); | 
|  | 129 |  | 
|  | 130 | if (pendingCallback != pendingCallbacks.end()) { | 
|  | 131 | auto& pendingCount = pendingCallback->second; | 
|  | 132 |  | 
|  | 133 | // Decrease the pending count for this listener | 
|  | 134 | if (--pendingCount == 0) { | 
|  | 135 | pendingCallbacks.erase(pendingCallback); | 
|  | 136 | } | 
|  | 137 | } else { | 
|  | 138 | ALOGW("there are more latched callbacks than there were registered callbacks"); | 
|  | 139 | } | 
|  | 140 | if (listener->second.size() == 0) { | 
|  | 141 | mPendingTransactions.erase(listener); | 
|  | 142 | } | 
|  | 143 | } else { | 
|  | 144 | ALOGW("cannot find listener in mPendingTransactions"); | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | status_t err = addCallbackHandle(handle, jankData); | 
|  | 148 | if (err != NO_ERROR) { | 
|  | 149 | ALOGE("could not add callback handle"); | 
|  | 150 | return err; | 
|  | 151 | } | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | return NO_ERROR; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | status_t TransactionCallbackInvoker::registerUnpresentedCallbackHandle( | 
|  | 158 | const sp<CallbackHandle>& handle) { | 
|  | 159 | std::lock_guard lock(mMutex); | 
|  | 160 |  | 
|  | 161 | return addCallbackHandle(handle, std::vector<JankData>()); | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | status_t TransactionCallbackInvoker::findTransactionStats( | 
|  | 165 | const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds, | 
|  | 166 | TransactionStats** outTransactionStats) { | 
|  | 167 | auto& transactionStatsDeque = mCompletedTransactions[listener]; | 
|  | 168 |  | 
|  | 169 | // Search back to front because the most recent transactions are at the back of the deque | 
|  | 170 | auto itr = transactionStatsDeque.rbegin(); | 
|  | 171 | for (; itr != transactionStatsDeque.rend(); itr++) { | 
|  | 172 | if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) { | 
|  | 173 | *outTransactionStats = &(*itr); | 
|  | 174 | return NO_ERROR; | 
|  | 175 | } | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | ALOGE("could not find transaction stats"); | 
|  | 179 | return BAD_VALUE; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle, | 
|  | 183 | const std::vector<JankData>& jankData) { | 
|  | 184 | // If we can't find the transaction stats something has gone wrong. The client should call | 
|  | 185 | // startRegistration before trying to add a callback handle. | 
|  | 186 | TransactionStats* transactionStats; | 
|  | 187 | status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); | 
|  | 188 | if (err != NO_ERROR) { | 
|  | 189 | return err; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | transactionStats->latchTime = handle->latchTime; | 
|  | 193 | // If the layer has already been destroyed, don't add the SurfaceControl to the callback. | 
|  | 194 | // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been | 
|  | 195 | // destroyed the client side is dead and there won't be anyone to send the callback to. | 
|  | 196 | sp<IBinder> surfaceControl = handle->surfaceControl.promote(); | 
|  | 197 | if (surfaceControl) { | 
|  | 198 | FrameEventHistoryStats eventStats(handle->frameNumber, | 
|  | 199 | handle->gpuCompositionDoneFence->getSnapshot().fence, | 
|  | 200 | handle->compositorTiming, handle->refreshStartTime, | 
|  | 201 | handle->dequeueReadyTime); | 
|  | 202 | transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime, | 
|  | 203 | handle->previousReleaseFence, | 
|  | 204 | handle->transformHint, eventStats, jankData); | 
|  | 205 | } | 
|  | 206 | return NO_ERROR; | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | void TransactionCallbackInvoker::addPresentFence(const sp<Fence>& presentFence) { | 
|  | 210 | std::lock_guard<std::mutex> lock(mMutex); | 
|  | 211 | mPresentFence = presentFence; | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | void TransactionCallbackInvoker::sendCallbacks() { | 
|  | 215 | std::lock_guard lock(mMutex); | 
|  | 216 |  | 
|  | 217 | // For each listener | 
|  | 218 | auto completedTransactionsItr = mCompletedTransactions.begin(); | 
|  | 219 | while (completedTransactionsItr != mCompletedTransactions.end()) { | 
|  | 220 | auto& [listener, transactionStatsDeque] = *completedTransactionsItr; | 
|  | 221 | ListenerStats listenerStats; | 
|  | 222 | listenerStats.listener = listener; | 
|  | 223 |  | 
|  | 224 | // For each transaction | 
|  | 225 | auto transactionStatsItr = transactionStatsDeque.begin(); | 
|  | 226 | while (transactionStatsItr != transactionStatsDeque.end()) { | 
|  | 227 | auto& transactionStats = *transactionStatsItr; | 
|  | 228 |  | 
|  | 229 | // If this transaction is still registering, it is not safe to send a callback | 
|  | 230 | // because there could be surface controls that haven't been added to | 
|  | 231 | // transaction stats or mPendingTransactions. | 
|  | 232 | if (isRegisteringTransaction(listener, transactionStats.callbackIds)) { | 
|  | 233 | break; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | // If we are still waiting on the callback handles for this transaction, stop | 
|  | 237 | // here because all transaction callbacks for the same listener must come in order | 
|  | 238 | auto pendingTransactions = mPendingTransactions.find(listener); | 
|  | 239 | if (pendingTransactions != mPendingTransactions.end() && | 
|  | 240 | pendingTransactions->second.count(transactionStats.callbackIds) != 0) { | 
|  | 241 | break; | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | // If the transaction has been latched | 
|  | 245 | if (transactionStats.latchTime >= 0) { | 
|  | 246 | if (!mPresentFence) { | 
|  | 247 | break; | 
|  | 248 | } | 
|  | 249 | transactionStats.presentFence = mPresentFence; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | // Remove the transaction from completed to the callback | 
|  | 253 | listenerStats.transactionStats.push_back(std::move(transactionStats)); | 
|  | 254 | transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr); | 
|  | 255 | } | 
|  | 256 | // If the listener has completed transactions | 
|  | 257 | if (!listenerStats.transactionStats.empty()) { | 
|  | 258 | // If the listener is still alive | 
|  | 259 | if (listener->isBinderAlive()) { | 
|  | 260 | // Send callback.  The listener stored in listenerStats | 
|  | 261 | // comes from the cross-process setTransactionState call to | 
|  | 262 | // SF.  This MUST be an ITransactionCompletedListener.  We | 
|  | 263 | // keep it as an IBinder due to consistency reasons: if we | 
|  | 264 | // interface_cast at the IPC boundary when reading a Parcel, | 
|  | 265 | // we get pointers that compare unequal in the SF process. | 
|  | 266 | interface_cast<ITransactionCompletedListener>(listenerStats.listener) | 
|  | 267 | ->onTransactionCompleted(listenerStats); | 
|  | 268 | if (transactionStatsDeque.empty()) { | 
|  | 269 | listener->unlinkToDeath(mDeathRecipient); | 
|  | 270 | completedTransactionsItr = | 
|  | 271 | mCompletedTransactions.erase(completedTransactionsItr); | 
|  | 272 | } else { | 
|  | 273 | completedTransactionsItr++; | 
|  | 274 | } | 
|  | 275 | } else { | 
|  | 276 | completedTransactionsItr = | 
|  | 277 | mCompletedTransactions.erase(completedTransactionsItr); | 
|  | 278 | } | 
|  | 279 | } else { | 
|  | 280 | completedTransactionsItr++; | 
|  | 281 | } | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | if (mPresentFence) { | 
|  | 285 | mPresentFence.clear(); | 
|  | 286 | } | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | // ----------------------------------------------------------------------- | 
|  | 290 |  | 
|  | 291 | CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener, | 
|  | 292 | const std::vector<CallbackId>& ids, const sp<IBinder>& sc) | 
|  | 293 | : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {} | 
|  | 294 |  | 
|  | 295 | } // namespace android | 
|  | 296 |  | 
|  | 297 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 298 | #pragma clang diagnostic pop // ignored "-Wconversion" |