blob: e2f5d31d00c9101c966d9ffdf5fd23e8efe1ac7d [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 Carr78c25dd2019-08-15 14:10:33 -070096BLASTBufferQueue::BLASTBufferQueue(const sp<SurfaceControl>& surface, int width, int height)
Valerie Haud3b90d22019-11-06 09:37:31 -080097 : mSurfaceControl(surface),
Valerie Haud3b90d22019-11-06 09:37:31 -080098 mWidth(width),
99 mHeight(height),
100 mNextTransaction(nullptr) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700101 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
Valerie Haua32c5522019-12-09 10:11:08 -0800102 mConsumer->setMaxAcquiredBufferCount(MAX_ACQUIRED_BUFFERS);
Robert Carr78c25dd2019-08-15 14:10:33 -0700103 mBufferItemConsumer =
Valerie Hau871d6352020-01-29 08:44:02 -0800104 new BLASTBufferItemConsumer(mConsumer, AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER, 1, true);
Valerie Haua32c5522019-12-09 10:11:08 -0800105 static int32_t id = 0;
106 auto name = std::string("BLAST Consumer") + std::to_string(id);
107 id++;
108 mBufferItemConsumer->setName(String8(name.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -0700109 mBufferItemConsumer->setFrameAvailableListener(this);
110 mBufferItemConsumer->setBufferFreedListener(this);
111 mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight);
112 mBufferItemConsumer->setDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888);
Valerie Hau2882e982020-01-23 13:33:10 -0800113 mTransformHint = mSurfaceControl->getTransformHint();
Valerie Haud3b90d22019-11-06 09:37:31 -0800114
Valerie Haua32c5522019-12-09 10:11:08 -0800115 mNumAcquired = 0;
116 mNumFrameAvailable = 0;
117 mPendingReleaseItem.item = BufferItem();
118 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700119}
120
121void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, int width, int height) {
122 std::unique_lock _lock{mMutex};
123 mSurfaceControl = surface;
124 mWidth = width;
125 mHeight = height;
126 mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight);
127}
128
129static void transactionCallbackThunk(void* context, nsecs_t latchTime,
130 const sp<Fence>& presentFence,
131 const std::vector<SurfaceControlStats>& stats) {
132 if (context == nullptr) {
133 return;
134 }
135 BLASTBufferQueue* bq = static_cast<BLASTBufferQueue*>(context);
136 bq->transactionCallback(latchTime, presentFence, stats);
137}
138
139void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
140 const std::vector<SurfaceControlStats>& stats) {
141 std::unique_lock _lock{mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -0800142 ATRACE_CALL();
Robert Carr78c25dd2019-08-15 14:10:33 -0700143
Valerie Hau871d6352020-01-29 08:44:02 -0800144 if (!stats.empty()) {
145 mTransformHint = stats[0].transformHint;
146 mBufferItemConsumer->setTransformHint(mTransformHint);
147 mBufferItemConsumer->updateFrameTimestamps(stats[0].frameEventStats.frameNumber,
148 stats[0].frameEventStats.refreshStartTime,
149 stats[0].frameEventStats.gpuCompositionDoneFence,
150 stats[0].presentFence,
151 stats[0].previousReleaseFence,
152 stats[0].frameEventStats.compositorTiming,
153 stats[0].latchTime,
154 stats[0].frameEventStats.dequeueReadyTime);
155 }
Valerie Haua32c5522019-12-09 10:11:08 -0800156 if (mPendingReleaseItem.item.mGraphicBuffer != nullptr) {
Valerie Hau871d6352020-01-29 08:44:02 -0800157 if (!stats.empty()) {
Valerie Haua32c5522019-12-09 10:11:08 -0800158 mPendingReleaseItem.releaseFence = stats[0].previousReleaseFence;
Valerie Haua32c5522019-12-09 10:11:08 -0800159 } else {
160 ALOGE("Warning: no SurfaceControlStats returned in BLASTBufferQueue callback");
161 mPendingReleaseItem.releaseFence = nullptr;
162 }
163 mBufferItemConsumer->releaseBuffer(mPendingReleaseItem.item,
164 mPendingReleaseItem.releaseFence
165 ? mPendingReleaseItem.releaseFence
Robert Carr78c25dd2019-08-15 14:10:33 -0700166 : Fence::NO_FENCE);
Valerie Haua32c5522019-12-09 10:11:08 -0800167 mNumAcquired--;
168 mPendingReleaseItem.item = BufferItem();
169 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -0700170 }
Valerie Haua32c5522019-12-09 10:11:08 -0800171
172 if (mSubmitted.empty()) {
173 ALOGE("ERROR: callback with no corresponding submitted buffer item");
174 }
175 mPendingReleaseItem.item = std::move(mSubmitted.front());
176 mSubmitted.pop();
Valerie Haud3b90d22019-11-06 09:37:31 -0800177 processNextBufferLocked();
178 mCallbackCV.notify_all();
Robert Carr78c25dd2019-08-15 14:10:33 -0700179 decStrong((void*)transactionCallbackThunk);
180}
181
Valerie Haud3b90d22019-11-06 09:37:31 -0800182void BLASTBufferQueue::processNextBufferLocked() {
Valerie Haua32c5522019-12-09 10:11:08 -0800183 ATRACE_CALL();
Valerie Haud44034f2020-01-17 12:51:56 -0800184 if (mNumFrameAvailable == 0 || mNumAcquired == MAX_ACQUIRED_BUFFERS) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800185 return;
186 }
187
Valerie Haua32c5522019-12-09 10:11:08 -0800188 if (mSurfaceControl == nullptr) {
189 ALOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800190 return;
191 }
192
Robert Carr78c25dd2019-08-15 14:10:33 -0700193 SurfaceComposerClient::Transaction localTransaction;
194 bool applyTransaction = true;
195 SurfaceComposerClient::Transaction* t = &localTransaction;
196 if (mNextTransaction != nullptr) {
197 t = mNextTransaction;
198 mNextTransaction = nullptr;
199 applyTransaction = false;
200 }
201
Valerie Haua32c5522019-12-09 10:11:08 -0800202 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800203
Valerie Haua32c5522019-12-09 10:11:08 -0800204 status_t status = mBufferItemConsumer->acquireBuffer(&bufferItem, -1, false);
Robert Carr78c25dd2019-08-15 14:10:33 -0700205 if (status != OK) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700206 return;
207 }
Valerie Haua32c5522019-12-09 10:11:08 -0800208 auto buffer = bufferItem.mGraphicBuffer;
209 mNumFrameAvailable--;
210
211 if (buffer == nullptr) {
212 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
213 return;
214 }
215
216 mNumAcquired++;
217 mSubmitted.push(bufferItem);
Robert Carr78c25dd2019-08-15 14:10:33 -0700218
Valerie Hau871d6352020-01-29 08:44:02 -0800219 bool needsDisconnect = false;
220 mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
221
222 // if producer disconnected before, notify SurfaceFlinger
223 if (needsDisconnect) {
224 t->notifyProducerDisconnect(mSurfaceControl);
225 }
226
Robert Carr78c25dd2019-08-15 14:10:33 -0700227 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
228 incStrong((void*)transactionCallbackThunk);
229
230 t->setBuffer(mSurfaceControl, buffer);
231 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800232 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700233 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
234
Valerie Hau81ec6072020-01-30 09:45:24 -0800235 t->setFrame(mSurfaceControl, {0, 0, mWidth, mHeight});
Valerie Haua32c5522019-12-09 10:11:08 -0800236 t->setCrop(mSurfaceControl, computeCrop(bufferItem));
237 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800238 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Valerie Hau181abd32020-01-27 14:18:28 -0800239 t->setDesiredPresentTime(bufferItem.mTimestamp);
Robert Carr78c25dd2019-08-15 14:10:33 -0700240
241 if (applyTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700242 t->apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700243 }
244}
245
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800246Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
247 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
248 return GLConsumer::scaleDownCrop(item.mCrop, mWidth, mHeight);
249 }
250 return item.mCrop;
251}
252
Valerie Haua32c5522019-12-09 10:11:08 -0800253void BLASTBufferQueue::onFrameAvailable(const BufferItem& /*item*/) {
254 ATRACE_CALL();
Valerie Haud3b90d22019-11-06 09:37:31 -0800255 std::lock_guard _lock{mMutex};
256
257 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800258 mNumFrameAvailable++;
Valerie Haud3b90d22019-11-06 09:37:31 -0800259 processNextBufferLocked();
Valerie Haud3b90d22019-11-06 09:37:31 -0800260}
261
Robert Carr78c25dd2019-08-15 14:10:33 -0700262void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800263 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700264 mNextTransaction = t;
265}
266
267} // namespace android