blob: b751e985aa5cebaa68645a288ee16c61a07c10c9 [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>
Valerie Hau45e4b3b2019-12-03 10:49:17 -080025#include <gui/GLConsumer.h>
Robert Carr05086b22020-10-13 18:22:51 -070026#include <gui/Surface.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070027
Valerie Haua32c5522019-12-09 10:11:08 -080028#include <utils/Trace.h>
29
Robert Carr78c25dd2019-08-15 14:10:33 -070030#include <chrono>
31
32using namespace std::chrono_literals;
33
Vishnu Nairdab94092020-09-29 16:09:04 -070034namespace {
35inline const char* toString(bool b) {
36 return b ? "true" : "false";
37}
38} // namespace
39
Robert Carr78c25dd2019-08-15 14:10:33 -070040namespace android {
41
Vishnu Nairdab94092020-09-29 16:09:04 -070042// Macros to include adapter info in log messages
43#define BQA_LOGV(x, ...) \
44 ALOGV("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
45#define BQA_LOGE(x, ...) \
46 ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
47
Valerie Hau871d6352020-01-29 08:44:02 -080048void BLASTBufferItemConsumer::onDisconnect() {
49 Mutex::Autolock lock(mFrameEventHistoryMutex);
50 mPreviouslyConnected = mCurrentlyConnected;
51 mCurrentlyConnected = false;
52 if (mPreviouslyConnected) {
53 mDisconnectEvents.push(mCurrentFrameNumber);
54 }
55 mFrameEventHistory.onDisconnect();
56}
57
58void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
59 FrameEventHistoryDelta* outDelta) {
60 Mutex::Autolock lock(mFrameEventHistoryMutex);
61 if (newTimestamps) {
62 // BufferQueueProducer only adds a new timestamp on
63 // queueBuffer
64 mCurrentFrameNumber = newTimestamps->frameNumber;
65 mFrameEventHistory.addQueue(*newTimestamps);
66 }
67 if (outDelta) {
68 // frame event histories will be processed
69 // only after the producer connects and requests
70 // deltas for the first time. Forward this intent
71 // to SF-side to turn event processing back on
72 mPreviouslyConnected = mCurrentlyConnected;
73 mCurrentlyConnected = true;
74 mFrameEventHistory.getAndResetDelta(outDelta);
75 }
76}
77
78void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
79 const sp<Fence>& glDoneFence,
80 const sp<Fence>& presentFence,
81 const sp<Fence>& prevReleaseFence,
82 CompositorTiming compositorTiming,
83 nsecs_t latchTime, nsecs_t dequeueReadyTime) {
84 Mutex::Autolock lock(mFrameEventHistoryMutex);
85
86 // if the producer is not connected, don't bother updating,
87 // the next producer that connects won't access this frame event
88 if (!mCurrentlyConnected) return;
89 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
90 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
91 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
92
93 mFrameEventHistory.addLatch(frameNumber, latchTime);
94 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
95 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
96 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
97 compositorTiming);
98}
99
100void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
101 bool disconnect = false;
102 Mutex::Autolock lock(mFrameEventHistoryMutex);
103 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
104 disconnect = true;
105 mDisconnectEvents.pop();
106 }
107 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
108}
109
Vishnu Nairdab94092020-09-29 16:09:04 -0700110BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface,
111 int width, int height, bool enableTripleBuffering)
112 : mName(name),
113 mSurfaceControl(surface),
Vishnu Nairea0de002020-11-17 17:42:37 -0800114 mSize(width, height),
115 mRequestedSize(mSize),
Valerie Haud3b90d22019-11-06 09:37:31 -0800116 mNextTransaction(nullptr) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700117 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
Valerie Hau0889c622020-02-19 15:04:47 -0800118 // since the adapter is in the client process, set dequeue timeout
119 // explicitly so that dequeueBuffer will block
120 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
Valerie Hau65b8e872020-02-13 09:45:14 -0800121
Robert Carrcedef052020-04-22 15:58:23 -0700122 if (enableTripleBuffering) {
Valerie Hau65b8e872020-02-13 09:45:14 -0800123 mProducer->setMaxDequeuedBufferCount(2);
124 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700125 mBufferItemConsumer =
Robert Carr5b2ae912020-04-01 15:40:40 -0700126 new BLASTBufferItemConsumer(mConsumer, GraphicBuffer::USAGE_HW_COMPOSER, 1, true);
Valerie Haua32c5522019-12-09 10:11:08 -0800127 static int32_t id = 0;
Vishnu Nairdab94092020-09-29 16:09:04 -0700128 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(id);
Valerie Haua32c5522019-12-09 10:11:08 -0800129 id++;
Vishnu Nairdab94092020-09-29 16:09:04 -0700130 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700131 mBufferItemConsumer->setFrameAvailableListener(this);
132 mBufferItemConsumer->setBufferFreedListener(this);
Vishnu Nairea0de002020-11-17 17:42:37 -0800133 mBufferItemConsumer->setDefaultBufferSize(mSize.width, mSize.height);
Robert Carr78c25dd2019-08-15 14:10:33 -0700134 mBufferItemConsumer->setDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888);
Robert Carr9f133d72020-04-01 15:51:46 -0700135
Valerie Hau2882e982020-01-23 13:33:10 -0800136 mTransformHint = mSurfaceControl->getTransformHint();
Robert Carr9f133d72020-04-01 15:51:46 -0700137 mBufferItemConsumer->setTransformHint(mTransformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800138
Valerie Haua32c5522019-12-09 10:11:08 -0800139 mNumAcquired = 0;
140 mNumFrameAvailable = 0;
141 mPendingReleaseItem.item = BufferItem();
142 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700143}
144
Vishnu Nairdab94092020-09-29 16:09:04 -0700145void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700146 std::unique_lock _lock{mMutex};
147 mSurfaceControl = surface;
Robert Carrfc416512020-04-02 12:32:44 -0700148
Vishnu Nairea0de002020-11-17 17:42:37 -0800149 ui::Size newSize(width, height);
150 if (mRequestedSize != newSize) {
151 mRequestedSize.set(newSize);
152 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800153 if (mLastBufferScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
154 // If the buffer supports scaling, update the frame immediately since the client may
155 // want to scale the existing buffer to the new size.
156 mSize = mRequestedSize;
157 SurfaceComposerClient::Transaction t;
158 t.setFrame(mSurfaceControl,
159 {0, 0, static_cast<int32_t>(mSize.width),
160 static_cast<int32_t>(mSize.height)});
161 t.apply();
162 }
Robert Carrfc416512020-04-02 12:32:44 -0700163 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700164}
165
166static void transactionCallbackThunk(void* context, nsecs_t latchTime,
167 const sp<Fence>& presentFence,
168 const std::vector<SurfaceControlStats>& stats) {
169 if (context == nullptr) {
170 return;
171 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800172 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700173 bq->transactionCallback(latchTime, presentFence, stats);
174}
175
176void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
177 const std::vector<SurfaceControlStats>& stats) {
178 std::unique_lock _lock{mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -0800179 ATRACE_CALL();
Vishnu Nairdab94092020-09-29 16:09:04 -0700180 BQA_LOGV("transactionCallback");
Vishnu Nair670b3f72020-09-29 17:52:18 -0700181 mInitialCallbackReceived = true;
Vishnu Nairdab94092020-09-29 16:09:04 -0700182
Valerie Hau871d6352020-01-29 08:44:02 -0800183 if (!stats.empty()) {
184 mTransformHint = stats[0].transformHint;
185 mBufferItemConsumer->setTransformHint(mTransformHint);
186 mBufferItemConsumer->updateFrameTimestamps(stats[0].frameEventStats.frameNumber,
187 stats[0].frameEventStats.refreshStartTime,
188 stats[0].frameEventStats.gpuCompositionDoneFence,
189 stats[0].presentFence,
190 stats[0].previousReleaseFence,
191 stats[0].frameEventStats.compositorTiming,
192 stats[0].latchTime,
193 stats[0].frameEventStats.dequeueReadyTime);
194 }
Valerie Haua32c5522019-12-09 10:11:08 -0800195 if (mPendingReleaseItem.item.mGraphicBuffer != nullptr) {
Valerie Hau871d6352020-01-29 08:44:02 -0800196 if (!stats.empty()) {
Valerie Haua32c5522019-12-09 10:11:08 -0800197 mPendingReleaseItem.releaseFence = stats[0].previousReleaseFence;
Valerie Haua32c5522019-12-09 10:11:08 -0800198 } else {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700199 BQA_LOGE("Warning: no SurfaceControlStats returned in BLASTBufferQueue callback");
Valerie Haua32c5522019-12-09 10:11:08 -0800200 mPendingReleaseItem.releaseFence = nullptr;
201 }
202 mBufferItemConsumer->releaseBuffer(mPendingReleaseItem.item,
203 mPendingReleaseItem.releaseFence
204 ? mPendingReleaseItem.releaseFence
Robert Carr78c25dd2019-08-15 14:10:33 -0700205 : Fence::NO_FENCE);
Valerie Haua32c5522019-12-09 10:11:08 -0800206 mNumAcquired--;
207 mPendingReleaseItem.item = BufferItem();
208 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700209 }
Valerie Haua32c5522019-12-09 10:11:08 -0800210
211 if (mSubmitted.empty()) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700212 BQA_LOGE("ERROR: callback with no corresponding submitted buffer item");
Valerie Haua32c5522019-12-09 10:11:08 -0800213 }
214 mPendingReleaseItem.item = std::move(mSubmitted.front());
215 mSubmitted.pop();
Robert Carre4943eb2020-02-15 18:34:59 -0800216
Robert Carr255acdc2020-04-17 14:08:55 -0700217 processNextBufferLocked(false);
Robert Carre4943eb2020-02-15 18:34:59 -0800218
Valerie Haud3b90d22019-11-06 09:37:31 -0800219 mCallbackCV.notify_all();
Robert Carr78c25dd2019-08-15 14:10:33 -0700220 decStrong((void*)transactionCallbackThunk);
221}
222
Robert Carr255acdc2020-04-17 14:08:55 -0700223void BLASTBufferQueue::processNextBufferLocked(bool useNextTransaction) {
Valerie Haua32c5522019-12-09 10:11:08 -0800224 ATRACE_CALL();
Vishnu Nairdab94092020-09-29 16:09:04 -0700225 BQA_LOGV("processNextBufferLocked useNextTransaction=%s", toString(useNextTransaction));
226
Vishnu Nairbf255772020-10-16 10:54:41 -0700227 // Wait to acquire a buffer if there are no frames available or we have acquired the max
Vishnu Nair670b3f72020-09-29 17:52:18 -0700228 // number of buffers.
Vishnu Nairbf255772020-10-16 10:54:41 -0700229 if (mNumFrameAvailable == 0 || maxBuffersAcquired()) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700230 BQA_LOGV("processNextBufferLocked waiting for frame available or callback");
Vishnu Nairea0de002020-11-17 17:42:37 -0800231 mCallbackCV.notify_all();
Valerie Haud3b90d22019-11-06 09:37:31 -0800232 return;
233 }
234
Valerie Haua32c5522019-12-09 10:11:08 -0800235 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700236 BQA_LOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800237 return;
238 }
239
Robert Carr78c25dd2019-08-15 14:10:33 -0700240 SurfaceComposerClient::Transaction localTransaction;
241 bool applyTransaction = true;
242 SurfaceComposerClient::Transaction* t = &localTransaction;
Robert Carr255acdc2020-04-17 14:08:55 -0700243 if (mNextTransaction != nullptr && useNextTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700244 t = mNextTransaction;
245 mNextTransaction = nullptr;
246 applyTransaction = false;
247 }
248
Valerie Haua32c5522019-12-09 10:11:08 -0800249 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800250
Valerie Haua32c5522019-12-09 10:11:08 -0800251 status_t status = mBufferItemConsumer->acquireBuffer(&bufferItem, -1, false);
Robert Carr78c25dd2019-08-15 14:10:33 -0700252 if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700253 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Robert Carr78c25dd2019-08-15 14:10:33 -0700254 return;
255 }
Valerie Haua32c5522019-12-09 10:11:08 -0800256 auto buffer = bufferItem.mGraphicBuffer;
257 mNumFrameAvailable--;
258
259 if (buffer == nullptr) {
260 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700261 BQA_LOGE("Buffer was empty");
Valerie Haua32c5522019-12-09 10:11:08 -0800262 return;
263 }
264
Vishnu Nair670b3f72020-09-29 17:52:18 -0700265 if (rejectBuffer(bufferItem)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800266 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d"
267 "buffer{size=%dx%d transform=%d}",
268 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
269 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
270 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
271 processNextBufferLocked(useNextTransaction);
272 return;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700273 }
274
Valerie Haua32c5522019-12-09 10:11:08 -0800275 mNumAcquired++;
276 mSubmitted.push(bufferItem);
Robert Carr78c25dd2019-08-15 14:10:33 -0700277
Valerie Hau871d6352020-01-29 08:44:02 -0800278 bool needsDisconnect = false;
279 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
280
281 // if producer disconnected before, notify SurfaceFlinger
282 if (needsDisconnect) {
283 t->notifyProducerDisconnect(mSurfaceControl);
284 }
285
Robert Carr78c25dd2019-08-15 14:10:33 -0700286 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
287 incStrong((void*)transactionCallbackThunk);
288
Vishnu Nair53c936c2020-12-03 11:46:37 -0800289 mLastBufferScalingMode = bufferItem.mScalingMode;
290
Robert Carr78c25dd2019-08-15 14:10:33 -0700291 t->setBuffer(mSurfaceControl, buffer);
John Reck137069e2020-12-10 22:07:37 -0500292 t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
293 t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
294 t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
Robert Carr78c25dd2019-08-15 14:10:33 -0700295 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800296 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700297 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
298
Vishnu Nairdab94092020-09-29 16:09:04 -0700299 t->setFrame(mSurfaceControl,
Vishnu Nairea0de002020-11-17 17:42:37 -0800300 {0, 0, static_cast<int32_t>(mSize.width), static_cast<int32_t>(mSize.height)});
Valerie Haua32c5522019-12-09 10:11:08 -0800301 t->setCrop(mSurfaceControl, computeCrop(bufferItem));
302 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800303 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Valerie Hau181abd32020-01-27 14:18:28 -0800304 t->setDesiredPresentTime(bufferItem.mTimestamp);
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700305 t->setFrameNumber(mSurfaceControl, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700306
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100307 if (!mNextFrameTimelineVsyncIdQueue.empty()) {
308 t->setFrameTimelineVsync(mSurfaceControl, mNextFrameTimelineVsyncIdQueue.front());
309 mNextFrameTimelineVsyncIdQueue.pop();
310 }
311
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800312 if (mAutoRefresh != bufferItem.mAutoRefresh) {
313 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
314 mAutoRefresh = bufferItem.mAutoRefresh;
315 }
316
Robert Carr78c25dd2019-08-15 14:10:33 -0700317 if (applyTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700318 t->apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700319 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700320
321 BQA_LOGV("processNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
322 " applyTransaction=%s mTimestamp=%" PRId64,
Vishnu Nairea0de002020-11-17 17:42:37 -0800323 mSize.width, mSize.height, bufferItem.mFrameNumber, toString(applyTransaction),
Vishnu Nairdab94092020-09-29 16:09:04 -0700324 bufferItem.mTimestamp);
Robert Carr78c25dd2019-08-15 14:10:33 -0700325}
326
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800327Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
328 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800329 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800330 }
331 return item.mCrop;
332}
333
Vishnu Nairaef1de92020-10-22 12:15:53 -0700334void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Valerie Haua32c5522019-12-09 10:11:08 -0800335 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800336 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800337
Vishnu Nairdab94092020-09-29 16:09:04 -0700338 const bool nextTransactionSet = mNextTransaction != nullptr;
Vishnu Nairaef1de92020-10-22 12:15:53 -0700339 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " nextTransactionSet=%s mFlushShadowQueue=%s",
340 item.mFrameNumber, toString(nextTransactionSet), toString(mFlushShadowQueue));
Vishnu Nairdab94092020-09-29 16:09:04 -0700341
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700342 if (nextTransactionSet || mFlushShadowQueue) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700343 while (mNumFrameAvailable > 0 || maxBuffersAcquired()) {
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700344 BQA_LOGV("waiting in onFrameAvailable...");
Valerie Hau0188adf2020-02-13 08:29:20 -0800345 mCallbackCV.wait(_lock);
346 }
347 }
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700348 mFlushShadowQueue = false;
Valerie Haud3b90d22019-11-06 09:37:31 -0800349 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800350 mNumFrameAvailable++;
Robert Carr255acdc2020-04-17 14:08:55 -0700351 processNextBufferLocked(true);
Valerie Haud3b90d22019-11-06 09:37:31 -0800352}
353
Vishnu Nairaef1de92020-10-22 12:15:53 -0700354void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
355 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
356 // Do nothing since we are not storing unacquired buffer items locally.
357}
358
Robert Carr78c25dd2019-08-15 14:10:33 -0700359void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800360 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700361 mNextTransaction = t;
362}
363
Vishnu Nairea0de002020-11-17 17:42:37 -0800364bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700365 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
366 // Only reject buffers if scaling mode is freeze.
367 return false;
368 }
369
Vishnu Naire1a42322020-10-02 17:42:04 -0700370 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
371 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
372
373 // Take the buffer's orientation into account
374 if (item.mTransform & ui::Transform::ROT_90) {
375 std::swap(bufWidth, bufHeight);
376 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800377 ui::Size bufferSize(bufWidth, bufHeight);
378 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
379 mSize = mRequestedSize;
380 return false;
381 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700382
Vishnu Nair670b3f72020-09-29 17:52:18 -0700383 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800384 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700385}
Vishnu Nairbf255772020-10-16 10:54:41 -0700386
387// Check if we have acquired the maximum number of buffers.
388// As a special case, we wait for the first callback before acquiring the second buffer so we
389// can ensure the first buffer is presented if multiple buffers are queued in succession.
390bool BLASTBufferQueue::maxBuffersAcquired() const {
391 return mNumAcquired == MAX_ACQUIRED_BUFFERS + 1 ||
392 (!mInitialCallbackReceived && mNumAcquired == 1);
393}
394
Robert Carr05086b22020-10-13 18:22:51 -0700395class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700396private:
397 sp<BLASTBufferQueue> mBbq;
Robert Carr05086b22020-10-13 18:22:51 -0700398public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700399 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
400 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
401 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700402
Robert Carr05086b22020-10-13 18:22:51 -0700403 void allocateBuffers() override {
404 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
405 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
406 auto gbp = getIGraphicBufferProducer();
407 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
408 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
409 gbp->allocateBuffers(reqWidth, reqHeight,
410 reqFormat, reqUsage);
411
412 }).detach();
413 }
Robert Carr9c006e02020-10-14 13:41:57 -0700414
Marin Shalamanov46084422020-10-13 12:33:42 +0200415 status_t setFrameRate(float frameRate, int8_t compatibility, bool shouldBeSeamless) override {
Robert Carr9c006e02020-10-14 13:41:57 -0700416 if (!ValidateFrameRate(frameRate, compatibility, "BBQSurface::setFrameRate")) {
417 return BAD_VALUE;
418 }
Marin Shalamanov46084422020-10-13 12:33:42 +0200419 return mBbq->setFrameRate(frameRate, compatibility, shouldBeSeamless);
Robert Carr9c006e02020-10-14 13:41:57 -0700420 }
Robert Carr9b611b72020-10-19 12:00:23 -0700421
422 status_t setFrameTimelineVsync(int64_t frameTimelineVsyncId) override {
423 return mBbq->setFrameTimelineVsync(frameTimelineVsyncId);
424 }
Robert Carr05086b22020-10-13 18:22:51 -0700425};
426
Robert Carr9c006e02020-10-14 13:41:57 -0700427// TODO: Can we coalesce this with frame updates? Need to confirm
428// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200429status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
430 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700431 std::unique_lock _lock{mMutex};
432 SurfaceComposerClient::Transaction t;
433
Marin Shalamanov46084422020-10-13 12:33:42 +0200434 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700435}
436
Robert Carr9b611b72020-10-19 12:00:23 -0700437status_t BLASTBufferQueue::setFrameTimelineVsync(int64_t frameTimelineVsyncId) {
438 std::unique_lock _lock{mMutex};
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100439 mNextFrameTimelineVsyncIdQueue.push(frameTimelineVsyncId);
440 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700441}
442
Vishnu Nair992496b2020-10-22 17:27:21 -0700443sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
444 std::unique_lock _lock{mMutex};
445 sp<IBinder> scHandle = nullptr;
446 if (includeSurfaceControlHandle && mSurfaceControl) {
447 scHandle = mSurfaceControl->getHandle();
448 }
449 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700450}
451
Robert Carr78c25dd2019-08-15 14:10:33 -0700452} // namespace android