blob: 8d0331ebb56b343d3a01c0814d3f79f2feefbd83 [file] [log] [blame]
Robert Carr78c25dd2019-08-15 14:10:33 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Valerie Haud3b90d22019-11-06 09:37:31 -080017#undef LOG_TAG
18#define LOG_TAG "BLASTBufferQueue"
19
Valerie Haua32c5522019-12-09 10:11:08 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Vishnu Naire1a42322020-10-02 17:42:04 -070021//#define LOG_NDEBUG 0
Valerie Haua32c5522019-12-09 10:11:08 -080022
liulijuneb489f62022-10-17 22:02:14 +080023#include <cutils/atomic.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070024#include <gui/BLASTBufferQueue.h>
25#include <gui/BufferItemConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080026#include <gui/BufferQueueConsumer.h>
27#include <gui/BufferQueueCore.h>
28#include <gui/BufferQueueProducer.h>
Ady Abraham107788e2023-10-17 12:31:08 -070029
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070030#include <gui/FrameRateUtils.h>
Valerie Hau45e4b3b2019-12-03 10:49:17 -080031#include <gui/GLConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080032#include <gui/IProducerListener.h>
Robert Carr05086b22020-10-13 18:22:51 -070033#include <gui/Surface.h>
chaviw57ae4b22022-02-03 16:51:39 -060034#include <gui/TraceUtils.h>
Vishnu Nair89496122020-12-14 17:14:53 -080035#include <utils/Singleton.h>
Valerie Haua32c5522019-12-09 10:11:08 -080036#include <utils/Trace.h>
37
Ady Abraham0bde6b52021-05-18 13:57:02 -070038#include <private/gui/ComposerService.h>
Huihong Luo02186fb2022-02-23 14:21:54 -080039#include <private/gui/ComposerServiceAIDL.h>
Ady Abraham0bde6b52021-05-18 13:57:02 -070040
Chavi Weingartene0237bb2023-02-06 21:48:32 +000041#include <android-base/thread_annotations.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070042#include <chrono>
43
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070044using namespace com::android::graphics::libgui;
Robert Carr78c25dd2019-08-15 14:10:33 -070045using namespace std::chrono_literals;
46
Vishnu Nairdab94092020-09-29 16:09:04 -070047namespace {
chaviw3277faf2021-05-19 16:45:23 -050048inline const char* boolToString(bool b) {
Vishnu Nairdab94092020-09-29 16:09:04 -070049 return b ? "true" : "false";
50}
51} // namespace
52
Robert Carr78c25dd2019-08-15 14:10:33 -070053namespace android {
54
Vishnu Nairdab94092020-09-29 16:09:04 -070055// Macros to include adapter info in log messages
chaviwd7deef72021-10-06 11:53:40 -050056#define BQA_LOGD(x, ...) \
57 ALOGD("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070058#define BQA_LOGV(x, ...) \
59 ALOGV("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairc6f89ee2020-12-11 14:27:32 -080060// enable logs for a single layer
61//#define BQA_LOGV(x, ...) \
62// ALOGV_IF((strstr(mName.c_str(), "SurfaceView") != nullptr), "[%s](f:%u,a:%u) " x, \
63// mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070064#define BQA_LOGE(x, ...) \
65 ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
66
chaviw57ae4b22022-02-03 16:51:39 -060067#define BBQ_TRACE(x, ...) \
68 ATRACE_FORMAT("%s - %s(f:%u,a:%u)" x, __FUNCTION__, mName.c_str(), mNumFrameAvailable, \
69 mNumAcquired, ##__VA_ARGS__)
70
Chavi Weingartene0237bb2023-02-06 21:48:32 +000071#define UNIQUE_LOCK_WITH_ASSERTION(mutex) \
72 std::unique_lock _lock{mutex}; \
73 base::ScopedLockAssertion assumeLocked(mutex);
74
Valerie Hau871d6352020-01-29 08:44:02 -080075void BLASTBufferItemConsumer::onDisconnect() {
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000076 Mutex::Autolock lock(mMutex);
77 mPreviouslyConnected = mCurrentlyConnected;
78 mCurrentlyConnected = false;
79 if (mPreviouslyConnected) {
80 mDisconnectEvents.push(mCurrentFrameNumber);
Valerie Hau871d6352020-01-29 08:44:02 -080081 }
Jiakai Zhangc33c63a2021-11-09 11:24:04 +000082 mFrameEventHistory.onDisconnect();
Valerie Hau871d6352020-01-29 08:44:02 -080083}
84
85void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
86 FrameEventHistoryDelta* outDelta) {
Hongguang Chen621ec582021-02-16 15:42:35 -080087 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080088 if (newTimestamps) {
89 // BufferQueueProducer only adds a new timestamp on
90 // queueBuffer
91 mCurrentFrameNumber = newTimestamps->frameNumber;
92 mFrameEventHistory.addQueue(*newTimestamps);
93 }
94 if (outDelta) {
95 // frame event histories will be processed
96 // only after the producer connects and requests
97 // deltas for the first time. Forward this intent
98 // to SF-side to turn event processing back on
99 mPreviouslyConnected = mCurrentlyConnected;
100 mCurrentlyConnected = true;
101 mFrameEventHistory.getAndResetDelta(outDelta);
102 }
103}
104
105void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
106 const sp<Fence>& glDoneFence,
107 const sp<Fence>& presentFence,
108 const sp<Fence>& prevReleaseFence,
109 CompositorTiming compositorTiming,
110 nsecs_t latchTime, nsecs_t dequeueReadyTime) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800111 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800112
113 // if the producer is not connected, don't bother updating,
114 // the next producer that connects won't access this frame event
115 if (!mCurrentlyConnected) return;
116 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
117 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
118 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
119
120 mFrameEventHistory.addLatch(frameNumber, latchTime);
121 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
122 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
123 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
124 compositorTiming);
125}
126
127void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
128 bool disconnect = false;
Hongguang Chen621ec582021-02-16 15:42:35 -0800129 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800130 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
131 disconnect = true;
132 mDisconnectEvents.pop();
133 }
134 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
135}
136
Hongguang Chen621ec582021-02-16 15:42:35 -0800137void BLASTBufferItemConsumer::onSidebandStreamChanged() {
Ady Abrahamdbca1352021-12-15 11:58:56 -0800138 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
139 if (bbq != nullptr) {
Hongguang Chen621ec582021-02-16 15:42:35 -0800140 sp<NativeHandle> stream = getSidebandStream();
Ady Abrahamdbca1352021-12-15 11:58:56 -0800141 bbq->setSidebandStream(stream);
Hongguang Chen621ec582021-02-16 15:42:35 -0800142 }
143}
144
Ady Abraham107788e2023-10-17 12:31:08 -0700145#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
Ady Abraham6cdd3fd2023-09-07 18:45:58 -0700146void BLASTBufferItemConsumer::onSetFrameRate(float frameRate, int8_t compatibility,
147 int8_t changeFrameRateStrategy) {
148 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
149 if (bbq != nullptr) {
150 bbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
151 }
152}
153#endif
154
Brian Lindahlc794b692023-01-31 15:42:47 -0700155void BLASTBufferItemConsumer::resizeFrameEventHistory(size_t newSize) {
156 Mutex::Autolock lock(mMutex);
157 mFrameEventHistory.resize(newSize);
158}
159
Vishnu Naird2aaab12022-02-10 14:49:09 -0800160BLASTBufferQueue::BLASTBufferQueue(const std::string& name, bool updateDestinationFrame)
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800161 : mSurfaceControl(nullptr),
162 mSize(1, 1),
Vishnu Nairea0de002020-11-17 17:42:37 -0800163 mRequestedSize(mSize),
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800164 mFormat(PIXEL_FORMAT_RGBA_8888),
Tianhao Yao4861b102022-02-03 20:18:35 +0000165 mTransactionReadyCallback(nullptr),
Vishnu Naird2aaab12022-02-10 14:49:09 -0800166 mSyncTransaction(nullptr),
167 mUpdateDestinationFrame(updateDestinationFrame) {
Vishnu Nair89496122020-12-14 17:14:53 -0800168 createBufferQueue(&mProducer, &mConsumer);
Valerie Hau0889c622020-02-19 15:04:47 -0800169 // since the adapter is in the client process, set dequeue timeout
170 // explicitly so that dequeueBuffer will block
171 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
Valerie Hau65b8e872020-02-13 09:45:14 -0800172
Vishnu Nairdebd1cb2021-03-16 10:06:01 -0700173 // safe default, most producers are expected to override this
174 mProducer->setMaxDequeuedBufferCount(2);
Vishnu Nair1618c672021-02-05 13:08:26 -0800175 mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
176 GraphicBuffer::USAGE_HW_COMPOSER |
177 GraphicBuffer::USAGE_HW_TEXTURE,
Ady Abrahamdbca1352021-12-15 11:58:56 -0800178 1, false, this);
liulijuneb489f62022-10-17 22:02:14 +0800179 static std::atomic<uint32_t> nextId = 0;
180 mProducerId = nextId++;
181 mName = name + "#" + std::to_string(mProducerId);
182 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(mProducerId);
183 mQueuedBufferTrace = "QueuedBuffer - " + mName + "BLAST#" + std::to_string(mProducerId);
Vishnu Nairdab94092020-09-29 16:09:04 -0700184 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700185 mBufferItemConsumer->setFrameAvailableListener(this);
Robert Carr9f133d72020-04-01 15:51:46 -0700186
Huihong Luo02186fb2022-02-23 14:21:54 -0800187 ComposerServiceAIDL::getComposerService()->getMaxAcquiredBufferCount(&mMaxAcquiredBuffers);
Ady Abraham0bde6b52021-05-18 13:57:02 -0700188 mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxAcquiredBuffers);
chaviw69058fb2021-09-27 09:37:30 -0500189 mCurrentMaxAcquiredBufferCount = mMaxAcquiredBuffers;
Valerie Haua32c5522019-12-09 10:11:08 -0800190 mNumAcquired = 0;
191 mNumFrameAvailable = 0;
Robert Carr4c1b6462021-12-21 10:30:50 -0800192
193 TransactionCompletedListener::getInstance()->addQueueStallListener(
Patrick Williamsf1e5df12022-10-17 21:37:42 +0000194 [&](const std::string& reason) {
195 std::function<void(const std::string&)> callbackCopy;
196 {
197 std::unique_lock _lock{mMutex};
198 callbackCopy = mTransactionHangCallback;
199 }
200 if (callbackCopy) callbackCopy(reason);
201 },
202 this);
Robert Carr4c1b6462021-12-21 10:30:50 -0800203
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800204 BQA_LOGV("BLASTBufferQueue created");
Vishnu Nair1cb8e892021-12-06 16:45:48 -0800205}
206
207BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface,
208 int width, int height, int32_t format)
209 : BLASTBufferQueue(name) {
210 update(surface, width, height, format);
Robert Carr78c25dd2019-08-15 14:10:33 -0700211}
212
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800213BLASTBufferQueue::~BLASTBufferQueue() {
Robert Carr4c1b6462021-12-21 10:30:50 -0800214 TransactionCompletedListener::getInstance()->removeQueueStallListener(this);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800215 if (mPendingTransactions.empty()) {
216 return;
217 }
218 BQA_LOGE("Applying pending transactions on dtor %d",
219 static_cast<uint32_t>(mPendingTransactions.size()));
220 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800221 mergePendingTransactions(&t, std::numeric_limits<uint64_t>::max() /* frameNumber */);
Robert Carr79dc06a2022-02-22 15:28:59 -0800222 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
223 t.setApplyToken(mApplyToken).apply(false, true);
chaviw3b4bdcf2022-03-17 09:27:03 -0500224
225 if (mTransactionReadyCallback) {
226 mTransactionReadyCallback(mSyncTransaction);
227 }
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800228}
229
chaviw565ee542021-01-14 10:21:23 -0800230void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height,
Vishnu Naird2aaab12022-02-10 14:49:09 -0800231 int32_t format) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800232 LOG_ALWAYS_FATAL_IF(surface == nullptr, "BLASTBufferQueue: mSurfaceControl must not be NULL");
233
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000234 std::lock_guard _lock{mMutex};
chaviw565ee542021-01-14 10:21:23 -0800235 if (mFormat != format) {
236 mFormat = format;
chaviw497e81c2021-02-04 17:09:47 -0800237 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
chaviw565ee542021-01-14 10:21:23 -0800238 }
239
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800240 const bool surfaceControlChanged = !SurfaceControl::isSameSurface(mSurfaceControl, surface);
Vishnu Nairab066512022-01-04 22:28:00 +0000241 if (surfaceControlChanged && mSurfaceControl != nullptr) {
242 BQA_LOGD("Updating SurfaceControl without recreating BBQ");
243 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800244 bool applyTransaction = false;
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800245
Vishnu Nair5fa91c22021-06-29 14:30:48 -0700246 // Always update the native object even though they might have the same layer handle, so we can
247 // get the updated transform hint from WM.
248 mSurfaceControl = surface;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800249 SurfaceComposerClient::Transaction t;
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800250 if (surfaceControlChanged) {
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800251 t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure,
252 layer_state_t::eEnableBackpressure);
253 applyTransaction = true;
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800254 }
Vishnu Nair1e8bf102021-12-28 14:36:59 -0800255 mTransformHint = mSurfaceControl->getTransformHint();
256 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Naira4fbca52021-07-07 16:52:34 -0700257 BQA_LOGV("update width=%d height=%d format=%d mTransformHint=%d", width, height, format,
258 mTransformHint);
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800259
Vishnu Nairea0de002020-11-17 17:42:37 -0800260 ui::Size newSize(width, height);
261 if (mRequestedSize != newSize) {
262 mRequestedSize.set(newSize);
263 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000264 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800265 // If the buffer supports scaling, update the frame immediately since the client may
266 // want to scale the existing buffer to the new size.
267 mSize = mRequestedSize;
Vishnu Naird2aaab12022-02-10 14:49:09 -0800268 if (mUpdateDestinationFrame) {
269 t.setDestinationFrame(mSurfaceControl, Rect(newSize));
270 applyTransaction = true;
271 }
Vishnu Nair53c936c2020-12-03 11:46:37 -0800272 }
Robert Carrfc416512020-04-02 12:32:44 -0700273 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800274 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800275 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
276 t.setApplyToken(mApplyToken).apply(false, true);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800277 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700278}
279
chaviwd7deef72021-10-06 11:53:40 -0500280static std::optional<SurfaceControlStats> findMatchingStat(
281 const std::vector<SurfaceControlStats>& stats, const sp<SurfaceControl>& sc) {
282 for (auto stat : stats) {
283 if (SurfaceControl::isSameSurface(sc, stat.surfaceControl)) {
284 return stat;
285 }
286 }
287 return std::nullopt;
288}
289
290static void transactionCommittedCallbackThunk(void* context, nsecs_t latchTime,
291 const sp<Fence>& presentFence,
292 const std::vector<SurfaceControlStats>& stats) {
293 if (context == nullptr) {
294 return;
295 }
296 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
297 bq->transactionCommittedCallback(latchTime, presentFence, stats);
298}
299
300void BLASTBufferQueue::transactionCommittedCallback(nsecs_t /*latchTime*/,
301 const sp<Fence>& /*presentFence*/,
302 const std::vector<SurfaceControlStats>& stats) {
303 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000304 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600305 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500306 BQA_LOGV("transactionCommittedCallback");
307 if (!mSurfaceControlsWithPendingCallback.empty()) {
308 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
309 std::optional<SurfaceControlStats> stat = findMatchingStat(stats, pendingSC);
310 if (stat) {
311 uint64_t currFrameNumber = stat->frameEventStats.frameNumber;
312
313 // We need to check if we were waiting for a transaction callback in order to
314 // process any pending buffers and unblock. It's possible to get transaction
chaviwc1cf4022022-06-03 13:32:33 -0500315 // callbacks for previous requests so we need to ensure that there are no pending
316 // frame numbers that were in a sync. We remove the frame from mSyncedFrameNumbers
317 // set and then check if it's empty. If there are no more pending syncs, we can
318 // proceed with flushing the shadow queue.
chaviwc1cf4022022-06-03 13:32:33 -0500319 mSyncedFrameNumbers.erase(currFrameNumber);
Chavi Weingartend48797b2023-08-04 13:11:39 +0000320 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500321 flushShadowQueue();
322 }
323 } else {
chaviw768bfa02021-11-01 09:50:57 -0500324 BQA_LOGE("Failed to find matching SurfaceControl in transactionCommittedCallback");
chaviwd7deef72021-10-06 11:53:40 -0500325 }
326 } else {
327 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
328 "empty.");
329 }
chaviwd7deef72021-10-06 11:53:40 -0500330 decStrong((void*)transactionCommittedCallbackThunk);
331 }
332}
333
Robert Carr78c25dd2019-08-15 14:10:33 -0700334static void transactionCallbackThunk(void* context, nsecs_t latchTime,
335 const sp<Fence>& presentFence,
336 const std::vector<SurfaceControlStats>& stats) {
337 if (context == nullptr) {
338 return;
339 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800340 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700341 bq->transactionCallback(latchTime, presentFence, stats);
342}
343
344void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
345 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700346 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000347 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600348 BBQ_TRACE();
chaviw71c2cc42020-10-23 16:42:02 -0700349 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700350
chaviw42026162021-04-16 15:46:12 -0500351 if (!mSurfaceControlsWithPendingCallback.empty()) {
352 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
353 mSurfaceControlsWithPendingCallback.pop();
chaviwd7deef72021-10-06 11:53:40 -0500354 std::optional<SurfaceControlStats> statsOptional = findMatchingStat(stats, pendingSC);
355 if (statsOptional) {
356 SurfaceControlStats stat = *statsOptional;
Vishnu Nair71fcf912022-10-18 09:14:20 -0700357 if (stat.transformHint) {
358 mTransformHint = *stat.transformHint;
359 mBufferItemConsumer->setTransformHint(mTransformHint);
360 BQA_LOGV("updated mTransformHint=%d", mTransformHint);
361 }
Vishnu Nairde66dc72021-06-17 17:54:41 -0700362 // Update frametime stamps if the frame was latched and presented, indicated by a
363 // valid latch time.
364 if (stat.latchTime > 0) {
365 mBufferItemConsumer
366 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
367 stat.frameEventStats.refreshStartTime,
368 stat.frameEventStats.gpuCompositionDoneFence,
369 stat.presentFence, stat.previousReleaseFence,
370 stat.frameEventStats.compositorTiming,
371 stat.latchTime,
372 stat.frameEventStats.dequeueReadyTime);
373 }
Robert Carr405e2f62021-12-31 16:59:34 -0800374 auto currFrameNumber = stat.frameEventStats.frameNumber;
375 std::vector<ReleaseCallbackId> staleReleases;
376 for (const auto& [key, value]: mSubmitted) {
377 if (currFrameNumber > key.framenumber) {
378 staleReleases.push_back(key);
379 }
380 }
381 for (const auto& staleRelease : staleReleases) {
Robert Carr405e2f62021-12-31 16:59:34 -0800382 releaseBufferCallbackLocked(staleRelease,
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700383 stat.previousReleaseFence
384 ? stat.previousReleaseFence
385 : Fence::NO_FENCE,
386 stat.currentMaxAcquiredBufferCount,
387 true /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800388 }
chaviwd7deef72021-10-06 11:53:40 -0500389 } else {
chaviw768bfa02021-11-01 09:50:57 -0500390 BQA_LOGE("Failed to find matching SurfaceControl in transactionCallback");
chaviw42026162021-04-16 15:46:12 -0500391 }
392 } else {
393 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
394 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800395 }
chaviw71c2cc42020-10-23 16:42:02 -0700396
chaviw71c2cc42020-10-23 16:42:02 -0700397 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700398 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700399}
400
Vishnu Nair1506b182021-02-22 14:35:15 -0800401// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
402// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
403// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
404// Otherwise, this is a no-op.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700405static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, const ReleaseCallbackId& id,
chaviw69058fb2021-09-27 09:37:30 -0500406 const sp<Fence>& releaseFence,
407 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800408 sp<BLASTBufferQueue> blastBufferQueue = context.promote();
Vishnu Nair1506b182021-02-22 14:35:15 -0800409 if (blastBufferQueue) {
chaviw69058fb2021-09-27 09:37:30 -0500410 blastBufferQueue->releaseBufferCallback(id, releaseFence, currentMaxAcquiredBufferCount);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700411 } else {
412 ALOGV("releaseBufferCallbackThunk %s blastBufferQueue is dead", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800413 }
414}
415
chaviwd7deef72021-10-06 11:53:40 -0500416void BLASTBufferQueue::flushShadowQueue() {
417 BQA_LOGV("flushShadowQueue");
418 int numFramesToFlush = mNumFrameAvailable;
419 while (numFramesToFlush > 0) {
420 acquireNextBufferLocked(std::nullopt);
421 numFramesToFlush--;
422 }
423}
424
chaviw69058fb2021-09-27 09:37:30 -0500425void BLASTBufferQueue::releaseBufferCallback(
426 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
427 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000428 std::lock_guard _lock{mMutex};
chaviw57ae4b22022-02-03 16:51:39 -0600429 BBQ_TRACE();
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700430 releaseBufferCallbackLocked(id, releaseFence, currentMaxAcquiredBufferCount,
431 false /* fakeRelease */);
Robert Carr405e2f62021-12-31 16:59:34 -0800432}
433
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700434void BLASTBufferQueue::releaseBufferCallbackLocked(
435 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
436 std::optional<uint32_t> currentMaxAcquiredBufferCount, bool fakeRelease) {
Robert Carr405e2f62021-12-31 16:59:34 -0800437 ATRACE_CALL();
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700438 BQA_LOGV("releaseBufferCallback %s", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800439
Ady Abraham899dcdb2021-06-15 16:56:21 -0700440 // Calculate how many buffers we need to hold before we release them back
441 // to the buffer queue. This will prevent higher latency when we are running
442 // on a lower refresh rate than the max supported. We only do that for EGL
443 // clients as others don't care about latency
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000444 const auto it = mSubmitted.find(id);
445 const bool isEGL = it != mSubmitted.end() && it->second.mApi == NATIVE_WINDOW_API_EGL;
Ady Abraham899dcdb2021-06-15 16:56:21 -0700446
chaviw69058fb2021-09-27 09:37:30 -0500447 if (currentMaxAcquiredBufferCount) {
448 mCurrentMaxAcquiredBufferCount = *currentMaxAcquiredBufferCount;
449 }
450
liulijunf90df632022-11-14 14:24:48 +0800451 const uint32_t numPendingBuffersToHold =
452 isEGL ? std::max(0, mMaxAcquiredBuffers - (int32_t)mCurrentMaxAcquiredBufferCount) : 0;
Robert Carr405e2f62021-12-31 16:59:34 -0800453
454 auto rb = ReleasedBuffer{id, releaseFence};
455 if (std::find(mPendingRelease.begin(), mPendingRelease.end(), rb) == mPendingRelease.end()) {
456 mPendingRelease.emplace_back(rb);
Vishnu Nair28fe2e62022-11-01 14:29:10 -0700457 if (fakeRelease) {
458 BQA_LOGE("Faking releaseBufferCallback from transactionCompleteCallback %" PRIu64,
459 id.framenumber);
460 BBQ_TRACE("FakeReleaseCallback");
461 }
Robert Carr405e2f62021-12-31 16:59:34 -0800462 }
Ady Abraham899dcdb2021-06-15 16:56:21 -0700463
464 // Release all buffers that are beyond the ones that we need to hold
465 while (mPendingRelease.size() > numPendingBuffersToHold) {
chaviw0acd33a2021-11-02 11:55:37 -0500466 const auto releasedBuffer = mPendingRelease.front();
Ady Abraham899dcdb2021-06-15 16:56:21 -0700467 mPendingRelease.pop_front();
chaviw0acd33a2021-11-02 11:55:37 -0500468 releaseBuffer(releasedBuffer.callbackId, releasedBuffer.releaseFence);
chaviwc1cf4022022-06-03 13:32:33 -0500469 // Don't process the transactions here if mSyncedFrameNumbers is not empty. That means
470 // are still transactions that have sync buffers in them that have not been applied or
471 // dropped. Instead, let onFrameAvailable handle processing them since it will merge with
472 // the syncTransaction.
473 if (mSyncedFrameNumbers.empty()) {
chaviwd7deef72021-10-06 11:53:40 -0500474 acquireNextBufferLocked(std::nullopt);
475 }
Vishnu Nair1506b182021-02-22 14:35:15 -0800476 }
477
Ady Abraham899dcdb2021-06-15 16:56:21 -0700478 ATRACE_INT("PendingRelease", mPendingRelease.size());
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700479 ATRACE_INT(mQueuedBufferTrace.c_str(),
480 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
Vishnu Nair1506b182021-02-22 14:35:15 -0800481 mCallbackCV.notify_all();
482}
483
chaviw0acd33a2021-11-02 11:55:37 -0500484void BLASTBufferQueue::releaseBuffer(const ReleaseCallbackId& callbackId,
485 const sp<Fence>& releaseFence) {
486 auto it = mSubmitted.find(callbackId);
487 if (it == mSubmitted.end()) {
488 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %s",
489 callbackId.to_string().c_str());
490 return;
491 }
492 mNumAcquired--;
chaviw57ae4b22022-02-03 16:51:39 -0600493 BBQ_TRACE("frame=%" PRIu64, callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500494 BQA_LOGV("released %s", callbackId.to_string().c_str());
495 mBufferItemConsumer->releaseBuffer(it->second, releaseFence);
496 mSubmitted.erase(it);
chaviwc1cf4022022-06-03 13:32:33 -0500497 // Remove the frame number from mSyncedFrameNumbers since we can get a release callback
498 // without getting a transaction committed if the buffer was dropped.
499 mSyncedFrameNumbers.erase(callbackId.framenumber);
chaviw0acd33a2021-11-02 11:55:37 -0500500}
501
Chavi Weingarten70670e62023-02-22 17:36:40 +0000502static ui::Size getBufferSize(const BufferItem& item) {
503 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
504 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
505
506 // Take the buffer's orientation into account
507 if (item.mTransform & ui::Transform::ROT_90) {
508 std::swap(bufWidth, bufHeight);
509 }
510 return ui::Size(bufWidth, bufHeight);
511}
512
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000513status_t BLASTBufferQueue::acquireNextBufferLocked(
chaviwd7deef72021-10-06 11:53:40 -0500514 const std::optional<SurfaceComposerClient::Transaction*> transaction) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800515 // Check if we have frames available and we have not acquired the maximum number of buffers.
516 // Even with this check, the consumer can fail to acquire an additional buffer if the consumer
517 // has already acquired (mMaxAcquiredBuffers + 1) and the new buffer is not droppable. In this
518 // case mBufferItemConsumer->acquireBuffer will return with NO_BUFFER_AVAILABLE.
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000519 if (mNumFrameAvailable == 0) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800520 BQA_LOGV("Can't acquire next buffer. No available frames");
521 return BufferQueue::NO_BUFFER_AVAILABLE;
522 }
523
524 if (mNumAcquired >= (mMaxAcquiredBuffers + 2)) {
525 BQA_LOGV("Can't acquire next buffer. Already acquired max frames %d max:%d + 2",
526 mNumAcquired, mMaxAcquiredBuffers);
527 return BufferQueue::NO_BUFFER_AVAILABLE;
Valerie Haud3b90d22019-11-06 09:37:31 -0800528 }
529
Valerie Haua32c5522019-12-09 10:11:08 -0800530 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700531 BQA_LOGE("ERROR : surface control is null");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000532 return NAME_NOT_FOUND;
Valerie Haud3b90d22019-11-06 09:37:31 -0800533 }
534
Robert Carr78c25dd2019-08-15 14:10:33 -0700535 SurfaceComposerClient::Transaction localTransaction;
536 bool applyTransaction = true;
537 SurfaceComposerClient::Transaction* t = &localTransaction;
chaviwd7deef72021-10-06 11:53:40 -0500538 if (transaction) {
539 t = *transaction;
Robert Carr78c25dd2019-08-15 14:10:33 -0700540 applyTransaction = false;
541 }
542
Valerie Haua32c5522019-12-09 10:11:08 -0800543 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800544
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800545 status_t status =
546 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800547 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
548 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000549 return status;
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800550 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700551 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000552 return status;
Robert Carr78c25dd2019-08-15 14:10:33 -0700553 }
chaviw57ae4b22022-02-03 16:51:39 -0600554
Valerie Haua32c5522019-12-09 10:11:08 -0800555 auto buffer = bufferItem.mGraphicBuffer;
556 mNumFrameAvailable--;
chaviw57ae4b22022-02-03 16:51:39 -0600557 BBQ_TRACE("frame=%" PRIu64, bufferItem.mFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800558
559 if (buffer == nullptr) {
560 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700561 BQA_LOGE("Buffer was empty");
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000562 return BAD_VALUE;
Valerie Haua32c5522019-12-09 10:11:08 -0800563 }
564
Vishnu Nair670b3f72020-09-29 17:52:18 -0700565 if (rejectBuffer(bufferItem)) {
Vishnu Naira4fbca52021-07-07 16:52:34 -0700566 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d "
Vishnu Nairea0de002020-11-17 17:42:37 -0800567 "buffer{size=%dx%d transform=%d}",
568 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
569 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
570 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000571 return acquireNextBufferLocked(transaction);
Vishnu Nair670b3f72020-09-29 17:52:18 -0700572 }
573
Valerie Haua32c5522019-12-09 10:11:08 -0800574 mNumAcquired++;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700575 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
576 ReleaseCallbackId releaseCallbackId(buffer->getId(), mLastAcquiredFrameNumber);
577 mSubmitted[releaseCallbackId] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700578
Valerie Hau871d6352020-01-29 08:44:02 -0800579 bool needsDisconnect = false;
580 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
581
582 // if producer disconnected before, notify SurfaceFlinger
583 if (needsDisconnect) {
584 t->notifyProducerDisconnect(mSurfaceControl);
585 }
586
Robert Carr78c25dd2019-08-15 14:10:33 -0700587 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
588 incStrong((void*)transactionCallbackThunk);
589
Chavi Weingarten70670e62023-02-22 17:36:40 +0000590 // Only update mSize for destination bounds if the incoming buffer matches the requested size.
591 // Otherwise, it could cause stretching since the destination bounds will update before the
592 // buffer with the new size is acquired.
Vishnu Nair5b5f6932023-04-12 16:28:19 -0700593 if (mRequestedSize == getBufferSize(bufferItem) ||
594 bufferItem.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Chavi Weingarten70670e62023-02-22 17:36:40 +0000595 mSize = mRequestedSize;
596 }
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700597 Rect crop = computeCrop(bufferItem);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000598 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
599 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700600 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800601
Vishnu Nair1506b182021-02-22 14:35:15 -0800602 auto releaseBufferCallback =
603 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
chaviw69058fb2021-09-27 09:37:30 -0500604 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
chaviwba4320c2021-09-15 15:20:53 -0500605 sp<Fence> fence = bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE;
liulijuneb489f62022-10-17 22:02:14 +0800606 t->setBuffer(mSurfaceControl, buffer, fence, bufferItem.mFrameNumber, mProducerId,
607 releaseBufferCallback);
John Reck137069e2020-12-10 22:07:37 -0500608 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
609 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
610 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700611 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviwf2dace72021-11-17 17:36:50 -0600612
chaviw42026162021-04-16 15:46:12 -0500613 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700614
Vishnu Naird2aaab12022-02-10 14:49:09 -0800615 if (mUpdateDestinationFrame) {
616 t->setDestinationFrame(mSurfaceControl, Rect(mSize));
617 } else {
618 const bool ignoreDestinationFrame =
619 bufferItem.mScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE;
620 t->setFlags(mSurfaceControl,
621 ignoreDestinationFrame ? layer_state_t::eIgnoreDestinationFrame : 0,
622 layer_state_t::eIgnoreDestinationFrame);
Vishnu Nair084514a2021-07-30 16:07:42 -0700623 }
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700624 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800625 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800626 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Vishnu Naird2aaab12022-02-10 14:49:09 -0800627 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800628 if (!bufferItem.mIsAutoTimestamp) {
629 t->setDesiredPresentTime(bufferItem.mTimestamp);
630 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700631
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800632 // Drop stale frame timeline infos
633 while (!mPendingFrameTimelines.empty() &&
634 mPendingFrameTimelines.front().first < bufferItem.mFrameNumber) {
635 ATRACE_FORMAT_INSTANT("dropping stale frameNumber: %" PRIu64 " vsyncId: %" PRId64,
636 mPendingFrameTimelines.front().first,
637 mPendingFrameTimelines.front().second.vsyncId);
638 mPendingFrameTimelines.pop();
639 }
640
641 if (!mPendingFrameTimelines.empty() &&
642 mPendingFrameTimelines.front().first == bufferItem.mFrameNumber) {
643 ATRACE_FORMAT_INSTANT("Transaction::setFrameTimelineInfo frameNumber: %" PRIu64
644 " vsyncId: %" PRId64,
645 bufferItem.mFrameNumber,
646 mPendingFrameTimelines.front().second.vsyncId);
647 t->setFrameTimelineInfo(mPendingFrameTimelines.front().second);
648 mPendingFrameTimelines.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100649 }
650
Vishnu Nairadf632b2021-01-07 14:05:08 -0800651 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000652 std::lock_guard _lock{mTimestampMutex};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800653 auto dequeueTime = mDequeueTimestamps.find(buffer->getId());
654 if (dequeueTime != mDequeueTimestamps.end()) {
655 Parcel p;
656 p.writeInt64(dequeueTime->second);
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800657 t->setMetadata(mSurfaceControl, gui::METADATA_DEQUEUE_TIME, p);
Vishnu Nairadf632b2021-01-07 14:05:08 -0800658 mDequeueTimestamps.erase(dequeueTime);
659 }
660 }
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800661
chaviw6a195272021-09-03 16:14:25 -0500662 mergePendingTransactions(t, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700663 if (applyTransaction) {
Robert Carr79dc06a2022-02-22 15:28:59 -0800664 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
665 t->setApplyToken(mApplyToken).apply(false, true);
666 mAppliedLastTransaction = true;
667 mLastAppliedFrameNumber = bufferItem.mFrameNumber;
668 } else {
669 t->setBufferHasBarrier(mSurfaceControl, mLastAppliedFrameNumber);
670 mAppliedLastTransaction = false;
Robert Carr78c25dd2019-08-15 14:10:33 -0700671 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700672
chaviwd7deef72021-10-06 11:53:40 -0500673 BQA_LOGV("acquireNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800674 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
Vishnu Naira4fbca52021-07-07 16:52:34 -0700675 " graphicBufferId=%" PRIu64 "%s transform=%d",
chaviw3277faf2021-05-19 16:45:23 -0500676 mSize.width, mSize.height, bufferItem.mFrameNumber, boolToString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800677 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700678 static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(),
Vishnu Naira4fbca52021-07-07 16:52:34 -0700679 bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000680 return OK;
Robert Carr78c25dd2019-08-15 14:10:33 -0700681}
682
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800683Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
684 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800685 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800686 }
687 return item.mCrop;
688}
689
chaviwd7deef72021-10-06 11:53:40 -0500690void BLASTBufferQueue::acquireAndReleaseBuffer() {
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000691 BBQ_TRACE();
chaviwd7deef72021-10-06 11:53:40 -0500692 BufferItem bufferItem;
chaviw6ebdf5f2021-10-14 11:57:22 -0500693 status_t status =
694 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
695 if (status != OK) {
696 BQA_LOGE("Failed to acquire a buffer in acquireAndReleaseBuffer, err=%s",
697 statusToString(status).c_str());
698 return;
699 }
chaviwd7deef72021-10-06 11:53:40 -0500700 mNumFrameAvailable--;
chaviw6ebdf5f2021-10-14 11:57:22 -0500701 mBufferItemConsumer->releaseBuffer(bufferItem, bufferItem.mFence);
chaviwd7deef72021-10-06 11:53:40 -0500702}
703
Vishnu Nairaef1de92020-10-22 12:15:53 -0700704void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000705 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
706 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
chaviwc1cf4022022-06-03 13:32:33 -0500707
Tianhao Yao4861b102022-02-03 20:18:35 +0000708 {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000709 UNIQUE_LOCK_WITH_ASSERTION(mMutex);
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000710 BBQ_TRACE();
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000711 bool waitForTransactionCallback = !mSyncedFrameNumbers.empty();
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800712
Tianhao Yao4861b102022-02-03 20:18:35 +0000713 const bool syncTransactionSet = mTransactionReadyCallback != nullptr;
714 BQA_LOGV("onFrameAvailable-start syncTransactionSet=%s", boolToString(syncTransactionSet));
Valerie Haud3b90d22019-11-06 09:37:31 -0800715
Tianhao Yao4861b102022-02-03 20:18:35 +0000716 if (syncTransactionSet) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000717 // If we are going to re-use the same mSyncTransaction, release the buffer that may
718 // already be set in the Transaction. This is to allow us a free slot early to continue
719 // processing a new buffer.
720 if (!mAcquireSingleBuffer) {
721 auto bufferData = mSyncTransaction->getAndClearBuffer(mSurfaceControl);
722 if (bufferData) {
723 BQA_LOGD("Releasing previous buffer when syncing: framenumber=%" PRIu64,
724 bufferData->frameNumber);
725 releaseBuffer(bufferData->generateReleaseCallbackId(),
726 bufferData->acquireFence);
Tianhao Yao4861b102022-02-03 20:18:35 +0000727 }
728 }
chaviw0acd33a2021-11-02 11:55:37 -0500729
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000730 if (waitForTransactionCallback) {
731 // We are waiting on a previous sync's transaction callback so allow another sync
732 // transaction to proceed.
733 //
734 // We need to first flush out the transactions that were in between the two syncs.
735 // We do this by merging them into mSyncTransaction so any buffer merging will get
736 // a release callback invoked.
737 while (mNumFrameAvailable > 0) {
738 // flush out the shadow queue
739 acquireAndReleaseBuffer();
740 }
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800741 } else {
742 // Make sure the frame available count is 0 before proceeding with a sync to ensure
743 // the correct frame is used for the sync. The only way mNumFrameAvailable would be
744 // greater than 0 is if we already ran out of buffers previously. This means we
745 // need to flush the buffers before proceeding with the sync.
746 while (mNumFrameAvailable > 0) {
747 BQA_LOGD("waiting until no queued buffers");
748 mCallbackCV.wait(_lock);
749 }
chaviwd7deef72021-10-06 11:53:40 -0500750 }
751 }
752
Tianhao Yao4861b102022-02-03 20:18:35 +0000753 // add to shadow queue
754 mNumFrameAvailable++;
chaviwc1cf4022022-06-03 13:32:33 -0500755 if (waitForTransactionCallback && mNumFrameAvailable >= 2) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000756 acquireAndReleaseBuffer();
757 }
758 ATRACE_INT(mQueuedBufferTrace.c_str(),
759 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
760
761 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " syncTransactionSet=%s",
762 item.mFrameNumber, boolToString(syncTransactionSet));
763
764 if (syncTransactionSet) {
Vishnu Nairb4b484a2023-01-20 10:00:18 -0800765 // Add to mSyncedFrameNumbers before waiting in case any buffers are released
766 // while waiting for a free buffer. The release and commit callback will try to
767 // acquire buffers if there are any available, but we don't want it to acquire
768 // in the case where a sync transaction wants the buffer.
769 mSyncedFrameNumbers.emplace(item.mFrameNumber);
Chavi Weingarten3a8f19b2022-12-27 22:00:24 +0000770 // If there's no available buffer and we're in a sync transaction, we need to wait
771 // instead of returning since we guarantee a buffer will be acquired for the sync.
772 while (acquireNextBufferLocked(mSyncTransaction) == BufferQueue::NO_BUFFER_AVAILABLE) {
773 BQA_LOGD("waiting for available buffer");
774 mCallbackCV.wait(_lock);
775 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000776
777 // Only need a commit callback when syncing to ensure the buffer that's synced has been
778 // sent to SF
779 incStrong((void*)transactionCommittedCallbackThunk);
780 mSyncTransaction->addTransactionCommittedCallback(transactionCommittedCallbackThunk,
781 static_cast<void*>(this));
Tianhao Yao4861b102022-02-03 20:18:35 +0000782 if (mAcquireSingleBuffer) {
783 prevCallback = mTransactionReadyCallback;
784 prevTransaction = mSyncTransaction;
785 mTransactionReadyCallback = nullptr;
786 mSyncTransaction = nullptr;
787 }
chaviwc1cf4022022-06-03 13:32:33 -0500788 } else if (!waitForTransactionCallback) {
Tianhao Yao4861b102022-02-03 20:18:35 +0000789 acquireNextBufferLocked(std::nullopt);
Valerie Hau0188adf2020-02-13 08:29:20 -0800790 }
791 }
Tianhao Yao4861b102022-02-03 20:18:35 +0000792 if (prevCallback) {
793 prevCallback(prevTransaction);
chaviwd7deef72021-10-06 11:53:40 -0500794 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800795}
796
Vishnu Nairaef1de92020-10-22 12:15:53 -0700797void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
798 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
799 // Do nothing since we are not storing unacquired buffer items locally.
800}
801
Vishnu Nairadf632b2021-01-07 14:05:08 -0800802void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000803 std::lock_guard _lock{mTimestampMutex};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800804 mDequeueTimestamps[bufferId] = systemTime();
805};
806
807void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000808 std::lock_guard _lock{mTimestampMutex};
Vishnu Nairadf632b2021-01-07 14:05:08 -0800809 mDequeueTimestamps.erase(bufferId);
810};
811
Chavi Weingartenc398c012023-04-12 17:26:02 +0000812bool BLASTBufferQueue::syncNextTransaction(
Tianhao Yao4861b102022-02-03 20:18:35 +0000813 std::function<void(SurfaceComposerClient::Transaction*)> callback,
814 bool acquireSingleBuffer) {
Chavi Weingartenc398c012023-04-12 17:26:02 +0000815 LOG_ALWAYS_FATAL_IF(!callback,
816 "BLASTBufferQueue: callback passed in to syncNextTransaction must not be "
817 "NULL");
chaviw3b4bdcf2022-03-17 09:27:03 -0500818
Chavi Weingartenc398c012023-04-12 17:26:02 +0000819 std::lock_guard _lock{mMutex};
820 BBQ_TRACE();
821 if (mTransactionReadyCallback) {
822 ALOGW("Attempting to overwrite transaction callback in syncNextTransaction");
823 return false;
Tianhao Yao4861b102022-02-03 20:18:35 +0000824 }
chaviw3b4bdcf2022-03-17 09:27:03 -0500825
Chavi Weingartenc398c012023-04-12 17:26:02 +0000826 mTransactionReadyCallback = callback;
827 mSyncTransaction = new SurfaceComposerClient::Transaction();
828 mAcquireSingleBuffer = acquireSingleBuffer;
829 return true;
Tianhao Yao4861b102022-02-03 20:18:35 +0000830}
831
832void BLASTBufferQueue::stopContinuousSyncTransaction() {
833 std::function<void(SurfaceComposerClient::Transaction*)> prevCallback = nullptr;
834 SurfaceComposerClient::Transaction* prevTransaction = nullptr;
835 {
836 std::lock_guard _lock{mMutex};
Chavi Weingartenc398c012023-04-12 17:26:02 +0000837 if (mAcquireSingleBuffer || !mTransactionReadyCallback) {
838 ALOGW("Attempting to stop continuous sync when none are active");
839 return;
Tianhao Yao4861b102022-02-03 20:18:35 +0000840 }
Chavi Weingartenc398c012023-04-12 17:26:02 +0000841
842 prevCallback = mTransactionReadyCallback;
843 prevTransaction = mSyncTransaction;
844
Tianhao Yao4861b102022-02-03 20:18:35 +0000845 mTransactionReadyCallback = nullptr;
846 mSyncTransaction = nullptr;
847 mAcquireSingleBuffer = true;
848 }
Chavi Weingartenc398c012023-04-12 17:26:02 +0000849
Tianhao Yao4861b102022-02-03 20:18:35 +0000850 if (prevCallback) {
851 prevCallback(prevTransaction);
852 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700853}
854
Chavi Weingartenc398c012023-04-12 17:26:02 +0000855void BLASTBufferQueue::clearSyncTransaction() {
856 std::lock_guard _lock{mMutex};
857 if (!mAcquireSingleBuffer) {
858 ALOGW("Attempting to clear sync transaction when none are active");
859 return;
860 }
861
862 mTransactionReadyCallback = nullptr;
863 mSyncTransaction = nullptr;
864}
865
Vishnu Nairea0de002020-11-17 17:42:37 -0800866bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700867 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
868 // Only reject buffers if scaling mode is freeze.
869 return false;
870 }
871
Chavi Weingarten70670e62023-02-22 17:36:40 +0000872 ui::Size bufferSize = getBufferSize(item);
Vishnu Nairea0de002020-11-17 17:42:37 -0800873 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800874 return false;
875 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700876
Vishnu Nair670b3f72020-09-29 17:52:18 -0700877 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800878 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700879}
Vishnu Nairbf255772020-10-16 10:54:41 -0700880
Robert Carr05086b22020-10-13 18:22:51 -0700881class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700882private:
Vishnu Nair95b6d512021-08-30 15:31:08 -0700883 std::mutex mMutex;
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000884 sp<BLASTBufferQueue> mBbq GUARDED_BY(mMutex);
885 bool mDestroyed GUARDED_BY(mMutex) = false;
Vishnu Nair95b6d512021-08-30 15:31:08 -0700886
Robert Carr05086b22020-10-13 18:22:51 -0700887public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700888 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
889 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
890 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700891
Robert Carr05086b22020-10-13 18:22:51 -0700892 void allocateBuffers() override {
893 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
894 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
895 auto gbp = getIGraphicBufferProducer();
896 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
897 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
898 gbp->allocateBuffers(reqWidth, reqHeight,
899 reqFormat, reqUsage);
900
901 }).detach();
902 }
Robert Carr9c006e02020-10-14 13:41:57 -0700903
Marin Shalamanovc5986772021-03-16 16:09:49 +0100904 status_t setFrameRate(float frameRate, int8_t compatibility,
905 int8_t changeFrameRateStrategy) override {
Ady Abraham6cdd3fd2023-09-07 18:45:58 -0700906 if (flags::bq_setframerate()) {
907 return Surface::setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
908 }
909
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000910 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700911 if (mDestroyed) {
912 return DEAD_OBJECT;
913 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100914 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
915 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700916 return BAD_VALUE;
917 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100918 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700919 }
Robert Carr9b611b72020-10-19 12:00:23 -0700920
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800921 status_t setFrameTimelineInfo(uint64_t frameNumber,
922 const FrameTimelineInfo& frameTimelineInfo) override {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000923 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700924 if (mDestroyed) {
925 return DEAD_OBJECT;
926 }
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800927 return mBbq->setFrameTimelineInfo(frameNumber, frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700928 }
Vishnu Nair95b6d512021-08-30 15:31:08 -0700929
930 void destroy() override {
931 Surface::destroy();
932
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000933 std::lock_guard _lock{mMutex};
Vishnu Nair95b6d512021-08-30 15:31:08 -0700934 mDestroyed = true;
935 mBbq = nullptr;
936 }
Robert Carr05086b22020-10-13 18:22:51 -0700937};
938
Robert Carr9c006e02020-10-14 13:41:57 -0700939// TODO: Can we coalesce this with frame updates? Need to confirm
940// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200941status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
942 bool shouldBeSeamless) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000943 std::lock_guard _lock{mMutex};
Robert Carr9c006e02020-10-14 13:41:57 -0700944 SurfaceComposerClient::Transaction t;
945
Marin Shalamanov46084422020-10-13 12:33:42 +0200946 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700947}
948
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800949status_t BLASTBufferQueue::setFrameTimelineInfo(uint64_t frameNumber,
950 const FrameTimelineInfo& frameTimelineInfo) {
951 ATRACE_FORMAT("%s(%s) frameNumber: %" PRIu64 " vsyncId: %" PRId64, __func__, mName.c_str(),
952 frameNumber, frameTimelineInfo.vsyncId);
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000953 std::lock_guard _lock{mMutex};
Ady Abrahamd6e409e2023-01-19 16:07:31 -0800954 mPendingFrameTimelines.push({frameNumber, frameTimelineInfo});
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100955 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700956}
957
Hongguang Chen621ec582021-02-16 15:42:35 -0800958void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000959 std::lock_guard _lock{mMutex};
Hongguang Chen621ec582021-02-16 15:42:35 -0800960 SurfaceComposerClient::Transaction t;
961
962 t.setSidebandStream(mSurfaceControl, stream).apply();
963}
964
Vishnu Nair992496b2020-10-22 17:27:21 -0700965sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +0000966 std::lock_guard _lock{mMutex};
Vishnu Nair992496b2020-10-22 17:27:21 -0700967 sp<IBinder> scHandle = nullptr;
968 if (includeSurfaceControlHandle && mSurfaceControl) {
969 scHandle = mSurfaceControl->getHandle();
970 }
971 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700972}
973
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800974void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
975 uint64_t frameNumber) {
976 std::lock_guard _lock{mMutex};
977 if (mLastAcquiredFrameNumber >= frameNumber) {
978 // Apply the transaction since we have already acquired the desired frame.
979 t->apply();
980 } else {
chaviwaad6cf52021-03-23 17:27:20 -0500981 mPendingTransactions.emplace_back(frameNumber, *t);
982 // Clear the transaction so it can't be applied elsewhere.
983 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800984 }
985}
986
chaviw6a195272021-09-03 16:14:25 -0500987void BLASTBufferQueue::applyPendingTransactions(uint64_t frameNumber) {
988 std::lock_guard _lock{mMutex};
989
990 SurfaceComposerClient::Transaction t;
991 mergePendingTransactions(&t, frameNumber);
Robert Carr79dc06a2022-02-22 15:28:59 -0800992 // All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
993 t.setApplyToken(mApplyToken).apply(false, true);
chaviw6a195272021-09-03 16:14:25 -0500994}
995
996void BLASTBufferQueue::mergePendingTransactions(SurfaceComposerClient::Transaction* t,
997 uint64_t frameNumber) {
998 auto mergeTransaction =
999 [&t, currentFrameNumber = frameNumber](
1000 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
1001 auto& [targetFrameNumber, transaction] = pendingTransaction;
1002 if (currentFrameNumber < targetFrameNumber) {
1003 return false;
1004 }
1005 t->merge(std::move(transaction));
1006 return true;
1007 };
1008
1009 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
1010 mPendingTransactions.end(), mergeTransaction),
1011 mPendingTransactions.end());
1012}
1013
chaviwd84085a2022-02-08 11:07:04 -06001014SurfaceComposerClient::Transaction* BLASTBufferQueue::gatherPendingTransactions(
1015 uint64_t frameNumber) {
1016 std::lock_guard _lock{mMutex};
1017 SurfaceComposerClient::Transaction* t = new SurfaceComposerClient::Transaction();
1018 mergePendingTransactions(t, frameNumber);
1019 return t;
1020}
1021
Vishnu Nair89496122020-12-14 17:14:53 -08001022// Maintains a single worker thread per process that services a list of runnables.
1023class AsyncWorker : public Singleton<AsyncWorker> {
1024private:
1025 std::thread mThread;
1026 bool mDone = false;
1027 std::deque<std::function<void()>> mRunnables;
1028 std::mutex mMutex;
1029 std::condition_variable mCv;
1030 void run() {
1031 std::unique_lock<std::mutex> lock(mMutex);
1032 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -08001033 while (!mRunnables.empty()) {
Vishnu Nair51e4dc82021-10-01 15:32:33 -07001034 std::deque<std::function<void()>> runnables = std::move(mRunnables);
1035 mRunnables.clear();
1036 lock.unlock();
1037 // Run outside the lock since the runnable might trigger another
1038 // post to the async worker.
1039 execute(runnables);
1040 lock.lock();
Vishnu Nair89496122020-12-14 17:14:53 -08001041 }
Wonsik Kim567533e2021-05-04 19:31:29 -07001042 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -08001043 }
1044 }
1045
Vishnu Nair51e4dc82021-10-01 15:32:33 -07001046 void execute(std::deque<std::function<void()>>& runnables) {
1047 while (!runnables.empty()) {
1048 std::function<void()> runnable = runnables.front();
1049 runnables.pop_front();
1050 runnable();
1051 }
1052 }
1053
Vishnu Nair89496122020-12-14 17:14:53 -08001054public:
1055 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
1056
1057 ~AsyncWorker() {
1058 mDone = true;
1059 mCv.notify_all();
1060 if (mThread.joinable()) {
1061 mThread.join();
1062 }
1063 }
1064
1065 void post(std::function<void()> runnable) {
1066 std::unique_lock<std::mutex> lock(mMutex);
1067 mRunnables.emplace_back(std::move(runnable));
1068 mCv.notify_one();
1069 }
1070};
1071ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
1072
1073// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
1074class AsyncProducerListener : public BnProducerListener {
1075private:
1076 const sp<IProducerListener> mListener;
1077
1078public:
1079 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
1080
1081 void onBufferReleased() override {
1082 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
1083 }
1084
1085 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
1086 AsyncWorker::getInstance().post(
1087 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
1088 }
1089};
1090
1091// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
1092// can be non-blocking when the producer is in the client process.
1093class BBQBufferQueueProducer : public BufferQueueProducer {
1094public:
Brian Lindahlc794b692023-01-31 15:42:47 -07001095 BBQBufferQueueProducer(const sp<BufferQueueCore>& core, wp<BLASTBufferQueue> bbq)
1096 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/),
1097 mBLASTBufferQueue(std::move(bbq)) {}
Vishnu Nair89496122020-12-14 17:14:53 -08001098
1099 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
1100 QueueBufferOutput* output) override {
1101 if (!listener) {
1102 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
1103 }
1104
1105 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
1106 producerControlledByApp, output);
1107 }
Vishnu Nair17dde612020-12-28 11:39:59 -08001108
Brian Lindahlc794b692023-01-31 15:42:47 -07001109 // We want to resize the frame history when changing the size of the buffer queue
1110 status_t setMaxDequeuedBufferCount(int maxDequeuedBufferCount) override {
1111 int maxBufferCount;
1112 status_t status = BufferQueueProducer::setMaxDequeuedBufferCount(maxDequeuedBufferCount,
1113 &maxBufferCount);
1114 // if we can't determine the max buffer count, then just skip growing the history size
1115 if (status == OK) {
1116 size_t newFrameHistorySize = maxBufferCount + 2; // +2 because triple buffer rendering
1117 // optimize away resizing the frame history unless it will grow
1118 if (newFrameHistorySize > FrameEventHistory::INITIAL_MAX_FRAME_HISTORY) {
1119 sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote();
1120 if (bbq != nullptr) {
1121 ALOGV("increasing frame history size to %zu", newFrameHistorySize);
1122 bbq->resizeFrameEventHistory(newFrameHistorySize);
1123 }
1124 }
1125 }
1126 return status;
1127 }
1128
Vishnu Nair17dde612020-12-28 11:39:59 -08001129 int query(int what, int* value) override {
1130 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
1131 *value = 1;
1132 return NO_ERROR;
1133 }
1134 return BufferQueueProducer::query(what, value);
1135 }
Brian Lindahlc794b692023-01-31 15:42:47 -07001136
1137private:
1138 const wp<BLASTBufferQueue> mBLASTBufferQueue;
Vishnu Nair89496122020-12-14 17:14:53 -08001139};
1140
1141// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
1142// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
1143// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
1144// we can deadlock.
1145void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
1146 sp<IGraphicBufferConsumer>* outConsumer) {
1147 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
1148 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
1149
1150 sp<BufferQueueCore> core(new BufferQueueCore());
1151 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
1152
Brian Lindahlc794b692023-01-31 15:42:47 -07001153 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core, this));
Vishnu Nair89496122020-12-14 17:14:53 -08001154 LOG_ALWAYS_FATAL_IF(producer == nullptr,
1155 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
1156
Vishnu Nair8b30dd12021-01-25 14:16:54 -08001157 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
1158 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -08001159 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
1160 "BLASTBufferQueue: failed to create BufferQueueConsumer");
1161
1162 *outProducer = producer;
1163 *outConsumer = consumer;
1164}
1165
Brian Lindahlc794b692023-01-31 15:42:47 -07001166void BLASTBufferQueue::resizeFrameEventHistory(size_t newSize) {
1167 // This can be null during creation of the buffer queue, but resizing won't do anything at that
1168 // point in time, so just ignore. This can go away once the class relationships and lifetimes of
1169 // objects are cleaned up with a major refactor of BufferQueue as a whole.
1170 if (mBufferItemConsumer != nullptr) {
1171 std::unique_lock _lock{mMutex};
1172 mBufferItemConsumer->resizeFrameEventHistory(newSize);
1173 }
1174}
1175
chaviw497e81c2021-02-04 17:09:47 -08001176PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
1177 PixelFormat convertedFormat = format;
1178 switch (format) {
1179 case PIXEL_FORMAT_TRANSPARENT:
1180 case PIXEL_FORMAT_TRANSLUCENT:
1181 convertedFormat = PIXEL_FORMAT_RGBA_8888;
1182 break;
1183 case PIXEL_FORMAT_OPAQUE:
1184 convertedFormat = PIXEL_FORMAT_RGBX_8888;
1185 break;
1186 }
1187 return convertedFormat;
1188}
1189
Robert Carr82d07c92021-05-10 11:36:43 -07001190uint32_t BLASTBufferQueue::getLastTransformHint() const {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001191 std::lock_guard _lock{mMutex};
Robert Carr82d07c92021-05-10 11:36:43 -07001192 if (mSurfaceControl != nullptr) {
1193 return mSurfaceControl->getTransformHint();
1194 } else {
1195 return 0;
1196 }
1197}
1198
chaviw0b020f82021-08-20 12:00:47 -05001199uint64_t BLASTBufferQueue::getLastAcquiredFrameNum() {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001200 std::lock_guard _lock{mMutex};
chaviw0b020f82021-08-20 12:00:47 -05001201 return mLastAcquiredFrameNumber;
1202}
1203
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001204bool BLASTBufferQueue::isSameSurfaceControl(const sp<SurfaceControl>& surfaceControl) const {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001205 std::lock_guard _lock{mMutex};
Vishnu Nair1e8bf102021-12-28 14:36:59 -08001206 return SurfaceControl::isSameSurface(mSurfaceControl, surfaceControl);
1207}
1208
Patrick Williamsf1e5df12022-10-17 21:37:42 +00001209void BLASTBufferQueue::setTransactionHangCallback(
1210 std::function<void(const std::string&)> callback) {
Chavi Weingartene0237bb2023-02-06 21:48:32 +00001211 std::lock_guard _lock{mMutex};
Robert Carr4c1b6462021-12-21 10:30:50 -08001212 mTransactionHangCallback = callback;
1213}
1214
Robert Carr78c25dd2019-08-15 14:10:33 -07001215} // namespace android