blob: 56591bdc638c3b161db23d9a8f62fc96b77112d3 [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
Robert Carr78c25dd2019-08-15 14:10:33 -070022#include <gui/BLASTBufferQueue.h>
23#include <gui/BufferItemConsumer.h>
Valerie Hau45e4b3b2019-12-03 10:49:17 -080024#include <gui/GLConsumer.h>
Robert Carr78c25dd2019-08-15 14:10:33 -070025
Valerie Haua32c5522019-12-09 10:11:08 -080026#include <utils/Trace.h>
27
Robert Carr78c25dd2019-08-15 14:10:33 -070028#include <chrono>
29
30using namespace std::chrono_literals;
31
32namespace android {
33
Valerie Hau871d6352020-01-29 08:44:02 -080034void BLASTBufferItemConsumer::onDisconnect() {
35 Mutex::Autolock lock(mFrameEventHistoryMutex);
36 mPreviouslyConnected = mCurrentlyConnected;
37 mCurrentlyConnected = false;
38 if (mPreviouslyConnected) {
39 mDisconnectEvents.push(mCurrentFrameNumber);
40 }
41 mFrameEventHistory.onDisconnect();
42}
43
44void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
45 FrameEventHistoryDelta* outDelta) {
46 Mutex::Autolock lock(mFrameEventHistoryMutex);
47 if (newTimestamps) {
48 // BufferQueueProducer only adds a new timestamp on
49 // queueBuffer
50 mCurrentFrameNumber = newTimestamps->frameNumber;
51 mFrameEventHistory.addQueue(*newTimestamps);
52 }
53 if (outDelta) {
54 // frame event histories will be processed
55 // only after the producer connects and requests
56 // deltas for the first time. Forward this intent
57 // to SF-side to turn event processing back on
58 mPreviouslyConnected = mCurrentlyConnected;
59 mCurrentlyConnected = true;
60 mFrameEventHistory.getAndResetDelta(outDelta);
61 }
62}
63
64void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime,
65 const sp<Fence>& glDoneFence,
66 const sp<Fence>& presentFence,
67 const sp<Fence>& prevReleaseFence,
68 CompositorTiming compositorTiming,
69 nsecs_t latchTime, nsecs_t dequeueReadyTime) {
70 Mutex::Autolock lock(mFrameEventHistoryMutex);
71
72 // if the producer is not connected, don't bother updating,
73 // the next producer that connects won't access this frame event
74 if (!mCurrentlyConnected) return;
75 std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence);
76 std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence);
77 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence);
78
79 mFrameEventHistory.addLatch(frameNumber, latchTime);
80 mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime));
81 mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime);
82 mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime,
83 compositorTiming);
84}
85
86void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) {
87 bool disconnect = false;
88 Mutex::Autolock lock(mFrameEventHistoryMutex);
89 while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) {
90 disconnect = true;
91 mDisconnectEvents.pop();
92 }
93 if (needsDisconnect != nullptr) *needsDisconnect = disconnect;
94}
95
Robert Carrcedef052020-04-22 15:58:23 -070096BLASTBufferQueue::BLASTBufferQueue(const sp<SurfaceControl>& surface, int width, int height,
97 bool enableTripleBuffering)
Valerie Haud3b90d22019-11-06 09:37:31 -080098 : mSurfaceControl(surface),
Valerie Haud3b90d22019-11-06 09:37:31 -080099 mWidth(width),
100 mHeight(height),
101 mNextTransaction(nullptr) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700102 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
Valerie Hau0889c622020-02-19 15:04:47 -0800103 // since the adapter is in the client process, set dequeue timeout
104 // explicitly so that dequeueBuffer will block
105 mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max());
Valerie Hau65b8e872020-02-13 09:45:14 -0800106
Robert Carrcedef052020-04-22 15:58:23 -0700107 if (enableTripleBuffering) {
Valerie Hau65b8e872020-02-13 09:45:14 -0800108 mProducer->setMaxDequeuedBufferCount(2);
109 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700110 mBufferItemConsumer =
Robert Carr5b2ae912020-04-01 15:40:40 -0700111 new BLASTBufferItemConsumer(mConsumer, GraphicBuffer::USAGE_HW_COMPOSER, 1, true);
Valerie Haua32c5522019-12-09 10:11:08 -0800112 static int32_t id = 0;
113 auto name = std::string("BLAST Consumer") + std::to_string(id);
114 id++;
115 mBufferItemConsumer->setName(String8(name.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700116 mBufferItemConsumer->setFrameAvailableListener(this);
117 mBufferItemConsumer->setBufferFreedListener(this);
118 mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight);
119 mBufferItemConsumer->setDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888);
Robert Carr9f133d72020-04-01 15:51:46 -0700120
Valerie Hau2882e982020-01-23 13:33:10 -0800121 mTransformHint = mSurfaceControl->getTransformHint();
Robert Carr9f133d72020-04-01 15:51:46 -0700122 mBufferItemConsumer->setTransformHint(mTransformHint);
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;
Robert Carrfc416512020-04-02 12:32:44 -0700133
134 if (mWidth != width || mHeight != height) {
135 mWidth = width;
136 mHeight = height;
137 mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight);
138 }
Robert Carr78c25dd2019-08-15 14:10:33 -0700139}
140
141static void transactionCallbackThunk(void* context, nsecs_t latchTime,
142 const sp<Fence>& presentFence,
143 const std::vector<SurfaceControlStats>& stats) {
144 if (context == nullptr) {
145 return;
146 }
147 BLASTBufferQueue* bq = static_cast<BLASTBufferQueue*>(context);
148 bq->transactionCallback(latchTime, presentFence, stats);
149}
150
151void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
152 const std::vector<SurfaceControlStats>& stats) {
153 std::unique_lock _lock{mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -0800154 ATRACE_CALL();
Robert Carr78c25dd2019-08-15 14:10:33 -0700155
Valerie Hau871d6352020-01-29 08:44:02 -0800156 if (!stats.empty()) {
157 mTransformHint = stats[0].transformHint;
158 mBufferItemConsumer->setTransformHint(mTransformHint);
159 mBufferItemConsumer->updateFrameTimestamps(stats[0].frameEventStats.frameNumber,
160 stats[0].frameEventStats.refreshStartTime,
161 stats[0].frameEventStats.gpuCompositionDoneFence,
162 stats[0].presentFence,
163 stats[0].previousReleaseFence,
164 stats[0].frameEventStats.compositorTiming,
165 stats[0].latchTime,
166 stats[0].frameEventStats.dequeueReadyTime);
167 }
Valerie Haua32c5522019-12-09 10:11:08 -0800168 if (mPendingReleaseItem.item.mGraphicBuffer != nullptr) {
Valerie Hau871d6352020-01-29 08:44:02 -0800169 if (!stats.empty()) {
Valerie Haua32c5522019-12-09 10:11:08 -0800170 mPendingReleaseItem.releaseFence = stats[0].previousReleaseFence;
Valerie Haua32c5522019-12-09 10:11:08 -0800171 } else {
172 ALOGE("Warning: no SurfaceControlStats returned in BLASTBufferQueue callback");
173 mPendingReleaseItem.releaseFence = nullptr;
174 }
175 mBufferItemConsumer->releaseBuffer(mPendingReleaseItem.item,
176 mPendingReleaseItem.releaseFence
177 ? mPendingReleaseItem.releaseFence
Robert Carr78c25dd2019-08-15 14:10:33 -0700178 : Fence::NO_FENCE);
Valerie Haua32c5522019-12-09 10:11:08 -0800179 mNumAcquired--;
180 mPendingReleaseItem.item = BufferItem();
181 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700182 }
Valerie Haua32c5522019-12-09 10:11:08 -0800183
184 if (mSubmitted.empty()) {
185 ALOGE("ERROR: callback with no corresponding submitted buffer item");
186 }
187 mPendingReleaseItem.item = std::move(mSubmitted.front());
188 mSubmitted.pop();
Robert Carre4943eb2020-02-15 18:34:59 -0800189
Robert Carr255acdc2020-04-17 14:08:55 -0700190 processNextBufferLocked(false);
Robert Carre4943eb2020-02-15 18:34:59 -0800191
Valerie Haud3b90d22019-11-06 09:37:31 -0800192 mCallbackCV.notify_all();
Robert Carr78c25dd2019-08-15 14:10:33 -0700193 decStrong((void*)transactionCallbackThunk);
194}
195
Robert Carr255acdc2020-04-17 14:08:55 -0700196void BLASTBufferQueue::processNextBufferLocked(bool useNextTransaction) {
Valerie Haua32c5522019-12-09 10:11:08 -0800197 ATRACE_CALL();
Valerie Hau65b8e872020-02-13 09:45:14 -0800198 if (mNumFrameAvailable == 0 || mNumAcquired == MAX_ACQUIRED_BUFFERS + 1) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800199 return;
200 }
201
Valerie Haua32c5522019-12-09 10:11:08 -0800202 if (mSurfaceControl == nullptr) {
203 ALOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800204 return;
205 }
206
Robert Carr78c25dd2019-08-15 14:10:33 -0700207 SurfaceComposerClient::Transaction localTransaction;
208 bool applyTransaction = true;
209 SurfaceComposerClient::Transaction* t = &localTransaction;
Robert Carr255acdc2020-04-17 14:08:55 -0700210 if (mNextTransaction != nullptr && useNextTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700211 t = mNextTransaction;
212 mNextTransaction = nullptr;
213 applyTransaction = false;
214 }
215
Valerie Haua32c5522019-12-09 10:11:08 -0800216 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800217
Valerie Haua32c5522019-12-09 10:11:08 -0800218 status_t status = mBufferItemConsumer->acquireBuffer(&bufferItem, -1, false);
Robert Carr78c25dd2019-08-15 14:10:33 -0700219 if (status != OK) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700220 return;
221 }
Valerie Haua32c5522019-12-09 10:11:08 -0800222 auto buffer = bufferItem.mGraphicBuffer;
223 mNumFrameAvailable--;
224
225 if (buffer == nullptr) {
226 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
227 return;
228 }
229
230 mNumAcquired++;
231 mSubmitted.push(bufferItem);
Robert Carr78c25dd2019-08-15 14:10:33 -0700232
Valerie Hau871d6352020-01-29 08:44:02 -0800233 bool needsDisconnect = false;
234 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
235
236 // if producer disconnected before, notify SurfaceFlinger
237 if (needsDisconnect) {
238 t->notifyProducerDisconnect(mSurfaceControl);
239 }
240
Robert Carr78c25dd2019-08-15 14:10:33 -0700241 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
242 incStrong((void*)transactionCallbackThunk);
243
244 t->setBuffer(mSurfaceControl, buffer);
245 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800246 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700247 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
248
Valerie Hau81ec6072020-01-30 09:45:24 -0800249 t->setFrame(mSurfaceControl, {0, 0, mWidth, mHeight});
Valerie Haua32c5522019-12-09 10:11:08 -0800250 t->setCrop(mSurfaceControl, computeCrop(bufferItem));
251 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800252 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Valerie Hau181abd32020-01-27 14:18:28 -0800253 t->setDesiredPresentTime(bufferItem.mTimestamp);
Robert Carr78c25dd2019-08-15 14:10:33 -0700254
255 if (applyTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700256 t->apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700257 }
258}
259
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800260Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
261 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
262 return GLConsumer::scaleDownCrop(item.mCrop, mWidth, mHeight);
263 }
264 return item.mCrop;
265}
266
Valerie Haua32c5522019-12-09 10:11:08 -0800267void BLASTBufferQueue::onFrameAvailable(const BufferItem& /*item*/) {
268 ATRACE_CALL();
Valerie Hau0188adf2020-02-13 08:29:20 -0800269 std::unique_lock _lock{mMutex};
Valerie Haud3b90d22019-11-06 09:37:31 -0800270
Valerie Hau0188adf2020-02-13 08:29:20 -0800271 if (mNextTransaction != nullptr) {
Robert Carre4943eb2020-02-15 18:34:59 -0800272 while (mNumFrameAvailable > 0 || mNumAcquired == MAX_ACQUIRED_BUFFERS + 1) {
Valerie Hau0188adf2020-02-13 08:29:20 -0800273 mCallbackCV.wait(_lock);
274 }
275 }
Valerie Haud3b90d22019-11-06 09:37:31 -0800276 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800277 mNumFrameAvailable++;
Robert Carr255acdc2020-04-17 14:08:55 -0700278 processNextBufferLocked(true);
Valerie Haud3b90d22019-11-06 09:37:31 -0800279}
280
Robert Carr78c25dd2019-08-15 14:10:33 -0700281void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800282 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700283 mNextTransaction = t;
284}
285
286} // namespace android