blob: 52d3fa33f0ea46ad4d66ebab882d0d1726fc016e [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 Abraham899dcdb2021-06-15 16:56:21 -0700162 ComposerService::getComposerService()->getMaxAcquiredBufferCount(&mMaxAcquiredBuffers);
Ady Abraham0bde6b52021-05-18 13:57:02 -0700163 mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxAcquiredBuffers);
164
Valerie Hau2882e982020-01-23 13:33:10 -0800165 mTransformHint = mSurfaceControl->getTransformHint();
Robert Carr9f133d72020-04-01 15:51:46 -0700166 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800167 SurfaceComposerClient::Transaction()
Robert Carr5b3b9142021-02-22 12:27:32 -0800168 .setFlags(surface, layer_state_t::eEnableBackpressure,
169 layer_state_t::eEnableBackpressure)
170 .apply();
Valerie Haud3b90d22019-11-06 09:37:31 -0800171
Valerie Haua32c5522019-12-09 10:11:08 -0800172 mNumAcquired = 0;
173 mNumFrameAvailable = 0;
Robert Carr78c25dd2019-08-15 14:10:33 -0700174}
175
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800176BLASTBufferQueue::~BLASTBufferQueue() {
Hongguang Chen621ec582021-02-16 15:42:35 -0800177 mBufferItemConsumer->setBlastBufferQueue(nullptr);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800178 if (mPendingTransactions.empty()) {
179 return;
180 }
181 BQA_LOGE("Applying pending transactions on dtor %d",
182 static_cast<uint32_t>(mPendingTransactions.size()));
183 SurfaceComposerClient::Transaction t;
184 for (auto& [targetFrameNumber, transaction] : mPendingTransactions) {
185 t.merge(std::move(transaction));
186 }
187 t.apply();
188}
189
chaviw565ee542021-01-14 10:21:23 -0800190void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height,
191 int32_t format) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700192 std::unique_lock _lock{mMutex};
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700193 BQA_LOGV("update width=%d height=%d format=%d", width, height, format);
chaviw565ee542021-01-14 10:21:23 -0800194 if (mFormat != format) {
195 mFormat = format;
chaviw497e81c2021-02-04 17:09:47 -0800196 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
chaviw565ee542021-01-14 10:21:23 -0800197 }
198
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800199 SurfaceComposerClient::Transaction t;
200 bool applyTransaction = false;
201 if (!SurfaceControl::isSameSurface(mSurfaceControl, surface)) {
202 mSurfaceControl = surface;
203 t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure,
204 layer_state_t::eEnableBackpressure);
205 applyTransaction = true;
206 }
207
Vishnu Nairea0de002020-11-17 17:42:37 -0800208 ui::Size newSize(width, height);
209 if (mRequestedSize != newSize) {
210 mRequestedSize.set(newSize);
211 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000212 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800213 // If the buffer supports scaling, update the frame immediately since the client may
214 // want to scale the existing buffer to the new size.
215 mSize = mRequestedSize;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000216 // We only need to update the scale if we've received at least one buffer. The reason
217 // for this is the scale is calculated based on the requested size and buffer size.
218 // If there's no buffer, the scale will always be 1.
219 if (mLastBufferInfo.hasBuffer) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700220 t.setDestinationFrame(mSurfaceControl,
221 Rect(0, 0, newSize.getWidth(), newSize.getHeight()));
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000222 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800223 applyTransaction = true;
Vishnu Nair53c936c2020-12-03 11:46:37 -0800224 }
Robert Carrfc416512020-04-02 12:32:44 -0700225 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800226 if (applyTransaction) {
227 t.apply();
228 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700229}
230
231static void transactionCallbackThunk(void* context, nsecs_t latchTime,
232 const sp<Fence>& presentFence,
233 const std::vector<SurfaceControlStats>& stats) {
234 if (context == nullptr) {
235 return;
236 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800237 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700238 bq->transactionCallback(latchTime, presentFence, stats);
239}
240
241void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
242 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700243 std::function<void(int64_t)> transactionCompleteCallback = nullptr;
244 uint64_t currFrameNumber = 0;
Vishnu Nairdab94092020-09-29 16:09:04 -0700245
chaviw71c2cc42020-10-23 16:42:02 -0700246 {
247 std::unique_lock _lock{mMutex};
248 ATRACE_CALL();
249 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700250
chaviw42026162021-04-16 15:46:12 -0500251 if (!mSurfaceControlsWithPendingCallback.empty()) {
252 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
253 mSurfaceControlsWithPendingCallback.pop();
254 bool found = false;
255 for (auto stat : stats) {
256 if (!SurfaceControl::isSameSurface(pendingSC, stat.surfaceControl)) {
257 continue;
Vishnu Nair1506b182021-02-22 14:35:15 -0800258 }
chaviw42026162021-04-16 15:46:12 -0500259
260 mTransformHint = stat.transformHint;
261 mBufferItemConsumer->setTransformHint(mTransformHint);
262 mBufferItemConsumer
263 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
264 stat.frameEventStats.refreshStartTime,
265 stat.frameEventStats.gpuCompositionDoneFence,
266 stat.presentFence, stat.previousReleaseFence,
267 stat.frameEventStats.compositorTiming,
268 stat.latchTime,
269 stat.frameEventStats.dequeueReadyTime);
270
271 currFrameNumber = stat.frameEventStats.frameNumber;
272
273 if (mTransactionCompleteCallback &&
274 currFrameNumber >= mTransactionCompleteFrameNumber) {
275 if (currFrameNumber > mTransactionCompleteFrameNumber) {
276 BQA_LOGE("transactionCallback received for a newer framenumber=%" PRIu64
277 " than expected=%" PRIu64,
278 currFrameNumber, mTransactionCompleteFrameNumber);
279 }
280 transactionCompleteCallback = std::move(mTransactionCompleteCallback);
281 mTransactionCompleteFrameNumber = 0;
282 }
283
284 found = true;
285 break;
chaviw71c2cc42020-10-23 16:42:02 -0700286 }
chaviw42026162021-04-16 15:46:12 -0500287
288 if (!found) {
289 BQA_LOGE("Failed to find matching SurfaceControl in transaction callback");
290 }
291 } else {
292 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
293 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800294 }
chaviw71c2cc42020-10-23 16:42:02 -0700295
chaviw71c2cc42020-10-23 16:42:02 -0700296 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700297 }
Valerie Haua32c5522019-12-09 10:11:08 -0800298
chaviw71c2cc42020-10-23 16:42:02 -0700299 if (transactionCompleteCallback) {
300 transactionCompleteCallback(currFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800301 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700302}
303
Vishnu Nair1506b182021-02-22 14:35:15 -0800304// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
305// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
306// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
307// Otherwise, this is a no-op.
308static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, uint64_t graphicBufferId,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700309 const sp<Fence>& releaseFence, uint32_t transformHint,
310 uint32_t currentMaxAcquiredBufferCount) {
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) {
Ady Abraham899dcdb2021-06-15 16:56:21 -0700315 blastBufferQueue->releaseBufferCallback(graphicBufferId, releaseFence, transformHint,
316 currentMaxAcquiredBufferCount);
Vishnu Nair1506b182021-02-22 14:35:15 -0800317 }
318}
319
320void BLASTBufferQueue::releaseBufferCallback(uint64_t graphicBufferId,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700321 const sp<Fence>& releaseFence, uint32_t transformHint,
322 uint32_t currentMaxAcquiredBufferCount) {
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
Ady Abraham899dcdb2021-06-15 16:56:21 -0700333 // Calculate how many buffers we need to hold before we release them back
334 // to the buffer queue. This will prevent higher latency when we are running
335 // on a lower refresh rate than the max supported. We only do that for EGL
336 // clients as others don't care about latency
337 const bool isEGL = [&] {
338 const auto it = mSubmitted.find(graphicBufferId);
339 return it != mSubmitted.end() && it->second.mApi == NATIVE_WINDOW_API_EGL;
340 }();
341
342 const auto numPendingBuffersToHold =
343 isEGL ? std::max(0u, mMaxAcquiredBuffers - currentMaxAcquiredBufferCount) : 0;
344 mPendingRelease.emplace_back(ReleasedBuffer{graphicBufferId, releaseFence});
345
346 // Release all buffers that are beyond the ones that we need to hold
347 while (mPendingRelease.size() > numPendingBuffersToHold) {
348 const auto releaseBuffer = mPendingRelease.front();
349 mPendingRelease.pop_front();
350 auto it = mSubmitted.find(releaseBuffer.bufferId);
351 if (it == mSubmitted.end()) {
352 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %" PRIu64,
353 graphicBufferId);
354 return;
355 }
356
357 mBufferItemConsumer->releaseBuffer(it->second, releaseBuffer.releaseFence);
358 mSubmitted.erase(it);
Vishnu Nair1506b182021-02-22 14:35:15 -0800359 }
360
Ady Abraham899dcdb2021-06-15 16:56:21 -0700361 ATRACE_INT("PendingRelease", mPendingRelease.size());
362
Vishnu Nair1506b182021-02-22 14:35:15 -0800363 mNumAcquired--;
364 processNextBufferLocked(false /* useNextTransaction */);
365 mCallbackCV.notify_all();
366}
367
Robert Carr255acdc2020-04-17 14:08:55 -0700368void BLASTBufferQueue::processNextBufferLocked(bool useNextTransaction) {
Valerie Haua32c5522019-12-09 10:11:08 -0800369 ATRACE_CALL();
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800370 // If the next transaction is set, we want to guarantee the our acquire will not fail, so don't
371 // include the extra buffer when checking if we can acquire the next buffer.
372 const bool includeExtraAcquire = !useNextTransaction;
373 if (mNumFrameAvailable == 0 || maxBuffersAcquired(includeExtraAcquire)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800374 mCallbackCV.notify_all();
Valerie Haud3b90d22019-11-06 09:37:31 -0800375 return;
376 }
377
Valerie Haua32c5522019-12-09 10:11:08 -0800378 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700379 BQA_LOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800380 return;
381 }
382
Robert Carr78c25dd2019-08-15 14:10:33 -0700383 SurfaceComposerClient::Transaction localTransaction;
384 bool applyTransaction = true;
385 SurfaceComposerClient::Transaction* t = &localTransaction;
Robert Carr255acdc2020-04-17 14:08:55 -0700386 if (mNextTransaction != nullptr && useNextTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700387 t = mNextTransaction;
388 mNextTransaction = nullptr;
389 applyTransaction = false;
390 }
391
Valerie Haua32c5522019-12-09 10:11:08 -0800392 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800393
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800394 status_t status =
395 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800396 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
397 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
398 return;
399 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700400 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Robert Carr78c25dd2019-08-15 14:10:33 -0700401 return;
402 }
Valerie Haua32c5522019-12-09 10:11:08 -0800403 auto buffer = bufferItem.mGraphicBuffer;
404 mNumFrameAvailable--;
405
406 if (buffer == nullptr) {
407 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700408 BQA_LOGE("Buffer was empty");
Valerie Haua32c5522019-12-09 10:11:08 -0800409 return;
410 }
411
Vishnu Nair670b3f72020-09-29 17:52:18 -0700412 if (rejectBuffer(bufferItem)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800413 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d"
414 "buffer{size=%dx%d transform=%d}",
415 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
416 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
417 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
418 processNextBufferLocked(useNextTransaction);
419 return;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700420 }
421
Valerie Haua32c5522019-12-09 10:11:08 -0800422 mNumAcquired++;
Vishnu Nair1506b182021-02-22 14:35:15 -0800423 mSubmitted[buffer->getId()] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700424
Valerie Hau871d6352020-01-29 08:44:02 -0800425 bool needsDisconnect = false;
426 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
427
428 // if producer disconnected before, notify SurfaceFlinger
429 if (needsDisconnect) {
430 t->notifyProducerDisconnect(mSurfaceControl);
431 }
432
Robert Carr78c25dd2019-08-15 14:10:33 -0700433 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
434 incStrong((void*)transactionCallbackThunk);
435
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700436 Rect crop = computeCrop(bufferItem);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800437 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000438 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
439 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700440 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800441
Vishnu Nair1506b182021-02-22 14:35:15 -0800442 auto releaseBufferCallback =
443 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700444 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
445 std::placeholders::_4);
Vishnu Nair1506b182021-02-22 14:35:15 -0800446 t->setBuffer(mSurfaceControl, buffer, releaseBufferCallback);
John Reck137069e2020-12-10 22:07:37 -0500447 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
448 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
449 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700450 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800451 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700452 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviw42026162021-04-16 15:46:12 -0500453 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700454
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700455 t->setDestinationFrame(mSurfaceControl, Rect(0, 0, mSize.getWidth(), mSize.getHeight()));
456 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800457 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800458 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800459 if (!bufferItem.mIsAutoTimestamp) {
460 t->setDesiredPresentTime(bufferItem.mTimestamp);
461 }
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700462 t->setFrameNumber(mSurfaceControl, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700463
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000464 if (!mNextFrameTimelineInfoQueue.empty()) {
Ady Abraham8db10102021-03-15 17:19:23 -0700465 t->setFrameTimelineInfo(mNextFrameTimelineInfoQueue.front());
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000466 mNextFrameTimelineInfoQueue.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100467 }
468
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800469 if (mAutoRefresh != bufferItem.mAutoRefresh) {
470 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
471 mAutoRefresh = bufferItem.mAutoRefresh;
472 }
Vishnu Nairadf632b2021-01-07 14:05:08 -0800473 {
474 std::unique_lock _lock{mTimestampMutex};
475 auto dequeueTime = mDequeueTimestamps.find(buffer->getId());
476 if (dequeueTime != mDequeueTimestamps.end()) {
477 Parcel p;
478 p.writeInt64(dequeueTime->second);
479 t->setMetadata(mSurfaceControl, METADATA_DEQUEUE_TIME, p);
480 mDequeueTimestamps.erase(dequeueTime);
481 }
482 }
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800483
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800484 auto mergeTransaction =
485 [&t, currentFrameNumber = bufferItem.mFrameNumber](
486 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
487 auto& [targetFrameNumber, transaction] = pendingTransaction;
488 if (currentFrameNumber < targetFrameNumber) {
489 return false;
490 }
491 t->merge(std::move(transaction));
492 return true;
493 };
494
495 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
496 mPendingTransactions.end(), mergeTransaction),
497 mPendingTransactions.end());
498
Robert Carr78c25dd2019-08-15 14:10:33 -0700499 if (applyTransaction) {
Vishnu Nair277142c2021-01-05 18:35:29 -0800500 t->setApplyToken(mApplyToken).apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700501 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700502
503 BQA_LOGV("processNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800504 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
505 " graphicBufferId=%" PRIu64,
Vishnu Nairea0de002020-11-17 17:42:37 -0800506 mSize.width, mSize.height, bufferItem.mFrameNumber, toString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800507 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
508 static_cast<uint32_t>(mPendingTransactions.size()),
509 bufferItem.mGraphicBuffer->getId());
Robert Carr78c25dd2019-08-15 14:10:33 -0700510}
511
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800512Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
513 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800514 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800515 }
516 return item.mCrop;
517}
518
Vishnu Nairaef1de92020-10-22 12:15:53 -0700519void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Valerie Haua32c5522019-12-09 10:11:08 -0800520 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800521 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800522
Vishnu Nairdab94092020-09-29 16:09:04 -0700523 const bool nextTransactionSet = mNextTransaction != nullptr;
Vishnu Nair1506b182021-02-22 14:35:15 -0800524 if (nextTransactionSet) {
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800525 while (mNumFrameAvailable > 0 || maxBuffersAcquired(false /* includeExtraAcquire */)) {
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700526 BQA_LOGV("waiting in onFrameAvailable...");
Valerie Hau0188adf2020-02-13 08:29:20 -0800527 mCallbackCV.wait(_lock);
528 }
529 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800530 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800531 mNumFrameAvailable++;
Vishnu Nair1506b182021-02-22 14:35:15 -0800532
533 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " nextTransactionSet=%s", item.mFrameNumber,
534 toString(nextTransactionSet));
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800535 processNextBufferLocked(nextTransactionSet /* useNextTransaction */);
Valerie Haud3b90d22019-11-06 09:37:31 -0800536}
537
Vishnu Nairaef1de92020-10-22 12:15:53 -0700538void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
539 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
540 // Do nothing since we are not storing unacquired buffer items locally.
541}
542
Vishnu Nairadf632b2021-01-07 14:05:08 -0800543void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
544 std::unique_lock _lock{mTimestampMutex};
545 mDequeueTimestamps[bufferId] = systemTime();
546};
547
548void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
549 std::unique_lock _lock{mTimestampMutex};
550 mDequeueTimestamps.erase(bufferId);
551};
552
Robert Carr78c25dd2019-08-15 14:10:33 -0700553void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800554 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700555 mNextTransaction = t;
556}
557
Vishnu Nairea0de002020-11-17 17:42:37 -0800558bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700559 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Hongguang Chen33100282021-04-15 18:36:05 -0700560 mSize = mRequestedSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700561 // Only reject buffers if scaling mode is freeze.
562 return false;
563 }
564
Vishnu Naire1a42322020-10-02 17:42:04 -0700565 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
566 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
567
568 // Take the buffer's orientation into account
569 if (item.mTransform & ui::Transform::ROT_90) {
570 std::swap(bufWidth, bufHeight);
571 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800572 ui::Size bufferSize(bufWidth, bufHeight);
573 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
574 mSize = mRequestedSize;
575 return false;
576 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700577
Vishnu Nair670b3f72020-09-29 17:52:18 -0700578 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800579 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700580}
Vishnu Nairbf255772020-10-16 10:54:41 -0700581
chaviw71c2cc42020-10-23 16:42:02 -0700582void BLASTBufferQueue::setTransactionCompleteCallback(
583 uint64_t frameNumber, std::function<void(int64_t)>&& transactionCompleteCallback) {
584 std::lock_guard _lock{mMutex};
585 if (transactionCompleteCallback == nullptr) {
586 mTransactionCompleteCallback = nullptr;
587 } else {
588 mTransactionCompleteCallback = std::move(transactionCompleteCallback);
589 mTransactionCompleteFrameNumber = frameNumber;
590 }
591}
592
Vishnu Nairbf255772020-10-16 10:54:41 -0700593// Check if we have acquired the maximum number of buffers.
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800594// Consumer can acquire an additional buffer if that buffer is not droppable. Set
595// includeExtraAcquire is true to include this buffer to the count. Since this depends on the state
596// of the buffer, the next acquire may return with NO_BUFFER_AVAILABLE.
597bool BLASTBufferQueue::maxBuffersAcquired(bool includeExtraAcquire) const {
Ady Abraham0bde6b52021-05-18 13:57:02 -0700598 int maxAcquiredBuffers = mMaxAcquiredBuffers + (includeExtraAcquire ? 2 : 1);
Vishnu Nair1506b182021-02-22 14:35:15 -0800599 return mNumAcquired == maxAcquiredBuffers;
Vishnu Nairbf255772020-10-16 10:54:41 -0700600}
601
Robert Carr05086b22020-10-13 18:22:51 -0700602class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700603private:
604 sp<BLASTBufferQueue> mBbq;
Robert Carr05086b22020-10-13 18:22:51 -0700605public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700606 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
607 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
608 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700609
Robert Carr05086b22020-10-13 18:22:51 -0700610 void allocateBuffers() override {
611 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
612 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
613 auto gbp = getIGraphicBufferProducer();
614 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
615 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
616 gbp->allocateBuffers(reqWidth, reqHeight,
617 reqFormat, reqUsage);
618
619 }).detach();
620 }
Robert Carr9c006e02020-10-14 13:41:57 -0700621
Marin Shalamanovc5986772021-03-16 16:09:49 +0100622 status_t setFrameRate(float frameRate, int8_t compatibility,
623 int8_t changeFrameRateStrategy) override {
624 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
625 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700626 return BAD_VALUE;
627 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100628 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700629 }
Robert Carr9b611b72020-10-19 12:00:23 -0700630
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000631 status_t setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) override {
632 return mBbq->setFrameTimelineInfo(frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700633 }
Robert Carr82d07c92021-05-10 11:36:43 -0700634 protected:
635 uint32_t getTransformHint() const override {
636 if (mStickyTransform == 0 && !transformToDisplayInverse()) {
637 return mBbq->getLastTransformHint();
638 } else {
639 return 0;
640 }
641 }
Robert Carr05086b22020-10-13 18:22:51 -0700642};
643
Robert Carr9c006e02020-10-14 13:41:57 -0700644// TODO: Can we coalesce this with frame updates? Need to confirm
645// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200646status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
647 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700648 std::unique_lock _lock{mMutex};
649 SurfaceComposerClient::Transaction t;
650
Marin Shalamanov46084422020-10-13 12:33:42 +0200651 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700652}
653
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000654status_t BLASTBufferQueue::setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) {
Robert Carr9b611b72020-10-19 12:00:23 -0700655 std::unique_lock _lock{mMutex};
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000656 mNextFrameTimelineInfoQueue.push(frameTimelineInfo);
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100657 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700658}
659
Hongguang Chen621ec582021-02-16 15:42:35 -0800660void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
661 std::unique_lock _lock{mMutex};
662 SurfaceComposerClient::Transaction t;
663
664 t.setSidebandStream(mSurfaceControl, stream).apply();
665}
666
Vishnu Nair992496b2020-10-22 17:27:21 -0700667sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
668 std::unique_lock _lock{mMutex};
669 sp<IBinder> scHandle = nullptr;
670 if (includeSurfaceControlHandle && mSurfaceControl) {
671 scHandle = mSurfaceControl->getHandle();
672 }
673 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700674}
675
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800676void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
677 uint64_t frameNumber) {
678 std::lock_guard _lock{mMutex};
679 if (mLastAcquiredFrameNumber >= frameNumber) {
680 // Apply the transaction since we have already acquired the desired frame.
681 t->apply();
682 } else {
chaviwaad6cf52021-03-23 17:27:20 -0500683 mPendingTransactions.emplace_back(frameNumber, *t);
684 // Clear the transaction so it can't be applied elsewhere.
685 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800686 }
687}
688
Vishnu Nair89496122020-12-14 17:14:53 -0800689// Maintains a single worker thread per process that services a list of runnables.
690class AsyncWorker : public Singleton<AsyncWorker> {
691private:
692 std::thread mThread;
693 bool mDone = false;
694 std::deque<std::function<void()>> mRunnables;
695 std::mutex mMutex;
696 std::condition_variable mCv;
697 void run() {
698 std::unique_lock<std::mutex> lock(mMutex);
699 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -0800700 while (!mRunnables.empty()) {
701 std::function<void()> runnable = mRunnables.front();
702 mRunnables.pop_front();
703 runnable();
704 }
Wonsik Kim567533e2021-05-04 19:31:29 -0700705 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -0800706 }
707 }
708
709public:
710 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
711
712 ~AsyncWorker() {
713 mDone = true;
714 mCv.notify_all();
715 if (mThread.joinable()) {
716 mThread.join();
717 }
718 }
719
720 void post(std::function<void()> runnable) {
721 std::unique_lock<std::mutex> lock(mMutex);
722 mRunnables.emplace_back(std::move(runnable));
723 mCv.notify_one();
724 }
725};
726ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
727
728// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
729class AsyncProducerListener : public BnProducerListener {
730private:
731 const sp<IProducerListener> mListener;
732
733public:
734 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
735
736 void onBufferReleased() override {
737 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
738 }
739
740 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
741 AsyncWorker::getInstance().post(
742 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
743 }
744};
745
746// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
747// can be non-blocking when the producer is in the client process.
748class BBQBufferQueueProducer : public BufferQueueProducer {
749public:
750 BBQBufferQueueProducer(const sp<BufferQueueCore>& core)
751 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/) {}
752
753 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
754 QueueBufferOutput* output) override {
755 if (!listener) {
756 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
757 }
758
759 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
760 producerControlledByApp, output);
761 }
Vishnu Nair17dde612020-12-28 11:39:59 -0800762
763 int query(int what, int* value) override {
764 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
765 *value = 1;
766 return NO_ERROR;
767 }
768 return BufferQueueProducer::query(what, value);
769 }
Vishnu Nair89496122020-12-14 17:14:53 -0800770};
771
772// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
773// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
774// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
775// we can deadlock.
776void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
777 sp<IGraphicBufferConsumer>* outConsumer) {
778 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
779 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
780
781 sp<BufferQueueCore> core(new BufferQueueCore());
782 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
783
784 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core));
785 LOG_ALWAYS_FATAL_IF(producer == nullptr,
786 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
787
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800788 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
789 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -0800790 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
791 "BLASTBufferQueue: failed to create BufferQueueConsumer");
792
793 *outProducer = producer;
794 *outConsumer = consumer;
795}
796
chaviw497e81c2021-02-04 17:09:47 -0800797PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
798 PixelFormat convertedFormat = format;
799 switch (format) {
800 case PIXEL_FORMAT_TRANSPARENT:
801 case PIXEL_FORMAT_TRANSLUCENT:
802 convertedFormat = PIXEL_FORMAT_RGBA_8888;
803 break;
804 case PIXEL_FORMAT_OPAQUE:
805 convertedFormat = PIXEL_FORMAT_RGBX_8888;
806 break;
807 }
808 return convertedFormat;
809}
810
Robert Carr82d07c92021-05-10 11:36:43 -0700811uint32_t BLASTBufferQueue::getLastTransformHint() const {
812 if (mSurfaceControl != nullptr) {
813 return mSurfaceControl->getTransformHint();
814 } else {
815 return 0;
816 }
817}
818
Robert Carr78c25dd2019-08-15 14:10:33 -0700819} // namespace android