1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2018 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker // TODO(b/129481165): remove the #pragma below and fix conversion issues
18*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic push
19*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic ignored "-Wconversion"
20*38e8c45fSAndroid Build Coastguard Worker
21*38e8c45fSAndroid Build Coastguard Worker //#define LOG_NDEBUG 0
22*38e8c45fSAndroid Build Coastguard Worker #undef LOG_TAG
23*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "TransactionCallbackInvoker"
24*38e8c45fSAndroid Build Coastguard Worker #define ATRACE_TAG ATRACE_TAG_GRAPHICS
25*38e8c45fSAndroid Build Coastguard Worker
26*38e8c45fSAndroid Build Coastguard Worker #include "TransactionCallbackInvoker.h"
27*38e8c45fSAndroid Build Coastguard Worker #include "BackgroundExecutor.h"
28*38e8c45fSAndroid Build Coastguard Worker #include "Utils/FenceUtils.h"
29*38e8c45fSAndroid Build Coastguard Worker
30*38e8c45fSAndroid Build Coastguard Worker #include <binder/IInterface.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <common/trace.h>
32*38e8c45fSAndroid Build Coastguard Worker #include <utils/RefBase.h>
33*38e8c45fSAndroid Build Coastguard Worker
34*38e8c45fSAndroid Build Coastguard Worker namespace android {
35*38e8c45fSAndroid Build Coastguard Worker
36*38e8c45fSAndroid Build Coastguard Worker // Returns 0 if they are equal
37*38e8c45fSAndroid Build Coastguard Worker // <0 if the first id that doesn't match is lower in c2 or all ids match but c2 is shorter
38*38e8c45fSAndroid Build Coastguard Worker // >0 if the first id that doesn't match is greater in c2 or all ids match but c2 is longer
39*38e8c45fSAndroid Build Coastguard Worker //
40*38e8c45fSAndroid Build Coastguard Worker // See CallbackIdsHash for a explanation of why this works
compareCallbackIds(const std::vector<CallbackId> & c1,const std::vector<CallbackId> & c2)41*38e8c45fSAndroid Build Coastguard Worker static int compareCallbackIds(const std::vector<CallbackId>& c1,
42*38e8c45fSAndroid Build Coastguard Worker const std::vector<CallbackId>& c2) {
43*38e8c45fSAndroid Build Coastguard Worker if (c1.empty()) {
44*38e8c45fSAndroid Build Coastguard Worker return !c2.empty();
45*38e8c45fSAndroid Build Coastguard Worker }
46*38e8c45fSAndroid Build Coastguard Worker return c1.front().id - c2.front().id;
47*38e8c45fSAndroid Build Coastguard Worker }
48*38e8c45fSAndroid Build Coastguard Worker
containsOnCommitCallbacks(const std::vector<CallbackId> & callbacks)49*38e8c45fSAndroid Build Coastguard Worker static bool containsOnCommitCallbacks(const std::vector<CallbackId>& callbacks) {
50*38e8c45fSAndroid Build Coastguard Worker return !callbacks.empty() && callbacks.front().type == CallbackId::Type::ON_COMMIT;
51*38e8c45fSAndroid Build Coastguard Worker }
52*38e8c45fSAndroid Build Coastguard Worker
addEmptyTransaction(const ListenerCallbacks & listenerCallbacks)53*38e8c45fSAndroid Build Coastguard Worker void TransactionCallbackInvoker::addEmptyTransaction(const ListenerCallbacks& listenerCallbacks) {
54*38e8c45fSAndroid Build Coastguard Worker auto& [listener, callbackIds] = listenerCallbacks;
55*38e8c45fSAndroid Build Coastguard Worker auto& transactionStatsDeque = mCompletedTransactions[listener];
56*38e8c45fSAndroid Build Coastguard Worker transactionStatsDeque.emplace_back(callbackIds);
57*38e8c45fSAndroid Build Coastguard Worker }
58*38e8c45fSAndroid Build Coastguard Worker
addOnCommitCallbackHandles(const std::deque<sp<CallbackHandle>> & handles,std::deque<sp<CallbackHandle>> & outRemainingHandles)59*38e8c45fSAndroid Build Coastguard Worker status_t TransactionCallbackInvoker::addOnCommitCallbackHandles(
60*38e8c45fSAndroid Build Coastguard Worker const std::deque<sp<CallbackHandle>>& handles,
61*38e8c45fSAndroid Build Coastguard Worker std::deque<sp<CallbackHandle>>& outRemainingHandles) {
62*38e8c45fSAndroid Build Coastguard Worker if (handles.empty()) {
63*38e8c45fSAndroid Build Coastguard Worker return NO_ERROR;
64*38e8c45fSAndroid Build Coastguard Worker }
65*38e8c45fSAndroid Build Coastguard Worker for (const auto& handle : handles) {
66*38e8c45fSAndroid Build Coastguard Worker if (!containsOnCommitCallbacks(handle->callbackIds)) {
67*38e8c45fSAndroid Build Coastguard Worker outRemainingHandles.push_back(handle);
68*38e8c45fSAndroid Build Coastguard Worker continue;
69*38e8c45fSAndroid Build Coastguard Worker }
70*38e8c45fSAndroid Build Coastguard Worker status_t err = addCallbackHandle(handle);
71*38e8c45fSAndroid Build Coastguard Worker if (err != NO_ERROR) {
72*38e8c45fSAndroid Build Coastguard Worker return err;
73*38e8c45fSAndroid Build Coastguard Worker }
74*38e8c45fSAndroid Build Coastguard Worker }
75*38e8c45fSAndroid Build Coastguard Worker
76*38e8c45fSAndroid Build Coastguard Worker return NO_ERROR;
77*38e8c45fSAndroid Build Coastguard Worker }
78*38e8c45fSAndroid Build Coastguard Worker
addCallbackHandles(const std::deque<sp<CallbackHandle>> & handles)79*38e8c45fSAndroid Build Coastguard Worker status_t TransactionCallbackInvoker::addCallbackHandles(
80*38e8c45fSAndroid Build Coastguard Worker const std::deque<sp<CallbackHandle>>& handles) {
81*38e8c45fSAndroid Build Coastguard Worker if (handles.empty()) {
82*38e8c45fSAndroid Build Coastguard Worker return NO_ERROR;
83*38e8c45fSAndroid Build Coastguard Worker }
84*38e8c45fSAndroid Build Coastguard Worker for (const auto& handle : handles) {
85*38e8c45fSAndroid Build Coastguard Worker status_t err = addCallbackHandle(handle);
86*38e8c45fSAndroid Build Coastguard Worker if (err != NO_ERROR) {
87*38e8c45fSAndroid Build Coastguard Worker return err;
88*38e8c45fSAndroid Build Coastguard Worker }
89*38e8c45fSAndroid Build Coastguard Worker }
90*38e8c45fSAndroid Build Coastguard Worker
91*38e8c45fSAndroid Build Coastguard Worker return NO_ERROR;
92*38e8c45fSAndroid Build Coastguard Worker }
93*38e8c45fSAndroid Build Coastguard Worker
findOrCreateTransactionStats(const sp<IBinder> & listener,const std::vector<CallbackId> & callbackIds,TransactionStats ** outTransactionStats)94*38e8c45fSAndroid Build Coastguard Worker status_t TransactionCallbackInvoker::findOrCreateTransactionStats(
95*38e8c45fSAndroid Build Coastguard Worker const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds,
96*38e8c45fSAndroid Build Coastguard Worker TransactionStats** outTransactionStats) {
97*38e8c45fSAndroid Build Coastguard Worker auto& transactionStatsDeque = mCompletedTransactions[listener];
98*38e8c45fSAndroid Build Coastguard Worker
99*38e8c45fSAndroid Build Coastguard Worker // Search back to front because the most recent transactions are at the back of the deque
100*38e8c45fSAndroid Build Coastguard Worker auto itr = transactionStatsDeque.rbegin();
101*38e8c45fSAndroid Build Coastguard Worker for (; itr != transactionStatsDeque.rend(); itr++) {
102*38e8c45fSAndroid Build Coastguard Worker if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) {
103*38e8c45fSAndroid Build Coastguard Worker *outTransactionStats = &(*itr);
104*38e8c45fSAndroid Build Coastguard Worker return NO_ERROR;
105*38e8c45fSAndroid Build Coastguard Worker }
106*38e8c45fSAndroid Build Coastguard Worker }
107*38e8c45fSAndroid Build Coastguard Worker *outTransactionStats = &transactionStatsDeque.emplace_back(callbackIds);
108*38e8c45fSAndroid Build Coastguard Worker return NO_ERROR;
109*38e8c45fSAndroid Build Coastguard Worker }
110*38e8c45fSAndroid Build Coastguard Worker
addCallbackHandle(const sp<CallbackHandle> & handle)111*38e8c45fSAndroid Build Coastguard Worker status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle) {
112*38e8c45fSAndroid Build Coastguard Worker // If we can't find the transaction stats something has gone wrong. The client should call
113*38e8c45fSAndroid Build Coastguard Worker // startRegistration before trying to add a callback handle.
114*38e8c45fSAndroid Build Coastguard Worker TransactionStats* transactionStats;
115*38e8c45fSAndroid Build Coastguard Worker status_t err =
116*38e8c45fSAndroid Build Coastguard Worker findOrCreateTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
117*38e8c45fSAndroid Build Coastguard Worker if (err != NO_ERROR) {
118*38e8c45fSAndroid Build Coastguard Worker return err;
119*38e8c45fSAndroid Build Coastguard Worker }
120*38e8c45fSAndroid Build Coastguard Worker
121*38e8c45fSAndroid Build Coastguard Worker transactionStats->latchTime = handle->latchTime;
122*38e8c45fSAndroid Build Coastguard Worker // If the layer has already been destroyed, don't add the SurfaceControl to the callback.
123*38e8c45fSAndroid Build Coastguard Worker // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been
124*38e8c45fSAndroid Build Coastguard Worker // destroyed the client side is dead and there won't be anyone to send the callback to.
125*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> surfaceControl = handle->surfaceControl.promote();
126*38e8c45fSAndroid Build Coastguard Worker if (surfaceControl) {
127*38e8c45fSAndroid Build Coastguard Worker sp<Fence> prevFence = nullptr;
128*38e8c45fSAndroid Build Coastguard Worker
129*38e8c45fSAndroid Build Coastguard Worker for (auto& future : handle->previousReleaseFences) {
130*38e8c45fSAndroid Build Coastguard Worker mergeFence(handle->name.c_str(), future.get().value_or(Fence::NO_FENCE), prevFence);
131*38e8c45fSAndroid Build Coastguard Worker }
132*38e8c45fSAndroid Build Coastguard Worker
133*38e8c45fSAndroid Build Coastguard Worker handle->previousReleaseFence = prevFence;
134*38e8c45fSAndroid Build Coastguard Worker handle->previousReleaseFences.clear();
135*38e8c45fSAndroid Build Coastguard Worker
136*38e8c45fSAndroid Build Coastguard Worker FrameEventHistoryStats eventStats(handle->frameNumber, handle->previousFrameNumber,
137*38e8c45fSAndroid Build Coastguard Worker handle->gpuCompositionDoneFence->getSnapshot().fence,
138*38e8c45fSAndroid Build Coastguard Worker handle->compositorTiming, handle->refreshStartTime,
139*38e8c45fSAndroid Build Coastguard Worker handle->dequeueReadyTime);
140*38e8c45fSAndroid Build Coastguard Worker transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTimeOrFence,
141*38e8c45fSAndroid Build Coastguard Worker handle->previousReleaseFence,
142*38e8c45fSAndroid Build Coastguard Worker handle->transformHint,
143*38e8c45fSAndroid Build Coastguard Worker handle->currentMaxAcquiredBufferCount,
144*38e8c45fSAndroid Build Coastguard Worker eventStats, handle->previousReleaseCallbackId);
145*38e8c45fSAndroid Build Coastguard Worker if (handle->bufferReleaseChannel &&
146*38e8c45fSAndroid Build Coastguard Worker handle->previousReleaseCallbackId != ReleaseCallbackId::INVALID_ID) {
147*38e8c45fSAndroid Build Coastguard Worker mBufferReleases.emplace_back(handle->name, handle->bufferReleaseChannel,
148*38e8c45fSAndroid Build Coastguard Worker handle->previousReleaseCallbackId,
149*38e8c45fSAndroid Build Coastguard Worker handle->previousReleaseFence,
150*38e8c45fSAndroid Build Coastguard Worker handle->currentMaxAcquiredBufferCount);
151*38e8c45fSAndroid Build Coastguard Worker }
152*38e8c45fSAndroid Build Coastguard Worker }
153*38e8c45fSAndroid Build Coastguard Worker return NO_ERROR;
154*38e8c45fSAndroid Build Coastguard Worker }
155*38e8c45fSAndroid Build Coastguard Worker
addPresentFence(sp<Fence> presentFence)156*38e8c45fSAndroid Build Coastguard Worker void TransactionCallbackInvoker::addPresentFence(sp<Fence> presentFence) {
157*38e8c45fSAndroid Build Coastguard Worker mPresentFence = std::move(presentFence);
158*38e8c45fSAndroid Build Coastguard Worker }
159*38e8c45fSAndroid Build Coastguard Worker
sendCallbacks(bool onCommitOnly)160*38e8c45fSAndroid Build Coastguard Worker void TransactionCallbackInvoker::sendCallbacks(bool onCommitOnly) {
161*38e8c45fSAndroid Build Coastguard Worker for (const auto& bufferRelease : mBufferReleases) {
162*38e8c45fSAndroid Build Coastguard Worker status_t status = bufferRelease.channel
163*38e8c45fSAndroid Build Coastguard Worker ->writeReleaseFence(bufferRelease.callbackId, bufferRelease.fence,
164*38e8c45fSAndroid Build Coastguard Worker bufferRelease.currentMaxAcquiredBufferCount);
165*38e8c45fSAndroid Build Coastguard Worker if (status != OK) {
166*38e8c45fSAndroid Build Coastguard Worker ALOGE("[%s] writeReleaseFence failed. error %d (%s)", bufferRelease.layerName.c_str(),
167*38e8c45fSAndroid Build Coastguard Worker -status, strerror(-status));
168*38e8c45fSAndroid Build Coastguard Worker }
169*38e8c45fSAndroid Build Coastguard Worker }
170*38e8c45fSAndroid Build Coastguard Worker mBufferReleases.clear();
171*38e8c45fSAndroid Build Coastguard Worker
172*38e8c45fSAndroid Build Coastguard Worker // For each listener
173*38e8c45fSAndroid Build Coastguard Worker auto completedTransactionsItr = mCompletedTransactions.begin();
174*38e8c45fSAndroid Build Coastguard Worker ftl::SmallVector<ListenerStats, 10> listenerStatsToSend;
175*38e8c45fSAndroid Build Coastguard Worker while (completedTransactionsItr != mCompletedTransactions.end()) {
176*38e8c45fSAndroid Build Coastguard Worker auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
177*38e8c45fSAndroid Build Coastguard Worker ListenerStats listenerStats;
178*38e8c45fSAndroid Build Coastguard Worker listenerStats.listener = listener;
179*38e8c45fSAndroid Build Coastguard Worker
180*38e8c45fSAndroid Build Coastguard Worker // For each transaction
181*38e8c45fSAndroid Build Coastguard Worker auto transactionStatsItr = transactionStatsDeque.begin();
182*38e8c45fSAndroid Build Coastguard Worker while (transactionStatsItr != transactionStatsDeque.end()) {
183*38e8c45fSAndroid Build Coastguard Worker auto& transactionStats = *transactionStatsItr;
184*38e8c45fSAndroid Build Coastguard Worker if (onCommitOnly && !containsOnCommitCallbacks(transactionStats.callbackIds)) {
185*38e8c45fSAndroid Build Coastguard Worker transactionStatsItr++;
186*38e8c45fSAndroid Build Coastguard Worker continue;
187*38e8c45fSAndroid Build Coastguard Worker }
188*38e8c45fSAndroid Build Coastguard Worker
189*38e8c45fSAndroid Build Coastguard Worker // If the transaction has been latched
190*38e8c45fSAndroid Build Coastguard Worker if (transactionStats.latchTime >= 0 &&
191*38e8c45fSAndroid Build Coastguard Worker !containsOnCommitCallbacks(transactionStats.callbackIds)) {
192*38e8c45fSAndroid Build Coastguard Worker transactionStats.presentFence = mPresentFence;
193*38e8c45fSAndroid Build Coastguard Worker }
194*38e8c45fSAndroid Build Coastguard Worker
195*38e8c45fSAndroid Build Coastguard Worker // Remove the transaction from completed to the callback
196*38e8c45fSAndroid Build Coastguard Worker listenerStats.transactionStats.push_back(std::move(transactionStats));
197*38e8c45fSAndroid Build Coastguard Worker transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
198*38e8c45fSAndroid Build Coastguard Worker }
199*38e8c45fSAndroid Build Coastguard Worker // If the listener has completed transactions
200*38e8c45fSAndroid Build Coastguard Worker if (!listenerStats.transactionStats.empty()) {
201*38e8c45fSAndroid Build Coastguard Worker // If the listener is still alive
202*38e8c45fSAndroid Build Coastguard Worker if (listener->isBinderAlive()) {
203*38e8c45fSAndroid Build Coastguard Worker // Send callback. The listener stored in listenerStats
204*38e8c45fSAndroid Build Coastguard Worker // comes from the cross-process setTransactionState call to
205*38e8c45fSAndroid Build Coastguard Worker // SF. This MUST be an ITransactionCompletedListener. We
206*38e8c45fSAndroid Build Coastguard Worker // keep it as an IBinder due to consistency reasons: if we
207*38e8c45fSAndroid Build Coastguard Worker // interface_cast at the IPC boundary when reading a Parcel,
208*38e8c45fSAndroid Build Coastguard Worker // we get pointers that compare unequal in the SF process.
209*38e8c45fSAndroid Build Coastguard Worker listenerStatsToSend.emplace_back(std::move(listenerStats));
210*38e8c45fSAndroid Build Coastguard Worker }
211*38e8c45fSAndroid Build Coastguard Worker }
212*38e8c45fSAndroid Build Coastguard Worker completedTransactionsItr++;
213*38e8c45fSAndroid Build Coastguard Worker }
214*38e8c45fSAndroid Build Coastguard Worker
215*38e8c45fSAndroid Build Coastguard Worker if (mPresentFence) {
216*38e8c45fSAndroid Build Coastguard Worker mPresentFence.clear();
217*38e8c45fSAndroid Build Coastguard Worker }
218*38e8c45fSAndroid Build Coastguard Worker
219*38e8c45fSAndroid Build Coastguard Worker BackgroundExecutor::getInstance().sendCallbacks(
220*38e8c45fSAndroid Build Coastguard Worker {[listenerStatsToSend = std::move(listenerStatsToSend)]() {
221*38e8c45fSAndroid Build Coastguard Worker SFTRACE_NAME("TransactionCallbackInvoker::sendCallbacks");
222*38e8c45fSAndroid Build Coastguard Worker for (auto& stats : listenerStatsToSend) {
223*38e8c45fSAndroid Build Coastguard Worker interface_cast<ITransactionCompletedListener>(stats.listener)
224*38e8c45fSAndroid Build Coastguard Worker ->onTransactionCompleted(stats);
225*38e8c45fSAndroid Build Coastguard Worker }
226*38e8c45fSAndroid Build Coastguard Worker }});
227*38e8c45fSAndroid Build Coastguard Worker }
228*38e8c45fSAndroid Build Coastguard Worker
229*38e8c45fSAndroid Build Coastguard Worker // -----------------------------------------------------------------------
230*38e8c45fSAndroid Build Coastguard Worker
CallbackHandle(const sp<IBinder> & transactionListener,const std::vector<CallbackId> & ids,const sp<IBinder> & sc)231*38e8c45fSAndroid Build Coastguard Worker CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
232*38e8c45fSAndroid Build Coastguard Worker const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
233*38e8c45fSAndroid Build Coastguard Worker : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
234*38e8c45fSAndroid Build Coastguard Worker
235*38e8c45fSAndroid Build Coastguard Worker } // namespace android
236*38e8c45fSAndroid Build Coastguard Worker
237*38e8c45fSAndroid Build Coastguard Worker // TODO(b/129481165): remove the #pragma below and fix conversion issues
238*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic pop // ignored "-Wconversion"
239