blob: 30e1351b334844919f9a9324c34b2d4e1c5548ad [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
21
Valerie Hau65b8e872020-02-13 09:45:14 -080022#include <cutils/properties.h>
23
Robert Carr78c25dd2019-08-15 14:10:33 -070024#include <gui/BLASTBufferQueue.h>
25#include <gui/BufferItemConsumer.h>
Valerie Hau45e4b3b2019-12-03 10:49:17 -080026#include <gui/GLConsumer.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
34namespace android {
35
Valerie Hau871d6352020-01-29 08:44:02 -080036void BLASTBufferItemConsumer::onDisconnect() {
37 Mutex::Autolock lock(mFrameEventHistoryMutex);
38 mPreviouslyConnected = mCurrentlyConnected;
39 mCurrentlyConnected = false;
40 if (mPreviouslyConnected) {
41 mDisconnectEvents.push(mCurrentFrameNumber);
42 }
43 mFrameEventHistory.onDisconnect();
44}
45
46void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
47 FrameEventHistoryDelta* outDelta) {
48 Mutex::Autolock lock(mFrameEventHistoryMutex);
49 if (newTimestamps) {
50 // BufferQueueProducer only adds a new timestamp on
51 // queueBuffer
52 mCurrentFrameNumber = newTimestamps->frameNumber;
53 mFrameEventHistory.addQueue(*newTimestamps);
54 }
55 if (outDelta) {
56 // frame event histories will be processed
57 // only after the producer connects and requests
58 // deltas for the first time. Forward this intent
59 // to SF-side to turn event processing back on
60 mPreviouslyConnected = mCurrentlyConnected;
61 mCurrentlyConnected = true;
62 mFrameEventHistory.getAndResetDelta(outDelta);
63 }
64}
65
66void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
67 const sp<Fence>& glDoneFence,
68 const sp<Fence>& presentFence,
69 const sp<Fence>& prevReleaseFence,
70 CompositorTiming compositorTiming,
71 nsecs_t latchTime, nsecs_t dequeueReadyTime) {
72 Mutex::Autolock lock(mFrameEventHistoryMutex);
73
74 // if the producer is not connected, don't bother updating,
75 // the next producer that connects won't access this frame event
76 if (!mCurrentlyConnected) return;
77 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
78 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
79 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
80
81 mFrameEventHistory.addLatch(frameNumber, latchTime);
82 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
83 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
84 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
85 compositorTiming);
86}
87
88void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
89 bool disconnect = false;
90 Mutex::Autolock lock(mFrameEventHistoryMutex);
91 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
92 disconnect = true;
93 mDisconnectEvents.pop();
94 }
95 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
96}
97
Robert Carr78c25dd2019-08-15 14:10:33 -070098BLASTBufferQueue::BLASTBufferQueue(const sp<SurfaceControl>& surface, int width, int height)
Valerie Haud3b90d22019-11-06 09:37:31 -080099 : mSurfaceControl(surface),
Valerie Haud3b90d22019-11-06 09:37:31 -0800100 mWidth(width),
101 mHeight(height),
102 mNextTransaction(nullptr) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700103 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
Valerie Hau0889c622020-02-19 15:04:47 -0800104 // since the adapter is in the client process, set dequeue timeout
105 // explicitly so that dequeueBuffer will block
106 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
Valerie Hau65b8e872020-02-13 09:45:14 -0800107
108 int8_t disableTripleBuffer = property_get_bool("ro.sf.disable_triple_buffer", 0);
109 if (!disableTripleBuffer) {
110 mProducer->setMaxDequeuedBufferCount(2);
111 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700112 mBufferItemConsumer =
Valerie Hau871d6352020-01-29 08:44:02 -0800113 new BLASTBufferItemConsumer(mConsumer, AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER, 1, true);
Valerie Haua32c5522019-12-09 10:11:08 -0800114 static int32_t id = 0;
115 auto name = std::string("BLAST Consumer") + std::to_string(id);
116 id++;
117 mBufferItemConsumer->setName(String8(name.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700118 mBufferItemConsumer->setFrameAvailableListener(this);
119 mBufferItemConsumer->setBufferFreedListener(this);
120 mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight);
121 mBufferItemConsumer->setDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888);
Valerie Hau2882e982020-01-23 13:33:10 -0800122 mTransformHint = mSurfaceControl->getTransformHint();
Valerie Haud3b90d22019-11-06 09:37:31 -0800123
Valerie Haua32c5522019-12-09 10:11:08 -0800124 mNumAcquired = 0;
125 mNumFrameAvailable = 0;
126 mPendingReleaseItem.item = BufferItem();
127 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700128}
129
130void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, int width, int height) {
131 std::unique_lock _lock{mMutex};
132 mSurfaceControl = surface;
133 mWidth = width;
134 mHeight = height;
135 mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight);
136}
137
138static void transactionCallbackThunk(void* context, nsecs_t latchTime,
139 const sp<Fence>& presentFence,
140 const std::vector<SurfaceControlStats>& stats) {
141 if (context == nullptr) {
142 return;
143 }
144 BLASTBufferQueue* bq = static_cast<BLASTBufferQueue*>(context);
145 bq->transactionCallback(latchTime, presentFence, stats);
146}
147
148void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
149 const std::vector<SurfaceControlStats>& stats) {
150 std::unique_lock _lock{mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -0800151 ATRACE_CALL();
Robert Carr78c25dd2019-08-15 14:10:33 -0700152
Valerie Hau871d6352020-01-29 08:44:02 -0800153 if (!stats.empty()) {
154 mTransformHint = stats[0].transformHint;
155 mBufferItemConsumer->setTransformHint(mTransformHint);
156 mBufferItemConsumer->updateFrameTimestamps(stats[0].frameEventStats.frameNumber,
157 stats[0].frameEventStats.refreshStartTime,
158 stats[0].frameEventStats.gpuCompositionDoneFence,
159 stats[0].presentFence,
160 stats[0].previousReleaseFence,
161 stats[0].frameEventStats.compositorTiming,
162 stats[0].latchTime,
163 stats[0].frameEventStats.dequeueReadyTime);
164 }
Valerie Haua32c5522019-12-09 10:11:08 -0800165 if (mPendingReleaseItem.item.mGraphicBuffer != nullptr) {
Valerie Hau871d6352020-01-29 08:44:02 -0800166 if (!stats.empty()) {
Valerie Haua32c5522019-12-09 10:11:08 -0800167 mPendingReleaseItem.releaseFence = stats[0].previousReleaseFence;
Valerie Haua32c5522019-12-09 10:11:08 -0800168 } else {
169 ALOGE("Warning: no SurfaceControlStats returned in BLASTBufferQueue callback");
170 mPendingReleaseItem.releaseFence = nullptr;
171 }
172 mBufferItemConsumer->releaseBuffer(mPendingReleaseItem.item,
173 mPendingReleaseItem.releaseFence
174 ? mPendingReleaseItem.releaseFence
Robert Carr78c25dd2019-08-15 14:10:33 -0700175 : Fence::NO_FENCE);
Valerie Haua32c5522019-12-09 10:11:08 -0800176 mNumAcquired--;
177 mPendingReleaseItem.item = BufferItem();
178 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700179 }
Valerie Haua32c5522019-12-09 10:11:08 -0800180
181 if (mSubmitted.empty()) {
182 ALOGE("ERROR: callback with no corresponding submitted buffer item");
183 }
184 mPendingReleaseItem.item = std::move(mSubmitted.front());
185 mSubmitted.pop();
Robert Carre4943eb2020-02-15 18:34:59 -0800186
187 processNextBufferLocked();
188
Valerie Haud3b90d22019-11-06 09:37:31 -0800189 mCallbackCV.notify_all();
Robert Carr78c25dd2019-08-15 14:10:33 -0700190 decStrong((void*)transactionCallbackThunk);
191}
192
Valerie Haud3b90d22019-11-06 09:37:31 -0800193void BLASTBufferQueue::processNextBufferLocked() {
Valerie Haua32c5522019-12-09 10:11:08 -0800194 ATRACE_CALL();
Valerie Hau65b8e872020-02-13 09:45:14 -0800195 if (mNumFrameAvailable == 0 || mNumAcquired == MAX_ACQUIRED_BUFFERS + 1) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800196 return;
197 }
198
Valerie Haua32c5522019-12-09 10:11:08 -0800199 if (mSurfaceControl == nullptr) {
200 ALOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800201 return;
202 }
203
Robert Carr78c25dd2019-08-15 14:10:33 -0700204 SurfaceComposerClient::Transaction localTransaction;
205 bool applyTransaction = true;
206 SurfaceComposerClient::Transaction* t = &localTransaction;
Robert Carre4943eb2020-02-15 18:34:59 -0800207 if (mNextTransaction != nullptr && mUseNextTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700208 t = mNextTransaction;
209 mNextTransaction = nullptr;
210 applyTransaction = false;
211 }
212
Valerie Haua32c5522019-12-09 10:11:08 -0800213 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800214
Valerie Haua32c5522019-12-09 10:11:08 -0800215 status_t status = mBufferItemConsumer->acquireBuffer(&bufferItem, -1, false);
Robert Carr78c25dd2019-08-15 14:10:33 -0700216 if (status != OK) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700217 return;
218 }
Valerie Haua32c5522019-12-09 10:11:08 -0800219 auto buffer = bufferItem.mGraphicBuffer;
220 mNumFrameAvailable--;
221
222 if (buffer == nullptr) {
223 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
224 return;
225 }
226
227 mNumAcquired++;
228 mSubmitted.push(bufferItem);
Robert Carr78c25dd2019-08-15 14:10:33 -0700229
Valerie Hau871d6352020-01-29 08:44:02 -0800230 bool needsDisconnect = false;
231 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
232
233 // if producer disconnected before, notify SurfaceFlinger
234 if (needsDisconnect) {
235 t->notifyProducerDisconnect(mSurfaceControl);
236 }
237
Robert Carr78c25dd2019-08-15 14:10:33 -0700238 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
239 incStrong((void*)transactionCallbackThunk);
240
241 t->setBuffer(mSurfaceControl, buffer);
242 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800243 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700244 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
245
Valerie Hau81ec6072020-01-30 09:45:24 -0800246 t->setFrame(mSurfaceControl, {0, 0, mWidth, mHeight});
Valerie Haua32c5522019-12-09 10:11:08 -0800247 t->setCrop(mSurfaceControl, computeCrop(bufferItem));
248 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800249 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Valerie Hau181abd32020-01-27 14:18:28 -0800250 t->setDesiredPresentTime(bufferItem.mTimestamp);
Robert Carr78c25dd2019-08-15 14:10:33 -0700251
252 if (applyTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700253 t->apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700254 }
255}
256
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800257Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
258 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
259 return GLConsumer::scaleDownCrop(item.mCrop, mWidth, mHeight);
260 }
261 return item.mCrop;
262}
263
Valerie Haua32c5522019-12-09 10:11:08 -0800264void BLASTBufferQueue::onFrameAvailable(const BufferItem& /*item*/) {
265 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800266 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800267
Valerie Hau0188adf2020-02-13 08:29:20 -0800268 if (mNextTransaction != nullptr) {
Robert Carre4943eb2020-02-15 18:34:59 -0800269 while (mNumFrameAvailable > 0 || mNumAcquired == MAX_ACQUIRED_BUFFERS + 1) {
Valerie Hau0188adf2020-02-13 08:29:20 -0800270 mCallbackCV.wait(_lock);
271 }
Robert Carre4943eb2020-02-15 18:34:59 -0800272 mUseNextTransaction = true;
Valerie Hau0188adf2020-02-13 08:29:20 -0800273 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800274 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800275 mNumFrameAvailable++;
Valerie Haud3b90d22019-11-06 09:37:31 -0800276 processNextBufferLocked();
Valerie Haud3b90d22019-11-06 09:37:31 -0800277}
278
Robert Carr78c25dd2019-08-15 14:10:33 -0700279void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800280 std::lock_guard _lock{mMutex};
Robert Carre4943eb2020-02-15 18:34:59 -0800281 mUseNextTransaction = false;
Robert Carr78c25dd2019-08-15 14:10:33 -0700282 mNextTransaction = t;
283}
284
285} // namespace android