1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//#define LOG_NDEBUG 0
#undef LOG_TAG
#define LOG_TAG "TransactionCompletedThread"
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#include "TransactionCompletedThread.h"
#include <cinttypes>
#include <binder/IInterface.h>
#include <gui/ITransactionCompletedListener.h>
#include <utils/RefBase.h>
namespace android {
TransactionCompletedThread::~TransactionCompletedThread() {
std::lock_guard lockThread(mThreadMutex);
{
std::lock_guard lock(mMutex);
mKeepRunning = false;
mConditionVariable.notify_all();
}
if (mThread.joinable()) {
mThread.join();
}
{
std::lock_guard lock(mMutex);
for (const auto& [listener, listenerStats] : mListenerStats) {
listener->unlinkToDeath(mDeathRecipient);
}
}
}
void TransactionCompletedThread::run() {
std::lock_guard lock(mMutex);
if (mRunning || !mKeepRunning) {
return;
}
mDeathRecipient = new ThreadDeathRecipient();
mRunning = true;
std::lock_guard lockThread(mThreadMutex);
mThread = std::thread(&TransactionCompletedThread::threadMain, this);
}
void TransactionCompletedThread::registerPendingLatchedCallbackHandle(
const sp<CallbackHandle>& handle) {
std::lock_guard lock(mMutex);
sp<IBinder> listener = IInterface::asBinder(handle->listener);
const auto& callbackIds = handle->callbackIds;
mPendingTransactions[listener][callbackIds]++;
}
void TransactionCompletedThread::addLatchedCallbackHandles(
const std::deque<sp<CallbackHandle>>& handles, nsecs_t latchTime,
const sp<Fence>& previousReleaseFence) {
std::lock_guard lock(mMutex);
// If the previous release fences have not signaled, something as probably gone wrong.
// Store the fences and check them again before sending a callback.
if (previousReleaseFence &&
previousReleaseFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
ALOGD("release fence from the previous frame has not signaled");
mPreviousReleaseFences.push_back(previousReleaseFence);
}
for (const auto& handle : handles) {
auto listener = mPendingTransactions.find(IInterface::asBinder(handle->listener));
auto& pendingCallbacks = listener->second;
auto pendingCallback = pendingCallbacks.find(handle->callbackIds);
if (pendingCallback != pendingCallbacks.end()) {
auto& pendingCount = pendingCallback->second;
// Decrease the pending count for this listener
if (--pendingCount == 0) {
pendingCallbacks.erase(pendingCallback);
}
} else {
ALOGE("there are more latched callbacks than there were registered callbacks");
}
addCallbackHandle(handle, latchTime);
}
}
void TransactionCompletedThread::addUnlatchedCallbackHandle(const sp<CallbackHandle>& handle) {
std::lock_guard lock(mMutex);
addCallbackHandle(handle);
}
void TransactionCompletedThread::addCallbackHandle(const sp<CallbackHandle>& handle,
nsecs_t latchTime) {
const sp<IBinder> listener = IInterface::asBinder(handle->listener);
// If we don't already have a reference to this listener, linkToDeath so we get a notification
// if it dies.
if (mListenerStats.count(listener) == 0) {
status_t error = listener->linkToDeath(mDeathRecipient);
if (error != NO_ERROR) {
ALOGE("cannot add callback handle because linkToDeath failed, err: %d", error);
return;
}
}
auto& listenerStats = mListenerStats[listener];
listenerStats.listener = handle->listener;
auto& transactionStats = listenerStats.transactionStats[handle->callbackIds];
transactionStats.latchTime = latchTime;
transactionStats.surfaceStats.emplace_back(handle->surfaceControl, handle->acquireTime,
handle->releasePreviousBuffer);
}
void TransactionCompletedThread::addPresentFence(const sp<Fence>& presentFence) {
std::lock_guard<std::mutex> lock(mMutex);
mPresentFence = presentFence;
}
void TransactionCompletedThread::sendCallbacks() {
std::lock_guard lock(mMutex);
if (mRunning) {
mConditionVariable.notify_all();
}
}
void TransactionCompletedThread::threadMain() {
std::lock_guard lock(mMutex);
while (mKeepRunning) {
mConditionVariable.wait(mMutex);
// Present fence should fire almost immediately. If the fence has not signaled in 100ms,
// there is a major problem and it will probably never fire.
nsecs_t presentTime = -1;
if (mPresentFence) {
status_t status = mPresentFence->wait(100);
if (status == NO_ERROR) {
presentTime = mPresentFence->getSignalTime();
} else {
ALOGE("present fence has not signaled, err %d", status);
}
}
// We should never hit this case. The release fences from the previous frame should have
// signaled long before the current frame is presented.
for (const auto& fence : mPreviousReleaseFences) {
status_t status = fence->wait(100);
if (status != NO_ERROR) {
ALOGE("previous release fence has not signaled, err %d", status);
}
}
// For each listener
auto it = mListenerStats.begin();
while (it != mListenerStats.end()) {
auto& [listener, listenerStats] = *it;
// For each transaction
bool sendCallback = true;
for (auto& [callbackIds, transactionStats] : listenerStats.transactionStats) {
// If we are still waiting on the callback handles for this transaction, skip it
if (mPendingTransactions[listener].count(callbackIds) != 0) {
sendCallback = false;
break;
}
// If the transaction has been latched
if (transactionStats.latchTime >= 0) {
// If the present time is < 0, this transaction has been latched but not
// presented. Skip it for now. This can happen when a new transaction comes
// in between the latch and present steps. sendCallbacks is called by
// SurfaceFlinger when the transaction is received to ensure that if the
// transaction that didn't update state it still got a callback.
if (presentTime < 0) {
sendCallback = false;
break;
}
transactionStats.presentTime = presentTime;
}
}
// If the listener has no pending transactions and all latched transactions have been
// presented
if (sendCallback) {
// If the listener is still alive
if (listener->isBinderAlive()) {
// Send callback
listenerStats.listener->onTransactionCompleted(listenerStats);
listener->unlinkToDeath(mDeathRecipient);
}
it = mListenerStats.erase(it);
} else {
it++;
}
}
if (mPresentFence) {
mPresentFence.clear();
mPreviousReleaseFences.clear();
}
}
}
// -----------------------------------------------------------------------
CallbackHandle::CallbackHandle(const sp<ITransactionCompletedListener>& transactionListener,
const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
: listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
} // namespace android
|