blob: 5773d64704c791ed8a64c11e4a446a5559bf4446 [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__)
Vishnu Nairc6f89ee2020-12-11 14:27:32 -080045// enable logs for a single layer
46//#define BQA_LOGV(x, ...) \
47// ALOGV_IF((strstr(mName.c_str(), "SurfaceView") != nullptr), "[%s](f:%u,a:%u) " x, \
48// mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
Vishnu Nairdab94092020-09-29 16:09:04 -070049#define BQA_LOGE(x, ...) \
50 ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__)
51
Valerie Hau871d6352020-01-29 08:44:02 -080052void BLASTBufferItemConsumer::onDisconnect() {
53 Mutex::Autolock lock(mFrameEventHistoryMutex);
54 mPreviouslyConnected = mCurrentlyConnected;
55 mCurrentlyConnected = false;
56 if (mPreviouslyConnected) {
57 mDisconnectEvents.push(mCurrentFrameNumber);
58 }
59 mFrameEventHistory.onDisconnect();
60}
61
62void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
63 FrameEventHistoryDelta* outDelta) {
64 Mutex::Autolock lock(mFrameEventHistoryMutex);
65 if (newTimestamps) {
66 // BufferQueueProducer only adds a new timestamp on
67 // queueBuffer
68 mCurrentFrameNumber = newTimestamps->frameNumber;
69 mFrameEventHistory.addQueue(*newTimestamps);
70 }
71 if (outDelta) {
72 // frame event histories will be processed
73 // only after the producer connects and requests
74 // deltas for the first time. Forward this intent
75 // to SF-side to turn event processing back on
76 mPreviouslyConnected = mCurrentlyConnected;
77 mCurrentlyConnected = true;
78 mFrameEventHistory.getAndResetDelta(outDelta);
79 }
80}
81
82void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
83 const sp<Fence>& glDoneFence,
84 const sp<Fence>& presentFence,
85 const sp<Fence>& prevReleaseFence,
86 CompositorTiming compositorTiming,
87 nsecs_t latchTime, nsecs_t dequeueReadyTime) {
88 Mutex::Autolock lock(mFrameEventHistoryMutex);
89
90 // if the producer is not connected, don't bother updating,
91 // the next producer that connects won't access this frame event
92 if (!mCurrentlyConnected) return;
93 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
94 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
95 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
96
97 mFrameEventHistory.addLatch(frameNumber, latchTime);
98 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
99 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
100 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
101 compositorTiming);
102}
103
104void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
105 bool disconnect = false;
106 Mutex::Autolock lock(mFrameEventHistoryMutex);
107 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
108 disconnect = true;
109 mDisconnectEvents.pop();
110 }
111 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
112}
113
Vishnu Nairdab94092020-09-29 16:09:04 -0700114BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface,
115 int width, int height, bool enableTripleBuffering)
116 : mName(name),
117 mSurfaceControl(surface),
Vishnu Nairea0de002020-11-17 17:42:37 -0800118 mSize(width, height),
119 mRequestedSize(mSize),
Valerie Haud3b90d22019-11-06 09:37:31 -0800120 mNextTransaction(nullptr) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700121 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
Valerie Hau0889c622020-02-19 15:04:47 -0800122 // since the adapter is in the client process, set dequeue timeout
123 // explicitly so that dequeueBuffer will block
124 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
Valerie Hau65b8e872020-02-13 09:45:14 -0800125
Robert Carrcedef052020-04-22 15:58:23 -0700126 if (enableTripleBuffering) {
Valerie Hau65b8e872020-02-13 09:45:14 -0800127 mProducer->setMaxDequeuedBufferCount(2);
128 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700129 mBufferItemConsumer =
Robert Carr5b2ae912020-04-01 15:40:40 -0700130 new BLASTBufferItemConsumer(mConsumer, GraphicBuffer::USAGE_HW_COMPOSER, 1, true);
Valerie Haua32c5522019-12-09 10:11:08 -0800131 static int32_t id = 0;
Vishnu Nairdab94092020-09-29 16:09:04 -0700132 auto consumerName = mName + "(BLAST Consumer)" + std::to_string(id);
Valerie Haua32c5522019-12-09 10:11:08 -0800133 id++;
Vishnu Nairdab94092020-09-29 16:09:04 -0700134 mBufferItemConsumer->setName(String8(consumerName.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700135 mBufferItemConsumer->setFrameAvailableListener(this);
136 mBufferItemConsumer->setBufferFreedListener(this);
Vishnu Nairea0de002020-11-17 17:42:37 -0800137 mBufferItemConsumer->setDefaultBufferSize(mSize.width, mSize.height);
Robert Carr78c25dd2019-08-15 14:10:33 -0700138 mBufferItemConsumer->setDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888);
Robert Carr9f133d72020-04-01 15:51:46 -0700139
Valerie Hau2882e982020-01-23 13:33:10 -0800140 mTransformHint = mSurfaceControl->getTransformHint();
Robert Carr9f133d72020-04-01 15:51:46 -0700141 mBufferItemConsumer->setTransformHint(mTransformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800142
Valerie Haua32c5522019-12-09 10:11:08 -0800143 mNumAcquired = 0;
144 mNumFrameAvailable = 0;
145 mPendingReleaseItem.item = BufferItem();
146 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700147}
148
Vishnu Nairdab94092020-09-29 16:09:04 -0700149void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700150 std::unique_lock _lock{mMutex};
151 mSurfaceControl = surface;
Robert Carrfc416512020-04-02 12:32:44 -0700152
Vishnu Nairea0de002020-11-17 17:42:37 -0800153 ui::Size newSize(width, height);
154 if (mRequestedSize != newSize) {
155 mRequestedSize.set(newSize);
156 mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height);
Vishnu Nair53c936c2020-12-03 11:46:37 -0800157 if (mLastBufferScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
158 // If the buffer supports scaling, update the frame immediately since the client may
159 // want to scale the existing buffer to the new size.
160 mSize = mRequestedSize;
161 SurfaceComposerClient::Transaction t;
162 t.setFrame(mSurfaceControl,
163 {0, 0, static_cast<int32_t>(mSize.width),
164 static_cast<int32_t>(mSize.height)});
165 t.apply();
166 }
Robert Carrfc416512020-04-02 12:32:44 -0700167 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700168}
169
170static void transactionCallbackThunk(void* context, nsecs_t latchTime,
171 const sp<Fence>& presentFence,
172 const std::vector<SurfaceControlStats>& stats) {
173 if (context == nullptr) {
174 return;
175 }
Robert Carrfbcbb4c2020-11-02 14:14:34 -0800176 sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context);
Robert Carr78c25dd2019-08-15 14:10:33 -0700177 bq->transactionCallback(latchTime, presentFence, stats);
178}
179
180void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
181 const std::vector<SurfaceControlStats>& stats) {
182 std::unique_lock _lock{mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -0800183 ATRACE_CALL();
Vishnu Nairdab94092020-09-29 16:09:04 -0700184 BQA_LOGV("transactionCallback");
Vishnu Nair670b3f72020-09-29 17:52:18 -0700185 mInitialCallbackReceived = true;
Vishnu Nairdab94092020-09-29 16:09:04 -0700186
Valerie Hau871d6352020-01-29 08:44:02 -0800187 if (!stats.empty()) {
188 mTransformHint = stats[0].transformHint;
189 mBufferItemConsumer->setTransformHint(mTransformHint);
190 mBufferItemConsumer->updateFrameTimestamps(stats[0].frameEventStats.frameNumber,
191 stats[0].frameEventStats.refreshStartTime,
192 stats[0].frameEventStats.gpuCompositionDoneFence,
193 stats[0].presentFence,
194 stats[0].previousReleaseFence,
195 stats[0].frameEventStats.compositorTiming,
196 stats[0].latchTime,
197 stats[0].frameEventStats.dequeueReadyTime);
198 }
Valerie Haua32c5522019-12-09 10:11:08 -0800199 if (mPendingReleaseItem.item.mGraphicBuffer != nullptr) {
Valerie Hau871d6352020-01-29 08:44:02 -0800200 if (!stats.empty()) {
Valerie Haua32c5522019-12-09 10:11:08 -0800201 mPendingReleaseItem.releaseFence = stats[0].previousReleaseFence;
Valerie Haua32c5522019-12-09 10:11:08 -0800202 } else {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700203 BQA_LOGE("Warning: no SurfaceControlStats returned in BLASTBufferQueue callback");
Valerie Haua32c5522019-12-09 10:11:08 -0800204 mPendingReleaseItem.releaseFence = nullptr;
205 }
206 mBufferItemConsumer->releaseBuffer(mPendingReleaseItem.item,
207 mPendingReleaseItem.releaseFence
208 ? mPendingReleaseItem.releaseFence
Robert Carr78c25dd2019-08-15 14:10:33 -0700209 : Fence::NO_FENCE);
Valerie Haua32c5522019-12-09 10:11:08 -0800210 mNumAcquired--;
211 mPendingReleaseItem.item = BufferItem();
212 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700213 }
Valerie Haua32c5522019-12-09 10:11:08 -0800214
215 if (mSubmitted.empty()) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700216 BQA_LOGE("ERROR: callback with no corresponding submitted buffer item");
Valerie Haua32c5522019-12-09 10:11:08 -0800217 }
218 mPendingReleaseItem.item = std::move(mSubmitted.front());
219 mSubmitted.pop();
Robert Carre4943eb2020-02-15 18:34:59 -0800220
Robert Carr255acdc2020-04-17 14:08:55 -0700221 processNextBufferLocked(false);
Robert Carre4943eb2020-02-15 18:34:59 -0800222
Valerie Haud3b90d22019-11-06 09:37:31 -0800223 mCallbackCV.notify_all();
Robert Carr78c25dd2019-08-15 14:10:33 -0700224 decStrong((void*)transactionCallbackThunk);
225}
226
Robert Carr255acdc2020-04-17 14:08:55 -0700227void BLASTBufferQueue::processNextBufferLocked(bool useNextTransaction) {
Valerie Haua32c5522019-12-09 10:11:08 -0800228 ATRACE_CALL();
Vishnu Nairdab94092020-09-29 16:09:04 -0700229 BQA_LOGV("processNextBufferLocked useNextTransaction=%s", toString(useNextTransaction));
230
Vishnu Nairbf255772020-10-16 10:54:41 -0700231 // Wait to acquire a buffer if there are no frames available or we have acquired the max
Vishnu Nair670b3f72020-09-29 17:52:18 -0700232 // number of buffers.
Vishnu Nairbf255772020-10-16 10:54:41 -0700233 if (mNumFrameAvailable == 0 || maxBuffersAcquired()) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700234 BQA_LOGV("processNextBufferLocked waiting for frame available or callback");
Vishnu Nairea0de002020-11-17 17:42:37 -0800235 mCallbackCV.notify_all();
Valerie Haud3b90d22019-11-06 09:37:31 -0800236 return;
237 }
238
Valerie Haua32c5522019-12-09 10:11:08 -0800239 if (mSurfaceControl == nullptr) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700240 BQA_LOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800241 return;
242 }
243
Robert Carr78c25dd2019-08-15 14:10:33 -0700244 SurfaceComposerClient::Transaction localTransaction;
245 bool applyTransaction = true;
246 SurfaceComposerClient::Transaction* t = &localTransaction;
Robert Carr255acdc2020-04-17 14:08:55 -0700247 if (mNextTransaction != nullptr && useNextTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700248 t = mNextTransaction;
249 mNextTransaction = nullptr;
250 applyTransaction = false;
251 }
252
Valerie Haua32c5522019-12-09 10:11:08 -0800253 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800254
Vishnu Nairc6f89ee2020-12-11 14:27:32 -0800255 status_t status =
256 mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
Robert Carr78c25dd2019-08-15 14:10:33 -0700257 if (status != OK) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700258 BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str());
Robert Carr78c25dd2019-08-15 14:10:33 -0700259 return;
260 }
Valerie Haua32c5522019-12-09 10:11:08 -0800261 auto buffer = bufferItem.mGraphicBuffer;
262 mNumFrameAvailable--;
263
264 if (buffer == nullptr) {
265 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
Vishnu Nairbf255772020-10-16 10:54:41 -0700266 BQA_LOGE("Buffer was empty");
Valerie Haua32c5522019-12-09 10:11:08 -0800267 return;
268 }
269
Vishnu Nair670b3f72020-09-29 17:52:18 -0700270 if (rejectBuffer(bufferItem)) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800271 BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d"
272 "buffer{size=%dx%d transform=%d}",
273 mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height,
274 buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform);
275 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
276 processNextBufferLocked(useNextTransaction);
277 return;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700278 }
279
Valerie Haua32c5522019-12-09 10:11:08 -0800280 mNumAcquired++;
281 mSubmitted.push(bufferItem);
Robert Carr78c25dd2019-08-15 14:10:33 -0700282
Valerie Hau871d6352020-01-29 08:44:02 -0800283 bool needsDisconnect = false;
284 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
285
286 // if producer disconnected before, notify SurfaceFlinger
287 if (needsDisconnect) {
288 t->notifyProducerDisconnect(mSurfaceControl);
289 }
290
Robert Carr78c25dd2019-08-15 14:10:33 -0700291 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
292 incStrong((void*)transactionCallbackThunk);
293
Vishnu Nair53c936c2020-12-03 11:46:37 -0800294 mLastBufferScalingMode = bufferItem.mScalingMode;
295
Robert Carr78c25dd2019-08-15 14:10:33 -0700296 t->setBuffer(mSurfaceControl, buffer);
297 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800298 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700299 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
300
Vishnu Nairdab94092020-09-29 16:09:04 -0700301 t->setFrame(mSurfaceControl,
Vishnu Nairea0de002020-11-17 17:42:37 -0800302 {0, 0, static_cast<int32_t>(mSize.width), static_cast<int32_t>(mSize.height)});
Valerie Haua32c5522019-12-09 10:11:08 -0800303 t->setCrop(mSurfaceControl, computeCrop(bufferItem));
304 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800305 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Valerie Hau181abd32020-01-27 14:18:28 -0800306 t->setDesiredPresentTime(bufferItem.mTimestamp);
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700307 t->setFrameNumber(mSurfaceControl, bufferItem.mFrameNumber);
Robert Carr78c25dd2019-08-15 14:10:33 -0700308
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100309 if (!mNextFrameTimelineVsyncIdQueue.empty()) {
310 t->setFrameTimelineVsync(mSurfaceControl, mNextFrameTimelineVsyncIdQueue.front());
311 mNextFrameTimelineVsyncIdQueue.pop();
312 }
313
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800314 if (mAutoRefresh != bufferItem.mAutoRefresh) {
315 t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
316 mAutoRefresh = bufferItem.mAutoRefresh;
317 }
318
Robert Carr78c25dd2019-08-15 14:10:33 -0700319 if (applyTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700320 t->apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700321 }
Vishnu Nairdab94092020-09-29 16:09:04 -0700322
323 BQA_LOGV("processNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
324 " applyTransaction=%s mTimestamp=%" PRId64,
Vishnu Nairea0de002020-11-17 17:42:37 -0800325 mSize.width, mSize.height, bufferItem.mFrameNumber, toString(applyTransaction),
Vishnu Nairdab94092020-09-29 16:09:04 -0700326 bufferItem.mTimestamp);
Robert Carr78c25dd2019-08-15 14:10:33 -0700327}
328
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800329Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
330 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Vishnu Nairea0de002020-11-17 17:42:37 -0800331 return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height);
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800332 }
333 return item.mCrop;
334}
335
Vishnu Nairaef1de92020-10-22 12:15:53 -0700336void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) {
Valerie Haua32c5522019-12-09 10:11:08 -0800337 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800338 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800339
Vishnu Nairdab94092020-09-29 16:09:04 -0700340 const bool nextTransactionSet = mNextTransaction != nullptr;
Vishnu Nairaef1de92020-10-22 12:15:53 -0700341 BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " nextTransactionSet=%s mFlushShadowQueue=%s",
342 item.mFrameNumber, toString(nextTransactionSet), toString(mFlushShadowQueue));
Vishnu Nairdab94092020-09-29 16:09:04 -0700343
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700344 if (nextTransactionSet || mFlushShadowQueue) {
Vishnu Nairbf255772020-10-16 10:54:41 -0700345 while (mNumFrameAvailable > 0 || maxBuffersAcquired()) {
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700346 BQA_LOGV("waiting in onFrameAvailable...");
Valerie Hau0188adf2020-02-13 08:29:20 -0800347 mCallbackCV.wait(_lock);
348 }
349 }
Vishnu Nair7eb670a2020-10-15 12:16:10 -0700350 mFlushShadowQueue = false;
Valerie Haud3b90d22019-11-06 09:37:31 -0800351 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800352 mNumFrameAvailable++;
Robert Carr255acdc2020-04-17 14:08:55 -0700353 processNextBufferLocked(true);
Valerie Haud3b90d22019-11-06 09:37:31 -0800354}
355
Vishnu Nairaef1de92020-10-22 12:15:53 -0700356void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) {
357 BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber);
358 // Do nothing since we are not storing unacquired buffer items locally.
359}
360
Robert Carr78c25dd2019-08-15 14:10:33 -0700361void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800362 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700363 mNextTransaction = t;
364}
365
Vishnu Nairea0de002020-11-17 17:42:37 -0800366bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) {
Vishnu Nair670b3f72020-09-29 17:52:18 -0700367 if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
368 // Only reject buffers if scaling mode is freeze.
369 return false;
370 }
371
Vishnu Naire1a42322020-10-02 17:42:04 -0700372 uint32_t bufWidth = item.mGraphicBuffer->getWidth();
373 uint32_t bufHeight = item.mGraphicBuffer->getHeight();
374
375 // Take the buffer's orientation into account
376 if (item.mTransform & ui::Transform::ROT_90) {
377 std::swap(bufWidth, bufHeight);
378 }
Vishnu Nairea0de002020-11-17 17:42:37 -0800379 ui::Size bufferSize(bufWidth, bufHeight);
380 if (mRequestedSize != mSize && mRequestedSize == bufferSize) {
381 mSize = mRequestedSize;
382 return false;
383 }
Vishnu Naire1a42322020-10-02 17:42:04 -0700384
Vishnu Nair670b3f72020-09-29 17:52:18 -0700385 // reject buffers if the buffer size doesn't match.
Vishnu Nairea0de002020-11-17 17:42:37 -0800386 return mSize != bufferSize;
Vishnu Nair670b3f72020-09-29 17:52:18 -0700387}
Vishnu Nairbf255772020-10-16 10:54:41 -0700388
389// Check if we have acquired the maximum number of buffers.
390// As a special case, we wait for the first callback before acquiring the second buffer so we
391// can ensure the first buffer is presented if multiple buffers are queued in succession.
392bool BLASTBufferQueue::maxBuffersAcquired() const {
393 return mNumAcquired == MAX_ACQUIRED_BUFFERS + 1 ||
394 (!mInitialCallbackReceived && mNumAcquired == 1);
395}
396
Robert Carr05086b22020-10-13 18:22:51 -0700397class BBQSurface : public Surface {
Robert Carr9c006e02020-10-14 13:41:57 -0700398private:
399 sp<BLASTBufferQueue> mBbq;
Robert Carr05086b22020-10-13 18:22:51 -0700400public:
Vishnu Nair992496b2020-10-22 17:27:21 -0700401 BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp,
402 const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq)
403 : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {}
Robert Carr9c006e02020-10-14 13:41:57 -0700404
Robert Carr05086b22020-10-13 18:22:51 -0700405 void allocateBuffers() override {
406 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
407 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
408 auto gbp = getIGraphicBufferProducer();
409 std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(),
410 reqFormat=mReqFormat, reqUsage=mReqUsage] () {
411 gbp->allocateBuffers(reqWidth, reqHeight,
412 reqFormat, reqUsage);
413
414 }).detach();
415 }
Robert Carr9c006e02020-10-14 13:41:57 -0700416
Marin Shalamanov46084422020-10-13 12:33:42 +0200417 status_t setFrameRate(float frameRate, int8_t compatibility, bool shouldBeSeamless) override {
Robert Carr9c006e02020-10-14 13:41:57 -0700418 if (!ValidateFrameRate(frameRate, compatibility, "BBQSurface::setFrameRate")) {
419 return BAD_VALUE;
420 }
Marin Shalamanov46084422020-10-13 12:33:42 +0200421 return mBbq->setFrameRate(frameRate, compatibility, shouldBeSeamless);
Robert Carr9c006e02020-10-14 13:41:57 -0700422 }
Robert Carr9b611b72020-10-19 12:00:23 -0700423
424 status_t setFrameTimelineVsync(int64_t frameTimelineVsyncId) override {
425 return mBbq->setFrameTimelineVsync(frameTimelineVsyncId);
426 }
Robert Carr05086b22020-10-13 18:22:51 -0700427};
428
Robert Carr9c006e02020-10-14 13:41:57 -0700429// TODO: Can we coalesce this with frame updates? Need to confirm
430// no timing issues.
Marin Shalamanov46084422020-10-13 12:33:42 +0200431status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility,
432 bool shouldBeSeamless) {
Robert Carr9c006e02020-10-14 13:41:57 -0700433 std::unique_lock _lock{mMutex};
434 SurfaceComposerClient::Transaction t;
435
Marin Shalamanov46084422020-10-13 12:33:42 +0200436 return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply();
Robert Carr9c006e02020-10-14 13:41:57 -0700437}
438
Robert Carr9b611b72020-10-19 12:00:23 -0700439status_t BLASTBufferQueue::setFrameTimelineVsync(int64_t frameTimelineVsyncId) {
440 std::unique_lock _lock{mMutex};
Jorim Jaggia3fe67b2020-12-01 00:24:33 +0100441 mNextFrameTimelineVsyncIdQueue.push(frameTimelineVsyncId);
442 return OK;
Robert Carr9b611b72020-10-19 12:00:23 -0700443}
444
Vishnu Nair992496b2020-10-22 17:27:21 -0700445sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) {
446 std::unique_lock _lock{mMutex};
447 sp<IBinder> scHandle = nullptr;
448 if (includeSurfaceControlHandle && mSurfaceControl) {
449 scHandle = mSurfaceControl->getHandle();
450 }
451 return new BBQSurface(mProducer, true, scHandle, this);
Robert Carr05086b22020-10-13 18:22:51 -0700452}
453
Robert Carr78c25dd2019-08-15 14:10:33 -0700454} // namespace android