blob: 3a3a96fc3276a54e0c1191f00da8925eb63da8ee [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);
Robert Carrfc416512020-04-02 12:32:44 -0700153 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700154}
155
156static void transactionCallbackThunk(void* context, nsecs_t latchTime,
157 const sp<Fence>& presentFence,
158 const std::vector<SurfaceControlStats>& stats) {
159 if (context == nullptr) {
160 return;
161 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800162 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700163 bq->transactionCallback(latchTime, presentFence, stats);
164}
165
166void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
167 const std::vector<SurfaceControlStats>& stats) {
168 std::unique_lock _lock{mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -0800169 ATRACE_CALL();
Vishnu Nairdab94092020-09-29 16:09:04 -0700170 BQA_LOGV("transactionCallback");
Vishnu Nair670b3f72020-09-29 17:52:18 -0700171 mInitialCallbackReceived = true;
Vishnu Nairdab94092020-09-29 16:09:04 -0700172
Valerie Hau871d6352020-01-29 08:44:02 -0800173 if (!stats.empty()) {
174 mTransformHint = stats[0].transformHint;
175 mBufferItemConsumer->setTransformHint(mTransformHint);
176 mBufferItemConsumer->updateFrameTimestamps(stats[0].frameEventStats.frameNumber,
177 stats[0].frameEventStats.refreshStartTime,
178 stats[0].frameEventStats.gpuCompositionDoneFence,
179 stats[0].presentFence,
180 stats[0].previousReleaseFence,
181 stats[0].frameEventStats.compositorTiming,
182 stats[0].latchTime,
183 stats[0].frameEventStats.dequeueReadyTime);
184 }
Valerie Haua32c5522019-12-09 10:11:08 -0800185 if (mPendingReleaseItem.item.mGraphicBuffer != nullptr) {
Valerie Hau871d6352020-01-29 08:44:02 -0800186 if (!stats.empty()) {
Valerie Haua32c5522019-12-09 10:11:08 -0800187 mPendingReleaseItem.releaseFence = stats[0].previousReleaseFence;
Valerie Haua32c5522019-12-09 10:11:08 -0800188 } else {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700189 BQA_LOGE("Warning: no SurfaceControlStats returned in BLASTBufferQueue callback");
Valerie Haua32c5522019-12-09 10:11:08 -0800190 mPendingReleaseItem.releaseFence = nullptr;
191 }
192 mBufferItemConsumer->releaseBuffer(mPendingReleaseItem.item,
193 mPendingReleaseItem.releaseFence
194 ? mPendingReleaseItem.releaseFence
Robert Carr78c25dd2019-08-15 14:10:33 -0700195 : Fence::NO_FENCE);
Valerie Haua32c5522019-12-09 10:11:08 -0800196 mNumAcquired--;
197 mPendingReleaseItem.item = BufferItem();
198 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700199 }
Valerie Haua32c5522019-12-09 10:11:08 -0800200
201 if (mSubmitted.empty()) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700202 BQA_LOGE("ERROR: callback with no corresponding submitted buffer item");
Valerie Haua32c5522019-12-09 10:11:08 -0800203 }
204 mPendingReleaseItem.item = std::move(mSubmitted.front());
205 mSubmitted.pop();
Robert Carre4943eb2020-02-15 18:34:59 -0800206
Robert Carr255acdc2020-04-17 14:08:55 -0700207 processNextBufferLocked(false);
Robert Carre4943eb2020-02-15 18:34:59 -0800208
Valerie Haud3b90d22019-11-06 09:37:31 -0800209 mCallbackCV.notify_all();
Robert Carr78c25dd2019-08-15 14:10:33 -0700210 decStrong((void*)transactionCallbackThunk);
211}
212
Robert Carr255acdc2020-04-17 14:08:55 -0700213void BLASTBufferQueue::processNextBufferLocked(bool useNextTransaction) {
Valerie Haua32c5522019-12-09 10:11:08 -0800214 ATRACE_CALL();
Vishnu Nairdab94092020-09-29 16:09:04 -0700215 BQA_LOGV("processNextBufferLocked useNextTransaction=%s", toString(useNextTransaction));
216
Vishnu Nairbf255772020-10-16 10:54:41 -0700217 // Wait to acquire a buffer if there are no frames available or we have acquired the max
Vishnu Nair670b3f72020-09-29 17:52:18 -0700218 // number of buffers.
Vishnu Nairbf255772020-10-16 10:54:41 -0700219 if (mNumFrameAvailable == 0 || maxBuffersAcquired()) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700220 BQA_LOGV("processNextBufferLocked waiting for frame available or callback");
Vishnu Nairea0de002020-11-17 17:42:37 -0800221 mCallbackCV.notify_all();
Valerie Haud3b90d22019-11-06 09:37:31 -0800222 return;
223 }
224
Valerie Haua32c5522019-12-09 10:11:08 -0800225 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700226 BQA_LOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800227 return;
228 }
229
Robert Carr78c25dd2019-08-15 14:10:33 -0700230 SurfaceComposerClient::Transaction localTransaction;
231 bool applyTransaction = true;
232 SurfaceComposerClient::Transaction* t = &localTransaction;
Robert Carr255acdc2020-04-17 14:08:55 -0700233 if (mNextTransaction != nullptr && useNextTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700234 t = mNextTransaction;
235 mNextTransaction = nullptr;
236 applyTransaction = false;
237 }
238
Valerie Haua32c5522019-12-09 10:11:08 -0800239 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800240
Valerie Haua32c5522019-12-09 10:11:08 -0800241 status_t status = mBufferItemConsumer->acquireBuffer(&bufferItem, -1, false);
Robert Carr78c25dd2019-08-15 14:10:33 -0700242 if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700243 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Robert Carr78c25dd2019-08-15 14:10:33 -0700244 return;
245 }
Valerie Haua32c5522019-12-09 10:11:08 -0800246 auto buffer = bufferItem.mGraphicBuffer;
247 mNumFrameAvailable--;
248
249 if (buffer == nullptr) {
250 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700251 BQA_LOGE("Buffer was empty");
Valerie Haua32c5522019-12-09 10:11:08 -0800252 return;
253 }
254
Vishnu Nair670b3f72020-09-29 17:52:18 -0700255 if (rejectBuffer(bufferItem)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800256 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d"
257 "buffer{size=%dx%d transform=%d}",
258 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
259 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
260 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
261 processNextBufferLocked(useNextTransaction);
262 return;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700263 }
264
Valerie Haua32c5522019-12-09 10:11:08 -0800265 mNumAcquired++;
266 mSubmitted.push(bufferItem);
Robert Carr78c25dd2019-08-15 14:10:33 -0700267
Valerie Hau871d6352020-01-29 08:44:02 -0800268 bool needsDisconnect = false;
269 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
270
271 // if producer disconnected before, notify SurfaceFlinger
272 if (needsDisconnect) {
273 t->notifyProducerDisconnect(mSurfaceControl);
274 }
275
Robert Carr78c25dd2019-08-15 14:10:33 -0700276 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
277 incStrong((void*)transactionCallbackThunk);
278
279 t->setBuffer(mSurfaceControl, buffer);
280 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800281 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700282 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
283
Vishnu Nairdab94092020-09-29 16:09:04 -0700284 t->setFrame(mSurfaceControl,
Vishnu Nairea0de002020-11-17 17:42:37 -0800285 {0, 0, static_cast<int32_t>(mSize.width), static_cast<int32_t>(mSize.height)});
Valerie Haua32c5522019-12-09 10:11:08 -0800286 t->setCrop(mSurfaceControl, computeCrop(bufferItem));
287 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800288 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Valerie Hau181abd32020-01-27 14:18:28 -0800289 t->setDesiredPresentTime(bufferItem.mTimestamp);
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700290 t->setFrameNumber(mSurfaceControl, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700291
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100292 if (!mNextFrameTimelineVsyncIdQueue.empty()) {
293 t->setFrameTimelineVsync(mSurfaceControl, mNextFrameTimelineVsyncIdQueue.front());
294 mNextFrameTimelineVsyncIdQueue.pop();
295 }
296
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800297 if (mAutoRefresh != bufferItem.mAutoRefresh) {
298 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
299 mAutoRefresh = bufferItem.mAutoRefresh;
300 }
301
Robert Carr78c25dd2019-08-15 14:10:33 -0700302 if (applyTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700303 t->apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700304 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700305
306 BQA_LOGV("processNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
307 " applyTransaction=%s mTimestamp=%" PRId64,
Vishnu Nairea0de002020-11-17 17:42:37 -0800308 mSize.width, mSize.height, bufferItem.mFrameNumber, toString(applyTransaction),
Vishnu Nairdab94092020-09-29 16:09:04 -0700309 bufferItem.mTimestamp);
Robert Carr78c25dd2019-08-15 14:10:33 -0700310}
311
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800312Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
313 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800314 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800315 }
316 return item.mCrop;
317}
318
Vishnu Nairaef1de92020-10-22 12:15:53 -0700319void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Valerie Haua32c5522019-12-09 10:11:08 -0800320 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800321 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800322
Vishnu Nairdab94092020-09-29 16:09:04 -0700323 const bool nextTransactionSet = mNextTransaction != nullptr;
Vishnu Nairaef1de92020-10-22 12:15:53 -0700324 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " nextTransactionSet=%s mFlushShadowQueue=%s",
325 item.mFrameNumber, toString(nextTransactionSet), toString(mFlushShadowQueue));
Vishnu Nairdab94092020-09-29 16:09:04 -0700326
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700327 if (nextTransactionSet || mFlushShadowQueue) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700328 while (mNumFrameAvailable > 0 || maxBuffersAcquired()) {
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700329 BQA_LOGV("waiting in onFrameAvailable...");
Valerie Hau0188adf2020-02-13 08:29:20 -0800330 mCallbackCV.wait(_lock);
331 }
332 }
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700333 mFlushShadowQueue = false;
Valerie Haud3b90d22019-11-06 09:37:31 -0800334 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800335 mNumFrameAvailable++;
Robert Carr255acdc2020-04-17 14:08:55 -0700336 processNextBufferLocked(true);
Valerie Haud3b90d22019-11-06 09:37:31 -0800337}
338
Vishnu Nairaef1de92020-10-22 12:15:53 -0700339void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
340 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
341 // Do nothing since we are not storing unacquired buffer items locally.
342}
343
Robert Carr78c25dd2019-08-15 14:10:33 -0700344void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800345 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700346 mNextTransaction = t;
347}
348
Vishnu Nairea0de002020-11-17 17:42:37 -0800349bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700350 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800351 mSize = mRequestedSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700352 // Only reject buffers if scaling mode is freeze.
353 return false;
354 }
355
Vishnu Naire1a42322020-10-02 17:42:04 -0700356 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
357 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
358
359 // Take the buffer's orientation into account
360 if (item.mTransform & ui::Transform::ROT_90) {
361 std::swap(bufWidth, bufHeight);
362 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800363 ui::Size bufferSize(bufWidth, bufHeight);
364 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
365 mSize = mRequestedSize;
366 return false;
367 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700368
Vishnu Nair670b3f72020-09-29 17:52:18 -0700369 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800370 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700371}
Vishnu Nairbf255772020-10-16 10:54:41 -0700372
373// Check if we have acquired the maximum number of buffers.
374// As a special case, we wait for the first callback before acquiring the second buffer so we
375// can ensure the first buffer is presented if multiple buffers are queued in succession.
376bool BLASTBufferQueue::maxBuffersAcquired() const {
377 return mNumAcquired == MAX_ACQUIRED_BUFFERS + 1 ||
378 (!mInitialCallbackReceived && mNumAcquired == 1);
379}
380
Robert Carr05086b22020-10-13 18:22:51 -0700381class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700382private:
383 sp<BLASTBufferQueue> mBbq;
Robert Carr05086b22020-10-13 18:22:51 -0700384public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700385 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
386 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
387 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700388
Robert Carr05086b22020-10-13 18:22:51 -0700389 void allocateBuffers() override {
390 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
391 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
392 auto gbp = getIGraphicBufferProducer();
393 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
394 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
395 gbp->allocateBuffers(reqWidth, reqHeight,
396 reqFormat, reqUsage);
397
398 }).detach();
399 }
Robert Carr9c006e02020-10-14 13:41:57 -0700400
Marin Shalamanov46084422020-10-13 12:33:42 +0200401 status_t setFrameRate(float frameRate, int8_t compatibility, bool shouldBeSeamless) override {
Robert Carr9c006e02020-10-14 13:41:57 -0700402 if (!ValidateFrameRate(frameRate, compatibility, "BBQSurface::setFrameRate")) {
403 return BAD_VALUE;
404 }
Marin Shalamanov46084422020-10-13 12:33:42 +0200405 return mBbq->setFrameRate(frameRate, compatibility, shouldBeSeamless);
Robert Carr9c006e02020-10-14 13:41:57 -0700406 }
Robert Carr9b611b72020-10-19 12:00:23 -0700407
408 status_t setFrameTimelineVsync(int64_t frameTimelineVsyncId) override {
409 return mBbq->setFrameTimelineVsync(frameTimelineVsyncId);
410 }
Robert Carr05086b22020-10-13 18:22:51 -0700411};
412
Robert Carr9c006e02020-10-14 13:41:57 -0700413// TODO: Can we coalesce this with frame updates? Need to confirm
414// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200415status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
416 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700417 std::unique_lock _lock{mMutex};
418 SurfaceComposerClient::Transaction t;
419
Marin Shalamanov46084422020-10-13 12:33:42 +0200420 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700421}
422
Robert Carr9b611b72020-10-19 12:00:23 -0700423status_t BLASTBufferQueue::setFrameTimelineVsync(int64_t frameTimelineVsyncId) {
424 std::unique_lock _lock{mMutex};
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100425 mNextFrameTimelineVsyncIdQueue.push(frameTimelineVsyncId);
426 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700427}
428
Vishnu Nair992496b2020-10-22 17:27:21 -0700429sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
430 std::unique_lock _lock{mMutex};
431 sp<IBinder> scHandle = nullptr;
432 if (includeSurfaceControlHandle && mSurfaceControl) {
433 scHandle = mSurfaceControl->getHandle();
434 }
435 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700436}
437
Robert Carr78c25dd2019-08-15 14:10:33 -0700438} // namespace android