blob: f3d46ea061ec024bdca1df7cdf0689463a158054 [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 Carr3ec2b852021-09-28 17:46:57 -070052TransactionCallbackInvoker::TransactionCallbackInvoker() {
53 mThread = std::thread([&]() {
54 std::unique_lock lock(mCallbackThreadMutex);
55
56 while (mKeepRunning) {
57 while (mCallbackThreadWork.size() > 0) {
58 mCallbackThreadWork.front()();
59 mCallbackThreadWork.pop();
60 }
61 mCallbackConditionVariable.wait(lock);
62 }
63 });
64}
65
66TransactionCallbackInvoker::~TransactionCallbackInvoker() {
67 {
68 std::unique_lock lock(mCallbackThreadMutex);
69 mKeepRunning = false;
70 mCallbackConditionVariable.notify_all();
71 }
72 if (mThread.joinable()) {
73 mThread.join();
74 }
75}
76
Robert Carr3d1047b2021-09-20 18:22:32 -070077void TransactionCallbackInvoker::addEmptyTransaction(const ListenerCallbacks& listenerCallbacks) {
Robert Carr9a803c32021-01-14 16:57:58 -080078 auto& [listener, callbackIds] = listenerCallbacks;
Robert Carr3d1047b2021-09-20 18:22:32 -070079 auto& transactionStatsDeque = mCompletedTransactions[listener];
80 transactionStatsDeque.emplace_back(callbackIds);
Robert Carr9a803c32021-01-14 16:57:58 -080081}
82
Robert Carr3d1047b2021-09-20 18:22:32 -070083status_t TransactionCallbackInvoker::addOnCommitCallbackHandles(
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070084 const std::deque<sp<CallbackHandle>>& handles,
85 std::deque<sp<CallbackHandle>>& outRemainingHandles) {
86 if (handles.empty()) {
87 return NO_ERROR;
88 }
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070089 const std::vector<JankData>& jankData = std::vector<JankData>();
90 for (const auto& handle : handles) {
91 if (!containsOnCommitCallbacks(handle->callbackIds)) {
92 outRemainingHandles.push_back(handle);
93 continue;
94 }
Robert Carr3d1047b2021-09-20 18:22:32 -070095 status_t err = addCallbackHandle(handle, jankData);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -070096 if (err != NO_ERROR) {
97 return err;
98 }
99 }
100
101 return NO_ERROR;
102}
103
Robert Carr3d1047b2021-09-20 18:22:32 -0700104status_t TransactionCallbackInvoker::addCallbackHandles(
Robert Carr9a803c32021-01-14 16:57:58 -0800105 const std::deque<sp<CallbackHandle>>& handles, const std::vector<JankData>& jankData) {
106 if (handles.empty()) {
107 return NO_ERROR;
108 }
Robert Carr9a803c32021-01-14 16:57:58 -0800109 for (const auto& handle : handles) {
Robert Carr3d1047b2021-09-20 18:22:32 -0700110 status_t err = addCallbackHandle(handle, jankData);
Robert Carr9a803c32021-01-14 16:57:58 -0800111 if (err != NO_ERROR) {
Robert Carr9a803c32021-01-14 16:57:58 -0800112 return err;
113 }
114 }
115
116 return NO_ERROR;
117}
118
119status_t TransactionCallbackInvoker::registerUnpresentedCallbackHandle(
120 const sp<CallbackHandle>& handle) {
Robert Carr9a803c32021-01-14 16:57:58 -0800121 return addCallbackHandle(handle, std::vector<JankData>());
122}
123
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700124status_t TransactionCallbackInvoker::findOrCreateTransactionStats(
Robert Carr9a803c32021-01-14 16:57:58 -0800125 const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds,
126 TransactionStats** outTransactionStats) {
127 auto& transactionStatsDeque = mCompletedTransactions[listener];
128
129 // Search back to front because the most recent transactions are at the back of the deque
130 auto itr = transactionStatsDeque.rbegin();
131 for (; itr != transactionStatsDeque.rend(); itr++) {
132 if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) {
133 *outTransactionStats = &(*itr);
134 return NO_ERROR;
135 }
136 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700137 *outTransactionStats = &transactionStatsDeque.emplace_back(callbackIds);
138 return NO_ERROR;
Robert Carr9a803c32021-01-14 16:57:58 -0800139}
140
141status_t TransactionCallbackInvoker::addCallbackHandle(const sp<CallbackHandle>& handle,
142 const std::vector<JankData>& jankData) {
143 // If we can't find the transaction stats something has gone wrong. The client should call
144 // startRegistration before trying to add a callback handle.
145 TransactionStats* transactionStats;
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700146 status_t err =
147 findOrCreateTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
Robert Carr9a803c32021-01-14 16:57:58 -0800148 if (err != NO_ERROR) {
149 return err;
150 }
151
152 transactionStats->latchTime = handle->latchTime;
153 // If the layer has already been destroyed, don't add the SurfaceControl to the callback.
154 // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been
155 // destroyed the client side is dead and there won't be anyone to send the callback to.
156 sp<IBinder> surfaceControl = handle->surfaceControl.promote();
157 if (surfaceControl) {
Sally Qi59a9f502021-10-12 18:53:23 +0000158 sp<Fence> prevFence = nullptr;
159
160 for (const auto& futureStruct : handle->previousReleaseFences) {
161 sp<Fence> currentFence = sp<Fence>::make(dup(futureStruct.get().drawFence));
162 if (prevFence == nullptr && currentFence->getStatus() != Fence::Status::Invalid) {
163 prevFence = currentFence;
164 handle->previousReleaseFence = prevFence;
165 } else if (prevFence != nullptr) {
166 // If both fences are signaled or both are unsignaled, we need to merge
167 // them to get an accurate timestamp.
168 if (prevFence->getStatus() != Fence::Status::Invalid &&
169 prevFence->getStatus() == currentFence->getStatus()) {
170 char fenceName[32] = {};
171 snprintf(fenceName, 32, "%.28s", handle->name.c_str());
172 sp<Fence> mergedFence = Fence::merge(fenceName, prevFence, currentFence);
173 if (mergedFence->isValid()) {
174 handle->previousReleaseFence = mergedFence;
175 prevFence = handle->previousReleaseFence;
176 }
177 } else if (currentFence->getStatus() == Fence::Status::Unsignaled) {
178 // If one fence has signaled and the other hasn't, the unsignaled
179 // fence will approximately correspond with the correct timestamp.
180 // There's a small race if both fences signal at about the same time
181 // and their statuses are retrieved with unfortunate timing. However,
182 // by this point, they will have both signaled and only the timestamp
183 // will be slightly off; any dependencies after this point will
184 // already have been met.
185 handle->previousReleaseFence = currentFence;
186 }
187 }
188 }
189 handle->previousReleaseFences = {};
Robert Carr9a803c32021-01-14 16:57:58 -0800190 FrameEventHistoryStats eventStats(handle->frameNumber,
191 handle->gpuCompositionDoneFence->getSnapshot().fence,
192 handle->compositorTiming, handle->refreshStartTime,
193 handle->dequeueReadyTime);
194 transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime,
195 handle->previousReleaseFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700196 handle->transformHint,
197 handle->currentMaxAcquiredBufferCount,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700198 eventStats, jankData,
199 handle->previousReleaseCallbackId);
Robert Carr9a803c32021-01-14 16:57:58 -0800200 }
201 return NO_ERROR;
202}
203
204void TransactionCallbackInvoker::addPresentFence(const sp<Fence>& presentFence) {
Robert Carr9a803c32021-01-14 16:57:58 -0800205 mPresentFence = presentFence;
206}
207
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700208void TransactionCallbackInvoker::sendCallbacks(bool onCommitOnly) {
Robert Carr9a803c32021-01-14 16:57:58 -0800209 // For each listener
210 auto completedTransactionsItr = mCompletedTransactions.begin();
211 while (completedTransactionsItr != mCompletedTransactions.end()) {
212 auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
213 ListenerStats listenerStats;
214 listenerStats.listener = listener;
215
216 // For each transaction
217 auto transactionStatsItr = transactionStatsDeque.begin();
218 while (transactionStatsItr != transactionStatsDeque.end()) {
219 auto& transactionStats = *transactionStatsItr;
Vishnu Naircd52e2d2021-10-18 08:42:46 -0700220 if (onCommitOnly && !containsOnCommitCallbacks(transactionStats.callbackIds)) {
221 transactionStatsItr++;
222 continue;
223 }
Robert Carr9a803c32021-01-14 16:57:58 -0800224
Robert Carr9a803c32021-01-14 16:57:58 -0800225 // If the transaction has been latched
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700226 if (transactionStats.latchTime >= 0 &&
227 !containsOnCommitCallbacks(transactionStats.callbackIds)) {
Robert Carr9a803c32021-01-14 16:57:58 -0800228 transactionStats.presentFence = mPresentFence;
229 }
230
231 // Remove the transaction from completed to the callback
232 listenerStats.transactionStats.push_back(std::move(transactionStats));
233 transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
234 }
235 // If the listener has completed transactions
236 if (!listenerStats.transactionStats.empty()) {
237 // If the listener is still alive
238 if (listener->isBinderAlive()) {
239 // Send callback. The listener stored in listenerStats
240 // comes from the cross-process setTransactionState call to
241 // SF. This MUST be an ITransactionCompletedListener. We
242 // keep it as an IBinder due to consistency reasons: if we
243 // interface_cast at the IPC boundary when reading a Parcel,
244 // we get pointers that compare unequal in the SF process.
Robert Carr3ec2b852021-09-28 17:46:57 -0700245 {
246 std::unique_lock lock(mCallbackThreadMutex);
247 mCallbackThreadWork.push(
248 [stats = std::move(listenerStats)]() {
249 interface_cast<ITransactionCompletedListener>(stats.listener)
250 ->onTransactionCompleted(stats);
251 });
252 mCallbackConditionVariable.notify_all();
253 }
Robert Carr9a803c32021-01-14 16:57:58 -0800254 }
Robert Carr9a803c32021-01-14 16:57:58 -0800255 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700256 completedTransactionsItr++;
Robert Carr9a803c32021-01-14 16:57:58 -0800257 }
258
259 if (mPresentFence) {
260 mPresentFence.clear();
261 }
262}
263
264// -----------------------------------------------------------------------
265
266CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
267 const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
268 : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
269
270} // namespace android
271
272// TODO(b/129481165): remove the #pragma below and fix conversion issues
273#pragma clang diagnostic pop // ignored "-Wconversion"