blob: a023067887ac9b6d89ce8b09fa6798a6ed37322f [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
Vishnu Nairea0de002020-11-17 17:42:37 -0800204 ui::Size newSize(width, height);
205 if (mRequestedSize != newSize) {
206 mRequestedSize.set(newSize);
207 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000208 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800209 // If the buffer supports scaling, update the frame immediately since the client may
210 // want to scale the existing buffer to the new size.
211 mSize = mRequestedSize;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000212 // We only need to update the scale if we've received at least one buffer. The reason
213 // for this is the scale is calculated based on the requested size and buffer size.
214 // If there's no buffer, the scale will always be 1.
215 if (mLastBufferInfo.hasBuffer) {
216 setMatrix(&t, mLastBufferInfo);
217 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800218 applyTransaction = true;
Vishnu Nair53c936c2020-12-03 11:46:37 -0800219 }
Robert Carrfc416512020-04-02 12:32:44 -0700220 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800221 if (applyTransaction) {
222 t.apply();
223 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700224}
225
226static void transactionCallbackThunk(void* context, nsecs_t latchTime,
227 const sp<Fence>& presentFence,
228 const std::vector<SurfaceControlStats>& stats) {
229 if (context == nullptr) {
230 return;
231 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800232 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700233 bq->transactionCallback(latchTime, presentFence, stats);
234}
235
236void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
237 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700238 std::function<void(int64_t)> transactionCompleteCallback = nullptr;
239 uint64_t currFrameNumber = 0;
Vishnu Nairdab94092020-09-29 16:09:04 -0700240
chaviw71c2cc42020-10-23 16:42:02 -0700241 {
242 std::unique_lock _lock{mMutex};
243 ATRACE_CALL();
244 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700245
chaviw42026162021-04-16 15:46:12 -0500246 if (!mSurfaceControlsWithPendingCallback.empty()) {
247 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
248 mSurfaceControlsWithPendingCallback.pop();
249 bool found = false;
250 for (auto stat : stats) {
251 if (!SurfaceControl::isSameSurface(pendingSC, stat.surfaceControl)) {
252 continue;
Vishnu Nair1506b182021-02-22 14:35:15 -0800253 }
chaviw42026162021-04-16 15:46:12 -0500254
255 mTransformHint = stat.transformHint;
256 mBufferItemConsumer->setTransformHint(mTransformHint);
257 mBufferItemConsumer
258 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
259 stat.frameEventStats.refreshStartTime,
260 stat.frameEventStats.gpuCompositionDoneFence,
261 stat.presentFence, stat.previousReleaseFence,
262 stat.frameEventStats.compositorTiming,
263 stat.latchTime,
264 stat.frameEventStats.dequeueReadyTime);
265
266 currFrameNumber = stat.frameEventStats.frameNumber;
267
268 if (mTransactionCompleteCallback &&
269 currFrameNumber >= mTransactionCompleteFrameNumber) {
270 if (currFrameNumber > mTransactionCompleteFrameNumber) {
271 BQA_LOGE("transactionCallback received for a newer framenumber=%" PRIu64
272 " than expected=%" PRIu64,
273 currFrameNumber, mTransactionCompleteFrameNumber);
274 }
275 transactionCompleteCallback = std::move(mTransactionCompleteCallback);
276 mTransactionCompleteFrameNumber = 0;
277 }
278
279 found = true;
280 break;
chaviw71c2cc42020-10-23 16:42:02 -0700281 }
chaviw42026162021-04-16 15:46:12 -0500282
283 if (!found) {
284 BQA_LOGE("Failed to find matching SurfaceControl in transaction callback");
285 }
286 } else {
287 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
288 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800289 }
chaviw71c2cc42020-10-23 16:42:02 -0700290
chaviw71c2cc42020-10-23 16:42:02 -0700291 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700292 }
Valerie Haua32c5522019-12-09 10:11:08 -0800293
chaviw71c2cc42020-10-23 16:42:02 -0700294 if (transactionCompleteCallback) {
295 transactionCompleteCallback(currFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800296 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700297}
298
Vishnu Nair1506b182021-02-22 14:35:15 -0800299// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
300// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
301// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
302// Otherwise, this is a no-op.
303static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, uint64_t graphicBufferId,
304 const sp<Fence>& releaseFence) {
305 sp<BLASTBufferQueue> blastBufferQueue = context.promote();
306 ALOGV("releaseBufferCallbackThunk graphicBufferId=%" PRIu64 " blastBufferQueue=%s",
307 graphicBufferId, blastBufferQueue ? "alive" : "dead");
308 if (blastBufferQueue) {
309 blastBufferQueue->releaseBufferCallback(graphicBufferId, releaseFence);
310 }
311}
312
313void BLASTBufferQueue::releaseBufferCallback(uint64_t graphicBufferId,
314 const sp<Fence>& releaseFence) {
315 ATRACE_CALL();
316 std::unique_lock _lock{mMutex};
317 BQA_LOGV("releaseBufferCallback graphicBufferId=%" PRIu64, graphicBufferId);
318
Robert Carr82d07c92021-05-10 11:36:43 -0700319 if (mSurfaceControl != nullptr) {
320 mTransformHint = mSurfaceControl->getTransformHint();
321 mBufferItemConsumer->setTransformHint(mTransformHint);
322 }
323
Vishnu Nair1506b182021-02-22 14:35:15 -0800324 auto it = mSubmitted.find(graphicBufferId);
325 if (it == mSubmitted.end()) {
326 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %" PRIu64,
327 graphicBufferId);
328 return;
329 }
330
331 mBufferItemConsumer->releaseBuffer(it->second, releaseFence);
332 mSubmitted.erase(it);
333 mNumAcquired--;
334 processNextBufferLocked(false /* useNextTransaction */);
335 mCallbackCV.notify_all();
336}
337
Robert Carr255acdc2020-04-17 14:08:55 -0700338void BLASTBufferQueue::processNextBufferLocked(bool useNextTransaction) {
Valerie Haua32c5522019-12-09 10:11:08 -0800339 ATRACE_CALL();
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800340 // If the next transaction is set, we want to guarantee the our acquire will not fail, so don't
341 // include the extra buffer when checking if we can acquire the next buffer.
342 const bool includeExtraAcquire = !useNextTransaction;
343 if (mNumFrameAvailable == 0 || maxBuffersAcquired(includeExtraAcquire)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800344 mCallbackCV.notify_all();
Valerie Haud3b90d22019-11-06 09:37:31 -0800345 return;
346 }
347
Valerie Haua32c5522019-12-09 10:11:08 -0800348 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700349 BQA_LOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800350 return;
351 }
352
Robert Carr78c25dd2019-08-15 14:10:33 -0700353 SurfaceComposerClient::Transaction localTransaction;
354 bool applyTransaction = true;
355 SurfaceComposerClient::Transaction* t = &localTransaction;
Robert Carr255acdc2020-04-17 14:08:55 -0700356 if (mNextTransaction != nullptr && useNextTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700357 t = mNextTransaction;
358 mNextTransaction = nullptr;
359 applyTransaction = false;
360 }
361
Valerie Haua32c5522019-12-09 10:11:08 -0800362 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800363
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800364 status_t status =
365 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800366 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
367 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
368 return;
369 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700370 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Robert Carr78c25dd2019-08-15 14:10:33 -0700371 return;
372 }
Valerie Haua32c5522019-12-09 10:11:08 -0800373 auto buffer = bufferItem.mGraphicBuffer;
374 mNumFrameAvailable--;
375
376 if (buffer == nullptr) {
377 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700378 BQA_LOGE("Buffer was empty");
Valerie Haua32c5522019-12-09 10:11:08 -0800379 return;
380 }
381
Vishnu Nair670b3f72020-09-29 17:52:18 -0700382 if (rejectBuffer(bufferItem)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800383 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d"
384 "buffer{size=%dx%d transform=%d}",
385 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
386 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
387 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
388 processNextBufferLocked(useNextTransaction);
389 return;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700390 }
391
Valerie Haua32c5522019-12-09 10:11:08 -0800392 mNumAcquired++;
Vishnu Nair1506b182021-02-22 14:35:15 -0800393 mSubmitted[buffer->getId()] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700394
Valerie Hau871d6352020-01-29 08:44:02 -0800395 bool needsDisconnect = false;
396 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
397
398 // if producer disconnected before, notify SurfaceFlinger
399 if (needsDisconnect) {
400 t->notifyProducerDisconnect(mSurfaceControl);
401 }
402
Robert Carr78c25dd2019-08-15 14:10:33 -0700403 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
404 incStrong((void*)transactionCallbackThunk);
405
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700406 Rect crop = computeCrop(bufferItem);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800407 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000408 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
409 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700410 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800411
Vishnu Nair1506b182021-02-22 14:35:15 -0800412 auto releaseBufferCallback =
413 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
414 std::placeholders::_1, std::placeholders::_2);
415 t->setBuffer(mSurfaceControl, buffer, releaseBufferCallback);
John Reck137069e2020-12-10 22:07:37 -0500416 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
417 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
418 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700419 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800420 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700421 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviw42026162021-04-16 15:46:12 -0500422 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700423
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000424 setMatrix(t, mLastBufferInfo);
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700425 t->setCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800426 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800427 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800428 if (!bufferItem.mIsAutoTimestamp) {
429 t->setDesiredPresentTime(bufferItem.mTimestamp);
430 }
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700431 t->setFrameNumber(mSurfaceControl, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700432
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000433 if (!mNextFrameTimelineInfoQueue.empty()) {
Ady Abraham8db10102021-03-15 17:19:23 -0700434 t->setFrameTimelineInfo(mNextFrameTimelineInfoQueue.front());
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000435 mNextFrameTimelineInfoQueue.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100436 }
437
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800438 if (mAutoRefresh != bufferItem.mAutoRefresh) {
439 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
440 mAutoRefresh = bufferItem.mAutoRefresh;
441 }
Vishnu Nairadf632b2021-01-07 14:05:08 -0800442 {
443 std::unique_lock _lock{mTimestampMutex};
444 auto dequeueTime = mDequeueTimestamps.find(buffer->getId());
445 if (dequeueTime != mDequeueTimestamps.end()) {
446 Parcel p;
447 p.writeInt64(dequeueTime->second);
448 t->setMetadata(mSurfaceControl, METADATA_DEQUEUE_TIME, p);
449 mDequeueTimestamps.erase(dequeueTime);
450 }
451 }
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800452
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800453 auto mergeTransaction =
454 [&t, currentFrameNumber = bufferItem.mFrameNumber](
455 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
456 auto& [targetFrameNumber, transaction] = pendingTransaction;
457 if (currentFrameNumber < targetFrameNumber) {
458 return false;
459 }
460 t->merge(std::move(transaction));
461 return true;
462 };
463
464 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
465 mPendingTransactions.end(), mergeTransaction),
466 mPendingTransactions.end());
467
Robert Carr78c25dd2019-08-15 14:10:33 -0700468 if (applyTransaction) {
Vishnu Nair277142c2021-01-05 18:35:29 -0800469 t->setApplyToken(mApplyToken).apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700470 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700471
472 BQA_LOGV("processNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800473 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
474 " graphicBufferId=%" PRIu64,
Vishnu Nairea0de002020-11-17 17:42:37 -0800475 mSize.width, mSize.height, bufferItem.mFrameNumber, toString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800476 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
477 static_cast<uint32_t>(mPendingTransactions.size()),
478 bufferItem.mGraphicBuffer->getId());
Robert Carr78c25dd2019-08-15 14:10:33 -0700479}
480
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800481Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
482 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800483 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800484 }
485 return item.mCrop;
486}
487
Vishnu Nairaef1de92020-10-22 12:15:53 -0700488void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Valerie Haua32c5522019-12-09 10:11:08 -0800489 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800490 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800491
Vishnu Nairdab94092020-09-29 16:09:04 -0700492 const bool nextTransactionSet = mNextTransaction != nullptr;
Vishnu Nair1506b182021-02-22 14:35:15 -0800493 if (nextTransactionSet) {
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800494 while (mNumFrameAvailable > 0 || maxBuffersAcquired(false /* includeExtraAcquire */)) {
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700495 BQA_LOGV("waiting in onFrameAvailable...");
Valerie Hau0188adf2020-02-13 08:29:20 -0800496 mCallbackCV.wait(_lock);
497 }
498 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800499 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800500 mNumFrameAvailable++;
Vishnu Nair1506b182021-02-22 14:35:15 -0800501
502 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " nextTransactionSet=%s", item.mFrameNumber,
503 toString(nextTransactionSet));
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800504 processNextBufferLocked(nextTransactionSet /* useNextTransaction */);
Valerie Haud3b90d22019-11-06 09:37:31 -0800505}
506
Vishnu Nairaef1de92020-10-22 12:15:53 -0700507void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
508 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
509 // Do nothing since we are not storing unacquired buffer items locally.
510}
511
Vishnu Nairadf632b2021-01-07 14:05:08 -0800512void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
513 std::unique_lock _lock{mTimestampMutex};
514 mDequeueTimestamps[bufferId] = systemTime();
515};
516
517void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
518 std::unique_lock _lock{mTimestampMutex};
519 mDequeueTimestamps.erase(bufferId);
520};
521
Robert Carr78c25dd2019-08-15 14:10:33 -0700522void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800523 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700524 mNextTransaction = t;
525}
526
Vishnu Nairea0de002020-11-17 17:42:37 -0800527bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700528 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Hongguang Chen33100282021-04-15 18:36:05 -0700529 mSize = mRequestedSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700530 // Only reject buffers if scaling mode is freeze.
531 return false;
532 }
533
Vishnu Naire1a42322020-10-02 17:42:04 -0700534 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
535 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
536
537 // Take the buffer's orientation into account
538 if (item.mTransform & ui::Transform::ROT_90) {
539 std::swap(bufWidth, bufHeight);
540 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800541 ui::Size bufferSize(bufWidth, bufHeight);
542 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
543 mSize = mRequestedSize;
544 return false;
545 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700546
Vishnu Nair670b3f72020-09-29 17:52:18 -0700547 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800548 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700549}
Vishnu Nairbf255772020-10-16 10:54:41 -0700550
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000551void BLASTBufferQueue::setMatrix(SurfaceComposerClient::Transaction* t,
552 const BufferInfo& bufferInfo) {
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700553 uint32_t bufWidth = bufferInfo.crop.getWidth();
554 uint32_t bufHeight = bufferInfo.crop.getHeight();
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000555
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700556 float sx = mSize.width / static_cast<float>(bufWidth);
557 float sy = mSize.height / static_cast<float>(bufHeight);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000558
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700559 t->setMatrix(mSurfaceControl, sx, 0, 0, sy);
560 // Update position based on crop.
561 t->setPosition(mSurfaceControl, bufferInfo.crop.left * sx * -1, bufferInfo.crop.top * sy * -1);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000562}
563
chaviw71c2cc42020-10-23 16:42:02 -0700564void BLASTBufferQueue::setTransactionCompleteCallback(
565 uint64_t frameNumber, std::function<void(int64_t)>&& transactionCompleteCallback) {
566 std::lock_guard _lock{mMutex};
567 if (transactionCompleteCallback == nullptr) {
568 mTransactionCompleteCallback = nullptr;
569 } else {
570 mTransactionCompleteCallback = std::move(transactionCompleteCallback);
571 mTransactionCompleteFrameNumber = frameNumber;
572 }
573}
574
Vishnu Nairbf255772020-10-16 10:54:41 -0700575// Check if we have acquired the maximum number of buffers.
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800576// Consumer can acquire an additional buffer if that buffer is not droppable. Set
577// includeExtraAcquire is true to include this buffer to the count. Since this depends on the state
578// of the buffer, the next acquire may return with NO_BUFFER_AVAILABLE.
579bool BLASTBufferQueue::maxBuffersAcquired(bool includeExtraAcquire) const {
580 int maxAcquiredBuffers = MAX_ACQUIRED_BUFFERS + (includeExtraAcquire ? 2 : 1);
Vishnu Nair1506b182021-02-22 14:35:15 -0800581 return mNumAcquired == maxAcquiredBuffers;
Vishnu Nairbf255772020-10-16 10:54:41 -0700582}
583
Robert Carr05086b22020-10-13 18:22:51 -0700584class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700585private:
586 sp<BLASTBufferQueue> mBbq;
Robert Carr05086b22020-10-13 18:22:51 -0700587public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700588 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
589 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
590 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700591
Robert Carr05086b22020-10-13 18:22:51 -0700592 void allocateBuffers() override {
593 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
594 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
595 auto gbp = getIGraphicBufferProducer();
596 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
597 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
598 gbp->allocateBuffers(reqWidth, reqHeight,
599 reqFormat, reqUsage);
600
601 }).detach();
602 }
Robert Carr9c006e02020-10-14 13:41:57 -0700603
Marin Shalamanovc5986772021-03-16 16:09:49 +0100604 status_t setFrameRate(float frameRate, int8_t compatibility,
605 int8_t changeFrameRateStrategy) override {
606 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
607 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700608 return BAD_VALUE;
609 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100610 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700611 }
Robert Carr9b611b72020-10-19 12:00:23 -0700612
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000613 status_t setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) override {
614 return mBbq->setFrameTimelineInfo(frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700615 }
Robert Carr82d07c92021-05-10 11:36:43 -0700616 protected:
617 uint32_t getTransformHint() const override {
618 if (mStickyTransform == 0 && !transformToDisplayInverse()) {
619 return mBbq->getLastTransformHint();
620 } else {
621 return 0;
622 }
623 }
Robert Carr05086b22020-10-13 18:22:51 -0700624};
625
Robert Carr9c006e02020-10-14 13:41:57 -0700626// TODO: Can we coalesce this with frame updates? Need to confirm
627// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200628status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
629 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700630 std::unique_lock _lock{mMutex};
631 SurfaceComposerClient::Transaction t;
632
Marin Shalamanov46084422020-10-13 12:33:42 +0200633 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700634}
635
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000636status_t BLASTBufferQueue::setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) {
Robert Carr9b611b72020-10-19 12:00:23 -0700637 std::unique_lock _lock{mMutex};
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000638 mNextFrameTimelineInfoQueue.push(frameTimelineInfo);
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100639 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700640}
641
Hongguang Chen621ec582021-02-16 15:42:35 -0800642void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
643 std::unique_lock _lock{mMutex};
644 SurfaceComposerClient::Transaction t;
645
646 t.setSidebandStream(mSurfaceControl, stream).apply();
647}
648
Vishnu Nair992496b2020-10-22 17:27:21 -0700649sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
650 std::unique_lock _lock{mMutex};
651 sp<IBinder> scHandle = nullptr;
652 if (includeSurfaceControlHandle && mSurfaceControl) {
653 scHandle = mSurfaceControl->getHandle();
654 }
655 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700656}
657
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800658void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
659 uint64_t frameNumber) {
660 std::lock_guard _lock{mMutex};
661 if (mLastAcquiredFrameNumber >= frameNumber) {
662 // Apply the transaction since we have already acquired the desired frame.
663 t->apply();
664 } else {
chaviwaad6cf52021-03-23 17:27:20 -0500665 mPendingTransactions.emplace_back(frameNumber, *t);
666 // Clear the transaction so it can't be applied elsewhere.
667 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800668 }
669}
670
Vishnu Nair89496122020-12-14 17:14:53 -0800671// Maintains a single worker thread per process that services a list of runnables.
672class AsyncWorker : public Singleton<AsyncWorker> {
673private:
674 std::thread mThread;
675 bool mDone = false;
676 std::deque<std::function<void()>> mRunnables;
677 std::mutex mMutex;
678 std::condition_variable mCv;
679 void run() {
680 std::unique_lock<std::mutex> lock(mMutex);
681 while (!mDone) {
682 mCv.wait(lock);
683 while (!mRunnables.empty()) {
684 std::function<void()> runnable = mRunnables.front();
685 mRunnables.pop_front();
686 runnable();
687 }
688 }
689 }
690
691public:
692 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
693
694 ~AsyncWorker() {
695 mDone = true;
696 mCv.notify_all();
697 if (mThread.joinable()) {
698 mThread.join();
699 }
700 }
701
702 void post(std::function<void()> runnable) {
703 std::unique_lock<std::mutex> lock(mMutex);
704 mRunnables.emplace_back(std::move(runnable));
705 mCv.notify_one();
706 }
707};
708ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
709
710// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
711class AsyncProducerListener : public BnProducerListener {
712private:
713 const sp<IProducerListener> mListener;
714
715public:
716 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
717
718 void onBufferReleased() override {
719 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
720 }
721
722 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
723 AsyncWorker::getInstance().post(
724 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
725 }
726};
727
728// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
729// can be non-blocking when the producer is in the client process.
730class BBQBufferQueueProducer : public BufferQueueProducer {
731public:
732 BBQBufferQueueProducer(const sp<BufferQueueCore>& core)
733 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/) {}
734
735 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
736 QueueBufferOutput* output) override {
737 if (!listener) {
738 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
739 }
740
741 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
742 producerControlledByApp, output);
743 }
Vishnu Nair17dde612020-12-28 11:39:59 -0800744
745 int query(int what, int* value) override {
746 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
747 *value = 1;
748 return NO_ERROR;
749 }
750 return BufferQueueProducer::query(what, value);
751 }
Vishnu Nair89496122020-12-14 17:14:53 -0800752};
753
754// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
755// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
756// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
757// we can deadlock.
758void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
759 sp<IGraphicBufferConsumer>* outConsumer) {
760 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
761 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
762
763 sp<BufferQueueCore> core(new BufferQueueCore());
764 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
765
766 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core));
767 LOG_ALWAYS_FATAL_IF(producer == nullptr,
768 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
769
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800770 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
771 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -0800772 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
773 "BLASTBufferQueue: failed to create BufferQueueConsumer");
774
775 *outProducer = producer;
776 *outConsumer = consumer;
777}
778
chaviw497e81c2021-02-04 17:09:47 -0800779PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
780 PixelFormat convertedFormat = format;
781 switch (format) {
782 case PIXEL_FORMAT_TRANSPARENT:
783 case PIXEL_FORMAT_TRANSLUCENT:
784 convertedFormat = PIXEL_FORMAT_RGBA_8888;
785 break;
786 case PIXEL_FORMAT_OPAQUE:
787 convertedFormat = PIXEL_FORMAT_RGBX_8888;
788 break;
789 }
790 return convertedFormat;
791}
792
Robert Carr82d07c92021-05-10 11:36:43 -0700793uint32_t BLASTBufferQueue::getLastTransformHint() const {
794 if (mSurfaceControl != nullptr) {
795 return mSurfaceControl->getTransformHint();
796 } else {
797 return 0;
798 }
799}
800
Robert Carr78c25dd2019-08-15 14:10:33 -0700801} // namespace android