blob: 8fbf0b4d0783fa237eb1c8b88ec863533e667e25 [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
124status_t TransactionCallbackInvoker::findTransactionStats(
125 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;
146 status_t err = findTransactionStats(handle->listener, handle->callbackIds, &transactionStats);
147 if (err != NO_ERROR) {
148 return err;
149 }
150
151 transactionStats->latchTime = handle->latchTime;
152 // If the layer has already been destroyed, don't add the SurfaceControl to the callback.
153 // The client side keeps a sp<> to the SurfaceControl so if the SurfaceControl has been
154 // destroyed the client side is dead and there won't be anyone to send the callback to.
155 sp<IBinder> surfaceControl = handle->surfaceControl.promote();
156 if (surfaceControl) {
Sally Qi59a9f502021-10-12 18:53:23 +0000157 sp<Fence> prevFence = nullptr;
158
159 for (const auto& futureStruct : handle->previousReleaseFences) {
160 sp<Fence> currentFence = sp<Fence>::make(dup(futureStruct.get().drawFence));
161 if (prevFence == nullptr && currentFence->getStatus() != Fence::Status::Invalid) {
162 prevFence = currentFence;
163 handle->previousReleaseFence = prevFence;
164 } else if (prevFence != nullptr) {
165 // If both fences are signaled or both are unsignaled, we need to merge
166 // them to get an accurate timestamp.
167 if (prevFence->getStatus() != Fence::Status::Invalid &&
168 prevFence->getStatus() == currentFence->getStatus()) {
169 char fenceName[32] = {};
170 snprintf(fenceName, 32, "%.28s", handle->name.c_str());
171 sp<Fence> mergedFence = Fence::merge(fenceName, prevFence, currentFence);
172 if (mergedFence->isValid()) {
173 handle->previousReleaseFence = mergedFence;
174 prevFence = handle->previousReleaseFence;
175 }
176 } else if (currentFence->getStatus() == Fence::Status::Unsignaled) {
177 // If one fence has signaled and the other hasn't, the unsignaled
178 // fence will approximately correspond with the correct timestamp.
179 // There's a small race if both fences signal at about the same time
180 // and their statuses are retrieved with unfortunate timing. However,
181 // by this point, they will have both signaled and only the timestamp
182 // will be slightly off; any dependencies after this point will
183 // already have been met.
184 handle->previousReleaseFence = currentFence;
185 }
186 }
187 }
188 handle->previousReleaseFences = {};
Robert Carr9a803c32021-01-14 16:57:58 -0800189 FrameEventHistoryStats eventStats(handle->frameNumber,
190 handle->gpuCompositionDoneFence->getSnapshot().fence,
191 handle->compositorTiming, handle->refreshStartTime,
192 handle->dequeueReadyTime);
193 transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime,
194 handle->previousReleaseFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700195 handle->transformHint,
196 handle->currentMaxAcquiredBufferCount,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700197 eventStats, jankData,
198 handle->previousReleaseCallbackId);
Robert Carr9a803c32021-01-14 16:57:58 -0800199 }
200 return NO_ERROR;
201}
202
203void TransactionCallbackInvoker::addPresentFence(const sp<Fence>& presentFence) {
Robert Carr9a803c32021-01-14 16:57:58 -0800204 mPresentFence = presentFence;
205}
206
207void TransactionCallbackInvoker::sendCallbacks() {
Robert Carr9a803c32021-01-14 16:57:58 -0800208 // For each listener
209 auto completedTransactionsItr = mCompletedTransactions.begin();
210 while (completedTransactionsItr != mCompletedTransactions.end()) {
211 auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
212 ListenerStats listenerStats;
213 listenerStats.listener = listener;
214
215 // For each transaction
216 auto transactionStatsItr = transactionStatsDeque.begin();
217 while (transactionStatsItr != transactionStatsDeque.end()) {
218 auto& transactionStats = *transactionStatsItr;
219
Robert Carr9a803c32021-01-14 16:57:58 -0800220 // If the transaction has been latched
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700221 if (transactionStats.latchTime >= 0 &&
222 !containsOnCommitCallbacks(transactionStats.callbackIds)) {
Robert Carr9a803c32021-01-14 16:57:58 -0800223 transactionStats.presentFence = mPresentFence;
224 }
225
226 // Remove the transaction from completed to the callback
227 listenerStats.transactionStats.push_back(std::move(transactionStats));
228 transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
229 }
230 // If the listener has completed transactions
231 if (!listenerStats.transactionStats.empty()) {
232 // If the listener is still alive
233 if (listener->isBinderAlive()) {
234 // Send callback. The listener stored in listenerStats
235 // comes from the cross-process setTransactionState call to
236 // SF. This MUST be an ITransactionCompletedListener. We
237 // keep it as an IBinder due to consistency reasons: if we
238 // interface_cast at the IPC boundary when reading a Parcel,
239 // we get pointers that compare unequal in the SF process.
Robert Carr3ec2b852021-09-28 17:46:57 -0700240 {
241 std::unique_lock lock(mCallbackThreadMutex);
242 mCallbackThreadWork.push(
243 [stats = std::move(listenerStats)]() {
244 interface_cast<ITransactionCompletedListener>(stats.listener)
245 ->onTransactionCompleted(stats);
246 });
247 mCallbackConditionVariable.notify_all();
248 }
Robert Carr9a803c32021-01-14 16:57:58 -0800249 }
Robert Carr9a803c32021-01-14 16:57:58 -0800250 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700251 completedTransactionsItr++;
Robert Carr9a803c32021-01-14 16:57:58 -0800252 }
253
254 if (mPresentFence) {
255 mPresentFence.clear();
256 }
257}
258
259// -----------------------------------------------------------------------
260
261CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
262 const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
263 : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
264
265} // namespace android
266
267// TODO(b/129481165): remove the #pragma below and fix conversion issues
268#pragma clang diagnostic pop // ignored "-Wconversion"