blob: 3f359f537f988e657ed523bff4f6dc5f140781ae [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
34BLASTBufferQueue::BLASTBufferQueue(const sp<SurfaceControl>& surface, int width, int height)
Valerie Haud3b90d22019-11-06 09:37:31 -080035 : mSurfaceControl(surface),
Valerie Haud3b90d22019-11-06 09:37:31 -080036 mWidth(width),
37 mHeight(height),
38 mNextTransaction(nullptr) {
Robert Carr78c25dd2019-08-15 14:10:33 -070039 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
Valerie Haua32c5522019-12-09 10:11:08 -080040 mConsumer->setMaxAcquiredBufferCount(MAX_ACQUIRED_BUFFERS);
Robert Carr78c25dd2019-08-15 14:10:33 -070041 mBufferItemConsumer =
42 new BufferItemConsumer(mConsumer, AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER, 1, true);
Valerie Haua32c5522019-12-09 10:11:08 -080043 static int32_t id = 0;
44 auto name = std::string("BLAST Consumer") + std::to_string(id);
45 id++;
46 mBufferItemConsumer->setName(String8(name.c_str()));
Robert Carr78c25dd2019-08-15 14:10:33 -070047 mBufferItemConsumer->setFrameAvailableListener(this);
48 mBufferItemConsumer->setBufferFreedListener(this);
49 mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight);
50 mBufferItemConsumer->setDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888);
Valerie Hau2882e982020-01-23 13:33:10 -080051 mTransformHint = mSurfaceControl->getTransformHint();
Valerie Haud3b90d22019-11-06 09:37:31 -080052
Valerie Haua32c5522019-12-09 10:11:08 -080053 mNumAcquired = 0;
54 mNumFrameAvailable = 0;
55 mPendingReleaseItem.item = BufferItem();
56 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -070057}
58
59void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, int width, int height) {
60 std::unique_lock _lock{mMutex};
61 mSurfaceControl = surface;
62 mWidth = width;
63 mHeight = height;
64 mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight);
65}
66
67static void transactionCallbackThunk(void* context, nsecs_t latchTime,
68 const sp<Fence>& presentFence,
69 const std::vector<SurfaceControlStats>& stats) {
70 if (context == nullptr) {
71 return;
72 }
73 BLASTBufferQueue* bq = static_cast<BLASTBufferQueue*>(context);
74 bq->transactionCallback(latchTime, presentFence, stats);
75}
76
77void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/,
78 const std::vector<SurfaceControlStats>& stats) {
79 std::unique_lock _lock{mMutex};
Valerie Haua32c5522019-12-09 10:11:08 -080080 ATRACE_CALL();
Robert Carr78c25dd2019-08-15 14:10:33 -070081
Valerie Haua32c5522019-12-09 10:11:08 -080082 if (mPendingReleaseItem.item.mGraphicBuffer != nullptr) {
83 if (stats.size() > 0) {
84 mPendingReleaseItem.releaseFence = stats[0].previousReleaseFence;
85 mTransformHint = stats[0].transformHint;
86 } else {
87 ALOGE("Warning: no SurfaceControlStats returned in BLASTBufferQueue callback");
88 mPendingReleaseItem.releaseFence = nullptr;
89 }
90 mBufferItemConsumer->releaseBuffer(mPendingReleaseItem.item,
91 mPendingReleaseItem.releaseFence
92 ? mPendingReleaseItem.releaseFence
Robert Carr78c25dd2019-08-15 14:10:33 -070093 : Fence::NO_FENCE);
Valerie Haua32c5522019-12-09 10:11:08 -080094 mNumAcquired--;
95 mPendingReleaseItem.item = BufferItem();
96 mPendingReleaseItem.releaseFence = nullptr;
Robert Carr78c25dd2019-08-15 14:10:33 -070097 }
Valerie Haua32c5522019-12-09 10:11:08 -080098
99 if (mSubmitted.empty()) {
100 ALOGE("ERROR: callback with no corresponding submitted buffer item");
101 }
102 mPendingReleaseItem.item = std::move(mSubmitted.front());
103 mSubmitted.pop();
Valerie Haud3b90d22019-11-06 09:37:31 -0800104 processNextBufferLocked();
105 mCallbackCV.notify_all();
Robert Carr78c25dd2019-08-15 14:10:33 -0700106 decStrong((void*)transactionCallbackThunk);
107}
108
Valerie Haud3b90d22019-11-06 09:37:31 -0800109void BLASTBufferQueue::processNextBufferLocked() {
Valerie Haua32c5522019-12-09 10:11:08 -0800110 ATRACE_CALL();
Valerie Haud44034f2020-01-17 12:51:56 -0800111 if (mNumFrameAvailable == 0 || mNumAcquired == MAX_ACQUIRED_BUFFERS) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800112 return;
113 }
114
Valerie Haua32c5522019-12-09 10:11:08 -0800115 if (mSurfaceControl == nullptr) {
116 ALOGE("ERROR : surface control is null");
Valerie Haud3b90d22019-11-06 09:37:31 -0800117 return;
118 }
119
Robert Carr78c25dd2019-08-15 14:10:33 -0700120 SurfaceComposerClient::Transaction localTransaction;
121 bool applyTransaction = true;
122 SurfaceComposerClient::Transaction* t = &localTransaction;
123 if (mNextTransaction != nullptr) {
124 t = mNextTransaction;
125 mNextTransaction = nullptr;
126 applyTransaction = false;
127 }
128
Valerie Haua32c5522019-12-09 10:11:08 -0800129 BufferItem bufferItem;
Valerie Haud3b90d22019-11-06 09:37:31 -0800130
Valerie Haua32c5522019-12-09 10:11:08 -0800131 status_t status = mBufferItemConsumer->acquireBuffer(&bufferItem, -1, false);
Robert Carr78c25dd2019-08-15 14:10:33 -0700132 if (status != OK) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700133 return;
134 }
Valerie Haua32c5522019-12-09 10:11:08 -0800135 auto buffer = bufferItem.mGraphicBuffer;
136 mNumFrameAvailable--;
137
138 if (buffer == nullptr) {
139 mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE);
140 return;
141 }
142
143 mNumAcquired++;
144 mSubmitted.push(bufferItem);
Robert Carr78c25dd2019-08-15 14:10:33 -0700145
Robert Carr78c25dd2019-08-15 14:10:33 -0700146 // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
147 incStrong((void*)transactionCallbackThunk);
148
149 t->setBuffer(mSurfaceControl, buffer);
150 t->setAcquireFence(mSurfaceControl,
Valerie Haua32c5522019-12-09 10:11:08 -0800151 bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE);
Robert Carr78c25dd2019-08-15 14:10:33 -0700152 t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
153
154 t->setFrame(mSurfaceControl, {0, 0, (int32_t)buffer->getWidth(), (int32_t)buffer->getHeight()});
Valerie Haua32c5522019-12-09 10:11:08 -0800155 t->setCrop(mSurfaceControl, computeCrop(bufferItem));
156 t->setTransform(mSurfaceControl, bufferItem.mTransform);
Valerie Hau2882e982020-01-23 13:33:10 -0800157 t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
Valerie Hau181abd32020-01-27 14:18:28 -0800158 t->setDesiredPresentTime(bufferItem.mTimestamp);
Robert Carr78c25dd2019-08-15 14:10:33 -0700159
160 if (applyTransaction) {
Robert Carr78c25dd2019-08-15 14:10:33 -0700161 t->apply();
Robert Carr78c25dd2019-08-15 14:10:33 -0700162 }
163}
164
Valerie Hau45e4b3b2019-12-03 10:49:17 -0800165Rect BLASTBufferQueue::computeCrop(const BufferItem& item) {
166 if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
167 return GLConsumer::scaleDownCrop(item.mCrop, mWidth, mHeight);
168 }
169 return item.mCrop;
170}
171
Valerie Haua32c5522019-12-09 10:11:08 -0800172void BLASTBufferQueue::onFrameAvailable(const BufferItem& /*item*/) {
173 ATRACE_CALL();
Valerie Haud3b90d22019-11-06 09:37:31 -0800174 std::lock_guard _lock{mMutex};
175
176 // add to shadow queue
Valerie Haua32c5522019-12-09 10:11:08 -0800177 mNumFrameAvailable++;
Valerie Haud3b90d22019-11-06 09:37:31 -0800178 processNextBufferLocked();
Valerie Haud3b90d22019-11-06 09:37:31 -0800179}
180
Robert Carr78c25dd2019-08-15 14:10:33 -0700181void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) {
Valerie Haud3b90d22019-11-06 09:37:31 -0800182 std::lock_guard _lock{mMutex};
Robert Carr78c25dd2019-08-15 14:10:33 -0700183 mNextTransaction = t;
184}
185
186} // namespace android