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 #include "BackgroundExecutor.h"
28 #include "Utils/FenceUtils.h"
29
30 #include <cinttypes>
31
32 #include <binder/IInterface.h>
33 #include <common/FlagManager.h>
34 #include <utils/RefBase.h>
35
36 namespace android {
37
38 // Returns 0 if they are equal
39 // <0 if the first id that doesn't match is lower in c2 or all ids match but c2 is shorter
40 // >0 if the first id that doesn't match is greater in c2 or all ids match but c2 is longer
41 //
42 // See CallbackIdsHash for a explanation of why this works
compareCallbackIds(const std::vector<CallbackId> & c1,const std::vector<CallbackId> & c2)43 static int compareCallbackIds(const std::vector<CallbackId>& c1,
44 const std::vector<CallbackId>& c2) {
45 if (c1.empty()) {
46 return !c2.empty();
47 }
48 return c1.front().id - c2.front().id;
49 }
50
containsOnCommitCallbacks(const std::vector<CallbackId> & callbacks)51 static bool containsOnCommitCallbacks(const std::vector<CallbackId>& callbacks) {
52 return !callbacks.empty() && callbacks.front().type == CallbackId::Type::ON_COMMIT;
53 }
54
addEmptyTransaction(const ListenerCallbacks & listenerCallbacks)55 void TransactionCallbackInvoker::addEmptyTransaction(const ListenerCallbacks& listenerCallbacks) {
56 auto& [listener, callbackIds] = listenerCallbacks;
57 auto& transactionStatsDeque = mCompletedTransactions[listener];
58 transactionStatsDeque.emplace_back(callbackIds);
59 }
60
addOnCommitCallbackHandles(const std::deque<sp<CallbackHandle>> & handles,std::deque<sp<CallbackHandle>> & outRemainingHandles)61 status_t TransactionCallbackInvoker::addOnCommitCallbackHandles(
62 const std::deque<sp<CallbackHandle>>& handles,
63 std::deque<sp<CallbackHandle>>& outRemainingHandles) {
64 if (handles.empty()) {
65 return NO_ERROR;
66 }
67 const std::vector<JankData>& jankData = std::vector<JankData>();
68 for (const auto& handle : handles) {
69 if (!containsOnCommitCallbacks(handle->callbackIds)) {
70 outRemainingHandles.push_back(handle);
71 continue;
72 }
73 status_t err = addCallbackHandle(handle, jankData);
74 if (err != NO_ERROR) {
75 return err;
76 }
77 }
78
79 return NO_ERROR;
80 }
81
addCallbackHandles(const std::deque<sp<CallbackHandle>> & handles,const std::vector<JankData> & jankData)82 status_t TransactionCallbackInvoker::addCallbackHandles(
83 const std::deque<sp<CallbackHandle>>& handles, const std::vector<JankData>& jankData) {
84 if (handles.empty()) {
85 return NO_ERROR;
86 }
87 for (const auto& handle : handles) {
88 status_t err = addCallbackHandle(handle, jankData);
89 if (err != NO_ERROR) {
90 return err;
91 }
92 }
93
94 return NO_ERROR;
95 }
96
findOrCreateTransactionStats(const sp<IBinder> & listener,const std::vector<CallbackId> & callbackIds,TransactionStats ** outTransactionStats)97 status_t TransactionCallbackInvoker::findOrCreateTransactionStats(
98 const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds,
99 TransactionStats** outTransactionStats) {
100 auto& transactionStatsDeque = mCompletedTransactions[listener];
101
102 // Search back to front because the most recent transactions are at the back of the deque
103 auto itr = transactionStatsDeque.rbegin();
104 for (; itr != transactionStatsDeque.rend(); itr++) {
105 if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) {
106 *outTransactionStats = &(*itr);
107 return NO_ERROR;
108 }
109 }
110 *outTransactionStats = &transactionStatsDeque.emplace_back(callbackIds);
111 return NO_ERROR;
112 }
113
addCallbackHandle(const sp<CallbackHandle> & handle,const std::vector<JankData> & jankData)114 status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle,
115 const std::vector<JankData>& jankData) {
116 // If we can't find the transaction stats something has gone wrong. The client should call
117 // startRegistration before trying to add a callback handle.
118 TransactionStats* transactionStats;
119 status_t err =
120 findOrCreateTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
121 if (err != NO_ERROR) {
122 return err;
123 }
124
125 transactionStats->latchTime = handle->latchTime;
126 // If the layer has already been destroyed, don't add the SurfaceControl to the callback.
127 // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been
128 // destroyed the client side is dead and there won't be anyone to send the callback to.
129 sp<IBinder> surfaceControl = handle->surfaceControl.promote();
130 if (surfaceControl) {
131 sp<Fence> prevFence = nullptr;
132
133 if (FlagManager::getInstance().ce_fence_promise()) {
134 for (auto& future : handle->previousReleaseFences) {
135 mergeFence(handle->name.c_str(), future.get().value_or(Fence::NO_FENCE), prevFence);
136 }
137 } else {
138 for (const auto& future : handle->previousSharedReleaseFences) {
139 mergeFence(handle->name.c_str(), future.get().value_or(Fence::NO_FENCE), prevFence);
140 }
141 }
142
143 handle->previousReleaseFence = prevFence;
144 handle->previousReleaseFences.clear();
145
146 FrameEventHistoryStats eventStats(handle->frameNumber, handle->previousFrameNumber,
147 handle->gpuCompositionDoneFence->getSnapshot().fence,
148 handle->compositorTiming, handle->refreshStartTime,
149 handle->dequeueReadyTime);
150 transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTimeOrFence,
151 handle->previousReleaseFence,
152 handle->transformHint,
153 handle->currentMaxAcquiredBufferCount,
154 eventStats, jankData,
155 handle->previousReleaseCallbackId);
156 }
157 return NO_ERROR;
158 }
159
addPresentFence(sp<Fence> presentFence)160 void TransactionCallbackInvoker::addPresentFence(sp<Fence> presentFence) {
161 mPresentFence = std::move(presentFence);
162 }
163
sendCallbacks(bool onCommitOnly)164 void TransactionCallbackInvoker::sendCallbacks(bool onCommitOnly) {
165 // For each listener
166 auto completedTransactionsItr = mCompletedTransactions.begin();
167 BackgroundExecutor::Callbacks callbacks;
168 while (completedTransactionsItr != mCompletedTransactions.end()) {
169 auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
170 ListenerStats listenerStats;
171 listenerStats.listener = listener;
172
173 // For each transaction
174 auto transactionStatsItr = transactionStatsDeque.begin();
175 while (transactionStatsItr != transactionStatsDeque.end()) {
176 auto& transactionStats = *transactionStatsItr;
177 if (onCommitOnly && !containsOnCommitCallbacks(transactionStats.callbackIds)) {
178 transactionStatsItr++;
179 continue;
180 }
181
182 // If the transaction has been latched
183 if (transactionStats.latchTime >= 0 &&
184 !containsOnCommitCallbacks(transactionStats.callbackIds)) {
185 transactionStats.presentFence = mPresentFence;
186 }
187
188 // Remove the transaction from completed to the callback
189 listenerStats.transactionStats.push_back(std::move(transactionStats));
190 transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
191 }
192 // If the listener has completed transactions
193 if (!listenerStats.transactionStats.empty()) {
194 // If the listener is still alive
195 if (listener->isBinderAlive()) {
196 // Send callback. The listener stored in listenerStats
197 // comes from the cross-process setTransactionState call to
198 // SF. This MUST be an ITransactionCompletedListener. We
199 // keep it as an IBinder due to consistency reasons: if we
200 // interface_cast at the IPC boundary when reading a Parcel,
201 // we get pointers that compare unequal in the SF process.
202 callbacks.emplace_back([stats = std::move(listenerStats)]() {
203 interface_cast<ITransactionCompletedListener>(stats.listener)
204 ->onTransactionCompleted(stats);
205 });
206 }
207 }
208 completedTransactionsItr++;
209 }
210
211 if (mPresentFence) {
212 mPresentFence.clear();
213 }
214
215 BackgroundExecutor::getInstance().sendCallbacks(std::move(callbacks));
216 }
217
218 // -----------------------------------------------------------------------
219
CallbackHandle(const sp<IBinder> & transactionListener,const std::vector<CallbackId> & ids,const sp<IBinder> & sc)220 CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
221 const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
222 : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
223
224 } // namespace android
225
226 // TODO(b/129481165): remove the #pragma below and fix conversion issues
227 #pragma clang diagnostic pop // ignored "-Wconversion"
228