blob: 0cdff8f380c52e1a40722199b1569e2d4bba6303 [file] [log] [blame]
Marissa Walle2ffb422018-10-12 11:33:52 -07001/*
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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Marissa Walle2ffb422018-10-12 11:33:52 -070021//#define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "TransactionCompletedThread"
24#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
26#include "TransactionCompletedThread.h"
27
28#include <cinttypes>
29
30#include <binder/IInterface.h>
Marissa Walle2ffb422018-10-12 11:33:52 -070031#include <utils/RefBase.h>
32
33namespace android {
34
Marissa Walld600d572019-03-26 15:38:50 -070035// 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//
39// See CallbackIdsHash for a explaniation of why this works
Greg Kaisera9e843a2019-04-01 06:23:09 -070040static int compareCallbackIds(const std::vector<CallbackId>& c1,
41 const std::vector<CallbackId>& c2) {
Marissa Walld600d572019-03-26 15:38:50 -070042 if (c1.empty()) {
43 return !c2.empty();
44 }
45 return c1.front() - c2.front();
46}
47
Marissa Walle2ffb422018-10-12 11:33:52 -070048TransactionCompletedThread::~TransactionCompletedThread() {
Marissa Wall99343ba2018-11-13 10:39:08 -080049 std::lock_guard lockThread(mThreadMutex);
50
Marissa Walle2ffb422018-10-12 11:33:52 -070051 {
52 std::lock_guard lock(mMutex);
53 mKeepRunning = false;
54 mConditionVariable.notify_all();
55 }
56
Marissa Wall05d9dd32018-11-13 10:05:14 -080057 if (mThread.joinable()) {
58 mThread.join();
59 }
Marissa Walle2ffb422018-10-12 11:33:52 -070060
61 {
62 std::lock_guard lock(mMutex);
Marissa Walld600d572019-03-26 15:38:50 -070063 for (const auto& [listener, transactionStats] : mCompletedTransactions) {
Valerie Hau5de3ad22019-08-20 07:47:43 -070064 listener->unlinkToDeath(mDeathRecipient);
Marissa Walle2ffb422018-10-12 11:33:52 -070065 }
66 }
67}
68
69void TransactionCompletedThread::run() {
70 std::lock_guard lock(mMutex);
Marissa Wall99343ba2018-11-13 10:39:08 -080071 if (mRunning || !mKeepRunning) {
Marissa Walle2ffb422018-10-12 11:33:52 -070072 return;
73 }
74 mDeathRecipient = new ThreadDeathRecipient();
75 mRunning = true;
Marissa Wall99343ba2018-11-13 10:39:08 -080076
77 std::lock_guard lockThread(mThreadMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -070078 mThread = std::thread(&TransactionCompletedThread::threadMain, this);
79}
80
Marissa Wallefb71af2019-06-27 14:45:53 -070081status_t TransactionCompletedThread::startRegistration(const ListenerCallbacks& listenerCallbacks) {
Valerie Hau9dab9732019-08-20 09:29:25 -070082 // begin running if not already running
83 run();
Marissa Walle2ffb422018-10-12 11:33:52 -070084 std::lock_guard lock(mMutex);
Marissa Walld600d572019-03-26 15:38:50 -070085 if (!mRunning) {
86 ALOGE("cannot add callback because the callback thread isn't running");
87 return BAD_VALUE;
Marissa Wall3dad52d2019-03-22 14:03:19 -070088 }
89
Valerie Hau9dab9732019-08-20 09:29:25 -070090 auto [itr, inserted] = mRegisteringTransactions.insert(listenerCallbacks);
Marissa Wallefb71af2019-06-27 14:45:53 -070091 auto& [listener, callbackIds] = listenerCallbacks;
92
Valerie Hau9dab9732019-08-20 09:29:25 -070093 if (inserted) {
94 if (mCompletedTransactions.count(listener) == 0) {
95 status_t err = listener->linkToDeath(mDeathRecipient);
96 if (err != NO_ERROR) {
97 ALOGE("cannot add callback because linkToDeath failed, err: %d", err);
98 return err;
99 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700100 }
Valerie Hau9dab9732019-08-20 09:29:25 -0700101 auto& transactionStatsDeque = mCompletedTransactions[listener];
102 transactionStatsDeque.emplace_back(callbackIds);
Marissa Walle2ffb422018-10-12 11:33:52 -0700103 }
104
Marissa Walld600d572019-03-26 15:38:50 -0700105 return NO_ERROR;
106}
107
Marissa Wallefb71af2019-06-27 14:45:53 -0700108status_t TransactionCompletedThread::endRegistration(const ListenerCallbacks& listenerCallbacks) {
109 std::lock_guard lock(mMutex);
110 if (!mRunning) {
111 ALOGE("cannot add callback because the callback thread isn't running");
112 return BAD_VALUE;
113 }
114
115 auto itr = mRegisteringTransactions.find(listenerCallbacks);
116 if (itr == mRegisteringTransactions.end()) {
117 ALOGE("cannot end a registration that does not exist");
118 return BAD_VALUE;
119 }
120
121 mRegisteringTransactions.erase(itr);
122
123 return NO_ERROR;
124}
125
126bool TransactionCompletedThread::isRegisteringTransaction(
Valerie Hau5de3ad22019-08-20 07:47:43 -0700127 const sp<IBinder>& transactionListener, const std::vector<CallbackId>& callbackIds) {
Marissa Wallefb71af2019-06-27 14:45:53 -0700128 ListenerCallbacks listenerCallbacks(transactionListener, callbackIds);
129
130 auto itr = mRegisteringTransactions.find(listenerCallbacks);
131 return itr != mRegisteringTransactions.end();
132}
133
Marissa Walld600d572019-03-26 15:38:50 -0700134status_t TransactionCompletedThread::registerPendingCallbackHandle(
135 const sp<CallbackHandle>& handle) {
136 std::lock_guard lock(mMutex);
137 if (!mRunning) {
138 ALOGE("cannot register callback handle because the callback thread isn't running");
139 return BAD_VALUE;
140 }
141
142 // If we can't find the transaction stats something has gone wrong. The client should call
Marissa Wallefb71af2019-06-27 14:45:53 -0700143 // startRegistration before trying to register a pending callback handle.
Marissa Walld600d572019-03-26 15:38:50 -0700144 TransactionStats* transactionStats;
145 status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
146 if (err != NO_ERROR) {
147 ALOGE("cannot find transaction stats");
148 return err;
149 }
150
151 mPendingTransactions[handle->listener][handle->callbackIds]++;
152 return NO_ERROR;
153}
154
Marissa Wallefb71af2019-06-27 14:45:53 -0700155status_t TransactionCompletedThread::finalizePendingCallbackHandles(
Marissa Walld600d572019-03-26 15:38:50 -0700156 const std::deque<sp<CallbackHandle>>& handles) {
157 std::lock_guard lock(mMutex);
158 if (!mRunning) {
159 ALOGE("cannot add presented callback handle because the callback thread isn't running");
160 return BAD_VALUE;
161 }
162
163 for (const auto& handle : handles) {
164 auto listener = mPendingTransactions.find(handle->listener);
165 if (listener != mPendingTransactions.end()) {
166 auto& pendingCallbacks = listener->second;
167 auto pendingCallback = pendingCallbacks.find(handle->callbackIds);
168
169 if (pendingCallback != pendingCallbacks.end()) {
170 auto& pendingCount = pendingCallback->second;
171
172 // Decrease the pending count for this listener
173 if (--pendingCount == 0) {
174 pendingCallbacks.erase(pendingCallback);
175 }
176 } else {
177 ALOGW("there are more latched callbacks than there were registered callbacks");
178 }
Marissa Wallb0022cc2019-04-16 14:19:55 -0700179 if (listener->second.size() == 0) {
180 mPendingTransactions.erase(listener);
181 }
Marissa Walld600d572019-03-26 15:38:50 -0700182 } else {
183 ALOGW("cannot find listener in mPendingTransactions");
184 }
185
186 status_t err = addCallbackHandle(handle);
187 if (err != NO_ERROR) {
188 ALOGE("could not add callback handle");
189 return err;
190 }
191 }
192
193 return NO_ERROR;
194}
195
Marissa Wallefb71af2019-06-27 14:45:53 -0700196status_t TransactionCompletedThread::registerUnpresentedCallbackHandle(
Marissa Walld600d572019-03-26 15:38:50 -0700197 const sp<CallbackHandle>& handle) {
198 std::lock_guard lock(mMutex);
199 if (!mRunning) {
200 ALOGE("cannot add unpresented callback handle because the callback thread isn't running");
201 return BAD_VALUE;
202 }
203
204 return addCallbackHandle(handle);
205}
206
207status_t TransactionCompletedThread::findTransactionStats(
Valerie Hau5de3ad22019-08-20 07:47:43 -0700208 const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds,
209 TransactionStats** outTransactionStats) {
Marissa Walld600d572019-03-26 15:38:50 -0700210 auto& transactionStatsDeque = mCompletedTransactions[listener];
211
212 // Search back to front because the most recent transactions are at the back of the deque
213 auto itr = transactionStatsDeque.rbegin();
214 for (; itr != transactionStatsDeque.rend(); itr++) {
215 if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) {
216 *outTransactionStats = &(*itr);
217 return NO_ERROR;
218 }
219 }
220
221 ALOGE("could not find transaction stats");
222 return BAD_VALUE;
223}
224
225status_t TransactionCompletedThread::addCallbackHandle(const sp<CallbackHandle>& handle) {
226 // If we can't find the transaction stats something has gone wrong. The client should call
Marissa Wallefb71af2019-06-27 14:45:53 -0700227 // startRegistration before trying to add a callback handle.
Marissa Walld600d572019-03-26 15:38:50 -0700228 TransactionStats* transactionStats;
229 status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
230 if (err != NO_ERROR) {
231 return err;
232 }
233
234 transactionStats->latchTime = handle->latchTime;
Marissa Wall0e24a832019-07-10 15:32:50 -0700235 // If the layer has already been destroyed, don't add the SurfaceControl to the callback.
236 // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been
237 // destroyed the client side is dead and there won't be anyone to send the callback to.
238 sp<IBinder> surfaceControl = handle->surfaceControl.promote();
239 if (surfaceControl) {
Valerie Hau871d6352020-01-29 08:44:02 -0800240 FrameEventHistoryStats eventStats(handle->frameNumber,
241 handle->gpuCompositionDoneFence->getSnapshot().fence,
242 handle->compositorTiming, handle->refreshStartTime,
243 handle->dequeueReadyTime);
Marissa Wall0e24a832019-07-10 15:32:50 -0700244 transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime,
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700245 handle->previousReleaseFence,
Valerie Hau871d6352020-01-29 08:44:02 -0800246 handle->transformHint, eventStats);
Marissa Wall0e24a832019-07-10 15:32:50 -0700247 }
Marissa Wall3dad52d2019-03-22 14:03:19 -0700248 return NO_ERROR;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700249}
250
251void TransactionCompletedThread::addPresentFence(const sp<Fence>& presentFence) {
252 std::lock_guard<std::mutex> lock(mMutex);
253 mPresentFence = presentFence;
Marissa Walle2ffb422018-10-12 11:33:52 -0700254}
255
256void TransactionCompletedThread::sendCallbacks() {
257 std::lock_guard lock(mMutex);
258 if (mRunning) {
259 mConditionVariable.notify_all();
260 }
261}
262
263void TransactionCompletedThread::threadMain() {
264 std::lock_guard lock(mMutex);
265
266 while (mKeepRunning) {
267 mConditionVariable.wait(mMutex);
Marissa Wallcaa83f52019-05-29 13:03:25 -0700268 std::vector<ListenerStats> completedListenerStats;
Marissa Walle2ffb422018-10-12 11:33:52 -0700269
270 // For each listener
Marissa Walld600d572019-03-26 15:38:50 -0700271 auto completedTransactionsItr = mCompletedTransactions.begin();
272 while (completedTransactionsItr != mCompletedTransactions.end()) {
273 auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
274 ListenerStats listenerStats;
275 listenerStats.listener = listener;
Marissa Walle2ffb422018-10-12 11:33:52 -0700276
277 // For each transaction
Marissa Walld600d572019-03-26 15:38:50 -0700278 auto transactionStatsItr = transactionStatsDeque.begin();
279 while (transactionStatsItr != transactionStatsDeque.end()) {
280 auto& transactionStats = *transactionStatsItr;
281
Marissa Wallefb71af2019-06-27 14:45:53 -0700282 // If this transaction is still registering, it is not safe to send a callback
283 // because there could be surface controls that haven't been added to
284 // transaction stats or mPendingTransactions.
285 if (isRegisteringTransaction(listener, transactionStats.callbackIds)) {
286 break;
287 }
288
Marissa Walld600d572019-03-26 15:38:50 -0700289 // If we are still waiting on the callback handles for this transaction, stop
290 // here because all transaction callbacks for the same listener must come in order
Marissa Wall6110e842019-04-12 13:29:59 -0700291 auto pendingTransactions = mPendingTransactions.find(listener);
292 if (pendingTransactions != mPendingTransactions.end() &&
293 pendingTransactions->second.count(transactionStats.callbackIds) != 0) {
Marissa Walle2ffb422018-10-12 11:33:52 -0700294 break;
295 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700296
297 // If the transaction has been latched
298 if (transactionStats.latchTime >= 0) {
Valerie Hau63258a12018-12-14 14:31:48 -0800299 if (!mPresentFence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700300 break;
301 }
Valerie Hau63258a12018-12-14 14:31:48 -0800302 transactionStats.presentFence = mPresentFence;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700303 }
Marissa Walld600d572019-03-26 15:38:50 -0700304
305 // Remove the transaction from completed to the callback
306 listenerStats.transactionStats.push_back(std::move(transactionStats));
307 transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
Marissa Walle2ffb422018-10-12 11:33:52 -0700308 }
Marissa Walld600d572019-03-26 15:38:50 -0700309 // If the listener has completed transactions
310 if (!listenerStats.transactionStats.empty()) {
Marissa Walle2ffb422018-10-12 11:33:52 -0700311 // If the listener is still alive
Valerie Hau5de3ad22019-08-20 07:47:43 -0700312 if (listener->isBinderAlive()) {
313 // Send callback. The listener stored in listenerStats
314 // comes from the cross-process setTransactionState call to
315 // SF. This MUST be an ITransactionCompletedListener. We
316 // keep it as an IBinder due to consistency reasons: if we
317 // interface_cast at the IPC boundary when reading a Parcel,
318 // we get pointers that compare unequal in the SF process.
319 interface_cast<ITransactionCompletedListener>(listenerStats.listener)
320 ->onTransactionCompleted(listenerStats);
Valerie Haudfca3ec2019-12-17 08:50:33 -0800321 if (transactionStatsDeque.empty()) {
Valerie Hau783ec682020-01-10 08:41:15 -0800322 listener->unlinkToDeath(mDeathRecipient);
Valerie Haudfca3ec2019-12-17 08:50:33 -0800323 completedTransactionsItr =
324 mCompletedTransactions.erase(completedTransactionsItr);
325 } else {
326 completedTransactionsItr++;
327 }
328 } else {
329 completedTransactionsItr =
330 mCompletedTransactions.erase(completedTransactionsItr);
Marissa Walle2ffb422018-10-12 11:33:52 -0700331 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700332 } else {
Marissa Walld600d572019-03-26 15:38:50 -0700333 completedTransactionsItr++;
Marissa Walle2ffb422018-10-12 11:33:52 -0700334 }
Marissa Wallcaa83f52019-05-29 13:03:25 -0700335
336 completedListenerStats.push_back(std::move(listenerStats));
Marissa Walle2ffb422018-10-12 11:33:52 -0700337 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700338
339 if (mPresentFence) {
340 mPresentFence.clear();
Marissa Wallfda30bb2018-10-12 11:34:28 -0700341 }
Marissa Wallcaa83f52019-05-29 13:03:25 -0700342
343 // If everyone else has dropped their reference to a layer and its listener is dead,
344 // we are about to cause the layer to be deleted. If this happens at the wrong time and
345 // we are holding mMutex, we will cause a deadlock.
346 //
347 // The deadlock happens because this thread is holding on to mMutex and when we delete
348 // the layer, it grabs SF's mStateLock. A different SF binder thread grabs mStateLock,
349 // then call's TransactionCompletedThread::run() which tries to grab mMutex.
350 //
351 // To avoid this deadlock, we need to unlock mMutex when dropping our last reference to
352 // to the layer.
353 mMutex.unlock();
354 completedListenerStats.clear();
355 mMutex.lock();
Marissa Walle2ffb422018-10-12 11:33:52 -0700356 }
357}
358
359// -----------------------------------------------------------------------
360
Valerie Hau5de3ad22019-08-20 07:47:43 -0700361CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
Marissa Walle2ffb422018-10-12 11:33:52 -0700362 const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
363 : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
364
365} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800366
367// TODO(b/129481165): remove the #pragma below and fix conversion issues
368#pragma clang diagnostic pop // ignored "-Wconversion"