blob: f065ffa61114fadadcc8f6332a5da8174db8290a [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
Jim Shargod30823a2024-07-27 02:49:39 +000023#include <com_android_graphics_libgui_flags.h>
liulijuneb489f62022-10-17 22:02:14 +080024#include <cutils/atomic.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070025#include <gui/BLASTBufferQueue.h>
26#include <gui/BufferItemConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080027#include <gui/BufferQueueConsumer.h>
28#include <gui/BufferQueueCore.h>
29#include <gui/BufferQueueProducer.h>
Ady Abraham107788e2023-10-17 12:31:08 -070030
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070031#include <gui/FrameRateUtils.h>
Valerie Hau45e4b3b2019-12-03 10:49:17 -080032#include <gui/GLConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080033#include <gui/IProducerListener.h>
Robert Carr05086b22020-10-13 18:22:51 -070034#include <gui/Surface.h>
chaviw57ae4b22022-02-03 16:51:39 -060035#include <gui/TraceUtils.h>
Vishnu Nair89496122020-12-14 17:14:53 -080036#include <utils/Singleton.h>
Valerie Haua32c5522019-12-09 10:11:08 -080037#include <utils/Trace.h>
38
Ady Abraham0bde6b52021-05-18 13:57:02 -070039#include <private/gui/ComposerService.h>
Huihong Luo02186fb2022-02-23 14:21:54 -080040#include <private/gui/ComposerServiceAIDL.h>
Ady Abraham0bde6b52021-05-18 13:57:02 -070041
Chavi Weingartene0237bb2023-02-06 21:48:32 +000042#include <android-base/thread_annotations.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070043
Alec Mouri21d94322023-10-17 19:51:39 +000044#include <com_android_graphics_libgui_flags.h>
45
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070046using namespace com::android::graphics::libgui;
Robert Carr78c25dd2019-08-15 14:10:33 -070047using namespace std::chrono_literals;
48
Vishnu Nairdab94092020-09-29 16:09:04 -070049namespace {
chaviw3277faf2021-05-19 16:45:23 -050050inline const char* boolToString(bool b) {
Vishnu Nairdab94092020-09-29 16:09:04 -070051 return b ? "true" : "false";
52}
53} // namespace
54
Robert Carr78c25dd2019-08-15 14:10:33 -070055namespace android {
56
Vishnu Nairdab94092020-09-29 16:09:04 -070057// Macros to include adapter info in log messages
chaviwd7deef72021-10-06 11:53:40 -050058#define BQA_LOGD(x, ...) \
59 ALOGD("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070060#define BQA_LOGV(x, ...) \
61 ALOGV("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairc6f89ee2020-12-11 14:27:32 -080062// enable logs for a single layer
63//#define BQA_LOGV(x, ...) \
64// ALOGV_IF((strstr(mName.c_str(), "SurfaceView") != nullptr), "[%s](f:%u,a:%u) " x, \
65// mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070066#define BQA_LOGE(x, ...) \
67 ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
68
chaviw57ae4b22022-02-03 16:51:39 -060069#define BBQ_TRACE(x, ...) \
70 ATRACE_FORMAT("%s - %s(f:%u,a:%u)" x, __FUNCTION__, mName.c_str(), mNumFrameAvailable, \
71 mNumAcquired, ##__VA_ARGS__)
72
Chavi Weingartene0237bb2023-02-06 21:48:32 +000073#define UNIQUE_LOCK_WITH_ASSERTION(mutex) \
74 std::unique_lock _lock{mutex}; \
75 base::ScopedLockAssertion assumeLocked(mutex);
76
Valerie Hau871d6352020-01-29 08:44:02 -080077void BLASTBufferItemConsumer::onDisconnect() {
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000078 Mutex::Autolock lock(mMutex);
79 mPreviouslyConnected = mCurrentlyConnected;
80 mCurrentlyConnected = false;
81 if (mPreviouslyConnected) {
82 mDisconnectEvents.push(mCurrentFrameNumber);
Valerie Hau871d6352020-01-29 08:44:02 -080083 }
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000084 mFrameEventHistory.onDisconnect();
Valerie Hau871d6352020-01-29 08:44:02 -080085}
86
87void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
88 FrameEventHistoryDelta* outDelta) {
Hongguang Chen621ec582021-02-16 15:42:35 -080089 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080090 if (newTimestamps) {
91 // BufferQueueProducer only adds a new timestamp on
92 // queueBuffer
93 mCurrentFrameNumber = newTimestamps->frameNumber;
94 mFrameEventHistory.addQueue(*newTimestamps);
95 }
96 if (outDelta) {
97 // frame event histories will be processed
98 // only after the producer connects and requests
99 // deltas for the first time. Forward this intent
100 // to SF-side to turn event processing back on
101 mPreviouslyConnected = mCurrentlyConnected;
102 mCurrentlyConnected = true;
103 mFrameEventHistory.getAndResetDelta(outDelta);
104 }
105}
106
Alec Mouri21d94322023-10-17 19:51:39 +0000107void BLASTBufferItemConsumer::updateFrameTimestamps(
108 uint64_t frameNumber, uint64_t previousFrameNumber, nsecs_t refreshStartTime,
109 const sp<Fence>& glDoneFence, const sp<Fence>& presentFence,
110 const sp<Fence>& prevReleaseFence, CompositorTiming compositorTiming, nsecs_t latchTime,
111 nsecs_t dequeueReadyTime) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800112 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800113
114 // if the producer is not connected, don't bother updating,
115 // the next producer that connects won't access this frame event
116 if (!mCurrentlyConnected) return;
117 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
118 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
119 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
120
121 mFrameEventHistory.addLatch(frameNumber, latchTime);
Alec Mouri21d94322023-10-17 19:51:39 +0000122 if (flags::frametimestamps_previousrelease()) {
123 if (previousFrameNumber > 0) {
124 mFrameEventHistory.addRelease(previousFrameNumber, dequeueReadyTime,
125 std::move(releaseFenceTime));
126 }
127 } else {
128 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
129 }
130
Valerie Hau871d6352020-01-29 08:44:02 -0800131 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
132 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
133 compositorTiming);
134}
135
136void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
137 bool disconnect = false;
Hongguang Chen621ec582021-02-16 15:42:35 -0800138 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800139 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
140 disconnect = true;
141 mDisconnectEvents.pop();
142 }
143 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
144}
145
Hongguang Chen621ec582021-02-16 15:42:35 -0800146void BLASTBufferItemConsumer::onSidebandStreamChanged() {
Ady Abrahamdbca1352021-12-15 11:58:56 -0800147 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
148 if (bbq != nullptr) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800149 sp<NativeHandle> stream = getSidebandStream();
Ady Abrahamdbca1352021-12-15 11:58:56 -0800150 bbq->setSidebandStream(stream);
Hongguang Chen621ec582021-02-16 15:42:35 -0800151 }
152}
153
Ady Abraham107788e2023-10-17 12:31:08 -0700154#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -0700155void BLASTBufferItemConsumer::onSetFrameRate(float frameRate, int8_t compatibility,
156 int8_t changeFrameRateStrategy) {
157 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
158 if (bbq != nullptr) {
159 bbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
160 }
161}
162#endif
163
Brian Lindahlc794b692023-01-31 15:42:47 -0700164void BLASTBufferItemConsumer::resizeFrameEventHistory(size_t newSize) {
165 Mutex::Autolock lock(mMutex);
166 mFrameEventHistory.resize(newSize);
167}
168
Vishnu Naird2aaab12022-02-10 14:49:09 -0800169BLASTBufferQueue::BLASTBufferQueue(const std::string& name, bool updateDestinationFrame)
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800170 : mSurfaceControl(nullptr),
171 mSize(1, 1),
Vishnu Nairea0de002020-11-17 17:42:37 -0800172 mRequestedSize(mSize),
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800173 mFormat(PIXEL_FORMAT_RGBA_8888),
Tianhao Yao4861b102022-02-03 20:18:35 +0000174 mTransactionReadyCallback(nullptr),
Vishnu Naird2aaab12022-02-10 14:49:09 -0800175 mSyncTransaction(nullptr),
176 mUpdateDestinationFrame(updateDestinationFrame) {
Vishnu Nair89496122020-12-14 17:14:53 -0800177 createBufferQueue(&mProducer, &mConsumer);
Jim Shargod30823a2024-07-27 02:49:39 +0000178#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
179 mBufferItemConsumer = new BLASTBufferItemConsumer(mProducer, mConsumer,
180 GraphicBuffer::USAGE_HW_COMPOSER |
181 GraphicBuffer::USAGE_HW_TEXTURE,
182 1, false, this);
183#else
Vishnu Nair1618c672021-02-05 13:08:26 -0800184 mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
185 GraphicBuffer::USAGE_HW_COMPOSER |
186 GraphicBuffer::USAGE_HW_TEXTURE,
Ady Abrahamdbca1352021-12-15 11:58:56 -0800187 1, false, this);
Jim Shargod30823a2024-07-27 02:49:39 +0000188#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
189 // since the adapter is in the client process, set dequeue timeout
190 // explicitly so that dequeueBuffer will block
191 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
192
liulijuneb489f62022-10-17 22:02:14 +0800193 static std::atomic<uint32_t> nextId = 0;
194 mProducerId = nextId++;
195 mName = name + "#" + std::to_string(mProducerId);
196 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(mProducerId);
197 mQueuedBufferTrace = "QueuedBuffer - " + mName + "BLAST#" + std::to_string(mProducerId);
Vishnu Nairdab94092020-09-29 16:09:04 -0700198 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700199 mBufferItemConsumer->setFrameAvailableListener(this);
Robert Carr9f133d72020-04-01 15:51:46 -0700200
Huihong Luo02186fb2022-02-23 14:21:54 -0800201 ComposerServiceAIDL::getComposerService()->getMaxAcquiredBufferCount(&mMaxAcquiredBuffers);
Ady Abraham0bde6b52021-05-18 13:57:02 -0700202 mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxAcquiredBuffers);
chaviw69058fb2021-09-27 09:37:30 -0500203 mCurrentMaxAcquiredBufferCount = mMaxAcquiredBuffers;
Valerie Haua32c5522019-12-09 10:11:08 -0800204 mNumAcquired = 0;
205 mNumFrameAvailable = 0;
Robert Carr4c1b6462021-12-21 10:30:50 -0800206
207 TransactionCompletedListener::getInstance()->addQueueStallListener(
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000208 [&](const std::string& reason) {
209 std::function<void(const std::string&)> callbackCopy;
210 {
211 std::unique_lock _lock{mMutex};
212 callbackCopy = mTransactionHangCallback;
213 }
214 if (callbackCopy) callbackCopy(reason);
215 },
216 this);
Robert Carr4c1b6462021-12-21 10:30:50 -0800217
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800218 BQA_LOGV("BLASTBufferQueue created");
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800219}
220
221BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface,
222 int width, int height, int32_t format)
223 : BLASTBufferQueue(name) {
224 update(surface, width, height, format);
Robert Carr78c25dd2019-08-15 14:10:33 -0700225}
226
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800227BLASTBufferQueue::~BLASTBufferQueue() {
Robert Carr4c1b6462021-12-21 10:30:50 -0800228 TransactionCompletedListener::getInstance()->removeQueueStallListener(this);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800229 if (mPendingTransactions.empty()) {
230 return;
231 }
232 BQA_LOGE("Applying pending transactions on dtor %d",
233 static_cast<uint32_t>(mPendingTransactions.size()));
234 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800235 mergePendingTransactions(&t, std::numeric_limits<uint64_t>::max() /* frameNumber */);
Robert Carr79dc06a2022-02-22 15:28:59 -0800236 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
237 t.setApplyToken(mApplyToken).apply(false, true);
chaviw3b4bdcf2022-03-17 09:27:03 -0500238
239 if (mTransactionReadyCallback) {
240 mTransactionReadyCallback(mSyncTransaction);
241 }
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800242}
243
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500244void BLASTBufferQueue::onFirstRef() {
245 // safe default, most producers are expected to override this
246 mProducer->setMaxDequeuedBufferCount(2);
247}
248
chaviw565ee542021-01-14 10:21:23 -0800249void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height,
Vishnu Naird2aaab12022-02-10 14:49:09 -0800250 int32_t format) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800251 LOG_ALWAYS_FATAL_IF(surface == nullptr, "BLASTBufferQueue: mSurfaceControl must not be NULL");
252
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000253 std::lock_guard _lock{mMutex};
chaviw565ee542021-01-14 10:21:23 -0800254 if (mFormat != format) {
255 mFormat = format;
chaviw497e81c2021-02-04 17:09:47 -0800256 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
chaviw565ee542021-01-14 10:21:23 -0800257 }
258
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800259 const bool surfaceControlChanged = !SurfaceControl::isSameSurface(mSurfaceControl, surface);
Vishnu Nairab066512022-01-04 22:28:00 +0000260 if (surfaceControlChanged && mSurfaceControl != nullptr) {
261 BQA_LOGD("Updating SurfaceControl without recreating BBQ");
262 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800263 bool applyTransaction = false;
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800264
Vishnu Nair5fa91c22021-06-29 14:30:48 -0700265 // Always update the native object even though they might have the same layer handle, so we can
266 // get the updated transform hint from WM.
267 mSurfaceControl = surface;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800268 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800269 if (surfaceControlChanged) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800270 t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure,
271 layer_state_t::eEnableBackpressure);
272 applyTransaction = true;
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800273 }
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800274 mTransformHint = mSurfaceControl->getTransformHint();
275 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Naira4fbca52021-07-07 16:52:34 -0700276 BQA_LOGV("update width=%d height=%d format=%d mTransformHint=%d", width, height, format,
277 mTransformHint);
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800278
Vishnu Nairea0de002020-11-17 17:42:37 -0800279 ui::Size newSize(width, height);
280 if (mRequestedSize != newSize) {
281 mRequestedSize.set(newSize);
282 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000283 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800284 // If the buffer supports scaling, update the frame immediately since the client may
285 // want to scale the existing buffer to the new size.
286 mSize = mRequestedSize;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800287 if (mUpdateDestinationFrame) {
288 t.setDestinationFrame(mSurfaceControl, Rect(newSize));
289 applyTransaction = true;
290 }
Vishnu Nair53c936c2020-12-03 11:46:37 -0800291 }
Robert Carrfc416512020-04-02 12:32:44 -0700292 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800293 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800294 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
295 t.setApplyToken(mApplyToken).apply(false, true);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800296 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700297}
298
chaviwd7deef72021-10-06 11:53:40 -0500299static std::optional<SurfaceControlStats> findMatchingStat(
300 const std::vector<SurfaceControlStats>& stats, const sp<SurfaceControl>& sc) {
301 for (auto stat : stats) {
302 if (SurfaceControl::isSameSurface(sc, stat.surfaceControl)) {
303 return stat;
304 }
305 }
306 return std::nullopt;
307}
308
309static void transactionCommittedCallbackThunk(void* context, nsecs_t latchTime,
310 const sp<Fence>& presentFence,
311 const std::vector<SurfaceControlStats>& stats) {
312 if (context == nullptr) {
313 return;
314 }
315 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
316 bq->transactionCommittedCallback(latchTime, presentFence, stats);
317}
318
319void BLASTBufferQueue::transactionCommittedCallback(nsecs_t /*latchTime*/,
320 const sp<Fence>& /*presentFence*/,
321 const std::vector<SurfaceControlStats>& stats) {
322 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000323 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600324 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500325 BQA_LOGV("transactionCommittedCallback");
326 if (!mSurfaceControlsWithPendingCallback.empty()) {
327 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
328 std::optional<SurfaceControlStats> stat = findMatchingStat(stats, pendingSC);
329 if (stat) {
330 uint64_t currFrameNumber = stat->frameEventStats.frameNumber;
331
332 // We need to check if we were waiting for a transaction callback in order to
333 // process any pending buffers and unblock. It's possible to get transaction
chaviwc1cf4022022-06-03 13:32:33 -0500334 // callbacks for previous requests so we need to ensure that there are no pending
335 // frame numbers that were in a sync. We remove the frame from mSyncedFrameNumbers
336 // set and then check if it's empty. If there are no more pending syncs, we can
337 // proceed with flushing the shadow queue.
chaviwc1cf4022022-06-03 13:32:33 -0500338 mSyncedFrameNumbers.erase(currFrameNumber);
Chavi Weingartend48797b2023-08-04 13:11:39 +0000339 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500340 flushShadowQueue();
341 }
342 } else {
chaviw768bfa02021-11-01 09:50:57 -0500343 BQA_LOGE("Failed to find matching SurfaceControl in transactionCommittedCallback");
chaviwd7deef72021-10-06 11:53:40 -0500344 }
345 } else {
346 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
347 "empty.");
348 }
chaviwd7deef72021-10-06 11:53:40 -0500349 decStrong((void*)transactionCommittedCallbackThunk);
350 }
351}
352
Robert Carr78c25dd2019-08-15 14:10:33 -0700353static void transactionCallbackThunk(void* context, nsecs_t latchTime,
354 const sp<Fence>& presentFence,
355 const std::vector<SurfaceControlStats>& stats) {
356 if (context == nullptr) {
357 return;
358 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800359 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700360 bq->transactionCallback(latchTime, presentFence, stats);
361}
362
363void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
364 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700365 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000366 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600367 BBQ_TRACE();
chaviw71c2cc42020-10-23 16:42:02 -0700368 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700369
chaviw42026162021-04-16 15:46:12 -0500370 if (!mSurfaceControlsWithPendingCallback.empty()) {
371 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
372 mSurfaceControlsWithPendingCallback.pop();
chaviwd7deef72021-10-06 11:53:40 -0500373 std::optional<SurfaceControlStats> statsOptional = findMatchingStat(stats, pendingSC);
374 if (statsOptional) {
375 SurfaceControlStats stat = *statsOptional;
Vishnu Nair71fcf912022-10-18 09:14:20 -0700376 if (stat.transformHint) {
377 mTransformHint = *stat.transformHint;
378 mBufferItemConsumer->setTransformHint(mTransformHint);
379 BQA_LOGV("updated mTransformHint=%d", mTransformHint);
380 }
Vishnu Nairde66dc72021-06-17 17:54:41 -0700381 // Update frametime stamps if the frame was latched and presented, indicated by a
382 // valid latch time.
383 if (stat.latchTime > 0) {
384 mBufferItemConsumer
385 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
Alec Mouri21d94322023-10-17 19:51:39 +0000386 stat.frameEventStats.previousFrameNumber,
Vishnu Nairde66dc72021-06-17 17:54:41 -0700387 stat.frameEventStats.refreshStartTime,
388 stat.frameEventStats.gpuCompositionDoneFence,
389 stat.presentFence, stat.previousReleaseFence,
390 stat.frameEventStats.compositorTiming,
391 stat.latchTime,
392 stat.frameEventStats.dequeueReadyTime);
393 }
Robert Carr405e2f62021-12-31 16:59:34 -0800394 auto currFrameNumber = stat.frameEventStats.frameNumber;
395 std::vector<ReleaseCallbackId> staleReleases;
396 for (const auto& [key, value]: mSubmitted) {
397 if (currFrameNumber > key.framenumber) {
398 staleReleases.push_back(key);
399 }
400 }
401 for (const auto& staleRelease : staleReleases) {
Robert Carr405e2f62021-12-31 16:59:34 -0800402 releaseBufferCallbackLocked(staleRelease,
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700403 stat.previousReleaseFence
404 ? stat.previousReleaseFence
405 : Fence::NO_FENCE,
406 stat.currentMaxAcquiredBufferCount,
407 true /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800408 }
chaviwd7deef72021-10-06 11:53:40 -0500409 } else {
chaviw768bfa02021-11-01 09:50:57 -0500410 BQA_LOGE("Failed to find matching SurfaceControl in transactionCallback");
chaviw42026162021-04-16 15:46:12 -0500411 }
412 } else {
413 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
414 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800415 }
chaviw71c2cc42020-10-23 16:42:02 -0700416
chaviw71c2cc42020-10-23 16:42:02 -0700417 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700418 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700419}
420
Vishnu Nair1506b182021-02-22 14:35:15 -0800421// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
422// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
423// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
424// Otherwise, this is a no-op.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700425static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, const ReleaseCallbackId& id,
chaviw69058fb2021-09-27 09:37:30 -0500426 const sp<Fence>& releaseFence,
427 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800428 sp<BLASTBufferQueue> blastBufferQueue = context.promote();
Vishnu Nair1506b182021-02-22 14:35:15 -0800429 if (blastBufferQueue) {
chaviw69058fb2021-09-27 09:37:30 -0500430 blastBufferQueue->releaseBufferCallback(id, releaseFence, currentMaxAcquiredBufferCount);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700431 } else {
432 ALOGV("releaseBufferCallbackThunk %s blastBufferQueue is dead", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800433 }
434}
435
chaviwd7deef72021-10-06 11:53:40 -0500436void BLASTBufferQueue::flushShadowQueue() {
437 BQA_LOGV("flushShadowQueue");
438 int numFramesToFlush = mNumFrameAvailable;
439 while (numFramesToFlush > 0) {
440 acquireNextBufferLocked(std::nullopt);
441 numFramesToFlush--;
442 }
443}
444
chaviw69058fb2021-09-27 09:37:30 -0500445void BLASTBufferQueue::releaseBufferCallback(
446 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
447 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000448 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600449 BBQ_TRACE();
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700450 releaseBufferCallbackLocked(id, releaseFence, currentMaxAcquiredBufferCount,
451 false /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800452}
453
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700454void BLASTBufferQueue::releaseBufferCallbackLocked(
455 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
456 std::optional<uint32_t> currentMaxAcquiredBufferCount, bool fakeRelease) {
Robert Carr405e2f62021-12-31 16:59:34 -0800457 ATRACE_CALL();
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700458 BQA_LOGV("releaseBufferCallback %s", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800459
Ady Abraham899dcdb2021-06-15 16:56:21 -0700460 // Calculate how many buffers we need to hold before we release them back
461 // to the buffer queue. This will prevent higher latency when we are running
462 // on a lower refresh rate than the max supported. We only do that for EGL
463 // clients as others don't care about latency
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000464 const auto it = mSubmitted.find(id);
465 const bool isEGL = it != mSubmitted.end() && it->second.mApi == NATIVE_WINDOW_API_EGL;
Ady Abraham899dcdb2021-06-15 16:56:21 -0700466
chaviw69058fb2021-09-27 09:37:30 -0500467 if (currentMaxAcquiredBufferCount) {
468 mCurrentMaxAcquiredBufferCount = *currentMaxAcquiredBufferCount;
469 }
470
liulijunf90df632022-11-14 14:24:48 +0800471 const uint32_t numPendingBuffersToHold =
472 isEGL ? std::max(0, mMaxAcquiredBuffers - (int32_t)mCurrentMaxAcquiredBufferCount) : 0;
Robert Carr405e2f62021-12-31 16:59:34 -0800473
474 auto rb = ReleasedBuffer{id, releaseFence};
475 if (std::find(mPendingRelease.begin(), mPendingRelease.end(), rb) == mPendingRelease.end()) {
476 mPendingRelease.emplace_back(rb);
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700477 if (fakeRelease) {
478 BQA_LOGE("Faking releaseBufferCallback from transactionCompleteCallback %" PRIu64,
479 id.framenumber);
480 BBQ_TRACE("FakeReleaseCallback");
481 }
Robert Carr405e2f62021-12-31 16:59:34 -0800482 }
Ady Abraham899dcdb2021-06-15 16:56:21 -0700483
484 // Release all buffers that are beyond the ones that we need to hold
485 while (mPendingRelease.size() > numPendingBuffersToHold) {
chaviw0acd33a2021-11-02 11:55:37 -0500486 const auto releasedBuffer = mPendingRelease.front();
Ady Abraham899dcdb2021-06-15 16:56:21 -0700487 mPendingRelease.pop_front();
chaviw0acd33a2021-11-02 11:55:37 -0500488 releaseBuffer(releasedBuffer.callbackId, releasedBuffer.releaseFence);
chaviwc1cf4022022-06-03 13:32:33 -0500489 // Don't process the transactions here if mSyncedFrameNumbers is not empty. That means
490 // are still transactions that have sync buffers in them that have not been applied or
491 // dropped. Instead, let onFrameAvailable handle processing them since it will merge with
492 // the syncTransaction.
493 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500494 acquireNextBufferLocked(std::nullopt);
495 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800496 }
497
Ady Abraham899dcdb2021-06-15 16:56:21 -0700498 ATRACE_INT("PendingRelease", mPendingRelease.size());
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700499 ATRACE_INT(mQueuedBufferTrace.c_str(),
500 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
Vishnu Nair1506b182021-02-22 14:35:15 -0800501 mCallbackCV.notify_all();
502}
503
chaviw0acd33a2021-11-02 11:55:37 -0500504void BLASTBufferQueue::releaseBuffer(const ReleaseCallbackId& callbackId,
505 const sp<Fence>& releaseFence) {
506 auto it = mSubmitted.find(callbackId);
507 if (it == mSubmitted.end()) {
Patrick Williams4b9507d2024-07-25 09:55:52 -0500508 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %s",
509 callbackId.to_string().c_str());
chaviw0acd33a2021-11-02 11:55:37 -0500510 return;
511 }
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500512#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
Patrick Williamsca81c052024-08-15 12:38:34 -0500513 if (!it->second.disconnectedAfterAcquired) {
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500514 mNumAcquired--;
515 }
516#else
chaviw0acd33a2021-11-02 11:55:37 -0500517 mNumAcquired--;
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500518#endif
chaviw57ae4b22022-02-03 16:51:39 -0600519 BBQ_TRACE("frame=%" PRIu64, callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500520 BQA_LOGV("released %s", callbackId.to_string().c_str());
521 mBufferItemConsumer->releaseBuffer(it->second, releaseFence);
522 mSubmitted.erase(it);
chaviwc1cf4022022-06-03 13:32:33 -0500523 // Remove the frame number from mSyncedFrameNumbers since we can get a release callback
524 // without getting a transaction committed if the buffer was dropped.
525 mSyncedFrameNumbers.erase(callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500526}
527
Chavi Weingarten70670e62023-02-22 17:36:40 +0000528static ui::Size getBufferSize(const BufferItem& item) {
529 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
530 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
531
532 // Take the buffer's orientation into account
533 if (item.mTransform & ui::Transform::ROT_90) {
534 std::swap(bufWidth, bufHeight);
535 }
536 return ui::Size(bufWidth, bufHeight);
537}
538
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000539status_t BLASTBufferQueue::acquireNextBufferLocked(
chaviwd7deef72021-10-06 11:53:40 -0500540 const std::optional<SurfaceComposerClient::Transaction*> transaction) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800541 // Check if we have frames available and we have not acquired the maximum number of buffers.
542 // Even with this check, the consumer can fail to acquire an additional buffer if the consumer
543 // has already acquired (mMaxAcquiredBuffers + 1) and the new buffer is not droppable. In this
544 // case mBufferItemConsumer->acquireBuffer will return with NO_BUFFER_AVAILABLE.
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000545 if (mNumFrameAvailable == 0) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800546 BQA_LOGV("Can't acquire next buffer. No available frames");
547 return BufferQueue::NO_BUFFER_AVAILABLE;
548 }
549
550 if (mNumAcquired >= (mMaxAcquiredBuffers + 2)) {
551 BQA_LOGV("Can't acquire next buffer. Already acquired max frames %d max:%d + 2",
552 mNumAcquired, mMaxAcquiredBuffers);
553 return BufferQueue::NO_BUFFER_AVAILABLE;
Valerie Haud3b90d22019-11-06 09:37:31 -0800554 }
555
Valerie Haua32c5522019-12-09 10:11:08 -0800556 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700557 BQA_LOGE("ERROR : surface control is null");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000558 return NAME_NOT_FOUND;
Valerie Haud3b90d22019-11-06 09:37:31 -0800559 }
560
Robert Carr78c25dd2019-08-15 14:10:33 -0700561 SurfaceComposerClient::Transaction localTransaction;
562 bool applyTransaction = true;
563 SurfaceComposerClient::Transaction* t = &localTransaction;
chaviwd7deef72021-10-06 11:53:40 -0500564 if (transaction) {
565 t = *transaction;
Robert Carr78c25dd2019-08-15 14:10:33 -0700566 applyTransaction = false;
567 }
568
Patrick Williamsca81c052024-08-15 12:38:34 -0500569 BLASTBufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800570
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800571 status_t status =
572 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800573 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
574 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000575 return status;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800576 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700577 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000578 return status;
Robert Carr78c25dd2019-08-15 14:10:33 -0700579 }
chaviw57ae4b22022-02-03 16:51:39 -0600580
Valerie Haua32c5522019-12-09 10:11:08 -0800581 auto buffer = bufferItem.mGraphicBuffer;
582 mNumFrameAvailable--;
chaviw57ae4b22022-02-03 16:51:39 -0600583 BBQ_TRACE("frame=%" PRIu64, bufferItem.mFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800584
585 if (buffer == nullptr) {
586 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700587 BQA_LOGE("Buffer was empty");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000588 return BAD_VALUE;
Valerie Haua32c5522019-12-09 10:11:08 -0800589 }
590
Vishnu Nair670b3f72020-09-29 17:52:18 -0700591 if (rejectBuffer(bufferItem)) {
Vishnu Naira4fbca52021-07-07 16:52:34 -0700592 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d "
Vishnu Nairea0de002020-11-17 17:42:37 -0800593 "buffer{size=%dx%d transform=%d}",
594 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
595 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
596 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000597 return acquireNextBufferLocked(transaction);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700598 }
599
Valerie Haua32c5522019-12-09 10:11:08 -0800600 mNumAcquired++;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700601 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
602 ReleaseCallbackId releaseCallbackId(buffer->getId(), mLastAcquiredFrameNumber);
603 mSubmitted[releaseCallbackId] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700604
Valerie Hau871d6352020-01-29 08:44:02 -0800605 bool needsDisconnect = false;
606 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
607
608 // if producer disconnected before, notify SurfaceFlinger
609 if (needsDisconnect) {
610 t->notifyProducerDisconnect(mSurfaceControl);
611 }
612
Robert Carr78c25dd2019-08-15 14:10:33 -0700613 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
614 incStrong((void*)transactionCallbackThunk);
615
Chavi Weingarten70670e62023-02-22 17:36:40 +0000616 // Only update mSize for destination bounds if the incoming buffer matches the requested size.
617 // Otherwise, it could cause stretching since the destination bounds will update before the
618 // buffer with the new size is acquired.
Vishnu Nair5b5f6932023-04-12 16:28:19 -0700619 if (mRequestedSize == getBufferSize(bufferItem) ||
620 bufferItem.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Chavi Weingarten70670e62023-02-22 17:36:40 +0000621 mSize = mRequestedSize;
622 }
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700623 Rect crop = computeCrop(bufferItem);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000624 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
625 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700626 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800627
Vishnu Nair1506b182021-02-22 14:35:15 -0800628 auto releaseBufferCallback =
629 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
chaviw69058fb2021-09-27 09:37:30 -0500630 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
chaviwba4320c2021-09-15 15:20:53 -0500631 sp<Fence> fence = bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE;
Nergi Rahardi39f510f2024-05-23 15:16:54 +0900632
633 nsecs_t dequeueTime = -1;
634 {
635 std::lock_guard _lock{mTimestampMutex};
636 auto dequeueTimeIt = mDequeueTimestamps.find(buffer->getId());
637 if (dequeueTimeIt != mDequeueTimestamps.end()) {
638 dequeueTime = dequeueTimeIt->second;
639 mDequeueTimestamps.erase(dequeueTimeIt);
640 }
641 }
642
liulijuneb489f62022-10-17 22:02:14 +0800643 t->setBuffer(mSurfaceControl, buffer, fence, bufferItem.mFrameNumber, mProducerId,
Nergi Rahardi39f510f2024-05-23 15:16:54 +0900644 releaseBufferCallback, dequeueTime);
John Reck137069e2020-12-10 22:07:37 -0500645 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
646 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
647 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700648 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviwf2dace72021-11-17 17:36:50 -0600649
chaviw42026162021-04-16 15:46:12 -0500650 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700651
Vishnu Naird2aaab12022-02-10 14:49:09 -0800652 if (mUpdateDestinationFrame) {
653 t->setDestinationFrame(mSurfaceControl, Rect(mSize));
654 } else {
655 const bool ignoreDestinationFrame =
656 bufferItem.mScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE;
657 t->setFlags(mSurfaceControl,
658 ignoreDestinationFrame ? layer_state_t::eIgnoreDestinationFrame : 0,
659 layer_state_t::eIgnoreDestinationFrame);
Vishnu Nair084514a2021-07-30 16:07:42 -0700660 }
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700661 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800662 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800663 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Vishnu Naird2aaab12022-02-10 14:49:09 -0800664 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800665 if (!bufferItem.mIsAutoTimestamp) {
666 t->setDesiredPresentTime(bufferItem.mTimestamp);
667 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700668
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800669 // Drop stale frame timeline infos
670 while (!mPendingFrameTimelines.empty() &&
671 mPendingFrameTimelines.front().first < bufferItem.mFrameNumber) {
672 ATRACE_FORMAT_INSTANT("dropping stale frameNumber: %" PRIu64 " vsyncId: %" PRId64,
673 mPendingFrameTimelines.front().first,
674 mPendingFrameTimelines.front().second.vsyncId);
675 mPendingFrameTimelines.pop();
676 }
677
678 if (!mPendingFrameTimelines.empty() &&
679 mPendingFrameTimelines.front().first == bufferItem.mFrameNumber) {
680 ATRACE_FORMAT_INSTANT("Transaction::setFrameTimelineInfo frameNumber: %" PRIu64
681 " vsyncId: %" PRId64,
682 bufferItem.mFrameNumber,
683 mPendingFrameTimelines.front().second.vsyncId);
684 t->setFrameTimelineInfo(mPendingFrameTimelines.front().second);
685 mPendingFrameTimelines.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100686 }
687
chaviw6a195272021-09-03 16:14:25 -0500688 mergePendingTransactions(t, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700689 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800690 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
691 t->setApplyToken(mApplyToken).apply(false, true);
692 mAppliedLastTransaction = true;
693 mLastAppliedFrameNumber = bufferItem.mFrameNumber;
694 } else {
695 t->setBufferHasBarrier(mSurfaceControl, mLastAppliedFrameNumber);
696 mAppliedLastTransaction = false;
Robert Carr78c25dd2019-08-15 14:10:33 -0700697 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700698
chaviwd7deef72021-10-06 11:53:40 -0500699 BQA_LOGV("acquireNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800700 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
Vishnu Naira4fbca52021-07-07 16:52:34 -0700701 " graphicBufferId=%" PRIu64 "%s transform=%d",
chaviw3277faf2021-05-19 16:45:23 -0500702 mSize.width, mSize.height, bufferItem.mFrameNumber, boolToString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800703 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700704 static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(),
Vishnu Naira4fbca52021-07-07 16:52:34 -0700705 bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000706 return OK;
Robert Carr78c25dd2019-08-15 14:10:33 -0700707}
708
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800709Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
710 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800711 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800712 }
713 return item.mCrop;
714}
715
chaviwd7deef72021-10-06 11:53:40 -0500716void BLASTBufferQueue::acquireAndReleaseBuffer() {
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000717 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500718 BufferItem bufferItem;
chaviw6ebdf5f2021-10-14 11:57:22 -0500719 status_t status =
720 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
721 if (status != OK) {
722 BQA_LOGE("Failed to acquire a buffer in acquireAndReleaseBuffer, err=%s",
723 statusToString(status).c_str());
724 return;
725 }
chaviwd7deef72021-10-06 11:53:40 -0500726 mNumFrameAvailable--;
chaviw6ebdf5f2021-10-14 11:57:22 -0500727 mBufferItemConsumer->releaseBuffer(bufferItem, bufferItem.mFence);
chaviwd7deef72021-10-06 11:53:40 -0500728}
729
Vishnu Nairaef1de92020-10-22 12:15:53 -0700730void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000731 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
732 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
chaviwc1cf4022022-06-03 13:32:33 -0500733
Tianhao Yao4861b102022-02-03 20:18:35 +0000734 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000735 UNIQUE_LOCK_WITH_ASSERTION(mMutex);
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000736 BBQ_TRACE();
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000737 bool waitForTransactionCallback = !mSyncedFrameNumbers.empty();
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800738
Tianhao Yao4861b102022-02-03 20:18:35 +0000739 const bool syncTransactionSet = mTransactionReadyCallback != nullptr;
740 BQA_LOGV("onFrameAvailable-start syncTransactionSet=%s", boolToString(syncTransactionSet));
Valerie Haud3b90d22019-11-06 09:37:31 -0800741
Tianhao Yao4861b102022-02-03 20:18:35 +0000742 if (syncTransactionSet) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000743 // If we are going to re-use the same mSyncTransaction, release the buffer that may
744 // already be set in the Transaction. This is to allow us a free slot early to continue
745 // processing a new buffer.
746 if (!mAcquireSingleBuffer) {
747 auto bufferData = mSyncTransaction->getAndClearBuffer(mSurfaceControl);
748 if (bufferData) {
749 BQA_LOGD("Releasing previous buffer when syncing: framenumber=%" PRIu64,
750 bufferData->frameNumber);
751 releaseBuffer(bufferData->generateReleaseCallbackId(),
752 bufferData->acquireFence);
Tianhao Yao4861b102022-02-03 20:18:35 +0000753 }
754 }
chaviw0acd33a2021-11-02 11:55:37 -0500755
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000756 if (waitForTransactionCallback) {
757 // We are waiting on a previous sync's transaction callback so allow another sync
758 // transaction to proceed.
759 //
760 // We need to first flush out the transactions that were in between the two syncs.
761 // We do this by merging them into mSyncTransaction so any buffer merging will get
762 // a release callback invoked.
763 while (mNumFrameAvailable > 0) {
764 // flush out the shadow queue
765 acquireAndReleaseBuffer();
766 }
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800767 } else {
768 // Make sure the frame available count is 0 before proceeding with a sync to ensure
769 // the correct frame is used for the sync. The only way mNumFrameAvailable would be
770 // greater than 0 is if we already ran out of buffers previously. This means we
771 // need to flush the buffers before proceeding with the sync.
772 while (mNumFrameAvailable > 0) {
773 BQA_LOGD("waiting until no queued buffers");
774 mCallbackCV.wait(_lock);
775 }
chaviwd7deef72021-10-06 11:53:40 -0500776 }
777 }
778
Tianhao Yao4861b102022-02-03 20:18:35 +0000779 // add to shadow queue
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500780#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
781 mNumDequeued--;
782#endif
Tianhao Yao4861b102022-02-03 20:18:35 +0000783 mNumFrameAvailable++;
chaviwc1cf4022022-06-03 13:32:33 -0500784 if (waitForTransactionCallback && mNumFrameAvailable >= 2) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000785 acquireAndReleaseBuffer();
786 }
787 ATRACE_INT(mQueuedBufferTrace.c_str(),
788 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
789
790 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " syncTransactionSet=%s",
791 item.mFrameNumber, boolToString(syncTransactionSet));
792
793 if (syncTransactionSet) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800794 // Add to mSyncedFrameNumbers before waiting in case any buffers are released
795 // while waiting for a free buffer. The release and commit callback will try to
796 // acquire buffers if there are any available, but we don't want it to acquire
797 // in the case where a sync transaction wants the buffer.
798 mSyncedFrameNumbers.emplace(item.mFrameNumber);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000799 // If there's no available buffer and we're in a sync transaction, we need to wait
800 // instead of returning since we guarantee a buffer will be acquired for the sync.
801 while (acquireNextBufferLocked(mSyncTransaction) == BufferQueue::NO_BUFFER_AVAILABLE) {
802 BQA_LOGD("waiting for available buffer");
803 mCallbackCV.wait(_lock);
804 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000805
806 // Only need a commit callback when syncing to ensure the buffer that's synced has been
807 // sent to SF
808 incStrong((void*)transactionCommittedCallbackThunk);
809 mSyncTransaction->addTransactionCommittedCallback(transactionCommittedCallbackThunk,
810 static_cast<void*>(this));
Tianhao Yao4861b102022-02-03 20:18:35 +0000811 if (mAcquireSingleBuffer) {
812 prevCallback = mTransactionReadyCallback;
813 prevTransaction = mSyncTransaction;
814 mTransactionReadyCallback = nullptr;
815 mSyncTransaction = nullptr;
816 }
chaviwc1cf4022022-06-03 13:32:33 -0500817 } else if (!waitForTransactionCallback) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000818 acquireNextBufferLocked(std::nullopt);
Valerie Hau0188adf2020-02-13 08:29:20 -0800819 }
820 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000821 if (prevCallback) {
822 prevCallback(prevTransaction);
chaviwd7deef72021-10-06 11:53:40 -0500823 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800824}
825
Vishnu Nairaef1de92020-10-22 12:15:53 -0700826void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
827 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
828 // Do nothing since we are not storing unacquired buffer items locally.
829}
830
Vishnu Nairadf632b2021-01-07 14:05:08 -0800831void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000832 std::lock_guard _lock{mTimestampMutex};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800833 mDequeueTimestamps[bufferId] = systemTime();
Patrick Williams4b9507d2024-07-25 09:55:52 -0500834};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800835
836void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500837 {
838 std::lock_guard _lock{mTimestampMutex};
839 mDequeueTimestamps.erase(bufferId);
840 }
841
842#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
843 {
844 std::lock_guard lock{mMutex};
845 mNumDequeued--;
846 }
847#endif
848}
Vishnu Nairadf632b2021-01-07 14:05:08 -0800849
Chavi Weingartenc398c012023-04-12 17:26:02 +0000850bool BLASTBufferQueue::syncNextTransaction(
Tianhao Yao4861b102022-02-03 20:18:35 +0000851 std::function<void(SurfaceComposerClient::Transaction*)> callback,
852 bool acquireSingleBuffer) {
Chavi Weingartenc398c012023-04-12 17:26:02 +0000853 LOG_ALWAYS_FATAL_IF(!callback,
854 "BLASTBufferQueue: callback passed in to syncNextTransaction must not be "
855 "NULL");
chaviw3b4bdcf2022-03-17 09:27:03 -0500856
Chavi Weingartenc398c012023-04-12 17:26:02 +0000857 std::lock_guard _lock{mMutex};
858 BBQ_TRACE();
859 if (mTransactionReadyCallback) {
860 ALOGW("Attempting to overwrite transaction callback in syncNextTransaction");
861 return false;
Tianhao Yao4861b102022-02-03 20:18:35 +0000862 }
chaviw3b4bdcf2022-03-17 09:27:03 -0500863
Chavi Weingartenc398c012023-04-12 17:26:02 +0000864 mTransactionReadyCallback = callback;
865 mSyncTransaction = new SurfaceComposerClient::Transaction();
866 mAcquireSingleBuffer = acquireSingleBuffer;
867 return true;
Tianhao Yao4861b102022-02-03 20:18:35 +0000868}
869
870void BLASTBufferQueue::stopContinuousSyncTransaction() {
871 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
872 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
873 {
874 std::lock_guard _lock{mMutex};
Chavi Weingartenc398c012023-04-12 17:26:02 +0000875 if (mAcquireSingleBuffer || !mTransactionReadyCallback) {
876 ALOGW("Attempting to stop continuous sync when none are active");
877 return;
Tianhao Yao4861b102022-02-03 20:18:35 +0000878 }
Chavi Weingartenc398c012023-04-12 17:26:02 +0000879
880 prevCallback = mTransactionReadyCallback;
881 prevTransaction = mSyncTransaction;
882
Tianhao Yao4861b102022-02-03 20:18:35 +0000883 mTransactionReadyCallback = nullptr;
884 mSyncTransaction = nullptr;
885 mAcquireSingleBuffer = true;
886 }
Chavi Weingartenc398c012023-04-12 17:26:02 +0000887
Tianhao Yao4861b102022-02-03 20:18:35 +0000888 if (prevCallback) {
889 prevCallback(prevTransaction);
890 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700891}
892
Chavi Weingartenc398c012023-04-12 17:26:02 +0000893void BLASTBufferQueue::clearSyncTransaction() {
894 std::lock_guard _lock{mMutex};
895 if (!mAcquireSingleBuffer) {
896 ALOGW("Attempting to clear sync transaction when none are active");
897 return;
898 }
899
900 mTransactionReadyCallback = nullptr;
901 mSyncTransaction = nullptr;
902}
903
Vishnu Nairea0de002020-11-17 17:42:37 -0800904bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700905 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
906 // Only reject buffers if scaling mode is freeze.
907 return false;
908 }
909
Chavi Weingarten70670e62023-02-22 17:36:40 +0000910 ui::Size bufferSize = getBufferSize(item);
Vishnu Nairea0de002020-11-17 17:42:37 -0800911 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800912 return false;
913 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700914
Vishnu Nair670b3f72020-09-29 17:52:18 -0700915 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800916 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700917}
Vishnu Nairbf255772020-10-16 10:54:41 -0700918
Robert Carr05086b22020-10-13 18:22:51 -0700919class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700920private:
Vishnu Nair95b6d512021-08-30 15:31:08 -0700921 std::mutex mMutex;
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000922 sp<BLASTBufferQueue> mBbq GUARDED_BY(mMutex);
923 bool mDestroyed GUARDED_BY(mMutex) = false;
Vishnu Nair95b6d512021-08-30 15:31:08 -0700924
Robert Carr05086b22020-10-13 18:22:51 -0700925public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700926 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
927 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
928 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700929
Robert Carr05086b22020-10-13 18:22:51 -0700930 void allocateBuffers() override {
931 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
932 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
933 auto gbp = getIGraphicBufferProducer();
934 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
935 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
936 gbp->allocateBuffers(reqWidth, reqHeight,
937 reqFormat, reqUsage);
938
939 }).detach();
940 }
Robert Carr9c006e02020-10-14 13:41:57 -0700941
Marin Shalamanovc5986772021-03-16 16:09:49 +0100942 status_t setFrameRate(float frameRate, int8_t compatibility,
943 int8_t changeFrameRateStrategy) override {
Ady Abraham6cdd3fd2023-09-07 18:45:58 -0700944 if (flags::bq_setframerate()) {
945 return Surface::setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
946 }
947
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000948 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700949 if (mDestroyed) {
950 return DEAD_OBJECT;
951 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100952 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
953 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700954 return BAD_VALUE;
955 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100956 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700957 }
Robert Carr9b611b72020-10-19 12:00:23 -0700958
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800959 status_t setFrameTimelineInfo(uint64_t frameNumber,
960 const FrameTimelineInfo& frameTimelineInfo) override {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000961 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700962 if (mDestroyed) {
963 return DEAD_OBJECT;
964 }
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800965 return mBbq->setFrameTimelineInfo(frameNumber, frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700966 }
Vishnu Nair95b6d512021-08-30 15:31:08 -0700967
968 void destroy() override {
969 Surface::destroy();
970
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000971 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700972 mDestroyed = true;
973 mBbq = nullptr;
974 }
Robert Carr05086b22020-10-13 18:22:51 -0700975};
976
Robert Carr9c006e02020-10-14 13:41:57 -0700977// TODO: Can we coalesce this with frame updates? Need to confirm
978// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200979status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
980 bool shouldBeSeamless) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000981 std::lock_guard _lock{mMutex};
Robert Carr9c006e02020-10-14 13:41:57 -0700982 SurfaceComposerClient::Transaction t;
983
Marin Shalamanov46084422020-10-13 12:33:42 +0200984 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700985}
986
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800987status_t BLASTBufferQueue::setFrameTimelineInfo(uint64_t frameNumber,
988 const FrameTimelineInfo& frameTimelineInfo) {
989 ATRACE_FORMAT("%s(%s) frameNumber: %" PRIu64 " vsyncId: %" PRId64, __func__, mName.c_str(),
990 frameNumber, frameTimelineInfo.vsyncId);
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000991 std::lock_guard _lock{mMutex};
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800992 mPendingFrameTimelines.push({frameNumber, frameTimelineInfo});
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100993 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700994}
995
Hongguang Chen621ec582021-02-16 15:42:35 -0800996void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000997 std::lock_guard _lock{mMutex};
Hongguang Chen621ec582021-02-16 15:42:35 -0800998 SurfaceComposerClient::Transaction t;
999
1000 t.setSidebandStream(mSurfaceControl, stream).apply();
1001}
1002
Vishnu Nair992496b2020-10-22 17:27:21 -07001003sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001004 std::lock_guard _lock{mMutex};
Vishnu Nair992496b2020-10-22 17:27:21 -07001005 sp<IBinder> scHandle = nullptr;
1006 if (includeSurfaceControlHandle && mSurfaceControl) {
1007 scHandle = mSurfaceControl->getHandle();
1008 }
1009 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -07001010}
1011
Vishnu Nairc4a40c12020-12-23 09:14:32 -08001012void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
1013 uint64_t frameNumber) {
1014 std::lock_guard _lock{mMutex};
1015 if (mLastAcquiredFrameNumber >= frameNumber) {
1016 // Apply the transaction since we have already acquired the desired frame.
1017 t->apply();
1018 } else {
chaviwaad6cf52021-03-23 17:27:20 -05001019 mPendingTransactions.emplace_back(frameNumber, *t);
1020 // Clear the transaction so it can't be applied elsewhere.
1021 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -08001022 }
1023}
1024
chaviw6a195272021-09-03 16:14:25 -05001025void BLASTBufferQueue::applyPendingTransactions(uint64_t frameNumber) {
1026 std::lock_guard _lock{mMutex};
1027
1028 SurfaceComposerClient::Transaction t;
1029 mergePendingTransactions(&t, frameNumber);
Robert Carr79dc06a2022-02-22 15:28:59 -08001030 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
1031 t.setApplyToken(mApplyToken).apply(false, true);
chaviw6a195272021-09-03 16:14:25 -05001032}
1033
1034void BLASTBufferQueue::mergePendingTransactions(SurfaceComposerClient::Transaction* t,
1035 uint64_t frameNumber) {
1036 auto mergeTransaction =
1037 [&t, currentFrameNumber = frameNumber](
1038 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
1039 auto& [targetFrameNumber, transaction] = pendingTransaction;
1040 if (currentFrameNumber < targetFrameNumber) {
1041 return false;
1042 }
1043 t->merge(std::move(transaction));
1044 return true;
1045 };
1046
1047 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
1048 mPendingTransactions.end(), mergeTransaction),
1049 mPendingTransactions.end());
1050}
1051
chaviwd84085a2022-02-08 11:07:04 -06001052SurfaceComposerClient::Transaction* BLASTBufferQueue::gatherPendingTransactions(
1053 uint64_t frameNumber) {
1054 std::lock_guard _lock{mMutex};
1055 SurfaceComposerClient::Transaction* t = new SurfaceComposerClient::Transaction();
1056 mergePendingTransactions(t, frameNumber);
1057 return t;
1058}
1059
Vishnu Nair89496122020-12-14 17:14:53 -08001060// Maintains a single worker thread per process that services a list of runnables.
1061class AsyncWorker : public Singleton<AsyncWorker> {
1062private:
1063 std::thread mThread;
1064 bool mDone = false;
1065 std::deque<std::function<void()>> mRunnables;
1066 std::mutex mMutex;
1067 std::condition_variable mCv;
1068 void run() {
1069 std::unique_lock<std::mutex> lock(mMutex);
1070 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -08001071 while (!mRunnables.empty()) {
Vishnu Nair51e4dc82021-10-01 15:32:33 -07001072 std::deque<std::function<void()>> runnables = std::move(mRunnables);
1073 mRunnables.clear();
1074 lock.unlock();
1075 // Run outside the lock since the runnable might trigger another
1076 // post to the async worker.
1077 execute(runnables);
1078 lock.lock();
Vishnu Nair89496122020-12-14 17:14:53 -08001079 }
Wonsik Kim567533e2021-05-04 19:31:29 -07001080 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -08001081 }
1082 }
1083
Vishnu Nair51e4dc82021-10-01 15:32:33 -07001084 void execute(std::deque<std::function<void()>>& runnables) {
1085 while (!runnables.empty()) {
1086 std::function<void()> runnable = runnables.front();
1087 runnables.pop_front();
1088 runnable();
1089 }
1090 }
1091
Vishnu Nair89496122020-12-14 17:14:53 -08001092public:
1093 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
1094
1095 ~AsyncWorker() {
1096 mDone = true;
1097 mCv.notify_all();
1098 if (mThread.joinable()) {
1099 mThread.join();
1100 }
1101 }
1102
1103 void post(std::function<void()> runnable) {
1104 std::unique_lock<std::mutex> lock(mMutex);
1105 mRunnables.emplace_back(std::move(runnable));
1106 mCv.notify_one();
1107 }
1108};
1109ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
1110
1111// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
1112class AsyncProducerListener : public BnProducerListener {
1113private:
1114 const sp<IProducerListener> mListener;
1115
1116public:
1117 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
1118
1119 void onBufferReleased() override {
1120 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
1121 }
1122
1123 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
1124 AsyncWorker::getInstance().post(
1125 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
1126 }
1127};
1128
1129// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
1130// can be non-blocking when the producer is in the client process.
1131class BBQBufferQueueProducer : public BufferQueueProducer {
1132public:
Patrick Williamsca81c052024-08-15 12:38:34 -05001133 BBQBufferQueueProducer(const sp<BufferQueueCore>& core, const wp<BLASTBufferQueue>& bbq)
Brian Lindahlc794b692023-01-31 15:42:47 -07001134 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/),
Patrick Williamsca81c052024-08-15 12:38:34 -05001135 mBLASTBufferQueue(bbq) {}
Vishnu Nair89496122020-12-14 17:14:53 -08001136
1137 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
1138 QueueBufferOutput* output) override {
1139 if (!listener) {
1140 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
1141 }
1142
1143 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
1144 producerControlledByApp, output);
1145 }
Vishnu Nair17dde612020-12-28 11:39:59 -08001146
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001147#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1148 status_t disconnect(int api, DisconnectMode mode) override {
1149 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1150 if (!bbq) {
1151 return BufferQueueProducer::disconnect(api, mode);
1152 }
1153
1154 std::lock_guard lock{bbq->mMutex};
1155 if (status_t status = BufferQueueProducer::disconnect(api, mode); status != OK) {
1156 return status;
1157 }
1158
Patrick Williamsca81c052024-08-15 12:38:34 -05001159 // We need to reset dequeued and acquired counts because BufferQueueProducer::disconnect
1160 // calls BufferQueueCore::freeAllBuffersLocked which frees all dequeued and acquired
1161 // buffers. We don't reset mNumFrameAvailable because these buffers are still available
1162 // in BufferItemConsumer.
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001163 bbq->mNumDequeued = 0;
1164 bbq->mNumAcquired = 0;
Patrick Williamsca81c052024-08-15 12:38:34 -05001165 // SurfaceFlinger sends release callbacks for buffers that have been acquired after a
1166 // disconnect. We set disconnectedAfterAcquired to true so that we can ignore any stale
1167 // releases that come in after the producer is disconnected. Otherwise, releaseBuffer will
1168 // decrement mNumAcquired for a buffer that was acquired before we reset mNumAcquired to
1169 // zero.
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001170 for (auto& [releaseId, bufferItem] : bbq->mSubmitted) {
Patrick Williamsca81c052024-08-15 12:38:34 -05001171 bufferItem.disconnectedAfterAcquired = true;
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001172 }
1173
1174 return OK;
1175 }
1176
1177 status_t setAsyncMode(bool asyncMode) override {
1178 if (status_t status = BufferQueueProducer::setAsyncMode(asyncMode); status != OK) {
1179 return status;
1180 }
1181
1182 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1183 if (!bbq) {
1184 return OK;
1185 }
1186
1187 {
1188 std::lock_guard lock{bbq->mMutex};
1189 bbq->mAsyncMode = asyncMode;
1190 }
1191
1192 return OK;
1193 }
1194
1195 status_t setSharedBufferMode(bool sharedBufferMode) override {
1196 if (status_t status = BufferQueueProducer::setSharedBufferMode(sharedBufferMode);
1197 status != OK) {
1198 return status;
1199 }
1200
1201 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1202 if (!bbq) {
1203 return OK;
1204 }
1205
1206 {
1207 std::lock_guard lock{bbq->mMutex};
1208 bbq->mSharedBufferMode = sharedBufferMode;
1209 }
1210
1211 return OK;
1212 }
1213
1214 status_t detachBuffer(int slot) override {
1215 if (status_t status = BufferQueueProducer::detachBuffer(slot); status != OK) {
1216 return status;
1217 }
1218
1219 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1220 if (!bbq) {
1221 return OK;
1222 }
1223
1224 {
1225 std::lock_guard lock{bbq->mMutex};
1226 bbq->mNumDequeued--;
1227 }
1228
1229 return OK;
1230 }
1231
1232 status_t dequeueBuffer(int* outSlot, sp<Fence>* outFence, uint32_t width, uint32_t height,
1233 PixelFormat format, uint64_t usage, uint64_t* outBufferAge,
1234 FrameEventHistoryDelta* outTimestamps) override {
1235 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1236 if (!bbq) {
1237 return BufferQueueProducer::dequeueBuffer(outSlot, outFence, width, height, format,
1238 usage, outBufferAge, outTimestamps);
1239 }
1240
1241 {
1242 std::lock_guard lock{bbq->mMutex};
1243 bbq->mNumDequeued++;
1244 }
1245
1246 status_t status =
1247 BufferQueueProducer::dequeueBuffer(outSlot, outFence, width, height, format, usage,
1248 outBufferAge, outTimestamps);
1249 if (status < 0) {
1250 std::lock_guard lock{bbq->mMutex};
1251 bbq->mNumDequeued--;
1252 }
1253 return status;
1254 }
1255#endif
1256
Brian Lindahlc794b692023-01-31 15:42:47 -07001257 // We want to resize the frame history when changing the size of the buffer queue
1258 status_t setMaxDequeuedBufferCount(int maxDequeuedBufferCount) override {
1259 int maxBufferCount;
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001260 if (status_t status = BufferQueueProducer::setMaxDequeuedBufferCount(maxDequeuedBufferCount,
1261 &maxBufferCount);
1262 status != OK) {
1263 return status;
Brian Lindahlc794b692023-01-31 15:42:47 -07001264 }
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001265
1266 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1267 if (!bbq) {
1268 return OK;
1269 }
1270
1271 // if we can't determine the max buffer count, then just skip growing the history size
1272 size_t newFrameHistorySize = maxBufferCount + 2; // +2 because triple buffer rendering
1273 // optimize away resizing the frame history unless it will grow
1274 if (newFrameHistorySize > FrameEventHistory::INITIAL_MAX_FRAME_HISTORY) {
1275 ALOGV("increasing frame history size to %zu", newFrameHistorySize);
1276 bbq->resizeFrameEventHistory(newFrameHistorySize);
1277 }
1278
1279#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1280 {
1281 std::lock_guard lock{bbq->mMutex};
1282 bbq->mMaxDequeuedBuffers = maxDequeuedBufferCount;
1283 }
1284#endif
1285
1286 return OK;
Brian Lindahlc794b692023-01-31 15:42:47 -07001287 }
1288
Vishnu Nair17dde612020-12-28 11:39:59 -08001289 int query(int what, int* value) override {
1290 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
1291 *value = 1;
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001292 return OK;
Vishnu Nair17dde612020-12-28 11:39:59 -08001293 }
1294 return BufferQueueProducer::query(what, value);
1295 }
Brian Lindahlc794b692023-01-31 15:42:47 -07001296
1297private:
1298 const wp<BLASTBufferQueue> mBLASTBufferQueue;
Vishnu Nair89496122020-12-14 17:14:53 -08001299};
1300
1301// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
1302// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
1303// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
1304// we can deadlock.
1305void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
1306 sp<IGraphicBufferConsumer>* outConsumer) {
1307 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
1308 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
1309
1310 sp<BufferQueueCore> core(new BufferQueueCore());
1311 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
1312
Brian Lindahlc794b692023-01-31 15:42:47 -07001313 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core, this));
Vishnu Nair89496122020-12-14 17:14:53 -08001314 LOG_ALWAYS_FATAL_IF(producer == nullptr,
1315 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
1316
Vishnu Nair8b30dd12021-01-25 14:16:54 -08001317 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
1318 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -08001319 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
1320 "BLASTBufferQueue: failed to create BufferQueueConsumer");
1321
1322 *outProducer = producer;
1323 *outConsumer = consumer;
1324}
1325
Brian Lindahlc794b692023-01-31 15:42:47 -07001326void BLASTBufferQueue::resizeFrameEventHistory(size_t newSize) {
1327 // This can be null during creation of the buffer queue, but resizing won't do anything at that
1328 // point in time, so just ignore. This can go away once the class relationships and lifetimes of
1329 // objects are cleaned up with a major refactor of BufferQueue as a whole.
1330 if (mBufferItemConsumer != nullptr) {
1331 std::unique_lock _lock{mMutex};
1332 mBufferItemConsumer->resizeFrameEventHistory(newSize);
1333 }
1334}
1335
chaviw497e81c2021-02-04 17:09:47 -08001336PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
1337 PixelFormat convertedFormat = format;
1338 switch (format) {
1339 case PIXEL_FORMAT_TRANSPARENT:
1340 case PIXEL_FORMAT_TRANSLUCENT:
1341 convertedFormat = PIXEL_FORMAT_RGBA_8888;
1342 break;
1343 case PIXEL_FORMAT_OPAQUE:
1344 convertedFormat = PIXEL_FORMAT_RGBX_8888;
1345 break;
1346 }
1347 return convertedFormat;
1348}
1349
Robert Carr82d07c92021-05-10 11:36:43 -07001350uint32_t BLASTBufferQueue::getLastTransformHint() const {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001351 std::lock_guard _lock{mMutex};
Robert Carr82d07c92021-05-10 11:36:43 -07001352 if (mSurfaceControl != nullptr) {
1353 return mSurfaceControl->getTransformHint();
1354 } else {
1355 return 0;
1356 }
1357}
1358
chaviw0b020f82021-08-20 12:00:47 -05001359uint64_t BLASTBufferQueue::getLastAcquiredFrameNum() {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001360 std::lock_guard _lock{mMutex};
chaviw0b020f82021-08-20 12:00:47 -05001361 return mLastAcquiredFrameNumber;
1362}
1363
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001364bool BLASTBufferQueue::isSameSurfaceControl(const sp<SurfaceControl>& surfaceControl) const {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001365 std::lock_guard _lock{mMutex};
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001366 return SurfaceControl::isSameSurface(mSurfaceControl, surfaceControl);
1367}
1368
Patrick Williamsf1e5df12022-10-17 21:37:42 +00001369void BLASTBufferQueue::setTransactionHangCallback(
1370 std::function<void(const std::string&)> callback) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001371 std::lock_guard _lock{mMutex};
Robert Carr4c1b6462021-12-21 10:30:50 -08001372 mTransactionHangCallback = callback;
1373}
1374
Robert Carr78c25dd2019-08-15 14:10:33 -07001375} // namespace android