blob: de64ea847670e2363b8c1d56013af27bc20e4da9 [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
Chavi Weingartene0237bb2023-02-06 21:48:32 +000038#include <android-base/thread_annotations.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070039#include <chrono>
40
41using namespace std::chrono_literals;
42
Vishnu Nairdab94092020-09-29 16:09:04 -070043namespace {
chaviw3277faf2021-05-19 16:45:23 -050044inline const char* boolToString(bool b) {
Vishnu Nairdab94092020-09-29 16:09:04 -070045 return b ? "true" : "false";
46}
47} // namespace
48
Robert Carr78c25dd2019-08-15 14:10:33 -070049namespace android {
50
Vishnu Nairdab94092020-09-29 16:09:04 -070051// Macros to include adapter info in log messages
chaviwd7deef72021-10-06 11:53:40 -050052#define BQA_LOGD(x, ...) \
53 ALOGD("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070054#define BQA_LOGV(x, ...) \
55 ALOGV("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairc6f89ee2020-12-11 14:27:32 -080056// enable logs for a single layer
57//#define BQA_LOGV(x, ...) \
58// ALOGV_IF((strstr(mName.c_str(), "SurfaceView") != nullptr), "[%s](f:%u,a:%u) " x, \
59// mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070060#define BQA_LOGE(x, ...) \
61 ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
62
chaviw57ae4b22022-02-03 16:51:39 -060063#define BBQ_TRACE(x, ...) \
64 ATRACE_FORMAT("%s - %s(f:%u,a:%u)" x, __FUNCTION__, mName.c_str(), mNumFrameAvailable, \
65 mNumAcquired, ##__VA_ARGS__)
66
Chavi Weingartene0237bb2023-02-06 21:48:32 +000067#define UNIQUE_LOCK_WITH_ASSERTION(mutex) \
68 std::unique_lock _lock{mutex}; \
69 base::ScopedLockAssertion assumeLocked(mutex);
70
Valerie Hau871d6352020-01-29 08:44:02 -080071void BLASTBufferItemConsumer::onDisconnect() {
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000072 Mutex::Autolock lock(mMutex);
73 mPreviouslyConnected = mCurrentlyConnected;
74 mCurrentlyConnected = false;
75 if (mPreviouslyConnected) {
76 mDisconnectEvents.push(mCurrentFrameNumber);
Valerie Hau871d6352020-01-29 08:44:02 -080077 }
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000078 mFrameEventHistory.onDisconnect();
Valerie Hau871d6352020-01-29 08:44:02 -080079}
80
81void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
82 FrameEventHistoryDelta* outDelta) {
Hongguang Chen621ec582021-02-16 15:42:35 -080083 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080084 if (newTimestamps) {
85 // BufferQueueProducer only adds a new timestamp on
86 // queueBuffer
87 mCurrentFrameNumber = newTimestamps->frameNumber;
88 mFrameEventHistory.addQueue(*newTimestamps);
89 }
90 if (outDelta) {
91 // frame event histories will be processed
92 // only after the producer connects and requests
93 // deltas for the first time. Forward this intent
94 // to SF-side to turn event processing back on
95 mPreviouslyConnected = mCurrentlyConnected;
96 mCurrentlyConnected = true;
97 mFrameEventHistory.getAndResetDelta(outDelta);
98 }
99}
100
101void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
102 const sp<Fence>& glDoneFence,
103 const sp<Fence>& presentFence,
104 const sp<Fence>& prevReleaseFence,
105 CompositorTiming compositorTiming,
106 nsecs_t latchTime, nsecs_t dequeueReadyTime) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800107 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800108
109 // if the producer is not connected, don't bother updating,
110 // the next producer that connects won't access this frame event
111 if (!mCurrentlyConnected) return;
112 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
113 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
114 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
115
116 mFrameEventHistory.addLatch(frameNumber, latchTime);
117 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
118 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
119 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
120 compositorTiming);
121}
122
123void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
124 bool disconnect = false;
Hongguang Chen621ec582021-02-16 15:42:35 -0800125 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800126 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
127 disconnect = true;
128 mDisconnectEvents.pop();
129 }
130 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
131}
132
Hongguang Chen621ec582021-02-16 15:42:35 -0800133void BLASTBufferItemConsumer::onSidebandStreamChanged() {
Ady Abrahamdbca1352021-12-15 11:58:56 -0800134 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
135 if (bbq != nullptr) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800136 sp<NativeHandle> stream = getSidebandStream();
Ady Abrahamdbca1352021-12-15 11:58:56 -0800137 bbq->setSidebandStream(stream);
Hongguang Chen621ec582021-02-16 15:42:35 -0800138 }
139}
140
Vishnu Naird2aaab12022-02-10 14:49:09 -0800141BLASTBufferQueue::BLASTBufferQueue(const std::string& name, bool updateDestinationFrame)
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800142 : mSurfaceControl(nullptr),
143 mSize(1, 1),
Vishnu Nairea0de002020-11-17 17:42:37 -0800144 mRequestedSize(mSize),
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800145 mFormat(PIXEL_FORMAT_RGBA_8888),
Tianhao Yao4861b102022-02-03 20:18:35 +0000146 mTransactionReadyCallback(nullptr),
Vishnu Naird2aaab12022-02-10 14:49:09 -0800147 mSyncTransaction(nullptr),
148 mUpdateDestinationFrame(updateDestinationFrame) {
Vishnu Nair89496122020-12-14 17:14:53 -0800149 createBufferQueue(&mProducer, &mConsumer);
Valerie Hau0889c622020-02-19 15:04:47 -0800150 // since the adapter is in the client process, set dequeue timeout
151 // explicitly so that dequeueBuffer will block
152 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
Valerie Hau65b8e872020-02-13 09:45:14 -0800153
Vishnu Nairdebd1cb2021-03-16 10:06:01 -0700154 // safe default, most producers are expected to override this
155 mProducer->setMaxDequeuedBufferCount(2);
Vishnu Nair1618c672021-02-05 13:08:26 -0800156 mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
157 GraphicBuffer::USAGE_HW_COMPOSER |
158 GraphicBuffer::USAGE_HW_TEXTURE,
Ady Abrahamdbca1352021-12-15 11:58:56 -0800159 1, false, this);
Valerie Haua32c5522019-12-09 10:11:08 -0800160 static int32_t id = 0;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700161 mName = name + "#" + std::to_string(id);
Vishnu Nairdab94092020-09-29 16:09:04 -0700162 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(id);
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700163 mQueuedBufferTrace = "QueuedBuffer - " + mName + "BLAST#" + std::to_string(id);
Valerie Haua32c5522019-12-09 10:11:08 -0800164 id++;
Vishnu Nairdab94092020-09-29 16:09:04 -0700165 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700166 mBufferItemConsumer->setFrameAvailableListener(this);
Robert Carr9f133d72020-04-01 15:51:46 -0700167
Huihong Luo02186fb2022-02-23 14:21:54 -0800168 ComposerServiceAIDL::getComposerService()->getMaxAcquiredBufferCount(&mMaxAcquiredBuffers);
Ady Abraham0bde6b52021-05-18 13:57:02 -0700169 mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxAcquiredBuffers);
chaviw69058fb2021-09-27 09:37:30 -0500170 mCurrentMaxAcquiredBufferCount = mMaxAcquiredBuffers;
Valerie Haua32c5522019-12-09 10:11:08 -0800171 mNumAcquired = 0;
172 mNumFrameAvailable = 0;
Robert Carr4c1b6462021-12-21 10:30:50 -0800173
174 TransactionCompletedListener::getInstance()->addQueueStallListener(
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000175 [&](const std::string& reason) {
176 std::function<void(const std::string&)> callbackCopy;
177 {
178 std::unique_lock _lock{mMutex};
179 callbackCopy = mTransactionHangCallback;
180 }
181 if (callbackCopy) callbackCopy(reason);
182 },
183 this);
Robert Carr4c1b6462021-12-21 10:30:50 -0800184
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800185 BQA_LOGV("BLASTBufferQueue created");
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800186}
187
188BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface,
189 int width, int height, int32_t format)
190 : BLASTBufferQueue(name) {
191 update(surface, width, height, format);
Robert Carr78c25dd2019-08-15 14:10:33 -0700192}
193
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800194BLASTBufferQueue::~BLASTBufferQueue() {
Robert Carr4c1b6462021-12-21 10:30:50 -0800195 TransactionCompletedListener::getInstance()->removeQueueStallListener(this);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800196 if (mPendingTransactions.empty()) {
197 return;
198 }
199 BQA_LOGE("Applying pending transactions on dtor %d",
200 static_cast<uint32_t>(mPendingTransactions.size()));
201 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800202 mergePendingTransactions(&t, std::numeric_limits<uint64_t>::max() /* frameNumber */);
Robert Carr79dc06a2022-02-22 15:28:59 -0800203 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
204 t.setApplyToken(mApplyToken).apply(false, true);
chaviw3b4bdcf2022-03-17 09:27:03 -0500205
206 if (mTransactionReadyCallback) {
207 mTransactionReadyCallback(mSyncTransaction);
208 }
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800209}
210
chaviw565ee542021-01-14 10:21:23 -0800211void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height,
Vishnu Naird2aaab12022-02-10 14:49:09 -0800212 int32_t format) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800213 LOG_ALWAYS_FATAL_IF(surface == nullptr, "BLASTBufferQueue: mSurfaceControl must not be NULL");
214
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000215 std::lock_guard _lock{mMutex};
chaviw565ee542021-01-14 10:21:23 -0800216 if (mFormat != format) {
217 mFormat = format;
chaviw497e81c2021-02-04 17:09:47 -0800218 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
chaviw565ee542021-01-14 10:21:23 -0800219 }
220
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800221 const bool surfaceControlChanged = !SurfaceControl::isSameSurface(mSurfaceControl, surface);
Vishnu Nairab066512022-01-04 22:28:00 +0000222 if (surfaceControlChanged && mSurfaceControl != nullptr) {
223 BQA_LOGD("Updating SurfaceControl without recreating BBQ");
224 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800225 bool applyTransaction = false;
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800226
Vishnu Nair5fa91c22021-06-29 14:30:48 -0700227 // Always update the native object even though they might have the same layer handle, so we can
228 // get the updated transform hint from WM.
229 mSurfaceControl = surface;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800230 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800231 if (surfaceControlChanged) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800232 t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure,
233 layer_state_t::eEnableBackpressure);
234 applyTransaction = true;
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800235 }
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800236 mTransformHint = mSurfaceControl->getTransformHint();
237 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Naira4fbca52021-07-07 16:52:34 -0700238 BQA_LOGV("update width=%d height=%d format=%d mTransformHint=%d", width, height, format,
239 mTransformHint);
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800240
Vishnu Nairea0de002020-11-17 17:42:37 -0800241 ui::Size newSize(width, height);
242 if (mRequestedSize != newSize) {
243 mRequestedSize.set(newSize);
244 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000245 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800246 // If the buffer supports scaling, update the frame immediately since the client may
247 // want to scale the existing buffer to the new size.
248 mSize = mRequestedSize;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800249 if (mUpdateDestinationFrame) {
250 t.setDestinationFrame(mSurfaceControl, Rect(newSize));
251 applyTransaction = true;
252 }
Vishnu Nair53c936c2020-12-03 11:46:37 -0800253 }
Robert Carrfc416512020-04-02 12:32:44 -0700254 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800255 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800256 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
257 t.setApplyToken(mApplyToken).apply(false, true);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800258 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700259}
260
chaviwd7deef72021-10-06 11:53:40 -0500261static std::optional<SurfaceControlStats> findMatchingStat(
262 const std::vector<SurfaceControlStats>& stats, const sp<SurfaceControl>& sc) {
263 for (auto stat : stats) {
264 if (SurfaceControl::isSameSurface(sc, stat.surfaceControl)) {
265 return stat;
266 }
267 }
268 return std::nullopt;
269}
270
271static void transactionCommittedCallbackThunk(void* context, nsecs_t latchTime,
272 const sp<Fence>& presentFence,
273 const std::vector<SurfaceControlStats>& stats) {
274 if (context == nullptr) {
275 return;
276 }
277 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
278 bq->transactionCommittedCallback(latchTime, presentFence, stats);
279}
280
281void BLASTBufferQueue::transactionCommittedCallback(nsecs_t /*latchTime*/,
282 const sp<Fence>& /*presentFence*/,
283 const std::vector<SurfaceControlStats>& stats) {
284 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000285 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600286 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500287 BQA_LOGV("transactionCommittedCallback");
288 if (!mSurfaceControlsWithPendingCallback.empty()) {
289 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
290 std::optional<SurfaceControlStats> stat = findMatchingStat(stats, pendingSC);
291 if (stat) {
292 uint64_t currFrameNumber = stat->frameEventStats.frameNumber;
293
294 // We need to check if we were waiting for a transaction callback in order to
295 // process any pending buffers and unblock. It's possible to get transaction
chaviwc1cf4022022-06-03 13:32:33 -0500296 // callbacks for previous requests so we need to ensure that there are no pending
297 // frame numbers that were in a sync. We remove the frame from mSyncedFrameNumbers
298 // set and then check if it's empty. If there are no more pending syncs, we can
299 // proceed with flushing the shadow queue.
300 // We also want to check if mSyncTransaction is null because it's possible another
chaviwd7deef72021-10-06 11:53:40 -0500301 // sync request came in while waiting, but it hasn't started processing yet. In that
302 // case, we don't actually want to flush the frames in between since they will get
303 // processed and merged with the sync transaction and released earlier than if they
304 // were sent to SF
chaviwc1cf4022022-06-03 13:32:33 -0500305 mSyncedFrameNumbers.erase(currFrameNumber);
306 if (mSyncedFrameNumbers.empty() && mSyncTransaction == nullptr) {
chaviwd7deef72021-10-06 11:53:40 -0500307 flushShadowQueue();
308 }
309 } else {
chaviw768bfa02021-11-01 09:50:57 -0500310 BQA_LOGE("Failed to find matching SurfaceControl in transactionCommittedCallback");
chaviwd7deef72021-10-06 11:53:40 -0500311 }
312 } else {
313 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
314 "empty.");
315 }
chaviwd7deef72021-10-06 11:53:40 -0500316 decStrong((void*)transactionCommittedCallbackThunk);
317 }
318}
319
Robert Carr78c25dd2019-08-15 14:10:33 -0700320static void transactionCallbackThunk(void* context, nsecs_t latchTime,
321 const sp<Fence>& presentFence,
322 const std::vector<SurfaceControlStats>& stats) {
323 if (context == nullptr) {
324 return;
325 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800326 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700327 bq->transactionCallback(latchTime, presentFence, stats);
328}
329
330void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
331 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700332 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000333 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600334 BBQ_TRACE();
chaviw71c2cc42020-10-23 16:42:02 -0700335 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700336
chaviw42026162021-04-16 15:46:12 -0500337 if (!mSurfaceControlsWithPendingCallback.empty()) {
338 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
339 mSurfaceControlsWithPendingCallback.pop();
chaviwd7deef72021-10-06 11:53:40 -0500340 std::optional<SurfaceControlStats> statsOptional = findMatchingStat(stats, pendingSC);
341 if (statsOptional) {
342 SurfaceControlStats stat = *statsOptional;
Vishnu Nair71fcf912022-10-18 09:14:20 -0700343 if (stat.transformHint) {
344 mTransformHint = *stat.transformHint;
345 mBufferItemConsumer->setTransformHint(mTransformHint);
346 BQA_LOGV("updated mTransformHint=%d", mTransformHint);
347 }
Vishnu Nairde66dc72021-06-17 17:54:41 -0700348 // Update frametime stamps if the frame was latched and presented, indicated by a
349 // valid latch time.
350 if (stat.latchTime > 0) {
351 mBufferItemConsumer
352 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
353 stat.frameEventStats.refreshStartTime,
354 stat.frameEventStats.gpuCompositionDoneFence,
355 stat.presentFence, stat.previousReleaseFence,
356 stat.frameEventStats.compositorTiming,
357 stat.latchTime,
358 stat.frameEventStats.dequeueReadyTime);
359 }
Robert Carr405e2f62021-12-31 16:59:34 -0800360 auto currFrameNumber = stat.frameEventStats.frameNumber;
361 std::vector<ReleaseCallbackId> staleReleases;
362 for (const auto& [key, value]: mSubmitted) {
363 if (currFrameNumber > key.framenumber) {
364 staleReleases.push_back(key);
365 }
366 }
367 for (const auto& staleRelease : staleReleases) {
Robert Carr405e2f62021-12-31 16:59:34 -0800368 releaseBufferCallbackLocked(staleRelease,
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700369 stat.previousReleaseFence
370 ? stat.previousReleaseFence
371 : Fence::NO_FENCE,
372 stat.currentMaxAcquiredBufferCount,
373 true /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800374 }
chaviwd7deef72021-10-06 11:53:40 -0500375 } else {
chaviw768bfa02021-11-01 09:50:57 -0500376 BQA_LOGE("Failed to find matching SurfaceControl in transactionCallback");
chaviw42026162021-04-16 15:46:12 -0500377 }
378 } else {
379 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
380 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800381 }
chaviw71c2cc42020-10-23 16:42:02 -0700382
chaviw71c2cc42020-10-23 16:42:02 -0700383 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700384 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700385}
386
Vishnu Nair1506b182021-02-22 14:35:15 -0800387// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
388// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
389// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
390// Otherwise, this is a no-op.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700391static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, const ReleaseCallbackId& id,
chaviw69058fb2021-09-27 09:37:30 -0500392 const sp<Fence>& releaseFence,
393 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800394 sp<BLASTBufferQueue> blastBufferQueue = context.promote();
Vishnu Nair1506b182021-02-22 14:35:15 -0800395 if (blastBufferQueue) {
chaviw69058fb2021-09-27 09:37:30 -0500396 blastBufferQueue->releaseBufferCallback(id, releaseFence, currentMaxAcquiredBufferCount);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700397 } else {
398 ALOGV("releaseBufferCallbackThunk %s blastBufferQueue is dead", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800399 }
400}
401
chaviwd7deef72021-10-06 11:53:40 -0500402void BLASTBufferQueue::flushShadowQueue() {
403 BQA_LOGV("flushShadowQueue");
404 int numFramesToFlush = mNumFrameAvailable;
405 while (numFramesToFlush > 0) {
406 acquireNextBufferLocked(std::nullopt);
407 numFramesToFlush--;
408 }
409}
410
chaviw69058fb2021-09-27 09:37:30 -0500411void BLASTBufferQueue::releaseBufferCallback(
412 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
413 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000414 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600415 BBQ_TRACE();
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700416 releaseBufferCallbackLocked(id, releaseFence, currentMaxAcquiredBufferCount,
417 false /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800418}
419
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700420void BLASTBufferQueue::releaseBufferCallbackLocked(
421 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
422 std::optional<uint32_t> currentMaxAcquiredBufferCount, bool fakeRelease) {
Robert Carr405e2f62021-12-31 16:59:34 -0800423 ATRACE_CALL();
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700424 BQA_LOGV("releaseBufferCallback %s", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800425
Ady Abraham899dcdb2021-06-15 16:56:21 -0700426 // Calculate how many buffers we need to hold before we release them back
427 // to the buffer queue. This will prevent higher latency when we are running
428 // on a lower refresh rate than the max supported. We only do that for EGL
429 // clients as others don't care about latency
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000430 const auto it = mSubmitted.find(id);
431 const bool isEGL = it != mSubmitted.end() && it->second.mApi == NATIVE_WINDOW_API_EGL;
Ady Abraham899dcdb2021-06-15 16:56:21 -0700432
chaviw69058fb2021-09-27 09:37:30 -0500433 if (currentMaxAcquiredBufferCount) {
434 mCurrentMaxAcquiredBufferCount = *currentMaxAcquiredBufferCount;
435 }
436
Ady Abraham899dcdb2021-06-15 16:56:21 -0700437 const auto numPendingBuffersToHold =
chaviw69058fb2021-09-27 09:37:30 -0500438 isEGL ? std::max(0u, mMaxAcquiredBuffers - mCurrentMaxAcquiredBufferCount) : 0;
Robert Carr405e2f62021-12-31 16:59:34 -0800439
440 auto rb = ReleasedBuffer{id, releaseFence};
441 if (std::find(mPendingRelease.begin(), mPendingRelease.end(), rb) == mPendingRelease.end()) {
442 mPendingRelease.emplace_back(rb);
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700443 if (fakeRelease) {
444 BQA_LOGE("Faking releaseBufferCallback from transactionCompleteCallback %" PRIu64,
445 id.framenumber);
446 BBQ_TRACE("FakeReleaseCallback");
447 }
Robert Carr405e2f62021-12-31 16:59:34 -0800448 }
Ady Abraham899dcdb2021-06-15 16:56:21 -0700449
450 // Release all buffers that are beyond the ones that we need to hold
451 while (mPendingRelease.size() > numPendingBuffersToHold) {
chaviw0acd33a2021-11-02 11:55:37 -0500452 const auto releasedBuffer = mPendingRelease.front();
Ady Abraham899dcdb2021-06-15 16:56:21 -0700453 mPendingRelease.pop_front();
chaviw0acd33a2021-11-02 11:55:37 -0500454 releaseBuffer(releasedBuffer.callbackId, releasedBuffer.releaseFence);
chaviwc1cf4022022-06-03 13:32:33 -0500455 // Don't process the transactions here if mSyncedFrameNumbers is not empty. That means
456 // are still transactions that have sync buffers in them that have not been applied or
457 // dropped. Instead, let onFrameAvailable handle processing them since it will merge with
458 // the syncTransaction.
459 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500460 acquireNextBufferLocked(std::nullopt);
461 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800462 }
463
Ady Abraham899dcdb2021-06-15 16:56:21 -0700464 ATRACE_INT("PendingRelease", mPendingRelease.size());
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700465 ATRACE_INT(mQueuedBufferTrace.c_str(),
466 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
Vishnu Nair1506b182021-02-22 14:35:15 -0800467 mCallbackCV.notify_all();
468}
469
chaviw0acd33a2021-11-02 11:55:37 -0500470void BLASTBufferQueue::releaseBuffer(const ReleaseCallbackId& callbackId,
471 const sp<Fence>& releaseFence) {
472 auto it = mSubmitted.find(callbackId);
473 if (it == mSubmitted.end()) {
474 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %s",
475 callbackId.to_string().c_str());
476 return;
477 }
478 mNumAcquired--;
chaviw57ae4b22022-02-03 16:51:39 -0600479 BBQ_TRACE("frame=%" PRIu64, callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500480 BQA_LOGV("released %s", callbackId.to_string().c_str());
481 mBufferItemConsumer->releaseBuffer(it->second, releaseFence);
482 mSubmitted.erase(it);
chaviwc1cf4022022-06-03 13:32:33 -0500483 // Remove the frame number from mSyncedFrameNumbers since we can get a release callback
484 // without getting a transaction committed if the buffer was dropped.
485 mSyncedFrameNumbers.erase(callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500486}
487
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000488status_t BLASTBufferQueue::acquireNextBufferLocked(
chaviwd7deef72021-10-06 11:53:40 -0500489 const std::optional<SurfaceComposerClient::Transaction*> transaction) {
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800490 // If the next transaction is set, we want to guarantee the our acquire will not fail, so don't
491 // include the extra buffer when checking if we can acquire the next buffer.
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000492 if (mNumFrameAvailable == 0) {
493 BQA_LOGV("Can't process next buffer. No available frames");
494 return NOT_ENOUGH_DATA;
Valerie Haud3b90d22019-11-06 09:37:31 -0800495 }
496
Valerie Haua32c5522019-12-09 10:11:08 -0800497 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700498 BQA_LOGE("ERROR : surface control is null");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000499 return NAME_NOT_FOUND;
Valerie Haud3b90d22019-11-06 09:37:31 -0800500 }
501
Robert Carr78c25dd2019-08-15 14:10:33 -0700502 SurfaceComposerClient::Transaction localTransaction;
503 bool applyTransaction = true;
504 SurfaceComposerClient::Transaction* t = &localTransaction;
chaviwd7deef72021-10-06 11:53:40 -0500505 if (transaction) {
506 t = *transaction;
Robert Carr78c25dd2019-08-15 14:10:33 -0700507 applyTransaction = false;
508 }
509
Valerie Haua32c5522019-12-09 10:11:08 -0800510 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800511
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800512 status_t status =
513 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800514 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
515 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000516 return status;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800517 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700518 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000519 return status;
Robert Carr78c25dd2019-08-15 14:10:33 -0700520 }
chaviw57ae4b22022-02-03 16:51:39 -0600521
Valerie Haua32c5522019-12-09 10:11:08 -0800522 auto buffer = bufferItem.mGraphicBuffer;
523 mNumFrameAvailable--;
chaviw57ae4b22022-02-03 16:51:39 -0600524 BBQ_TRACE("frame=%" PRIu64, bufferItem.mFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800525
526 if (buffer == nullptr) {
527 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700528 BQA_LOGE("Buffer was empty");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000529 return BAD_VALUE;
Valerie Haua32c5522019-12-09 10:11:08 -0800530 }
531
Vishnu Nair670b3f72020-09-29 17:52:18 -0700532 if (rejectBuffer(bufferItem)) {
Vishnu Naira4fbca52021-07-07 16:52:34 -0700533 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d "
Vishnu Nairea0de002020-11-17 17:42:37 -0800534 "buffer{size=%dx%d transform=%d}",
535 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
536 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
537 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000538 return acquireNextBufferLocked(transaction);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700539 }
540
Valerie Haua32c5522019-12-09 10:11:08 -0800541 mNumAcquired++;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700542 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
543 ReleaseCallbackId releaseCallbackId(buffer->getId(), mLastAcquiredFrameNumber);
544 mSubmitted[releaseCallbackId] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700545
Valerie Hau871d6352020-01-29 08:44:02 -0800546 bool needsDisconnect = false;
547 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
548
549 // if producer disconnected before, notify SurfaceFlinger
550 if (needsDisconnect) {
551 t->notifyProducerDisconnect(mSurfaceControl);
552 }
553
Robert Carr78c25dd2019-08-15 14:10:33 -0700554 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
555 incStrong((void*)transactionCallbackThunk);
556
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700557 mSize = mRequestedSize;
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700558 Rect crop = computeCrop(bufferItem);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000559 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
560 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700561 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800562
Vishnu Nair1506b182021-02-22 14:35:15 -0800563 auto releaseBufferCallback =
564 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
chaviw69058fb2021-09-27 09:37:30 -0500565 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
chaviwba4320c2021-09-15 15:20:53 -0500566 sp<Fence> fence = bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE;
chaviw8dd181f2022-01-05 18:36:46 -0600567 t->setBuffer(mSurfaceControl, buffer, fence, bufferItem.mFrameNumber, releaseBufferCallback);
John Reck137069e2020-12-10 22:07:37 -0500568 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
569 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
570 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700571 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviwf2dace72021-11-17 17:36:50 -0600572
chaviw42026162021-04-16 15:46:12 -0500573 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700574
Vishnu Naird2aaab12022-02-10 14:49:09 -0800575 if (mUpdateDestinationFrame) {
576 t->setDestinationFrame(mSurfaceControl, Rect(mSize));
577 } else {
578 const bool ignoreDestinationFrame =
579 bufferItem.mScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE;
580 t->setFlags(mSurfaceControl,
581 ignoreDestinationFrame ? layer_state_t::eIgnoreDestinationFrame : 0,
582 layer_state_t::eIgnoreDestinationFrame);
Vishnu Nair084514a2021-07-30 16:07:42 -0700583 }
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700584 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800585 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800586 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Vishnu Naird2aaab12022-02-10 14:49:09 -0800587 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800588 if (!bufferItem.mIsAutoTimestamp) {
589 t->setDesiredPresentTime(bufferItem.mTimestamp);
590 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700591
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800592 // Drop stale frame timeline infos
593 while (!mPendingFrameTimelines.empty() &&
594 mPendingFrameTimelines.front().first < bufferItem.mFrameNumber) {
595 ATRACE_FORMAT_INSTANT("dropping stale frameNumber: %" PRIu64 " vsyncId: %" PRId64,
596 mPendingFrameTimelines.front().first,
597 mPendingFrameTimelines.front().second.vsyncId);
598 mPendingFrameTimelines.pop();
599 }
600
601 if (!mPendingFrameTimelines.empty() &&
602 mPendingFrameTimelines.front().first == bufferItem.mFrameNumber) {
603 ATRACE_FORMAT_INSTANT("Transaction::setFrameTimelineInfo frameNumber: %" PRIu64
604 " vsyncId: %" PRId64,
605 bufferItem.mFrameNumber,
606 mPendingFrameTimelines.front().second.vsyncId);
607 t->setFrameTimelineInfo(mPendingFrameTimelines.front().second);
608 mPendingFrameTimelines.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100609 }
610
Vishnu Nairadf632b2021-01-07 14:05:08 -0800611 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000612 std::lock_guard _lock{mTimestampMutex};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800613 auto dequeueTime = mDequeueTimestamps.find(buffer->getId());
614 if (dequeueTime != mDequeueTimestamps.end()) {
615 Parcel p;
616 p.writeInt64(dequeueTime->second);
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800617 t->setMetadata(mSurfaceControl, gui::METADATA_DEQUEUE_TIME, p);
Vishnu Nairadf632b2021-01-07 14:05:08 -0800618 mDequeueTimestamps.erase(dequeueTime);
619 }
620 }
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800621
chaviw6a195272021-09-03 16:14:25 -0500622 mergePendingTransactions(t, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700623 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800624 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
625 t->setApplyToken(mApplyToken).apply(false, true);
626 mAppliedLastTransaction = true;
627 mLastAppliedFrameNumber = bufferItem.mFrameNumber;
628 } else {
629 t->setBufferHasBarrier(mSurfaceControl, mLastAppliedFrameNumber);
630 mAppliedLastTransaction = false;
Robert Carr78c25dd2019-08-15 14:10:33 -0700631 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700632
chaviwd7deef72021-10-06 11:53:40 -0500633 BQA_LOGV("acquireNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800634 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
Vishnu Naira4fbca52021-07-07 16:52:34 -0700635 " graphicBufferId=%" PRIu64 "%s transform=%d",
chaviw3277faf2021-05-19 16:45:23 -0500636 mSize.width, mSize.height, bufferItem.mFrameNumber, boolToString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800637 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700638 static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(),
Vishnu Naira4fbca52021-07-07 16:52:34 -0700639 bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000640 return OK;
Robert Carr78c25dd2019-08-15 14:10:33 -0700641}
642
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800643Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
644 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800645 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800646 }
647 return item.mCrop;
648}
649
chaviwd7deef72021-10-06 11:53:40 -0500650void BLASTBufferQueue::acquireAndReleaseBuffer() {
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000651 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500652 BufferItem bufferItem;
chaviw6ebdf5f2021-10-14 11:57:22 -0500653 status_t status =
654 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
655 if (status != OK) {
656 BQA_LOGE("Failed to acquire a buffer in acquireAndReleaseBuffer, err=%s",
657 statusToString(status).c_str());
658 return;
659 }
chaviwd7deef72021-10-06 11:53:40 -0500660 mNumFrameAvailable--;
chaviw6ebdf5f2021-10-14 11:57:22 -0500661 mBufferItemConsumer->releaseBuffer(bufferItem, bufferItem.mFence);
chaviwd7deef72021-10-06 11:53:40 -0500662}
663
Vishnu Nairaef1de92020-10-22 12:15:53 -0700664void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000665 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
666 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
chaviwc1cf4022022-06-03 13:32:33 -0500667
Tianhao Yao4861b102022-02-03 20:18:35 +0000668 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000669 UNIQUE_LOCK_WITH_ASSERTION(mMutex);
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000670 BBQ_TRACE();
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000671 bool waitForTransactionCallback = !mSyncedFrameNumbers.empty();
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800672
Tianhao Yao4861b102022-02-03 20:18:35 +0000673 const bool syncTransactionSet = mTransactionReadyCallback != nullptr;
674 BQA_LOGV("onFrameAvailable-start syncTransactionSet=%s", boolToString(syncTransactionSet));
Valerie Haud3b90d22019-11-06 09:37:31 -0800675
Tianhao Yao4861b102022-02-03 20:18:35 +0000676 if (syncTransactionSet) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000677 // If we are going to re-use the same mSyncTransaction, release the buffer that may
678 // already be set in the Transaction. This is to allow us a free slot early to continue
679 // processing a new buffer.
680 if (!mAcquireSingleBuffer) {
681 auto bufferData = mSyncTransaction->getAndClearBuffer(mSurfaceControl);
682 if (bufferData) {
683 BQA_LOGD("Releasing previous buffer when syncing: framenumber=%" PRIu64,
684 bufferData->frameNumber);
685 releaseBuffer(bufferData->generateReleaseCallbackId(),
686 bufferData->acquireFence);
Tianhao Yao4861b102022-02-03 20:18:35 +0000687 }
688 }
chaviw0acd33a2021-11-02 11:55:37 -0500689
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000690 if (waitForTransactionCallback) {
691 // We are waiting on a previous sync's transaction callback so allow another sync
692 // transaction to proceed.
693 //
694 // We need to first flush out the transactions that were in between the two syncs.
695 // We do this by merging them into mSyncTransaction so any buffer merging will get
696 // a release callback invoked.
697 while (mNumFrameAvailable > 0) {
698 // flush out the shadow queue
699 acquireAndReleaseBuffer();
700 }
chaviwd7deef72021-10-06 11:53:40 -0500701 }
702 }
703
Tianhao Yao4861b102022-02-03 20:18:35 +0000704 // add to shadow queue
705 mNumFrameAvailable++;
chaviwc1cf4022022-06-03 13:32:33 -0500706 if (waitForTransactionCallback && mNumFrameAvailable >= 2) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000707 acquireAndReleaseBuffer();
708 }
709 ATRACE_INT(mQueuedBufferTrace.c_str(),
710 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
711
712 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " syncTransactionSet=%s",
713 item.mFrameNumber, boolToString(syncTransactionSet));
714
715 if (syncTransactionSet) {
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000716 // If there's no available buffer and we're in a sync transaction, we need to wait
717 // instead of returning since we guarantee a buffer will be acquired for the sync.
718 while (acquireNextBufferLocked(mSyncTransaction) == BufferQueue::NO_BUFFER_AVAILABLE) {
719 BQA_LOGD("waiting for available buffer");
720 mCallbackCV.wait(_lock);
721 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000722
723 // Only need a commit callback when syncing to ensure the buffer that's synced has been
724 // sent to SF
725 incStrong((void*)transactionCommittedCallbackThunk);
726 mSyncTransaction->addTransactionCommittedCallback(transactionCommittedCallbackThunk,
727 static_cast<void*>(this));
chaviwc1cf4022022-06-03 13:32:33 -0500728 mSyncedFrameNumbers.emplace(item.mFrameNumber);
Tianhao Yao4861b102022-02-03 20:18:35 +0000729 if (mAcquireSingleBuffer) {
730 prevCallback = mTransactionReadyCallback;
731 prevTransaction = mSyncTransaction;
732 mTransactionReadyCallback = nullptr;
733 mSyncTransaction = nullptr;
734 }
chaviwc1cf4022022-06-03 13:32:33 -0500735 } else if (!waitForTransactionCallback) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000736 acquireNextBufferLocked(std::nullopt);
Valerie Hau0188adf2020-02-13 08:29:20 -0800737 }
738 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000739 if (prevCallback) {
740 prevCallback(prevTransaction);
chaviwd7deef72021-10-06 11:53:40 -0500741 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800742}
743
Vishnu Nairaef1de92020-10-22 12:15:53 -0700744void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
745 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
746 // Do nothing since we are not storing unacquired buffer items locally.
747}
748
Vishnu Nairadf632b2021-01-07 14:05:08 -0800749void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000750 std::lock_guard _lock{mTimestampMutex};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800751 mDequeueTimestamps[bufferId] = systemTime();
752};
753
754void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000755 std::lock_guard _lock{mTimestampMutex};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800756 mDequeueTimestamps.erase(bufferId);
757};
758
Tianhao Yao4861b102022-02-03 20:18:35 +0000759void BLASTBufferQueue::syncNextTransaction(
760 std::function<void(SurfaceComposerClient::Transaction*)> callback,
761 bool acquireSingleBuffer) {
chaviw3b4bdcf2022-03-17 09:27:03 -0500762 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
763 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
764
765 {
766 std::lock_guard _lock{mMutex};
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000767 BBQ_TRACE();
chaviw3b4bdcf2022-03-17 09:27:03 -0500768 // We're about to overwrite the previous call so we should invoke that callback
769 // immediately.
770 if (mTransactionReadyCallback) {
771 prevCallback = mTransactionReadyCallback;
772 prevTransaction = mSyncTransaction;
773 }
774
775 mTransactionReadyCallback = callback;
776 if (callback) {
777 mSyncTransaction = new SurfaceComposerClient::Transaction();
778 } else {
779 mSyncTransaction = nullptr;
780 }
781 mAcquireSingleBuffer = mTransactionReadyCallback ? acquireSingleBuffer : true;
Tianhao Yao4861b102022-02-03 20:18:35 +0000782 }
chaviw3b4bdcf2022-03-17 09:27:03 -0500783
784 if (prevCallback) {
785 prevCallback(prevTransaction);
786 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000787}
788
789void BLASTBufferQueue::stopContinuousSyncTransaction() {
790 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
791 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
792 {
793 std::lock_guard _lock{mMutex};
794 bool invokeCallback = mTransactionReadyCallback && !mAcquireSingleBuffer;
795 if (invokeCallback) {
796 prevCallback = mTransactionReadyCallback;
797 prevTransaction = mSyncTransaction;
798 }
799 mTransactionReadyCallback = nullptr;
800 mSyncTransaction = nullptr;
801 mAcquireSingleBuffer = true;
802 }
803 if (prevCallback) {
804 prevCallback(prevTransaction);
805 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700806}
807
Vishnu Nairea0de002020-11-17 17:42:37 -0800808bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700809 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
810 // Only reject buffers if scaling mode is freeze.
811 return false;
812 }
813
Vishnu Naire1a42322020-10-02 17:42:04 -0700814 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
815 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
816
817 // Take the buffer's orientation into account
818 if (item.mTransform & ui::Transform::ROT_90) {
819 std::swap(bufWidth, bufHeight);
820 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800821 ui::Size bufferSize(bufWidth, bufHeight);
822 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800823 return false;
824 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700825
Vishnu Nair670b3f72020-09-29 17:52:18 -0700826 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800827 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700828}
Vishnu Nairbf255772020-10-16 10:54:41 -0700829
Robert Carr05086b22020-10-13 18:22:51 -0700830class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700831private:
Vishnu Nair95b6d512021-08-30 15:31:08 -0700832 std::mutex mMutex;
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000833 sp<BLASTBufferQueue> mBbq GUARDED_BY(mMutex);
834 bool mDestroyed GUARDED_BY(mMutex) = false;
Vishnu Nair95b6d512021-08-30 15:31:08 -0700835
Robert Carr05086b22020-10-13 18:22:51 -0700836public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700837 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
838 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
839 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700840
Robert Carr05086b22020-10-13 18:22:51 -0700841 void allocateBuffers() override {
842 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
843 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
844 auto gbp = getIGraphicBufferProducer();
845 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
846 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
847 gbp->allocateBuffers(reqWidth, reqHeight,
848 reqFormat, reqUsage);
849
850 }).detach();
851 }
Robert Carr9c006e02020-10-14 13:41:57 -0700852
Marin Shalamanovc5986772021-03-16 16:09:49 +0100853 status_t setFrameRate(float frameRate, int8_t compatibility,
854 int8_t changeFrameRateStrategy) override {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000855 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700856 if (mDestroyed) {
857 return DEAD_OBJECT;
858 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100859 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
860 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700861 return BAD_VALUE;
862 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100863 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700864 }
Robert Carr9b611b72020-10-19 12:00:23 -0700865
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800866 status_t setFrameTimelineInfo(uint64_t frameNumber,
867 const FrameTimelineInfo& frameTimelineInfo) override {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000868 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700869 if (mDestroyed) {
870 return DEAD_OBJECT;
871 }
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800872 return mBbq->setFrameTimelineInfo(frameNumber, frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700873 }
Vishnu Nair95b6d512021-08-30 15:31:08 -0700874
875 void destroy() override {
876 Surface::destroy();
877
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000878 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700879 mDestroyed = true;
880 mBbq = nullptr;
881 }
Robert Carr05086b22020-10-13 18:22:51 -0700882};
883
Robert Carr9c006e02020-10-14 13:41:57 -0700884// TODO: Can we coalesce this with frame updates? Need to confirm
885// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200886status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
887 bool shouldBeSeamless) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000888 std::lock_guard _lock{mMutex};
Robert Carr9c006e02020-10-14 13:41:57 -0700889 SurfaceComposerClient::Transaction t;
890
Marin Shalamanov46084422020-10-13 12:33:42 +0200891 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700892}
893
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800894status_t BLASTBufferQueue::setFrameTimelineInfo(uint64_t frameNumber,
895 const FrameTimelineInfo& frameTimelineInfo) {
896 ATRACE_FORMAT("%s(%s) frameNumber: %" PRIu64 " vsyncId: %" PRId64, __func__, mName.c_str(),
897 frameNumber, frameTimelineInfo.vsyncId);
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000898 std::lock_guard _lock{mMutex};
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800899 mPendingFrameTimelines.push({frameNumber, frameTimelineInfo});
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100900 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700901}
902
Hongguang Chen621ec582021-02-16 15:42:35 -0800903void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000904 std::lock_guard _lock{mMutex};
Hongguang Chen621ec582021-02-16 15:42:35 -0800905 SurfaceComposerClient::Transaction t;
906
907 t.setSidebandStream(mSurfaceControl, stream).apply();
908}
909
Vishnu Nair992496b2020-10-22 17:27:21 -0700910sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000911 std::lock_guard _lock{mMutex};
Vishnu Nair992496b2020-10-22 17:27:21 -0700912 sp<IBinder> scHandle = nullptr;
913 if (includeSurfaceControlHandle && mSurfaceControl) {
914 scHandle = mSurfaceControl->getHandle();
915 }
916 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700917}
918
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800919void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
920 uint64_t frameNumber) {
921 std::lock_guard _lock{mMutex};
922 if (mLastAcquiredFrameNumber >= frameNumber) {
923 // Apply the transaction since we have already acquired the desired frame.
924 t->apply();
925 } else {
chaviwaad6cf52021-03-23 17:27:20 -0500926 mPendingTransactions.emplace_back(frameNumber, *t);
927 // Clear the transaction so it can't be applied elsewhere.
928 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800929 }
930}
931
chaviw6a195272021-09-03 16:14:25 -0500932void BLASTBufferQueue::applyPendingTransactions(uint64_t frameNumber) {
933 std::lock_guard _lock{mMutex};
934
935 SurfaceComposerClient::Transaction t;
936 mergePendingTransactions(&t, frameNumber);
Robert Carr79dc06a2022-02-22 15:28:59 -0800937 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
938 t.setApplyToken(mApplyToken).apply(false, true);
chaviw6a195272021-09-03 16:14:25 -0500939}
940
941void BLASTBufferQueue::mergePendingTransactions(SurfaceComposerClient::Transaction* t,
942 uint64_t frameNumber) {
943 auto mergeTransaction =
944 [&t, currentFrameNumber = frameNumber](
945 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
946 auto& [targetFrameNumber, transaction] = pendingTransaction;
947 if (currentFrameNumber < targetFrameNumber) {
948 return false;
949 }
950 t->merge(std::move(transaction));
951 return true;
952 };
953
954 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
955 mPendingTransactions.end(), mergeTransaction),
956 mPendingTransactions.end());
957}
958
chaviwd84085a2022-02-08 11:07:04 -0600959SurfaceComposerClient::Transaction* BLASTBufferQueue::gatherPendingTransactions(
960 uint64_t frameNumber) {
961 std::lock_guard _lock{mMutex};
962 SurfaceComposerClient::Transaction* t = new SurfaceComposerClient::Transaction();
963 mergePendingTransactions(t, frameNumber);
964 return t;
965}
966
Vishnu Nair89496122020-12-14 17:14:53 -0800967// Maintains a single worker thread per process that services a list of runnables.
968class AsyncWorker : public Singleton<AsyncWorker> {
969private:
970 std::thread mThread;
971 bool mDone = false;
972 std::deque<std::function<void()>> mRunnables;
973 std::mutex mMutex;
974 std::condition_variable mCv;
975 void run() {
976 std::unique_lock<std::mutex> lock(mMutex);
977 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -0800978 while (!mRunnables.empty()) {
Vishnu Nair51e4dc82021-10-01 15:32:33 -0700979 std::deque<std::function<void()>> runnables = std::move(mRunnables);
980 mRunnables.clear();
981 lock.unlock();
982 // Run outside the lock since the runnable might trigger another
983 // post to the async worker.
984 execute(runnables);
985 lock.lock();
Vishnu Nair89496122020-12-14 17:14:53 -0800986 }
Wonsik Kim567533e2021-05-04 19:31:29 -0700987 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -0800988 }
989 }
990
Vishnu Nair51e4dc82021-10-01 15:32:33 -0700991 void execute(std::deque<std::function<void()>>& runnables) {
992 while (!runnables.empty()) {
993 std::function<void()> runnable = runnables.front();
994 runnables.pop_front();
995 runnable();
996 }
997 }
998
Vishnu Nair89496122020-12-14 17:14:53 -0800999public:
1000 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
1001
1002 ~AsyncWorker() {
1003 mDone = true;
1004 mCv.notify_all();
1005 if (mThread.joinable()) {
1006 mThread.join();
1007 }
1008 }
1009
1010 void post(std::function<void()> runnable) {
1011 std::unique_lock<std::mutex> lock(mMutex);
1012 mRunnables.emplace_back(std::move(runnable));
1013 mCv.notify_one();
1014 }
1015};
1016ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
1017
1018// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
1019class AsyncProducerListener : public BnProducerListener {
1020private:
1021 const sp<IProducerListener> mListener;
1022
1023public:
1024 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
1025
1026 void onBufferReleased() override {
1027 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
1028 }
1029
1030 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
1031 AsyncWorker::getInstance().post(
1032 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
1033 }
1034};
1035
1036// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
1037// can be non-blocking when the producer is in the client process.
1038class BBQBufferQueueProducer : public BufferQueueProducer {
1039public:
1040 BBQBufferQueueProducer(const sp<BufferQueueCore>& core)
1041 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/) {}
1042
1043 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
1044 QueueBufferOutput* output) override {
1045 if (!listener) {
1046 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
1047 }
1048
1049 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
1050 producerControlledByApp, output);
1051 }
Vishnu Nair17dde612020-12-28 11:39:59 -08001052
1053 int query(int what, int* value) override {
1054 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
1055 *value = 1;
1056 return NO_ERROR;
1057 }
1058 return BufferQueueProducer::query(what, value);
1059 }
Vishnu Nair89496122020-12-14 17:14:53 -08001060};
1061
1062// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
1063// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
1064// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
1065// we can deadlock.
1066void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
1067 sp<IGraphicBufferConsumer>* outConsumer) {
1068 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
1069 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
1070
1071 sp<BufferQueueCore> core(new BufferQueueCore());
1072 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
1073
1074 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core));
1075 LOG_ALWAYS_FATAL_IF(producer == nullptr,
1076 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
1077
Vishnu Nair8b30dd12021-01-25 14:16:54 -08001078 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
1079 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -08001080 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
1081 "BLASTBufferQueue: failed to create BufferQueueConsumer");
1082
1083 *outProducer = producer;
1084 *outConsumer = consumer;
1085}
1086
chaviw497e81c2021-02-04 17:09:47 -08001087PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
1088 PixelFormat convertedFormat = format;
1089 switch (format) {
1090 case PIXEL_FORMAT_TRANSPARENT:
1091 case PIXEL_FORMAT_TRANSLUCENT:
1092 convertedFormat = PIXEL_FORMAT_RGBA_8888;
1093 break;
1094 case PIXEL_FORMAT_OPAQUE:
1095 convertedFormat = PIXEL_FORMAT_RGBX_8888;
1096 break;
1097 }
1098 return convertedFormat;
1099}
1100
Robert Carr82d07c92021-05-10 11:36:43 -07001101uint32_t BLASTBufferQueue::getLastTransformHint() const {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001102 std::lock_guard _lock{mMutex};
Robert Carr82d07c92021-05-10 11:36:43 -07001103 if (mSurfaceControl != nullptr) {
1104 return mSurfaceControl->getTransformHint();
1105 } else {
1106 return 0;
1107 }
1108}
1109
chaviw0b020f82021-08-20 12:00:47 -05001110uint64_t BLASTBufferQueue::getLastAcquiredFrameNum() {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001111 std::lock_guard _lock{mMutex};
chaviw0b020f82021-08-20 12:00:47 -05001112 return mLastAcquiredFrameNumber;
1113}
1114
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001115bool BLASTBufferQueue::isSameSurfaceControl(const sp<SurfaceControl>& surfaceControl) const {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001116 std::lock_guard _lock{mMutex};
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001117 return SurfaceControl::isSameSurface(mSurfaceControl, surfaceControl);
1118}
1119
Patrick Williamsf1e5df12022-10-17 21:37:42 +00001120void BLASTBufferQueue::setTransactionHangCallback(
1121 std::function<void(const std::string&)> callback) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001122 std::lock_guard _lock{mMutex};
Robert Carr4c1b6462021-12-21 10:30:50 -08001123 mTransactionHangCallback = callback;
1124}
1125
Robert Carr78c25dd2019-08-15 14:10:33 -07001126} // namespace android