blob: ba82046388a00df9256bb245cbbdbdf44aa602c0 [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>
Patrick Williams7c9fa272024-08-30 12:38:43 +000025#include <ftl/fake_guard.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070026#include <gui/BLASTBufferQueue.h>
27#include <gui/BufferItemConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080028#include <gui/BufferQueueConsumer.h>
29#include <gui/BufferQueueCore.h>
30#include <gui/BufferQueueProducer.h>
Patrick Williams7c9fa272024-08-30 12:38:43 +000031#include <sys/epoll.h>
32#include <sys/eventfd.h>
Ady Abraham107788e2023-10-17 12:31:08 -070033
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070034#include <gui/FrameRateUtils.h>
Valerie Hau45e4b3b2019-12-03 10:49:17 -080035#include <gui/GLConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080036#include <gui/IProducerListener.h>
Robert Carr05086b22020-10-13 18:22:51 -070037#include <gui/Surface.h>
chaviw57ae4b22022-02-03 16:51:39 -060038#include <gui/TraceUtils.h>
Vishnu Nair89496122020-12-14 17:14:53 -080039#include <utils/Singleton.h>
Valerie Haua32c5522019-12-09 10:11:08 -080040#include <utils/Trace.h>
41
Ady Abraham0bde6b52021-05-18 13:57:02 -070042#include <private/gui/ComposerService.h>
Huihong Luo02186fb2022-02-23 14:21:54 -080043#include <private/gui/ComposerServiceAIDL.h>
Ady Abraham0bde6b52021-05-18 13:57:02 -070044
Chavi Weingartene0237bb2023-02-06 21:48:32 +000045#include <android-base/thread_annotations.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070046
Alec Mouri21d94322023-10-17 19:51:39 +000047#include <com_android_graphics_libgui_flags.h>
48
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070049using namespace com::android::graphics::libgui;
Robert Carr78c25dd2019-08-15 14:10:33 -070050using namespace std::chrono_literals;
51
Vishnu Nairdab94092020-09-29 16:09:04 -070052namespace {
chaviw3277faf2021-05-19 16:45:23 -050053inline const char* boolToString(bool b) {
Vishnu Nairdab94092020-09-29 16:09:04 -070054 return b ? "true" : "false";
55}
56} // namespace
57
Robert Carr78c25dd2019-08-15 14:10:33 -070058namespace android {
59
Vishnu Nairdab94092020-09-29 16:09:04 -070060// Macros to include adapter info in log messages
chaviwd7deef72021-10-06 11:53:40 -050061#define BQA_LOGD(x, ...) \
62 ALOGD("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070063#define BQA_LOGV(x, ...) \
64 ALOGV("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairc6f89ee2020-12-11 14:27:32 -080065// enable logs for a single layer
66//#define BQA_LOGV(x, ...) \
67// ALOGV_IF((strstr(mName.c_str(), "SurfaceView") != nullptr), "[%s](f:%u,a:%u) " x, \
68// mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070069#define BQA_LOGE(x, ...) \
70 ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
71
chaviw57ae4b22022-02-03 16:51:39 -060072#define BBQ_TRACE(x, ...) \
73 ATRACE_FORMAT("%s - %s(f:%u,a:%u)" x, __FUNCTION__, mName.c_str(), mNumFrameAvailable, \
74 mNumAcquired, ##__VA_ARGS__)
75
Chavi Weingartene0237bb2023-02-06 21:48:32 +000076#define UNIQUE_LOCK_WITH_ASSERTION(mutex) \
77 std::unique_lock _lock{mutex}; \
78 base::ScopedLockAssertion assumeLocked(mutex);
79
Patrick Williams7c9fa272024-08-30 12:38:43 +000080#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
81static ReleaseBufferCallback EMPTY_RELEASE_CALLBACK =
82 [](const ReleaseCallbackId&, const sp<Fence>& /*releaseFence*/,
83 std::optional<uint32_t> /*currentMaxAcquiredBufferCount*/) {};
84#endif
85
Valerie Hau871d6352020-01-29 08:44:02 -080086void BLASTBufferItemConsumer::onDisconnect() {
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000087 Mutex::Autolock lock(mMutex);
88 mPreviouslyConnected = mCurrentlyConnected;
89 mCurrentlyConnected = false;
90 if (mPreviouslyConnected) {
91 mDisconnectEvents.push(mCurrentFrameNumber);
Valerie Hau871d6352020-01-29 08:44:02 -080092 }
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000093 mFrameEventHistory.onDisconnect();
Valerie Hau871d6352020-01-29 08:44:02 -080094}
95
96void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
97 FrameEventHistoryDelta* outDelta) {
Hongguang Chen621ec582021-02-16 15:42:35 -080098 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080099 if (newTimestamps) {
100 // BufferQueueProducer only adds a new timestamp on
101 // queueBuffer
102 mCurrentFrameNumber = newTimestamps->frameNumber;
103 mFrameEventHistory.addQueue(*newTimestamps);
104 }
105 if (outDelta) {
106 // frame event histories will be processed
107 // only after the producer connects and requests
108 // deltas for the first time. Forward this intent
109 // to SF-side to turn event processing back on
110 mPreviouslyConnected = mCurrentlyConnected;
111 mCurrentlyConnected = true;
112 mFrameEventHistory.getAndResetDelta(outDelta);
113 }
114}
115
Alec Mouri21d94322023-10-17 19:51:39 +0000116void BLASTBufferItemConsumer::updateFrameTimestamps(
117 uint64_t frameNumber, uint64_t previousFrameNumber, nsecs_t refreshStartTime,
118 const sp<Fence>& glDoneFence, const sp<Fence>& presentFence,
119 const sp<Fence>& prevReleaseFence, CompositorTiming compositorTiming, nsecs_t latchTime,
120 nsecs_t dequeueReadyTime) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800121 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800122
123 // if the producer is not connected, don't bother updating,
124 // the next producer that connects won't access this frame event
125 if (!mCurrentlyConnected) return;
126 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
127 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
128 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
129
130 mFrameEventHistory.addLatch(frameNumber, latchTime);
Alec Mouri21d94322023-10-17 19:51:39 +0000131 if (flags::frametimestamps_previousrelease()) {
132 if (previousFrameNumber > 0) {
133 mFrameEventHistory.addRelease(previousFrameNumber, dequeueReadyTime,
134 std::move(releaseFenceTime));
135 }
136 } else {
137 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
138 }
139
Valerie Hau871d6352020-01-29 08:44:02 -0800140 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
141 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
142 compositorTiming);
143}
144
145void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
146 bool disconnect = false;
Hongguang Chen621ec582021-02-16 15:42:35 -0800147 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800148 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
149 disconnect = true;
150 mDisconnectEvents.pop();
151 }
152 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
153}
154
Hongguang Chen621ec582021-02-16 15:42:35 -0800155void BLASTBufferItemConsumer::onSidebandStreamChanged() {
Ady Abrahamdbca1352021-12-15 11:58:56 -0800156 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
157 if (bbq != nullptr) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800158 sp<NativeHandle> stream = getSidebandStream();
Ady Abrahamdbca1352021-12-15 11:58:56 -0800159 bbq->setSidebandStream(stream);
Hongguang Chen621ec582021-02-16 15:42:35 -0800160 }
161}
162
Ady Abraham107788e2023-10-17 12:31:08 -0700163#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -0700164void BLASTBufferItemConsumer::onSetFrameRate(float frameRate, int8_t compatibility,
165 int8_t changeFrameRateStrategy) {
166 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
167 if (bbq != nullptr) {
168 bbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
169 }
170}
171#endif
172
Brian Lindahlc794b692023-01-31 15:42:47 -0700173void BLASTBufferItemConsumer::resizeFrameEventHistory(size_t newSize) {
174 Mutex::Autolock lock(mMutex);
175 mFrameEventHistory.resize(newSize);
176}
177
Vishnu Naird2aaab12022-02-10 14:49:09 -0800178BLASTBufferQueue::BLASTBufferQueue(const std::string& name, bool updateDestinationFrame)
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800179 : mSurfaceControl(nullptr),
180 mSize(1, 1),
Vishnu Nairea0de002020-11-17 17:42:37 -0800181 mRequestedSize(mSize),
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800182 mFormat(PIXEL_FORMAT_RGBA_8888),
Tianhao Yao4861b102022-02-03 20:18:35 +0000183 mTransactionReadyCallback(nullptr),
Vishnu Naird2aaab12022-02-10 14:49:09 -0800184 mSyncTransaction(nullptr),
185 mUpdateDestinationFrame(updateDestinationFrame) {
Vishnu Nair89496122020-12-14 17:14:53 -0800186 createBufferQueue(&mProducer, &mConsumer);
Jim Shargod30823a2024-07-27 02:49:39 +0000187#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
188 mBufferItemConsumer = new BLASTBufferItemConsumer(mProducer, mConsumer,
189 GraphicBuffer::USAGE_HW_COMPOSER |
190 GraphicBuffer::USAGE_HW_TEXTURE,
191 1, false, this);
192#else
Vishnu Nair1618c672021-02-05 13:08:26 -0800193 mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
194 GraphicBuffer::USAGE_HW_COMPOSER |
195 GraphicBuffer::USAGE_HW_TEXTURE,
Ady Abrahamdbca1352021-12-15 11:58:56 -0800196 1, false, this);
Jim Shargod30823a2024-07-27 02:49:39 +0000197#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
198 // since the adapter is in the client process, set dequeue timeout
199 // explicitly so that dequeueBuffer will block
200 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
201
liulijuneb489f62022-10-17 22:02:14 +0800202 static std::atomic<uint32_t> nextId = 0;
203 mProducerId = nextId++;
204 mName = name + "#" + std::to_string(mProducerId);
205 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(mProducerId);
206 mQueuedBufferTrace = "QueuedBuffer - " + mName + "BLAST#" + std::to_string(mProducerId);
Vishnu Nairdab94092020-09-29 16:09:04 -0700207 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700208 mBufferItemConsumer->setFrameAvailableListener(this);
Robert Carr9f133d72020-04-01 15:51:46 -0700209
Huihong Luo02186fb2022-02-23 14:21:54 -0800210 ComposerServiceAIDL::getComposerService()->getMaxAcquiredBufferCount(&mMaxAcquiredBuffers);
Ady Abraham0bde6b52021-05-18 13:57:02 -0700211 mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxAcquiredBuffers);
chaviw69058fb2021-09-27 09:37:30 -0500212 mCurrentMaxAcquiredBufferCount = mMaxAcquiredBuffers;
Robert Carr4c1b6462021-12-21 10:30:50 -0800213
214 TransactionCompletedListener::getInstance()->addQueueStallListener(
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000215 [&](const std::string& reason) {
216 std::function<void(const std::string&)> callbackCopy;
217 {
218 std::unique_lock _lock{mMutex};
219 callbackCopy = mTransactionHangCallback;
220 }
221 if (callbackCopy) callbackCopy(reason);
222 },
223 this);
Robert Carr4c1b6462021-12-21 10:30:50 -0800224
Patrick Williams7c9fa272024-08-30 12:38:43 +0000225#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
226 std::unique_ptr<gui::BufferReleaseChannel::ConsumerEndpoint> bufferReleaseConsumer;
227 gui::BufferReleaseChannel::open(mName, bufferReleaseConsumer, mBufferReleaseProducer);
228 mBufferReleaseReader = std::make_shared<BufferReleaseReader>(std::move(bufferReleaseConsumer));
229#endif
230
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800231 BQA_LOGV("BLASTBufferQueue created");
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800232}
233
234BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface,
235 int width, int height, int32_t format)
236 : BLASTBufferQueue(name) {
237 update(surface, width, height, format);
Robert Carr78c25dd2019-08-15 14:10:33 -0700238}
239
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800240BLASTBufferQueue::~BLASTBufferQueue() {
Robert Carr4c1b6462021-12-21 10:30:50 -0800241 TransactionCompletedListener::getInstance()->removeQueueStallListener(this);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800242 if (mPendingTransactions.empty()) {
243 return;
244 }
245 BQA_LOGE("Applying pending transactions on dtor %d",
246 static_cast<uint32_t>(mPendingTransactions.size()));
247 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800248 mergePendingTransactions(&t, std::numeric_limits<uint64_t>::max() /* frameNumber */);
Robert Carr79dc06a2022-02-22 15:28:59 -0800249 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
250 t.setApplyToken(mApplyToken).apply(false, true);
chaviw3b4bdcf2022-03-17 09:27:03 -0500251
252 if (mTransactionReadyCallback) {
253 mTransactionReadyCallback(mSyncTransaction);
254 }
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800255}
256
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500257void BLASTBufferQueue::onFirstRef() {
258 // safe default, most producers are expected to override this
259 mProducer->setMaxDequeuedBufferCount(2);
Patrick Williams7c9fa272024-08-30 12:38:43 +0000260#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
261 mBufferReleaseThread.start(sp<BLASTBufferQueue>::fromExisting(this));
262#endif
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500263}
264
chaviw565ee542021-01-14 10:21:23 -0800265void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height,
Vishnu Naird2aaab12022-02-10 14:49:09 -0800266 int32_t format) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800267 LOG_ALWAYS_FATAL_IF(surface == nullptr, "BLASTBufferQueue: mSurfaceControl must not be NULL");
268
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000269 std::lock_guard _lock{mMutex};
chaviw565ee542021-01-14 10:21:23 -0800270 if (mFormat != format) {
271 mFormat = format;
chaviw497e81c2021-02-04 17:09:47 -0800272 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
chaviw565ee542021-01-14 10:21:23 -0800273 }
274
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800275 const bool surfaceControlChanged = !SurfaceControl::isSameSurface(mSurfaceControl, surface);
Vishnu Nairab066512022-01-04 22:28:00 +0000276 if (surfaceControlChanged && mSurfaceControl != nullptr) {
277 BQA_LOGD("Updating SurfaceControl without recreating BBQ");
278 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800279 bool applyTransaction = false;
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800280
Vishnu Nair5fa91c22021-06-29 14:30:48 -0700281 // Always update the native object even though they might have the same layer handle, so we can
282 // get the updated transform hint from WM.
283 mSurfaceControl = surface;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800284 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800285 if (surfaceControlChanged) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800286 t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure,
287 layer_state_t::eEnableBackpressure);
Patrick Williams7c9fa272024-08-30 12:38:43 +0000288#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
289 t.setBufferReleaseChannel(mSurfaceControl, mBufferReleaseProducer);
290#endif
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800291 applyTransaction = true;
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800292 }
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800293 mTransformHint = mSurfaceControl->getTransformHint();
294 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Naira4fbca52021-07-07 16:52:34 -0700295 BQA_LOGV("update width=%d height=%d format=%d mTransformHint=%d", width, height, format,
296 mTransformHint);
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800297
Vishnu Nairea0de002020-11-17 17:42:37 -0800298 ui::Size newSize(width, height);
299 if (mRequestedSize != newSize) {
300 mRequestedSize.set(newSize);
301 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000302 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800303 // If the buffer supports scaling, update the frame immediately since the client may
304 // want to scale the existing buffer to the new size.
305 mSize = mRequestedSize;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800306 if (mUpdateDestinationFrame) {
307 t.setDestinationFrame(mSurfaceControl, Rect(newSize));
308 applyTransaction = true;
309 }
Vishnu Nair53c936c2020-12-03 11:46:37 -0800310 }
Robert Carrfc416512020-04-02 12:32:44 -0700311 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800312 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800313 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
314 t.setApplyToken(mApplyToken).apply(false, true);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800315 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700316}
317
chaviwd7deef72021-10-06 11:53:40 -0500318static std::optional<SurfaceControlStats> findMatchingStat(
319 const std::vector<SurfaceControlStats>& stats, const sp<SurfaceControl>& sc) {
320 for (auto stat : stats) {
321 if (SurfaceControl::isSameSurface(sc, stat.surfaceControl)) {
322 return stat;
323 }
324 }
325 return std::nullopt;
326}
327
Patrick Williams5312ec12024-08-23 16:11:10 -0500328TransactionCompletedCallbackTakesContext BLASTBufferQueue::makeTransactionCommittedCallbackThunk() {
329 return [bbq = sp<BLASTBufferQueue>::fromExisting(
330 this)](void* /*context*/, nsecs_t latchTime, const sp<Fence>& presentFence,
331 const std::vector<SurfaceControlStats>& stats) {
332 bbq->transactionCommittedCallback(latchTime, presentFence, stats);
333 };
chaviwd7deef72021-10-06 11:53:40 -0500334}
335
336void BLASTBufferQueue::transactionCommittedCallback(nsecs_t /*latchTime*/,
337 const sp<Fence>& /*presentFence*/,
338 const std::vector<SurfaceControlStats>& stats) {
339 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000340 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600341 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500342 BQA_LOGV("transactionCommittedCallback");
343 if (!mSurfaceControlsWithPendingCallback.empty()) {
344 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
345 std::optional<SurfaceControlStats> stat = findMatchingStat(stats, pendingSC);
346 if (stat) {
347 uint64_t currFrameNumber = stat->frameEventStats.frameNumber;
348
349 // We need to check if we were waiting for a transaction callback in order to
350 // process any pending buffers and unblock. It's possible to get transaction
chaviwc1cf4022022-06-03 13:32:33 -0500351 // callbacks for previous requests so we need to ensure that there are no pending
352 // frame numbers that were in a sync. We remove the frame from mSyncedFrameNumbers
353 // set and then check if it's empty. If there are no more pending syncs, we can
354 // proceed with flushing the shadow queue.
chaviwc1cf4022022-06-03 13:32:33 -0500355 mSyncedFrameNumbers.erase(currFrameNumber);
Chavi Weingartend48797b2023-08-04 13:11:39 +0000356 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500357 flushShadowQueue();
358 }
359 } else {
chaviw768bfa02021-11-01 09:50:57 -0500360 BQA_LOGE("Failed to find matching SurfaceControl in transactionCommittedCallback");
chaviwd7deef72021-10-06 11:53:40 -0500361 }
362 } else {
363 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
364 "empty.");
365 }
chaviwd7deef72021-10-06 11:53:40 -0500366 }
367}
368
Patrick Williams5312ec12024-08-23 16:11:10 -0500369TransactionCompletedCallbackTakesContext BLASTBufferQueue::makeTransactionCallbackThunk() {
370 return [bbq = sp<BLASTBufferQueue>::fromExisting(
371 this)](void* /*context*/, nsecs_t latchTime, const sp<Fence>& presentFence,
372 const std::vector<SurfaceControlStats>& stats) {
373 bbq->transactionCallback(latchTime, presentFence, stats);
374 };
Robert Carr78c25dd2019-08-15 14:10:33 -0700375}
376
377void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
378 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700379 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000380 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600381 BBQ_TRACE();
chaviw71c2cc42020-10-23 16:42:02 -0700382 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700383
chaviw42026162021-04-16 15:46:12 -0500384 if (!mSurfaceControlsWithPendingCallback.empty()) {
385 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
386 mSurfaceControlsWithPendingCallback.pop();
chaviwd7deef72021-10-06 11:53:40 -0500387 std::optional<SurfaceControlStats> statsOptional = findMatchingStat(stats, pendingSC);
388 if (statsOptional) {
389 SurfaceControlStats stat = *statsOptional;
Vishnu Nair71fcf912022-10-18 09:14:20 -0700390 if (stat.transformHint) {
391 mTransformHint = *stat.transformHint;
392 mBufferItemConsumer->setTransformHint(mTransformHint);
393 BQA_LOGV("updated mTransformHint=%d", mTransformHint);
394 }
Vishnu Nairde66dc72021-06-17 17:54:41 -0700395 // Update frametime stamps if the frame was latched and presented, indicated by a
396 // valid latch time.
397 if (stat.latchTime > 0) {
398 mBufferItemConsumer
399 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
Alec Mouri21d94322023-10-17 19:51:39 +0000400 stat.frameEventStats.previousFrameNumber,
Vishnu Nairde66dc72021-06-17 17:54:41 -0700401 stat.frameEventStats.refreshStartTime,
402 stat.frameEventStats.gpuCompositionDoneFence,
403 stat.presentFence, stat.previousReleaseFence,
404 stat.frameEventStats.compositorTiming,
405 stat.latchTime,
406 stat.frameEventStats.dequeueReadyTime);
407 }
Patrick Williams7c9fa272024-08-30 12:38:43 +0000408#if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
Robert Carr405e2f62021-12-31 16:59:34 -0800409 auto currFrameNumber = stat.frameEventStats.frameNumber;
410 std::vector<ReleaseCallbackId> staleReleases;
411 for (const auto& [key, value]: mSubmitted) {
412 if (currFrameNumber > key.framenumber) {
413 staleReleases.push_back(key);
414 }
415 }
416 for (const auto& staleRelease : staleReleases) {
Robert Carr405e2f62021-12-31 16:59:34 -0800417 releaseBufferCallbackLocked(staleRelease,
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700418 stat.previousReleaseFence
419 ? stat.previousReleaseFence
420 : Fence::NO_FENCE,
421 stat.currentMaxAcquiredBufferCount,
422 true /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800423 }
Patrick Williams7c9fa272024-08-30 12:38:43 +0000424#endif
chaviwd7deef72021-10-06 11:53:40 -0500425 } else {
chaviw768bfa02021-11-01 09:50:57 -0500426 BQA_LOGE("Failed to find matching SurfaceControl in transactionCallback");
chaviw42026162021-04-16 15:46:12 -0500427 }
428 } else {
429 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
430 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800431 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700432 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700433}
434
Patrick Williams7c9fa272024-08-30 12:38:43 +0000435void BLASTBufferQueue::flushShadowQueue() {
436 BQA_LOGV("flushShadowQueue");
Shuangxi Xianga576ccd2025-02-12 03:39:54 -0800437 int32_t numFramesToFlush = mNumFrameAvailable;
Patrick Williams7c9fa272024-08-30 12:38:43 +0000438 while (numFramesToFlush > 0) {
439 acquireNextBufferLocked(std::nullopt);
440 numFramesToFlush--;
441 }
442}
443
Vishnu Nair1506b182021-02-22 14:35:15 -0800444// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
445// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
446// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
447// Otherwise, this is a no-op.
Patrick Williams5312ec12024-08-23 16:11:10 -0500448ReleaseBufferCallback BLASTBufferQueue::makeReleaseBufferCallbackThunk() {
449 return [weakBbq = wp<BLASTBufferQueue>::fromExisting(
450 this)](const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
451 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
452 sp<BLASTBufferQueue> bbq = weakBbq.promote();
453 if (!bbq) {
454 ALOGV("releaseBufferCallbackThunk %s blastBufferQueue is dead", id.to_string().c_str());
455 return;
456 }
457 bbq->releaseBufferCallback(id, releaseFence, currentMaxAcquiredBufferCount);
458 };
Vishnu Nair1506b182021-02-22 14:35:15 -0800459}
460
chaviw69058fb2021-09-27 09:37:30 -0500461void BLASTBufferQueue::releaseBufferCallback(
462 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
463 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000464 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600465 BBQ_TRACE();
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700466 releaseBufferCallbackLocked(id, releaseFence, currentMaxAcquiredBufferCount,
467 false /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800468}
469
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700470void BLASTBufferQueue::releaseBufferCallbackLocked(
471 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
472 std::optional<uint32_t> currentMaxAcquiredBufferCount, bool fakeRelease) {
Robert Carr405e2f62021-12-31 16:59:34 -0800473 ATRACE_CALL();
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700474 BQA_LOGV("releaseBufferCallback %s", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800475
Ady Abraham899dcdb2021-06-15 16:56:21 -0700476 // Calculate how many buffers we need to hold before we release them back
477 // to the buffer queue. This will prevent higher latency when we are running
478 // on a lower refresh rate than the max supported. We only do that for EGL
479 // clients as others don't care about latency
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000480 const auto it = mSubmitted.find(id);
481 const bool isEGL = it != mSubmitted.end() && it->second.mApi == NATIVE_WINDOW_API_EGL;
Ady Abraham899dcdb2021-06-15 16:56:21 -0700482
chaviw69058fb2021-09-27 09:37:30 -0500483 if (currentMaxAcquiredBufferCount) {
484 mCurrentMaxAcquiredBufferCount = *currentMaxAcquiredBufferCount;
485 }
486
liulijunf90df632022-11-14 14:24:48 +0800487 const uint32_t numPendingBuffersToHold =
488 isEGL ? std::max(0, mMaxAcquiredBuffers - (int32_t)mCurrentMaxAcquiredBufferCount) : 0;
Robert Carr405e2f62021-12-31 16:59:34 -0800489
490 auto rb = ReleasedBuffer{id, releaseFence};
491 if (std::find(mPendingRelease.begin(), mPendingRelease.end(), rb) == mPendingRelease.end()) {
492 mPendingRelease.emplace_back(rb);
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700493 if (fakeRelease) {
494 BQA_LOGE("Faking releaseBufferCallback from transactionCompleteCallback %" PRIu64,
495 id.framenumber);
496 BBQ_TRACE("FakeReleaseCallback");
497 }
Robert Carr405e2f62021-12-31 16:59:34 -0800498 }
Ady Abraham899dcdb2021-06-15 16:56:21 -0700499
500 // Release all buffers that are beyond the ones that we need to hold
501 while (mPendingRelease.size() > numPendingBuffersToHold) {
chaviw0acd33a2021-11-02 11:55:37 -0500502 const auto releasedBuffer = mPendingRelease.front();
Ady Abraham899dcdb2021-06-15 16:56:21 -0700503 mPendingRelease.pop_front();
chaviw0acd33a2021-11-02 11:55:37 -0500504 releaseBuffer(releasedBuffer.callbackId, releasedBuffer.releaseFence);
chaviwc1cf4022022-06-03 13:32:33 -0500505 // Don't process the transactions here if mSyncedFrameNumbers is not empty. That means
506 // are still transactions that have sync buffers in them that have not been applied or
507 // dropped. Instead, let onFrameAvailable handle processing them since it will merge with
508 // the syncTransaction.
509 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500510 acquireNextBufferLocked(std::nullopt);
511 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800512 }
513
Ady Abraham899dcdb2021-06-15 16:56:21 -0700514 ATRACE_INT("PendingRelease", mPendingRelease.size());
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700515 ATRACE_INT(mQueuedBufferTrace.c_str(),
516 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
Vishnu Nair1506b182021-02-22 14:35:15 -0800517 mCallbackCV.notify_all();
518}
519
chaviw0acd33a2021-11-02 11:55:37 -0500520void BLASTBufferQueue::releaseBuffer(const ReleaseCallbackId& callbackId,
521 const sp<Fence>& releaseFence) {
522 auto it = mSubmitted.find(callbackId);
523 if (it == mSubmitted.end()) {
Patrick Williams4b9507d2024-07-25 09:55:52 -0500524 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %s",
525 callbackId.to_string().c_str());
chaviw0acd33a2021-11-02 11:55:37 -0500526 return;
527 }
528 mNumAcquired--;
chaviw57ae4b22022-02-03 16:51:39 -0600529 BBQ_TRACE("frame=%" PRIu64, callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500530 BQA_LOGV("released %s", callbackId.to_string().c_str());
531 mBufferItemConsumer->releaseBuffer(it->second, releaseFence);
532 mSubmitted.erase(it);
chaviwc1cf4022022-06-03 13:32:33 -0500533 // Remove the frame number from mSyncedFrameNumbers since we can get a release callback
534 // without getting a transaction committed if the buffer was dropped.
535 mSyncedFrameNumbers.erase(callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500536}
537
Chavi Weingarten70670e62023-02-22 17:36:40 +0000538static ui::Size getBufferSize(const BufferItem& item) {
539 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
540 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
541
542 // Take the buffer's orientation into account
543 if (item.mTransform & ui::Transform::ROT_90) {
544 std::swap(bufWidth, bufHeight);
545 }
546 return ui::Size(bufWidth, bufHeight);
547}
548
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000549status_t BLASTBufferQueue::acquireNextBufferLocked(
chaviwd7deef72021-10-06 11:53:40 -0500550 const std::optional<SurfaceComposerClient::Transaction*> transaction) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800551 // Check if we have frames available and we have not acquired the maximum number of buffers.
552 // Even with this check, the consumer can fail to acquire an additional buffer if the consumer
553 // has already acquired (mMaxAcquiredBuffers + 1) and the new buffer is not droppable. In this
554 // case mBufferItemConsumer->acquireBuffer will return with NO_BUFFER_AVAILABLE.
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000555 if (mNumFrameAvailable == 0) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800556 BQA_LOGV("Can't acquire next buffer. No available frames");
557 return BufferQueue::NO_BUFFER_AVAILABLE;
558 }
559
560 if (mNumAcquired >= (mMaxAcquiredBuffers + 2)) {
561 BQA_LOGV("Can't acquire next buffer. Already acquired max frames %d max:%d + 2",
562 mNumAcquired, mMaxAcquiredBuffers);
563 return BufferQueue::NO_BUFFER_AVAILABLE;
Valerie Haud3b90d22019-11-06 09:37:31 -0800564 }
565
Valerie Haua32c5522019-12-09 10:11:08 -0800566 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700567 BQA_LOGE("ERROR : surface control is null");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000568 return NAME_NOT_FOUND;
Valerie Haud3b90d22019-11-06 09:37:31 -0800569 }
570
Robert Carr78c25dd2019-08-15 14:10:33 -0700571 SurfaceComposerClient::Transaction localTransaction;
572 bool applyTransaction = true;
573 SurfaceComposerClient::Transaction* t = &localTransaction;
chaviwd7deef72021-10-06 11:53:40 -0500574 if (transaction) {
575 t = *transaction;
Robert Carr78c25dd2019-08-15 14:10:33 -0700576 applyTransaction = false;
577 }
578
Patrick Williams3ced5382024-08-21 15:39:32 -0500579 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800580
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800581 status_t status =
582 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800583 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
584 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000585 return status;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800586 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700587 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000588 return status;
Robert Carr78c25dd2019-08-15 14:10:33 -0700589 }
chaviw57ae4b22022-02-03 16:51:39 -0600590
Valerie Haua32c5522019-12-09 10:11:08 -0800591 auto buffer = bufferItem.mGraphicBuffer;
592 mNumFrameAvailable--;
chaviw57ae4b22022-02-03 16:51:39 -0600593 BBQ_TRACE("frame=%" PRIu64, bufferItem.mFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800594
595 if (buffer == nullptr) {
596 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700597 BQA_LOGE("Buffer was empty");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000598 return BAD_VALUE;
Valerie Haua32c5522019-12-09 10:11:08 -0800599 }
600
Vishnu Nair670b3f72020-09-29 17:52:18 -0700601 if (rejectBuffer(bufferItem)) {
Vishnu Naira4fbca52021-07-07 16:52:34 -0700602 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d "
Vishnu Nairea0de002020-11-17 17:42:37 -0800603 "buffer{size=%dx%d transform=%d}",
604 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
605 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
606 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000607 return acquireNextBufferLocked(transaction);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700608 }
609
Valerie Haua32c5522019-12-09 10:11:08 -0800610 mNumAcquired++;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700611 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
612 ReleaseCallbackId releaseCallbackId(buffer->getId(), mLastAcquiredFrameNumber);
613 mSubmitted[releaseCallbackId] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700614
Valerie Hau871d6352020-01-29 08:44:02 -0800615 bool needsDisconnect = false;
616 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
617
618 // if producer disconnected before, notify SurfaceFlinger
619 if (needsDisconnect) {
620 t->notifyProducerDisconnect(mSurfaceControl);
621 }
622
Chavi Weingarten70670e62023-02-22 17:36:40 +0000623 // Only update mSize for destination bounds if the incoming buffer matches the requested size.
624 // Otherwise, it could cause stretching since the destination bounds will update before the
625 // buffer with the new size is acquired.
Vishnu Nair5b5f6932023-04-12 16:28:19 -0700626 if (mRequestedSize == getBufferSize(bufferItem) ||
627 bufferItem.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Chavi Weingarten70670e62023-02-22 17:36:40 +0000628 mSize = mRequestedSize;
629 }
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700630 Rect crop = computeCrop(bufferItem);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000631 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
632 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700633 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800634
Patrick Williams7c9fa272024-08-30 12:38:43 +0000635#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
636 ReleaseBufferCallback releaseBufferCallback =
637 applyTransaction ? EMPTY_RELEASE_CALLBACK : makeReleaseBufferCallbackThunk();
638#else
Patrick Williams5312ec12024-08-23 16:11:10 -0500639 auto releaseBufferCallback = makeReleaseBufferCallbackThunk();
Patrick Williams7c9fa272024-08-30 12:38:43 +0000640#endif
chaviwba4320c2021-09-15 15:20:53 -0500641 sp<Fence> fence = bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE;
Nergi Rahardi39f510f2024-05-23 15:16:54 +0900642
643 nsecs_t dequeueTime = -1;
644 {
645 std::lock_guard _lock{mTimestampMutex};
646 auto dequeueTimeIt = mDequeueTimestamps.find(buffer->getId());
647 if (dequeueTimeIt != mDequeueTimestamps.end()) {
648 dequeueTime = dequeueTimeIt->second;
649 mDequeueTimestamps.erase(dequeueTimeIt);
650 }
651 }
652
liulijuneb489f62022-10-17 22:02:14 +0800653 t->setBuffer(mSurfaceControl, buffer, fence, bufferItem.mFrameNumber, mProducerId,
Nergi Rahardi39f510f2024-05-23 15:16:54 +0900654 releaseBufferCallback, dequeueTime);
John Reck137069e2020-12-10 22:07:37 -0500655 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
656 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
657 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Patrick Williams5312ec12024-08-23 16:11:10 -0500658 t->addTransactionCompletedCallback(makeTransactionCallbackThunk(), nullptr);
chaviwf2dace72021-11-17 17:36:50 -0600659
chaviw42026162021-04-16 15:46:12 -0500660 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700661
Vishnu Naird2aaab12022-02-10 14:49:09 -0800662 if (mUpdateDestinationFrame) {
663 t->setDestinationFrame(mSurfaceControl, Rect(mSize));
664 } else {
665 const bool ignoreDestinationFrame =
666 bufferItem.mScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE;
667 t->setFlags(mSurfaceControl,
668 ignoreDestinationFrame ? layer_state_t::eIgnoreDestinationFrame : 0,
669 layer_state_t::eIgnoreDestinationFrame);
Vishnu Nair084514a2021-07-30 16:07:42 -0700670 }
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700671 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800672 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800673 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Vishnu Naird2aaab12022-02-10 14:49:09 -0800674 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800675 if (!bufferItem.mIsAutoTimestamp) {
676 t->setDesiredPresentTime(bufferItem.mTimestamp);
677 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700678
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800679 // Drop stale frame timeline infos
680 while (!mPendingFrameTimelines.empty() &&
681 mPendingFrameTimelines.front().first < bufferItem.mFrameNumber) {
682 ATRACE_FORMAT_INSTANT("dropping stale frameNumber: %" PRIu64 " vsyncId: %" PRId64,
683 mPendingFrameTimelines.front().first,
684 mPendingFrameTimelines.front().second.vsyncId);
685 mPendingFrameTimelines.pop();
686 }
687
688 if (!mPendingFrameTimelines.empty() &&
689 mPendingFrameTimelines.front().first == bufferItem.mFrameNumber) {
690 ATRACE_FORMAT_INSTANT("Transaction::setFrameTimelineInfo frameNumber: %" PRIu64
691 " vsyncId: %" PRId64,
692 bufferItem.mFrameNumber,
693 mPendingFrameTimelines.front().second.vsyncId);
694 t->setFrameTimelineInfo(mPendingFrameTimelines.front().second);
695 mPendingFrameTimelines.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100696 }
697
chaviw6a195272021-09-03 16:14:25 -0500698 mergePendingTransactions(t, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700699 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800700 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
701 t->setApplyToken(mApplyToken).apply(false, true);
702 mAppliedLastTransaction = true;
703 mLastAppliedFrameNumber = bufferItem.mFrameNumber;
704 } else {
705 t->setBufferHasBarrier(mSurfaceControl, mLastAppliedFrameNumber);
706 mAppliedLastTransaction = false;
Robert Carr78c25dd2019-08-15 14:10:33 -0700707 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700708
chaviwd7deef72021-10-06 11:53:40 -0500709 BQA_LOGV("acquireNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800710 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
Vishnu Naira4fbca52021-07-07 16:52:34 -0700711 " graphicBufferId=%" PRIu64 "%s transform=%d",
chaviw3277faf2021-05-19 16:45:23 -0500712 mSize.width, mSize.height, bufferItem.mFrameNumber, boolToString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800713 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700714 static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(),
Vishnu Naira4fbca52021-07-07 16:52:34 -0700715 bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000716 return OK;
Robert Carr78c25dd2019-08-15 14:10:33 -0700717}
718
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800719Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
720 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800721 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800722 }
723 return item.mCrop;
724}
725
chaviwd7deef72021-10-06 11:53:40 -0500726void BLASTBufferQueue::acquireAndReleaseBuffer() {
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000727 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500728 BufferItem bufferItem;
chaviw6ebdf5f2021-10-14 11:57:22 -0500729 status_t status =
730 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
731 if (status != OK) {
732 BQA_LOGE("Failed to acquire a buffer in acquireAndReleaseBuffer, err=%s",
733 statusToString(status).c_str());
734 return;
735 }
chaviwd7deef72021-10-06 11:53:40 -0500736 mNumFrameAvailable--;
chaviw6ebdf5f2021-10-14 11:57:22 -0500737 mBufferItemConsumer->releaseBuffer(bufferItem, bufferItem.mFence);
chaviwd7deef72021-10-06 11:53:40 -0500738}
739
Vishnu Nairaef1de92020-10-22 12:15:53 -0700740void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000741 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
742 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
chaviwc1cf4022022-06-03 13:32:33 -0500743
Tianhao Yao4861b102022-02-03 20:18:35 +0000744 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000745 UNIQUE_LOCK_WITH_ASSERTION(mMutex);
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000746 BBQ_TRACE();
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000747 bool waitForTransactionCallback = !mSyncedFrameNumbers.empty();
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800748
Tianhao Yao4861b102022-02-03 20:18:35 +0000749 const bool syncTransactionSet = mTransactionReadyCallback != nullptr;
750 BQA_LOGV("onFrameAvailable-start syncTransactionSet=%s", boolToString(syncTransactionSet));
Valerie Haud3b90d22019-11-06 09:37:31 -0800751
Tianhao Yao4861b102022-02-03 20:18:35 +0000752 if (syncTransactionSet) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000753 // If we are going to re-use the same mSyncTransaction, release the buffer that may
754 // already be set in the Transaction. This is to allow us a free slot early to continue
755 // processing a new buffer.
756 if (!mAcquireSingleBuffer) {
757 auto bufferData = mSyncTransaction->getAndClearBuffer(mSurfaceControl);
758 if (bufferData) {
759 BQA_LOGD("Releasing previous buffer when syncing: framenumber=%" PRIu64,
760 bufferData->frameNumber);
761 releaseBuffer(bufferData->generateReleaseCallbackId(),
762 bufferData->acquireFence);
Tianhao Yao4861b102022-02-03 20:18:35 +0000763 }
764 }
chaviw0acd33a2021-11-02 11:55:37 -0500765
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000766 if (waitForTransactionCallback) {
767 // We are waiting on a previous sync's transaction callback so allow another sync
768 // transaction to proceed.
769 //
770 // We need to first flush out the transactions that were in between the two syncs.
771 // We do this by merging them into mSyncTransaction so any buffer merging will get
772 // a release callback invoked.
773 while (mNumFrameAvailable > 0) {
774 // flush out the shadow queue
775 acquireAndReleaseBuffer();
776 }
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800777 } else {
778 // Make sure the frame available count is 0 before proceeding with a sync to ensure
779 // the correct frame is used for the sync. The only way mNumFrameAvailable would be
780 // greater than 0 is if we already ran out of buffers previously. This means we
781 // need to flush the buffers before proceeding with the sync.
782 while (mNumFrameAvailable > 0) {
783 BQA_LOGD("waiting until no queued buffers");
784 mCallbackCV.wait(_lock);
785 }
chaviwd7deef72021-10-06 11:53:40 -0500786 }
787 }
788
Tianhao Yao4861b102022-02-03 20:18:35 +0000789 // add to shadow queue
790 mNumFrameAvailable++;
chaviwc1cf4022022-06-03 13:32:33 -0500791 if (waitForTransactionCallback && mNumFrameAvailable >= 2) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000792 acquireAndReleaseBuffer();
793 }
794 ATRACE_INT(mQueuedBufferTrace.c_str(),
795 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
796
797 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " syncTransactionSet=%s",
798 item.mFrameNumber, boolToString(syncTransactionSet));
799
800 if (syncTransactionSet) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800801 // Add to mSyncedFrameNumbers before waiting in case any buffers are released
802 // while waiting for a free buffer. The release and commit callback will try to
803 // acquire buffers if there are any available, but we don't want it to acquire
804 // in the case where a sync transaction wants the buffer.
805 mSyncedFrameNumbers.emplace(item.mFrameNumber);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000806 // If there's no available buffer and we're in a sync transaction, we need to wait
807 // instead of returning since we guarantee a buffer will be acquired for the sync.
808 while (acquireNextBufferLocked(mSyncTransaction) == BufferQueue::NO_BUFFER_AVAILABLE) {
809 BQA_LOGD("waiting for available buffer");
810 mCallbackCV.wait(_lock);
811 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000812
813 // Only need a commit callback when syncing to ensure the buffer that's synced has been
814 // sent to SF
Patrick Williams5312ec12024-08-23 16:11:10 -0500815 mSyncTransaction
816 ->addTransactionCommittedCallback(makeTransactionCommittedCallbackThunk(),
817 nullptr);
Tianhao Yao4861b102022-02-03 20:18:35 +0000818 if (mAcquireSingleBuffer) {
819 prevCallback = mTransactionReadyCallback;
820 prevTransaction = mSyncTransaction;
821 mTransactionReadyCallback = nullptr;
822 mSyncTransaction = nullptr;
823 }
chaviwc1cf4022022-06-03 13:32:33 -0500824 } else if (!waitForTransactionCallback) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000825 acquireNextBufferLocked(std::nullopt);
Valerie Hau0188adf2020-02-13 08:29:20 -0800826 }
827 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000828 if (prevCallback) {
829 prevCallback(prevTransaction);
chaviwd7deef72021-10-06 11:53:40 -0500830 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800831}
832
Vishnu Nairaef1de92020-10-22 12:15:53 -0700833void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
834 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
835 // Do nothing since we are not storing unacquired buffer items locally.
836}
837
Vishnu Nairadf632b2021-01-07 14:05:08 -0800838void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000839 std::lock_guard _lock{mTimestampMutex};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800840 mDequeueTimestamps[bufferId] = systemTime();
Patrick Williams4b9507d2024-07-25 09:55:52 -0500841};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800842
843void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
Patrick Williams3ced5382024-08-21 15:39:32 -0500844 std::lock_guard _lock{mTimestampMutex};
845 mDequeueTimestamps.erase(bufferId);
Patrick Williamsf5b42de2024-08-01 16:08:51 -0500846}
Vishnu Nairadf632b2021-01-07 14:05:08 -0800847
Chavi Weingartenc398c012023-04-12 17:26:02 +0000848bool BLASTBufferQueue::syncNextTransaction(
Tianhao Yao4861b102022-02-03 20:18:35 +0000849 std::function<void(SurfaceComposerClient::Transaction*)> callback,
850 bool acquireSingleBuffer) {
Chavi Weingartenc398c012023-04-12 17:26:02 +0000851 LOG_ALWAYS_FATAL_IF(!callback,
852 "BLASTBufferQueue: callback passed in to syncNextTransaction must not be "
853 "NULL");
chaviw3b4bdcf2022-03-17 09:27:03 -0500854
Chavi Weingartenc398c012023-04-12 17:26:02 +0000855 std::lock_guard _lock{mMutex};
856 BBQ_TRACE();
857 if (mTransactionReadyCallback) {
858 ALOGW("Attempting to overwrite transaction callback in syncNextTransaction");
859 return false;
Tianhao Yao4861b102022-02-03 20:18:35 +0000860 }
chaviw3b4bdcf2022-03-17 09:27:03 -0500861
Chavi Weingartenc398c012023-04-12 17:26:02 +0000862 mTransactionReadyCallback = callback;
863 mSyncTransaction = new SurfaceComposerClient::Transaction();
864 mAcquireSingleBuffer = acquireSingleBuffer;
865 return true;
Tianhao Yao4861b102022-02-03 20:18:35 +0000866}
867
868void BLASTBufferQueue::stopContinuousSyncTransaction() {
869 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
870 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
871 {
872 std::lock_guard _lock{mMutex};
Chavi Weingartenc398c012023-04-12 17:26:02 +0000873 if (mAcquireSingleBuffer || !mTransactionReadyCallback) {
874 ALOGW("Attempting to stop continuous sync when none are active");
875 return;
Tianhao Yao4861b102022-02-03 20:18:35 +0000876 }
Chavi Weingartenc398c012023-04-12 17:26:02 +0000877
878 prevCallback = mTransactionReadyCallback;
879 prevTransaction = mSyncTransaction;
880
Tianhao Yao4861b102022-02-03 20:18:35 +0000881 mTransactionReadyCallback = nullptr;
882 mSyncTransaction = nullptr;
883 mAcquireSingleBuffer = true;
884 }
Chavi Weingartenc398c012023-04-12 17:26:02 +0000885
Tianhao Yao4861b102022-02-03 20:18:35 +0000886 if (prevCallback) {
887 prevCallback(prevTransaction);
888 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700889}
890
Chavi Weingartenc398c012023-04-12 17:26:02 +0000891void BLASTBufferQueue::clearSyncTransaction() {
892 std::lock_guard _lock{mMutex};
893 if (!mAcquireSingleBuffer) {
894 ALOGW("Attempting to clear sync transaction when none are active");
895 return;
896 }
897
898 mTransactionReadyCallback = nullptr;
899 mSyncTransaction = nullptr;
900}
901
Vishnu Nairea0de002020-11-17 17:42:37 -0800902bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700903 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
904 // Only reject buffers if scaling mode is freeze.
905 return false;
906 }
907
Chavi Weingarten70670e62023-02-22 17:36:40 +0000908 ui::Size bufferSize = getBufferSize(item);
Vishnu Nairea0de002020-11-17 17:42:37 -0800909 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800910 return false;
911 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700912
Vishnu Nair670b3f72020-09-29 17:52:18 -0700913 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800914 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700915}
Vishnu Nairbf255772020-10-16 10:54:41 -0700916
Robert Carr05086b22020-10-13 18:22:51 -0700917class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700918private:
Vishnu Nair95b6d512021-08-30 15:31:08 -0700919 std::mutex mMutex;
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000920 sp<BLASTBufferQueue> mBbq GUARDED_BY(mMutex);
921 bool mDestroyed GUARDED_BY(mMutex) = false;
Vishnu Nair95b6d512021-08-30 15:31:08 -0700922
Robert Carr05086b22020-10-13 18:22:51 -0700923public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700924 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
925 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
926 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700927
Robert Carr05086b22020-10-13 18:22:51 -0700928 void allocateBuffers() override {
929 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
930 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
931 auto gbp = getIGraphicBufferProducer();
932 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
933 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
934 gbp->allocateBuffers(reqWidth, reqHeight,
935 reqFormat, reqUsage);
936
937 }).detach();
938 }
Robert Carr9c006e02020-10-14 13:41:57 -0700939
Marin Shalamanovc5986772021-03-16 16:09:49 +0100940 status_t setFrameRate(float frameRate, int8_t compatibility,
941 int8_t changeFrameRateStrategy) override {
Ady Abraham6cdd3fd2023-09-07 18:45:58 -0700942 if (flags::bq_setframerate()) {
943 return Surface::setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
944 }
945
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000946 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700947 if (mDestroyed) {
948 return DEAD_OBJECT;
949 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100950 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
951 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700952 return BAD_VALUE;
953 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100954 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700955 }
Robert Carr9b611b72020-10-19 12:00:23 -0700956
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800957 status_t setFrameTimelineInfo(uint64_t frameNumber,
958 const FrameTimelineInfo& frameTimelineInfo) override {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000959 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700960 if (mDestroyed) {
961 return DEAD_OBJECT;
962 }
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800963 return mBbq->setFrameTimelineInfo(frameNumber, frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700964 }
Vishnu Nair95b6d512021-08-30 15:31:08 -0700965
966 void destroy() override {
967 Surface::destroy();
968
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000969 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700970 mDestroyed = true;
971 mBbq = nullptr;
972 }
Robert Carr05086b22020-10-13 18:22:51 -0700973};
974
Robert Carr9c006e02020-10-14 13:41:57 -0700975// TODO: Can we coalesce this with frame updates? Need to confirm
976// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200977status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
978 bool shouldBeSeamless) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000979 std::lock_guard _lock{mMutex};
Robert Carr9c006e02020-10-14 13:41:57 -0700980 SurfaceComposerClient::Transaction t;
981
Marin Shalamanov46084422020-10-13 12:33:42 +0200982 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700983}
984
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800985status_t BLASTBufferQueue::setFrameTimelineInfo(uint64_t frameNumber,
986 const FrameTimelineInfo& frameTimelineInfo) {
987 ATRACE_FORMAT("%s(%s) frameNumber: %" PRIu64 " vsyncId: %" PRId64, __func__, mName.c_str(),
988 frameNumber, frameTimelineInfo.vsyncId);
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000989 std::lock_guard _lock{mMutex};
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800990 mPendingFrameTimelines.push({frameNumber, frameTimelineInfo});
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100991 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700992}
993
Hongguang Chen621ec582021-02-16 15:42:35 -0800994void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000995 std::lock_guard _lock{mMutex};
Hongguang Chen621ec582021-02-16 15:42:35 -0800996 SurfaceComposerClient::Transaction t;
997
998 t.setSidebandStream(mSurfaceControl, stream).apply();
999}
1000
Vishnu Nair992496b2020-10-22 17:27:21 -07001001sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001002 std::lock_guard _lock{mMutex};
Vishnu Nair992496b2020-10-22 17:27:21 -07001003 sp<IBinder> scHandle = nullptr;
1004 if (includeSurfaceControlHandle && mSurfaceControl) {
1005 scHandle = mSurfaceControl->getHandle();
1006 }
1007 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -07001008}
1009
Vishnu Nairc4a40c12020-12-23 09:14:32 -08001010void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
1011 uint64_t frameNumber) {
1012 std::lock_guard _lock{mMutex};
1013 if (mLastAcquiredFrameNumber >= frameNumber) {
1014 // Apply the transaction since we have already acquired the desired frame.
1015 t->apply();
1016 } else {
chaviwaad6cf52021-03-23 17:27:20 -05001017 mPendingTransactions.emplace_back(frameNumber, *t);
1018 // Clear the transaction so it can't be applied elsewhere.
1019 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -08001020 }
1021}
1022
chaviw6a195272021-09-03 16:14:25 -05001023void BLASTBufferQueue::applyPendingTransactions(uint64_t frameNumber) {
1024 std::lock_guard _lock{mMutex};
1025
1026 SurfaceComposerClient::Transaction t;
1027 mergePendingTransactions(&t, frameNumber);
Robert Carr79dc06a2022-02-22 15:28:59 -08001028 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
1029 t.setApplyToken(mApplyToken).apply(false, true);
chaviw6a195272021-09-03 16:14:25 -05001030}
1031
1032void BLASTBufferQueue::mergePendingTransactions(SurfaceComposerClient::Transaction* t,
1033 uint64_t frameNumber) {
1034 auto mergeTransaction =
1035 [&t, currentFrameNumber = frameNumber](
1036 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
1037 auto& [targetFrameNumber, transaction] = pendingTransaction;
1038 if (currentFrameNumber < targetFrameNumber) {
1039 return false;
1040 }
1041 t->merge(std::move(transaction));
1042 return true;
1043 };
1044
1045 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
1046 mPendingTransactions.end(), mergeTransaction),
1047 mPendingTransactions.end());
1048}
1049
chaviwd84085a2022-02-08 11:07:04 -06001050SurfaceComposerClient::Transaction* BLASTBufferQueue::gatherPendingTransactions(
1051 uint64_t frameNumber) {
1052 std::lock_guard _lock{mMutex};
1053 SurfaceComposerClient::Transaction* t = new SurfaceComposerClient::Transaction();
1054 mergePendingTransactions(t, frameNumber);
1055 return t;
1056}
1057
Vishnu Nair89496122020-12-14 17:14:53 -08001058// Maintains a single worker thread per process that services a list of runnables.
1059class AsyncWorker : public Singleton<AsyncWorker> {
1060private:
1061 std::thread mThread;
1062 bool mDone = false;
1063 std::deque<std::function<void()>> mRunnables;
1064 std::mutex mMutex;
1065 std::condition_variable mCv;
1066 void run() {
1067 std::unique_lock<std::mutex> lock(mMutex);
1068 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -08001069 while (!mRunnables.empty()) {
Vishnu Nair51e4dc82021-10-01 15:32:33 -07001070 std::deque<std::function<void()>> runnables = std::move(mRunnables);
1071 mRunnables.clear();
1072 lock.unlock();
1073 // Run outside the lock since the runnable might trigger another
1074 // post to the async worker.
1075 execute(runnables);
1076 lock.lock();
Vishnu Nair89496122020-12-14 17:14:53 -08001077 }
Wonsik Kim567533e2021-05-04 19:31:29 -07001078 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -08001079 }
1080 }
1081
Vishnu Nair51e4dc82021-10-01 15:32:33 -07001082 void execute(std::deque<std::function<void()>>& runnables) {
1083 while (!runnables.empty()) {
1084 std::function<void()> runnable = runnables.front();
1085 runnables.pop_front();
1086 runnable();
1087 }
1088 }
1089
Vishnu Nair89496122020-12-14 17:14:53 -08001090public:
1091 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
1092
1093 ~AsyncWorker() {
1094 mDone = true;
1095 mCv.notify_all();
1096 if (mThread.joinable()) {
1097 mThread.join();
1098 }
1099 }
1100
1101 void post(std::function<void()> runnable) {
1102 std::unique_lock<std::mutex> lock(mMutex);
1103 mRunnables.emplace_back(std::move(runnable));
1104 mCv.notify_one();
1105 }
1106};
1107ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
1108
1109// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
1110class AsyncProducerListener : public BnProducerListener {
1111private:
1112 const sp<IProducerListener> mListener;
1113
1114public:
1115 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
1116
1117 void onBufferReleased() override {
1118 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
1119 }
1120
1121 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
1122 AsyncWorker::getInstance().post(
1123 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
1124 }
Sungtak Lee7c935092024-09-16 16:55:04 +00001125
1126 void onBufferDetached(int slot) override {
1127 AsyncWorker::getInstance().post(
1128 [listener = mListener, slot = slot]() { listener->onBufferDetached(slot); });
1129 }
1130
1131#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK)
1132 void onBufferAttached() override {
1133 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferAttached(); });
1134 }
1135#endif
Vishnu Nair89496122020-12-14 17:14:53 -08001136};
1137
1138// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
1139// can be non-blocking when the producer is in the client process.
1140class BBQBufferQueueProducer : public BufferQueueProducer {
1141public:
Patrick Williamsca81c052024-08-15 12:38:34 -05001142 BBQBufferQueueProducer(const sp<BufferQueueCore>& core, const wp<BLASTBufferQueue>& bbq)
Brian Lindahlc794b692023-01-31 15:42:47 -07001143 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/),
Patrick Williamsca81c052024-08-15 12:38:34 -05001144 mBLASTBufferQueue(bbq) {}
Vishnu Nair89496122020-12-14 17:14:53 -08001145
1146 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
1147 QueueBufferOutput* output) override {
1148 if (!listener) {
1149 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
1150 }
1151
1152 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
1153 producerControlledByApp, output);
1154 }
Vishnu Nair17dde612020-12-28 11:39:59 -08001155
Brian Lindahlc794b692023-01-31 15:42:47 -07001156 // We want to resize the frame history when changing the size of the buffer queue
1157 status_t setMaxDequeuedBufferCount(int maxDequeuedBufferCount) override {
1158 int maxBufferCount;
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001159 if (status_t status = BufferQueueProducer::setMaxDequeuedBufferCount(maxDequeuedBufferCount,
1160 &maxBufferCount);
1161 status != OK) {
1162 return status;
Brian Lindahlc794b692023-01-31 15:42:47 -07001163 }
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001164
1165 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1166 if (!bbq) {
1167 return OK;
1168 }
1169
1170 // if we can't determine the max buffer count, then just skip growing the history size
1171 size_t newFrameHistorySize = maxBufferCount + 2; // +2 because triple buffer rendering
1172 // optimize away resizing the frame history unless it will grow
1173 if (newFrameHistorySize > FrameEventHistory::INITIAL_MAX_FRAME_HISTORY) {
1174 ALOGV("increasing frame history size to %zu", newFrameHistorySize);
1175 bbq->resizeFrameEventHistory(newFrameHistorySize);
1176 }
1177
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001178 return OK;
Brian Lindahlc794b692023-01-31 15:42:47 -07001179 }
1180
Vishnu Nair17dde612020-12-28 11:39:59 -08001181 int query(int what, int* value) override {
1182 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
1183 *value = 1;
Patrick Williamsf5b42de2024-08-01 16:08:51 -05001184 return OK;
Vishnu Nair17dde612020-12-28 11:39:59 -08001185 }
1186 return BufferQueueProducer::query(what, value);
1187 }
Brian Lindahlc794b692023-01-31 15:42:47 -07001188
1189private:
1190 const wp<BLASTBufferQueue> mBLASTBufferQueue;
Vishnu Nair89496122020-12-14 17:14:53 -08001191};
1192
1193// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
1194// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
1195// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
1196// we can deadlock.
1197void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
1198 sp<IGraphicBufferConsumer>* outConsumer) {
1199 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
1200 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
1201
1202 sp<BufferQueueCore> core(new BufferQueueCore());
1203 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
1204
Brian Lindahlc794b692023-01-31 15:42:47 -07001205 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core, this));
Vishnu Nair89496122020-12-14 17:14:53 -08001206 LOG_ALWAYS_FATAL_IF(producer == nullptr,
1207 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
1208
Vishnu Nair8b30dd12021-01-25 14:16:54 -08001209 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
1210 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -08001211 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
1212 "BLASTBufferQueue: failed to create BufferQueueConsumer");
1213
1214 *outProducer = producer;
1215 *outConsumer = consumer;
1216}
1217
Brian Lindahlc794b692023-01-31 15:42:47 -07001218void BLASTBufferQueue::resizeFrameEventHistory(size_t newSize) {
1219 // This can be null during creation of the buffer queue, but resizing won't do anything at that
1220 // point in time, so just ignore. This can go away once the class relationships and lifetimes of
1221 // objects are cleaned up with a major refactor of BufferQueue as a whole.
1222 if (mBufferItemConsumer != nullptr) {
1223 std::unique_lock _lock{mMutex};
1224 mBufferItemConsumer->resizeFrameEventHistory(newSize);
1225 }
1226}
1227
chaviw497e81c2021-02-04 17:09:47 -08001228PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
1229 PixelFormat convertedFormat = format;
1230 switch (format) {
1231 case PIXEL_FORMAT_TRANSPARENT:
1232 case PIXEL_FORMAT_TRANSLUCENT:
1233 convertedFormat = PIXEL_FORMAT_RGBA_8888;
1234 break;
1235 case PIXEL_FORMAT_OPAQUE:
1236 convertedFormat = PIXEL_FORMAT_RGBX_8888;
1237 break;
1238 }
1239 return convertedFormat;
1240}
1241
Robert Carr82d07c92021-05-10 11:36:43 -07001242uint32_t BLASTBufferQueue::getLastTransformHint() const {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001243 std::lock_guard _lock{mMutex};
Robert Carr82d07c92021-05-10 11:36:43 -07001244 if (mSurfaceControl != nullptr) {
1245 return mSurfaceControl->getTransformHint();
1246 } else {
1247 return 0;
1248 }
1249}
1250
chaviw0b020f82021-08-20 12:00:47 -05001251uint64_t BLASTBufferQueue::getLastAcquiredFrameNum() {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001252 std::lock_guard _lock{mMutex};
chaviw0b020f82021-08-20 12:00:47 -05001253 return mLastAcquiredFrameNumber;
1254}
1255
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001256bool BLASTBufferQueue::isSameSurfaceControl(const sp<SurfaceControl>& surfaceControl) const {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001257 std::lock_guard _lock{mMutex};
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001258 return SurfaceControl::isSameSurface(mSurfaceControl, surfaceControl);
1259}
1260
Patrick Williamsf1e5df12022-10-17 21:37:42 +00001261void BLASTBufferQueue::setTransactionHangCallback(
1262 std::function<void(const std::string&)> callback) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001263 std::lock_guard _lock{mMutex};
Patrick Williams7c9fa272024-08-30 12:38:43 +00001264 mTransactionHangCallback = std::move(callback);
Robert Carr4c1b6462021-12-21 10:30:50 -08001265}
1266
Vishnu Nairaf15fab2024-07-30 08:59:26 -07001267void BLASTBufferQueue::setApplyToken(sp<IBinder> applyToken) {
1268 std::lock_guard _lock{mMutex};
1269 mApplyToken = std::move(applyToken);
1270}
1271
Patrick Williams7c9fa272024-08-30 12:38:43 +00001272#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
1273
1274BLASTBufferQueue::BufferReleaseReader::BufferReleaseReader(
1275 std::unique_ptr<gui::BufferReleaseChannel::ConsumerEndpoint> endpoint)
1276 : mEndpoint{std::move(endpoint)} {
1277 mEpollFd = android::base::unique_fd{epoll_create1(0)};
1278 LOG_ALWAYS_FATAL_IF(!mEpollFd.ok(),
1279 "Failed to create buffer release epoll file descriptor. errno=%d "
1280 "message='%s'",
1281 errno, strerror(errno));
1282
1283 epoll_event registerEndpointFd{};
1284 registerEndpointFd.events = EPOLLIN;
1285 registerEndpointFd.data.fd = mEndpoint->getFd();
1286 status_t status =
1287 epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, mEndpoint->getFd(), &registerEndpointFd);
1288 LOG_ALWAYS_FATAL_IF(status == -1,
1289 "Failed to register buffer release consumer file descriptor with epoll. "
1290 "errno=%d message='%s'",
1291 errno, strerror(errno));
1292
1293 mEventFd = android::base::unique_fd(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
1294 LOG_ALWAYS_FATAL_IF(!mEventFd.ok(),
1295 "Failed to create buffer release event file descriptor. errno=%d "
1296 "message='%s'",
1297 errno, strerror(errno));
1298
1299 epoll_event registerEventFd{};
1300 registerEventFd.events = EPOLLIN;
1301 registerEventFd.data.fd = mEventFd.get();
1302 status = epoll_ctl(mEpollFd.get(), EPOLL_CTL_ADD, mEventFd.get(), &registerEventFd);
1303 LOG_ALWAYS_FATAL_IF(status == -1,
1304 "Failed to register buffer release event file descriptor with epoll. "
1305 "errno=%d message='%s'",
1306 errno, strerror(errno));
1307}
1308
1309BLASTBufferQueue::BufferReleaseReader& BLASTBufferQueue::BufferReleaseReader::operator=(
1310 BufferReleaseReader&& other) {
1311 if (this != &other) {
1312 ftl::FakeGuard guard{mMutex};
1313 ftl::FakeGuard otherGuard{other.mMutex};
1314 mEndpoint = std::move(other.mEndpoint);
1315 mEpollFd = std::move(other.mEpollFd);
1316 mEventFd = std::move(other.mEventFd);
1317 }
1318 return *this;
1319}
1320
1321status_t BLASTBufferQueue::BufferReleaseReader::readBlocking(ReleaseCallbackId& outId,
1322 sp<Fence>& outFence,
1323 uint32_t& outMaxAcquiredBufferCount) {
1324 epoll_event event{};
1325 while (true) {
1326 int eventCount = epoll_wait(mEpollFd.get(), &event, 1 /* maxevents */, -1 /* timeout */);
1327 if (eventCount == 1) {
1328 break;
1329 }
1330 if (eventCount == -1 && errno != EINTR) {
1331 ALOGE("epoll_wait error while waiting for buffer release. errno=%d message='%s'", errno,
1332 strerror(errno));
1333 }
1334 }
1335
1336 if (event.data.fd == mEventFd.get()) {
1337 uint64_t value;
1338 if (read(mEventFd.get(), &value, sizeof(uint64_t)) == -1 && errno != EWOULDBLOCK) {
1339 ALOGE("error while reading from eventfd. errno=%d message='%s'", errno,
1340 strerror(errno));
1341 }
1342 return WOULD_BLOCK;
1343 }
1344
1345 std::lock_guard lock{mMutex};
1346 return mEndpoint->readReleaseFence(outId, outFence, outMaxAcquiredBufferCount);
1347}
1348
1349void BLASTBufferQueue::BufferReleaseReader::interruptBlockingRead() {
1350 uint64_t value = 1;
1351 if (write(mEventFd.get(), &value, sizeof(uint64_t)) == -1) {
1352 ALOGE("failed to notify dequeue event. errno=%d message='%s'", errno, strerror(errno));
1353 }
1354}
1355
1356void BLASTBufferQueue::BufferReleaseThread::start(const sp<BLASTBufferQueue>& bbq) {
1357 mRunning = std::make_shared<std::atomic_bool>(true);
1358 mReader = bbq->mBufferReleaseReader;
1359 std::thread([running = mRunning, reader = mReader, weakBbq = wp<BLASTBufferQueue>(bbq)]() {
1360 pthread_setname_np(pthread_self(), "BufferReleaseThread");
1361 while (*running) {
1362 ReleaseCallbackId id;
1363 sp<Fence> fence;
1364 uint32_t maxAcquiredBufferCount;
1365 if (status_t status = reader->readBlocking(id, fence, maxAcquiredBufferCount);
1366 status != OK) {
1367 continue;
1368 }
1369 sp<BLASTBufferQueue> bbq = weakBbq.promote();
1370 if (!bbq) {
1371 return;
1372 }
1373 bbq->releaseBufferCallback(id, fence, maxAcquiredBufferCount);
1374 }
1375 }).detach();
1376}
1377
1378BLASTBufferQueue::BufferReleaseThread::~BufferReleaseThread() {
1379 *mRunning = false;
1380 mReader->interruptBlockingRead();
1381}
1382
1383#endif
1384
Robert Carr78c25dd2019-08-15 14:10:33 -07001385} // namespace android