blob: 422002dfdce34663c471ad89b1224c3b7c151851 [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(
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000170 [&](const std::string& reason) {
171 std::function<void(const std::string&)> callbackCopy;
172 {
173 std::unique_lock _lock{mMutex};
174 callbackCopy = mTransactionHangCallback;
175 }
176 if (callbackCopy) callbackCopy(reason);
177 },
178 this);
Robert Carr4c1b6462021-12-21 10:30:50 -0800179
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800180 BQA_LOGV("BLASTBufferQueue created");
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800181}
182
183BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface,
184 int width, int height, int32_t format)
185 : BLASTBufferQueue(name) {
186 update(surface, width, height, format);
Robert Carr78c25dd2019-08-15 14:10:33 -0700187}
188
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800189BLASTBufferQueue::~BLASTBufferQueue() {
Robert Carr4c1b6462021-12-21 10:30:50 -0800190 TransactionCompletedListener::getInstance()->removeQueueStallListener(this);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800191 if (mPendingTransactions.empty()) {
192 return;
193 }
194 BQA_LOGE("Applying pending transactions on dtor %d",
195 static_cast<uint32_t>(mPendingTransactions.size()));
196 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800197 mergePendingTransactions(&t, std::numeric_limits<uint64_t>::max() /* frameNumber */);
Robert Carr79dc06a2022-02-22 15:28:59 -0800198 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
199 t.setApplyToken(mApplyToken).apply(false, true);
chaviw3b4bdcf2022-03-17 09:27:03 -0500200
201 if (mTransactionReadyCallback) {
202 mTransactionReadyCallback(mSyncTransaction);
203 }
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800204}
205
chaviw565ee542021-01-14 10:21:23 -0800206void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height,
Vishnu Naird2aaab12022-02-10 14:49:09 -0800207 int32_t format) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800208 LOG_ALWAYS_FATAL_IF(surface == nullptr, "BLASTBufferQueue: mSurfaceControl must not be NULL");
209
Robert Carr78c25dd2019-08-15 14:10:33 -0700210 std::unique_lock _lock{mMutex};
chaviw565ee542021-01-14 10:21:23 -0800211 if (mFormat != format) {
212 mFormat = format;
chaviw497e81c2021-02-04 17:09:47 -0800213 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
chaviw565ee542021-01-14 10:21:23 -0800214 }
215
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800216 const bool surfaceControlChanged = !SurfaceControl::isSameSurface(mSurfaceControl, surface);
Vishnu Nairab066512022-01-04 22:28:00 +0000217 if (surfaceControlChanged && mSurfaceControl != nullptr) {
218 BQA_LOGD("Updating SurfaceControl without recreating BBQ");
219 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800220 bool applyTransaction = false;
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800221
Vishnu Nair5fa91c22021-06-29 14:30:48 -0700222 // Always update the native object even though they might have the same layer handle, so we can
223 // get the updated transform hint from WM.
224 mSurfaceControl = surface;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800225 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800226 if (surfaceControlChanged) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800227 t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure,
228 layer_state_t::eEnableBackpressure);
229 applyTransaction = true;
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800230 }
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800231 mTransformHint = mSurfaceControl->getTransformHint();
232 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Naira4fbca52021-07-07 16:52:34 -0700233 BQA_LOGV("update width=%d height=%d format=%d mTransformHint=%d", width, height, format,
234 mTransformHint);
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800235
Vishnu Nairea0de002020-11-17 17:42:37 -0800236 ui::Size newSize(width, height);
237 if (mRequestedSize != newSize) {
238 mRequestedSize.set(newSize);
239 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000240 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800241 // If the buffer supports scaling, update the frame immediately since the client may
242 // want to scale the existing buffer to the new size.
243 mSize = mRequestedSize;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800244 if (mUpdateDestinationFrame) {
245 t.setDestinationFrame(mSurfaceControl, Rect(newSize));
246 applyTransaction = true;
247 }
Vishnu Nair53c936c2020-12-03 11:46:37 -0800248 }
Robert Carrfc416512020-04-02 12:32:44 -0700249 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800250 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800251 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
252 t.setApplyToken(mApplyToken).apply(false, true);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800253 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700254}
255
chaviwd7deef72021-10-06 11:53:40 -0500256static std::optional<SurfaceControlStats> findMatchingStat(
257 const std::vector<SurfaceControlStats>& stats, const sp<SurfaceControl>& sc) {
258 for (auto stat : stats) {
259 if (SurfaceControl::isSameSurface(sc, stat.surfaceControl)) {
260 return stat;
261 }
262 }
263 return std::nullopt;
264}
265
266static void transactionCommittedCallbackThunk(void* context, nsecs_t latchTime,
267 const sp<Fence>& presentFence,
268 const std::vector<SurfaceControlStats>& stats) {
269 if (context == nullptr) {
270 return;
271 }
272 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
273 bq->transactionCommittedCallback(latchTime, presentFence, stats);
274}
275
276void BLASTBufferQueue::transactionCommittedCallback(nsecs_t /*latchTime*/,
277 const sp<Fence>& /*presentFence*/,
278 const std::vector<SurfaceControlStats>& stats) {
279 {
280 std::unique_lock _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600281 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500282 BQA_LOGV("transactionCommittedCallback");
283 if (!mSurfaceControlsWithPendingCallback.empty()) {
284 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
285 std::optional<SurfaceControlStats> stat = findMatchingStat(stats, pendingSC);
286 if (stat) {
287 uint64_t currFrameNumber = stat->frameEventStats.frameNumber;
288
289 // We need to check if we were waiting for a transaction callback in order to
290 // process any pending buffers and unblock. It's possible to get transaction
chaviwc1cf4022022-06-03 13:32:33 -0500291 // callbacks for previous requests so we need to ensure that there are no pending
292 // frame numbers that were in a sync. We remove the frame from mSyncedFrameNumbers
293 // set and then check if it's empty. If there are no more pending syncs, we can
294 // proceed with flushing the shadow queue.
295 // We also want to check if mSyncTransaction is null because it's possible another
chaviwd7deef72021-10-06 11:53:40 -0500296 // sync request came in while waiting, but it hasn't started processing yet. In that
297 // case, we don't actually want to flush the frames in between since they will get
298 // processed and merged with the sync transaction and released earlier than if they
299 // were sent to SF
chaviwc1cf4022022-06-03 13:32:33 -0500300 mSyncedFrameNumbers.erase(currFrameNumber);
301 if (mSyncedFrameNumbers.empty() && mSyncTransaction == nullptr) {
chaviwd7deef72021-10-06 11:53:40 -0500302 flushShadowQueue();
303 }
304 } else {
chaviw768bfa02021-11-01 09:50:57 -0500305 BQA_LOGE("Failed to find matching SurfaceControl in transactionCommittedCallback");
chaviwd7deef72021-10-06 11:53:40 -0500306 }
307 } else {
308 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
309 "empty.");
310 }
chaviwd7deef72021-10-06 11:53:40 -0500311 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;
Vishnu Nair71fcf912022-10-18 09:14:20 -0700338 if (stat.transformHint) {
339 mTransformHint = *stat.transformHint;
340 mBufferItemConsumer->setTransformHint(mTransformHint);
341 BQA_LOGV("updated mTransformHint=%d", mTransformHint);
342 }
Vishnu Nairde66dc72021-06-17 17:54:41 -0700343 // Update frametime stamps if the frame was latched and presented, indicated by a
344 // valid latch time.
345 if (stat.latchTime > 0) {
346 mBufferItemConsumer
347 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
348 stat.frameEventStats.refreshStartTime,
349 stat.frameEventStats.gpuCompositionDoneFence,
350 stat.presentFence, stat.previousReleaseFence,
351 stat.frameEventStats.compositorTiming,
352 stat.latchTime,
353 stat.frameEventStats.dequeueReadyTime);
354 }
Robert Carr405e2f62021-12-31 16:59:34 -0800355 auto currFrameNumber = stat.frameEventStats.frameNumber;
356 std::vector<ReleaseCallbackId> staleReleases;
357 for (const auto& [key, value]: mSubmitted) {
358 if (currFrameNumber > key.framenumber) {
359 staleReleases.push_back(key);
360 }
361 }
362 for (const auto& staleRelease : staleReleases) {
Robert Carr405e2f62021-12-31 16:59:34 -0800363 releaseBufferCallbackLocked(staleRelease,
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700364 stat.previousReleaseFence
365 ? stat.previousReleaseFence
366 : Fence::NO_FENCE,
367 stat.currentMaxAcquiredBufferCount,
368 true /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800369 }
chaviwd7deef72021-10-06 11:53:40 -0500370 } else {
chaviw768bfa02021-11-01 09:50:57 -0500371 BQA_LOGE("Failed to find matching SurfaceControl in transactionCallback");
chaviw42026162021-04-16 15:46:12 -0500372 }
373 } else {
374 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
375 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800376 }
chaviw71c2cc42020-10-23 16:42:02 -0700377
chaviw71c2cc42020-10-23 16:42:02 -0700378 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700379 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700380}
381
Vishnu Nair1506b182021-02-22 14:35:15 -0800382// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
383// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
384// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
385// Otherwise, this is a no-op.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700386static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, const ReleaseCallbackId& id,
chaviw69058fb2021-09-27 09:37:30 -0500387 const sp<Fence>& releaseFence,
388 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800389 sp<BLASTBufferQueue> blastBufferQueue = context.promote();
Vishnu Nair1506b182021-02-22 14:35:15 -0800390 if (blastBufferQueue) {
chaviw69058fb2021-09-27 09:37:30 -0500391 blastBufferQueue->releaseBufferCallback(id, releaseFence, currentMaxAcquiredBufferCount);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700392 } else {
393 ALOGV("releaseBufferCallbackThunk %s blastBufferQueue is dead", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800394 }
395}
396
chaviwd7deef72021-10-06 11:53:40 -0500397void BLASTBufferQueue::flushShadowQueue() {
398 BQA_LOGV("flushShadowQueue");
399 int numFramesToFlush = mNumFrameAvailable;
400 while (numFramesToFlush > 0) {
401 acquireNextBufferLocked(std::nullopt);
402 numFramesToFlush--;
403 }
404}
405
chaviw69058fb2021-09-27 09:37:30 -0500406void BLASTBufferQueue::releaseBufferCallback(
407 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
408 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
chaviw57ae4b22022-02-03 16:51:39 -0600409 BBQ_TRACE();
Robert Carr405e2f62021-12-31 16:59:34 -0800410
Vishnu Nair1506b182021-02-22 14:35:15 -0800411 std::unique_lock _lock{mMutex};
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700412 releaseBufferCallbackLocked(id, releaseFence, currentMaxAcquiredBufferCount,
413 false /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800414}
415
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700416void BLASTBufferQueue::releaseBufferCallbackLocked(
417 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
418 std::optional<uint32_t> currentMaxAcquiredBufferCount, bool fakeRelease) {
Robert Carr405e2f62021-12-31 16:59:34 -0800419 ATRACE_CALL();
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700420 BQA_LOGV("releaseBufferCallback %s", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800421
Ady Abraham899dcdb2021-06-15 16:56:21 -0700422 // Calculate how many buffers we need to hold before we release them back
423 // to the buffer queue. This will prevent higher latency when we are running
424 // on a lower refresh rate than the max supported. We only do that for EGL
425 // clients as others don't care about latency
426 const bool isEGL = [&] {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700427 const auto it = mSubmitted.find(id);
Ady Abraham899dcdb2021-06-15 16:56:21 -0700428 return it != mSubmitted.end() && it->second.mApi == NATIVE_WINDOW_API_EGL;
429 }();
430
chaviw69058fb2021-09-27 09:37:30 -0500431 if (currentMaxAcquiredBufferCount) {
432 mCurrentMaxAcquiredBufferCount = *currentMaxAcquiredBufferCount;
433 }
434
liulijunf90df632022-11-14 14:24:48 +0800435 const uint32_t numPendingBuffersToHold =
436 isEGL ? std::max(0, mMaxAcquiredBuffers - (int32_t)mCurrentMaxAcquiredBufferCount) : 0;
Robert Carr405e2f62021-12-31 16:59:34 -0800437
438 auto rb = ReleasedBuffer{id, releaseFence};
439 if (std::find(mPendingRelease.begin(), mPendingRelease.end(), rb) == mPendingRelease.end()) {
440 mPendingRelease.emplace_back(rb);
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700441 if (fakeRelease) {
442 BQA_LOGE("Faking releaseBufferCallback from transactionCompleteCallback %" PRIu64,
443 id.framenumber);
444 BBQ_TRACE("FakeReleaseCallback");
445 }
Robert Carr405e2f62021-12-31 16:59:34 -0800446 }
Ady Abraham899dcdb2021-06-15 16:56:21 -0700447
448 // Release all buffers that are beyond the ones that we need to hold
449 while (mPendingRelease.size() > numPendingBuffersToHold) {
chaviw0acd33a2021-11-02 11:55:37 -0500450 const auto releasedBuffer = mPendingRelease.front();
Ady Abraham899dcdb2021-06-15 16:56:21 -0700451 mPendingRelease.pop_front();
chaviw0acd33a2021-11-02 11:55:37 -0500452 releaseBuffer(releasedBuffer.callbackId, releasedBuffer.releaseFence);
chaviwc1cf4022022-06-03 13:32:33 -0500453 // Don't process the transactions here if mSyncedFrameNumbers is not empty. That means
454 // are still transactions that have sync buffers in them that have not been applied or
455 // dropped. Instead, let onFrameAvailable handle processing them since it will merge with
456 // the syncTransaction.
457 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500458 acquireNextBufferLocked(std::nullopt);
459 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800460 }
461
Ady Abraham899dcdb2021-06-15 16:56:21 -0700462 ATRACE_INT("PendingRelease", mPendingRelease.size());
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700463 ATRACE_INT(mQueuedBufferTrace.c_str(),
464 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
Vishnu Nair1506b182021-02-22 14:35:15 -0800465 mCallbackCV.notify_all();
466}
467
chaviw0acd33a2021-11-02 11:55:37 -0500468void BLASTBufferQueue::releaseBuffer(const ReleaseCallbackId& callbackId,
469 const sp<Fence>& releaseFence) {
470 auto it = mSubmitted.find(callbackId);
471 if (it == mSubmitted.end()) {
472 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %s",
473 callbackId.to_string().c_str());
474 return;
475 }
476 mNumAcquired--;
chaviw57ae4b22022-02-03 16:51:39 -0600477 BBQ_TRACE("frame=%" PRIu64, callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500478 BQA_LOGV("released %s", callbackId.to_string().c_str());
479 mBufferItemConsumer->releaseBuffer(it->second, releaseFence);
480 mSubmitted.erase(it);
chaviwc1cf4022022-06-03 13:32:33 -0500481 // Remove the frame number from mSyncedFrameNumbers since we can get a release callback
482 // without getting a transaction committed if the buffer was dropped.
483 mSyncedFrameNumbers.erase(callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500484}
485
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000486status_t BLASTBufferQueue::acquireNextBufferLocked(
chaviwd7deef72021-10-06 11:53:40 -0500487 const std::optional<SurfaceComposerClient::Transaction*> transaction) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800488 // Check if we have frames available and we have not acquired the maximum number of buffers.
489 // Even with this check, the consumer can fail to acquire an additional buffer if the consumer
490 // has already acquired (mMaxAcquiredBuffers + 1) and the new buffer is not droppable. In this
491 // case mBufferItemConsumer->acquireBuffer will return with NO_BUFFER_AVAILABLE.
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000492 if (mNumFrameAvailable == 0) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800493 BQA_LOGV("Can't acquire next buffer. No available frames");
494 return BufferQueue::NO_BUFFER_AVAILABLE;
495 }
496
497 if (mNumAcquired >= (mMaxAcquiredBuffers + 2)) {
498 BQA_LOGV("Can't acquire next buffer. Already acquired max frames %d max:%d + 2",
499 mNumAcquired, mMaxAcquiredBuffers);
500 return BufferQueue::NO_BUFFER_AVAILABLE;
Valerie Haud3b90d22019-11-06 09:37:31 -0800501 }
502
Valerie Haua32c5522019-12-09 10:11:08 -0800503 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700504 BQA_LOGE("ERROR : surface control is null");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000505 return NAME_NOT_FOUND;
Valerie Haud3b90d22019-11-06 09:37:31 -0800506 }
507
Robert Carr78c25dd2019-08-15 14:10:33 -0700508 SurfaceComposerClient::Transaction localTransaction;
509 bool applyTransaction = true;
510 SurfaceComposerClient::Transaction* t = &localTransaction;
chaviwd7deef72021-10-06 11:53:40 -0500511 if (transaction) {
512 t = *transaction;
Robert Carr78c25dd2019-08-15 14:10:33 -0700513 applyTransaction = false;
514 }
515
Valerie Haua32c5522019-12-09 10:11:08 -0800516 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800517
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800518 status_t status =
519 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800520 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
521 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000522 return status;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800523 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700524 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000525 return status;
Robert Carr78c25dd2019-08-15 14:10:33 -0700526 }
chaviw57ae4b22022-02-03 16:51:39 -0600527
Valerie Haua32c5522019-12-09 10:11:08 -0800528 auto buffer = bufferItem.mGraphicBuffer;
529 mNumFrameAvailable--;
chaviw57ae4b22022-02-03 16:51:39 -0600530 BBQ_TRACE("frame=%" PRIu64, bufferItem.mFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800531
532 if (buffer == nullptr) {
533 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700534 BQA_LOGE("Buffer was empty");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000535 return BAD_VALUE;
Valerie Haua32c5522019-12-09 10:11:08 -0800536 }
537
Vishnu Nair670b3f72020-09-29 17:52:18 -0700538 if (rejectBuffer(bufferItem)) {
Vishnu Naira4fbca52021-07-07 16:52:34 -0700539 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d "
Vishnu Nairea0de002020-11-17 17:42:37 -0800540 "buffer{size=%dx%d transform=%d}",
541 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
542 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
543 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000544 return acquireNextBufferLocked(transaction);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700545 }
546
Valerie Haua32c5522019-12-09 10:11:08 -0800547 mNumAcquired++;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700548 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
549 ReleaseCallbackId releaseCallbackId(buffer->getId(), mLastAcquiredFrameNumber);
550 mSubmitted[releaseCallbackId] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700551
Valerie Hau871d6352020-01-29 08:44:02 -0800552 bool needsDisconnect = false;
553 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
554
555 // if producer disconnected before, notify SurfaceFlinger
556 if (needsDisconnect) {
557 t->notifyProducerDisconnect(mSurfaceControl);
558 }
559
Robert Carr78c25dd2019-08-15 14:10:33 -0700560 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
561 incStrong((void*)transactionCallbackThunk);
562
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700563 mSize = mRequestedSize;
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700564 Rect crop = computeCrop(bufferItem);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000565 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
566 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700567 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800568
Vishnu Nair1506b182021-02-22 14:35:15 -0800569 auto releaseBufferCallback =
570 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
chaviw69058fb2021-09-27 09:37:30 -0500571 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
chaviwba4320c2021-09-15 15:20:53 -0500572 sp<Fence> fence = bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE;
chaviw8dd181f2022-01-05 18:36:46 -0600573 t->setBuffer(mSurfaceControl, buffer, fence, bufferItem.mFrameNumber, releaseBufferCallback);
John Reck137069e2020-12-10 22:07:37 -0500574 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
575 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
576 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700577 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviwf2dace72021-11-17 17:36:50 -0600578
chaviw42026162021-04-16 15:46:12 -0500579 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700580
Vishnu Naird2aaab12022-02-10 14:49:09 -0800581 if (mUpdateDestinationFrame) {
582 t->setDestinationFrame(mSurfaceControl, Rect(mSize));
583 } else {
584 const bool ignoreDestinationFrame =
585 bufferItem.mScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE;
586 t->setFlags(mSurfaceControl,
587 ignoreDestinationFrame ? layer_state_t::eIgnoreDestinationFrame : 0,
588 layer_state_t::eIgnoreDestinationFrame);
Vishnu Nair084514a2021-07-30 16:07:42 -0700589 }
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700590 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800591 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800592 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Vishnu Naird2aaab12022-02-10 14:49:09 -0800593 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800594 if (!bufferItem.mIsAutoTimestamp) {
595 t->setDesiredPresentTime(bufferItem.mTimestamp);
596 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700597
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800598 // Drop stale frame timeline infos
599 while (!mPendingFrameTimelines.empty() &&
600 mPendingFrameTimelines.front().first < bufferItem.mFrameNumber) {
601 ATRACE_FORMAT_INSTANT("dropping stale frameNumber: %" PRIu64 " vsyncId: %" PRId64,
602 mPendingFrameTimelines.front().first,
603 mPendingFrameTimelines.front().second.vsyncId);
604 mPendingFrameTimelines.pop();
605 }
606
607 if (!mPendingFrameTimelines.empty() &&
608 mPendingFrameTimelines.front().first == bufferItem.mFrameNumber) {
609 ATRACE_FORMAT_INSTANT("Transaction::setFrameTimelineInfo frameNumber: %" PRIu64
610 " vsyncId: %" PRId64,
611 bufferItem.mFrameNumber,
612 mPendingFrameTimelines.front().second.vsyncId);
613 t->setFrameTimelineInfo(mPendingFrameTimelines.front().second);
614 mPendingFrameTimelines.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100615 }
616
Vishnu Nairadf632b2021-01-07 14:05:08 -0800617 {
618 std::unique_lock _lock{mTimestampMutex};
619 auto dequeueTime = mDequeueTimestamps.find(buffer->getId());
620 if (dequeueTime != mDequeueTimestamps.end()) {
621 Parcel p;
622 p.writeInt64(dequeueTime->second);
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800623 t->setMetadata(mSurfaceControl, gui::METADATA_DEQUEUE_TIME, p);
Vishnu Nairadf632b2021-01-07 14:05:08 -0800624 mDequeueTimestamps.erase(dequeueTime);
625 }
626 }
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800627
chaviw6a195272021-09-03 16:14:25 -0500628 mergePendingTransactions(t, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700629 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800630 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
631 t->setApplyToken(mApplyToken).apply(false, true);
632 mAppliedLastTransaction = true;
633 mLastAppliedFrameNumber = bufferItem.mFrameNumber;
634 } else {
635 t->setBufferHasBarrier(mSurfaceControl, mLastAppliedFrameNumber);
636 mAppliedLastTransaction = false;
Robert Carr78c25dd2019-08-15 14:10:33 -0700637 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700638
chaviwd7deef72021-10-06 11:53:40 -0500639 BQA_LOGV("acquireNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800640 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
Vishnu Naira4fbca52021-07-07 16:52:34 -0700641 " graphicBufferId=%" PRIu64 "%s transform=%d",
chaviw3277faf2021-05-19 16:45:23 -0500642 mSize.width, mSize.height, bufferItem.mFrameNumber, boolToString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800643 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700644 static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(),
Vishnu Naira4fbca52021-07-07 16:52:34 -0700645 bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000646 return OK;
Robert Carr78c25dd2019-08-15 14:10:33 -0700647}
648
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800649Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
650 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800651 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800652 }
653 return item.mCrop;
654}
655
chaviwd7deef72021-10-06 11:53:40 -0500656void BLASTBufferQueue::acquireAndReleaseBuffer() {
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000657 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500658 BufferItem bufferItem;
chaviw6ebdf5f2021-10-14 11:57:22 -0500659 status_t status =
660 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
661 if (status != OK) {
662 BQA_LOGE("Failed to acquire a buffer in acquireAndReleaseBuffer, err=%s",
663 statusToString(status).c_str());
664 return;
665 }
chaviwd7deef72021-10-06 11:53:40 -0500666 mNumFrameAvailable--;
chaviw6ebdf5f2021-10-14 11:57:22 -0500667 mBufferItemConsumer->releaseBuffer(bufferItem, bufferItem.mFence);
chaviwd7deef72021-10-06 11:53:40 -0500668}
669
Vishnu Nairaef1de92020-10-22 12:15:53 -0700670void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000671 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
672 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
chaviwc1cf4022022-06-03 13:32:33 -0500673
Tianhao Yao4861b102022-02-03 20:18:35 +0000674 {
Tianhao Yao4861b102022-02-03 20:18:35 +0000675 std::unique_lock _lock{mMutex};
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000676 BBQ_TRACE();
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800677
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800678 bool waitForTransactionCallback = !mSyncedFrameNumbers.empty();
Tianhao Yao4861b102022-02-03 20:18:35 +0000679 const bool syncTransactionSet = mTransactionReadyCallback != nullptr;
680 BQA_LOGV("onFrameAvailable-start syncTransactionSet=%s", boolToString(syncTransactionSet));
Valerie Haud3b90d22019-11-06 09:37:31 -0800681
Tianhao Yao4861b102022-02-03 20:18:35 +0000682 if (syncTransactionSet) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000683 // If we are going to re-use the same mSyncTransaction, release the buffer that may
684 // already be set in the Transaction. This is to allow us a free slot early to continue
685 // processing a new buffer.
686 if (!mAcquireSingleBuffer) {
687 auto bufferData = mSyncTransaction->getAndClearBuffer(mSurfaceControl);
688 if (bufferData) {
689 BQA_LOGD("Releasing previous buffer when syncing: framenumber=%" PRIu64,
690 bufferData->frameNumber);
691 releaseBuffer(bufferData->generateReleaseCallbackId(),
692 bufferData->acquireFence);
Tianhao Yao4861b102022-02-03 20:18:35 +0000693 }
694 }
chaviw0acd33a2021-11-02 11:55:37 -0500695
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000696 if (waitForTransactionCallback) {
697 // We are waiting on a previous sync's transaction callback so allow another sync
698 // transaction to proceed.
699 //
700 // We need to first flush out the transactions that were in between the two syncs.
701 // We do this by merging them into mSyncTransaction so any buffer merging will get
702 // a release callback invoked.
703 while (mNumFrameAvailable > 0) {
704 // flush out the shadow queue
705 acquireAndReleaseBuffer();
706 }
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800707 } else {
708 // Make sure the frame available count is 0 before proceeding with a sync to ensure
709 // the correct frame is used for the sync. The only way mNumFrameAvailable would be
710 // greater than 0 is if we already ran out of buffers previously. This means we
711 // need to flush the buffers before proceeding with the sync.
712 while (mNumFrameAvailable > 0) {
713 BQA_LOGD("waiting until no queued buffers");
714 mCallbackCV.wait(_lock);
715 }
chaviwd7deef72021-10-06 11:53:40 -0500716 }
717 }
718
Tianhao Yao4861b102022-02-03 20:18:35 +0000719 // add to shadow queue
720 mNumFrameAvailable++;
chaviwc1cf4022022-06-03 13:32:33 -0500721 if (waitForTransactionCallback && mNumFrameAvailable >= 2) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000722 acquireAndReleaseBuffer();
723 }
724 ATRACE_INT(mQueuedBufferTrace.c_str(),
725 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
726
727 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " syncTransactionSet=%s",
728 item.mFrameNumber, boolToString(syncTransactionSet));
729
730 if (syncTransactionSet) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800731 // Add to mSyncedFrameNumbers before waiting in case any buffers are released
732 // while waiting for a free buffer. The release and commit callback will try to
733 // acquire buffers if there are any available, but we don't want it to acquire
734 // in the case where a sync transaction wants the buffer.
735 mSyncedFrameNumbers.emplace(item.mFrameNumber);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000736 // If there's no available buffer and we're in a sync transaction, we need to wait
737 // instead of returning since we guarantee a buffer will be acquired for the sync.
738 while (acquireNextBufferLocked(mSyncTransaction) == BufferQueue::NO_BUFFER_AVAILABLE) {
739 BQA_LOGD("waiting for available buffer");
740 mCallbackCV.wait(_lock);
741 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000742
743 // Only need a commit callback when syncing to ensure the buffer that's synced has been
744 // sent to SF
745 incStrong((void*)transactionCommittedCallbackThunk);
746 mSyncTransaction->addTransactionCommittedCallback(transactionCommittedCallbackThunk,
747 static_cast<void*>(this));
Tianhao Yao4861b102022-02-03 20:18:35 +0000748 if (mAcquireSingleBuffer) {
749 prevCallback = mTransactionReadyCallback;
750 prevTransaction = mSyncTransaction;
751 mTransactionReadyCallback = nullptr;
752 mSyncTransaction = nullptr;
753 }
chaviwc1cf4022022-06-03 13:32:33 -0500754 } else if (!waitForTransactionCallback) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000755 acquireNextBufferLocked(std::nullopt);
Valerie Hau0188adf2020-02-13 08:29:20 -0800756 }
757 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000758 if (prevCallback) {
759 prevCallback(prevTransaction);
chaviwd7deef72021-10-06 11:53:40 -0500760 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800761}
762
Vishnu Nairaef1de92020-10-22 12:15:53 -0700763void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
764 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
765 // Do nothing since we are not storing unacquired buffer items locally.
766}
767
Vishnu Nairadf632b2021-01-07 14:05:08 -0800768void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
769 std::unique_lock _lock{mTimestampMutex};
770 mDequeueTimestamps[bufferId] = systemTime();
771};
772
773void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
774 std::unique_lock _lock{mTimestampMutex};
775 mDequeueTimestamps.erase(bufferId);
776};
777
Tianhao Yao4861b102022-02-03 20:18:35 +0000778void BLASTBufferQueue::syncNextTransaction(
779 std::function<void(SurfaceComposerClient::Transaction*)> callback,
780 bool acquireSingleBuffer) {
chaviw57ae4b22022-02-03 16:51:39 -0600781 BBQ_TRACE();
chaviw3b4bdcf2022-03-17 09:27:03 -0500782
783 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
784 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
785
786 {
787 std::lock_guard _lock{mMutex};
788 // We're about to overwrite the previous call so we should invoke that callback
789 // immediately.
790 if (mTransactionReadyCallback) {
791 prevCallback = mTransactionReadyCallback;
792 prevTransaction = mSyncTransaction;
793 }
794
795 mTransactionReadyCallback = callback;
796 if (callback) {
797 mSyncTransaction = new SurfaceComposerClient::Transaction();
798 } else {
799 mSyncTransaction = nullptr;
800 }
801 mAcquireSingleBuffer = mTransactionReadyCallback ? acquireSingleBuffer : true;
Tianhao Yao4861b102022-02-03 20:18:35 +0000802 }
chaviw3b4bdcf2022-03-17 09:27:03 -0500803
804 if (prevCallback) {
805 prevCallback(prevTransaction);
806 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000807}
808
809void BLASTBufferQueue::stopContinuousSyncTransaction() {
810 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
811 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
812 {
813 std::lock_guard _lock{mMutex};
814 bool invokeCallback = mTransactionReadyCallback && !mAcquireSingleBuffer;
815 if (invokeCallback) {
816 prevCallback = mTransactionReadyCallback;
817 prevTransaction = mSyncTransaction;
818 }
819 mTransactionReadyCallback = nullptr;
820 mSyncTransaction = nullptr;
821 mAcquireSingleBuffer = true;
822 }
823 if (prevCallback) {
824 prevCallback(prevTransaction);
825 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700826}
827
Vishnu Nairea0de002020-11-17 17:42:37 -0800828bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700829 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
830 // Only reject buffers if scaling mode is freeze.
831 return false;
832 }
833
Vishnu Naire1a42322020-10-02 17:42:04 -0700834 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
835 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
836
837 // Take the buffer's orientation into account
838 if (item.mTransform & ui::Transform::ROT_90) {
839 std::swap(bufWidth, bufHeight);
840 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800841 ui::Size bufferSize(bufWidth, bufHeight);
842 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800843 return false;
844 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700845
Vishnu Nair670b3f72020-09-29 17:52:18 -0700846 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800847 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700848}
Vishnu Nairbf255772020-10-16 10:54:41 -0700849
Robert Carr05086b22020-10-13 18:22:51 -0700850class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700851private:
Vishnu Nair95b6d512021-08-30 15:31:08 -0700852 std::mutex mMutex;
Robert Carr9c006e02020-10-14 13:41:57 -0700853 sp<BLASTBufferQueue> mBbq;
Vishnu Nair95b6d512021-08-30 15:31:08 -0700854 bool mDestroyed = false;
855
Robert Carr05086b22020-10-13 18:22:51 -0700856public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700857 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
858 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
859 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700860
Robert Carr05086b22020-10-13 18:22:51 -0700861 void allocateBuffers() override {
862 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
863 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
864 auto gbp = getIGraphicBufferProducer();
865 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
866 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
867 gbp->allocateBuffers(reqWidth, reqHeight,
868 reqFormat, reqUsage);
869
870 }).detach();
871 }
Robert Carr9c006e02020-10-14 13:41:57 -0700872
Marin Shalamanovc5986772021-03-16 16:09:49 +0100873 status_t setFrameRate(float frameRate, int8_t compatibility,
874 int8_t changeFrameRateStrategy) override {
Vishnu Nair95b6d512021-08-30 15:31:08 -0700875 std::unique_lock _lock{mMutex};
876 if (mDestroyed) {
877 return DEAD_OBJECT;
878 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100879 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
880 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700881 return BAD_VALUE;
882 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100883 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700884 }
Robert Carr9b611b72020-10-19 12:00:23 -0700885
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800886 status_t setFrameTimelineInfo(uint64_t frameNumber,
887 const FrameTimelineInfo& frameTimelineInfo) override {
Vishnu Nair95b6d512021-08-30 15:31:08 -0700888 std::unique_lock _lock{mMutex};
889 if (mDestroyed) {
890 return DEAD_OBJECT;
891 }
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800892 return mBbq->setFrameTimelineInfo(frameNumber, frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700893 }
Vishnu Nair95b6d512021-08-30 15:31:08 -0700894
895 void destroy() override {
896 Surface::destroy();
897
898 std::unique_lock _lock{mMutex};
899 mDestroyed = true;
900 mBbq = nullptr;
901 }
Robert Carr05086b22020-10-13 18:22:51 -0700902};
903
Robert Carr9c006e02020-10-14 13:41:57 -0700904// TODO: Can we coalesce this with frame updates? Need to confirm
905// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200906status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
907 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700908 std::unique_lock _lock{mMutex};
909 SurfaceComposerClient::Transaction t;
910
Marin Shalamanov46084422020-10-13 12:33:42 +0200911 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700912}
913
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800914status_t BLASTBufferQueue::setFrameTimelineInfo(uint64_t frameNumber,
915 const FrameTimelineInfo& frameTimelineInfo) {
916 ATRACE_FORMAT("%s(%s) frameNumber: %" PRIu64 " vsyncId: %" PRId64, __func__, mName.c_str(),
917 frameNumber, frameTimelineInfo.vsyncId);
Robert Carr9b611b72020-10-19 12:00:23 -0700918 std::unique_lock _lock{mMutex};
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800919 mPendingFrameTimelines.push({frameNumber, frameTimelineInfo});
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100920 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700921}
922
Hongguang Chen621ec582021-02-16 15:42:35 -0800923void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
924 std::unique_lock _lock{mMutex};
925 SurfaceComposerClient::Transaction t;
926
927 t.setSidebandStream(mSurfaceControl, stream).apply();
928}
929
Vishnu Nair992496b2020-10-22 17:27:21 -0700930sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
931 std::unique_lock _lock{mMutex};
932 sp<IBinder> scHandle = nullptr;
933 if (includeSurfaceControlHandle && mSurfaceControl) {
934 scHandle = mSurfaceControl->getHandle();
935 }
936 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700937}
938
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800939void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
940 uint64_t frameNumber) {
941 std::lock_guard _lock{mMutex};
942 if (mLastAcquiredFrameNumber >= frameNumber) {
943 // Apply the transaction since we have already acquired the desired frame.
944 t->apply();
945 } else {
chaviwaad6cf52021-03-23 17:27:20 -0500946 mPendingTransactions.emplace_back(frameNumber, *t);
947 // Clear the transaction so it can't be applied elsewhere.
948 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800949 }
950}
951
chaviw6a195272021-09-03 16:14:25 -0500952void BLASTBufferQueue::applyPendingTransactions(uint64_t frameNumber) {
953 std::lock_guard _lock{mMutex};
954
955 SurfaceComposerClient::Transaction t;
956 mergePendingTransactions(&t, frameNumber);
Robert Carr79dc06a2022-02-22 15:28:59 -0800957 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
958 t.setApplyToken(mApplyToken).apply(false, true);
chaviw6a195272021-09-03 16:14:25 -0500959}
960
961void BLASTBufferQueue::mergePendingTransactions(SurfaceComposerClient::Transaction* t,
962 uint64_t frameNumber) {
963 auto mergeTransaction =
964 [&t, currentFrameNumber = frameNumber](
965 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
966 auto& [targetFrameNumber, transaction] = pendingTransaction;
967 if (currentFrameNumber < targetFrameNumber) {
968 return false;
969 }
970 t->merge(std::move(transaction));
971 return true;
972 };
973
974 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
975 mPendingTransactions.end(), mergeTransaction),
976 mPendingTransactions.end());
977}
978
chaviwd84085a2022-02-08 11:07:04 -0600979SurfaceComposerClient::Transaction* BLASTBufferQueue::gatherPendingTransactions(
980 uint64_t frameNumber) {
981 std::lock_guard _lock{mMutex};
982 SurfaceComposerClient::Transaction* t = new SurfaceComposerClient::Transaction();
983 mergePendingTransactions(t, frameNumber);
984 return t;
985}
986
Vishnu Nair89496122020-12-14 17:14:53 -0800987// Maintains a single worker thread per process that services a list of runnables.
988class AsyncWorker : public Singleton<AsyncWorker> {
989private:
990 std::thread mThread;
991 bool mDone = false;
992 std::deque<std::function<void()>> mRunnables;
993 std::mutex mMutex;
994 std::condition_variable mCv;
995 void run() {
996 std::unique_lock<std::mutex> lock(mMutex);
997 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -0800998 while (!mRunnables.empty()) {
Vishnu Nair51e4dc82021-10-01 15:32:33 -0700999 std::deque<std::function<void()>> runnables = std::move(mRunnables);
1000 mRunnables.clear();
1001 lock.unlock();
1002 // Run outside the lock since the runnable might trigger another
1003 // post to the async worker.
1004 execute(runnables);
1005 lock.lock();
Vishnu Nair89496122020-12-14 17:14:53 -08001006 }
Wonsik Kim567533e2021-05-04 19:31:29 -07001007 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -08001008 }
1009 }
1010
Vishnu Nair51e4dc82021-10-01 15:32:33 -07001011 void execute(std::deque<std::function<void()>>& runnables) {
1012 while (!runnables.empty()) {
1013 std::function<void()> runnable = runnables.front();
1014 runnables.pop_front();
1015 runnable();
1016 }
1017 }
1018
Vishnu Nair89496122020-12-14 17:14:53 -08001019public:
1020 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
1021
1022 ~AsyncWorker() {
1023 mDone = true;
1024 mCv.notify_all();
1025 if (mThread.joinable()) {
1026 mThread.join();
1027 }
1028 }
1029
1030 void post(std::function<void()> runnable) {
1031 std::unique_lock<std::mutex> lock(mMutex);
1032 mRunnables.emplace_back(std::move(runnable));
1033 mCv.notify_one();
1034 }
1035};
1036ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
1037
1038// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
1039class AsyncProducerListener : public BnProducerListener {
1040private:
1041 const sp<IProducerListener> mListener;
1042
1043public:
1044 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
1045
1046 void onBufferReleased() override {
1047 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
1048 }
1049
1050 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
1051 AsyncWorker::getInstance().post(
1052 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
1053 }
1054};
1055
1056// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
1057// can be non-blocking when the producer is in the client process.
1058class BBQBufferQueueProducer : public BufferQueueProducer {
1059public:
1060 BBQBufferQueueProducer(const sp<BufferQueueCore>& core)
1061 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/) {}
1062
1063 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
1064 QueueBufferOutput* output) override {
1065 if (!listener) {
1066 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
1067 }
1068
1069 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
1070 producerControlledByApp, output);
1071 }
Vishnu Nair17dde612020-12-28 11:39:59 -08001072
1073 int query(int what, int* value) override {
1074 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
1075 *value = 1;
1076 return NO_ERROR;
1077 }
1078 return BufferQueueProducer::query(what, value);
1079 }
Vishnu Nair89496122020-12-14 17:14:53 -08001080};
1081
1082// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
1083// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
1084// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
1085// we can deadlock.
1086void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
1087 sp<IGraphicBufferConsumer>* outConsumer) {
1088 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
1089 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
1090
1091 sp<BufferQueueCore> core(new BufferQueueCore());
1092 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
1093
1094 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core));
1095 LOG_ALWAYS_FATAL_IF(producer == nullptr,
1096 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
1097
Vishnu Nair8b30dd12021-01-25 14:16:54 -08001098 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
1099 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -08001100 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
1101 "BLASTBufferQueue: failed to create BufferQueueConsumer");
1102
1103 *outProducer = producer;
1104 *outConsumer = consumer;
1105}
1106
chaviw497e81c2021-02-04 17:09:47 -08001107PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
1108 PixelFormat convertedFormat = format;
1109 switch (format) {
1110 case PIXEL_FORMAT_TRANSPARENT:
1111 case PIXEL_FORMAT_TRANSLUCENT:
1112 convertedFormat = PIXEL_FORMAT_RGBA_8888;
1113 break;
1114 case PIXEL_FORMAT_OPAQUE:
1115 convertedFormat = PIXEL_FORMAT_RGBX_8888;
1116 break;
1117 }
1118 return convertedFormat;
1119}
1120
Robert Carr82d07c92021-05-10 11:36:43 -07001121uint32_t BLASTBufferQueue::getLastTransformHint() const {
1122 if (mSurfaceControl != nullptr) {
1123 return mSurfaceControl->getTransformHint();
1124 } else {
1125 return 0;
1126 }
1127}
1128
chaviw0b020f82021-08-20 12:00:47 -05001129uint64_t BLASTBufferQueue::getLastAcquiredFrameNum() {
1130 std::unique_lock _lock{mMutex};
1131 return mLastAcquiredFrameNumber;
1132}
1133
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001134bool BLASTBufferQueue::isSameSurfaceControl(const sp<SurfaceControl>& surfaceControl) const {
1135 std::unique_lock _lock{mMutex};
1136 return SurfaceControl::isSameSurface(mSurfaceControl, surfaceControl);
1137}
1138
Patrick Williamsf1e5df12022-10-17 21:37:42 +00001139void BLASTBufferQueue::setTransactionHangCallback(
1140 std::function<void(const std::string&)> callback) {
Robert Carr4c1b6462021-12-21 10:30:50 -08001141 std::unique_lock _lock{mMutex};
1142 mTransactionHangCallback = callback;
1143}
1144
Robert Carr78c25dd2019-08-15 14:10:33 -07001145} // namespace android