blob: 36de581e38a9ccebd9d9ea68f0925a6758d7c18b [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 {
chaviw3277faf2021-05-19 16:45:23 -050041inline const char* boolToString(bool b) {
Vishnu Nairdab94092020-09-29 16:09:04 -070042 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) {
Alec Mouri5c8b18c2021-08-19 16:52:34 -0700121 std::scoped_lock lock(mBufferQueueMutex);
Hongguang Chen621ec582021-02-16 15:42:35 -0800122 mBLASTBufferQueue = blastbufferqueue;
123}
124
125void BLASTBufferItemConsumer::onSidebandStreamChanged() {
Alec Mouri5c8b18c2021-08-19 16:52:34 -0700126 std::scoped_lock lock(mBufferQueueMutex);
Hongguang Chen621ec582021-02-16 15:42:35 -0800127 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 Nair4ba0c2e2021-06-24 11:27:17 -0700135 : 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 Nair4ba0c2e2021-06-24 11:27:17 -0700152 mName = name + "#" + std::to_string(id);
Vishnu Nairdab94092020-09-29 16:09:04 -0700153 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(id);
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700154 mQueuedBufferTrace = "QueuedBuffer - " + mName + "BLAST#" + std::to_string(id);
Valerie Haua32c5522019-12-09 10:11:08 -0800155 id++;
Vishnu Nairdab94092020-09-29 16:09:04 -0700156 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700157 mBufferItemConsumer->setFrameAvailableListener(this);
158 mBufferItemConsumer->setBufferFreedListener(this);
Vishnu Nairea0de002020-11-17 17:42:37 -0800159 mBufferItemConsumer->setDefaultBufferSize(mSize.width, mSize.height);
chaviw497e81c2021-02-04 17:09:47 -0800160 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
Hongguang Chen621ec582021-02-16 15:42:35 -0800161 mBufferItemConsumer->setBlastBufferQueue(this);
Robert Carr9f133d72020-04-01 15:51:46 -0700162
Ady Abraham899dcdb2021-06-15 16:56:21 -0700163 ComposerService::getComposerService()->getMaxAcquiredBufferCount(&mMaxAcquiredBuffers);
Ady Abraham0bde6b52021-05-18 13:57:02 -0700164 mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxAcquiredBuffers);
chaviw69058fb2021-09-27 09:37:30 -0500165 mCurrentMaxAcquiredBufferCount = mMaxAcquiredBuffers;
Ady Abraham0bde6b52021-05-18 13:57:02 -0700166
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()
Vishnu Nair084514a2021-07-30 16:07:42 -0700170 .setFlags(surface, layer_state_t::eEnableBackpressure,
171 layer_state_t::eEnableBackpressure)
172 .setApplyToken(mApplyToken)
173 .apply();
Valerie Haua32c5522019-12-09 10:11:08 -0800174 mNumAcquired = 0;
175 mNumFrameAvailable = 0;
Vishnu Naira4fbca52021-07-07 16:52:34 -0700176 BQA_LOGV("BLASTBufferQueue created width=%d height=%d format=%d mTransformHint=%d", width,
177 height, format, mTransformHint);
Robert Carr78c25dd2019-08-15 14:10:33 -0700178}
179
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800180BLASTBufferQueue::~BLASTBufferQueue() {
Hongguang Chen621ec582021-02-16 15:42:35 -0800181 mBufferItemConsumer->setBlastBufferQueue(nullptr);
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800182 if (mPendingTransactions.empty()) {
183 return;
184 }
185 BQA_LOGE("Applying pending transactions on dtor %d",
186 static_cast<uint32_t>(mPendingTransactions.size()));
187 SurfaceComposerClient::Transaction t;
188 for (auto& [targetFrameNumber, transaction] : mPendingTransactions) {
189 t.merge(std::move(transaction));
190 }
191 t.apply();
192}
193
chaviw565ee542021-01-14 10:21:23 -0800194void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height,
Vishnu Nair084514a2021-07-30 16:07:42 -0700195 int32_t format, SurfaceComposerClient::Transaction* outTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700196 std::unique_lock _lock{mMutex};
chaviw565ee542021-01-14 10:21:23 -0800197 if (mFormat != format) {
198 mFormat = format;
chaviw497e81c2021-02-04 17:09:47 -0800199 mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format));
chaviw565ee542021-01-14 10:21:23 -0800200 }
201
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800202 SurfaceComposerClient::Transaction t;
Vishnu Nair5fa91c22021-06-29 14:30:48 -0700203 const bool setBackpressureFlag = !SurfaceControl::isSameSurface(mSurfaceControl, surface);
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800204 bool applyTransaction = false;
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800205
Vishnu Nair5fa91c22021-06-29 14:30:48 -0700206 // Always update the native object even though they might have the same layer handle, so we can
207 // get the updated transform hint from WM.
208 mSurfaceControl = surface;
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800209 if (mSurfaceControl != nullptr) {
Vishnu Nair5fa91c22021-06-29 14:30:48 -0700210 if (setBackpressureFlag) {
211 t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure,
212 layer_state_t::eEnableBackpressure);
213 applyTransaction = true;
214 }
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800215 mTransformHint = mSurfaceControl->getTransformHint();
216 mBufferItemConsumer->setTransformHint(mTransformHint);
217 }
Vishnu Naira4fbca52021-07-07 16:52:34 -0700218 BQA_LOGV("update width=%d height=%d format=%d mTransformHint=%d", width, height, format,
219 mTransformHint);
Arthur Hungb6aa9a02021-06-09 14:23:01 +0800220
Vishnu Nairea0de002020-11-17 17:42:37 -0800221 ui::Size newSize(width, height);
222 if (mRequestedSize != newSize) {
223 mRequestedSize.set(newSize);
224 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000225 if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nair53c936c2020-12-03 11:46:37 -0800226 // If the buffer supports scaling, update the frame immediately since the client may
227 // want to scale the existing buffer to the new size.
228 mSize = mRequestedSize;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000229 // We only need to update the scale if we've received at least one buffer. The reason
230 // for this is the scale is calculated based on the requested size and buffer size.
231 // If there's no buffer, the scale will always be 1.
Vishnu Nair084514a2021-07-30 16:07:42 -0700232 SurfaceComposerClient::Transaction* destFrameTransaction =
233 (outTransaction) ? outTransaction : &t;
Vishnu Nair5fa91c22021-06-29 14:30:48 -0700234 if (mSurfaceControl != nullptr && mLastBufferInfo.hasBuffer) {
Vishnu Nair084514a2021-07-30 16:07:42 -0700235 destFrameTransaction->setDestinationFrame(mSurfaceControl,
236 Rect(0, 0, newSize.getWidth(),
237 newSize.getHeight()));
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000238 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800239 applyTransaction = true;
Vishnu Nair53c936c2020-12-03 11:46:37 -0800240 }
Robert Carrfc416512020-04-02 12:32:44 -0700241 }
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800242 if (applyTransaction) {
Vishnu Nair084514a2021-07-30 16:07:42 -0700243 t.setApplyToken(mApplyToken).apply();
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800244 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700245}
246
247static void transactionCallbackThunk(void* context, nsecs_t latchTime,
248 const sp<Fence>& presentFence,
249 const std::vector<SurfaceControlStats>& stats) {
250 if (context == nullptr) {
251 return;
252 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800253 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700254 bq->transactionCallback(latchTime, presentFence, stats);
255}
256
257void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
258 const std::vector<SurfaceControlStats>& stats) {
chaviw71c2cc42020-10-23 16:42:02 -0700259 std::function<void(int64_t)> transactionCompleteCallback = nullptr;
260 uint64_t currFrameNumber = 0;
Vishnu Nairdab94092020-09-29 16:09:04 -0700261
chaviw71c2cc42020-10-23 16:42:02 -0700262 {
263 std::unique_lock _lock{mMutex};
264 ATRACE_CALL();
265 BQA_LOGV("transactionCallback");
chaviw71c2cc42020-10-23 16:42:02 -0700266
chaviw42026162021-04-16 15:46:12 -0500267 if (!mSurfaceControlsWithPendingCallback.empty()) {
268 sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front();
269 mSurfaceControlsWithPendingCallback.pop();
270 bool found = false;
271 for (auto stat : stats) {
272 if (!SurfaceControl::isSameSurface(pendingSC, stat.surfaceControl)) {
273 continue;
Vishnu Nair1506b182021-02-22 14:35:15 -0800274 }
chaviw42026162021-04-16 15:46:12 -0500275
276 mTransformHint = stat.transformHint;
277 mBufferItemConsumer->setTransformHint(mTransformHint);
Vishnu Naira4fbca52021-07-07 16:52:34 -0700278 BQA_LOGV("updated mTransformHint=%d", mTransformHint);
Vishnu Nairde66dc72021-06-17 17:54:41 -0700279 // Update frametime stamps if the frame was latched and presented, indicated by a
280 // valid latch time.
281 if (stat.latchTime > 0) {
282 mBufferItemConsumer
283 ->updateFrameTimestamps(stat.frameEventStats.frameNumber,
284 stat.frameEventStats.refreshStartTime,
285 stat.frameEventStats.gpuCompositionDoneFence,
286 stat.presentFence, stat.previousReleaseFence,
287 stat.frameEventStats.compositorTiming,
288 stat.latchTime,
289 stat.frameEventStats.dequeueReadyTime);
290 }
chaviw42026162021-04-16 15:46:12 -0500291 currFrameNumber = stat.frameEventStats.frameNumber;
292
293 if (mTransactionCompleteCallback &&
294 currFrameNumber >= mTransactionCompleteFrameNumber) {
295 if (currFrameNumber > mTransactionCompleteFrameNumber) {
296 BQA_LOGE("transactionCallback received for a newer framenumber=%" PRIu64
297 " than expected=%" PRIu64,
298 currFrameNumber, mTransactionCompleteFrameNumber);
299 }
300 transactionCompleteCallback = std::move(mTransactionCompleteCallback);
301 mTransactionCompleteFrameNumber = 0;
302 }
303
304 found = true;
305 break;
chaviw71c2cc42020-10-23 16:42:02 -0700306 }
chaviw42026162021-04-16 15:46:12 -0500307
308 if (!found) {
309 BQA_LOGE("Failed to find matching SurfaceControl in transaction callback");
310 }
311 } else {
312 BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was "
313 "empty.");
Valerie Haua32c5522019-12-09 10:11:08 -0800314 }
chaviw71c2cc42020-10-23 16:42:02 -0700315
chaviw71c2cc42020-10-23 16:42:02 -0700316 decStrong((void*)transactionCallbackThunk);
Robert Carr78c25dd2019-08-15 14:10:33 -0700317 }
Valerie Haua32c5522019-12-09 10:11:08 -0800318
chaviw71c2cc42020-10-23 16:42:02 -0700319 if (transactionCompleteCallback) {
320 transactionCompleteCallback(currFrameNumber);
Valerie Haua32c5522019-12-09 10:11:08 -0800321 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700322}
323
Vishnu Nair1506b182021-02-22 14:35:15 -0800324// Unlike transactionCallbackThunk the release buffer callback does not extend the life of the
325// BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client.
326// So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer.
327// Otherwise, this is a no-op.
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700328static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, const ReleaseCallbackId& id,
chaviw69058fb2021-09-27 09:37:30 -0500329 const sp<Fence>& releaseFence,
330 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800331 sp<BLASTBufferQueue> blastBufferQueue = context.promote();
Vishnu Nair1506b182021-02-22 14:35:15 -0800332 if (blastBufferQueue) {
chaviw69058fb2021-09-27 09:37:30 -0500333 blastBufferQueue->releaseBufferCallback(id, releaseFence, currentMaxAcquiredBufferCount);
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700334 } else {
335 ALOGV("releaseBufferCallbackThunk %s blastBufferQueue is dead", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800336 }
337}
338
chaviw69058fb2021-09-27 09:37:30 -0500339void BLASTBufferQueue::releaseBufferCallback(
340 const ReleaseCallbackId& id, const sp<Fence>& releaseFence,
341 std::optional<uint32_t> currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -0800342 ATRACE_CALL();
343 std::unique_lock _lock{mMutex};
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700344 BQA_LOGV("releaseBufferCallback %s", id.to_string().c_str());
Vishnu Nair1506b182021-02-22 14:35:15 -0800345
Ady Abraham899dcdb2021-06-15 16:56:21 -0700346 // Calculate how many buffers we need to hold before we release them back
347 // to the buffer queue. This will prevent higher latency when we are running
348 // on a lower refresh rate than the max supported. We only do that for EGL
349 // clients as others don't care about latency
350 const bool isEGL = [&] {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700351 const auto it = mSubmitted.find(id);
Ady Abraham899dcdb2021-06-15 16:56:21 -0700352 return it != mSubmitted.end() && it->second.mApi == NATIVE_WINDOW_API_EGL;
353 }();
354
chaviw69058fb2021-09-27 09:37:30 -0500355 if (currentMaxAcquiredBufferCount) {
356 mCurrentMaxAcquiredBufferCount = *currentMaxAcquiredBufferCount;
357 }
358
Ady Abraham899dcdb2021-06-15 16:56:21 -0700359 const auto numPendingBuffersToHold =
chaviw69058fb2021-09-27 09:37:30 -0500360 isEGL ? std::max(0u, mMaxAcquiredBuffers - mCurrentMaxAcquiredBufferCount) : 0;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700361 mPendingRelease.emplace_back(ReleasedBuffer{id, releaseFence});
Ady Abraham899dcdb2021-06-15 16:56:21 -0700362
363 // Release all buffers that are beyond the ones that we need to hold
364 while (mPendingRelease.size() > numPendingBuffersToHold) {
365 const auto releaseBuffer = mPendingRelease.front();
366 mPendingRelease.pop_front();
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700367 auto it = mSubmitted.find(releaseBuffer.callbackId);
Ady Abraham899dcdb2021-06-15 16:56:21 -0700368 if (it == mSubmitted.end()) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700369 BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %s",
370 releaseBuffer.callbackId.to_string().c_str());
Ady Abraham899dcdb2021-06-15 16:56:21 -0700371 return;
372 }
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700373 mNumAcquired--;
chaviw69058fb2021-09-27 09:37:30 -0500374 BQA_LOGV("released %s", releaseBuffer.callbackId.to_string().c_str());
Ady Abraham899dcdb2021-06-15 16:56:21 -0700375 mBufferItemConsumer->releaseBuffer(it->second, releaseBuffer.releaseFence);
376 mSubmitted.erase(it);
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700377 processNextBufferLocked(false /* useNextTransaction */);
Vishnu Nair1506b182021-02-22 14:35:15 -0800378 }
379
Ady Abraham899dcdb2021-06-15 16:56:21 -0700380 ATRACE_INT("PendingRelease", mPendingRelease.size());
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700381 ATRACE_INT(mQueuedBufferTrace.c_str(),
382 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
Vishnu Nair1506b182021-02-22 14:35:15 -0800383 mCallbackCV.notify_all();
384}
385
Robert Carr255acdc2020-04-17 14:08:55 -0700386void BLASTBufferQueue::processNextBufferLocked(bool useNextTransaction) {
Valerie Haua32c5522019-12-09 10:11:08 -0800387 ATRACE_CALL();
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800388 // If the next transaction is set, we want to guarantee the our acquire will not fail, so don't
389 // include the extra buffer when checking if we can acquire the next buffer.
390 const bool includeExtraAcquire = !useNextTransaction;
391 if (mNumFrameAvailable == 0 || maxBuffersAcquired(includeExtraAcquire)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800392 mCallbackCV.notify_all();
Valerie Haud3b90d22019-11-06 09:37:31 -0800393 return;
394 }
395
Valerie Haua32c5522019-12-09 10:11:08 -0800396 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700397 BQA_LOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800398 return;
399 }
400
Robert Carr78c25dd2019-08-15 14:10:33 -0700401 SurfaceComposerClient::Transaction localTransaction;
402 bool applyTransaction = true;
403 SurfaceComposerClient::Transaction* t = &localTransaction;
Robert Carr255acdc2020-04-17 14:08:55 -0700404 if (mNextTransaction != nullptr && useNextTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700405 t = mNextTransaction;
406 mNextTransaction = nullptr;
407 applyTransaction = false;
408 }
409
Valerie Haua32c5522019-12-09 10:11:08 -0800410 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800411
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800412 status_t status =
413 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800414 if (status == BufferQueue::NO_BUFFER_AVAILABLE) {
415 BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE");
416 return;
417 } else if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700418 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Robert Carr78c25dd2019-08-15 14:10:33 -0700419 return;
420 }
Valerie Haua32c5522019-12-09 10:11:08 -0800421 auto buffer = bufferItem.mGraphicBuffer;
422 mNumFrameAvailable--;
423
424 if (buffer == nullptr) {
425 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700426 BQA_LOGE("Buffer was empty");
Valerie Haua32c5522019-12-09 10:11:08 -0800427 return;
428 }
429
Vishnu Nair670b3f72020-09-29 17:52:18 -0700430 if (rejectBuffer(bufferItem)) {
Vishnu Naira4fbca52021-07-07 16:52:34 -0700431 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d "
Vishnu Nairea0de002020-11-17 17:42:37 -0800432 "buffer{size=%dx%d transform=%d}",
433 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
434 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
435 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
436 processNextBufferLocked(useNextTransaction);
437 return;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700438 }
439
Valerie Haua32c5522019-12-09 10:11:08 -0800440 mNumAcquired++;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700441 mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
442 ReleaseCallbackId releaseCallbackId(buffer->getId(), mLastAcquiredFrameNumber);
443 mSubmitted[releaseCallbackId] = bufferItem;
Robert Carr78c25dd2019-08-15 14:10:33 -0700444
Valerie Hau871d6352020-01-29 08:44:02 -0800445 bool needsDisconnect = false;
446 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
447
448 // if producer disconnected before, notify SurfaceFlinger
449 if (needsDisconnect) {
450 t->notifyProducerDisconnect(mSurfaceControl);
451 }
452
Robert Carr78c25dd2019-08-15 14:10:33 -0700453 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
454 incStrong((void*)transactionCallbackThunk);
455
Vishnu Nair932f6ae2021-09-29 17:33:10 -0700456 const bool sizeHasChanged = mRequestedSize != mSize;
457 mSize = mRequestedSize;
458 const bool updateDestinationFrame = sizeHasChanged || !mLastBufferInfo.hasBuffer;
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700459 Rect crop = computeCrop(bufferItem);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000460 mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
461 bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700462 bufferItem.mScalingMode, crop);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800463
Vishnu Nair1506b182021-02-22 14:35:15 -0800464 auto releaseBufferCallback =
465 std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
chaviw69058fb2021-09-27 09:37:30 -0500466 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
chaviwba4320c2021-09-15 15:20:53 -0500467 sp<Fence> fence = bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE;
468 t->setBuffer(mSurfaceControl, buffer, fence, bufferItem.mFrameNumber, releaseCallbackId,
469 releaseBufferCallback);
John Reck137069e2020-12-10 22:07:37 -0500470 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
471 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
472 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700473 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
chaviw42026162021-04-16 15:46:12 -0500474 mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
Robert Carr78c25dd2019-08-15 14:10:33 -0700475
Vishnu Nair084514a2021-07-30 16:07:42 -0700476 if (updateDestinationFrame) {
477 t->setDestinationFrame(mSurfaceControl, Rect(0, 0, mSize.getWidth(), mSize.getHeight()));
478 }
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700479 t->setBufferCrop(mSurfaceControl, crop);
Valerie Haua32c5522019-12-09 10:11:08 -0800480 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800481 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Ady Abrahamf0c56492020-12-17 18:04:15 -0800482 if (!bufferItem.mIsAutoTimestamp) {
483 t->setDesiredPresentTime(bufferItem.mTimestamp);
484 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700485
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000486 if (!mNextFrameTimelineInfoQueue.empty()) {
Ady Abraham8db10102021-03-15 17:19:23 -0700487 t->setFrameTimelineInfo(mNextFrameTimelineInfoQueue.front());
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000488 mNextFrameTimelineInfoQueue.pop();
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100489 }
490
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800491 if (mAutoRefresh != bufferItem.mAutoRefresh) {
492 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
493 mAutoRefresh = bufferItem.mAutoRefresh;
494 }
Vishnu Nairadf632b2021-01-07 14:05:08 -0800495 {
496 std::unique_lock _lock{mTimestampMutex};
497 auto dequeueTime = mDequeueTimestamps.find(buffer->getId());
498 if (dequeueTime != mDequeueTimestamps.end()) {
499 Parcel p;
500 p.writeInt64(dequeueTime->second);
501 t->setMetadata(mSurfaceControl, METADATA_DEQUEUE_TIME, p);
502 mDequeueTimestamps.erase(dequeueTime);
503 }
504 }
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800505
chaviw6a195272021-09-03 16:14:25 -0500506 mergePendingTransactions(t, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700507 if (applyTransaction) {
Vishnu Nair277142c2021-01-05 18:35:29 -0800508 t->setApplyToken(mApplyToken).apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700509 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700510
511 BQA_LOGV("processNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
Vishnu Nair1506b182021-02-22 14:35:15 -0800512 " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
Vishnu Naira4fbca52021-07-07 16:52:34 -0700513 " graphicBufferId=%" PRIu64 "%s transform=%d",
chaviw3277faf2021-05-19 16:45:23 -0500514 mSize.width, mSize.height, bufferItem.mFrameNumber, boolToString(applyTransaction),
Vishnu Nair1506b182021-02-22 14:35:15 -0800515 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700516 static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(),
Vishnu Naira4fbca52021-07-07 16:52:34 -0700517 bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform);
Robert Carr78c25dd2019-08-15 14:10:33 -0700518}
519
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800520Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
521 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800522 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800523 }
524 return item.mCrop;
525}
526
Vishnu Nairaef1de92020-10-22 12:15:53 -0700527void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Valerie Haua32c5522019-12-09 10:11:08 -0800528 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800529 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800530
Vishnu Nairdab94092020-09-29 16:09:04 -0700531 const bool nextTransactionSet = mNextTransaction != nullptr;
Vishnu Nair1506b182021-02-22 14:35:15 -0800532 if (nextTransactionSet) {
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800533 while (mNumFrameAvailable > 0 || maxBuffersAcquired(false /* includeExtraAcquire */)) {
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700534 BQA_LOGV("waiting in onFrameAvailable...");
Valerie Hau0188adf2020-02-13 08:29:20 -0800535 mCallbackCV.wait(_lock);
536 }
537 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800538 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800539 mNumFrameAvailable++;
Vishnu Nair2a52ca62021-06-24 13:08:53 -0700540 ATRACE_INT(mQueuedBufferTrace.c_str(),
541 mNumFrameAvailable + mNumAcquired - mPendingRelease.size());
Vishnu Nair1506b182021-02-22 14:35:15 -0800542
543 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " nextTransactionSet=%s", item.mFrameNumber,
chaviw3277faf2021-05-19 16:45:23 -0500544 boolToString(nextTransactionSet));
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800545 processNextBufferLocked(nextTransactionSet /* useNextTransaction */);
Valerie Haud3b90d22019-11-06 09:37:31 -0800546}
547
Vishnu Nairaef1de92020-10-22 12:15:53 -0700548void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
549 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
550 // Do nothing since we are not storing unacquired buffer items locally.
551}
552
Vishnu Nairadf632b2021-01-07 14:05:08 -0800553void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) {
554 std::unique_lock _lock{mTimestampMutex};
555 mDequeueTimestamps[bufferId] = systemTime();
556};
557
558void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) {
559 std::unique_lock _lock{mTimestampMutex};
560 mDequeueTimestamps.erase(bufferId);
561};
562
Robert Carr78c25dd2019-08-15 14:10:33 -0700563void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800564 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700565 mNextTransaction = t;
566}
567
Vishnu Nairea0de002020-11-17 17:42:37 -0800568bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700569 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
570 // Only reject buffers if scaling mode is freeze.
571 return false;
572 }
573
Vishnu Naire1a42322020-10-02 17:42:04 -0700574 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
575 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
576
577 // Take the buffer's orientation into account
578 if (item.mTransform & ui::Transform::ROT_90) {
579 std::swap(bufWidth, bufHeight);
580 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800581 ui::Size bufferSize(bufWidth, bufHeight);
582 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800583 return false;
584 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700585
Vishnu Nair670b3f72020-09-29 17:52:18 -0700586 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800587 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700588}
Vishnu Nairbf255772020-10-16 10:54:41 -0700589
chaviw71c2cc42020-10-23 16:42:02 -0700590void BLASTBufferQueue::setTransactionCompleteCallback(
591 uint64_t frameNumber, std::function<void(int64_t)>&& transactionCompleteCallback) {
592 std::lock_guard _lock{mMutex};
593 if (transactionCompleteCallback == nullptr) {
594 mTransactionCompleteCallback = nullptr;
595 } else {
596 mTransactionCompleteCallback = std::move(transactionCompleteCallback);
597 mTransactionCompleteFrameNumber = frameNumber;
598 }
599}
600
Vishnu Nairbf255772020-10-16 10:54:41 -0700601// Check if we have acquired the maximum number of buffers.
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800602// Consumer can acquire an additional buffer if that buffer is not droppable. Set
603// includeExtraAcquire is true to include this buffer to the count. Since this depends on the state
604// of the buffer, the next acquire may return with NO_BUFFER_AVAILABLE.
605bool BLASTBufferQueue::maxBuffersAcquired(bool includeExtraAcquire) const {
Ady Abraham0bde6b52021-05-18 13:57:02 -0700606 int maxAcquiredBuffers = mMaxAcquiredBuffers + (includeExtraAcquire ? 2 : 1);
Vishnu Nair1506b182021-02-22 14:35:15 -0800607 return mNumAcquired == maxAcquiredBuffers;
Vishnu Nairbf255772020-10-16 10:54:41 -0700608}
609
Robert Carr05086b22020-10-13 18:22:51 -0700610class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700611private:
Vishnu Nair95b6d512021-08-30 15:31:08 -0700612 std::mutex mMutex;
Robert Carr9c006e02020-10-14 13:41:57 -0700613 sp<BLASTBufferQueue> mBbq;
Vishnu Nair95b6d512021-08-30 15:31:08 -0700614 bool mDestroyed = false;
615
Robert Carr05086b22020-10-13 18:22:51 -0700616public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700617 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
618 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
619 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700620
Robert Carr05086b22020-10-13 18:22:51 -0700621 void allocateBuffers() override {
622 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
623 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
624 auto gbp = getIGraphicBufferProducer();
625 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
626 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
627 gbp->allocateBuffers(reqWidth, reqHeight,
628 reqFormat, reqUsage);
629
630 }).detach();
631 }
Robert Carr9c006e02020-10-14 13:41:57 -0700632
Marin Shalamanovc5986772021-03-16 16:09:49 +0100633 status_t setFrameRate(float frameRate, int8_t compatibility,
634 int8_t changeFrameRateStrategy) override {
Vishnu Nair95b6d512021-08-30 15:31:08 -0700635 std::unique_lock _lock{mMutex};
636 if (mDestroyed) {
637 return DEAD_OBJECT;
638 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100639 if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy,
640 "BBQSurface::setFrameRate")) {
Robert Carr9c006e02020-10-14 13:41:57 -0700641 return BAD_VALUE;
642 }
Marin Shalamanovc5986772021-03-16 16:09:49 +0100643 return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy);
Robert Carr9c006e02020-10-14 13:41:57 -0700644 }
Robert Carr9b611b72020-10-19 12:00:23 -0700645
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000646 status_t setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) override {
Vishnu Nair95b6d512021-08-30 15:31:08 -0700647 std::unique_lock _lock{mMutex};
648 if (mDestroyed) {
649 return DEAD_OBJECT;
650 }
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000651 return mBbq->setFrameTimelineInfo(frameTimelineInfo);
Robert Carr9b611b72020-10-19 12:00:23 -0700652 }
Vishnu Nair95b6d512021-08-30 15:31:08 -0700653
654 void destroy() override {
655 Surface::destroy();
656
657 std::unique_lock _lock{mMutex};
658 mDestroyed = true;
659 mBbq = nullptr;
660 }
Robert Carr05086b22020-10-13 18:22:51 -0700661};
662
Robert Carr9c006e02020-10-14 13:41:57 -0700663// TODO: Can we coalesce this with frame updates? Need to confirm
664// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200665status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
666 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700667 std::unique_lock _lock{mMutex};
668 SurfaceComposerClient::Transaction t;
669
Marin Shalamanov46084422020-10-13 12:33:42 +0200670 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700671}
672
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000673status_t BLASTBufferQueue::setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) {
Robert Carr9b611b72020-10-19 12:00:23 -0700674 std::unique_lock _lock{mMutex};
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000675 mNextFrameTimelineInfoQueue.push(frameTimelineInfo);
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100676 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700677}
678
Hongguang Chen621ec582021-02-16 15:42:35 -0800679void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
680 std::unique_lock _lock{mMutex};
681 SurfaceComposerClient::Transaction t;
682
683 t.setSidebandStream(mSurfaceControl, stream).apply();
684}
685
Vishnu Nair992496b2020-10-22 17:27:21 -0700686sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
687 std::unique_lock _lock{mMutex};
688 sp<IBinder> scHandle = nullptr;
689 if (includeSurfaceControlHandle && mSurfaceControl) {
690 scHandle = mSurfaceControl->getHandle();
691 }
692 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700693}
694
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800695void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t,
696 uint64_t frameNumber) {
697 std::lock_guard _lock{mMutex};
698 if (mLastAcquiredFrameNumber >= frameNumber) {
699 // Apply the transaction since we have already acquired the desired frame.
700 t->apply();
701 } else {
chaviwaad6cf52021-03-23 17:27:20 -0500702 mPendingTransactions.emplace_back(frameNumber, *t);
703 // Clear the transaction so it can't be applied elsewhere.
704 t->clear();
Vishnu Nairc4a40c12020-12-23 09:14:32 -0800705 }
706}
707
chaviw6a195272021-09-03 16:14:25 -0500708void BLASTBufferQueue::applyPendingTransactions(uint64_t frameNumber) {
709 std::lock_guard _lock{mMutex};
710
711 SurfaceComposerClient::Transaction t;
712 mergePendingTransactions(&t, frameNumber);
713 t.setApplyToken(mApplyToken).apply();
714}
715
716void BLASTBufferQueue::mergePendingTransactions(SurfaceComposerClient::Transaction* t,
717 uint64_t frameNumber) {
718 auto mergeTransaction =
719 [&t, currentFrameNumber = frameNumber](
720 std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) {
721 auto& [targetFrameNumber, transaction] = pendingTransaction;
722 if (currentFrameNumber < targetFrameNumber) {
723 return false;
724 }
725 t->merge(std::move(transaction));
726 return true;
727 };
728
729 mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(),
730 mPendingTransactions.end(), mergeTransaction),
731 mPendingTransactions.end());
732}
733
Vishnu Nair89496122020-12-14 17:14:53 -0800734// Maintains a single worker thread per process that services a list of runnables.
735class AsyncWorker : public Singleton<AsyncWorker> {
736private:
737 std::thread mThread;
738 bool mDone = false;
739 std::deque<std::function<void()>> mRunnables;
740 std::mutex mMutex;
741 std::condition_variable mCv;
742 void run() {
743 std::unique_lock<std::mutex> lock(mMutex);
744 while (!mDone) {
Vishnu Nair89496122020-12-14 17:14:53 -0800745 while (!mRunnables.empty()) {
Vishnu Nair51e4dc82021-10-01 15:32:33 -0700746 std::deque<std::function<void()>> runnables = std::move(mRunnables);
747 mRunnables.clear();
748 lock.unlock();
749 // Run outside the lock since the runnable might trigger another
750 // post to the async worker.
751 execute(runnables);
752 lock.lock();
Vishnu Nair89496122020-12-14 17:14:53 -0800753 }
Wonsik Kim567533e2021-05-04 19:31:29 -0700754 mCv.wait(lock);
Vishnu Nair89496122020-12-14 17:14:53 -0800755 }
756 }
757
Vishnu Nair51e4dc82021-10-01 15:32:33 -0700758 void execute(std::deque<std::function<void()>>& runnables) {
759 while (!runnables.empty()) {
760 std::function<void()> runnable = runnables.front();
761 runnables.pop_front();
762 runnable();
763 }
764 }
765
Vishnu Nair89496122020-12-14 17:14:53 -0800766public:
767 AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); }
768
769 ~AsyncWorker() {
770 mDone = true;
771 mCv.notify_all();
772 if (mThread.joinable()) {
773 mThread.join();
774 }
775 }
776
777 void post(std::function<void()> runnable) {
778 std::unique_lock<std::mutex> lock(mMutex);
779 mRunnables.emplace_back(std::move(runnable));
780 mCv.notify_one();
781 }
782};
783ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
784
785// Asynchronously calls ProducerListener functions so we can emulate one way binder calls.
786class AsyncProducerListener : public BnProducerListener {
787private:
788 const sp<IProducerListener> mListener;
789
790public:
791 AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
792
793 void onBufferReleased() override {
794 AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
795 }
796
797 void onBuffersDiscarded(const std::vector<int32_t>& slots) override {
798 AsyncWorker::getInstance().post(
799 [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); });
800 }
801};
802
803// Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls
804// can be non-blocking when the producer is in the client process.
805class BBQBufferQueueProducer : public BufferQueueProducer {
806public:
807 BBQBufferQueueProducer(const sp<BufferQueueCore>& core)
808 : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/) {}
809
810 status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp,
811 QueueBufferOutput* output) override {
812 if (!listener) {
813 return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
814 }
815
816 return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
817 producerControlledByApp, output);
818 }
Vishnu Nair17dde612020-12-28 11:39:59 -0800819
820 int query(int what, int* value) override {
821 if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) {
822 *value = 1;
823 return NO_ERROR;
824 }
825 return BufferQueueProducer::query(what, value);
826 }
Vishnu Nair89496122020-12-14 17:14:53 -0800827};
828
829// Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer.
830// This BQP allows invoking client specified ProducerListeners and invoke them asynchronously,
831// emulating one way binder call behavior. Without this, if the listener calls back into the queue,
832// we can deadlock.
833void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
834 sp<IGraphicBufferConsumer>* outConsumer) {
835 LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL");
836 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL");
837
838 sp<BufferQueueCore> core(new BufferQueueCore());
839 LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore");
840
841 sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core));
842 LOG_ALWAYS_FATAL_IF(producer == nullptr,
843 "BLASTBufferQueue: failed to create BBQBufferQueueProducer");
844
Vishnu Nair8b30dd12021-01-25 14:16:54 -0800845 sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core));
846 consumer->setAllowExtraAcquire(true);
Vishnu Nair89496122020-12-14 17:14:53 -0800847 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
848 "BLASTBufferQueue: failed to create BufferQueueConsumer");
849
850 *outProducer = producer;
851 *outConsumer = consumer;
852}
853
chaviw497e81c2021-02-04 17:09:47 -0800854PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) {
855 PixelFormat convertedFormat = format;
856 switch (format) {
857 case PIXEL_FORMAT_TRANSPARENT:
858 case PIXEL_FORMAT_TRANSLUCENT:
859 convertedFormat = PIXEL_FORMAT_RGBA_8888;
860 break;
861 case PIXEL_FORMAT_OPAQUE:
862 convertedFormat = PIXEL_FORMAT_RGBX_8888;
863 break;
864 }
865 return convertedFormat;
866}
867
Robert Carr82d07c92021-05-10 11:36:43 -0700868uint32_t BLASTBufferQueue::getLastTransformHint() const {
869 if (mSurfaceControl != nullptr) {
870 return mSurfaceControl->getTransformHint();
871 } else {
872 return 0;
873 }
874}
875
chaviw0b020f82021-08-20 12:00:47 -0500876uint64_t BLASTBufferQueue::getLastAcquiredFrameNum() {
877 std::unique_lock _lock{mMutex};
878 return mLastAcquiredFrameNumber;
879}
880
Robert Carr78c25dd2019-08-15 14:10:33 -0700881} // namespace android