blob: 7f0e80e0f05232b11163657a5b57cfb22cb5632b [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
liulijuneb489f62022-10-17 22:02:14 +080023#include <cutils/atomic.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070024#include <gui/BLASTBufferQueue.h>
25#include <gui/BufferItemConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080026#include <gui/BufferQueueConsumer.h>
27#include <gui/BufferQueueCore.h>
28#include <gui/BufferQueueProducer.h>
Ady Abraham107788e2023-10-17 12:31:08 -070029
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070030#include <gui/FrameRateUtils.h>
Valerie Hau45e4b3b2019-12-03 10:49:17 -080031#include <gui/GLConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080032#include <gui/IProducerListener.h>
Robert Carr05086b22020-10-13 18:22:51 -070033#include <gui/Surface.h>
chaviw57ae4b22022-02-03 16:51:39 -060034#include <gui/TraceUtils.h>
Vishnu Nair89496122020-12-14 17:14:53 -080035#include <utils/Singleton.h>
Valerie Haua32c5522019-12-09 10:11:08 -080036#include <utils/Trace.h>
37
Ady Abraham0bde6b52021-05-18 13:57:02 -070038#include <private/gui/ComposerService.h>
Huihong Luo02186fb2022-02-23 14:21:54 -080039#include <private/gui/ComposerServiceAIDL.h>
Ady Abraham0bde6b52021-05-18 13:57:02 -070040
Chavi Weingartene0237bb2023-02-06 21:48:32 +000041#include <android-base/thread_annotations.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070042
Alec Mouri21d94322023-10-17 19:51:39 +000043#include <com_android_graphics_libgui_flags.h>
44
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070045using namespace com::android::graphics::libgui;
Robert Carr78c25dd2019-08-15 14:10:33 -070046using namespace std::chrono_literals;
47
Vishnu Nairdab94092020-09-29 16:09:04 -070048namespace {
chaviw3277faf2021-05-19 16:45:23 -050049inline const char* boolToString(bool b) {
Vishnu Nairdab94092020-09-29 16:09:04 -070050 return b ? "true" : "false";
51}
52} // namespace
53
Robert Carr78c25dd2019-08-15 14:10:33 -070054namespace android {
55
Vishnu Nairdab94092020-09-29 16:09:04 -070056// Macros to include adapter info in log messages
chaviwd7deef72021-10-06 11:53:40 -050057#define BQA_LOGD(x, ...) \
58 ALOGD("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070059#define BQA_LOGV(x, ...) \
60 ALOGV("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairc6f89ee2020-12-11 14:27:32 -080061// enable logs for a single layer
62//#define BQA_LOGV(x, ...) \
63// ALOGV_IF((strstr(mName.c_str(), "SurfaceView") != nullptr), "[%s](f:%u,a:%u) " x, \
64// mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070065#define BQA_LOGE(x, ...) \
66 ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
67
chaviw57ae4b22022-02-03 16:51:39 -060068#define BBQ_TRACE(x, ...) \
69 ATRACE_FORMAT("%s - %s(f:%u,a:%u)" x, __FUNCTION__, mName.c_str(), mNumFrameAvailable, \
70 mNumAcquired, ##__VA_ARGS__)
71
Chavi Weingartene0237bb2023-02-06 21:48:32 +000072#define UNIQUE_LOCK_WITH_ASSERTION(mutex) \
73 std::unique_lock _lock{mutex}; \
74 base::ScopedLockAssertion assumeLocked(mutex);
75
Valerie Hau871d6352020-01-29 08:44:02 -080076void BLASTBufferItemConsumer::onDisconnect() {
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000077 Mutex::Autolock lock(mMutex);
78 mPreviouslyConnected = mCurrentlyConnected;
79 mCurrentlyConnected = false;
80 if (mPreviouslyConnected) {
81 mDisconnectEvents.push(mCurrentFrameNumber);
Valerie Hau871d6352020-01-29 08:44:02 -080082 }
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000083 mFrameEventHistory.onDisconnect();
Valerie Hau871d6352020-01-29 08:44:02 -080084}
85
86void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
87 FrameEventHistoryDelta* outDelta) {
Hongguang Chen621ec582021-02-16 15:42:35 -080088 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080089 if (newTimestamps) {
90 // BufferQueueProducer only adds a new timestamp on
91 // queueBuffer
92 mCurrentFrameNumber = newTimestamps->frameNumber;
93 mFrameEventHistory.addQueue(*newTimestamps);
94 }
95 if (outDelta) {
96 // frame event histories will be processed
97 // only after the producer connects and requests
98 // deltas for the first time. Forward this intent
99 // to SF-side to turn event processing back on
100 mPreviouslyConnected = mCurrentlyConnected;
101 mCurrentlyConnected = true;
102 mFrameEventHistory.getAndResetDelta(outDelta);
103 }
104}
105
Alec Mouri21d94322023-10-17 19:51:39 +0000106void BLASTBufferItemConsumer::updateFrameTimestamps(
107 uint64_t frameNumber, uint64_t previousFrameNumber, nsecs_t refreshStartTime,
108 const sp<Fence>& glDoneFence, const sp<Fence>& presentFence,
109 const sp<Fence>& prevReleaseFence, CompositorTiming compositorTiming, nsecs_t latchTime,
110 nsecs_t dequeueReadyTime) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800111 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800112
113 // if the producer is not connected, don't bother updating,
114 // the next producer that connects won't access this frame event
115 if (!mCurrentlyConnected) return;
116 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
117 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
118 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
119
120 mFrameEventHistory.addLatch(frameNumber, latchTime);
Alec Mouri21d94322023-10-17 19:51:39 +0000121 if (flags::frametimestamps_previousrelease()) {
122 if (previousFrameNumber > 0) {
123 mFrameEventHistory.addRelease(previousFrameNumber, dequeueReadyTime,
124 std::move(releaseFenceTime));
125 }
126 } else {
127 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
128 }
129
Valerie Hau871d6352020-01-29 08:44:02 -0800130 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
131 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
132 compositorTiming);
133}
134
135void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
136 bool disconnect = false;
Hongguang Chen621ec582021-02-16 15:42:35 -0800137 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800138 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
139 disconnect = true;
140 mDisconnectEvents.pop();
141 }
142 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
143}
144
Hongguang Chen621ec582021-02-16 15:42:35 -0800145void BLASTBufferItemConsumer::onSidebandStreamChanged() {
Ady Abrahamdbca1352021-12-15 11:58:56 -0800146 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
147 if (bbq != nullptr) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800148 sp<NativeHandle> stream = getSidebandStream();
Ady Abrahamdbca1352021-12-15 11:58:56 -0800149 bbq->setSidebandStream(stream);
Hongguang Chen621ec582021-02-16 15:42:35 -0800150 }
151}
152
Ady Abraham107788e2023-10-17 12:31:08 -0700153#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -0700154void BLASTBufferItemConsumer::onSetFrameRate(float frameRate, int8_t compatibility,
155 int8_t changeFrameRateStrategy) {
156 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
157 if (bbq != nullptr) {
158 bbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
159 }
160}
161#endif
162
Brian Lindahlc794b692023-01-31 15:42:47 -0700163void BLASTBufferItemConsumer::resizeFrameEventHistory(size_t newSize) {
164 Mutex::Autolock lock(mMutex);
165 mFrameEventHistory.resize(newSize);
166}
167
Vishnu Naird2aaab12022-02-10 14:49:09 -0800168BLASTBufferQueue::BLASTBufferQueue(const std::string& name, bool updateDestinationFrame)
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800169 : mSurfaceControl(nullptr),
170 mSize(1, 1),
Vishnu Nairea0de002020-11-17 17:42:37 -0800171 mRequestedSize(mSize),
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800172 mFormat(PIXEL_FORMAT_RGBA_8888),
Tianhao Yao4861b102022-02-03 20:18:35 +0000173 mTransactionReadyCallback(nullptr),
Vishnu Naird2aaab12022-02-10 14:49:09 -0800174 mSyncTransaction(nullptr),
175 mUpdateDestinationFrame(updateDestinationFrame) {
Vishnu Nair89496122020-12-14 17:14:53 -0800176 createBufferQueue(&mProducer, &mConsumer);
Valerie Hau0889c622020-02-19 15:04:47 -0800177 // since the adapter is in the client process, set dequeue timeout
178 // explicitly so that dequeueBuffer will block
179 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
Valerie Hau65b8e872020-02-13 09:45:14 -0800180
Vishnu Nair1618c672021-02-05 13:08:26 -0800181 mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
182 GraphicBuffer::USAGE_HW_COMPOSER |
183 GraphicBuffer::USAGE_HW_TEXTURE,
Ady Abrahamdbca1352021-12-15 11:58:56 -0800184 1, false, this);
liulijuneb489f62022-10-17 22:02:14 +0800185 static std::atomic<uint32_t> nextId = 0;
186 mProducerId = nextId++;
187 mName = name + "#" + std::to_string(mProducerId);
188 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(mProducerId);
189 mQueuedBufferTrace = "QueuedBuffer - " + mName + "BLAST#" + std::to_string(mProducerId);
Vishnu Nairdab94092020-09-29 16:09:04 -0700190 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700191 mBufferItemConsumer->setFrameAvailableListener(this);
Robert Carr9f133d72020-04-01 15:51:46 -0700192
Huihong Luo02186fb2022-02-23 14:21:54 -0800193 ComposerServiceAIDL::getComposerService()->getMaxAcquiredBufferCount(&mMaxAcquiredBuffers);
Ady Abraham0bde6b52021-05-18 13:57:02 -0700194 mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxAcquiredBuffers);
chaviw69058fb2021-09-27 09:37:30 -0500195 mCurrentMaxAcquiredBufferCount = mMaxAcquiredBuffers;
Valerie Haua32c5522019-12-09 10:11:08 -0800196 mNumAcquired = 0;
197 mNumFrameAvailable = 0;
Robert Carr4c1b6462021-12-21 10:30:50 -0800198
199 TransactionCompletedListener::getInstance()->addQueueStallListener(
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000200 [&](const std::string& reason) {
201 std::function<void(const std::string&)> callbackCopy;
202 {
203 std::unique_lock _lock{mMutex};
204 callbackCopy = mTransactionHangCallback;
205 }
206 if (callbackCopy) callbackCopy(reason);
207 },
208 this);
Robert Carr4c1b6462021-12-21 10:30:50 -0800209
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800210 BQA_LOGV("BLASTBufferQueue created");
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800211}
212
213BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface,
214 int width, int height, int32_t format)
215 : BLASTBufferQueue(name) {
216 update(surface, width, height, format);
Robert Carr78c25dd2019-08-15 14:10:33 -0700217}
218
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800219BLASTBufferQueue::~BLASTBufferQueue() {
Robert Carr4c1b6462021-12-21 10:30:50 -0800220 TransactionCompletedListener::getInstance()->removeQueueStallListener(this);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800221 if (mPendingTransactions.empty()) {
222 return;
223 }
224 BQA_LOGE("Applying pending transactions on dtor %d",
225 static_cast<uint32_t>(mPendingTransactions.size()));
226 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800227 mergePendingTransactions(&t, std::numeric_limits<uint64_t>::max() /* frameNumber */);
Robert Carr79dc06a2022-02-22 15:28:59 -0800228 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
229 t.setApplyToken(mApplyToken).apply(false, true);
chaviw3b4bdcf2022-03-17 09:27:03 -0500230
231 if (mTransactionReadyCallback) {
232 mTransactionReadyCallback(mSyncTransaction);
233 }
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800234}
235
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500236void BLASTBufferQueue::onFirstRef() {
237 // safe default, most producers are expected to override this
238 mProducer->setMaxDequeuedBufferCount(2);
239}
240
chaviw565ee542021-01-14 10:21:23 -0800241void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height,
Vishnu Naird2aaab12022-02-10 14:49:09 -0800242 int32_t format) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800243 LOG_ALWAYS_FATAL_IF(surface == nullptr, "BLASTBufferQueue: mSurfaceControl must not be NULL");
244
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000245 std::lock_guard _lock{mMutex};
chaviw565ee542021-01-14 10:21:23 -0800246 if (mFormat != format) {
247 mFormat = format;
chaviw497e81c2021-02-04 17:09:47 -0800248 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
chaviw565ee542021-01-14 10:21:23 -0800249 }
250
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800251 const bool surfaceControlChanged = !SurfaceControl::isSameSurface(mSurfaceControl, surface);
Vishnu Nairab066512022-01-04 22:28:00 +0000252 if (surfaceControlChanged && mSurfaceControl != nullptr) {
253 BQA_LOGD("Updating SurfaceControl without recreating BBQ");
254 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800255 bool applyTransaction = false;
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800256
Vishnu Nair5fa91c22021-06-29 14:30:48 -0700257 // Always update the native object even though they might have the same layer handle, so we can
258 // get the updated transform hint from WM.
259 mSurfaceControl = surface;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800260 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800261 if (surfaceControlChanged) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800262 t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure,
263 layer_state_t::eEnableBackpressure);
264 applyTransaction = true;
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800265 }
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800266 mTransformHint = mSurfaceControl->getTransformHint();
267 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Naira4fbca52021-07-07 16:52:34 -0700268 BQA_LOGV("update width=%d height=%d format=%d mTransformHint=%d", width, height, format,
269 mTransformHint);
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800270
Vishnu Nairea0de002020-11-17 17:42:37 -0800271 ui::Size newSize(width, height);
272 if (mRequestedSize != newSize) {
273 mRequestedSize.set(newSize);
274 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000275 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800276 // If the buffer supports scaling, update the frame immediately since the client may
277 // want to scale the existing buffer to the new size.
278 mSize = mRequestedSize;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800279 if (mUpdateDestinationFrame) {
280 t.setDestinationFrame(mSurfaceControl, Rect(newSize));
281 applyTransaction = true;
282 }
Vishnu Nair53c936c2020-12-03 11:46:37 -0800283 }
Robert Carrfc416512020-04-02 12:32:44 -0700284 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800285 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800286 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
287 t.setApplyToken(mApplyToken).apply(false, true);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800288 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700289}
290
chaviwd7deef72021-10-06 11:53:40 -0500291static std::optional<SurfaceControlStats> findMatchingStat(
292 const std::vector<SurfaceControlStats>& stats, const sp<SurfaceControl>& sc) {
293 for (auto stat : stats) {
294 if (SurfaceControl::isSameSurface(sc, stat.surfaceControl)) {
295 return stat;
296 }
297 }
298 return std::nullopt;
299}
300
301static void transactionCommittedCallbackThunk(void* context, nsecs_t latchTime,
302 const sp<Fence>& presentFence,
303 const std::vector<SurfaceControlStats>& stats) {
304 if (context == nullptr) {
305 return;
306 }
307 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
308 bq->transactionCommittedCallback(latchTime, presentFence, stats);
309}
310
311void BLASTBufferQueue::transactionCommittedCallback(nsecs_t /*latchTime*/,
312 const sp<Fence>& /*presentFence*/,
313 const std::vector<SurfaceControlStats>& stats) {
314 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000315 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600316 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500317 BQA_LOGV("transactionCommittedCallback");
318 if (!mSurfaceControlsWithPendingCallback.empty()) {
319 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
320 std::optional<SurfaceControlStats> stat = findMatchingStat(stats, pendingSC);
321 if (stat) {
322 uint64_t currFrameNumber = stat->frameEventStats.frameNumber;
323
324 // We need to check if we were waiting for a transaction callback in order to
325 // process any pending buffers and unblock. It's possible to get transaction
chaviwc1cf4022022-06-03 13:32:33 -0500326 // callbacks for previous requests so we need to ensure that there are no pending
327 // frame numbers that were in a sync. We remove the frame from mSyncedFrameNumbers
328 // set and then check if it's empty. If there are no more pending syncs, we can
329 // proceed with flushing the shadow queue.
chaviwc1cf4022022-06-03 13:32:33 -0500330 mSyncedFrameNumbers.erase(currFrameNumber);
Chavi Weingartend48797b2023-08-04 13:11:39 +0000331 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500332 flushShadowQueue();
333 }
334 } else {
chaviw768bfa02021-11-01 09:50:57 -0500335 BQA_LOGE("Failed to find matching SurfaceControl in transactionCommittedCallback");
chaviwd7deef72021-10-06 11:53:40 -0500336 }
337 } else {
338 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
339 "empty.");
340 }
chaviwd7deef72021-10-06 11:53:40 -0500341 decStrong((void*)transactionCommittedCallbackThunk);
342 }
343}
344
Robert Carr78c25dd2019-08-15 14:10:33 -0700345static void transactionCallbackThunk(void* context, nsecs_t latchTime,
346 const sp<Fence>& presentFence,
347 const std::vector<SurfaceControlStats>& stats) {
348 if (context == nullptr) {
349 return;
350 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800351 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700352 bq->transactionCallback(latchTime, presentFence, stats);
353}
354
355void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
356 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700357 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000358 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600359 BBQ_TRACE();
chaviw71c2cc42020-10-23 16:42:02 -0700360 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700361
chaviw42026162021-04-16 15:46:12 -0500362 if (!mSurfaceControlsWithPendingCallback.empty()) {
363 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
364 mSurfaceControlsWithPendingCallback.pop();
chaviwd7deef72021-10-06 11:53:40 -0500365 std::optional<SurfaceControlStats> statsOptional = findMatchingStat(stats, pendingSC);
366 if (statsOptional) {
367 SurfaceControlStats stat = *statsOptional;
Vishnu Nair71fcf912022-10-18 09:14:20 -0700368 if (stat.transformHint) {
369 mTransformHint = *stat.transformHint;
370 mBufferItemConsumer->setTransformHint(mTransformHint);
371 BQA_LOGV("updated mTransformHint=%d", mTransformHint);
372 }
Vishnu Nairde66dc72021-06-17 17:54:41 -0700373 // Update frametime stamps if the frame was latched and presented, indicated by a
374 // valid latch time.
375 if (stat.latchTime > 0) {
376 mBufferItemConsumer
377 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
Alec Mouri21d94322023-10-17 19:51:39 +0000378 stat.frameEventStats.previousFrameNumber,
Vishnu Nairde66dc72021-06-17 17:54:41 -0700379 stat.frameEventStats.refreshStartTime,
380 stat.frameEventStats.gpuCompositionDoneFence,
381 stat.presentFence, stat.previousReleaseFence,
382 stat.frameEventStats.compositorTiming,
383 stat.latchTime,
384 stat.frameEventStats.dequeueReadyTime);
385 }
Robert Carr405e2f62021-12-31 16:59:34 -0800386 auto currFrameNumber = stat.frameEventStats.frameNumber;
387 std::vector<ReleaseCallbackId> staleReleases;
388 for (const auto& [key, value]: mSubmitted) {
389 if (currFrameNumber > key.framenumber) {
390 staleReleases.push_back(key);
391 }
392 }
393 for (const auto& staleRelease : staleReleases) {
Robert Carr405e2f62021-12-31 16:59:34 -0800394 releaseBufferCallbackLocked(staleRelease,
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700395 stat.previousReleaseFence
396 ? stat.previousReleaseFence
397 : Fence::NO_FENCE,
398 stat.currentMaxAcquiredBufferCount,
399 true /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800400 }
chaviwd7deef72021-10-06 11:53:40 -0500401 } else {
chaviw768bfa02021-11-01 09:50:57 -0500402 BQA_LOGE("Failed to find matching SurfaceControl in transactionCallback");
chaviw42026162021-04-16 15:46:12 -0500403 }
404 } else {
405 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
406 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800407 }
chaviw71c2cc42020-10-23 16:42:02 -0700408
chaviw71c2cc42020-10-23 16:42:02 -0700409 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700410 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700411}
412
Vishnu Nair1506b182021-02-22 14:35:15 -0800413// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
414// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
415// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
416// Otherwise, this is a no-op.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700417static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, const ReleaseCallbackId& id,
chaviw69058fb2021-09-27 09:37:30 -0500418 const sp<Fence>& releaseFence,
419 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800420 sp<BLASTBufferQueue> blastBufferQueue = context.promote();
Vishnu Nair1506b182021-02-22 14:35:15 -0800421 if (blastBufferQueue) {
chaviw69058fb2021-09-27 09:37:30 -0500422 blastBufferQueue->releaseBufferCallback(id, releaseFence, currentMaxAcquiredBufferCount);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700423 } else {
424 ALOGV("releaseBufferCallbackThunk %s blastBufferQueue is dead", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800425 }
426}
427
chaviwd7deef72021-10-06 11:53:40 -0500428void BLASTBufferQueue::flushShadowQueue() {
429 BQA_LOGV("flushShadowQueue");
430 int numFramesToFlush = mNumFrameAvailable;
431 while (numFramesToFlush > 0) {
432 acquireNextBufferLocked(std::nullopt);
433 numFramesToFlush--;
434 }
435}
436
chaviw69058fb2021-09-27 09:37:30 -0500437void BLASTBufferQueue::releaseBufferCallback(
438 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
439 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000440 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600441 BBQ_TRACE();
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700442 releaseBufferCallbackLocked(id, releaseFence, currentMaxAcquiredBufferCount,
443 false /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800444}
445
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700446void BLASTBufferQueue::releaseBufferCallbackLocked(
447 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
448 std::optional<uint32_t> currentMaxAcquiredBufferCount, bool fakeRelease) {
Robert Carr405e2f62021-12-31 16:59:34 -0800449 ATRACE_CALL();
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700450 BQA_LOGV("releaseBufferCallback %s", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800451
Ady Abraham899dcdb2021-06-15 16:56:21 -0700452 // Calculate how many buffers we need to hold before we release them back
453 // to the buffer queue. This will prevent higher latency when we are running
454 // on a lower refresh rate than the max supported. We only do that for EGL
455 // clients as others don't care about latency
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000456 const auto it = mSubmitted.find(id);
457 const bool isEGL = it != mSubmitted.end() && it->second.mApi == NATIVE_WINDOW_API_EGL;
Ady Abraham899dcdb2021-06-15 16:56:21 -0700458
chaviw69058fb2021-09-27 09:37:30 -0500459 if (currentMaxAcquiredBufferCount) {
460 mCurrentMaxAcquiredBufferCount = *currentMaxAcquiredBufferCount;
461 }
462
liulijunf90df632022-11-14 14:24:48 +0800463 const uint32_t numPendingBuffersToHold =
464 isEGL ? std::max(0, mMaxAcquiredBuffers - (int32_t)mCurrentMaxAcquiredBufferCount) : 0;
Robert Carr405e2f62021-12-31 16:59:34 -0800465
466 auto rb = ReleasedBuffer{id, releaseFence};
467 if (std::find(mPendingRelease.begin(), mPendingRelease.end(), rb) == mPendingRelease.end()) {
468 mPendingRelease.emplace_back(rb);
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700469 if (fakeRelease) {
470 BQA_LOGE("Faking releaseBufferCallback from transactionCompleteCallback %" PRIu64,
471 id.framenumber);
472 BBQ_TRACE("FakeReleaseCallback");
473 }
Robert Carr405e2f62021-12-31 16:59:34 -0800474 }
Ady Abraham899dcdb2021-06-15 16:56:21 -0700475
476 // Release all buffers that are beyond the ones that we need to hold
477 while (mPendingRelease.size() > numPendingBuffersToHold) {
chaviw0acd33a2021-11-02 11:55:37 -0500478 const auto releasedBuffer = mPendingRelease.front();
Ady Abraham899dcdb2021-06-15 16:56:21 -0700479 mPendingRelease.pop_front();
chaviw0acd33a2021-11-02 11:55:37 -0500480 releaseBuffer(releasedBuffer.callbackId, releasedBuffer.releaseFence);
chaviwc1cf4022022-06-03 13:32:33 -0500481 // Don't process the transactions here if mSyncedFrameNumbers is not empty. That means
482 // are still transactions that have sync buffers in them that have not been applied or
483 // dropped. Instead, let onFrameAvailable handle processing them since it will merge with
484 // the syncTransaction.
485 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500486 acquireNextBufferLocked(std::nullopt);
487 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800488 }
489
Ady Abraham899dcdb2021-06-15 16:56:21 -0700490 ATRACE_INT("PendingRelease", mPendingRelease.size());
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700491 ATRACE_INT(mQueuedBufferTrace.c_str(),
492 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
Vishnu Nair1506b182021-02-22 14:35:15 -0800493 mCallbackCV.notify_all();
494}
495
chaviw0acd33a2021-11-02 11:55:37 -0500496void BLASTBufferQueue::releaseBuffer(const ReleaseCallbackId& callbackId,
497 const sp<Fence>& releaseFence) {
498 auto it = mSubmitted.find(callbackId);
499 if (it == mSubmitted.end()) {
Patrick Williams4b9507d2024-07-25 09:55:52 -0500500 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %s",
501 callbackId.to_string().c_str());
chaviw0acd33a2021-11-02 11:55:37 -0500502 return;
503 }
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500504#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
505 if (!it->second.mIsStale) {
506 mNumAcquired--;
507 }
508#else
chaviw0acd33a2021-11-02 11:55:37 -0500509 mNumAcquired--;
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500510#endif
chaviw57ae4b22022-02-03 16:51:39 -0600511 BBQ_TRACE("frame=%" PRIu64, callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500512 BQA_LOGV("released %s", callbackId.to_string().c_str());
513 mBufferItemConsumer->releaseBuffer(it->second, releaseFence);
514 mSubmitted.erase(it);
chaviwc1cf4022022-06-03 13:32:33 -0500515 // Remove the frame number from mSyncedFrameNumbers since we can get a release callback
516 // without getting a transaction committed if the buffer was dropped.
517 mSyncedFrameNumbers.erase(callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500518}
519
Chavi Weingarten70670e62023-02-22 17:36:40 +0000520static ui::Size getBufferSize(const BufferItem& item) {
521 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
522 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
523
524 // Take the buffer's orientation into account
525 if (item.mTransform & ui::Transform::ROT_90) {
526 std::swap(bufWidth, bufHeight);
527 }
528 return ui::Size(bufWidth, bufHeight);
529}
530
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000531status_t BLASTBufferQueue::acquireNextBufferLocked(
chaviwd7deef72021-10-06 11:53:40 -0500532 const std::optional<SurfaceComposerClient::Transaction*> transaction) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800533 // Check if we have frames available and we have not acquired the maximum number of buffers.
534 // Even with this check, the consumer can fail to acquire an additional buffer if the consumer
535 // has already acquired (mMaxAcquiredBuffers + 1) and the new buffer is not droppable. In this
536 // case mBufferItemConsumer->acquireBuffer will return with NO_BUFFER_AVAILABLE.
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000537 if (mNumFrameAvailable == 0) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800538 BQA_LOGV("Can't acquire next buffer. No available frames");
539 return BufferQueue::NO_BUFFER_AVAILABLE;
540 }
541
542 if (mNumAcquired >= (mMaxAcquiredBuffers + 2)) {
543 BQA_LOGV("Can't acquire next buffer. Already acquired max frames %d max:%d + 2",
544 mNumAcquired, mMaxAcquiredBuffers);
545 return BufferQueue::NO_BUFFER_AVAILABLE;
Valerie Haud3b90d22019-11-06 09:37:31 -0800546 }
547
Valerie Haua32c5522019-12-09 10:11:08 -0800548 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700549 BQA_LOGE("ERROR : surface control is null");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000550 return NAME_NOT_FOUND;
Valerie Haud3b90d22019-11-06 09:37:31 -0800551 }
552
Robert Carr78c25dd2019-08-15 14:10:33 -0700553 SurfaceComposerClient::Transaction localTransaction;
554 bool applyTransaction = true;
555 SurfaceComposerClient::Transaction* t = &localTransaction;
chaviwd7deef72021-10-06 11:53:40 -0500556 if (transaction) {
557 t = *transaction;
Robert Carr78c25dd2019-08-15 14:10:33 -0700558 applyTransaction = false;
559 }
560
Valerie Haua32c5522019-12-09 10:11:08 -0800561 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800562
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800563 status_t status =
564 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800565 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
566 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000567 return status;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800568 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700569 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000570 return status;
Robert Carr78c25dd2019-08-15 14:10:33 -0700571 }
chaviw57ae4b22022-02-03 16:51:39 -0600572
Valerie Haua32c5522019-12-09 10:11:08 -0800573 auto buffer = bufferItem.mGraphicBuffer;
574 mNumFrameAvailable--;
chaviw57ae4b22022-02-03 16:51:39 -0600575 BBQ_TRACE("frame=%" PRIu64, bufferItem.mFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800576
577 if (buffer == nullptr) {
578 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700579 BQA_LOGE("Buffer was empty");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000580 return BAD_VALUE;
Valerie Haua32c5522019-12-09 10:11:08 -0800581 }
582
Vishnu Nair670b3f72020-09-29 17:52:18 -0700583 if (rejectBuffer(bufferItem)) {
Vishnu Naira4fbca52021-07-07 16:52:34 -0700584 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d "
Vishnu Nairea0de002020-11-17 17:42:37 -0800585 "buffer{size=%dx%d transform=%d}",
586 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
587 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
588 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000589 return acquireNextBufferLocked(transaction);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700590 }
591
Valerie Haua32c5522019-12-09 10:11:08 -0800592 mNumAcquired++;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700593 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
594 ReleaseCallbackId releaseCallbackId(buffer->getId(), mLastAcquiredFrameNumber);
595 mSubmitted[releaseCallbackId] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700596
Valerie Hau871d6352020-01-29 08:44:02 -0800597 bool needsDisconnect = false;
598 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
599
600 // if producer disconnected before, notify SurfaceFlinger
601 if (needsDisconnect) {
602 t->notifyProducerDisconnect(mSurfaceControl);
603 }
604
Robert Carr78c25dd2019-08-15 14:10:33 -0700605 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
606 incStrong((void*)transactionCallbackThunk);
607
Chavi Weingarten70670e62023-02-22 17:36:40 +0000608 // Only update mSize for destination bounds if the incoming buffer matches the requested size.
609 // Otherwise, it could cause stretching since the destination bounds will update before the
610 // buffer with the new size is acquired.
Vishnu Nair5b5f6932023-04-12 16:28:19 -0700611 if (mRequestedSize == getBufferSize(bufferItem) ||
612 bufferItem.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Chavi Weingarten70670e62023-02-22 17:36:40 +0000613 mSize = mRequestedSize;
614 }
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700615 Rect crop = computeCrop(bufferItem);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000616 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
617 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700618 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800619
Vishnu Nair1506b182021-02-22 14:35:15 -0800620 auto releaseBufferCallback =
621 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
chaviw69058fb2021-09-27 09:37:30 -0500622 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
chaviwba4320c2021-09-15 15:20:53 -0500623 sp<Fence> fence = bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE;
Nergi Rahardi39f510f2024-05-23 15:16:54 +0900624
625 nsecs_t dequeueTime = -1;
626 {
627 std::lock_guard _lock{mTimestampMutex};
628 auto dequeueTimeIt = mDequeueTimestamps.find(buffer->getId());
629 if (dequeueTimeIt != mDequeueTimestamps.end()) {
630 dequeueTime = dequeueTimeIt->second;
631 mDequeueTimestamps.erase(dequeueTimeIt);
632 }
633 }
634
liulijuneb489f62022-10-17 22:02:14 +0800635 t->setBuffer(mSurfaceControl, buffer, fence, bufferItem.mFrameNumber, mProducerId,
Nergi Rahardi39f510f2024-05-23 15:16:54 +0900636 releaseBufferCallback, dequeueTime);
John Reck137069e2020-12-10 22:07:37 -0500637 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
638 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
639 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700640 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviwf2dace72021-11-17 17:36:50 -0600641
chaviw42026162021-04-16 15:46:12 -0500642 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700643
Vishnu Naird2aaab12022-02-10 14:49:09 -0800644 if (mUpdateDestinationFrame) {
645 t->setDestinationFrame(mSurfaceControl, Rect(mSize));
646 } else {
647 const bool ignoreDestinationFrame =
648 bufferItem.mScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE;
649 t->setFlags(mSurfaceControl,
650 ignoreDestinationFrame ? layer_state_t::eIgnoreDestinationFrame : 0,
651 layer_state_t::eIgnoreDestinationFrame);
Vishnu Nair084514a2021-07-30 16:07:42 -0700652 }
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700653 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800654 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800655 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Vishnu Naird2aaab12022-02-10 14:49:09 -0800656 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800657 if (!bufferItem.mIsAutoTimestamp) {
658 t->setDesiredPresentTime(bufferItem.mTimestamp);
659 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700660
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800661 // Drop stale frame timeline infos
662 while (!mPendingFrameTimelines.empty() &&
663 mPendingFrameTimelines.front().first < bufferItem.mFrameNumber) {
664 ATRACE_FORMAT_INSTANT("dropping stale frameNumber: %" PRIu64 " vsyncId: %" PRId64,
665 mPendingFrameTimelines.front().first,
666 mPendingFrameTimelines.front().second.vsyncId);
667 mPendingFrameTimelines.pop();
668 }
669
670 if (!mPendingFrameTimelines.empty() &&
671 mPendingFrameTimelines.front().first == bufferItem.mFrameNumber) {
672 ATRACE_FORMAT_INSTANT("Transaction::setFrameTimelineInfo frameNumber: %" PRIu64
673 " vsyncId: %" PRId64,
674 bufferItem.mFrameNumber,
675 mPendingFrameTimelines.front().second.vsyncId);
676 t->setFrameTimelineInfo(mPendingFrameTimelines.front().second);
677 mPendingFrameTimelines.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100678 }
679
chaviw6a195272021-09-03 16:14:25 -0500680 mergePendingTransactions(t, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700681 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800682 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
683 t->setApplyToken(mApplyToken).apply(false, true);
684 mAppliedLastTransaction = true;
685 mLastAppliedFrameNumber = bufferItem.mFrameNumber;
686 } else {
687 t->setBufferHasBarrier(mSurfaceControl, mLastAppliedFrameNumber);
688 mAppliedLastTransaction = false;
Robert Carr78c25dd2019-08-15 14:10:33 -0700689 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700690
chaviwd7deef72021-10-06 11:53:40 -0500691 BQA_LOGV("acquireNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800692 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
Vishnu Naira4fbca52021-07-07 16:52:34 -0700693 " graphicBufferId=%" PRIu64 "%s transform=%d",
chaviw3277faf2021-05-19 16:45:23 -0500694 mSize.width, mSize.height, bufferItem.mFrameNumber, boolToString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800695 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700696 static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(),
Vishnu Naira4fbca52021-07-07 16:52:34 -0700697 bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000698 return OK;
Robert Carr78c25dd2019-08-15 14:10:33 -0700699}
700
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800701Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
702 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800703 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800704 }
705 return item.mCrop;
706}
707
chaviwd7deef72021-10-06 11:53:40 -0500708void BLASTBufferQueue::acquireAndReleaseBuffer() {
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000709 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500710 BufferItem bufferItem;
chaviw6ebdf5f2021-10-14 11:57:22 -0500711 status_t status =
712 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
713 if (status != OK) {
714 BQA_LOGE("Failed to acquire a buffer in acquireAndReleaseBuffer, err=%s",
715 statusToString(status).c_str());
716 return;
717 }
chaviwd7deef72021-10-06 11:53:40 -0500718 mNumFrameAvailable--;
chaviw6ebdf5f2021-10-14 11:57:22 -0500719 mBufferItemConsumer->releaseBuffer(bufferItem, bufferItem.mFence);
chaviwd7deef72021-10-06 11:53:40 -0500720}
721
Vishnu Nairaef1de92020-10-22 12:15:53 -0700722void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000723 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
724 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
chaviwc1cf4022022-06-03 13:32:33 -0500725
Tianhao Yao4861b102022-02-03 20:18:35 +0000726 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000727 UNIQUE_LOCK_WITH_ASSERTION(mMutex);
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000728 BBQ_TRACE();
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000729 bool waitForTransactionCallback = !mSyncedFrameNumbers.empty();
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800730
Tianhao Yao4861b102022-02-03 20:18:35 +0000731 const bool syncTransactionSet = mTransactionReadyCallback != nullptr;
732 BQA_LOGV("onFrameAvailable-start syncTransactionSet=%s", boolToString(syncTransactionSet));
Valerie Haud3b90d22019-11-06 09:37:31 -0800733
Tianhao Yao4861b102022-02-03 20:18:35 +0000734 if (syncTransactionSet) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000735 // If we are going to re-use the same mSyncTransaction, release the buffer that may
736 // already be set in the Transaction. This is to allow us a free slot early to continue
737 // processing a new buffer.
738 if (!mAcquireSingleBuffer) {
739 auto bufferData = mSyncTransaction->getAndClearBuffer(mSurfaceControl);
740 if (bufferData) {
741 BQA_LOGD("Releasing previous buffer when syncing: framenumber=%" PRIu64,
742 bufferData->frameNumber);
743 releaseBuffer(bufferData->generateReleaseCallbackId(),
744 bufferData->acquireFence);
Tianhao Yao4861b102022-02-03 20:18:35 +0000745 }
746 }
chaviw0acd33a2021-11-02 11:55:37 -0500747
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000748 if (waitForTransactionCallback) {
749 // We are waiting on a previous sync's transaction callback so allow another sync
750 // transaction to proceed.
751 //
752 // We need to first flush out the transactions that were in between the two syncs.
753 // We do this by merging them into mSyncTransaction so any buffer merging will get
754 // a release callback invoked.
755 while (mNumFrameAvailable > 0) {
756 // flush out the shadow queue
757 acquireAndReleaseBuffer();
758 }
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800759 } else {
760 // Make sure the frame available count is 0 before proceeding with a sync to ensure
761 // the correct frame is used for the sync. The only way mNumFrameAvailable would be
762 // greater than 0 is if we already ran out of buffers previously. This means we
763 // need to flush the buffers before proceeding with the sync.
764 while (mNumFrameAvailable > 0) {
765 BQA_LOGD("waiting until no queued buffers");
766 mCallbackCV.wait(_lock);
767 }
chaviwd7deef72021-10-06 11:53:40 -0500768 }
769 }
770
Tianhao Yao4861b102022-02-03 20:18:35 +0000771 // add to shadow queue
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500772#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
773 mNumDequeued--;
774#endif
Tianhao Yao4861b102022-02-03 20:18:35 +0000775 mNumFrameAvailable++;
chaviwc1cf4022022-06-03 13:32:33 -0500776 if (waitForTransactionCallback && mNumFrameAvailable >= 2) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000777 acquireAndReleaseBuffer();
778 }
779 ATRACE_INT(mQueuedBufferTrace.c_str(),
780 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
781
782 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " syncTransactionSet=%s",
783 item.mFrameNumber, boolToString(syncTransactionSet));
784
785 if (syncTransactionSet) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800786 // Add to mSyncedFrameNumbers before waiting in case any buffers are released
787 // while waiting for a free buffer. The release and commit callback will try to
788 // acquire buffers if there are any available, but we don't want it to acquire
789 // in the case where a sync transaction wants the buffer.
790 mSyncedFrameNumbers.emplace(item.mFrameNumber);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000791 // If there's no available buffer and we're in a sync transaction, we need to wait
792 // instead of returning since we guarantee a buffer will be acquired for the sync.
793 while (acquireNextBufferLocked(mSyncTransaction) == BufferQueue::NO_BUFFER_AVAILABLE) {
794 BQA_LOGD("waiting for available buffer");
795 mCallbackCV.wait(_lock);
796 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000797
798 // Only need a commit callback when syncing to ensure the buffer that's synced has been
799 // sent to SF
800 incStrong((void*)transactionCommittedCallbackThunk);
801 mSyncTransaction->addTransactionCommittedCallback(transactionCommittedCallbackThunk,
802 static_cast<void*>(this));
Tianhao Yao4861b102022-02-03 20:18:35 +0000803 if (mAcquireSingleBuffer) {
804 prevCallback = mTransactionReadyCallback;
805 prevTransaction = mSyncTransaction;
806 mTransactionReadyCallback = nullptr;
807 mSyncTransaction = nullptr;
808 }
chaviwc1cf4022022-06-03 13:32:33 -0500809 } else if (!waitForTransactionCallback) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000810 acquireNextBufferLocked(std::nullopt);
Valerie Hau0188adf2020-02-13 08:29:20 -0800811 }
812 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000813 if (prevCallback) {
814 prevCallback(prevTransaction);
chaviwd7deef72021-10-06 11:53:40 -0500815 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800816}
817
Vishnu Nairaef1de92020-10-22 12:15:53 -0700818void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
819 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
820 // Do nothing since we are not storing unacquired buffer items locally.
821}
822
Vishnu Nairadf632b2021-01-07 14:05:08 -0800823void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000824 std::lock_guard _lock{mTimestampMutex};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800825 mDequeueTimestamps[bufferId] = systemTime();
Patrick Williams4b9507d2024-07-25 09:55:52 -0500826};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800827
828void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500829 {
830 std::lock_guard _lock{mTimestampMutex};
831 mDequeueTimestamps.erase(bufferId);
832 }
833
834#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
835 {
836 std::lock_guard lock{mMutex};
837 mNumDequeued--;
838 }
839#endif
840}
Vishnu Nairadf632b2021-01-07 14:05:08 -0800841
Chavi Weingartenc398c012023-04-12 17:26:02 +0000842bool BLASTBufferQueue::syncNextTransaction(
Tianhao Yao4861b102022-02-03 20:18:35 +0000843 std::function<void(SurfaceComposerClient::Transaction*)> callback,
844 bool acquireSingleBuffer) {
Chavi Weingartenc398c012023-04-12 17:26:02 +0000845 LOG_ALWAYS_FATAL_IF(!callback,
846 "BLASTBufferQueue: callback passed in to syncNextTransaction must not be "
847 "NULL");
chaviw3b4bdcf2022-03-17 09:27:03 -0500848
Chavi Weingartenc398c012023-04-12 17:26:02 +0000849 std::lock_guard _lock{mMutex};
850 BBQ_TRACE();
851 if (mTransactionReadyCallback) {
852 ALOGW("Attempting to overwrite transaction callback in syncNextTransaction");
853 return false;
Tianhao Yao4861b102022-02-03 20:18:35 +0000854 }
chaviw3b4bdcf2022-03-17 09:27:03 -0500855
Chavi Weingartenc398c012023-04-12 17:26:02 +0000856 mTransactionReadyCallback = callback;
857 mSyncTransaction = new SurfaceComposerClient::Transaction();
858 mAcquireSingleBuffer = acquireSingleBuffer;
859 return true;
Tianhao Yao4861b102022-02-03 20:18:35 +0000860}
861
862void BLASTBufferQueue::stopContinuousSyncTransaction() {
863 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
864 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
865 {
866 std::lock_guard _lock{mMutex};
Chavi Weingartenc398c012023-04-12 17:26:02 +0000867 if (mAcquireSingleBuffer || !mTransactionReadyCallback) {
868 ALOGW("Attempting to stop continuous sync when none are active");
869 return;
Tianhao Yao4861b102022-02-03 20:18:35 +0000870 }
Chavi Weingartenc398c012023-04-12 17:26:02 +0000871
872 prevCallback = mTransactionReadyCallback;
873 prevTransaction = mSyncTransaction;
874
Tianhao Yao4861b102022-02-03 20:18:35 +0000875 mTransactionReadyCallback = nullptr;
876 mSyncTransaction = nullptr;
877 mAcquireSingleBuffer = true;
878 }
Chavi Weingartenc398c012023-04-12 17:26:02 +0000879
Tianhao Yao4861b102022-02-03 20:18:35 +0000880 if (prevCallback) {
881 prevCallback(prevTransaction);
882 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700883}
884
Chavi Weingartenc398c012023-04-12 17:26:02 +0000885void BLASTBufferQueue::clearSyncTransaction() {
886 std::lock_guard _lock{mMutex};
887 if (!mAcquireSingleBuffer) {
888 ALOGW("Attempting to clear sync transaction when none are active");
889 return;
890 }
891
892 mTransactionReadyCallback = nullptr;
893 mSyncTransaction = nullptr;
894}
895
Vishnu Nairea0de002020-11-17 17:42:37 -0800896bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700897 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
898 // Only reject buffers if scaling mode is freeze.
899 return false;
900 }
901
Chavi Weingarten70670e62023-02-22 17:36:40 +0000902 ui::Size bufferSize = getBufferSize(item);
Vishnu Nairea0de002020-11-17 17:42:37 -0800903 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800904 return false;
905 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700906
Vishnu Nair670b3f72020-09-29 17:52:18 -0700907 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800908 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700909}
Vishnu Nairbf255772020-10-16 10:54:41 -0700910
Robert Carr05086b22020-10-13 18:22:51 -0700911class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700912private:
Vishnu Nair95b6d512021-08-30 15:31:08 -0700913 std::mutex mMutex;
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000914 sp<BLASTBufferQueue> mBbq GUARDED_BY(mMutex);
915 bool mDestroyed GUARDED_BY(mMutex) = false;
Vishnu Nair95b6d512021-08-30 15:31:08 -0700916
Robert Carr05086b22020-10-13 18:22:51 -0700917public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700918 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
919 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
920 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700921
Robert Carr05086b22020-10-13 18:22:51 -0700922 void allocateBuffers() override {
923 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
924 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
925 auto gbp = getIGraphicBufferProducer();
926 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
927 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
928 gbp->allocateBuffers(reqWidth, reqHeight,
929 reqFormat, reqUsage);
930
931 }).detach();
932 }
Robert Carr9c006e02020-10-14 13:41:57 -0700933
Marin Shalamanovc5986772021-03-16 16:09:49 +0100934 status_t setFrameRate(float frameRate, int8_t compatibility,
935 int8_t changeFrameRateStrategy) override {
Ady Abraham6cdd3fd2023-09-07 18:45:58 -0700936 if (flags::bq_setframerate()) {
937 return Surface::setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
938 }
939
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000940 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700941 if (mDestroyed) {
942 return DEAD_OBJECT;
943 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100944 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
945 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700946 return BAD_VALUE;
947 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100948 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700949 }
Robert Carr9b611b72020-10-19 12:00:23 -0700950
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800951 status_t setFrameTimelineInfo(uint64_t frameNumber,
952 const FrameTimelineInfo& frameTimelineInfo) override {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000953 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700954 if (mDestroyed) {
955 return DEAD_OBJECT;
956 }
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800957 return mBbq->setFrameTimelineInfo(frameNumber, frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700958 }
Vishnu Nair95b6d512021-08-30 15:31:08 -0700959
960 void destroy() override {
961 Surface::destroy();
962
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000963 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700964 mDestroyed = true;
965 mBbq = nullptr;
966 }
Robert Carr05086b22020-10-13 18:22:51 -0700967};
968
Robert Carr9c006e02020-10-14 13:41:57 -0700969// TODO: Can we coalesce this with frame updates? Need to confirm
970// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200971status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
972 bool shouldBeSeamless) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000973 std::lock_guard _lock{mMutex};
Robert Carr9c006e02020-10-14 13:41:57 -0700974 SurfaceComposerClient::Transaction t;
975
Marin Shalamanov46084422020-10-13 12:33:42 +0200976 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700977}
978
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800979status_t BLASTBufferQueue::setFrameTimelineInfo(uint64_t frameNumber,
980 const FrameTimelineInfo& frameTimelineInfo) {
981 ATRACE_FORMAT("%s(%s) frameNumber: %" PRIu64 " vsyncId: %" PRId64, __func__, mName.c_str(),
982 frameNumber, frameTimelineInfo.vsyncId);
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000983 std::lock_guard _lock{mMutex};
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800984 mPendingFrameTimelines.push({frameNumber, frameTimelineInfo});
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100985 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700986}
987
Hongguang Chen621ec582021-02-16 15:42:35 -0800988void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000989 std::lock_guard _lock{mMutex};
Hongguang Chen621ec582021-02-16 15:42:35 -0800990 SurfaceComposerClient::Transaction t;
991
992 t.setSidebandStream(mSurfaceControl, stream).apply();
993}
994
Vishnu Nair992496b2020-10-22 17:27:21 -0700995sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000996 std::lock_guard _lock{mMutex};
Vishnu Nair992496b2020-10-22 17:27:21 -0700997 sp<IBinder> scHandle = nullptr;
998 if (includeSurfaceControlHandle && mSurfaceControl) {
999 scHandle = mSurfaceControl->getHandle();
1000 }
1001 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -07001002}
1003
Vishnu Nairc4a40c12020-12-23 09:14:32 -08001004void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
1005 uint64_t frameNumber) {
1006 std::lock_guard _lock{mMutex};
1007 if (mLastAcquiredFrameNumber >= frameNumber) {
1008 // Apply the transaction since we have already acquired the desired frame.
1009 t->apply();
1010 } else {
chaviwaad6cf52021-03-23 17:27:20 -05001011 mPendingTransactions.emplace_back(frameNumber, *t);
1012 // Clear the transaction so it can't be applied elsewhere.
1013 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -08001014 }
1015}
1016
chaviw6a195272021-09-03 16:14:25 -05001017void BLASTBufferQueue::applyPendingTransactions(uint64_t frameNumber) {
1018 std::lock_guard _lock{mMutex};
1019
1020 SurfaceComposerClient::Transaction t;
1021 mergePendingTransactions(&t, frameNumber);
Robert Carr79dc06a2022-02-22 15:28:59 -08001022 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
1023 t.setApplyToken(mApplyToken).apply(false, true);
chaviw6a195272021-09-03 16:14:25 -05001024}
1025
1026void BLASTBufferQueue::mergePendingTransactions(SurfaceComposerClient::Transaction* t,
1027 uint64_t frameNumber) {
1028 auto mergeTransaction =
1029 [&t, currentFrameNumber = frameNumber](
1030 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
1031 auto& [targetFrameNumber, transaction] = pendingTransaction;
1032 if (currentFrameNumber < targetFrameNumber) {
1033 return false;
1034 }
1035 t->merge(std::move(transaction));
1036 return true;
1037 };
1038
1039 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
1040 mPendingTransactions.end(), mergeTransaction),
1041 mPendingTransactions.end());
1042}
1043
chaviwd84085a2022-02-08 11:07:04 -06001044SurfaceComposerClient::Transaction* BLASTBufferQueue::gatherPendingTransactions(
1045 uint64_t frameNumber) {
1046 std::lock_guard _lock{mMutex};
1047 SurfaceComposerClient::Transaction* t = new SurfaceComposerClient::Transaction();
1048 mergePendingTransactions(t, frameNumber);
1049 return t;
1050}
1051
Vishnu Nair89496122020-12-14 17:14:53 -08001052// Maintains a single worker thread per process that services a list of runnables.
1053class AsyncWorker : public Singleton<AsyncWorker> {
1054private:
1055 std::thread mThread;
1056 bool mDone = false;
1057 std::deque<std::function<void()>> mRunnables;
1058 std::mutex mMutex;
1059 std::condition_variable mCv;
1060 void run() {
1061 std::unique_lock<std::mutex> lock(mMutex);
1062 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -08001063 while (!mRunnables.empty()) {
Vishnu Nair51e4dc82021-10-01 15:32:33 -07001064 std::deque<std::function<void()>> runnables = std::move(mRunnables);
1065 mRunnables.clear();
1066 lock.unlock();
1067 // Run outside the lock since the runnable might trigger another
1068 // post to the async worker.
1069 execute(runnables);
1070 lock.lock();
Vishnu Nair89496122020-12-14 17:14:53 -08001071 }
Wonsik Kim567533e2021-05-04 19:31:29 -07001072 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -08001073 }
1074 }
1075
Vishnu Nair51e4dc82021-10-01 15:32:33 -07001076 void execute(std::deque<std::function<void()>>& runnables) {
1077 while (!runnables.empty()) {
1078 std::function<void()> runnable = runnables.front();
1079 runnables.pop_front();
1080 runnable();
1081 }
1082 }
1083
Vishnu Nair89496122020-12-14 17:14:53 -08001084public:
1085 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
1086
1087 ~AsyncWorker() {
1088 mDone = true;
1089 mCv.notify_all();
1090 if (mThread.joinable()) {
1091 mThread.join();
1092 }
1093 }
1094
1095 void post(std::function<void()> runnable) {
1096 std::unique_lock<std::mutex> lock(mMutex);
1097 mRunnables.emplace_back(std::move(runnable));
1098 mCv.notify_one();
1099 }
1100};
1101ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
1102
1103// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
1104class AsyncProducerListener : public BnProducerListener {
1105private:
1106 const sp<IProducerListener> mListener;
1107
1108public:
1109 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
1110
1111 void onBufferReleased() override {
1112 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
1113 }
1114
1115 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
1116 AsyncWorker::getInstance().post(
1117 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
1118 }
1119};
1120
1121// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
1122// can be non-blocking when the producer is in the client process.
1123class BBQBufferQueueProducer : public BufferQueueProducer {
1124public:
Brian Lindahlc794b692023-01-31 15:42:47 -07001125 BBQBufferQueueProducer(const sp<BufferQueueCore>& core, wp<BLASTBufferQueue> bbq)
1126 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/),
1127 mBLASTBufferQueue(std::move(bbq)) {}
Vishnu Nair89496122020-12-14 17:14:53 -08001128
1129 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
1130 QueueBufferOutput* output) override {
1131 if (!listener) {
1132 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
1133 }
1134
1135 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
1136 producerControlledByApp, output);
1137 }
Vishnu Nair17dde612020-12-28 11:39:59 -08001138
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001139#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1140 status_t disconnect(int api, DisconnectMode mode) override {
1141 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1142 if (!bbq) {
1143 return BufferQueueProducer::disconnect(api, mode);
1144 }
1145
1146 std::lock_guard lock{bbq->mMutex};
1147 if (status_t status = BufferQueueProducer::disconnect(api, mode); status != OK) {
1148 return status;
1149 }
1150
1151 bbq->mNumDequeued = 0;
1152 bbq->mNumAcquired = 0;
1153 for (auto& [releaseId, bufferItem] : bbq->mSubmitted) {
1154 bufferItem.mIsStale = true;
1155 }
1156
1157 return OK;
1158 }
1159
1160 status_t setAsyncMode(bool asyncMode) override {
1161 if (status_t status = BufferQueueProducer::setAsyncMode(asyncMode); status != OK) {
1162 return status;
1163 }
1164
1165 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1166 if (!bbq) {
1167 return OK;
1168 }
1169
1170 {
1171 std::lock_guard lock{bbq->mMutex};
1172 bbq->mAsyncMode = asyncMode;
1173 }
1174
1175 return OK;
1176 }
1177
1178 status_t setSharedBufferMode(bool sharedBufferMode) override {
1179 if (status_t status = BufferQueueProducer::setSharedBufferMode(sharedBufferMode);
1180 status != OK) {
1181 return status;
1182 }
1183
1184 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1185 if (!bbq) {
1186 return OK;
1187 }
1188
1189 {
1190 std::lock_guard lock{bbq->mMutex};
1191 bbq->mSharedBufferMode = sharedBufferMode;
1192 }
1193
1194 return OK;
1195 }
1196
1197 status_t detachBuffer(int slot) override {
1198 if (status_t status = BufferQueueProducer::detachBuffer(slot); status != OK) {
1199 return status;
1200 }
1201
1202 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1203 if (!bbq) {
1204 return OK;
1205 }
1206
1207 {
1208 std::lock_guard lock{bbq->mMutex};
1209 bbq->mNumDequeued--;
1210 }
1211
1212 return OK;
1213 }
1214
1215 status_t dequeueBuffer(int* outSlot, sp<Fence>* outFence, uint32_t width, uint32_t height,
1216 PixelFormat format, uint64_t usage, uint64_t* outBufferAge,
1217 FrameEventHistoryDelta* outTimestamps) override {
1218 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1219 if (!bbq) {
1220 return BufferQueueProducer::dequeueBuffer(outSlot, outFence, width, height, format,
1221 usage, outBufferAge, outTimestamps);
1222 }
1223
1224 {
1225 std::lock_guard lock{bbq->mMutex};
1226 bbq->mNumDequeued++;
1227 }
1228
1229 status_t status =
1230 BufferQueueProducer::dequeueBuffer(outSlot, outFence, width, height, format, usage,
1231 outBufferAge, outTimestamps);
1232 if (status < 0) {
1233 std::lock_guard lock{bbq->mMutex};
1234 bbq->mNumDequeued--;
1235 }
1236 return status;
1237 }
1238#endif
1239
Brian Lindahlc794b692023-01-31 15:42:47 -07001240 // We want to resize the frame history when changing the size of the buffer queue
1241 status_t setMaxDequeuedBufferCount(int maxDequeuedBufferCount) override {
1242 int maxBufferCount;
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001243 if (status_t status = BufferQueueProducer::setMaxDequeuedBufferCount(maxDequeuedBufferCount,
1244 &maxBufferCount);
1245 status != OK) {
1246 return status;
Brian Lindahlc794b692023-01-31 15:42:47 -07001247 }
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001248
1249 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1250 if (!bbq) {
1251 return OK;
1252 }
1253
1254 // if we can't determine the max buffer count, then just skip growing the history size
1255 size_t newFrameHistorySize = maxBufferCount + 2; // +2 because triple buffer rendering
1256 // optimize away resizing the frame history unless it will grow
1257 if (newFrameHistorySize > FrameEventHistory::INITIAL_MAX_FRAME_HISTORY) {
1258 ALOGV("increasing frame history size to %zu", newFrameHistorySize);
1259 bbq->resizeFrameEventHistory(newFrameHistorySize);
1260 }
1261
1262#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1263 {
1264 std::lock_guard lock{bbq->mMutex};
1265 bbq->mMaxDequeuedBuffers = maxDequeuedBufferCount;
1266 }
1267#endif
1268
1269 return OK;
Brian Lindahlc794b692023-01-31 15:42:47 -07001270 }
1271
Vishnu Nair17dde612020-12-28 11:39:59 -08001272 int query(int what, int* value) override {
1273 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
1274 *value = 1;
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001275 return OK;
Vishnu Nair17dde612020-12-28 11:39:59 -08001276 }
1277 return BufferQueueProducer::query(what, value);
1278 }
Brian Lindahlc794b692023-01-31 15:42:47 -07001279
1280private:
1281 const wp<BLASTBufferQueue> mBLASTBufferQueue;
Vishnu Nair89496122020-12-14 17:14:53 -08001282};
1283
1284// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
1285// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
1286// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
1287// we can deadlock.
1288void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
1289 sp<IGraphicBufferConsumer>* outConsumer) {
1290 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
1291 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
1292
1293 sp<BufferQueueCore> core(new BufferQueueCore());
1294 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
1295
Brian Lindahlc794b692023-01-31 15:42:47 -07001296 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core, this));
Vishnu Nair89496122020-12-14 17:14:53 -08001297 LOG_ALWAYS_FATAL_IF(producer == nullptr,
1298 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
1299
Vishnu Nair8b30dd12021-01-25 14:16:54 -08001300 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
1301 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -08001302 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
1303 "BLASTBufferQueue: failed to create BufferQueueConsumer");
1304
1305 *outProducer = producer;
1306 *outConsumer = consumer;
1307}
1308
Brian Lindahlc794b692023-01-31 15:42:47 -07001309void BLASTBufferQueue::resizeFrameEventHistory(size_t newSize) {
1310 // This can be null during creation of the buffer queue, but resizing won't do anything at that
1311 // point in time, so just ignore. This can go away once the class relationships and lifetimes of
1312 // objects are cleaned up with a major refactor of BufferQueue as a whole.
1313 if (mBufferItemConsumer != nullptr) {
1314 std::unique_lock _lock{mMutex};
1315 mBufferItemConsumer->resizeFrameEventHistory(newSize);
1316 }
1317}
1318
chaviw497e81c2021-02-04 17:09:47 -08001319PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
1320 PixelFormat convertedFormat = format;
1321 switch (format) {
1322 case PIXEL_FORMAT_TRANSPARENT:
1323 case PIXEL_FORMAT_TRANSLUCENT:
1324 convertedFormat = PIXEL_FORMAT_RGBA_8888;
1325 break;
1326 case PIXEL_FORMAT_OPAQUE:
1327 convertedFormat = PIXEL_FORMAT_RGBX_8888;
1328 break;
1329 }
1330 return convertedFormat;
1331}
1332
Robert Carr82d07c92021-05-10 11:36:43 -07001333uint32_t BLASTBufferQueue::getLastTransformHint() const {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001334 std::lock_guard _lock{mMutex};
Robert Carr82d07c92021-05-10 11:36:43 -07001335 if (mSurfaceControl != nullptr) {
1336 return mSurfaceControl->getTransformHint();
1337 } else {
1338 return 0;
1339 }
1340}
1341
chaviw0b020f82021-08-20 12:00:47 -05001342uint64_t BLASTBufferQueue::getLastAcquiredFrameNum() {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001343 std::lock_guard _lock{mMutex};
chaviw0b020f82021-08-20 12:00:47 -05001344 return mLastAcquiredFrameNumber;
1345}
1346
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001347bool BLASTBufferQueue::isSameSurfaceControl(const sp<SurfaceControl>& surfaceControl) const {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001348 std::lock_guard _lock{mMutex};
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001349 return SurfaceControl::isSameSurface(mSurfaceControl, surfaceControl);
1350}
1351
Patrick Williamsf1e5df12022-10-17 21:37:42 +00001352void BLASTBufferQueue::setTransactionHangCallback(
1353 std::function<void(const std::string&)> callback) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001354 std::lock_guard _lock{mMutex};
Robert Carr4c1b6462021-12-21 10:30:50 -08001355 mTransactionHangCallback = callback;
1356}
1357
Robert Carr78c25dd2019-08-15 14:10:33 -07001358} // namespace android