blob: 1e0719159e1d350cd227c5b706f390f827cd34d2 [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
17//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "TransactionCompletedThread"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
22#include "TransactionCompletedThread.h"
23
24#include <cinttypes>
25
26#include <binder/IInterface.h>
Marissa Walle2ffb422018-10-12 11:33:52 -070027#include <utils/RefBase.h>
28
29namespace android {
30
Marissa Walld600d572019-03-26 15:38:50 -070031// Returns 0 if they are equal
32// <0 if the first id that doesn't match is lower in c2 or all ids match but c2 is shorter
33// >0 if the first id that doesn't match is greater in c2 or all ids match but c2 is longer
34//
35// See CallbackIdsHash for a explaniation of why this works
Greg Kaisera9e843a2019-04-01 06:23:09 -070036static int compareCallbackIds(const std::vector<CallbackId>& c1,
37 const std::vector<CallbackId>& c2) {
Marissa Walld600d572019-03-26 15:38:50 -070038 if (c1.empty()) {
39 return !c2.empty();
40 }
41 return c1.front() - c2.front();
42}
43
Marissa Walle2ffb422018-10-12 11:33:52 -070044TransactionCompletedThread::~TransactionCompletedThread() {
Marissa Wall99343ba2018-11-13 10:39:08 -080045 std::lock_guard lockThread(mThreadMutex);
46
Marissa Walle2ffb422018-10-12 11:33:52 -070047 {
48 std::lock_guard lock(mMutex);
49 mKeepRunning = false;
50 mConditionVariable.notify_all();
51 }
52
Marissa Wall05d9dd32018-11-13 10:05:14 -080053 if (mThread.joinable()) {
54 mThread.join();
55 }
Marissa Walle2ffb422018-10-12 11:33:52 -070056
57 {
58 std::lock_guard lock(mMutex);
Marissa Walld600d572019-03-26 15:38:50 -070059 for (const auto& [listener, transactionStats] : mCompletedTransactions) {
Valerie Hau5de3ad22019-08-20 07:47:43 -070060 listener->unlinkToDeath(mDeathRecipient);
Marissa Walle2ffb422018-10-12 11:33:52 -070061 }
62 }
63}
64
65void TransactionCompletedThread::run() {
66 std::lock_guard lock(mMutex);
Marissa Wall99343ba2018-11-13 10:39:08 -080067 if (mRunning || !mKeepRunning) {
Marissa Walle2ffb422018-10-12 11:33:52 -070068 return;
69 }
70 mDeathRecipient = new ThreadDeathRecipient();
71 mRunning = true;
Marissa Wall99343ba2018-11-13 10:39:08 -080072
73 std::lock_guard lockThread(mThreadMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -070074 mThread = std::thread(&TransactionCompletedThread::threadMain, this);
75}
76
Marissa Wallefb71af2019-06-27 14:45:53 -070077status_t TransactionCompletedThread::startRegistration(const ListenerCallbacks& listenerCallbacks) {
Valerie Hau9dab9732019-08-20 09:29:25 -070078 // begin running if not already running
79 run();
Marissa Walle2ffb422018-10-12 11:33:52 -070080 std::lock_guard lock(mMutex);
Marissa Walld600d572019-03-26 15:38:50 -070081 if (!mRunning) {
82 ALOGE("cannot add callback because the callback thread isn't running");
83 return BAD_VALUE;
Marissa Wall3dad52d2019-03-22 14:03:19 -070084 }
85
Valerie Hau9dab9732019-08-20 09:29:25 -070086 auto [itr, inserted] = mRegisteringTransactions.insert(listenerCallbacks);
Marissa Wallefb71af2019-06-27 14:45:53 -070087 auto& [listener, callbackIds] = listenerCallbacks;
88
Valerie Hau9dab9732019-08-20 09:29:25 -070089 if (inserted) {
90 if (mCompletedTransactions.count(listener) == 0) {
91 status_t err = listener->linkToDeath(mDeathRecipient);
92 if (err != NO_ERROR) {
93 ALOGE("cannot add callback because linkToDeath failed, err: %d", err);
94 return err;
95 }
Marissa Walle2ffb422018-10-12 11:33:52 -070096 }
Valerie Hau9dab9732019-08-20 09:29:25 -070097 auto& transactionStatsDeque = mCompletedTransactions[listener];
98 transactionStatsDeque.emplace_back(callbackIds);
Marissa Walle2ffb422018-10-12 11:33:52 -070099 }
100
Marissa Walld600d572019-03-26 15:38:50 -0700101 return NO_ERROR;
102}
103
Marissa Wallefb71af2019-06-27 14:45:53 -0700104status_t TransactionCompletedThread::endRegistration(const ListenerCallbacks& listenerCallbacks) {
105 std::lock_guard lock(mMutex);
106 if (!mRunning) {
107 ALOGE("cannot add callback because the callback thread isn't running");
108 return BAD_VALUE;
109 }
110
111 auto itr = mRegisteringTransactions.find(listenerCallbacks);
112 if (itr == mRegisteringTransactions.end()) {
113 ALOGE("cannot end a registration that does not exist");
114 return BAD_VALUE;
115 }
116
117 mRegisteringTransactions.erase(itr);
118
119 return NO_ERROR;
120}
121
122bool TransactionCompletedThread::isRegisteringTransaction(
Valerie Hau5de3ad22019-08-20 07:47:43 -0700123 const sp<IBinder>& transactionListener, const std::vector<CallbackId>& callbackIds) {
Marissa Wallefb71af2019-06-27 14:45:53 -0700124 ListenerCallbacks listenerCallbacks(transactionListener, callbackIds);
125
126 auto itr = mRegisteringTransactions.find(listenerCallbacks);
127 return itr != mRegisteringTransactions.end();
128}
129
Marissa Walld600d572019-03-26 15:38:50 -0700130status_t TransactionCompletedThread::registerPendingCallbackHandle(
131 const sp<CallbackHandle>& handle) {
132 std::lock_guard lock(mMutex);
133 if (!mRunning) {
134 ALOGE("cannot register callback handle because the callback thread isn't running");
135 return BAD_VALUE;
136 }
137
138 // If we can't find the transaction stats something has gone wrong. The client should call
Marissa Wallefb71af2019-06-27 14:45:53 -0700139 // startRegistration before trying to register a pending callback handle.
Marissa Walld600d572019-03-26 15:38:50 -0700140 TransactionStats* transactionStats;
141 status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
142 if (err != NO_ERROR) {
143 ALOGE("cannot find transaction stats");
144 return err;
145 }
146
147 mPendingTransactions[handle->listener][handle->callbackIds]++;
148 return NO_ERROR;
149}
150
Marissa Wallefb71af2019-06-27 14:45:53 -0700151status_t TransactionCompletedThread::finalizePendingCallbackHandles(
Marissa Walld600d572019-03-26 15:38:50 -0700152 const std::deque<sp<CallbackHandle>>& handles) {
153 std::lock_guard lock(mMutex);
154 if (!mRunning) {
155 ALOGE("cannot add presented callback handle because the callback thread isn't running");
156 return BAD_VALUE;
157 }
158
159 for (const auto& handle : handles) {
160 auto listener = mPendingTransactions.find(handle->listener);
161 if (listener != mPendingTransactions.end()) {
162 auto& pendingCallbacks = listener->second;
163 auto pendingCallback = pendingCallbacks.find(handle->callbackIds);
164
165 if (pendingCallback != pendingCallbacks.end()) {
166 auto& pendingCount = pendingCallback->second;
167
168 // Decrease the pending count for this listener
169 if (--pendingCount == 0) {
170 pendingCallbacks.erase(pendingCallback);
171 }
172 } else {
173 ALOGW("there are more latched callbacks than there were registered callbacks");
174 }
Marissa Wallb0022cc2019-04-16 14:19:55 -0700175 if (listener->second.size() == 0) {
176 mPendingTransactions.erase(listener);
177 }
Marissa Walld600d572019-03-26 15:38:50 -0700178 } else {
179 ALOGW("cannot find listener in mPendingTransactions");
180 }
181
182 status_t err = addCallbackHandle(handle);
183 if (err != NO_ERROR) {
184 ALOGE("could not add callback handle");
185 return err;
186 }
187 }
188
189 return NO_ERROR;
190}
191
Valerie Hauac12ae72019-11-19 14:13:16 -0800192void TransactionCompletedThread::clearAllPending() {
193 std::lock_guard lock(mMutex);
194 if (!mRunning) {
195 return;
196 }
197 mPendingTransactions.clear();
198 mConditionVariable.notify_all();
199}
200
Marissa Wallefb71af2019-06-27 14:45:53 -0700201status_t TransactionCompletedThread::registerUnpresentedCallbackHandle(
Marissa Walld600d572019-03-26 15:38:50 -0700202 const sp<CallbackHandle>& handle) {
203 std::lock_guard lock(mMutex);
204 if (!mRunning) {
205 ALOGE("cannot add unpresented callback handle because the callback thread isn't running");
206 return BAD_VALUE;
207 }
208
209 return addCallbackHandle(handle);
210}
211
212status_t TransactionCompletedThread::findTransactionStats(
Valerie Hau5de3ad22019-08-20 07:47:43 -0700213 const sp<IBinder>& listener, const std::vector<CallbackId>& callbackIds,
214 TransactionStats** outTransactionStats) {
Marissa Walld600d572019-03-26 15:38:50 -0700215 auto& transactionStatsDeque = mCompletedTransactions[listener];
216
217 // Search back to front because the most recent transactions are at the back of the deque
218 auto itr = transactionStatsDeque.rbegin();
219 for (; itr != transactionStatsDeque.rend(); itr++) {
220 if (compareCallbackIds(itr->callbackIds, callbackIds) == 0) {
221 *outTransactionStats = &(*itr);
222 return NO_ERROR;
223 }
224 }
225
226 ALOGE("could not find transaction stats");
227 return BAD_VALUE;
228}
229
230status_t TransactionCompletedThread::addCallbackHandle(const sp<CallbackHandle>& handle) {
231 // If we can't find the transaction stats something has gone wrong. The client should call
Marissa Wallefb71af2019-06-27 14:45:53 -0700232 // startRegistration before trying to add a callback handle.
Marissa Walld600d572019-03-26 15:38:50 -0700233 TransactionStats* transactionStats;
234 status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
235 if (err != NO_ERROR) {
236 return err;
237 }
238
239 transactionStats->latchTime = handle->latchTime;
Marissa Wall0e24a832019-07-10 15:32:50 -0700240 // If the layer has already been destroyed, don't add the SurfaceControl to the callback.
241 // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been
242 // destroyed the client side is dead and there won't be anyone to send the callback to.
243 sp<IBinder> surfaceControl = handle->surfaceControl.promote();
244 if (surfaceControl) {
245 transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime,
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700246 handle->previousReleaseFence,
247 handle->transformHint);
Marissa Wall0e24a832019-07-10 15:32:50 -0700248 }
Marissa Wall3dad52d2019-03-22 14:03:19 -0700249 return NO_ERROR;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700250}
251
252void TransactionCompletedThread::addPresentFence(const sp<Fence>& presentFence) {
253 std::lock_guard<std::mutex> lock(mMutex);
254 mPresentFence = presentFence;
Marissa Walle2ffb422018-10-12 11:33:52 -0700255}
256
257void TransactionCompletedThread::sendCallbacks() {
258 std::lock_guard lock(mMutex);
259 if (mRunning) {
260 mConditionVariable.notify_all();
261 }
262}
263
264void TransactionCompletedThread::threadMain() {
265 std::lock_guard lock(mMutex);
266
267 while (mKeepRunning) {
268 mConditionVariable.wait(mMutex);
Marissa Wallcaa83f52019-05-29 13:03:25 -0700269 std::vector<ListenerStats> completedListenerStats;
Marissa Walle2ffb422018-10-12 11:33:52 -0700270
271 // For each listener
Marissa Walld600d572019-03-26 15:38:50 -0700272 auto completedTransactionsItr = mCompletedTransactions.begin();
273 while (completedTransactionsItr != mCompletedTransactions.end()) {
274 auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
275 ListenerStats listenerStats;
276 listenerStats.listener = listener;
Marissa Walle2ffb422018-10-12 11:33:52 -0700277
278 // For each transaction
Marissa Walld600d572019-03-26 15:38:50 -0700279 auto transactionStatsItr = transactionStatsDeque.begin();
280 while (transactionStatsItr != transactionStatsDeque.end()) {
281 auto& transactionStats = *transactionStatsItr;
282
Marissa Wallefb71af2019-06-27 14:45:53 -0700283 // If this transaction is still registering, it is not safe to send a callback
284 // because there could be surface controls that haven't been added to
285 // transaction stats or mPendingTransactions.
286 if (isRegisteringTransaction(listener, transactionStats.callbackIds)) {
287 break;
288 }
289
Marissa Walld600d572019-03-26 15:38:50 -0700290 // If we are still waiting on the callback handles for this transaction, stop
291 // here because all transaction callbacks for the same listener must come in order
Marissa Wall6110e842019-04-12 13:29:59 -0700292 auto pendingTransactions = mPendingTransactions.find(listener);
293 if (pendingTransactions != mPendingTransactions.end() &&
294 pendingTransactions->second.count(transactionStats.callbackIds) != 0) {
Marissa Walle2ffb422018-10-12 11:33:52 -0700295 break;
296 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700297
298 // If the transaction has been latched
299 if (transactionStats.latchTime >= 0) {
Valerie Hau63258a12018-12-14 14:31:48 -0800300 if (!mPresentFence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700301 break;
302 }
Valerie Hau63258a12018-12-14 14:31:48 -0800303 transactionStats.presentFence = mPresentFence;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700304 }
Marissa Walld600d572019-03-26 15:38:50 -0700305
306 // Remove the transaction from completed to the callback
307 listenerStats.transactionStats.push_back(std::move(transactionStats));
308 transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
Marissa Walle2ffb422018-10-12 11:33:52 -0700309 }
Marissa Walld600d572019-03-26 15:38:50 -0700310 // If the listener has completed transactions
311 if (!listenerStats.transactionStats.empty()) {
Marissa Walle2ffb422018-10-12 11:33:52 -0700312 // If the listener is still alive
Valerie Hau5de3ad22019-08-20 07:47:43 -0700313 if (listener->isBinderAlive()) {
314 // Send callback. The listener stored in listenerStats
315 // comes from the cross-process setTransactionState call to
316 // SF. This MUST be an ITransactionCompletedListener. We
317 // keep it as an IBinder due to consistency reasons: if we
318 // interface_cast at the IPC boundary when reading a Parcel,
319 // we get pointers that compare unequal in the SF process.
320 interface_cast<ITransactionCompletedListener>(listenerStats.listener)
321 ->onTransactionCompleted(listenerStats);
322 listener->unlinkToDeath(mDeathRecipient);
Valerie Haudfca3ec2019-12-17 08:50:33 -0800323 if (transactionStatsDeque.empty()) {
324 completedTransactionsItr =
325 mCompletedTransactions.erase(completedTransactionsItr);
326 } else {
327 completedTransactionsItr++;
328 }
329 } else {
330 completedTransactionsItr =
331 mCompletedTransactions.erase(completedTransactionsItr);
Marissa Walle2ffb422018-10-12 11:33:52 -0700332 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700333 } else {
Marissa Walld600d572019-03-26 15:38:50 -0700334 completedTransactionsItr++;
Marissa Walle2ffb422018-10-12 11:33:52 -0700335 }
Marissa Wallcaa83f52019-05-29 13:03:25 -0700336
337 completedListenerStats.push_back(std::move(listenerStats));
Marissa Walle2ffb422018-10-12 11:33:52 -0700338 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700339
340 if (mPresentFence) {
341 mPresentFence.clear();
Marissa Wallfda30bb2018-10-12 11:34:28 -0700342 }
Marissa Wallcaa83f52019-05-29 13:03:25 -0700343
344 // If everyone else has dropped their reference to a layer and its listener is dead,
345 // we are about to cause the layer to be deleted. If this happens at the wrong time and
346 // we are holding mMutex, we will cause a deadlock.
347 //
348 // The deadlock happens because this thread is holding on to mMutex and when we delete
349 // the layer, it grabs SF's mStateLock. A different SF binder thread grabs mStateLock,
350 // then call's TransactionCompletedThread::run() which tries to grab mMutex.
351 //
352 // To avoid this deadlock, we need to unlock mMutex when dropping our last reference to
353 // to the layer.
354 mMutex.unlock();
355 completedListenerStats.clear();
356 mMutex.lock();
Marissa Walle2ffb422018-10-12 11:33:52 -0700357 }
358}
359
360// -----------------------------------------------------------------------
361
Valerie Hau5de3ad22019-08-20 07:47:43 -0700362CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
Marissa Walle2ffb422018-10-12 11:33:52 -0700363 const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
364 : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
365
366} // namespace android