blob: c1eb8966d1b4b2a7548f5a63ca6c66527eadc6f2 [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) {
157 FrameEventHistoryStats eventStats(handle->frameNumber,
158 handle->gpuCompositionDoneFence->getSnapshot().fence,
159 handle->compositorTiming, handle->refreshStartTime,
160 handle->dequeueReadyTime);
161 transactionStats->surfaceStats.emplace_back(surfaceControl, handle->acquireTime,
162 handle->previousReleaseFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700163 handle->transformHint,
164 handle->currentMaxAcquiredBufferCount,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700165 eventStats, jankData,
166 handle->previousReleaseCallbackId);
Robert Carr9a803c32021-01-14 16:57:58 -0800167 }
168 return NO_ERROR;
169}
170
171void TransactionCallbackInvoker::addPresentFence(const sp<Fence>& presentFence) {
Robert Carr9a803c32021-01-14 16:57:58 -0800172 mPresentFence = presentFence;
173}
174
175void TransactionCallbackInvoker::sendCallbacks() {
Robert Carr9a803c32021-01-14 16:57:58 -0800176 // For each listener
177 auto completedTransactionsItr = mCompletedTransactions.begin();
178 while (completedTransactionsItr != mCompletedTransactions.end()) {
179 auto& [listener, transactionStatsDeque] = *completedTransactionsItr;
180 ListenerStats listenerStats;
181 listenerStats.listener = listener;
182
183 // For each transaction
184 auto transactionStatsItr = transactionStatsDeque.begin();
185 while (transactionStatsItr != transactionStatsDeque.end()) {
186 auto& transactionStats = *transactionStatsItr;
187
Robert Carr9a803c32021-01-14 16:57:58 -0800188 // If the transaction has been latched
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700189 if (transactionStats.latchTime >= 0 &&
190 !containsOnCommitCallbacks(transactionStats.callbackIds)) {
Robert Carr9a803c32021-01-14 16:57:58 -0800191 transactionStats.presentFence = mPresentFence;
192 }
193
194 // Remove the transaction from completed to the callback
195 listenerStats.transactionStats.push_back(std::move(transactionStats));
196 transactionStatsItr = transactionStatsDeque.erase(transactionStatsItr);
197 }
198 // If the listener has completed transactions
199 if (!listenerStats.transactionStats.empty()) {
200 // If the listener is still alive
201 if (listener->isBinderAlive()) {
202 // Send callback. The listener stored in listenerStats
203 // comes from the cross-process setTransactionState call to
204 // SF. This MUST be an ITransactionCompletedListener. We
205 // keep it as an IBinder due to consistency reasons: if we
206 // interface_cast at the IPC boundary when reading a Parcel,
207 // we get pointers that compare unequal in the SF process.
Robert Carr3ec2b852021-09-28 17:46:57 -0700208 {
209 std::unique_lock lock(mCallbackThreadMutex);
210 mCallbackThreadWork.push(
211 [stats = std::move(listenerStats)]() {
212 interface_cast<ITransactionCompletedListener>(stats.listener)
213 ->onTransactionCompleted(stats);
214 });
215 mCallbackConditionVariable.notify_all();
216 }
Robert Carr9a803c32021-01-14 16:57:58 -0800217 }
Robert Carr9a803c32021-01-14 16:57:58 -0800218 }
Robert Carr3d1047b2021-09-20 18:22:32 -0700219 completedTransactionsItr++;
Robert Carr9a803c32021-01-14 16:57:58 -0800220 }
221
222 if (mPresentFence) {
223 mPresentFence.clear();
224 }
225}
226
227// -----------------------------------------------------------------------
228
229CallbackHandle::CallbackHandle(const sp<IBinder>& transactionListener,
230 const std::vector<CallbackId>& ids, const sp<IBinder>& sc)
231 : listener(transactionListener), callbackIds(ids), surfaceControl(sc) {}
232
233} // namespace android
234
235// TODO(b/129481165): remove the #pragma below and fix conversion issues
236#pragma clang diagnostic pop // ignored "-Wconversion"