blob: e6aa02a8fe532d85971d886057dbc22a1e4efdee [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
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800292 if (mAutoRefresh != bufferItem.mAutoRefresh) {
293 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
294 mAutoRefresh = bufferItem.mAutoRefresh;
295 }
296
Robert Carr78c25dd2019-08-15 14:10:33 -0700297 if (applyTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700298 t->apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700299 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700300
301 BQA_LOGV("processNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
302 " applyTransaction=%s mTimestamp=%" PRId64,
Vishnu Nairea0de002020-11-17 17:42:37 -0800303 mSize.width, mSize.height, bufferItem.mFrameNumber, toString(applyTransaction),
Vishnu Nairdab94092020-09-29 16:09:04 -0700304 bufferItem.mTimestamp);
Robert Carr78c25dd2019-08-15 14:10:33 -0700305}
306
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800307Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
308 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800309 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800310 }
311 return item.mCrop;
312}
313
Vishnu Nairaef1de92020-10-22 12:15:53 -0700314void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Valerie Haua32c5522019-12-09 10:11:08 -0800315 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800316 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800317
Vishnu Nairdab94092020-09-29 16:09:04 -0700318 const bool nextTransactionSet = mNextTransaction != nullptr;
Vishnu Nairaef1de92020-10-22 12:15:53 -0700319 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " nextTransactionSet=%s mFlushShadowQueue=%s",
320 item.mFrameNumber, toString(nextTransactionSet), toString(mFlushShadowQueue));
Vishnu Nairdab94092020-09-29 16:09:04 -0700321
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700322 if (nextTransactionSet || mFlushShadowQueue) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700323 while (mNumFrameAvailable > 0 || maxBuffersAcquired()) {
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700324 BQA_LOGV("waiting in onFrameAvailable...");
Valerie Hau0188adf2020-02-13 08:29:20 -0800325 mCallbackCV.wait(_lock);
326 }
327 }
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700328 mFlushShadowQueue = false;
Valerie Haud3b90d22019-11-06 09:37:31 -0800329 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800330 mNumFrameAvailable++;
Robert Carr255acdc2020-04-17 14:08:55 -0700331 processNextBufferLocked(true);
Valerie Haud3b90d22019-11-06 09:37:31 -0800332}
333
Vishnu Nairaef1de92020-10-22 12:15:53 -0700334void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
335 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
336 // Do nothing since we are not storing unacquired buffer items locally.
337}
338
Robert Carr78c25dd2019-08-15 14:10:33 -0700339void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800340 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700341 mNextTransaction = t;
342}
343
Vishnu Nairea0de002020-11-17 17:42:37 -0800344bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700345 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800346 mSize = mRequestedSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700347 // Only reject buffers if scaling mode is freeze.
348 return false;
349 }
350
Vishnu Naire1a42322020-10-02 17:42:04 -0700351 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
352 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
353
354 // Take the buffer's orientation into account
355 if (item.mTransform & ui::Transform::ROT_90) {
356 std::swap(bufWidth, bufHeight);
357 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800358 ui::Size bufferSize(bufWidth, bufHeight);
359 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
360 mSize = mRequestedSize;
361 return false;
362 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700363
Vishnu Nair670b3f72020-09-29 17:52:18 -0700364 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800365 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700366}
Vishnu Nairbf255772020-10-16 10:54:41 -0700367
368// Check if we have acquired the maximum number of buffers.
369// As a special case, we wait for the first callback before acquiring the second buffer so we
370// can ensure the first buffer is presented if multiple buffers are queued in succession.
371bool BLASTBufferQueue::maxBuffersAcquired() const {
372 return mNumAcquired == MAX_ACQUIRED_BUFFERS + 1 ||
373 (!mInitialCallbackReceived && mNumAcquired == 1);
374}
375
Robert Carr05086b22020-10-13 18:22:51 -0700376class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700377private:
378 sp<BLASTBufferQueue> mBbq;
Robert Carr05086b22020-10-13 18:22:51 -0700379public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700380 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
381 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
382 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700383
Robert Carr05086b22020-10-13 18:22:51 -0700384 void allocateBuffers() override {
385 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
386 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
387 auto gbp = getIGraphicBufferProducer();
388 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
389 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
390 gbp->allocateBuffers(reqWidth, reqHeight,
391 reqFormat, reqUsage);
392
393 }).detach();
394 }
Robert Carr9c006e02020-10-14 13:41:57 -0700395
Marin Shalamanov46084422020-10-13 12:33:42 +0200396 status_t setFrameRate(float frameRate, int8_t compatibility, bool shouldBeSeamless) override {
Robert Carr9c006e02020-10-14 13:41:57 -0700397 if (!ValidateFrameRate(frameRate, compatibility, "BBQSurface::setFrameRate")) {
398 return BAD_VALUE;
399 }
Marin Shalamanov46084422020-10-13 12:33:42 +0200400 return mBbq->setFrameRate(frameRate, compatibility, shouldBeSeamless);
Robert Carr9c006e02020-10-14 13:41:57 -0700401 }
Robert Carr9b611b72020-10-19 12:00:23 -0700402
403 status_t setFrameTimelineVsync(int64_t frameTimelineVsyncId) override {
404 return mBbq->setFrameTimelineVsync(frameTimelineVsyncId);
405 }
Robert Carr05086b22020-10-13 18:22:51 -0700406};
407
Robert Carr9c006e02020-10-14 13:41:57 -0700408// TODO: Can we coalesce this with frame updates? Need to confirm
409// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200410status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
411 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700412 std::unique_lock _lock{mMutex};
413 SurfaceComposerClient::Transaction t;
414
Marin Shalamanov46084422020-10-13 12:33:42 +0200415 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700416}
417
Robert Carr9b611b72020-10-19 12:00:23 -0700418status_t BLASTBufferQueue::setFrameTimelineVsync(int64_t frameTimelineVsyncId) {
419 std::unique_lock _lock{mMutex};
420 SurfaceComposerClient::Transaction t;
421
422 return t.setFrameTimelineVsync(mSurfaceControl, frameTimelineVsyncId)
423 .apply();
424}
425
Vishnu Nair992496b2020-10-22 17:27:21 -0700426sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
427 std::unique_lock _lock{mMutex};
428 sp<IBinder> scHandle = nullptr;
429 if (includeSurfaceControlHandle && mSurfaceControl) {
430 scHandle = mSurfaceControl->getHandle();
431 }
432 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700433}
434
Robert Carr78c25dd2019-08-15 14:10:33 -0700435} // namespace android