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 | // |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 39 | // See CallbackIdsHash for a explanation of why this works |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 40 | static int compareCallbackIds(const std::vector<CallbackId>& c1, |
| 41 | const std::vector<CallbackId>& c2) { |
| 42 | if (c1.empty()) { |
| 43 | return !c2.empty(); |
| 44 | } |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 45 | return c1.front().id - c2.front().id; |
| 46 | } |
| 47 | |
| 48 | static bool containsOnCommitCallbacks(const std::vector<CallbackId>& callbacks) { |
| 49 | return !callbacks.empty() && callbacks.front().type == CallbackId::Type::ON_COMMIT; |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame^] | 52 | void TransactionCallbackInvoker::addEmptyTransaction(const ListenerCallbacks& listenerCallbacks) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 53 | auto& [listener, callbackIds] = listenerCallbacks; |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame^] | 54 | auto& transactionStatsDeque = mCompletedTransactions[listener]; |
| 55 | transactionStatsDeque.emplace_back(callbackIds); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame^] | 58 | status_t TransactionCallbackInvoker::addOnCommitCallbackHandles( |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 59 | const std::deque<sp<CallbackHandle>>& handles, |
| 60 | std::deque<sp<CallbackHandle>>& outRemainingHandles) { |
| 61 | if (handles.empty()) { |
| 62 | return NO_ERROR; |
| 63 | } |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 64 | const std::vector<JankData>& jankData = std::vector<JankData>(); |
| 65 | for (const auto& handle : handles) { |
| 66 | if (!containsOnCommitCallbacks(handle->callbackIds)) { |
| 67 | outRemainingHandles.push_back(handle); |
| 68 | continue; |
| 69 | } |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame^] | 70 | status_t err = addCallbackHandle(handle, jankData); |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 71 | if (err != NO_ERROR) { |
| 72 | return err; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return NO_ERROR; |
| 77 | } |
| 78 | |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame^] | 79 | status_t TransactionCallbackInvoker::addCallbackHandles( |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 80 | const std::deque<sp<CallbackHandle>>& handles, const std::vector<JankData>& jankData) { |
| 81 | if (handles.empty()) { |
| 82 | return NO_ERROR; |
| 83 | } |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 84 | for (const auto& handle : handles) { |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame^] | 85 | status_t err = addCallbackHandle(handle, jankData); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 86 | if (err != NO_ERROR) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 87 | return err; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return NO_ERROR; |
| 92 | } |
| 93 | |
| 94 | status_t TransactionCallbackInvoker::registerUnpresentedCallbackHandle( |
| 95 | const sp<CallbackHandle>& handle) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 96 | return addCallbackHandle(handle, std::vector<JankData>()); |
| 97 | } |
| 98 | |
| 99 | status_t TransactionCallbackInvoker::findTransactionStats( |
| 100 | const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds, |
| 101 | TransactionStats** outTransactionStats) { |
| 102 | auto& transactionStatsDeque = mCompletedTransactions[listener]; |
| 103 | |
| 104 | // Search back to front because the most recent transactions are at the back of the deque |
| 105 | auto itr = transactionStatsDeque.rbegin(); |
| 106 | for (; itr != transactionStatsDeque.rend(); itr++) { |
| 107 | if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) { |
| 108 | *outTransactionStats = &(*itr); |
| 109 | return NO_ERROR; |
| 110 | } |
| 111 | } |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame^] | 112 | *outTransactionStats = &transactionStatsDeque.emplace_back(callbackIds); |
| 113 | return NO_ERROR; |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle, |
| 117 | const std::vector<JankData>& jankData) { |
| 118 | // If we can't find the transaction stats something has gone wrong. The client should call |
| 119 | // startRegistration before trying to add a callback handle. |
| 120 | TransactionStats* transactionStats; |
| 121 | status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats); |
| 122 | if (err != NO_ERROR) { |
| 123 | return err; |
| 124 | } |
| 125 | |
| 126 | transactionStats->latchTime = handle->latchTime; |
| 127 | // If the layer has already been destroyed, don't add the SurfaceControl to the callback. |
| 128 | // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been |
| 129 | // destroyed the client side is dead and there won't be anyone to send the callback to. |
| 130 | sp<IBinder> surfaceControl = handle->surfaceControl.promote(); |
| 131 | if (surfaceControl) { |
| 132 | FrameEventHistoryStats eventStats(handle->frameNumber, |
| 133 | handle->gpuCompositionDoneFence->getSnapshot().fence, |
| 134 | handle->compositorTiming, handle->refreshStartTime, |
| 135 | handle->dequeueReadyTime); |
| 136 | transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime, |
| 137 | handle->previousReleaseFence, |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 138 | handle->transformHint, |
| 139 | handle->currentMaxAcquiredBufferCount, |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 140 | eventStats, jankData, |
| 141 | handle->previousReleaseCallbackId); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 142 | } |
| 143 | return NO_ERROR; |
| 144 | } |
| 145 | |
| 146 | void TransactionCallbackInvoker::addPresentFence(const sp<Fence>& presentFence) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 147 | mPresentFence = presentFence; |
| 148 | } |
| 149 | |
| 150 | void TransactionCallbackInvoker::sendCallbacks() { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 151 | // For each listener |
| 152 | auto completedTransactionsItr = mCompletedTransactions.begin(); |
| 153 | while (completedTransactionsItr != mCompletedTransactions.end()) { |
| 154 | auto& [listener, transactionStatsDeque] = *completedTransactionsItr; |
| 155 | ListenerStats listenerStats; |
| 156 | listenerStats.listener = listener; |
| 157 | |
| 158 | // For each transaction |
| 159 | auto transactionStatsItr = transactionStatsDeque.begin(); |
| 160 | while (transactionStatsItr != transactionStatsDeque.end()) { |
| 161 | auto& transactionStats = *transactionStatsItr; |
| 162 | |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 163 | // If the transaction has been latched |
Vishnu Nair | fc46c1e | 2021-04-21 08:31:32 -0700 | [diff] [blame] | 164 | if (transactionStats.latchTime >= 0 && |
| 165 | !containsOnCommitCallbacks(transactionStats.callbackIds)) { |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 166 | transactionStats.presentFence = mPresentFence; |
| 167 | } |
| 168 | |
| 169 | // Remove the transaction from completed to the callback |
| 170 | listenerStats.transactionStats.push_back(std::move(transactionStats)); |
| 171 | transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr); |
| 172 | } |
| 173 | // If the listener has completed transactions |
| 174 | if (!listenerStats.transactionStats.empty()) { |
| 175 | // If the listener is still alive |
| 176 | if (listener->isBinderAlive()) { |
| 177 | // Send callback. The listener stored in listenerStats |
| 178 | // comes from the cross-process setTransactionState call to |
| 179 | // SF. This MUST be an ITransactionCompletedListener. We |
| 180 | // keep it as an IBinder due to consistency reasons: if we |
| 181 | // interface_cast at the IPC boundary when reading a Parcel, |
| 182 | // we get pointers that compare unequal in the SF process. |
| 183 | interface_cast<ITransactionCompletedListener>(listenerStats.listener) |
| 184 | ->onTransactionCompleted(listenerStats); |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 185 | } |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 186 | } |
Robert Carr | 3d1047b | 2021-09-20 18:22:32 -0700 | [diff] [blame^] | 187 | completedTransactionsItr++; |
Robert Carr | 9a803c3 | 2021-01-14 16:57:58 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if (mPresentFence) { |
| 191 | mPresentFence.clear(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // ----------------------------------------------------------------------- |
| 196 | |
| 197 | CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener, |
| 198 | const std::vector<CallbackId>& ids, const sp<IBinder>& sc) |
| 199 | : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {} |
| 200 | |
| 201 | } // namespace android |
| 202 | |
| 203 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 204 | #pragma clang diagnostic pop // ignored "-Wconversion" |