blob: 4b12a2640bf2ccae90352bc91f50ac9313e7e677 [file] [log] [blame]
Robert Carr9a803c32021-01-14 16:57:58 -08001/*
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
33namespace 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 Nairfc46c1e2021-04-21 08:31:32 -070039// See CallbackIdsHash for a explanation of why this works
Robert Carr9a803c32021-01-14 16:57:58 -080040static int compareCallbackIds(const std::vector<CallbackId>& c1,
41 const std::vector<CallbackId>& c2) {
42 if (c1.empty()) {
43 return !c2.empty();
44 }
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070045 return c1.front().id - c2.front().id;
46}
47
48static bool containsOnCommitCallbacks(const std::vector<CallbackId>& callbacks) {
49 return !callbacks.empty() && callbacks.front().type == CallbackId::Type::ON_COMMIT;
Robert Carr9a803c32021-01-14 16:57:58 -080050}
51
Robert Carr3d1047b2021-09-20 18:22:32 -070052void TransactionCallbackInvoker::addEmptyTransaction(const ListenerCallbacks& listenerCallbacks) {
Robert Carr9a803c32021-01-14 16:57:58 -080053 auto& [listener, callbackIds] = listenerCallbacks;
Robert Carr3d1047b2021-09-20 18:22:32 -070054 auto& transactionStatsDeque = mCompletedTransactions[listener];
55 transactionStatsDeque.emplace_back(callbackIds);
Robert Carr9a803c32021-01-14 16:57:58 -080056}
57
Robert Carr3d1047b2021-09-20 18:22:32 -070058status_t TransactionCallbackInvoker::addOnCommitCallbackHandles(
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070059 const std::deque<sp<CallbackHandle>>& handles,
60 std::deque<sp<CallbackHandle>>& outRemainingHandles) {
61 if (handles.empty()) {
62 return NO_ERROR;
63 }
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070064 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 Carr3d1047b2021-09-20 18:22:32 -070070 status_t err = addCallbackHandle(handle, jankData);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070071 if (err != NO_ERROR) {
72 return err;
73 }
74 }
75
76 return NO_ERROR;
77}
78
Robert Carr3d1047b2021-09-20 18:22:32 -070079status_t TransactionCallbackInvoker::addCallbackHandles(
Robert Carr9a803c32021-01-14 16:57:58 -080080 const std::deque<sp<CallbackHandle>>& handles, const std::vector<JankData>& jankData) {
81 if (handles.empty()) {
82 return NO_ERROR;
83 }
Robert Carr9a803c32021-01-14 16:57:58 -080084 for (const auto& handle : handles) {
Robert Carr3d1047b2021-09-20 18:22:32 -070085 status_t err = addCallbackHandle(handle, jankData);
Robert Carr9a803c32021-01-14 16:57:58 -080086 if (err != NO_ERROR) {
Robert Carr9a803c32021-01-14 16:57:58 -080087 return err;
88 }
89 }
90
91 return NO_ERROR;
92}
93
94status_t TransactionCallbackInvoker::registerUnpresentedCallbackHandle(
95 const sp<CallbackHandle>& handle) {
Robert Carr9a803c32021-01-14 16:57:58 -080096 return addCallbackHandle(handle, std::vector<JankData>());
97}
98
99status_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 Carr3d1047b2021-09-20 18:22:32 -0700112 *outTransactionStats = &transactionStatsDeque.emplace_back(callbackIds);
113 return NO_ERROR;
Robert Carr9a803c32021-01-14 16:57:58 -0800114}
115
116status_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 Abraham899dcdb2021-06-15 16:56:21 -0700138 handle->transformHint,
139 handle->currentMaxAcquiredBufferCount,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700140 eventStats, jankData,
141 handle->previousReleaseCallbackId);
Robert Carr9a803c32021-01-14 16:57:58 -0800142 }
143 return NO_ERROR;
144}
145
146void TransactionCallbackInvoker::addPresentFence(const sp<Fence>& presentFence) {
Robert Carr9a803c32021-01-14 16:57:58 -0800147 mPresentFence = presentFence;
148}
149
150void TransactionCallbackInvoker::sendCallbacks() {
Robert Carr9a803c32021-01-14 16:57:58 -0800151 // 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 Carr9a803c32021-01-14 16:57:58 -0800163 // If the transaction has been latched
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700164 if (transactionStats.latchTime >= 0 &&
165 !containsOnCommitCallbacks(transactionStats.callbackIds)) {
Robert Carr9a803c32021-01-14 16:57:58 -0800166 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 Carr9a803c32021-01-14 16:57:58 -0800185 }
Robert Carr9a803c32021-01-14 16:57:58 -0800186 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700187 completedTransactionsItr++;
Robert Carr9a803c32021-01-14 16:57:58 -0800188 }
189
190 if (mPresentFence) {
191 mPresentFence.clear();
192 }
193}
194
195// -----------------------------------------------------------------------
196
197CallbackHandle::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"