blob: 68514cf9af640f9d7b3f95d2466ca4549263ba69 [file] [log] [blame]
Robert Carr78c25dd2019-08-15 14:10:33 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Valerie Haud3b90d22019-11-06 09:37:31 -080017#undef LOG_TAG
18#define LOG_TAG "BLASTBufferQueue"
19
Valerie Haua32c5522019-12-09 10:11:08 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Vishnu Naire1a42322020-10-02 17:42:04 -070021//#define LOG_NDEBUG 0
Valerie Haua32c5522019-12-09 10:11:08 -080022
Robert Carr78c25dd2019-08-15 14:10:33 -070023#include <gui/BLASTBufferQueue.h>
24#include <gui/BufferItemConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080025#include <gui/BufferQueueConsumer.h>
26#include <gui/BufferQueueCore.h>
27#include <gui/BufferQueueProducer.h>
Valerie Hau45e4b3b2019-12-03 10:49:17 -080028#include <gui/GLConsumer.h>
Vishnu Nair89496122020-12-14 17:14:53 -080029#include <gui/IProducerListener.h>
Robert Carr05086b22020-10-13 18:22:51 -070030#include <gui/Surface.h>
Vishnu Nair89496122020-12-14 17:14:53 -080031#include <utils/Singleton.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070032
Valerie Haua32c5522019-12-09 10:11:08 -080033#include <utils/Trace.h>
34
Robert Carr78c25dd2019-08-15 14:10:33 -070035#include <chrono>
36
37using namespace std::chrono_literals;
38
Vishnu Nairdab94092020-09-29 16:09:04 -070039namespace {
40inline const char* toString(bool b) {
41 return b ? "true" : "false";
42}
43} // namespace
44
Robert Carr78c25dd2019-08-15 14:10:33 -070045namespace android {
46
Vishnu Nairdab94092020-09-29 16:09:04 -070047// Macros to include adapter info in log messages
48#define BQA_LOGV(x, ...) \
49 ALOGV("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairc6f89ee2020-12-11 14:27:32 -080050// enable logs for a single layer
51//#define BQA_LOGV(x, ...) \
52// ALOGV_IF((strstr(mName.c_str(), "SurfaceView") != nullptr), "[%s](f:%u,a:%u) " x, \
53// mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070054#define BQA_LOGE(x, ...) \
55 ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
56
Valerie Hau871d6352020-01-29 08:44:02 -080057void BLASTBufferItemConsumer::onDisconnect() {
Hongguang Chen621ec582021-02-16 15:42:35 -080058 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080059 mPreviouslyConnected = mCurrentlyConnected;
60 mCurrentlyConnected = false;
61 if (mPreviouslyConnected) {
62 mDisconnectEvents.push(mCurrentFrameNumber);
63 }
64 mFrameEventHistory.onDisconnect();
65}
66
67void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
68 FrameEventHistoryDelta* outDelta) {
Hongguang Chen621ec582021-02-16 15:42:35 -080069 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080070 if (newTimestamps) {
71 // BufferQueueProducer only adds a new timestamp on
72 // queueBuffer
73 mCurrentFrameNumber = newTimestamps->frameNumber;
74 mFrameEventHistory.addQueue(*newTimestamps);
75 }
76 if (outDelta) {
77 // frame event histories will be processed
78 // only after the producer connects and requests
79 // deltas for the first time. Forward this intent
80 // to SF-side to turn event processing back on
81 mPreviouslyConnected = mCurrentlyConnected;
82 mCurrentlyConnected = true;
83 mFrameEventHistory.getAndResetDelta(outDelta);
84 }
85}
86
87void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
88 const sp<Fence>& glDoneFence,
89 const sp<Fence>& presentFence,
90 const sp<Fence>& prevReleaseFence,
91 CompositorTiming compositorTiming,
92 nsecs_t latchTime, nsecs_t dequeueReadyTime) {
Hongguang Chen621ec582021-02-16 15:42:35 -080093 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080094
95 // if the producer is not connected, don't bother updating,
96 // the next producer that connects won't access this frame event
97 if (!mCurrentlyConnected) return;
98 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
99 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
100 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
101
102 mFrameEventHistory.addLatch(frameNumber, latchTime);
103 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
104 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
105 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
106 compositorTiming);
107}
108
109void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
110 bool disconnect = false;
Hongguang Chen621ec582021-02-16 15:42:35 -0800111 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800112 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
113 disconnect = true;
114 mDisconnectEvents.pop();
115 }
116 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
117}
118
Hongguang Chen621ec582021-02-16 15:42:35 -0800119void BLASTBufferItemConsumer::setBlastBufferQueue(BLASTBufferQueue* blastbufferqueue) {
120 Mutex::Autolock lock(mMutex);
121 mBLASTBufferQueue = blastbufferqueue;
122}
123
124void BLASTBufferItemConsumer::onSidebandStreamChanged() {
125 Mutex::Autolock lock(mMutex);
126 if (mBLASTBufferQueue != nullptr) {
127 sp<NativeHandle> stream = getSidebandStream();
128 mBLASTBufferQueue->setSidebandStream(stream);
129 }
130}
131
Vishnu Nairdab94092020-09-29 16:09:04 -0700132BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface,
Vishnu Nairdebd1cb2021-03-16 10:06:01 -0700133 int width, int height, int32_t format)
Vishnu Nairdab94092020-09-29 16:09:04 -0700134 : mName(name),
135 mSurfaceControl(surface),
Vishnu Nairea0de002020-11-17 17:42:37 -0800136 mSize(width, height),
137 mRequestedSize(mSize),
chaviw565ee542021-01-14 10:21:23 -0800138 mFormat(format),
Valerie Haud3b90d22019-11-06 09:37:31 -0800139 mNextTransaction(nullptr) {
Vishnu Nair89496122020-12-14 17:14:53 -0800140 createBufferQueue(&mProducer, &mConsumer);
Valerie Hau0889c622020-02-19 15:04:47 -0800141 // since the adapter is in the client process, set dequeue timeout
142 // explicitly so that dequeueBuffer will block
143 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
Valerie Hau65b8e872020-02-13 09:45:14 -0800144
Vishnu Nairdebd1cb2021-03-16 10:06:01 -0700145 // safe default, most producers are expected to override this
146 mProducer->setMaxDequeuedBufferCount(2);
Vishnu Nair1618c672021-02-05 13:08:26 -0800147 mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
148 GraphicBuffer::USAGE_HW_COMPOSER |
149 GraphicBuffer::USAGE_HW_TEXTURE,
150 1, false);
Valerie Haua32c5522019-12-09 10:11:08 -0800151 static int32_t id = 0;
Vishnu Nairdab94092020-09-29 16:09:04 -0700152 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(id);
Valerie Haua32c5522019-12-09 10:11:08 -0800153 id++;
Vishnu Nairdab94092020-09-29 16:09:04 -0700154 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700155 mBufferItemConsumer->setFrameAvailableListener(this);
156 mBufferItemConsumer->setBufferFreedListener(this);
Vishnu Nairea0de002020-11-17 17:42:37 -0800157 mBufferItemConsumer->setDefaultBufferSize(mSize.width, mSize.height);
chaviw497e81c2021-02-04 17:09:47 -0800158 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
Hongguang Chen621ec582021-02-16 15:42:35 -0800159 mBufferItemConsumer->setBlastBufferQueue(this);
Robert Carr9f133d72020-04-01 15:51:46 -0700160
Valerie Hau2882e982020-01-23 13:33:10 -0800161 mTransformHint = mSurfaceControl->getTransformHint();
Robert Carr9f133d72020-04-01 15:51:46 -0700162 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800163 SurfaceComposerClient::Transaction()
Robert Carr5b3b9142021-02-22 12:27:32 -0800164 .setFlags(surface, layer_state_t::eEnableBackpressure,
165 layer_state_t::eEnableBackpressure)
166 .apply();
Valerie Haud3b90d22019-11-06 09:37:31 -0800167
Valerie Haua32c5522019-12-09 10:11:08 -0800168 mNumAcquired = 0;
169 mNumFrameAvailable = 0;
Robert Carr78c25dd2019-08-15 14:10:33 -0700170}
171
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800172BLASTBufferQueue::~BLASTBufferQueue() {
Hongguang Chen621ec582021-02-16 15:42:35 -0800173 mBufferItemConsumer->setBlastBufferQueue(nullptr);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800174 if (mPendingTransactions.empty()) {
175 return;
176 }
177 BQA_LOGE("Applying pending transactions on dtor %d",
178 static_cast<uint32_t>(mPendingTransactions.size()));
179 SurfaceComposerClient::Transaction t;
180 for (auto& [targetFrameNumber, transaction] : mPendingTransactions) {
181 t.merge(std::move(transaction));
182 }
183 t.apply();
184}
185
chaviw565ee542021-01-14 10:21:23 -0800186void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height,
187 int32_t format) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700188 std::unique_lock _lock{mMutex};
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700189 BQA_LOGV("update width=%d height=%d format=%d", width, height, format);
chaviw565ee542021-01-14 10:21:23 -0800190 if (mFormat != format) {
191 mFormat = format;
chaviw497e81c2021-02-04 17:09:47 -0800192 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
chaviw565ee542021-01-14 10:21:23 -0800193 }
194
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800195 SurfaceComposerClient::Transaction t;
196 bool applyTransaction = false;
197 if (!SurfaceControl::isSameSurface(mSurfaceControl, surface)) {
198 mSurfaceControl = surface;
199 t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure,
200 layer_state_t::eEnableBackpressure);
201 applyTransaction = true;
202 }
203
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800204 if (mSurfaceControl != nullptr) {
205 mTransformHint = mSurfaceControl->getTransformHint();
206 mBufferItemConsumer->setTransformHint(mTransformHint);
207 }
208
Vishnu Nairea0de002020-11-17 17:42:37 -0800209 ui::Size newSize(width, height);
210 if (mRequestedSize != newSize) {
211 mRequestedSize.set(newSize);
212 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000213 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800214 // If the buffer supports scaling, update the frame immediately since the client may
215 // want to scale the existing buffer to the new size.
216 mSize = mRequestedSize;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000217 // We only need to update the scale if we've received at least one buffer. The reason
218 // for this is the scale is calculated based on the requested size and buffer size.
219 // If there's no buffer, the scale will always be 1.
220 if (mLastBufferInfo.hasBuffer) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700221 t.setDestinationFrame(mSurfaceControl,
222 Rect(0, 0, newSize.getWidth(), newSize.getHeight()));
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000223 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800224 applyTransaction = true;
Vishnu Nair53c936c2020-12-03 11:46:37 -0800225 }
Robert Carrfc416512020-04-02 12:32:44 -0700226 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800227 if (applyTransaction) {
228 t.apply();
229 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700230}
231
232static void transactionCallbackThunk(void* context, nsecs_t latchTime,
233 const sp<Fence>& presentFence,
234 const std::vector<SurfaceControlStats>& stats) {
235 if (context == nullptr) {
236 return;
237 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800238 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700239 bq->transactionCallback(latchTime, presentFence, stats);
240}
241
242void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
243 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700244 std::function<void(int64_t)> transactionCompleteCallback = nullptr;
245 uint64_t currFrameNumber = 0;
Vishnu Nairdab94092020-09-29 16:09:04 -0700246
chaviw71c2cc42020-10-23 16:42:02 -0700247 {
248 std::unique_lock _lock{mMutex};
249 ATRACE_CALL();
250 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700251
chaviw42026162021-04-16 15:46:12 -0500252 if (!mSurfaceControlsWithPendingCallback.empty()) {
253 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
254 mSurfaceControlsWithPendingCallback.pop();
255 bool found = false;
256 for (auto stat : stats) {
257 if (!SurfaceControl::isSameSurface(pendingSC, stat.surfaceControl)) {
258 continue;
Vishnu Nair1506b182021-02-22 14:35:15 -0800259 }
chaviw42026162021-04-16 15:46:12 -0500260
261 mTransformHint = stat.transformHint;
262 mBufferItemConsumer->setTransformHint(mTransformHint);
263 mBufferItemConsumer
264 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
265 stat.frameEventStats.refreshStartTime,
266 stat.frameEventStats.gpuCompositionDoneFence,
267 stat.presentFence, stat.previousReleaseFence,
268 stat.frameEventStats.compositorTiming,
269 stat.latchTime,
270 stat.frameEventStats.dequeueReadyTime);
271
272 currFrameNumber = stat.frameEventStats.frameNumber;
273
274 if (mTransactionCompleteCallback &&
275 currFrameNumber >= mTransactionCompleteFrameNumber) {
276 if (currFrameNumber > mTransactionCompleteFrameNumber) {
277 BQA_LOGE("transactionCallback received for a newer framenumber=%" PRIu64
278 " than expected=%" PRIu64,
279 currFrameNumber, mTransactionCompleteFrameNumber);
280 }
281 transactionCompleteCallback = std::move(mTransactionCompleteCallback);
282 mTransactionCompleteFrameNumber = 0;
283 }
284
285 found = true;
286 break;
chaviw71c2cc42020-10-23 16:42:02 -0700287 }
chaviw42026162021-04-16 15:46:12 -0500288
289 if (!found) {
290 BQA_LOGE("Failed to find matching SurfaceControl in transaction callback");
291 }
292 } else {
293 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
294 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800295 }
chaviw71c2cc42020-10-23 16:42:02 -0700296
chaviw71c2cc42020-10-23 16:42:02 -0700297 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700298 }
Valerie Haua32c5522019-12-09 10:11:08 -0800299
chaviw71c2cc42020-10-23 16:42:02 -0700300 if (transactionCompleteCallback) {
301 transactionCompleteCallback(currFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800302 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700303}
304
Vishnu Nair1506b182021-02-22 14:35:15 -0800305// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
306// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
307// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
308// Otherwise, this is a no-op.
309static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, uint64_t graphicBufferId,
Robert Carr97e7cc02021-06-07 10:45:40 -0700310 const sp<Fence>& releaseFence, uint32_t transformHint) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800311 sp<BLASTBufferQueue> blastBufferQueue = context.promote();
312 ALOGV("releaseBufferCallbackThunk graphicBufferId=%" PRIu64 " blastBufferQueue=%s",
313 graphicBufferId, blastBufferQueue ? "alive" : "dead");
314 if (blastBufferQueue) {
Robert Carr97e7cc02021-06-07 10:45:40 -0700315 blastBufferQueue->releaseBufferCallback(graphicBufferId, releaseFence, transformHint);
Vishnu Nair1506b182021-02-22 14:35:15 -0800316 }
317}
318
319void BLASTBufferQueue::releaseBufferCallback(uint64_t graphicBufferId,
Robert Carr97e7cc02021-06-07 10:45:40 -0700320 const sp<Fence>& releaseFence,
321 uint32_t transformHint) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800322 ATRACE_CALL();
323 std::unique_lock _lock{mMutex};
324 BQA_LOGV("releaseBufferCallback graphicBufferId=%" PRIu64, graphicBufferId);
325
Robert Carr82d07c92021-05-10 11:36:43 -0700326 if (mSurfaceControl != nullptr) {
Robert Carr97e7cc02021-06-07 10:45:40 -0700327 mTransformHint = transformHint;
328 mSurfaceControl->setTransformHint(transformHint);
Robert Carr82d07c92021-05-10 11:36:43 -0700329 mBufferItemConsumer->setTransformHint(mTransformHint);
330 }
331
Vishnu Nair1506b182021-02-22 14:35:15 -0800332 auto it = mSubmitted.find(graphicBufferId);
333 if (it == mSubmitted.end()) {
334 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %" PRIu64,
335 graphicBufferId);
336 return;
337 }
338
339 mBufferItemConsumer->releaseBuffer(it->second, releaseFence);
340 mSubmitted.erase(it);
341 mNumAcquired--;
342 processNextBufferLocked(false /* useNextTransaction */);
343 mCallbackCV.notify_all();
344}
345
Robert Carr255acdc2020-04-17 14:08:55 -0700346void BLASTBufferQueue::processNextBufferLocked(bool useNextTransaction) {
Valerie Haua32c5522019-12-09 10:11:08 -0800347 ATRACE_CALL();
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800348 // If the next transaction is set, we want to guarantee the our acquire will not fail, so don't
349 // include the extra buffer when checking if we can acquire the next buffer.
350 const bool includeExtraAcquire = !useNextTransaction;
351 if (mNumFrameAvailable == 0 || maxBuffersAcquired(includeExtraAcquire)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800352 mCallbackCV.notify_all();
Valerie Haud3b90d22019-11-06 09:37:31 -0800353 return;
354 }
355
Valerie Haua32c5522019-12-09 10:11:08 -0800356 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700357 BQA_LOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800358 return;
359 }
360
Robert Carr78c25dd2019-08-15 14:10:33 -0700361 SurfaceComposerClient::Transaction localTransaction;
362 bool applyTransaction = true;
363 SurfaceComposerClient::Transaction* t = &localTransaction;
Robert Carr255acdc2020-04-17 14:08:55 -0700364 if (mNextTransaction != nullptr && useNextTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700365 t = mNextTransaction;
366 mNextTransaction = nullptr;
367 applyTransaction = false;
368 }
369
Valerie Haua32c5522019-12-09 10:11:08 -0800370 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800371
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800372 status_t status =
373 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800374 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
375 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
376 return;
377 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700378 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Robert Carr78c25dd2019-08-15 14:10:33 -0700379 return;
380 }
Valerie Haua32c5522019-12-09 10:11:08 -0800381 auto buffer = bufferItem.mGraphicBuffer;
382 mNumFrameAvailable--;
383
384 if (buffer == nullptr) {
385 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700386 BQA_LOGE("Buffer was empty");
Valerie Haua32c5522019-12-09 10:11:08 -0800387 return;
388 }
389
Vishnu Nair670b3f72020-09-29 17:52:18 -0700390 if (rejectBuffer(bufferItem)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800391 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d"
392 "buffer{size=%dx%d transform=%d}",
393 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
394 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
395 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
396 processNextBufferLocked(useNextTransaction);
397 return;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700398 }
399
Valerie Haua32c5522019-12-09 10:11:08 -0800400 mNumAcquired++;
Vishnu Nair1506b182021-02-22 14:35:15 -0800401 mSubmitted[buffer->getId()] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700402
Valerie Hau871d6352020-01-29 08:44:02 -0800403 bool needsDisconnect = false;
404 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
405
406 // if producer disconnected before, notify SurfaceFlinger
407 if (needsDisconnect) {
408 t->notifyProducerDisconnect(mSurfaceControl);
409 }
410
Robert Carr78c25dd2019-08-15 14:10:33 -0700411 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
412 incStrong((void*)transactionCallbackThunk);
413
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700414 Rect crop = computeCrop(bufferItem);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800415 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000416 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
417 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700418 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800419
Vishnu Nair1506b182021-02-22 14:35:15 -0800420 auto releaseBufferCallback =
421 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
Robert Carr97e7cc02021-06-07 10:45:40 -0700422 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
Vishnu Nair1506b182021-02-22 14:35:15 -0800423 t->setBuffer(mSurfaceControl, buffer, releaseBufferCallback);
John Reck137069e2020-12-10 22:07:37 -0500424 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
425 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
426 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700427 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800428 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700429 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviw42026162021-04-16 15:46:12 -0500430 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700431
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700432 t->setDestinationFrame(mSurfaceControl, Rect(0, 0, mSize.getWidth(), mSize.getHeight()));
433 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800434 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800435 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800436 if (!bufferItem.mIsAutoTimestamp) {
437 t->setDesiredPresentTime(bufferItem.mTimestamp);
438 }
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700439 t->setFrameNumber(mSurfaceControl, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700440
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000441 if (!mNextFrameTimelineInfoQueue.empty()) {
Ady Abraham8db10102021-03-15 17:19:23 -0700442 t->setFrameTimelineInfo(mNextFrameTimelineInfoQueue.front());
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000443 mNextFrameTimelineInfoQueue.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100444 }
445
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800446 if (mAutoRefresh != bufferItem.mAutoRefresh) {
447 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
448 mAutoRefresh = bufferItem.mAutoRefresh;
449 }
Vishnu Nairadf632b2021-01-07 14:05:08 -0800450 {
451 std::unique_lock _lock{mTimestampMutex};
452 auto dequeueTime = mDequeueTimestamps.find(buffer->getId());
453 if (dequeueTime != mDequeueTimestamps.end()) {
454 Parcel p;
455 p.writeInt64(dequeueTime->second);
456 t->setMetadata(mSurfaceControl, METADATA_DEQUEUE_TIME, p);
457 mDequeueTimestamps.erase(dequeueTime);
458 }
459 }
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800460
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800461 auto mergeTransaction =
462 [&t, currentFrameNumber = bufferItem.mFrameNumber](
463 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
464 auto& [targetFrameNumber, transaction] = pendingTransaction;
465 if (currentFrameNumber < targetFrameNumber) {
466 return false;
467 }
468 t->merge(std::move(transaction));
469 return true;
470 };
471
472 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
473 mPendingTransactions.end(), mergeTransaction),
474 mPendingTransactions.end());
475
Robert Carr78c25dd2019-08-15 14:10:33 -0700476 if (applyTransaction) {
Vishnu Nair277142c2021-01-05 18:35:29 -0800477 t->setApplyToken(mApplyToken).apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700478 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700479
480 BQA_LOGV("processNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800481 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
482 " graphicBufferId=%" PRIu64,
Vishnu Nairea0de002020-11-17 17:42:37 -0800483 mSize.width, mSize.height, bufferItem.mFrameNumber, toString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800484 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
485 static_cast<uint32_t>(mPendingTransactions.size()),
486 bufferItem.mGraphicBuffer->getId());
Robert Carr78c25dd2019-08-15 14:10:33 -0700487}
488
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800489Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
490 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800491 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800492 }
493 return item.mCrop;
494}
495
Vishnu Nairaef1de92020-10-22 12:15:53 -0700496void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Valerie Haua32c5522019-12-09 10:11:08 -0800497 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800498 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800499
Vishnu Nairdab94092020-09-29 16:09:04 -0700500 const bool nextTransactionSet = mNextTransaction != nullptr;
Vishnu Nair1506b182021-02-22 14:35:15 -0800501 if (nextTransactionSet) {
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800502 while (mNumFrameAvailable > 0 || maxBuffersAcquired(false /* includeExtraAcquire */)) {
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700503 BQA_LOGV("waiting in onFrameAvailable...");
Valerie Hau0188adf2020-02-13 08:29:20 -0800504 mCallbackCV.wait(_lock);
505 }
506 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800507 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800508 mNumFrameAvailable++;
Vishnu Nair1506b182021-02-22 14:35:15 -0800509
510 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " nextTransactionSet=%s", item.mFrameNumber,
511 toString(nextTransactionSet));
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800512 processNextBufferLocked(nextTransactionSet /* useNextTransaction */);
Valerie Haud3b90d22019-11-06 09:37:31 -0800513}
514
Vishnu Nairaef1de92020-10-22 12:15:53 -0700515void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
516 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
517 // Do nothing since we are not storing unacquired buffer items locally.
518}
519
Vishnu Nairadf632b2021-01-07 14:05:08 -0800520void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
521 std::unique_lock _lock{mTimestampMutex};
522 mDequeueTimestamps[bufferId] = systemTime();
523};
524
525void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
526 std::unique_lock _lock{mTimestampMutex};
527 mDequeueTimestamps.erase(bufferId);
528};
529
Robert Carr78c25dd2019-08-15 14:10:33 -0700530void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800531 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700532 mNextTransaction = t;
533}
534
Vishnu Nairea0de002020-11-17 17:42:37 -0800535bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700536 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Hongguang Chen33100282021-04-15 18:36:05 -0700537 mSize = mRequestedSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700538 // Only reject buffers if scaling mode is freeze.
539 return false;
540 }
541
Vishnu Naire1a42322020-10-02 17:42:04 -0700542 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
543 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
544
545 // Take the buffer's orientation into account
546 if (item.mTransform & ui::Transform::ROT_90) {
547 std::swap(bufWidth, bufHeight);
548 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800549 ui::Size bufferSize(bufWidth, bufHeight);
550 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
551 mSize = mRequestedSize;
552 return false;
553 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700554
Vishnu Nair670b3f72020-09-29 17:52:18 -0700555 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800556 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700557}
Vishnu Nairbf255772020-10-16 10:54:41 -0700558
chaviw71c2cc42020-10-23 16:42:02 -0700559void BLASTBufferQueue::setTransactionCompleteCallback(
560 uint64_t frameNumber, std::function<void(int64_t)>&& transactionCompleteCallback) {
561 std::lock_guard _lock{mMutex};
562 if (transactionCompleteCallback == nullptr) {
563 mTransactionCompleteCallback = nullptr;
564 } else {
565 mTransactionCompleteCallback = std::move(transactionCompleteCallback);
566 mTransactionCompleteFrameNumber = frameNumber;
567 }
568}
569
Vishnu Nairbf255772020-10-16 10:54:41 -0700570// Check if we have acquired the maximum number of buffers.
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800571// Consumer can acquire an additional buffer if that buffer is not droppable. Set
572// includeExtraAcquire is true to include this buffer to the count. Since this depends on the state
573// of the buffer, the next acquire may return with NO_BUFFER_AVAILABLE.
574bool BLASTBufferQueue::maxBuffersAcquired(bool includeExtraAcquire) const {
575 int maxAcquiredBuffers = MAX_ACQUIRED_BUFFERS + (includeExtraAcquire ? 2 : 1);
Vishnu Nair1506b182021-02-22 14:35:15 -0800576 return mNumAcquired == maxAcquiredBuffers;
Vishnu Nairbf255772020-10-16 10:54:41 -0700577}
578
Robert Carr05086b22020-10-13 18:22:51 -0700579class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700580private:
581 sp<BLASTBufferQueue> mBbq;
Robert Carr05086b22020-10-13 18:22:51 -0700582public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700583 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
584 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
585 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700586
Robert Carr05086b22020-10-13 18:22:51 -0700587 void allocateBuffers() override {
588 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
589 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
590 auto gbp = getIGraphicBufferProducer();
591 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
592 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
593 gbp->allocateBuffers(reqWidth, reqHeight,
594 reqFormat, reqUsage);
595
596 }).detach();
597 }
Robert Carr9c006e02020-10-14 13:41:57 -0700598
Marin Shalamanovc5986772021-03-16 16:09:49 +0100599 status_t setFrameRate(float frameRate, int8_t compatibility,
600 int8_t changeFrameRateStrategy) override {
601 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
602 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700603 return BAD_VALUE;
604 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100605 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700606 }
Robert Carr9b611b72020-10-19 12:00:23 -0700607
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000608 status_t setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) override {
609 return mBbq->setFrameTimelineInfo(frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700610 }
Robert Carr82d07c92021-05-10 11:36:43 -0700611 protected:
612 uint32_t getTransformHint() const override {
613 if (mStickyTransform == 0 && !transformToDisplayInverse()) {
614 return mBbq->getLastTransformHint();
615 } else {
616 return 0;
617 }
618 }
Robert Carr05086b22020-10-13 18:22:51 -0700619};
620
Robert Carr9c006e02020-10-14 13:41:57 -0700621// TODO: Can we coalesce this with frame updates? Need to confirm
622// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200623status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
624 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700625 std::unique_lock _lock{mMutex};
626 SurfaceComposerClient::Transaction t;
627
Marin Shalamanov46084422020-10-13 12:33:42 +0200628 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700629}
630
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000631status_t BLASTBufferQueue::setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) {
Robert Carr9b611b72020-10-19 12:00:23 -0700632 std::unique_lock _lock{mMutex};
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000633 mNextFrameTimelineInfoQueue.push(frameTimelineInfo);
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100634 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700635}
636
Hongguang Chen621ec582021-02-16 15:42:35 -0800637void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
638 std::unique_lock _lock{mMutex};
639 SurfaceComposerClient::Transaction t;
640
641 t.setSidebandStream(mSurfaceControl, stream).apply();
642}
643
Vishnu Nair992496b2020-10-22 17:27:21 -0700644sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
645 std::unique_lock _lock{mMutex};
646 sp<IBinder> scHandle = nullptr;
647 if (includeSurfaceControlHandle && mSurfaceControl) {
648 scHandle = mSurfaceControl->getHandle();
649 }
650 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700651}
652
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800653void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
654 uint64_t frameNumber) {
655 std::lock_guard _lock{mMutex};
656 if (mLastAcquiredFrameNumber >= frameNumber) {
657 // Apply the transaction since we have already acquired the desired frame.
658 t->apply();
659 } else {
chaviwaad6cf52021-03-23 17:27:20 -0500660 mPendingTransactions.emplace_back(frameNumber, *t);
661 // Clear the transaction so it can't be applied elsewhere.
662 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800663 }
664}
665
Vishnu Nair89496122020-12-14 17:14:53 -0800666// Maintains a single worker thread per process that services a list of runnables.
667class AsyncWorker : public Singleton<AsyncWorker> {
668private:
669 std::thread mThread;
670 bool mDone = false;
671 std::deque<std::function<void()>> mRunnables;
672 std::mutex mMutex;
673 std::condition_variable mCv;
674 void run() {
675 std::unique_lock<std::mutex> lock(mMutex);
676 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -0800677 while (!mRunnables.empty()) {
678 std::function<void()> runnable = mRunnables.front();
679 mRunnables.pop_front();
680 runnable();
681 }
Wonsik Kim567533e2021-05-04 19:31:29 -0700682 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -0800683 }
684 }
685
686public:
687 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
688
689 ~AsyncWorker() {
690 mDone = true;
691 mCv.notify_all();
692 if (mThread.joinable()) {
693 mThread.join();
694 }
695 }
696
697 void post(std::function<void()> runnable) {
698 std::unique_lock<std::mutex> lock(mMutex);
699 mRunnables.emplace_back(std::move(runnable));
700 mCv.notify_one();
701 }
702};
703ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
704
705// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
706class AsyncProducerListener : public BnProducerListener {
707private:
708 const sp<IProducerListener> mListener;
709
710public:
711 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
712
713 void onBufferReleased() override {
714 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
715 }
716
717 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
718 AsyncWorker::getInstance().post(
719 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
720 }
721};
722
723// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
724// can be non-blocking when the producer is in the client process.
725class BBQBufferQueueProducer : public BufferQueueProducer {
726public:
727 BBQBufferQueueProducer(const sp<BufferQueueCore>& core)
728 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/) {}
729
730 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
731 QueueBufferOutput* output) override {
732 if (!listener) {
733 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
734 }
735
736 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
737 producerControlledByApp, output);
738 }
Vishnu Nair17dde612020-12-28 11:39:59 -0800739
740 int query(int what, int* value) override {
741 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
742 *value = 1;
743 return NO_ERROR;
744 }
745 return BufferQueueProducer::query(what, value);
746 }
Vishnu Nair89496122020-12-14 17:14:53 -0800747};
748
749// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
750// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
751// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
752// we can deadlock.
753void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
754 sp<IGraphicBufferConsumer>* outConsumer) {
755 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
756 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
757
758 sp<BufferQueueCore> core(new BufferQueueCore());
759 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
760
761 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core));
762 LOG_ALWAYS_FATAL_IF(producer == nullptr,
763 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
764
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800765 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
766 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -0800767 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
768 "BLASTBufferQueue: failed to create BufferQueueConsumer");
769
770 *outProducer = producer;
771 *outConsumer = consumer;
772}
773
chaviw497e81c2021-02-04 17:09:47 -0800774PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
775 PixelFormat convertedFormat = format;
776 switch (format) {
777 case PIXEL_FORMAT_TRANSPARENT:
778 case PIXEL_FORMAT_TRANSLUCENT:
779 convertedFormat = PIXEL_FORMAT_RGBA_8888;
780 break;
781 case PIXEL_FORMAT_OPAQUE:
782 convertedFormat = PIXEL_FORMAT_RGBX_8888;
783 break;
784 }
785 return convertedFormat;
786}
787
Robert Carr82d07c92021-05-10 11:36:43 -0700788uint32_t BLASTBufferQueue::getLastTransformHint() const {
789 if (mSurfaceControl != nullptr) {
790 return mSurfaceControl->getTransformHint();
791 } else {
792 return 0;
793 }
794}
795
Robert Carr78c25dd2019-08-15 14:10:33 -0700796} // namespace android