blob: a51bbb1553acb21060749db58c80366c62c4d7c2 [file] [log] [blame]
Robert Carr78c25dd2019-08-15 14:10:33 -07001/*
2 * Copyright (C) 2019 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
Valerie Haud3b90d22019-11-06 09:37:31 -080017#undef LOG_TAG
18#define LOG_TAG "BLASTBufferQueue"
19
Valerie Haua32c5522019-12-09 10:11:08 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Vishnu Naire1a42322020-10-02 17:42:04 -070021//#define LOG_NDEBUG 0
Valerie Haua32c5522019-12-09 10:11:08 -080022
Robert Carr78c25dd2019-08-15 14:10:33 -070023#include <gui/BLASTBufferQueue.h>
24#include <gui/BufferItemConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080025#include <gui/BufferQueueConsumer.h>
26#include <gui/BufferQueueCore.h>
27#include <gui/BufferQueueProducer.h>
Valerie Hau45e4b3b2019-12-03 10:49:17 -080028#include <gui/GLConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080029#include <gui/IProducerListener.h>
Robert Carr05086b22020-10-13 18:22:51 -070030#include <gui/Surface.h>
chaviw57ae4b22022-02-03 16:51:39 -060031#include <gui/TraceUtils.h>
Vishnu Nair89496122020-12-14 17:14:53 -080032#include <utils/Singleton.h>
Valerie Haua32c5522019-12-09 10:11:08 -080033#include <utils/Trace.h>
34
Ady Abraham0bde6b52021-05-18 13:57:02 -070035#include <private/gui/ComposerService.h>
36
Robert Carr78c25dd2019-08-15 14:10:33 -070037#include <chrono>
38
39using namespace std::chrono_literals;
40
Vishnu Nairdab94092020-09-29 16:09:04 -070041namespace {
chaviw3277faf2021-05-19 16:45:23 -050042inline const char* boolToString(bool b) {
Vishnu Nairdab94092020-09-29 16:09:04 -070043 return b ? "true" : "false";
44}
45} // namespace
46
Robert Carr78c25dd2019-08-15 14:10:33 -070047namespace android {
48
Vishnu Nairdab94092020-09-29 16:09:04 -070049// Macros to include adapter info in log messages
chaviwd7deef72021-10-06 11:53:40 -050050#define BQA_LOGD(x, ...) \
51 ALOGD("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070052#define BQA_LOGV(x, ...) \
53 ALOGV("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairc6f89ee2020-12-11 14:27:32 -080054// enable logs for a single layer
55//#define BQA_LOGV(x, ...) \
56// ALOGV_IF((strstr(mName.c_str(), "SurfaceView") != nullptr), "[%s](f:%u,a:%u) " x, \
57// mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070058#define BQA_LOGE(x, ...) \
59 ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
60
chaviw57ae4b22022-02-03 16:51:39 -060061#define BBQ_TRACE(x, ...) \
62 ATRACE_FORMAT("%s - %s(f:%u,a:%u)" x, __FUNCTION__, mName.c_str(), mNumFrameAvailable, \
63 mNumAcquired, ##__VA_ARGS__)
64
Valerie Hau871d6352020-01-29 08:44:02 -080065void BLASTBufferItemConsumer::onDisconnect() {
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000066 Mutex::Autolock lock(mMutex);
67 mPreviouslyConnected = mCurrentlyConnected;
68 mCurrentlyConnected = false;
69 if (mPreviouslyConnected) {
70 mDisconnectEvents.push(mCurrentFrameNumber);
Valerie Hau871d6352020-01-29 08:44:02 -080071 }
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000072 mFrameEventHistory.onDisconnect();
Valerie Hau871d6352020-01-29 08:44:02 -080073}
74
75void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
76 FrameEventHistoryDelta* outDelta) {
Hongguang Chen621ec582021-02-16 15:42:35 -080077 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080078 if (newTimestamps) {
79 // BufferQueueProducer only adds a new timestamp on
80 // queueBuffer
81 mCurrentFrameNumber = newTimestamps->frameNumber;
82 mFrameEventHistory.addQueue(*newTimestamps);
83 }
84 if (outDelta) {
85 // frame event histories will be processed
86 // only after the producer connects and requests
87 // deltas for the first time. Forward this intent
88 // to SF-side to turn event processing back on
89 mPreviouslyConnected = mCurrentlyConnected;
90 mCurrentlyConnected = true;
91 mFrameEventHistory.getAndResetDelta(outDelta);
92 }
93}
94
95void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
96 const sp<Fence>& glDoneFence,
97 const sp<Fence>& presentFence,
98 const sp<Fence>& prevReleaseFence,
99 CompositorTiming compositorTiming,
100 nsecs_t latchTime, nsecs_t dequeueReadyTime) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800101 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800102
103 // if the producer is not connected, don't bother updating,
104 // the next producer that connects won't access this frame event
105 if (!mCurrentlyConnected) return;
106 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
107 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
108 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
109
110 mFrameEventHistory.addLatch(frameNumber, latchTime);
111 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
112 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
113 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
114 compositorTiming);
115}
116
117void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
118 bool disconnect = false;
Hongguang Chen621ec582021-02-16 15:42:35 -0800119 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800120 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
121 disconnect = true;
122 mDisconnectEvents.pop();
123 }
124 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
125}
126
Hongguang Chen621ec582021-02-16 15:42:35 -0800127void BLASTBufferItemConsumer::onSidebandStreamChanged() {
Ady Abrahamdbca1352021-12-15 11:58:56 -0800128 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
129 if (bbq != nullptr) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800130 sp<NativeHandle> stream = getSidebandStream();
Ady Abrahamdbca1352021-12-15 11:58:56 -0800131 bbq->setSidebandStream(stream);
Hongguang Chen621ec582021-02-16 15:42:35 -0800132 }
133}
134
Vishnu Naird2aaab12022-02-10 14:49:09 -0800135BLASTBufferQueue::BLASTBufferQueue(const std::string& name, bool updateDestinationFrame)
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800136 : mSurfaceControl(nullptr),
137 mSize(1, 1),
Vishnu Nairea0de002020-11-17 17:42:37 -0800138 mRequestedSize(mSize),
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800139 mFormat(PIXEL_FORMAT_RGBA_8888),
Tianhao Yao4861b102022-02-03 20:18:35 +0000140 mTransactionReadyCallback(nullptr),
Vishnu Naird2aaab12022-02-10 14:49:09 -0800141 mSyncTransaction(nullptr),
142 mUpdateDestinationFrame(updateDestinationFrame) {
Vishnu Nair89496122020-12-14 17:14:53 -0800143 createBufferQueue(&mProducer, &mConsumer);
Valerie Hau0889c622020-02-19 15:04:47 -0800144 // since the adapter is in the client process, set dequeue timeout
145 // explicitly so that dequeueBuffer will block
146 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
Valerie Hau65b8e872020-02-13 09:45:14 -0800147
Vishnu Nairdebd1cb2021-03-16 10:06:01 -0700148 // safe default, most producers are expected to override this
149 mProducer->setMaxDequeuedBufferCount(2);
Vishnu Nair1618c672021-02-05 13:08:26 -0800150 mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
151 GraphicBuffer::USAGE_HW_COMPOSER |
152 GraphicBuffer::USAGE_HW_TEXTURE,
Ady Abrahamdbca1352021-12-15 11:58:56 -0800153 1, false, this);
Valerie Haua32c5522019-12-09 10:11:08 -0800154 static int32_t id = 0;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700155 mName = name + "#" + std::to_string(id);
Vishnu Nairdab94092020-09-29 16:09:04 -0700156 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(id);
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700157 mQueuedBufferTrace = "QueuedBuffer - " + mName + "BLAST#" + std::to_string(id);
Valerie Haua32c5522019-12-09 10:11:08 -0800158 id++;
Vishnu Nairdab94092020-09-29 16:09:04 -0700159 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700160 mBufferItemConsumer->setFrameAvailableListener(this);
161 mBufferItemConsumer->setBufferFreedListener(this);
Robert Carr9f133d72020-04-01 15:51:46 -0700162
Ady Abraham899dcdb2021-06-15 16:56:21 -0700163 ComposerService::getComposerService()->getMaxAcquiredBufferCount(&mMaxAcquiredBuffers);
Ady Abraham0bde6b52021-05-18 13:57:02 -0700164 mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxAcquiredBuffers);
chaviw69058fb2021-09-27 09:37:30 -0500165 mCurrentMaxAcquiredBufferCount = mMaxAcquiredBuffers;
Valerie Haua32c5522019-12-09 10:11:08 -0800166 mNumAcquired = 0;
167 mNumFrameAvailable = 0;
Robert Carr4c1b6462021-12-21 10:30:50 -0800168
169 TransactionCompletedListener::getInstance()->addQueueStallListener(
170 [&]() {
171 std::function<void(bool)> callbackCopy;
172 {
173 std::unique_lock _lock{mMutex};
174 callbackCopy = mTransactionHangCallback;
175 }
176 if (callbackCopy) callbackCopy(true);
177 }, this);
178
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800179 BQA_LOGV("BLASTBufferQueue created");
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800180}
181
182BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface,
183 int width, int height, int32_t format)
184 : BLASTBufferQueue(name) {
185 update(surface, width, height, format);
Robert Carr78c25dd2019-08-15 14:10:33 -0700186}
187
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800188BLASTBufferQueue::~BLASTBufferQueue() {
Robert Carr4c1b6462021-12-21 10:30:50 -0800189 TransactionCompletedListener::getInstance()->removeQueueStallListener(this);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800190 if (mPendingTransactions.empty()) {
191 return;
192 }
193 BQA_LOGE("Applying pending transactions on dtor %d",
194 static_cast<uint32_t>(mPendingTransactions.size()));
195 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800196 mergePendingTransactions(&t, std::numeric_limits<uint64_t>::max() /* frameNumber */);
Robert Carr79dc06a2022-02-22 15:28:59 -0800197 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
198 t.setApplyToken(mApplyToken).apply(false, true);
chaviw3b4bdcf2022-03-17 09:27:03 -0500199
200 if (mTransactionReadyCallback) {
201 mTransactionReadyCallback(mSyncTransaction);
202 }
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800203}
204
chaviw565ee542021-01-14 10:21:23 -0800205void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height,
Vishnu Naird2aaab12022-02-10 14:49:09 -0800206 int32_t format) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800207 LOG_ALWAYS_FATAL_IF(surface == nullptr, "BLASTBufferQueue: mSurfaceControl must not be NULL");
208
Robert Carr78c25dd2019-08-15 14:10:33 -0700209 std::unique_lock _lock{mMutex};
chaviw565ee542021-01-14 10:21:23 -0800210 if (mFormat != format) {
211 mFormat = format;
chaviw497e81c2021-02-04 17:09:47 -0800212 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
chaviw565ee542021-01-14 10:21:23 -0800213 }
214
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800215 const bool surfaceControlChanged = !SurfaceControl::isSameSurface(mSurfaceControl, surface);
Vishnu Nairab066512022-01-04 22:28:00 +0000216 if (surfaceControlChanged && mSurfaceControl != nullptr) {
217 BQA_LOGD("Updating SurfaceControl without recreating BBQ");
218 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800219 bool applyTransaction = false;
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800220
Vishnu Nair5fa91c22021-06-29 14:30:48 -0700221 // Always update the native object even though they might have the same layer handle, so we can
222 // get the updated transform hint from WM.
223 mSurfaceControl = surface;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800224 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800225 if (surfaceControlChanged) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800226 t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure,
227 layer_state_t::eEnableBackpressure);
228 applyTransaction = true;
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800229 }
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800230 mTransformHint = mSurfaceControl->getTransformHint();
231 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Naira4fbca52021-07-07 16:52:34 -0700232 BQA_LOGV("update width=%d height=%d format=%d mTransformHint=%d", width, height, format,
233 mTransformHint);
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800234
Vishnu Nairea0de002020-11-17 17:42:37 -0800235 ui::Size newSize(width, height);
236 if (mRequestedSize != newSize) {
237 mRequestedSize.set(newSize);
238 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000239 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800240 // If the buffer supports scaling, update the frame immediately since the client may
241 // want to scale the existing buffer to the new size.
242 mSize = mRequestedSize;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800243 if (mUpdateDestinationFrame) {
244 t.setDestinationFrame(mSurfaceControl, Rect(newSize));
245 applyTransaction = true;
246 }
Vishnu Nair53c936c2020-12-03 11:46:37 -0800247 }
Robert Carrfc416512020-04-02 12:32:44 -0700248 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800249 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800250 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
251 t.setApplyToken(mApplyToken).apply(false, true);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800252 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700253}
254
chaviwd7deef72021-10-06 11:53:40 -0500255static std::optional<SurfaceControlStats> findMatchingStat(
256 const std::vector<SurfaceControlStats>& stats, const sp<SurfaceControl>& sc) {
257 for (auto stat : stats) {
258 if (SurfaceControl::isSameSurface(sc, stat.surfaceControl)) {
259 return stat;
260 }
261 }
262 return std::nullopt;
263}
264
265static void transactionCommittedCallbackThunk(void* context, nsecs_t latchTime,
266 const sp<Fence>& presentFence,
267 const std::vector<SurfaceControlStats>& stats) {
268 if (context == nullptr) {
269 return;
270 }
271 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
272 bq->transactionCommittedCallback(latchTime, presentFence, stats);
273}
274
275void BLASTBufferQueue::transactionCommittedCallback(nsecs_t /*latchTime*/,
276 const sp<Fence>& /*presentFence*/,
277 const std::vector<SurfaceControlStats>& stats) {
278 {
279 std::unique_lock _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600280 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500281 BQA_LOGV("transactionCommittedCallback");
282 if (!mSurfaceControlsWithPendingCallback.empty()) {
283 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
284 std::optional<SurfaceControlStats> stat = findMatchingStat(stats, pendingSC);
285 if (stat) {
286 uint64_t currFrameNumber = stat->frameEventStats.frameNumber;
287
288 // We need to check if we were waiting for a transaction callback in order to
289 // process any pending buffers and unblock. It's possible to get transaction
chaviwc1cf4022022-06-03 13:32:33 -0500290 // callbacks for previous requests so we need to ensure that there are no pending
291 // frame numbers that were in a sync. We remove the frame from mSyncedFrameNumbers
292 // set and then check if it's empty. If there are no more pending syncs, we can
293 // proceed with flushing the shadow queue.
294 // We also want to check if mSyncTransaction is null because it's possible another
chaviwd7deef72021-10-06 11:53:40 -0500295 // sync request came in while waiting, but it hasn't started processing yet. In that
296 // case, we don't actually want to flush the frames in between since they will get
297 // processed and merged with the sync transaction and released earlier than if they
298 // were sent to SF
chaviwc1cf4022022-06-03 13:32:33 -0500299 mSyncedFrameNumbers.erase(currFrameNumber);
300 if (mSyncedFrameNumbers.empty() && mSyncTransaction == nullptr) {
chaviwd7deef72021-10-06 11:53:40 -0500301 flushShadowQueue();
302 }
303 } else {
chaviw768bfa02021-11-01 09:50:57 -0500304 BQA_LOGE("Failed to find matching SurfaceControl in transactionCommittedCallback");
chaviwd7deef72021-10-06 11:53:40 -0500305 }
306 } else {
307 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
308 "empty.");
309 }
chaviwd7deef72021-10-06 11:53:40 -0500310 decStrong((void*)transactionCommittedCallbackThunk);
311 }
312}
313
Robert Carr78c25dd2019-08-15 14:10:33 -0700314static void transactionCallbackThunk(void* context, nsecs_t latchTime,
315 const sp<Fence>& presentFence,
316 const std::vector<SurfaceControlStats>& stats) {
317 if (context == nullptr) {
318 return;
319 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800320 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700321 bq->transactionCallback(latchTime, presentFence, stats);
322}
323
324void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
325 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700326 {
327 std::unique_lock _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600328 BBQ_TRACE();
chaviw71c2cc42020-10-23 16:42:02 -0700329 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700330
chaviw42026162021-04-16 15:46:12 -0500331 if (!mSurfaceControlsWithPendingCallback.empty()) {
332 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
333 mSurfaceControlsWithPendingCallback.pop();
chaviwd7deef72021-10-06 11:53:40 -0500334 std::optional<SurfaceControlStats> statsOptional = findMatchingStat(stats, pendingSC);
335 if (statsOptional) {
336 SurfaceControlStats stat = *statsOptional;
chaviw42026162021-04-16 15:46:12 -0500337 mTransformHint = stat.transformHint;
338 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Naira4fbca52021-07-07 16:52:34 -0700339 BQA_LOGV("updated mTransformHint=%d", mTransformHint);
Vishnu Nairde66dc72021-06-17 17:54:41 -0700340 // Update frametime stamps if the frame was latched and presented, indicated by a
341 // valid latch time.
342 if (stat.latchTime > 0) {
343 mBufferItemConsumer
344 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
345 stat.frameEventStats.refreshStartTime,
346 stat.frameEventStats.gpuCompositionDoneFence,
347 stat.presentFence, stat.previousReleaseFence,
348 stat.frameEventStats.compositorTiming,
349 stat.latchTime,
350 stat.frameEventStats.dequeueReadyTime);
351 }
Robert Carr405e2f62021-12-31 16:59:34 -0800352 auto currFrameNumber = stat.frameEventStats.frameNumber;
353 std::vector<ReleaseCallbackId> staleReleases;
354 for (const auto& [key, value]: mSubmitted) {
355 if (currFrameNumber > key.framenumber) {
356 staleReleases.push_back(key);
357 }
358 }
359 for (const auto& staleRelease : staleReleases) {
360 BQA_LOGE("Faking releaseBufferCallback from transactionCompleteCallback");
361 BBQ_TRACE("FakeReleaseCallback");
362 releaseBufferCallbackLocked(staleRelease,
363 stat.previousReleaseFence ? stat.previousReleaseFence : Fence::NO_FENCE,
364 stat.currentMaxAcquiredBufferCount);
365 }
chaviwd7deef72021-10-06 11:53:40 -0500366 } else {
chaviw768bfa02021-11-01 09:50:57 -0500367 BQA_LOGE("Failed to find matching SurfaceControl in transactionCallback");
chaviw42026162021-04-16 15:46:12 -0500368 }
369 } else {
370 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
371 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800372 }
chaviw71c2cc42020-10-23 16:42:02 -0700373
chaviw71c2cc42020-10-23 16:42:02 -0700374 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700375 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700376}
377
Vishnu Nair1506b182021-02-22 14:35:15 -0800378// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
379// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
380// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
381// Otherwise, this is a no-op.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700382static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, const ReleaseCallbackId& id,
chaviw69058fb2021-09-27 09:37:30 -0500383 const sp<Fence>& releaseFence,
384 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800385 sp<BLASTBufferQueue> blastBufferQueue = context.promote();
Vishnu Nair1506b182021-02-22 14:35:15 -0800386 if (blastBufferQueue) {
chaviw69058fb2021-09-27 09:37:30 -0500387 blastBufferQueue->releaseBufferCallback(id, releaseFence, currentMaxAcquiredBufferCount);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700388 } else {
389 ALOGV("releaseBufferCallbackThunk %s blastBufferQueue is dead", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800390 }
391}
392
chaviwd7deef72021-10-06 11:53:40 -0500393void BLASTBufferQueue::flushShadowQueue() {
394 BQA_LOGV("flushShadowQueue");
395 int numFramesToFlush = mNumFrameAvailable;
396 while (numFramesToFlush > 0) {
397 acquireNextBufferLocked(std::nullopt);
398 numFramesToFlush--;
399 }
400}
401
chaviw69058fb2021-09-27 09:37:30 -0500402void BLASTBufferQueue::releaseBufferCallback(
403 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
404 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
chaviw57ae4b22022-02-03 16:51:39 -0600405 BBQ_TRACE();
Robert Carr405e2f62021-12-31 16:59:34 -0800406
Vishnu Nair1506b182021-02-22 14:35:15 -0800407 std::unique_lock _lock{mMutex};
Robert Carr405e2f62021-12-31 16:59:34 -0800408 releaseBufferCallbackLocked(id, releaseFence, currentMaxAcquiredBufferCount);
409}
410
411void BLASTBufferQueue::releaseBufferCallbackLocked(const ReleaseCallbackId& id,
412 const sp<Fence>& releaseFence, std::optional<uint32_t> currentMaxAcquiredBufferCount) {
413 ATRACE_CALL();
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700414 BQA_LOGV("releaseBufferCallback %s", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800415
Ady Abraham899dcdb2021-06-15 16:56:21 -0700416 // Calculate how many buffers we need to hold before we release them back
417 // to the buffer queue. This will prevent higher latency when we are running
418 // on a lower refresh rate than the max supported. We only do that for EGL
419 // clients as others don't care about latency
420 const bool isEGL = [&] {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700421 const auto it = mSubmitted.find(id);
Ady Abraham899dcdb2021-06-15 16:56:21 -0700422 return it != mSubmitted.end() && it->second.mApi == NATIVE_WINDOW_API_EGL;
423 }();
424
chaviw69058fb2021-09-27 09:37:30 -0500425 if (currentMaxAcquiredBufferCount) {
426 mCurrentMaxAcquiredBufferCount = *currentMaxAcquiredBufferCount;
427 }
428
Ady Abraham899dcdb2021-06-15 16:56:21 -0700429 const auto numPendingBuffersToHold =
chaviw69058fb2021-09-27 09:37:30 -0500430 isEGL ? std::max(0u, mMaxAcquiredBuffers - mCurrentMaxAcquiredBufferCount) : 0;
Robert Carr405e2f62021-12-31 16:59:34 -0800431
432 auto rb = ReleasedBuffer{id, releaseFence};
433 if (std::find(mPendingRelease.begin(), mPendingRelease.end(), rb) == mPendingRelease.end()) {
434 mPendingRelease.emplace_back(rb);
435 }
Ady Abraham899dcdb2021-06-15 16:56:21 -0700436
437 // Release all buffers that are beyond the ones that we need to hold
438 while (mPendingRelease.size() > numPendingBuffersToHold) {
chaviw0acd33a2021-11-02 11:55:37 -0500439 const auto releasedBuffer = mPendingRelease.front();
Ady Abraham899dcdb2021-06-15 16:56:21 -0700440 mPendingRelease.pop_front();
chaviw0acd33a2021-11-02 11:55:37 -0500441 releaseBuffer(releasedBuffer.callbackId, releasedBuffer.releaseFence);
chaviwc1cf4022022-06-03 13:32:33 -0500442 // Don't process the transactions here if mSyncedFrameNumbers is not empty. That means
443 // are still transactions that have sync buffers in them that have not been applied or
444 // dropped. Instead, let onFrameAvailable handle processing them since it will merge with
445 // the syncTransaction.
446 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500447 acquireNextBufferLocked(std::nullopt);
448 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800449 }
450
Ady Abraham899dcdb2021-06-15 16:56:21 -0700451 ATRACE_INT("PendingRelease", mPendingRelease.size());
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700452 ATRACE_INT(mQueuedBufferTrace.c_str(),
453 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
Vishnu Nair1506b182021-02-22 14:35:15 -0800454 mCallbackCV.notify_all();
455}
456
chaviw0acd33a2021-11-02 11:55:37 -0500457void BLASTBufferQueue::releaseBuffer(const ReleaseCallbackId& callbackId,
458 const sp<Fence>& releaseFence) {
459 auto it = mSubmitted.find(callbackId);
460 if (it == mSubmitted.end()) {
461 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %s",
462 callbackId.to_string().c_str());
463 return;
464 }
465 mNumAcquired--;
chaviw57ae4b22022-02-03 16:51:39 -0600466 BBQ_TRACE("frame=%" PRIu64, callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500467 BQA_LOGV("released %s", callbackId.to_string().c_str());
468 mBufferItemConsumer->releaseBuffer(it->second, releaseFence);
469 mSubmitted.erase(it);
chaviwc1cf4022022-06-03 13:32:33 -0500470 // Remove the frame number from mSyncedFrameNumbers since we can get a release callback
471 // without getting a transaction committed if the buffer was dropped.
472 mSyncedFrameNumbers.erase(callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500473}
474
chaviwd7deef72021-10-06 11:53:40 -0500475void BLASTBufferQueue::acquireNextBufferLocked(
476 const std::optional<SurfaceComposerClient::Transaction*> transaction) {
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800477 // If the next transaction is set, we want to guarantee the our acquire will not fail, so don't
478 // include the extra buffer when checking if we can acquire the next buffer.
chaviwd7deef72021-10-06 11:53:40 -0500479 const bool includeExtraAcquire = !transaction;
480 const bool maxAcquired = maxBuffersAcquired(includeExtraAcquire);
481 if (mNumFrameAvailable == 0 || maxAcquired) {
482 BQA_LOGV("Can't process next buffer maxBuffersAcquired=%s", boolToString(maxAcquired));
Valerie Haud3b90d22019-11-06 09:37:31 -0800483 return;
484 }
485
Valerie Haua32c5522019-12-09 10:11:08 -0800486 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700487 BQA_LOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800488 return;
489 }
490
Robert Carr78c25dd2019-08-15 14:10:33 -0700491 SurfaceComposerClient::Transaction localTransaction;
492 bool applyTransaction = true;
493 SurfaceComposerClient::Transaction* t = &localTransaction;
chaviwd7deef72021-10-06 11:53:40 -0500494 if (transaction) {
495 t = *transaction;
Robert Carr78c25dd2019-08-15 14:10:33 -0700496 applyTransaction = false;
497 }
498
Valerie Haua32c5522019-12-09 10:11:08 -0800499 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800500
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800501 status_t status =
502 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800503 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
504 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
505 return;
506 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700507 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Robert Carr78c25dd2019-08-15 14:10:33 -0700508 return;
509 }
chaviw57ae4b22022-02-03 16:51:39 -0600510
Valerie Haua32c5522019-12-09 10:11:08 -0800511 auto buffer = bufferItem.mGraphicBuffer;
512 mNumFrameAvailable--;
chaviw57ae4b22022-02-03 16:51:39 -0600513 BBQ_TRACE("frame=%" PRIu64, bufferItem.mFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800514
515 if (buffer == nullptr) {
516 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700517 BQA_LOGE("Buffer was empty");
Valerie Haua32c5522019-12-09 10:11:08 -0800518 return;
519 }
520
Vishnu Nair670b3f72020-09-29 17:52:18 -0700521 if (rejectBuffer(bufferItem)) {
Vishnu Naira4fbca52021-07-07 16:52:34 -0700522 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d "
Vishnu Nairea0de002020-11-17 17:42:37 -0800523 "buffer{size=%dx%d transform=%d}",
524 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
525 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
526 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
chaviwd7deef72021-10-06 11:53:40 -0500527 acquireNextBufferLocked(transaction);
Vishnu Nairea0de002020-11-17 17:42:37 -0800528 return;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700529 }
530
Valerie Haua32c5522019-12-09 10:11:08 -0800531 mNumAcquired++;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700532 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
533 ReleaseCallbackId releaseCallbackId(buffer->getId(), mLastAcquiredFrameNumber);
534 mSubmitted[releaseCallbackId] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700535
Valerie Hau871d6352020-01-29 08:44:02 -0800536 bool needsDisconnect = false;
537 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
538
539 // if producer disconnected before, notify SurfaceFlinger
540 if (needsDisconnect) {
541 t->notifyProducerDisconnect(mSurfaceControl);
542 }
543
Robert Carr78c25dd2019-08-15 14:10:33 -0700544 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
545 incStrong((void*)transactionCallbackThunk);
546
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700547 mSize = mRequestedSize;
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700548 Rect crop = computeCrop(bufferItem);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000549 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
550 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700551 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800552
Vishnu Nair1506b182021-02-22 14:35:15 -0800553 auto releaseBufferCallback =
554 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
chaviw69058fb2021-09-27 09:37:30 -0500555 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
chaviwba4320c2021-09-15 15:20:53 -0500556 sp<Fence> fence = bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE;
chaviw8dd181f2022-01-05 18:36:46 -0600557 t->setBuffer(mSurfaceControl, buffer, fence, bufferItem.mFrameNumber, releaseBufferCallback);
John Reck137069e2020-12-10 22:07:37 -0500558 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
559 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
560 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700561 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviwf2dace72021-11-17 17:36:50 -0600562
chaviw42026162021-04-16 15:46:12 -0500563 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700564
Vishnu Naird2aaab12022-02-10 14:49:09 -0800565 if (mUpdateDestinationFrame) {
566 t->setDestinationFrame(mSurfaceControl, Rect(mSize));
567 } else {
568 const bool ignoreDestinationFrame =
569 bufferItem.mScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE;
570 t->setFlags(mSurfaceControl,
571 ignoreDestinationFrame ? layer_state_t::eIgnoreDestinationFrame : 0,
572 layer_state_t::eIgnoreDestinationFrame);
Vishnu Nair084514a2021-07-30 16:07:42 -0700573 }
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700574 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800575 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800576 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Vishnu Naird2aaab12022-02-10 14:49:09 -0800577 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800578 if (!bufferItem.mIsAutoTimestamp) {
579 t->setDesiredPresentTime(bufferItem.mTimestamp);
580 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700581
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000582 if (!mNextFrameTimelineInfoQueue.empty()) {
Ady Abraham8db10102021-03-15 17:19:23 -0700583 t->setFrameTimelineInfo(mNextFrameTimelineInfoQueue.front());
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000584 mNextFrameTimelineInfoQueue.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100585 }
586
Vishnu Nairadf632b2021-01-07 14:05:08 -0800587 {
588 std::unique_lock _lock{mTimestampMutex};
589 auto dequeueTime = mDequeueTimestamps.find(buffer->getId());
590 if (dequeueTime != mDequeueTimestamps.end()) {
591 Parcel p;
592 p.writeInt64(dequeueTime->second);
593 t->setMetadata(mSurfaceControl, METADATA_DEQUEUE_TIME, p);
594 mDequeueTimestamps.erase(dequeueTime);
595 }
596 }
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800597
chaviw6a195272021-09-03 16:14:25 -0500598 mergePendingTransactions(t, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700599 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800600 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
601 t->setApplyToken(mApplyToken).apply(false, true);
602 mAppliedLastTransaction = true;
603 mLastAppliedFrameNumber = bufferItem.mFrameNumber;
604 } else {
605 t->setBufferHasBarrier(mSurfaceControl, mLastAppliedFrameNumber);
606 mAppliedLastTransaction = false;
Robert Carr78c25dd2019-08-15 14:10:33 -0700607 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700608
chaviwd7deef72021-10-06 11:53:40 -0500609 BQA_LOGV("acquireNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800610 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
Vishnu Naira4fbca52021-07-07 16:52:34 -0700611 " graphicBufferId=%" PRIu64 "%s transform=%d",
chaviw3277faf2021-05-19 16:45:23 -0500612 mSize.width, mSize.height, bufferItem.mFrameNumber, boolToString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800613 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700614 static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(),
Vishnu Naira4fbca52021-07-07 16:52:34 -0700615 bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform);
Robert Carr78c25dd2019-08-15 14:10:33 -0700616}
617
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800618Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
619 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800620 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800621 }
622 return item.mCrop;
623}
624
chaviwd7deef72021-10-06 11:53:40 -0500625void BLASTBufferQueue::acquireAndReleaseBuffer() {
626 BufferItem bufferItem;
chaviw6ebdf5f2021-10-14 11:57:22 -0500627 status_t status =
628 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
629 if (status != OK) {
630 BQA_LOGE("Failed to acquire a buffer in acquireAndReleaseBuffer, err=%s",
631 statusToString(status).c_str());
632 return;
633 }
chaviwd7deef72021-10-06 11:53:40 -0500634 mNumFrameAvailable--;
chaviw6ebdf5f2021-10-14 11:57:22 -0500635 mBufferItemConsumer->releaseBuffer(bufferItem, bufferItem.mFence);
chaviwd7deef72021-10-06 11:53:40 -0500636}
637
chaviw0acd33a2021-11-02 11:55:37 -0500638void BLASTBufferQueue::flushAndWaitForFreeBuffer(std::unique_lock<std::mutex>& lock) {
chaviwc1cf4022022-06-03 13:32:33 -0500639 if (!mSyncedFrameNumbers.empty() && mNumFrameAvailable > 0) {
chaviw0acd33a2021-11-02 11:55:37 -0500640 // We are waiting on a previous sync's transaction callback so allow another sync
641 // transaction to proceed.
642 //
643 // We need to first flush out the transactions that were in between the two syncs.
644 // We do this by merging them into mSyncTransaction so any buffer merging will get
645 // a release callback invoked. The release callback will be async so we need to wait
646 // on max acquired to make sure we have the capacity to acquire another buffer.
647 if (maxBuffersAcquired(false /* includeExtraAcquire */)) {
648 BQA_LOGD("waiting to flush shadow queue...");
649 mCallbackCV.wait(lock);
650 }
651 while (mNumFrameAvailable > 0) {
652 // flush out the shadow queue
653 acquireAndReleaseBuffer();
654 }
655 }
656
657 while (maxBuffersAcquired(false /* includeExtraAcquire */)) {
658 BQA_LOGD("waiting for free buffer.");
659 mCallbackCV.wait(lock);
660 }
661}
662
Vishnu Nairaef1de92020-10-22 12:15:53 -0700663void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000664 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
665 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
chaviwc1cf4022022-06-03 13:32:33 -0500666 bool waitForTransactionCallback = !mSyncedFrameNumbers.empty();
667
Tianhao Yao4861b102022-02-03 20:18:35 +0000668 {
669 BBQ_TRACE();
670 std::unique_lock _lock{mMutex};
671 const bool syncTransactionSet = mTransactionReadyCallback != nullptr;
672 BQA_LOGV("onFrameAvailable-start syncTransactionSet=%s", boolToString(syncTransactionSet));
Valerie Haud3b90d22019-11-06 09:37:31 -0800673
Tianhao Yao4861b102022-02-03 20:18:35 +0000674 if (syncTransactionSet) {
675 bool mayNeedToWaitForBuffer = true;
676 // If we are going to re-use the same mSyncTransaction, release the buffer that may
677 // already be set in the Transaction. This is to allow us a free slot early to continue
678 // processing a new buffer.
679 if (!mAcquireSingleBuffer) {
680 auto bufferData = mSyncTransaction->getAndClearBuffer(mSurfaceControl);
681 if (bufferData) {
682 BQA_LOGD("Releasing previous buffer when syncing: framenumber=%" PRIu64,
683 bufferData->frameNumber);
684 releaseBuffer(bufferData->generateReleaseCallbackId(),
685 bufferData->acquireFence);
686 // Because we just released a buffer, we know there's no need to wait for a free
687 // buffer.
688 mayNeedToWaitForBuffer = false;
689 }
690 }
chaviw0acd33a2021-11-02 11:55:37 -0500691
Tianhao Yao4861b102022-02-03 20:18:35 +0000692 if (mayNeedToWaitForBuffer) {
693 flushAndWaitForFreeBuffer(_lock);
chaviwd7deef72021-10-06 11:53:40 -0500694 }
695 }
696
Tianhao Yao4861b102022-02-03 20:18:35 +0000697 // add to shadow queue
698 mNumFrameAvailable++;
chaviwc1cf4022022-06-03 13:32:33 -0500699 if (waitForTransactionCallback && mNumFrameAvailable >= 2) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000700 acquireAndReleaseBuffer();
701 }
702 ATRACE_INT(mQueuedBufferTrace.c_str(),
703 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
704
705 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " syncTransactionSet=%s",
706 item.mFrameNumber, boolToString(syncTransactionSet));
707
708 if (syncTransactionSet) {
709 acquireNextBufferLocked(mSyncTransaction);
710
711 // Only need a commit callback when syncing to ensure the buffer that's synced has been
712 // sent to SF
713 incStrong((void*)transactionCommittedCallbackThunk);
714 mSyncTransaction->addTransactionCommittedCallback(transactionCommittedCallbackThunk,
715 static_cast<void*>(this));
chaviwc1cf4022022-06-03 13:32:33 -0500716 mSyncedFrameNumbers.emplace(item.mFrameNumber);
Tianhao Yao4861b102022-02-03 20:18:35 +0000717 if (mAcquireSingleBuffer) {
718 prevCallback = mTransactionReadyCallback;
719 prevTransaction = mSyncTransaction;
720 mTransactionReadyCallback = nullptr;
721 mSyncTransaction = nullptr;
722 }
chaviwc1cf4022022-06-03 13:32:33 -0500723 } else if (!waitForTransactionCallback) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000724 acquireNextBufferLocked(std::nullopt);
Valerie Hau0188adf2020-02-13 08:29:20 -0800725 }
726 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000727 if (prevCallback) {
728 prevCallback(prevTransaction);
chaviwd7deef72021-10-06 11:53:40 -0500729 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800730}
731
Vishnu Nairaef1de92020-10-22 12:15:53 -0700732void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
733 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
734 // Do nothing since we are not storing unacquired buffer items locally.
735}
736
Vishnu Nairadf632b2021-01-07 14:05:08 -0800737void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
738 std::unique_lock _lock{mTimestampMutex};
739 mDequeueTimestamps[bufferId] = systemTime();
740};
741
742void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
743 std::unique_lock _lock{mTimestampMutex};
744 mDequeueTimestamps.erase(bufferId);
745};
746
Tianhao Yao4861b102022-02-03 20:18:35 +0000747void BLASTBufferQueue::syncNextTransaction(
748 std::function<void(SurfaceComposerClient::Transaction*)> callback,
749 bool acquireSingleBuffer) {
chaviw57ae4b22022-02-03 16:51:39 -0600750 BBQ_TRACE();
chaviw3b4bdcf2022-03-17 09:27:03 -0500751
752 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
753 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
754
755 {
756 std::lock_guard _lock{mMutex};
757 // We're about to overwrite the previous call so we should invoke that callback
758 // immediately.
759 if (mTransactionReadyCallback) {
760 prevCallback = mTransactionReadyCallback;
761 prevTransaction = mSyncTransaction;
762 }
763
764 mTransactionReadyCallback = callback;
765 if (callback) {
766 mSyncTransaction = new SurfaceComposerClient::Transaction();
767 } else {
768 mSyncTransaction = nullptr;
769 }
770 mAcquireSingleBuffer = mTransactionReadyCallback ? acquireSingleBuffer : true;
Tianhao Yao4861b102022-02-03 20:18:35 +0000771 }
chaviw3b4bdcf2022-03-17 09:27:03 -0500772
773 if (prevCallback) {
774 prevCallback(prevTransaction);
775 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000776}
777
778void BLASTBufferQueue::stopContinuousSyncTransaction() {
779 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
780 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
781 {
782 std::lock_guard _lock{mMutex};
783 bool invokeCallback = mTransactionReadyCallback && !mAcquireSingleBuffer;
784 if (invokeCallback) {
785 prevCallback = mTransactionReadyCallback;
786 prevTransaction = mSyncTransaction;
787 }
788 mTransactionReadyCallback = nullptr;
789 mSyncTransaction = nullptr;
790 mAcquireSingleBuffer = true;
791 }
792 if (prevCallback) {
793 prevCallback(prevTransaction);
794 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700795}
796
Vishnu Nairea0de002020-11-17 17:42:37 -0800797bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700798 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
799 // Only reject buffers if scaling mode is freeze.
800 return false;
801 }
802
Vishnu Naire1a42322020-10-02 17:42:04 -0700803 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
804 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
805
806 // Take the buffer's orientation into account
807 if (item.mTransform & ui::Transform::ROT_90) {
808 std::swap(bufWidth, bufHeight);
809 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800810 ui::Size bufferSize(bufWidth, bufHeight);
811 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800812 return false;
813 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700814
Vishnu Nair670b3f72020-09-29 17:52:18 -0700815 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800816 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700817}
Vishnu Nairbf255772020-10-16 10:54:41 -0700818
819// Check if we have acquired the maximum number of buffers.
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800820// Consumer can acquire an additional buffer if that buffer is not droppable. Set
821// includeExtraAcquire is true to include this buffer to the count. Since this depends on the state
822// of the buffer, the next acquire may return with NO_BUFFER_AVAILABLE.
823bool BLASTBufferQueue::maxBuffersAcquired(bool includeExtraAcquire) const {
Ady Abraham0bde6b52021-05-18 13:57:02 -0700824 int maxAcquiredBuffers = mMaxAcquiredBuffers + (includeExtraAcquire ? 2 : 1);
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800825 return mNumAcquired >= maxAcquiredBuffers;
Vishnu Nairbf255772020-10-16 10:54:41 -0700826}
827
Robert Carr05086b22020-10-13 18:22:51 -0700828class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700829private:
Vishnu Nair95b6d512021-08-30 15:31:08 -0700830 std::mutex mMutex;
Robert Carr9c006e02020-10-14 13:41:57 -0700831 sp<BLASTBufferQueue> mBbq;
Vishnu Nair95b6d512021-08-30 15:31:08 -0700832 bool mDestroyed = false;
833
Robert Carr05086b22020-10-13 18:22:51 -0700834public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700835 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
836 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
837 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700838
Robert Carr05086b22020-10-13 18:22:51 -0700839 void allocateBuffers() override {
840 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
841 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
842 auto gbp = getIGraphicBufferProducer();
843 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
844 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
845 gbp->allocateBuffers(reqWidth, reqHeight,
846 reqFormat, reqUsage);
847
848 }).detach();
849 }
Robert Carr9c006e02020-10-14 13:41:57 -0700850
Marin Shalamanovc5986772021-03-16 16:09:49 +0100851 status_t setFrameRate(float frameRate, int8_t compatibility,
852 int8_t changeFrameRateStrategy) override {
Vishnu Nair95b6d512021-08-30 15:31:08 -0700853 std::unique_lock _lock{mMutex};
854 if (mDestroyed) {
855 return DEAD_OBJECT;
856 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100857 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
858 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700859 return BAD_VALUE;
860 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100861 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700862 }
Robert Carr9b611b72020-10-19 12:00:23 -0700863
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000864 status_t setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) override {
Vishnu Nair95b6d512021-08-30 15:31:08 -0700865 std::unique_lock _lock{mMutex};
866 if (mDestroyed) {
867 return DEAD_OBJECT;
868 }
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000869 return mBbq->setFrameTimelineInfo(frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700870 }
Vishnu Nair95b6d512021-08-30 15:31:08 -0700871
872 void destroy() override {
873 Surface::destroy();
874
875 std::unique_lock _lock{mMutex};
876 mDestroyed = true;
877 mBbq = nullptr;
878 }
Robert Carr05086b22020-10-13 18:22:51 -0700879};
880
Robert Carr9c006e02020-10-14 13:41:57 -0700881// TODO: Can we coalesce this with frame updates? Need to confirm
882// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200883status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
884 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700885 std::unique_lock _lock{mMutex};
886 SurfaceComposerClient::Transaction t;
887
Marin Shalamanov46084422020-10-13 12:33:42 +0200888 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700889}
890
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000891status_t BLASTBufferQueue::setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) {
Robert Carr9b611b72020-10-19 12:00:23 -0700892 std::unique_lock _lock{mMutex};
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000893 mNextFrameTimelineInfoQueue.push(frameTimelineInfo);
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100894 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700895}
896
Hongguang Chen621ec582021-02-16 15:42:35 -0800897void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
898 std::unique_lock _lock{mMutex};
899 SurfaceComposerClient::Transaction t;
900
901 t.setSidebandStream(mSurfaceControl, stream).apply();
902}
903
Vishnu Nair992496b2020-10-22 17:27:21 -0700904sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
905 std::unique_lock _lock{mMutex};
906 sp<IBinder> scHandle = nullptr;
907 if (includeSurfaceControlHandle && mSurfaceControl) {
908 scHandle = mSurfaceControl->getHandle();
909 }
910 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700911}
912
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800913void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
914 uint64_t frameNumber) {
915 std::lock_guard _lock{mMutex};
916 if (mLastAcquiredFrameNumber >= frameNumber) {
917 // Apply the transaction since we have already acquired the desired frame.
918 t->apply();
919 } else {
chaviwaad6cf52021-03-23 17:27:20 -0500920 mPendingTransactions.emplace_back(frameNumber, *t);
921 // Clear the transaction so it can't be applied elsewhere.
922 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800923 }
924}
925
chaviw6a195272021-09-03 16:14:25 -0500926void BLASTBufferQueue::applyPendingTransactions(uint64_t frameNumber) {
927 std::lock_guard _lock{mMutex};
928
929 SurfaceComposerClient::Transaction t;
930 mergePendingTransactions(&t, frameNumber);
Robert Carr79dc06a2022-02-22 15:28:59 -0800931 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
932 t.setApplyToken(mApplyToken).apply(false, true);
chaviw6a195272021-09-03 16:14:25 -0500933}
934
935void BLASTBufferQueue::mergePendingTransactions(SurfaceComposerClient::Transaction* t,
936 uint64_t frameNumber) {
937 auto mergeTransaction =
938 [&t, currentFrameNumber = frameNumber](
939 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
940 auto& [targetFrameNumber, transaction] = pendingTransaction;
941 if (currentFrameNumber < targetFrameNumber) {
942 return false;
943 }
944 t->merge(std::move(transaction));
945 return true;
946 };
947
948 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
949 mPendingTransactions.end(), mergeTransaction),
950 mPendingTransactions.end());
951}
952
chaviwd84085a2022-02-08 11:07:04 -0600953SurfaceComposerClient::Transaction* BLASTBufferQueue::gatherPendingTransactions(
954 uint64_t frameNumber) {
955 std::lock_guard _lock{mMutex};
956 SurfaceComposerClient::Transaction* t = new SurfaceComposerClient::Transaction();
957 mergePendingTransactions(t, frameNumber);
958 return t;
959}
960
Vishnu Nair89496122020-12-14 17:14:53 -0800961// Maintains a single worker thread per process that services a list of runnables.
962class AsyncWorker : public Singleton<AsyncWorker> {
963private:
964 std::thread mThread;
965 bool mDone = false;
966 std::deque<std::function<void()>> mRunnables;
967 std::mutex mMutex;
968 std::condition_variable mCv;
969 void run() {
970 std::unique_lock<std::mutex> lock(mMutex);
971 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -0800972 while (!mRunnables.empty()) {
Vishnu Nair51e4dc82021-10-01 15:32:33 -0700973 std::deque<std::function<void()>> runnables = std::move(mRunnables);
974 mRunnables.clear();
975 lock.unlock();
976 // Run outside the lock since the runnable might trigger another
977 // post to the async worker.
978 execute(runnables);
979 lock.lock();
Vishnu Nair89496122020-12-14 17:14:53 -0800980 }
Wonsik Kim567533e2021-05-04 19:31:29 -0700981 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -0800982 }
983 }
984
Vishnu Nair51e4dc82021-10-01 15:32:33 -0700985 void execute(std::deque<std::function<void()>>& runnables) {
986 while (!runnables.empty()) {
987 std::function<void()> runnable = runnables.front();
988 runnables.pop_front();
989 runnable();
990 }
991 }
992
Vishnu Nair89496122020-12-14 17:14:53 -0800993public:
994 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
995
996 ~AsyncWorker() {
997 mDone = true;
998 mCv.notify_all();
999 if (mThread.joinable()) {
1000 mThread.join();
1001 }
1002 }
1003
1004 void post(std::function<void()> runnable) {
1005 std::unique_lock<std::mutex> lock(mMutex);
1006 mRunnables.emplace_back(std::move(runnable));
1007 mCv.notify_one();
1008 }
1009};
1010ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
1011
1012// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
1013class AsyncProducerListener : public BnProducerListener {
1014private:
1015 const sp<IProducerListener> mListener;
1016
1017public:
1018 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
1019
1020 void onBufferReleased() override {
1021 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
1022 }
1023
1024 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
1025 AsyncWorker::getInstance().post(
1026 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
1027 }
1028};
1029
1030// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
1031// can be non-blocking when the producer is in the client process.
1032class BBQBufferQueueProducer : public BufferQueueProducer {
1033public:
1034 BBQBufferQueueProducer(const sp<BufferQueueCore>& core)
1035 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/) {}
1036
1037 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
1038 QueueBufferOutput* output) override {
1039 if (!listener) {
1040 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
1041 }
1042
1043 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
1044 producerControlledByApp, output);
1045 }
Vishnu Nair17dde612020-12-28 11:39:59 -08001046
1047 int query(int what, int* value) override {
1048 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
1049 *value = 1;
1050 return NO_ERROR;
1051 }
1052 return BufferQueueProducer::query(what, value);
1053 }
Vishnu Nair89496122020-12-14 17:14:53 -08001054};
1055
1056// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
1057// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
1058// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
1059// we can deadlock.
1060void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
1061 sp<IGraphicBufferConsumer>* outConsumer) {
1062 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
1063 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
1064
1065 sp<BufferQueueCore> core(new BufferQueueCore());
1066 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
1067
1068 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core));
1069 LOG_ALWAYS_FATAL_IF(producer == nullptr,
1070 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
1071
Vishnu Nair8b30dd12021-01-25 14:16:54 -08001072 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
1073 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -08001074 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
1075 "BLASTBufferQueue: failed to create BufferQueueConsumer");
1076
1077 *outProducer = producer;
1078 *outConsumer = consumer;
1079}
1080
chaviw497e81c2021-02-04 17:09:47 -08001081PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
1082 PixelFormat convertedFormat = format;
1083 switch (format) {
1084 case PIXEL_FORMAT_TRANSPARENT:
1085 case PIXEL_FORMAT_TRANSLUCENT:
1086 convertedFormat = PIXEL_FORMAT_RGBA_8888;
1087 break;
1088 case PIXEL_FORMAT_OPAQUE:
1089 convertedFormat = PIXEL_FORMAT_RGBX_8888;
1090 break;
1091 }
1092 return convertedFormat;
1093}
1094
Robert Carr82d07c92021-05-10 11:36:43 -07001095uint32_t BLASTBufferQueue::getLastTransformHint() const {
1096 if (mSurfaceControl != nullptr) {
1097 return mSurfaceControl->getTransformHint();
1098 } else {
1099 return 0;
1100 }
1101}
1102
chaviw0b020f82021-08-20 12:00:47 -05001103uint64_t BLASTBufferQueue::getLastAcquiredFrameNum() {
1104 std::unique_lock _lock{mMutex};
1105 return mLastAcquiredFrameNumber;
1106}
1107
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001108void BLASTBufferQueue::abandon() {
1109 std::unique_lock _lock{mMutex};
1110 // flush out the shadow queue
1111 while (mNumFrameAvailable > 0) {
1112 acquireAndReleaseBuffer();
1113 }
1114
1115 // Clear submitted buffer states
1116 mNumAcquired = 0;
1117 mSubmitted.clear();
1118 mPendingRelease.clear();
1119
1120 if (!mPendingTransactions.empty()) {
1121 BQA_LOGD("Applying pending transactions on abandon %d",
1122 static_cast<uint32_t>(mPendingTransactions.size()));
1123 SurfaceComposerClient::Transaction t;
1124 mergePendingTransactions(&t, std::numeric_limits<uint64_t>::max() /* frameNumber */);
Robert Carr79dc06a2022-02-22 15:28:59 -08001125 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
1126 t.setApplyToken(mApplyToken).apply(false, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001127 }
1128
1129 // Clear sync states
chaviwc1cf4022022-06-03 13:32:33 -05001130 if (!mSyncedFrameNumbers.empty()) {
1131 BQA_LOGD("mSyncedFrameNumbers cleared");
1132 mSyncedFrameNumbers.clear();
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001133 }
1134
1135 if (mSyncTransaction != nullptr) {
1136 BQA_LOGD("mSyncTransaction cleared mAcquireSingleBuffer=%s",
1137 mAcquireSingleBuffer ? "true" : "false");
1138 mSyncTransaction = nullptr;
1139 mAcquireSingleBuffer = false;
1140 }
1141
1142 // abandon buffer queue
1143 if (mBufferItemConsumer != nullptr) {
1144 mBufferItemConsumer->abandon();
1145 mBufferItemConsumer->setFrameAvailableListener(nullptr);
1146 mBufferItemConsumer->setBufferFreedListener(nullptr);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001147 }
1148 mBufferItemConsumer = nullptr;
1149 mConsumer = nullptr;
1150 mProducer = nullptr;
1151}
1152
1153bool BLASTBufferQueue::isSameSurfaceControl(const sp<SurfaceControl>& surfaceControl) const {
1154 std::unique_lock _lock{mMutex};
1155 return SurfaceControl::isSameSurface(mSurfaceControl, surfaceControl);
1156}
1157
Robert Carr4c1b6462021-12-21 10:30:50 -08001158void BLASTBufferQueue::setTransactionHangCallback(std::function<void(bool)> callback) {
1159 std::unique_lock _lock{mMutex};
1160 mTransactionHangCallback = callback;
1161}
1162
Robert Carr78c25dd2019-08-15 14:10:33 -07001163} // namespace android