blob: 3bf2e195c57bb0dca0bb39a470a0b1ee9937a777 [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>
Huihong Luo02186fb2022-02-23 14:21:54 -080036#include <private/gui/ComposerServiceAIDL.h>
Ady Abraham0bde6b52021-05-18 13:57:02 -070037
Robert Carr78c25dd2019-08-15 14:10:33 -070038#include <chrono>
39
40using namespace std::chrono_literals;
41
Vishnu Nairdab94092020-09-29 16:09:04 -070042namespace {
chaviw3277faf2021-05-19 16:45:23 -050043inline const char* boolToString(bool b) {
Vishnu Nairdab94092020-09-29 16:09:04 -070044 return b ? "true" : "false";
45}
46} // namespace
47
Robert Carr78c25dd2019-08-15 14:10:33 -070048namespace android {
49
Vishnu Nairdab94092020-09-29 16:09:04 -070050// Macros to include adapter info in log messages
chaviwd7deef72021-10-06 11:53:40 -050051#define BQA_LOGD(x, ...) \
52 ALOGD("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070053#define BQA_LOGV(x, ...) \
54 ALOGV("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairc6f89ee2020-12-11 14:27:32 -080055// enable logs for a single layer
56//#define BQA_LOGV(x, ...) \
57// ALOGV_IF((strstr(mName.c_str(), "SurfaceView") != nullptr), "[%s](f:%u,a:%u) " x, \
58// mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070059#define BQA_LOGE(x, ...) \
60 ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
61
chaviw57ae4b22022-02-03 16:51:39 -060062#define BBQ_TRACE(x, ...) \
63 ATRACE_FORMAT("%s - %s(f:%u,a:%u)" x, __FUNCTION__, mName.c_str(), mNumFrameAvailable, \
64 mNumAcquired, ##__VA_ARGS__)
65
Valerie Hau871d6352020-01-29 08:44:02 -080066void BLASTBufferItemConsumer::onDisconnect() {
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000067 Mutex::Autolock lock(mMutex);
68 mPreviouslyConnected = mCurrentlyConnected;
69 mCurrentlyConnected = false;
70 if (mPreviouslyConnected) {
71 mDisconnectEvents.push(mCurrentFrameNumber);
Valerie Hau871d6352020-01-29 08:44:02 -080072 }
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000073 mFrameEventHistory.onDisconnect();
Valerie Hau871d6352020-01-29 08:44:02 -080074}
75
76void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
77 FrameEventHistoryDelta* outDelta) {
Hongguang Chen621ec582021-02-16 15:42:35 -080078 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080079 if (newTimestamps) {
80 // BufferQueueProducer only adds a new timestamp on
81 // queueBuffer
82 mCurrentFrameNumber = newTimestamps->frameNumber;
83 mFrameEventHistory.addQueue(*newTimestamps);
84 }
85 if (outDelta) {
86 // frame event histories will be processed
87 // only after the producer connects and requests
88 // deltas for the first time. Forward this intent
89 // to SF-side to turn event processing back on
90 mPreviouslyConnected = mCurrentlyConnected;
91 mCurrentlyConnected = true;
92 mFrameEventHistory.getAndResetDelta(outDelta);
93 }
94}
95
96void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
97 const sp<Fence>& glDoneFence,
98 const sp<Fence>& presentFence,
99 const sp<Fence>& prevReleaseFence,
100 CompositorTiming compositorTiming,
101 nsecs_t latchTime, nsecs_t dequeueReadyTime) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800102 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800103
104 // if the producer is not connected, don't bother updating,
105 // the next producer that connects won't access this frame event
106 if (!mCurrentlyConnected) return;
107 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
108 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
109 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
110
111 mFrameEventHistory.addLatch(frameNumber, latchTime);
112 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
113 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
114 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
115 compositorTiming);
116}
117
118void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
119 bool disconnect = false;
Hongguang Chen621ec582021-02-16 15:42:35 -0800120 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800121 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
122 disconnect = true;
123 mDisconnectEvents.pop();
124 }
125 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
126}
127
Hongguang Chen621ec582021-02-16 15:42:35 -0800128void BLASTBufferItemConsumer::onSidebandStreamChanged() {
Ady Abrahamdbca1352021-12-15 11:58:56 -0800129 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
130 if (bbq != nullptr) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800131 sp<NativeHandle> stream = getSidebandStream();
Ady Abrahamdbca1352021-12-15 11:58:56 -0800132 bbq->setSidebandStream(stream);
Hongguang Chen621ec582021-02-16 15:42:35 -0800133 }
134}
135
Vishnu Naird2aaab12022-02-10 14:49:09 -0800136BLASTBufferQueue::BLASTBufferQueue(const std::string& name, bool updateDestinationFrame)
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800137 : mSurfaceControl(nullptr),
138 mSize(1, 1),
Vishnu Nairea0de002020-11-17 17:42:37 -0800139 mRequestedSize(mSize),
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800140 mFormat(PIXEL_FORMAT_RGBA_8888),
Tianhao Yao4861b102022-02-03 20:18:35 +0000141 mTransactionReadyCallback(nullptr),
Vishnu Naird2aaab12022-02-10 14:49:09 -0800142 mSyncTransaction(nullptr),
143 mUpdateDestinationFrame(updateDestinationFrame) {
Vishnu Nair89496122020-12-14 17:14:53 -0800144 createBufferQueue(&mProducer, &mConsumer);
Valerie Hau0889c622020-02-19 15:04:47 -0800145 // since the adapter is in the client process, set dequeue timeout
146 // explicitly so that dequeueBuffer will block
147 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
Valerie Hau65b8e872020-02-13 09:45:14 -0800148
Vishnu Nairdebd1cb2021-03-16 10:06:01 -0700149 // safe default, most producers are expected to override this
150 mProducer->setMaxDequeuedBufferCount(2);
Vishnu Nair1618c672021-02-05 13:08:26 -0800151 mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
152 GraphicBuffer::USAGE_HW_COMPOSER |
153 GraphicBuffer::USAGE_HW_TEXTURE,
Ady Abrahamdbca1352021-12-15 11:58:56 -0800154 1, false, this);
Valerie Haua32c5522019-12-09 10:11:08 -0800155 static int32_t id = 0;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700156 mName = name + "#" + std::to_string(id);
Vishnu Nairdab94092020-09-29 16:09:04 -0700157 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(id);
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700158 mQueuedBufferTrace = "QueuedBuffer - " + mName + "BLAST#" + std::to_string(id);
Valerie Haua32c5522019-12-09 10:11:08 -0800159 id++;
Vishnu Nairdab94092020-09-29 16:09:04 -0700160 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700161 mBufferItemConsumer->setFrameAvailableListener(this);
Robert Carr9f133d72020-04-01 15:51:46 -0700162
Huihong Luo02186fb2022-02-23 14:21:54 -0800163 ComposerServiceAIDL::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 }
310
311 decStrong((void*)transactionCommittedCallbackThunk);
312 }
313}
314
Robert Carr78c25dd2019-08-15 14:10:33 -0700315static void transactionCallbackThunk(void* context, nsecs_t latchTime,
316 const sp<Fence>& presentFence,
317 const std::vector<SurfaceControlStats>& stats) {
318 if (context == nullptr) {
319 return;
320 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800321 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700322 bq->transactionCallback(latchTime, presentFence, stats);
323}
324
325void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
326 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700327 {
328 std::unique_lock _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600329 BBQ_TRACE();
chaviw71c2cc42020-10-23 16:42:02 -0700330 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700331
chaviw42026162021-04-16 15:46:12 -0500332 if (!mSurfaceControlsWithPendingCallback.empty()) {
333 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
334 mSurfaceControlsWithPendingCallback.pop();
chaviwd7deef72021-10-06 11:53:40 -0500335 std::optional<SurfaceControlStats> statsOptional = findMatchingStat(stats, pendingSC);
336 if (statsOptional) {
337 SurfaceControlStats stat = *statsOptional;
chaviw42026162021-04-16 15:46:12 -0500338 mTransformHint = stat.transformHint;
339 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Naira4fbca52021-07-07 16:52:34 -0700340 BQA_LOGV("updated mTransformHint=%d", mTransformHint);
Vishnu Nairde66dc72021-06-17 17:54:41 -0700341 // Update frametime stamps if the frame was latched and presented, indicated by a
342 // valid latch time.
343 if (stat.latchTime > 0) {
344 mBufferItemConsumer
345 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
346 stat.frameEventStats.refreshStartTime,
347 stat.frameEventStats.gpuCompositionDoneFence,
348 stat.presentFence, stat.previousReleaseFence,
349 stat.frameEventStats.compositorTiming,
350 stat.latchTime,
351 stat.frameEventStats.dequeueReadyTime);
352 }
chaviwd7deef72021-10-06 11:53:40 -0500353 } else {
chaviw768bfa02021-11-01 09:50:57 -0500354 BQA_LOGE("Failed to find matching SurfaceControl in transactionCallback");
chaviw42026162021-04-16 15:46:12 -0500355 }
356 } else {
357 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
358 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800359 }
chaviw71c2cc42020-10-23 16:42:02 -0700360
chaviw71c2cc42020-10-23 16:42:02 -0700361 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700362 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700363}
364
Vishnu Nair1506b182021-02-22 14:35:15 -0800365// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
366// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
367// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
368// Otherwise, this is a no-op.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700369static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, const ReleaseCallbackId& id,
chaviw69058fb2021-09-27 09:37:30 -0500370 const sp<Fence>& releaseFence,
371 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800372 sp<BLASTBufferQueue> blastBufferQueue = context.promote();
Vishnu Nair1506b182021-02-22 14:35:15 -0800373 if (blastBufferQueue) {
chaviw69058fb2021-09-27 09:37:30 -0500374 blastBufferQueue->releaseBufferCallback(id, releaseFence, currentMaxAcquiredBufferCount);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700375 } else {
376 ALOGV("releaseBufferCallbackThunk %s blastBufferQueue is dead", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800377 }
378}
379
chaviwd7deef72021-10-06 11:53:40 -0500380void BLASTBufferQueue::flushShadowQueue() {
381 BQA_LOGV("flushShadowQueue");
382 int numFramesToFlush = mNumFrameAvailable;
383 while (numFramesToFlush > 0) {
384 acquireNextBufferLocked(std::nullopt);
385 numFramesToFlush--;
386 }
387}
388
chaviw69058fb2021-09-27 09:37:30 -0500389void BLASTBufferQueue::releaseBufferCallback(
390 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
391 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
chaviw57ae4b22022-02-03 16:51:39 -0600392 BBQ_TRACE();
Vishnu Nair1506b182021-02-22 14:35:15 -0800393 std::unique_lock _lock{mMutex};
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700394 BQA_LOGV("releaseBufferCallback %s", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800395
Ady Abraham899dcdb2021-06-15 16:56:21 -0700396 // Calculate how many buffers we need to hold before we release them back
397 // to the buffer queue. This will prevent higher latency when we are running
398 // on a lower refresh rate than the max supported. We only do that for EGL
399 // clients as others don't care about latency
400 const bool isEGL = [&] {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700401 const auto it = mSubmitted.find(id);
Ady Abraham899dcdb2021-06-15 16:56:21 -0700402 return it != mSubmitted.end() && it->second.mApi == NATIVE_WINDOW_API_EGL;
403 }();
404
chaviw69058fb2021-09-27 09:37:30 -0500405 if (currentMaxAcquiredBufferCount) {
406 mCurrentMaxAcquiredBufferCount = *currentMaxAcquiredBufferCount;
407 }
408
Ady Abraham899dcdb2021-06-15 16:56:21 -0700409 const auto numPendingBuffersToHold =
chaviw69058fb2021-09-27 09:37:30 -0500410 isEGL ? std::max(0u, mMaxAcquiredBuffers - mCurrentMaxAcquiredBufferCount) : 0;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700411 mPendingRelease.emplace_back(ReleasedBuffer{id, releaseFence});
Ady Abraham899dcdb2021-06-15 16:56:21 -0700412
413 // Release all buffers that are beyond the ones that we need to hold
414 while (mPendingRelease.size() > numPendingBuffersToHold) {
chaviw0acd33a2021-11-02 11:55:37 -0500415 const auto releasedBuffer = mPendingRelease.front();
Ady Abraham899dcdb2021-06-15 16:56:21 -0700416 mPendingRelease.pop_front();
chaviw0acd33a2021-11-02 11:55:37 -0500417 releaseBuffer(releasedBuffer.callbackId, releasedBuffer.releaseFence);
chaviwc1cf4022022-06-03 13:32:33 -0500418 // Don't process the transactions here if mSyncedFrameNumbers is not empty. That means
419 // are still transactions that have sync buffers in them that have not been applied or
420 // dropped. Instead, let onFrameAvailable handle processing them since it will merge with
421 // the syncTransaction.
422 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500423 acquireNextBufferLocked(std::nullopt);
424 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800425 }
426
Ady Abraham899dcdb2021-06-15 16:56:21 -0700427 ATRACE_INT("PendingRelease", mPendingRelease.size());
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700428 ATRACE_INT(mQueuedBufferTrace.c_str(),
429 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
Vishnu Nair1506b182021-02-22 14:35:15 -0800430 mCallbackCV.notify_all();
431}
432
chaviw0acd33a2021-11-02 11:55:37 -0500433void BLASTBufferQueue::releaseBuffer(const ReleaseCallbackId& callbackId,
434 const sp<Fence>& releaseFence) {
435 auto it = mSubmitted.find(callbackId);
436 if (it == mSubmitted.end()) {
437 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %s",
438 callbackId.to_string().c_str());
439 return;
440 }
441 mNumAcquired--;
chaviw57ae4b22022-02-03 16:51:39 -0600442 BBQ_TRACE("frame=%" PRIu64, callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500443 BQA_LOGV("released %s", callbackId.to_string().c_str());
444 mBufferItemConsumer->releaseBuffer(it->second, releaseFence);
445 mSubmitted.erase(it);
chaviwc1cf4022022-06-03 13:32:33 -0500446 // Remove the frame number from mSyncedFrameNumbers since we can get a release callback
447 // without getting a transaction committed if the buffer was dropped.
448 mSyncedFrameNumbers.erase(callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500449}
450
chaviwd7deef72021-10-06 11:53:40 -0500451void BLASTBufferQueue::acquireNextBufferLocked(
452 const std::optional<SurfaceComposerClient::Transaction*> transaction) {
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800453 // If the next transaction is set, we want to guarantee the our acquire will not fail, so don't
454 // include the extra buffer when checking if we can acquire the next buffer.
chaviwd7deef72021-10-06 11:53:40 -0500455 const bool includeExtraAcquire = !transaction;
456 const bool maxAcquired = maxBuffersAcquired(includeExtraAcquire);
457 if (mNumFrameAvailable == 0 || maxAcquired) {
458 BQA_LOGV("Can't process next buffer maxBuffersAcquired=%s", boolToString(maxAcquired));
Valerie Haud3b90d22019-11-06 09:37:31 -0800459 return;
460 }
461
Valerie Haua32c5522019-12-09 10:11:08 -0800462 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700463 BQA_LOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800464 return;
465 }
466
Robert Carr78c25dd2019-08-15 14:10:33 -0700467 SurfaceComposerClient::Transaction localTransaction;
468 bool applyTransaction = true;
469 SurfaceComposerClient::Transaction* t = &localTransaction;
chaviwd7deef72021-10-06 11:53:40 -0500470 if (transaction) {
471 t = *transaction;
Robert Carr78c25dd2019-08-15 14:10:33 -0700472 applyTransaction = false;
473 }
474
Valerie Haua32c5522019-12-09 10:11:08 -0800475 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800476
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800477 status_t status =
478 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800479 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
480 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
481 return;
482 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700483 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Robert Carr78c25dd2019-08-15 14:10:33 -0700484 return;
485 }
chaviw57ae4b22022-02-03 16:51:39 -0600486
Valerie Haua32c5522019-12-09 10:11:08 -0800487 auto buffer = bufferItem.mGraphicBuffer;
488 mNumFrameAvailable--;
chaviw57ae4b22022-02-03 16:51:39 -0600489 BBQ_TRACE("frame=%" PRIu64, bufferItem.mFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800490
491 if (buffer == nullptr) {
492 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700493 BQA_LOGE("Buffer was empty");
Valerie Haua32c5522019-12-09 10:11:08 -0800494 return;
495 }
496
Vishnu Nair670b3f72020-09-29 17:52:18 -0700497 if (rejectBuffer(bufferItem)) {
Vishnu Naira4fbca52021-07-07 16:52:34 -0700498 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d "
Vishnu Nairea0de002020-11-17 17:42:37 -0800499 "buffer{size=%dx%d transform=%d}",
500 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
501 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
502 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
chaviwd7deef72021-10-06 11:53:40 -0500503 acquireNextBufferLocked(transaction);
Vishnu Nairea0de002020-11-17 17:42:37 -0800504 return;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700505 }
506
Valerie Haua32c5522019-12-09 10:11:08 -0800507 mNumAcquired++;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700508 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
509 ReleaseCallbackId releaseCallbackId(buffer->getId(), mLastAcquiredFrameNumber);
510 mSubmitted[releaseCallbackId] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700511
Valerie Hau871d6352020-01-29 08:44:02 -0800512 bool needsDisconnect = false;
513 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
514
515 // if producer disconnected before, notify SurfaceFlinger
516 if (needsDisconnect) {
517 t->notifyProducerDisconnect(mSurfaceControl);
518 }
519
Robert Carr78c25dd2019-08-15 14:10:33 -0700520 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
521 incStrong((void*)transactionCallbackThunk);
522
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700523 mSize = mRequestedSize;
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700524 Rect crop = computeCrop(bufferItem);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000525 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
526 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700527 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800528
Vishnu Nair1506b182021-02-22 14:35:15 -0800529 auto releaseBufferCallback =
530 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
chaviw69058fb2021-09-27 09:37:30 -0500531 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
chaviwba4320c2021-09-15 15:20:53 -0500532 sp<Fence> fence = bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE;
chaviw8dd181f2022-01-05 18:36:46 -0600533 t->setBuffer(mSurfaceControl, buffer, fence, bufferItem.mFrameNumber, releaseBufferCallback);
John Reck137069e2020-12-10 22:07:37 -0500534 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
535 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
536 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700537 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviwf2dace72021-11-17 17:36:50 -0600538
chaviw42026162021-04-16 15:46:12 -0500539 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700540
Vishnu Naird2aaab12022-02-10 14:49:09 -0800541 if (mUpdateDestinationFrame) {
542 t->setDestinationFrame(mSurfaceControl, Rect(mSize));
543 } else {
544 const bool ignoreDestinationFrame =
545 bufferItem.mScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE;
546 t->setFlags(mSurfaceControl,
547 ignoreDestinationFrame ? layer_state_t::eIgnoreDestinationFrame : 0,
548 layer_state_t::eIgnoreDestinationFrame);
Vishnu Nair084514a2021-07-30 16:07:42 -0700549 }
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700550 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800551 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800552 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Vishnu Naird2aaab12022-02-10 14:49:09 -0800553 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800554 if (!bufferItem.mIsAutoTimestamp) {
555 t->setDesiredPresentTime(bufferItem.mTimestamp);
556 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700557
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000558 if (!mNextFrameTimelineInfoQueue.empty()) {
Ady Abraham8db10102021-03-15 17:19:23 -0700559 t->setFrameTimelineInfo(mNextFrameTimelineInfoQueue.front());
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000560 mNextFrameTimelineInfoQueue.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100561 }
562
Vishnu Nairadf632b2021-01-07 14:05:08 -0800563 {
564 std::unique_lock _lock{mTimestampMutex};
565 auto dequeueTime = mDequeueTimestamps.find(buffer->getId());
566 if (dequeueTime != mDequeueTimestamps.end()) {
567 Parcel p;
568 p.writeInt64(dequeueTime->second);
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800569 t->setMetadata(mSurfaceControl, gui::METADATA_DEQUEUE_TIME, p);
Vishnu Nairadf632b2021-01-07 14:05:08 -0800570 mDequeueTimestamps.erase(dequeueTime);
571 }
572 }
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800573
chaviw6a195272021-09-03 16:14:25 -0500574 mergePendingTransactions(t, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700575 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800576 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
577 t->setApplyToken(mApplyToken).apply(false, true);
578 mAppliedLastTransaction = true;
579 mLastAppliedFrameNumber = bufferItem.mFrameNumber;
580 } else {
581 t->setBufferHasBarrier(mSurfaceControl, mLastAppliedFrameNumber);
582 mAppliedLastTransaction = false;
Robert Carr78c25dd2019-08-15 14:10:33 -0700583 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700584
chaviwd7deef72021-10-06 11:53:40 -0500585 BQA_LOGV("acquireNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800586 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
Vishnu Naira4fbca52021-07-07 16:52:34 -0700587 " graphicBufferId=%" PRIu64 "%s transform=%d",
chaviw3277faf2021-05-19 16:45:23 -0500588 mSize.width, mSize.height, bufferItem.mFrameNumber, boolToString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800589 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700590 static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(),
Vishnu Naira4fbca52021-07-07 16:52:34 -0700591 bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform);
Robert Carr78c25dd2019-08-15 14:10:33 -0700592}
593
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800594Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
595 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800596 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800597 }
598 return item.mCrop;
599}
600
chaviwd7deef72021-10-06 11:53:40 -0500601void BLASTBufferQueue::acquireAndReleaseBuffer() {
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000602 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500603 BufferItem bufferItem;
chaviw6ebdf5f2021-10-14 11:57:22 -0500604 status_t status =
605 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
606 if (status != OK) {
607 BQA_LOGE("Failed to acquire a buffer in acquireAndReleaseBuffer, err=%s",
608 statusToString(status).c_str());
609 return;
610 }
chaviwd7deef72021-10-06 11:53:40 -0500611 mNumFrameAvailable--;
chaviw6ebdf5f2021-10-14 11:57:22 -0500612 mBufferItemConsumer->releaseBuffer(bufferItem, bufferItem.mFence);
chaviwd7deef72021-10-06 11:53:40 -0500613}
614
chaviw0acd33a2021-11-02 11:55:37 -0500615void BLASTBufferQueue::flushAndWaitForFreeBuffer(std::unique_lock<std::mutex>& lock) {
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000616 BBQ_TRACE();
chaviwc1cf4022022-06-03 13:32:33 -0500617 if (!mSyncedFrameNumbers.empty() && mNumFrameAvailable > 0) {
chaviw0acd33a2021-11-02 11:55:37 -0500618 // We are waiting on a previous sync's transaction callback so allow another sync
619 // transaction to proceed.
620 //
621 // We need to first flush out the transactions that were in between the two syncs.
622 // We do this by merging them into mSyncTransaction so any buffer merging will get
623 // a release callback invoked. The release callback will be async so we need to wait
624 // on max acquired to make sure we have the capacity to acquire another buffer.
625 if (maxBuffersAcquired(false /* includeExtraAcquire */)) {
626 BQA_LOGD("waiting to flush shadow queue...");
627 mCallbackCV.wait(lock);
628 }
629 while (mNumFrameAvailable > 0) {
630 // flush out the shadow queue
631 acquireAndReleaseBuffer();
632 }
633 }
634
635 while (maxBuffersAcquired(false /* includeExtraAcquire */)) {
636 BQA_LOGD("waiting for free buffer.");
637 mCallbackCV.wait(lock);
638 }
639}
640
Vishnu Nairaef1de92020-10-22 12:15:53 -0700641void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000642 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
643 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
chaviwc1cf4022022-06-03 13:32:33 -0500644 bool waitForTransactionCallback = !mSyncedFrameNumbers.empty();
645
Tianhao Yao4861b102022-02-03 20:18:35 +0000646 {
Tianhao Yao4861b102022-02-03 20:18:35 +0000647 std::unique_lock _lock{mMutex};
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000648 BBQ_TRACE();
Tianhao Yao4861b102022-02-03 20:18:35 +0000649 const bool syncTransactionSet = mTransactionReadyCallback != nullptr;
650 BQA_LOGV("onFrameAvailable-start syncTransactionSet=%s", boolToString(syncTransactionSet));
Valerie Haud3b90d22019-11-06 09:37:31 -0800651
Tianhao Yao4861b102022-02-03 20:18:35 +0000652 if (syncTransactionSet) {
653 bool mayNeedToWaitForBuffer = true;
654 // If we are going to re-use the same mSyncTransaction, release the buffer that may
655 // already be set in the Transaction. This is to allow us a free slot early to continue
656 // processing a new buffer.
657 if (!mAcquireSingleBuffer) {
658 auto bufferData = mSyncTransaction->getAndClearBuffer(mSurfaceControl);
659 if (bufferData) {
660 BQA_LOGD("Releasing previous buffer when syncing: framenumber=%" PRIu64,
661 bufferData->frameNumber);
662 releaseBuffer(bufferData->generateReleaseCallbackId(),
663 bufferData->acquireFence);
664 // Because we just released a buffer, we know there's no need to wait for a free
665 // buffer.
666 mayNeedToWaitForBuffer = false;
667 }
668 }
chaviw0acd33a2021-11-02 11:55:37 -0500669
Tianhao Yao4861b102022-02-03 20:18:35 +0000670 if (mayNeedToWaitForBuffer) {
671 flushAndWaitForFreeBuffer(_lock);
chaviwd7deef72021-10-06 11:53:40 -0500672 }
673 }
674
Tianhao Yao4861b102022-02-03 20:18:35 +0000675 // add to shadow queue
676 mNumFrameAvailable++;
chaviwc1cf4022022-06-03 13:32:33 -0500677 if (waitForTransactionCallback && mNumFrameAvailable >= 2) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000678 acquireAndReleaseBuffer();
679 }
680 ATRACE_INT(mQueuedBufferTrace.c_str(),
681 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
682
683 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " syncTransactionSet=%s",
684 item.mFrameNumber, boolToString(syncTransactionSet));
685
686 if (syncTransactionSet) {
687 acquireNextBufferLocked(mSyncTransaction);
688
689 // Only need a commit callback when syncing to ensure the buffer that's synced has been
690 // sent to SF
691 incStrong((void*)transactionCommittedCallbackThunk);
692 mSyncTransaction->addTransactionCommittedCallback(transactionCommittedCallbackThunk,
693 static_cast<void*>(this));
chaviwc1cf4022022-06-03 13:32:33 -0500694 mSyncedFrameNumbers.emplace(item.mFrameNumber);
Tianhao Yao4861b102022-02-03 20:18:35 +0000695 if (mAcquireSingleBuffer) {
696 prevCallback = mTransactionReadyCallback;
697 prevTransaction = mSyncTransaction;
698 mTransactionReadyCallback = nullptr;
699 mSyncTransaction = nullptr;
700 }
chaviwc1cf4022022-06-03 13:32:33 -0500701 } else if (!waitForTransactionCallback) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000702 acquireNextBufferLocked(std::nullopt);
Valerie Hau0188adf2020-02-13 08:29:20 -0800703 }
704 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000705 if (prevCallback) {
706 prevCallback(prevTransaction);
chaviwd7deef72021-10-06 11:53:40 -0500707 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800708}
709
Vishnu Nairaef1de92020-10-22 12:15:53 -0700710void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
711 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
712 // Do nothing since we are not storing unacquired buffer items locally.
713}
714
Vishnu Nairadf632b2021-01-07 14:05:08 -0800715void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
716 std::unique_lock _lock{mTimestampMutex};
717 mDequeueTimestamps[bufferId] = systemTime();
718};
719
720void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
721 std::unique_lock _lock{mTimestampMutex};
722 mDequeueTimestamps.erase(bufferId);
723};
724
Tianhao Yao4861b102022-02-03 20:18:35 +0000725void BLASTBufferQueue::syncNextTransaction(
726 std::function<void(SurfaceComposerClient::Transaction*)> callback,
727 bool acquireSingleBuffer) {
chaviw57ae4b22022-02-03 16:51:39 -0600728 BBQ_TRACE();
chaviw3b4bdcf2022-03-17 09:27:03 -0500729
730 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
731 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
732
733 {
734 std::lock_guard _lock{mMutex};
735 // We're about to overwrite the previous call so we should invoke that callback
736 // immediately.
737 if (mTransactionReadyCallback) {
738 prevCallback = mTransactionReadyCallback;
739 prevTransaction = mSyncTransaction;
740 }
741
742 mTransactionReadyCallback = callback;
743 if (callback) {
744 mSyncTransaction = new SurfaceComposerClient::Transaction();
745 } else {
746 mSyncTransaction = nullptr;
747 }
748 mAcquireSingleBuffer = mTransactionReadyCallback ? acquireSingleBuffer : true;
Tianhao Yao4861b102022-02-03 20:18:35 +0000749 }
chaviw3b4bdcf2022-03-17 09:27:03 -0500750
751 if (prevCallback) {
752 prevCallback(prevTransaction);
753 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000754}
755
756void BLASTBufferQueue::stopContinuousSyncTransaction() {
757 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
758 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
759 {
760 std::lock_guard _lock{mMutex};
761 bool invokeCallback = mTransactionReadyCallback && !mAcquireSingleBuffer;
762 if (invokeCallback) {
763 prevCallback = mTransactionReadyCallback;
764 prevTransaction = mSyncTransaction;
765 }
766 mTransactionReadyCallback = nullptr;
767 mSyncTransaction = nullptr;
768 mAcquireSingleBuffer = true;
769 }
770 if (prevCallback) {
771 prevCallback(prevTransaction);
772 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700773}
774
Vishnu Nairea0de002020-11-17 17:42:37 -0800775bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700776 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
777 // Only reject buffers if scaling mode is freeze.
778 return false;
779 }
780
Vishnu Naire1a42322020-10-02 17:42:04 -0700781 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
782 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
783
784 // Take the buffer's orientation into account
785 if (item.mTransform & ui::Transform::ROT_90) {
786 std::swap(bufWidth, bufHeight);
787 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800788 ui::Size bufferSize(bufWidth, bufHeight);
789 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800790 return false;
791 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700792
Vishnu Nair670b3f72020-09-29 17:52:18 -0700793 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800794 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700795}
Vishnu Nairbf255772020-10-16 10:54:41 -0700796
797// Check if we have acquired the maximum number of buffers.
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800798// Consumer can acquire an additional buffer if that buffer is not droppable. Set
799// includeExtraAcquire is true to include this buffer to the count. Since this depends on the state
800// of the buffer, the next acquire may return with NO_BUFFER_AVAILABLE.
801bool BLASTBufferQueue::maxBuffersAcquired(bool includeExtraAcquire) const {
Ady Abraham0bde6b52021-05-18 13:57:02 -0700802 int maxAcquiredBuffers = mMaxAcquiredBuffers + (includeExtraAcquire ? 2 : 1);
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800803 return mNumAcquired >= maxAcquiredBuffers;
Vishnu Nairbf255772020-10-16 10:54:41 -0700804}
805
Robert Carr05086b22020-10-13 18:22:51 -0700806class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700807private:
Vishnu Nair95b6d512021-08-30 15:31:08 -0700808 std::mutex mMutex;
Robert Carr9c006e02020-10-14 13:41:57 -0700809 sp<BLASTBufferQueue> mBbq;
Vishnu Nair95b6d512021-08-30 15:31:08 -0700810 bool mDestroyed = false;
811
Robert Carr05086b22020-10-13 18:22:51 -0700812public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700813 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
814 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
815 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700816
Robert Carr05086b22020-10-13 18:22:51 -0700817 void allocateBuffers() override {
818 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
819 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
820 auto gbp = getIGraphicBufferProducer();
821 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
822 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
823 gbp->allocateBuffers(reqWidth, reqHeight,
824 reqFormat, reqUsage);
825
826 }).detach();
827 }
Robert Carr9c006e02020-10-14 13:41:57 -0700828
Marin Shalamanovc5986772021-03-16 16:09:49 +0100829 status_t setFrameRate(float frameRate, int8_t compatibility,
830 int8_t changeFrameRateStrategy) override {
Vishnu Nair95b6d512021-08-30 15:31:08 -0700831 std::unique_lock _lock{mMutex};
832 if (mDestroyed) {
833 return DEAD_OBJECT;
834 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100835 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
836 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700837 return BAD_VALUE;
838 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100839 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700840 }
Robert Carr9b611b72020-10-19 12:00:23 -0700841
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000842 status_t setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) override {
Vishnu Nair95b6d512021-08-30 15:31:08 -0700843 std::unique_lock _lock{mMutex};
844 if (mDestroyed) {
845 return DEAD_OBJECT;
846 }
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000847 return mBbq->setFrameTimelineInfo(frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700848 }
Vishnu Nair95b6d512021-08-30 15:31:08 -0700849
850 void destroy() override {
851 Surface::destroy();
852
853 std::unique_lock _lock{mMutex};
854 mDestroyed = true;
855 mBbq = nullptr;
856 }
Robert Carr05086b22020-10-13 18:22:51 -0700857};
858
Robert Carr9c006e02020-10-14 13:41:57 -0700859// TODO: Can we coalesce this with frame updates? Need to confirm
860// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200861status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
862 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700863 std::unique_lock _lock{mMutex};
864 SurfaceComposerClient::Transaction t;
865
Marin Shalamanov46084422020-10-13 12:33:42 +0200866 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700867}
868
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000869status_t BLASTBufferQueue::setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) {
Robert Carr9b611b72020-10-19 12:00:23 -0700870 std::unique_lock _lock{mMutex};
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000871 mNextFrameTimelineInfoQueue.push(frameTimelineInfo);
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100872 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700873}
874
Hongguang Chen621ec582021-02-16 15:42:35 -0800875void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
876 std::unique_lock _lock{mMutex};
877 SurfaceComposerClient::Transaction t;
878
879 t.setSidebandStream(mSurfaceControl, stream).apply();
880}
881
Vishnu Nair992496b2020-10-22 17:27:21 -0700882sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
883 std::unique_lock _lock{mMutex};
884 sp<IBinder> scHandle = nullptr;
885 if (includeSurfaceControlHandle && mSurfaceControl) {
886 scHandle = mSurfaceControl->getHandle();
887 }
888 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700889}
890
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800891void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
892 uint64_t frameNumber) {
893 std::lock_guard _lock{mMutex};
894 if (mLastAcquiredFrameNumber >= frameNumber) {
895 // Apply the transaction since we have already acquired the desired frame.
896 t->apply();
897 } else {
chaviwaad6cf52021-03-23 17:27:20 -0500898 mPendingTransactions.emplace_back(frameNumber, *t);
899 // Clear the transaction so it can't be applied elsewhere.
900 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800901 }
902}
903
chaviw6a195272021-09-03 16:14:25 -0500904void BLASTBufferQueue::applyPendingTransactions(uint64_t frameNumber) {
905 std::lock_guard _lock{mMutex};
906
907 SurfaceComposerClient::Transaction t;
908 mergePendingTransactions(&t, frameNumber);
Robert Carr79dc06a2022-02-22 15:28:59 -0800909 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
910 t.setApplyToken(mApplyToken).apply(false, true);
chaviw6a195272021-09-03 16:14:25 -0500911}
912
913void BLASTBufferQueue::mergePendingTransactions(SurfaceComposerClient::Transaction* t,
914 uint64_t frameNumber) {
915 auto mergeTransaction =
916 [&t, currentFrameNumber = frameNumber](
917 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
918 auto& [targetFrameNumber, transaction] = pendingTransaction;
919 if (currentFrameNumber < targetFrameNumber) {
920 return false;
921 }
922 t->merge(std::move(transaction));
923 return true;
924 };
925
926 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
927 mPendingTransactions.end(), mergeTransaction),
928 mPendingTransactions.end());
929}
930
chaviwd84085a2022-02-08 11:07:04 -0600931SurfaceComposerClient::Transaction* BLASTBufferQueue::gatherPendingTransactions(
932 uint64_t frameNumber) {
933 std::lock_guard _lock{mMutex};
934 SurfaceComposerClient::Transaction* t = new SurfaceComposerClient::Transaction();
935 mergePendingTransactions(t, frameNumber);
936 return t;
937}
938
Vishnu Nair89496122020-12-14 17:14:53 -0800939// Maintains a single worker thread per process that services a list of runnables.
940class AsyncWorker : public Singleton<AsyncWorker> {
941private:
942 std::thread mThread;
943 bool mDone = false;
944 std::deque<std::function<void()>> mRunnables;
945 std::mutex mMutex;
946 std::condition_variable mCv;
947 void run() {
948 std::unique_lock<std::mutex> lock(mMutex);
949 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -0800950 while (!mRunnables.empty()) {
Vishnu Nair51e4dc82021-10-01 15:32:33 -0700951 std::deque<std::function<void()>> runnables = std::move(mRunnables);
952 mRunnables.clear();
953 lock.unlock();
954 // Run outside the lock since the runnable might trigger another
955 // post to the async worker.
956 execute(runnables);
957 lock.lock();
Vishnu Nair89496122020-12-14 17:14:53 -0800958 }
Wonsik Kim567533e2021-05-04 19:31:29 -0700959 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -0800960 }
961 }
962
Vishnu Nair51e4dc82021-10-01 15:32:33 -0700963 void execute(std::deque<std::function<void()>>& runnables) {
964 while (!runnables.empty()) {
965 std::function<void()> runnable = runnables.front();
966 runnables.pop_front();
967 runnable();
968 }
969 }
970
Vishnu Nair89496122020-12-14 17:14:53 -0800971public:
972 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
973
974 ~AsyncWorker() {
975 mDone = true;
976 mCv.notify_all();
977 if (mThread.joinable()) {
978 mThread.join();
979 }
980 }
981
982 void post(std::function<void()> runnable) {
983 std::unique_lock<std::mutex> lock(mMutex);
984 mRunnables.emplace_back(std::move(runnable));
985 mCv.notify_one();
986 }
987};
988ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
989
990// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
991class AsyncProducerListener : public BnProducerListener {
992private:
993 const sp<IProducerListener> mListener;
994
995public:
996 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
997
998 void onBufferReleased() override {
999 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
1000 }
1001
1002 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
1003 AsyncWorker::getInstance().post(
1004 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
1005 }
1006};
1007
1008// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
1009// can be non-blocking when the producer is in the client process.
1010class BBQBufferQueueProducer : public BufferQueueProducer {
1011public:
1012 BBQBufferQueueProducer(const sp<BufferQueueCore>& core)
1013 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/) {}
1014
1015 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
1016 QueueBufferOutput* output) override {
1017 if (!listener) {
1018 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
1019 }
1020
1021 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
1022 producerControlledByApp, output);
1023 }
Vishnu Nair17dde612020-12-28 11:39:59 -08001024
1025 int query(int what, int* value) override {
1026 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
1027 *value = 1;
1028 return NO_ERROR;
1029 }
1030 return BufferQueueProducer::query(what, value);
1031 }
Vishnu Nair89496122020-12-14 17:14:53 -08001032};
1033
1034// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
1035// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
1036// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
1037// we can deadlock.
1038void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
1039 sp<IGraphicBufferConsumer>* outConsumer) {
1040 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
1041 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
1042
1043 sp<BufferQueueCore> core(new BufferQueueCore());
1044 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
1045
1046 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core));
1047 LOG_ALWAYS_FATAL_IF(producer == nullptr,
1048 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
1049
Vishnu Nair8b30dd12021-01-25 14:16:54 -08001050 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
1051 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -08001052 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
1053 "BLASTBufferQueue: failed to create BufferQueueConsumer");
1054
1055 *outProducer = producer;
1056 *outConsumer = consumer;
1057}
1058
chaviw497e81c2021-02-04 17:09:47 -08001059PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
1060 PixelFormat convertedFormat = format;
1061 switch (format) {
1062 case PIXEL_FORMAT_TRANSPARENT:
1063 case PIXEL_FORMAT_TRANSLUCENT:
1064 convertedFormat = PIXEL_FORMAT_RGBA_8888;
1065 break;
1066 case PIXEL_FORMAT_OPAQUE:
1067 convertedFormat = PIXEL_FORMAT_RGBX_8888;
1068 break;
1069 }
1070 return convertedFormat;
1071}
1072
Robert Carr82d07c92021-05-10 11:36:43 -07001073uint32_t BLASTBufferQueue::getLastTransformHint() const {
1074 if (mSurfaceControl != nullptr) {
1075 return mSurfaceControl->getTransformHint();
1076 } else {
1077 return 0;
1078 }
1079}
1080
chaviw0b020f82021-08-20 12:00:47 -05001081uint64_t BLASTBufferQueue::getLastAcquiredFrameNum() {
1082 std::unique_lock _lock{mMutex};
1083 return mLastAcquiredFrameNumber;
1084}
1085
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001086void BLASTBufferQueue::abandon() {
1087 std::unique_lock _lock{mMutex};
1088 // flush out the shadow queue
1089 while (mNumFrameAvailable > 0) {
1090 acquireAndReleaseBuffer();
1091 }
1092
1093 // Clear submitted buffer states
1094 mNumAcquired = 0;
1095 mSubmitted.clear();
1096 mPendingRelease.clear();
1097
1098 if (!mPendingTransactions.empty()) {
1099 BQA_LOGD("Applying pending transactions on abandon %d",
1100 static_cast<uint32_t>(mPendingTransactions.size()));
1101 SurfaceComposerClient::Transaction t;
1102 mergePendingTransactions(&t, std::numeric_limits<uint64_t>::max() /* frameNumber */);
Robert Carr79dc06a2022-02-22 15:28:59 -08001103 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
1104 t.setApplyToken(mApplyToken).apply(false, true);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001105 }
1106
1107 // Clear sync states
chaviwc1cf4022022-06-03 13:32:33 -05001108 if (!mSyncedFrameNumbers.empty()) {
1109 BQA_LOGD("mSyncedFrameNumbers cleared");
1110 mSyncedFrameNumbers.clear();
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001111 }
1112
1113 if (mSyncTransaction != nullptr) {
1114 BQA_LOGD("mSyncTransaction cleared mAcquireSingleBuffer=%s",
1115 mAcquireSingleBuffer ? "true" : "false");
1116 mSyncTransaction = nullptr;
1117 mAcquireSingleBuffer = false;
1118 }
1119
1120 // abandon buffer queue
1121 if (mBufferItemConsumer != nullptr) {
1122 mBufferItemConsumer->abandon();
1123 mBufferItemConsumer->setFrameAvailableListener(nullptr);
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001124 }
1125 mBufferItemConsumer = nullptr;
1126 mConsumer = nullptr;
1127 mProducer = nullptr;
1128}
1129
1130bool BLASTBufferQueue::isSameSurfaceControl(const sp<SurfaceControl>& surfaceControl) const {
1131 std::unique_lock _lock{mMutex};
1132 return SurfaceControl::isSameSurface(mSurfaceControl, surfaceControl);
1133}
1134
Robert Carr4c1b6462021-12-21 10:30:50 -08001135void BLASTBufferQueue::setTransactionHangCallback(std::function<void(bool)> callback) {
1136 std::unique_lock _lock{mMutex};
1137 mTransactionHangCallback = callback;
1138}
1139
Robert Carr78c25dd2019-08-15 14:10:33 -07001140} // namespace android