blob: a8384accce446f1f094df0c98bdba9150e344cfc [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 =
Robert Carr5b2ae912020-04-01 15:40:40 -0700113 new BLASTBufferItemConsumer(mConsumer, GraphicBuffer::USAGE_HW_COMPOSER, 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);
Robert Carr9f133d72020-04-01 15:51:46 -0700122
Valerie Hau2882e982020-01-23 13:33:10 -0800123 mTransformHint = mSurfaceControl->getTransformHint();
Robert Carr9f133d72020-04-01 15:51:46 -0700124 mBufferItemConsumer->setTransformHint(mTransformHint);
Valerie Haud3b90d22019-11-06 09:37:31 -0800125
Valerie Haua32c5522019-12-09 10:11:08 -0800126 mNumAcquired = 0;
127 mNumFrameAvailable = 0;
128 mPendingReleaseItem.item = BufferItem();
129 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700130}
131
132void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, int width, int height) {
133 std::unique_lock _lock{mMutex};
134 mSurfaceControl = surface;
Robert Carrfc416512020-04-02 12:32:44 -0700135
136 if (mWidth != width || mHeight != height) {
137 mWidth = width;
138 mHeight = height;
139 mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight);
140 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700141}
142
143static void transactionCallbackThunk(void* context, nsecs_t latchTime,
144 const sp<Fence>& presentFence,
145 const std::vector<SurfaceControlStats>& stats) {
146 if (context == nullptr) {
147 return;
148 }
149 BLASTBufferQueue* bq = static_cast<BLASTBufferQueue*>(context);
150 bq->transactionCallback(latchTime, presentFence, stats);
151}
152
153void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
154 const std::vector<SurfaceControlStats>& stats) {
155 std::unique_lock _lock{mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -0800156 ATRACE_CALL();
Robert Carr78c25dd2019-08-15 14:10:33 -0700157
Valerie Hau871d6352020-01-29 08:44:02 -0800158 if (!stats.empty()) {
159 mTransformHint = stats[0].transformHint;
160 mBufferItemConsumer->setTransformHint(mTransformHint);
161 mBufferItemConsumer->updateFrameTimestamps(stats[0].frameEventStats.frameNumber,
162 stats[0].frameEventStats.refreshStartTime,
163 stats[0].frameEventStats.gpuCompositionDoneFence,
164 stats[0].presentFence,
165 stats[0].previousReleaseFence,
166 stats[0].frameEventStats.compositorTiming,
167 stats[0].latchTime,
168 stats[0].frameEventStats.dequeueReadyTime);
169 }
Valerie Haua32c5522019-12-09 10:11:08 -0800170 if (mPendingReleaseItem.item.mGraphicBuffer != nullptr) {
Valerie Hau871d6352020-01-29 08:44:02 -0800171 if (!stats.empty()) {
Valerie Haua32c5522019-12-09 10:11:08 -0800172 mPendingReleaseItem.releaseFence = stats[0].previousReleaseFence;
Valerie Haua32c5522019-12-09 10:11:08 -0800173 } else {
174 ALOGE("Warning: no SurfaceControlStats returned in BLASTBufferQueue callback");
175 mPendingReleaseItem.releaseFence = nullptr;
176 }
177 mBufferItemConsumer->releaseBuffer(mPendingReleaseItem.item,
178 mPendingReleaseItem.releaseFence
179 ? mPendingReleaseItem.releaseFence
Robert Carr78c25dd2019-08-15 14:10:33 -0700180 : Fence::NO_FENCE);
Valerie Haua32c5522019-12-09 10:11:08 -0800181 mNumAcquired--;
182 mPendingReleaseItem.item = BufferItem();
183 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700184 }
Valerie Haua32c5522019-12-09 10:11:08 -0800185
186 if (mSubmitted.empty()) {
187 ALOGE("ERROR: callback with no corresponding submitted buffer item");
188 }
189 mPendingReleaseItem.item = std::move(mSubmitted.front());
190 mSubmitted.pop();
Robert Carre4943eb2020-02-15 18:34:59 -0800191
192 processNextBufferLocked();
193
Valerie Haud3b90d22019-11-06 09:37:31 -0800194 mCallbackCV.notify_all();
Robert Carr78c25dd2019-08-15 14:10:33 -0700195 decStrong((void*)transactionCallbackThunk);
196}
197
Valerie Haud3b90d22019-11-06 09:37:31 -0800198void BLASTBufferQueue::processNextBufferLocked() {
Valerie Haua32c5522019-12-09 10:11:08 -0800199 ATRACE_CALL();
Valerie Hau65b8e872020-02-13 09:45:14 -0800200 if (mNumFrameAvailable == 0 || mNumAcquired == MAX_ACQUIRED_BUFFERS + 1) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800201 return;
202 }
203
Valerie Haua32c5522019-12-09 10:11:08 -0800204 if (mSurfaceControl == nullptr) {
205 ALOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800206 return;
207 }
208
Robert Carr78c25dd2019-08-15 14:10:33 -0700209 SurfaceComposerClient::Transaction localTransaction;
210 bool applyTransaction = true;
211 SurfaceComposerClient::Transaction* t = &localTransaction;
Robert Carre4943eb2020-02-15 18:34:59 -0800212 if (mNextTransaction != nullptr && mUseNextTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700213 t = mNextTransaction;
214 mNextTransaction = nullptr;
215 applyTransaction = false;
216 }
217
Valerie Haua32c5522019-12-09 10:11:08 -0800218 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800219
Valerie Haua32c5522019-12-09 10:11:08 -0800220 status_t status = mBufferItemConsumer->acquireBuffer(&bufferItem, -1, false);
Robert Carr78c25dd2019-08-15 14:10:33 -0700221 if (status != OK) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700222 return;
223 }
Valerie Haua32c5522019-12-09 10:11:08 -0800224 auto buffer = bufferItem.mGraphicBuffer;
225 mNumFrameAvailable--;
226
227 if (buffer == nullptr) {
228 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
229 return;
230 }
231
232 mNumAcquired++;
233 mSubmitted.push(bufferItem);
Robert Carr78c25dd2019-08-15 14:10:33 -0700234
Valerie Hau871d6352020-01-29 08:44:02 -0800235 bool needsDisconnect = false;
236 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
237
238 // if producer disconnected before, notify SurfaceFlinger
239 if (needsDisconnect) {
240 t->notifyProducerDisconnect(mSurfaceControl);
241 }
242
Robert Carr78c25dd2019-08-15 14:10:33 -0700243 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
244 incStrong((void*)transactionCallbackThunk);
245
246 t->setBuffer(mSurfaceControl, buffer);
247 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800248 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700249 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
250
Valerie Hau81ec6072020-01-30 09:45:24 -0800251 t->setFrame(mSurfaceControl, {0, 0, mWidth, mHeight});
Valerie Haua32c5522019-12-09 10:11:08 -0800252 t->setCrop(mSurfaceControl, computeCrop(bufferItem));
253 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800254 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Valerie Hau181abd32020-01-27 14:18:28 -0800255 t->setDesiredPresentTime(bufferItem.mTimestamp);
Robert Carr78c25dd2019-08-15 14:10:33 -0700256
257 if (applyTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700258 t->apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700259 }
260}
261
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800262Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
263 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
264 return GLConsumer::scaleDownCrop(item.mCrop, mWidth, mHeight);
265 }
266 return item.mCrop;
267}
268
Valerie Haua32c5522019-12-09 10:11:08 -0800269void BLASTBufferQueue::onFrameAvailable(const BufferItem& /*item*/) {
270 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800271 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800272
Valerie Hau0188adf2020-02-13 08:29:20 -0800273 if (mNextTransaction != nullptr) {
Robert Carre4943eb2020-02-15 18:34:59 -0800274 while (mNumFrameAvailable > 0 || mNumAcquired == MAX_ACQUIRED_BUFFERS + 1) {
Valerie Hau0188adf2020-02-13 08:29:20 -0800275 mCallbackCV.wait(_lock);
276 }
Robert Carre4943eb2020-02-15 18:34:59 -0800277 mUseNextTransaction = true;
Valerie Hau0188adf2020-02-13 08:29:20 -0800278 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800279 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800280 mNumFrameAvailable++;
Valerie Haud3b90d22019-11-06 09:37:31 -0800281 processNextBufferLocked();
Valerie Haud3b90d22019-11-06 09:37:31 -0800282}
283
Robert Carr78c25dd2019-08-15 14:10:33 -0700284void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800285 std::lock_guard _lock{mMutex};
Robert Carre4943eb2020-02-15 18:34:59 -0800286 mUseNextTransaction = false;
Robert Carr78c25dd2019-08-15 14:10:33 -0700287 mNextTransaction = t;
288}
289
290} // namespace android