blob: 802448253190473ef0cef38350f37fd20592008e [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>
Valerie Haua32c5522019-12-09 10:11:08 -080032#include <utils/Trace.h>
33
Ady Abraham0bde6b52021-05-18 13:57:02 -070034#include <private/gui/ComposerService.h>
35
Robert Carr78c25dd2019-08-15 14:10:33 -070036#include <chrono>
37
38using namespace std::chrono_literals;
39
Vishnu Nairdab94092020-09-29 16:09:04 -070040namespace {
41inline const char* toString(bool b) {
42 return b ? "true" : "false";
43}
44} // namespace
45
Robert Carr78c25dd2019-08-15 14:10:33 -070046namespace android {
47
Vishnu Nairdab94092020-09-29 16:09:04 -070048// Macros to include adapter info in log messages
49#define BQA_LOGV(x, ...) \
50 ALOGV("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairc6f89ee2020-12-11 14:27:32 -080051// enable logs for a single layer
52//#define BQA_LOGV(x, ...) \
53// ALOGV_IF((strstr(mName.c_str(), "SurfaceView") != nullptr), "[%s](f:%u,a:%u) " x, \
54// mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070055#define BQA_LOGE(x, ...) \
56 ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
57
Valerie Hau871d6352020-01-29 08:44:02 -080058void BLASTBufferItemConsumer::onDisconnect() {
Hongguang Chen621ec582021-02-16 15:42:35 -080059 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080060 mPreviouslyConnected = mCurrentlyConnected;
61 mCurrentlyConnected = false;
62 if (mPreviouslyConnected) {
63 mDisconnectEvents.push(mCurrentFrameNumber);
64 }
65 mFrameEventHistory.onDisconnect();
66}
67
68void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
69 FrameEventHistoryDelta* outDelta) {
Hongguang Chen621ec582021-02-16 15:42:35 -080070 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080071 if (newTimestamps) {
72 // BufferQueueProducer only adds a new timestamp on
73 // queueBuffer
74 mCurrentFrameNumber = newTimestamps->frameNumber;
75 mFrameEventHistory.addQueue(*newTimestamps);
76 }
77 if (outDelta) {
78 // frame event histories will be processed
79 // only after the producer connects and requests
80 // deltas for the first time. Forward this intent
81 // to SF-side to turn event processing back on
82 mPreviouslyConnected = mCurrentlyConnected;
83 mCurrentlyConnected = true;
84 mFrameEventHistory.getAndResetDelta(outDelta);
85 }
86}
87
88void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
89 const sp<Fence>& glDoneFence,
90 const sp<Fence>& presentFence,
91 const sp<Fence>& prevReleaseFence,
92 CompositorTiming compositorTiming,
93 nsecs_t latchTime, nsecs_t dequeueReadyTime) {
Hongguang Chen621ec582021-02-16 15:42:35 -080094 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -080095
96 // if the producer is not connected, don't bother updating,
97 // the next producer that connects won't access this frame event
98 if (!mCurrentlyConnected) return;
99 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
100 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
101 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
102
103 mFrameEventHistory.addLatch(frameNumber, latchTime);
104 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
105 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
106 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
107 compositorTiming);
108}
109
110void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
111 bool disconnect = false;
Hongguang Chen621ec582021-02-16 15:42:35 -0800112 Mutex::Autolock lock(mMutex);
Valerie Hau871d6352020-01-29 08:44:02 -0800113 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
114 disconnect = true;
115 mDisconnectEvents.pop();
116 }
117 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
118}
119
Hongguang Chen621ec582021-02-16 15:42:35 -0800120void BLASTBufferItemConsumer::setBlastBufferQueue(BLASTBufferQueue* blastbufferqueue) {
121 Mutex::Autolock lock(mMutex);
122 mBLASTBufferQueue = blastbufferqueue;
123}
124
125void BLASTBufferItemConsumer::onSidebandStreamChanged() {
126 Mutex::Autolock lock(mMutex);
127 if (mBLASTBufferQueue != nullptr) {
128 sp<NativeHandle> stream = getSidebandStream();
129 mBLASTBufferQueue->setSidebandStream(stream);
130 }
131}
132
Vishnu Nairdab94092020-09-29 16:09:04 -0700133BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface,
Vishnu Nairdebd1cb2021-03-16 10:06:01 -0700134 int width, int height, int32_t format)
Vishnu Nairdab94092020-09-29 16:09:04 -0700135 : mName(name),
136 mSurfaceControl(surface),
Vishnu Nairea0de002020-11-17 17:42:37 -0800137 mSize(width, height),
138 mRequestedSize(mSize),
chaviw565ee542021-01-14 10:21:23 -0800139 mFormat(format),
Valerie Haud3b90d22019-11-06 09:37:31 -0800140 mNextTransaction(nullptr) {
Vishnu Nair89496122020-12-14 17:14:53 -0800141 createBufferQueue(&mProducer, &mConsumer);
Valerie Hau0889c622020-02-19 15:04:47 -0800142 // since the adapter is in the client process, set dequeue timeout
143 // explicitly so that dequeueBuffer will block
144 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
Valerie Hau65b8e872020-02-13 09:45:14 -0800145
Vishnu Nairdebd1cb2021-03-16 10:06:01 -0700146 // safe default, most producers are expected to override this
147 mProducer->setMaxDequeuedBufferCount(2);
Vishnu Nair1618c672021-02-05 13:08:26 -0800148 mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
149 GraphicBuffer::USAGE_HW_COMPOSER |
150 GraphicBuffer::USAGE_HW_TEXTURE,
151 1, false);
Valerie Haua32c5522019-12-09 10:11:08 -0800152 static int32_t id = 0;
Vishnu Nairdab94092020-09-29 16:09:04 -0700153 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(id);
Valerie Haua32c5522019-12-09 10:11:08 -0800154 id++;
Vishnu Nairdab94092020-09-29 16:09:04 -0700155 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700156 mBufferItemConsumer->setFrameAvailableListener(this);
157 mBufferItemConsumer->setBufferFreedListener(this);
Vishnu Nairea0de002020-11-17 17:42:37 -0800158 mBufferItemConsumer->setDefaultBufferSize(mSize.width, mSize.height);
chaviw497e81c2021-02-04 17:09:47 -0800159 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
Hongguang Chen621ec582021-02-16 15:42:35 -0800160 mBufferItemConsumer->setBlastBufferQueue(this);
Robert Carr9f133d72020-04-01 15:51:46 -0700161
Ady Abraham0bde6b52021-05-18 13:57:02 -0700162 int extraBufferCount = 0;
163 ComposerService::getComposerService()->getExtraBufferCount(&extraBufferCount);
164 mMaxAcquiredBuffers = 1 + extraBufferCount;
165 mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxAcquiredBuffers);
166
Valerie Hau2882e982020-01-23 13:33:10 -0800167 mTransformHint = mSurfaceControl->getTransformHint();
Robert Carr9f133d72020-04-01 15:51:46 -0700168 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800169 SurfaceComposerClient::Transaction()
Robert Carr5b3b9142021-02-22 12:27:32 -0800170 .setFlags(surface, layer_state_t::eEnableBackpressure,
171 layer_state_t::eEnableBackpressure)
172 .apply();
Valerie Haud3b90d22019-11-06 09:37:31 -0800173
Valerie Haua32c5522019-12-09 10:11:08 -0800174 mNumAcquired = 0;
175 mNumFrameAvailable = 0;
Robert Carr78c25dd2019-08-15 14:10:33 -0700176}
177
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800178BLASTBufferQueue::~BLASTBufferQueue() {
Hongguang Chen621ec582021-02-16 15:42:35 -0800179 mBufferItemConsumer->setBlastBufferQueue(nullptr);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800180 if (mPendingTransactions.empty()) {
181 return;
182 }
183 BQA_LOGE("Applying pending transactions on dtor %d",
184 static_cast<uint32_t>(mPendingTransactions.size()));
185 SurfaceComposerClient::Transaction t;
186 for (auto& [targetFrameNumber, transaction] : mPendingTransactions) {
187 t.merge(std::move(transaction));
188 }
189 t.apply();
190}
191
chaviw565ee542021-01-14 10:21:23 -0800192void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height,
193 int32_t format) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700194 std::unique_lock _lock{mMutex};
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700195 BQA_LOGV("update width=%d height=%d format=%d", width, height, format);
chaviw565ee542021-01-14 10:21:23 -0800196 if (mFormat != format) {
197 mFormat = format;
chaviw497e81c2021-02-04 17:09:47 -0800198 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
chaviw565ee542021-01-14 10:21:23 -0800199 }
200
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800201 SurfaceComposerClient::Transaction t;
202 bool applyTransaction = false;
203 if (!SurfaceControl::isSameSurface(mSurfaceControl, surface)) {
204 mSurfaceControl = surface;
205 t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure,
206 layer_state_t::eEnableBackpressure);
207 applyTransaction = true;
208 }
209
Vishnu Nairea0de002020-11-17 17:42:37 -0800210 ui::Size newSize(width, height);
211 if (mRequestedSize != newSize) {
212 mRequestedSize.set(newSize);
213 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000214 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800215 // If the buffer supports scaling, update the frame immediately since the client may
216 // want to scale the existing buffer to the new size.
217 mSize = mRequestedSize;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000218 // We only need to update the scale if we've received at least one buffer. The reason
219 // for this is the scale is calculated based on the requested size and buffer size.
220 // If there's no buffer, the scale will always be 1.
221 if (mLastBufferInfo.hasBuffer) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700222 t.setDestinationFrame(mSurfaceControl,
223 Rect(0, 0, newSize.getWidth(), newSize.getHeight()));
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000224 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800225 applyTransaction = true;
Vishnu Nair53c936c2020-12-03 11:46:37 -0800226 }
Robert Carrfc416512020-04-02 12:32:44 -0700227 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800228 if (applyTransaction) {
229 t.apply();
230 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700231}
232
233static void transactionCallbackThunk(void* context, nsecs_t latchTime,
234 const sp<Fence>& presentFence,
235 const std::vector<SurfaceControlStats>& stats) {
236 if (context == nullptr) {
237 return;
238 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800239 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700240 bq->transactionCallback(latchTime, presentFence, stats);
241}
242
243void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
244 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700245 std::function<void(int64_t)> transactionCompleteCallback = nullptr;
246 uint64_t currFrameNumber = 0;
Vishnu Nairdab94092020-09-29 16:09:04 -0700247
chaviw71c2cc42020-10-23 16:42:02 -0700248 {
249 std::unique_lock _lock{mMutex};
250 ATRACE_CALL();
251 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700252
chaviw42026162021-04-16 15:46:12 -0500253 if (!mSurfaceControlsWithPendingCallback.empty()) {
254 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
255 mSurfaceControlsWithPendingCallback.pop();
256 bool found = false;
257 for (auto stat : stats) {
258 if (!SurfaceControl::isSameSurface(pendingSC, stat.surfaceControl)) {
259 continue;
Vishnu Nair1506b182021-02-22 14:35:15 -0800260 }
chaviw42026162021-04-16 15:46:12 -0500261
262 mTransformHint = stat.transformHint;
263 mBufferItemConsumer->setTransformHint(mTransformHint);
264 mBufferItemConsumer
265 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
266 stat.frameEventStats.refreshStartTime,
267 stat.frameEventStats.gpuCompositionDoneFence,
268 stat.presentFence, stat.previousReleaseFence,
269 stat.frameEventStats.compositorTiming,
270 stat.latchTime,
271 stat.frameEventStats.dequeueReadyTime);
272
273 currFrameNumber = stat.frameEventStats.frameNumber;
274
275 if (mTransactionCompleteCallback &&
276 currFrameNumber >= mTransactionCompleteFrameNumber) {
277 if (currFrameNumber > mTransactionCompleteFrameNumber) {
278 BQA_LOGE("transactionCallback received for a newer framenumber=%" PRIu64
279 " than expected=%" PRIu64,
280 currFrameNumber, mTransactionCompleteFrameNumber);
281 }
282 transactionCompleteCallback = std::move(mTransactionCompleteCallback);
283 mTransactionCompleteFrameNumber = 0;
284 }
285
286 found = true;
287 break;
chaviw71c2cc42020-10-23 16:42:02 -0700288 }
chaviw42026162021-04-16 15:46:12 -0500289
290 if (!found) {
291 BQA_LOGE("Failed to find matching SurfaceControl in transaction callback");
292 }
293 } else {
294 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
295 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800296 }
chaviw71c2cc42020-10-23 16:42:02 -0700297
chaviw71c2cc42020-10-23 16:42:02 -0700298 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700299 }
Valerie Haua32c5522019-12-09 10:11:08 -0800300
chaviw71c2cc42020-10-23 16:42:02 -0700301 if (transactionCompleteCallback) {
302 transactionCompleteCallback(currFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800303 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700304}
305
Vishnu Nair1506b182021-02-22 14:35:15 -0800306// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
307// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
308// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
309// Otherwise, this is a no-op.
310static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, uint64_t graphicBufferId,
Robert Carr97e7cc02021-06-07 10:45:40 -0700311 const sp<Fence>& releaseFence, uint32_t transformHint) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800312 sp<BLASTBufferQueue> blastBufferQueue = context.promote();
313 ALOGV("releaseBufferCallbackThunk graphicBufferId=%" PRIu64 " blastBufferQueue=%s",
314 graphicBufferId, blastBufferQueue ? "alive" : "dead");
315 if (blastBufferQueue) {
Robert Carr97e7cc02021-06-07 10:45:40 -0700316 blastBufferQueue->releaseBufferCallback(graphicBufferId, releaseFence, transformHint);
Vishnu Nair1506b182021-02-22 14:35:15 -0800317 }
318}
319
320void BLASTBufferQueue::releaseBufferCallback(uint64_t graphicBufferId,
Robert Carr97e7cc02021-06-07 10:45:40 -0700321 const sp<Fence>& releaseFence,
322 uint32_t transformHint) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800323 ATRACE_CALL();
324 std::unique_lock _lock{mMutex};
325 BQA_LOGV("releaseBufferCallback graphicBufferId=%" PRIu64, graphicBufferId);
326
Robert Carr82d07c92021-05-10 11:36:43 -0700327 if (mSurfaceControl != nullptr) {
Robert Carr97e7cc02021-06-07 10:45:40 -0700328 mTransformHint = transformHint;
329 mSurfaceControl->setTransformHint(transformHint);
Robert Carr82d07c92021-05-10 11:36:43 -0700330 mBufferItemConsumer->setTransformHint(mTransformHint);
331 }
332
Vishnu Nair1506b182021-02-22 14:35:15 -0800333 auto it = mSubmitted.find(graphicBufferId);
334 if (it == mSubmitted.end()) {
335 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %" PRIu64,
336 graphicBufferId);
337 return;
338 }
339
340 mBufferItemConsumer->releaseBuffer(it->second, releaseFence);
341 mSubmitted.erase(it);
342 mNumAcquired--;
343 processNextBufferLocked(false /* useNextTransaction */);
344 mCallbackCV.notify_all();
345}
346
Robert Carr255acdc2020-04-17 14:08:55 -0700347void BLASTBufferQueue::processNextBufferLocked(bool useNextTransaction) {
Valerie Haua32c5522019-12-09 10:11:08 -0800348 ATRACE_CALL();
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800349 // If the next transaction is set, we want to guarantee the our acquire will not fail, so don't
350 // include the extra buffer when checking if we can acquire the next buffer.
351 const bool includeExtraAcquire = !useNextTransaction;
352 if (mNumFrameAvailable == 0 || maxBuffersAcquired(includeExtraAcquire)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800353 mCallbackCV.notify_all();
Valerie Haud3b90d22019-11-06 09:37:31 -0800354 return;
355 }
356
Valerie Haua32c5522019-12-09 10:11:08 -0800357 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700358 BQA_LOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800359 return;
360 }
361
Robert Carr78c25dd2019-08-15 14:10:33 -0700362 SurfaceComposerClient::Transaction localTransaction;
363 bool applyTransaction = true;
364 SurfaceComposerClient::Transaction* t = &localTransaction;
Robert Carr255acdc2020-04-17 14:08:55 -0700365 if (mNextTransaction != nullptr && useNextTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700366 t = mNextTransaction;
367 mNextTransaction = nullptr;
368 applyTransaction = false;
369 }
370
Valerie Haua32c5522019-12-09 10:11:08 -0800371 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800372
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800373 status_t status =
374 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800375 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
376 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
377 return;
378 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700379 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Robert Carr78c25dd2019-08-15 14:10:33 -0700380 return;
381 }
Valerie Haua32c5522019-12-09 10:11:08 -0800382 auto buffer = bufferItem.mGraphicBuffer;
383 mNumFrameAvailable--;
384
385 if (buffer == nullptr) {
386 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700387 BQA_LOGE("Buffer was empty");
Valerie Haua32c5522019-12-09 10:11:08 -0800388 return;
389 }
390
Vishnu Nair670b3f72020-09-29 17:52:18 -0700391 if (rejectBuffer(bufferItem)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800392 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d"
393 "buffer{size=%dx%d transform=%d}",
394 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
395 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
396 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
397 processNextBufferLocked(useNextTransaction);
398 return;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700399 }
400
Valerie Haua32c5522019-12-09 10:11:08 -0800401 mNumAcquired++;
Vishnu Nair1506b182021-02-22 14:35:15 -0800402 mSubmitted[buffer->getId()] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700403
Valerie Hau871d6352020-01-29 08:44:02 -0800404 bool needsDisconnect = false;
405 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
406
407 // if producer disconnected before, notify SurfaceFlinger
408 if (needsDisconnect) {
409 t->notifyProducerDisconnect(mSurfaceControl);
410 }
411
Robert Carr78c25dd2019-08-15 14:10:33 -0700412 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
413 incStrong((void*)transactionCallbackThunk);
414
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700415 Rect crop = computeCrop(bufferItem);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800416 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000417 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
418 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700419 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800420
Vishnu Nair1506b182021-02-22 14:35:15 -0800421 auto releaseBufferCallback =
422 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
Robert Carr97e7cc02021-06-07 10:45:40 -0700423 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
Vishnu Nair1506b182021-02-22 14:35:15 -0800424 t->setBuffer(mSurfaceControl, buffer, releaseBufferCallback);
John Reck137069e2020-12-10 22:07:37 -0500425 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
426 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
427 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700428 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800429 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700430 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviw42026162021-04-16 15:46:12 -0500431 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700432
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700433 t->setDestinationFrame(mSurfaceControl, Rect(0, 0, mSize.getWidth(), mSize.getHeight()));
434 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800435 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800436 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800437 if (!bufferItem.mIsAutoTimestamp) {
438 t->setDesiredPresentTime(bufferItem.mTimestamp);
439 }
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700440 t->setFrameNumber(mSurfaceControl, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700441
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000442 if (!mNextFrameTimelineInfoQueue.empty()) {
Ady Abraham8db10102021-03-15 17:19:23 -0700443 t->setFrameTimelineInfo(mNextFrameTimelineInfoQueue.front());
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000444 mNextFrameTimelineInfoQueue.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100445 }
446
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800447 if (mAutoRefresh != bufferItem.mAutoRefresh) {
448 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
449 mAutoRefresh = bufferItem.mAutoRefresh;
450 }
Vishnu Nairadf632b2021-01-07 14:05:08 -0800451 {
452 std::unique_lock _lock{mTimestampMutex};
453 auto dequeueTime = mDequeueTimestamps.find(buffer->getId());
454 if (dequeueTime != mDequeueTimestamps.end()) {
455 Parcel p;
456 p.writeInt64(dequeueTime->second);
457 t->setMetadata(mSurfaceControl, METADATA_DEQUEUE_TIME, p);
458 mDequeueTimestamps.erase(dequeueTime);
459 }
460 }
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800461
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800462 auto mergeTransaction =
463 [&t, currentFrameNumber = bufferItem.mFrameNumber](
464 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
465 auto& [targetFrameNumber, transaction] = pendingTransaction;
466 if (currentFrameNumber < targetFrameNumber) {
467 return false;
468 }
469 t->merge(std::move(transaction));
470 return true;
471 };
472
473 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
474 mPendingTransactions.end(), mergeTransaction),
475 mPendingTransactions.end());
476
Robert Carr78c25dd2019-08-15 14:10:33 -0700477 if (applyTransaction) {
Vishnu Nair277142c2021-01-05 18:35:29 -0800478 t->setApplyToken(mApplyToken).apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700479 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700480
481 BQA_LOGV("processNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800482 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
483 " graphicBufferId=%" PRIu64,
Vishnu Nairea0de002020-11-17 17:42:37 -0800484 mSize.width, mSize.height, bufferItem.mFrameNumber, toString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800485 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
486 static_cast<uint32_t>(mPendingTransactions.size()),
487 bufferItem.mGraphicBuffer->getId());
Robert Carr78c25dd2019-08-15 14:10:33 -0700488}
489
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800490Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
491 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800492 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800493 }
494 return item.mCrop;
495}
496
Vishnu Nairaef1de92020-10-22 12:15:53 -0700497void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Valerie Haua32c5522019-12-09 10:11:08 -0800498 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800499 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800500
Vishnu Nairdab94092020-09-29 16:09:04 -0700501 const bool nextTransactionSet = mNextTransaction != nullptr;
Vishnu Nair1506b182021-02-22 14:35:15 -0800502 if (nextTransactionSet) {
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800503 while (mNumFrameAvailable > 0 || maxBuffersAcquired(false /* includeExtraAcquire */)) {
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700504 BQA_LOGV("waiting in onFrameAvailable...");
Valerie Hau0188adf2020-02-13 08:29:20 -0800505 mCallbackCV.wait(_lock);
506 }
507 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800508 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800509 mNumFrameAvailable++;
Vishnu Nair1506b182021-02-22 14:35:15 -0800510
511 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " nextTransactionSet=%s", item.mFrameNumber,
512 toString(nextTransactionSet));
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800513 processNextBufferLocked(nextTransactionSet /* useNextTransaction */);
Valerie Haud3b90d22019-11-06 09:37:31 -0800514}
515
Vishnu Nairaef1de92020-10-22 12:15:53 -0700516void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
517 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
518 // Do nothing since we are not storing unacquired buffer items locally.
519}
520
Vishnu Nairadf632b2021-01-07 14:05:08 -0800521void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
522 std::unique_lock _lock{mTimestampMutex};
523 mDequeueTimestamps[bufferId] = systemTime();
524};
525
526void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
527 std::unique_lock _lock{mTimestampMutex};
528 mDequeueTimestamps.erase(bufferId);
529};
530
Robert Carr78c25dd2019-08-15 14:10:33 -0700531void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800532 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700533 mNextTransaction = t;
534}
535
Vishnu Nairea0de002020-11-17 17:42:37 -0800536bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700537 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Hongguang Chen33100282021-04-15 18:36:05 -0700538 mSize = mRequestedSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700539 // Only reject buffers if scaling mode is freeze.
540 return false;
541 }
542
Vishnu Naire1a42322020-10-02 17:42:04 -0700543 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
544 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
545
546 // Take the buffer's orientation into account
547 if (item.mTransform & ui::Transform::ROT_90) {
548 std::swap(bufWidth, bufHeight);
549 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800550 ui::Size bufferSize(bufWidth, bufHeight);
551 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
552 mSize = mRequestedSize;
553 return false;
554 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700555
Vishnu Nair670b3f72020-09-29 17:52:18 -0700556 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800557 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700558}
Vishnu Nairbf255772020-10-16 10:54:41 -0700559
chaviw71c2cc42020-10-23 16:42:02 -0700560void BLASTBufferQueue::setTransactionCompleteCallback(
561 uint64_t frameNumber, std::function<void(int64_t)>&& transactionCompleteCallback) {
562 std::lock_guard _lock{mMutex};
563 if (transactionCompleteCallback == nullptr) {
564 mTransactionCompleteCallback = nullptr;
565 } else {
566 mTransactionCompleteCallback = std::move(transactionCompleteCallback);
567 mTransactionCompleteFrameNumber = frameNumber;
568 }
569}
570
Vishnu Nairbf255772020-10-16 10:54:41 -0700571// Check if we have acquired the maximum number of buffers.
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800572// Consumer can acquire an additional buffer if that buffer is not droppable. Set
573// includeExtraAcquire is true to include this buffer to the count. Since this depends on the state
574// of the buffer, the next acquire may return with NO_BUFFER_AVAILABLE.
575bool BLASTBufferQueue::maxBuffersAcquired(bool includeExtraAcquire) const {
Ady Abraham0bde6b52021-05-18 13:57:02 -0700576 int maxAcquiredBuffers = mMaxAcquiredBuffers + (includeExtraAcquire ? 2 : 1);
Vishnu Nair1506b182021-02-22 14:35:15 -0800577 return mNumAcquired == maxAcquiredBuffers;
Vishnu Nairbf255772020-10-16 10:54:41 -0700578}
579
Robert Carr05086b22020-10-13 18:22:51 -0700580class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700581private:
582 sp<BLASTBufferQueue> mBbq;
Robert Carr05086b22020-10-13 18:22:51 -0700583public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700584 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
585 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
586 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700587
Robert Carr05086b22020-10-13 18:22:51 -0700588 void allocateBuffers() override {
589 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
590 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
591 auto gbp = getIGraphicBufferProducer();
592 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
593 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
594 gbp->allocateBuffers(reqWidth, reqHeight,
595 reqFormat, reqUsage);
596
597 }).detach();
598 }
Robert Carr9c006e02020-10-14 13:41:57 -0700599
Marin Shalamanovc5986772021-03-16 16:09:49 +0100600 status_t setFrameRate(float frameRate, int8_t compatibility,
601 int8_t changeFrameRateStrategy) override {
602 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
603 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700604 return BAD_VALUE;
605 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100606 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700607 }
Robert Carr9b611b72020-10-19 12:00:23 -0700608
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000609 status_t setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) override {
610 return mBbq->setFrameTimelineInfo(frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700611 }
Robert Carr82d07c92021-05-10 11:36:43 -0700612 protected:
613 uint32_t getTransformHint() const override {
614 if (mStickyTransform == 0 && !transformToDisplayInverse()) {
615 return mBbq->getLastTransformHint();
616 } else {
617 return 0;
618 }
619 }
Robert Carr05086b22020-10-13 18:22:51 -0700620};
621
Robert Carr9c006e02020-10-14 13:41:57 -0700622// TODO: Can we coalesce this with frame updates? Need to confirm
623// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200624status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
625 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700626 std::unique_lock _lock{mMutex};
627 SurfaceComposerClient::Transaction t;
628
Marin Shalamanov46084422020-10-13 12:33:42 +0200629 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700630}
631
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000632status_t BLASTBufferQueue::setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) {
Robert Carr9b611b72020-10-19 12:00:23 -0700633 std::unique_lock _lock{mMutex};
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000634 mNextFrameTimelineInfoQueue.push(frameTimelineInfo);
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100635 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700636}
637
Hongguang Chen621ec582021-02-16 15:42:35 -0800638void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
639 std::unique_lock _lock{mMutex};
640 SurfaceComposerClient::Transaction t;
641
642 t.setSidebandStream(mSurfaceControl, stream).apply();
643}
644
Vishnu Nair992496b2020-10-22 17:27:21 -0700645sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
646 std::unique_lock _lock{mMutex};
647 sp<IBinder> scHandle = nullptr;
648 if (includeSurfaceControlHandle && mSurfaceControl) {
649 scHandle = mSurfaceControl->getHandle();
650 }
651 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700652}
653
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800654void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
655 uint64_t frameNumber) {
656 std::lock_guard _lock{mMutex};
657 if (mLastAcquiredFrameNumber >= frameNumber) {
658 // Apply the transaction since we have already acquired the desired frame.
659 t->apply();
660 } else {
chaviwaad6cf52021-03-23 17:27:20 -0500661 mPendingTransactions.emplace_back(frameNumber, *t);
662 // Clear the transaction so it can't be applied elsewhere.
663 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800664 }
665}
666
Vishnu Nair89496122020-12-14 17:14:53 -0800667// Maintains a single worker thread per process that services a list of runnables.
668class AsyncWorker : public Singleton<AsyncWorker> {
669private:
670 std::thread mThread;
671 bool mDone = false;
672 std::deque<std::function<void()>> mRunnables;
673 std::mutex mMutex;
674 std::condition_variable mCv;
675 void run() {
676 std::unique_lock<std::mutex> lock(mMutex);
677 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -0800678 while (!mRunnables.empty()) {
679 std::function<void()> runnable = mRunnables.front();
680 mRunnables.pop_front();
681 runnable();
682 }
Wonsik Kim567533e2021-05-04 19:31:29 -0700683 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -0800684 }
685 }
686
687public:
688 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
689
690 ~AsyncWorker() {
691 mDone = true;
692 mCv.notify_all();
693 if (mThread.joinable()) {
694 mThread.join();
695 }
696 }
697
698 void post(std::function<void()> runnable) {
699 std::unique_lock<std::mutex> lock(mMutex);
700 mRunnables.emplace_back(std::move(runnable));
701 mCv.notify_one();
702 }
703};
704ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
705
706// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
707class AsyncProducerListener : public BnProducerListener {
708private:
709 const sp<IProducerListener> mListener;
710
711public:
712 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
713
714 void onBufferReleased() override {
715 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
716 }
717
718 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
719 AsyncWorker::getInstance().post(
720 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
721 }
722};
723
724// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
725// can be non-blocking when the producer is in the client process.
726class BBQBufferQueueProducer : public BufferQueueProducer {
727public:
728 BBQBufferQueueProducer(const sp<BufferQueueCore>& core)
729 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/) {}
730
731 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
732 QueueBufferOutput* output) override {
733 if (!listener) {
734 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
735 }
736
737 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
738 producerControlledByApp, output);
739 }
Vishnu Nair17dde612020-12-28 11:39:59 -0800740
741 int query(int what, int* value) override {
742 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
743 *value = 1;
744 return NO_ERROR;
745 }
746 return BufferQueueProducer::query(what, value);
747 }
Vishnu Nair89496122020-12-14 17:14:53 -0800748};
749
750// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
751// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
752// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
753// we can deadlock.
754void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
755 sp<IGraphicBufferConsumer>* outConsumer) {
756 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
757 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
758
759 sp<BufferQueueCore> core(new BufferQueueCore());
760 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
761
762 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core));
763 LOG_ALWAYS_FATAL_IF(producer == nullptr,
764 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
765
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800766 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
767 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -0800768 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
769 "BLASTBufferQueue: failed to create BufferQueueConsumer");
770
771 *outProducer = producer;
772 *outConsumer = consumer;
773}
774
chaviw497e81c2021-02-04 17:09:47 -0800775PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
776 PixelFormat convertedFormat = format;
777 switch (format) {
778 case PIXEL_FORMAT_TRANSPARENT:
779 case PIXEL_FORMAT_TRANSLUCENT:
780 convertedFormat = PIXEL_FORMAT_RGBA_8888;
781 break;
782 case PIXEL_FORMAT_OPAQUE:
783 convertedFormat = PIXEL_FORMAT_RGBX_8888;
784 break;
785 }
786 return convertedFormat;
787}
788
Robert Carr82d07c92021-05-10 11:36:43 -0700789uint32_t BLASTBufferQueue::getLastTransformHint() const {
790 if (mSurfaceControl != nullptr) {
791 return mSurfaceControl->getTransformHint();
792 } else {
793 return 0;
794 }
795}
796
Robert Carr78c25dd2019-08-15 14:10:33 -0700797} // namespace android