Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 1 | /* |
| 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 Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "BLASTBufferQueue" |
| 19 | |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Vishnu Nair | e1a4232 | 2020-10-02 17:42:04 -0700 | [diff] [blame] | 21 | //#define LOG_NDEBUG 0 |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 22 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 23 | #include <gui/BLASTBufferQueue.h> |
| 24 | #include <gui/BufferItemConsumer.h> |
Vishnu Nair | 8949612 | 2020-12-14 17:14:53 -0800 | [diff] [blame] | 25 | #include <gui/BufferQueueConsumer.h> |
| 26 | #include <gui/BufferQueueCore.h> |
| 27 | #include <gui/BufferQueueProducer.h> |
Valerie Hau | 45e4b3b | 2019-12-03 10:49:17 -0800 | [diff] [blame] | 28 | #include <gui/GLConsumer.h> |
Vishnu Nair | 8949612 | 2020-12-14 17:14:53 -0800 | [diff] [blame] | 29 | #include <gui/IProducerListener.h> |
Robert Carr | 05086b2 | 2020-10-13 18:22:51 -0700 | [diff] [blame] | 30 | #include <gui/Surface.h> |
Vishnu Nair | 8949612 | 2020-12-14 17:14:53 -0800 | [diff] [blame] | 31 | #include <utils/Singleton.h> |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 32 | #include <utils/Trace.h> |
| 33 | |
Ady Abraham | 0bde6b5 | 2021-05-18 13:57:02 -0700 | [diff] [blame] | 34 | #include <private/gui/ComposerService.h> |
| 35 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 36 | #include <chrono> |
| 37 | |
| 38 | using namespace std::chrono_literals; |
| 39 | |
Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 40 | namespace { |
| 41 | inline const char* toString(bool b) { |
| 42 | return b ? "true" : "false"; |
| 43 | } |
| 44 | } // namespace |
| 45 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 46 | namespace android { |
| 47 | |
Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 48 | // Macros to include adapter info in log messages |
| 49 | #define BQA_LOGV(x, ...) \ |
| 50 | ALOGV("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__) |
Vishnu Nair | c6f89ee | 2020-12-11 14:27:32 -0800 | [diff] [blame] | 51 | // enable logs for a single layer |
| 52 | //#define BQA_LOGV(x, ...) \ |
| 53 | // ALOGV_IF((strstr(mName.c_str(), "SurfaceView") != nullptr), "[%s](f:%u,a:%u) " x, \ |
| 54 | // mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__) |
Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 55 | #define BQA_LOGE(x, ...) \ |
| 56 | ALOGE("[%s](f:%u,a:%u) " x, mName.c_str(), mNumFrameAvailable, mNumAcquired, ##__VA_ARGS__) |
| 57 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 58 | void BLASTBufferItemConsumer::onDisconnect() { |
Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame] | 59 | Mutex::Autolock lock(mMutex); |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 60 | mPreviouslyConnected = mCurrentlyConnected; |
| 61 | mCurrentlyConnected = false; |
| 62 | if (mPreviouslyConnected) { |
| 63 | mDisconnectEvents.push(mCurrentFrameNumber); |
| 64 | } |
| 65 | mFrameEventHistory.onDisconnect(); |
| 66 | } |
| 67 | |
| 68 | void BLASTBufferItemConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| 69 | FrameEventHistoryDelta* outDelta) { |
Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame] | 70 | Mutex::Autolock lock(mMutex); |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 71 | if (newTimestamps) { |
| 72 | // BufferQueueProducer only adds a new timestamp on |
| 73 | // queueBuffer |
| 74 | mCurrentFrameNumber = newTimestamps->frameNumber; |
| 75 | mFrameEventHistory.addQueue(*newTimestamps); |
| 76 | } |
| 77 | if (outDelta) { |
| 78 | // frame event histories will be processed |
| 79 | // only after the producer connects and requests |
| 80 | // deltas for the first time. Forward this intent |
| 81 | // to SF-side to turn event processing back on |
| 82 | mPreviouslyConnected = mCurrentlyConnected; |
| 83 | mCurrentlyConnected = true; |
| 84 | mFrameEventHistory.getAndResetDelta(outDelta); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void BLASTBufferItemConsumer::updateFrameTimestamps(uint64_t frameNumber, nsecs_t refreshStartTime, |
| 89 | const sp<Fence>& glDoneFence, |
| 90 | const sp<Fence>& presentFence, |
| 91 | const sp<Fence>& prevReleaseFence, |
| 92 | CompositorTiming compositorTiming, |
| 93 | nsecs_t latchTime, nsecs_t dequeueReadyTime) { |
Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame] | 94 | Mutex::Autolock lock(mMutex); |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 95 | |
| 96 | // if the producer is not connected, don't bother updating, |
| 97 | // the next producer that connects won't access this frame event |
| 98 | if (!mCurrentlyConnected) return; |
| 99 | std::shared_ptr<FenceTime> glDoneFenceTime = std::make_shared<FenceTime>(glDoneFence); |
| 100 | std::shared_ptr<FenceTime> presentFenceTime = std::make_shared<FenceTime>(presentFence); |
| 101 | std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(prevReleaseFence); |
| 102 | |
| 103 | mFrameEventHistory.addLatch(frameNumber, latchTime); |
| 104 | mFrameEventHistory.addRelease(frameNumber, dequeueReadyTime, std::move(releaseFenceTime)); |
| 105 | mFrameEventHistory.addPreComposition(frameNumber, refreshStartTime); |
| 106 | mFrameEventHistory.addPostComposition(frameNumber, glDoneFenceTime, presentFenceTime, |
| 107 | compositorTiming); |
| 108 | } |
| 109 | |
| 110 | void BLASTBufferItemConsumer::getConnectionEvents(uint64_t frameNumber, bool* needsDisconnect) { |
| 111 | bool disconnect = false; |
Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame] | 112 | Mutex::Autolock lock(mMutex); |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 113 | while (!mDisconnectEvents.empty() && mDisconnectEvents.front() <= frameNumber) { |
| 114 | disconnect = true; |
| 115 | mDisconnectEvents.pop(); |
| 116 | } |
| 117 | if (needsDisconnect != nullptr) *needsDisconnect = disconnect; |
| 118 | } |
| 119 | |
Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame] | 120 | void BLASTBufferItemConsumer::onSidebandStreamChanged() { |
Ady Abraham | dc76ca0 | 2021-12-15 11:58:56 -0800 | [diff] [blame] | 121 | sp<BLASTBufferQueue> bbq = mBLASTBufferQueue.promote(); |
| 122 | if (bbq != nullptr) { |
Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame] | 123 | sp<NativeHandle> stream = getSidebandStream(); |
Ady Abraham | dc76ca0 | 2021-12-15 11:58:56 -0800 | [diff] [blame] | 124 | bbq->setSidebandStream(stream); |
Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 128 | BLASTBufferQueue::BLASTBufferQueue(const std::string& name, const sp<SurfaceControl>& surface, |
Vishnu Nair | debd1cb | 2021-03-16 10:06:01 -0700 | [diff] [blame] | 129 | int width, int height, int32_t format) |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 130 | : mSurfaceControl(surface), |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 131 | mSize(width, height), |
| 132 | mRequestedSize(mSize), |
chaviw | 565ee54 | 2021-01-14 10:21:23 -0800 | [diff] [blame] | 133 | mFormat(format), |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 134 | mNextTransaction(nullptr) { |
Vishnu Nair | 8949612 | 2020-12-14 17:14:53 -0800 | [diff] [blame] | 135 | createBufferQueue(&mProducer, &mConsumer); |
Valerie Hau | 0889c62 | 2020-02-19 15:04:47 -0800 | [diff] [blame] | 136 | // since the adapter is in the client process, set dequeue timeout |
| 137 | // explicitly so that dequeueBuffer will block |
| 138 | mProducer->setDequeueTimeout(std::numeric_limits<int64_t>::max()); |
Valerie Hau | 65b8e87 | 2020-02-13 09:45:14 -0800 | [diff] [blame] | 139 | |
Vishnu Nair | debd1cb | 2021-03-16 10:06:01 -0700 | [diff] [blame] | 140 | // safe default, most producers are expected to override this |
| 141 | mProducer->setMaxDequeuedBufferCount(2); |
Vishnu Nair | 1618c67 | 2021-02-05 13:08:26 -0800 | [diff] [blame] | 142 | mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer, |
| 143 | GraphicBuffer::USAGE_HW_COMPOSER | |
| 144 | GraphicBuffer::USAGE_HW_TEXTURE, |
Ady Abraham | dc76ca0 | 2021-12-15 11:58:56 -0800 | [diff] [blame] | 145 | 1, false, this); |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 146 | static int32_t id = 0; |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 147 | mName = name + "#" + std::to_string(id); |
Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 148 | auto consumerName = mName + "(BLAST Consumer)" + std::to_string(id); |
Vishnu Nair | 2a52ca6 | 2021-06-24 13:08:53 -0700 | [diff] [blame] | 149 | mQueuedBufferTrace = "QueuedBuffer - " + mName + "BLAST#" + std::to_string(id); |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 150 | id++; |
Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 151 | mBufferItemConsumer->setName(String8(consumerName.c_str())); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 152 | mBufferItemConsumer->setFrameAvailableListener(this); |
| 153 | mBufferItemConsumer->setBufferFreedListener(this); |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 154 | mBufferItemConsumer->setDefaultBufferSize(mSize.width, mSize.height); |
chaviw | 497e81c | 2021-02-04 17:09:47 -0800 | [diff] [blame] | 155 | mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format)); |
Robert Carr | 9f133d7 | 2020-04-01 15:51:46 -0700 | [diff] [blame] | 156 | |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 157 | ComposerService::getComposerService()->getMaxAcquiredBufferCount(&mMaxAcquiredBuffers); |
Ady Abraham | 0bde6b5 | 2021-05-18 13:57:02 -0700 | [diff] [blame] | 158 | mBufferItemConsumer->setMaxAcquiredBufferCount(mMaxAcquiredBuffers); |
| 159 | |
Valerie Hau | 2882e98 | 2020-01-23 13:33:10 -0800 | [diff] [blame] | 160 | mTransformHint = mSurfaceControl->getTransformHint(); |
Robert Carr | 9f133d7 | 2020-04-01 15:51:46 -0700 | [diff] [blame] | 161 | mBufferItemConsumer->setTransformHint(mTransformHint); |
Vishnu Nair | f6eddb6 | 2021-01-27 22:02:11 -0800 | [diff] [blame] | 162 | SurfaceComposerClient::Transaction() |
Vishnu Nair | 084514a | 2021-07-30 16:07:42 -0700 | [diff] [blame] | 163 | .setFlags(surface, layer_state_t::eEnableBackpressure, |
| 164 | layer_state_t::eEnableBackpressure) |
| 165 | .setApplyToken(mApplyToken) |
| 166 | .apply(); |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 167 | mNumAcquired = 0; |
| 168 | mNumFrameAvailable = 0; |
Vishnu Nair | 9c16128 | 2021-07-07 16:52:34 -0700 | [diff] [blame] | 169 | BQA_LOGV("BLASTBufferQueue created width=%d height=%d format=%d mTransformHint=%d", width, |
| 170 | height, format, mTransformHint); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Vishnu Nair | c4a40c1 | 2020-12-23 09:14:32 -0800 | [diff] [blame] | 173 | BLASTBufferQueue::~BLASTBufferQueue() { |
| 174 | if (mPendingTransactions.empty()) { |
| 175 | return; |
| 176 | } |
| 177 | BQA_LOGE("Applying pending transactions on dtor %d", |
| 178 | static_cast<uint32_t>(mPendingTransactions.size())); |
| 179 | SurfaceComposerClient::Transaction t; |
| 180 | for (auto& [targetFrameNumber, transaction] : mPendingTransactions) { |
| 181 | t.merge(std::move(transaction)); |
| 182 | } |
| 183 | t.apply(); |
| 184 | } |
| 185 | |
chaviw | 565ee54 | 2021-01-14 10:21:23 -0800 | [diff] [blame] | 186 | void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, uint32_t width, uint32_t height, |
Vishnu Nair | 084514a | 2021-07-30 16:07:42 -0700 | [diff] [blame] | 187 | int32_t format, SurfaceComposerClient::Transaction* outTransaction) { |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 188 | std::unique_lock _lock{mMutex}; |
chaviw | 565ee54 | 2021-01-14 10:21:23 -0800 | [diff] [blame] | 189 | if (mFormat != format) { |
| 190 | mFormat = format; |
chaviw | 497e81c | 2021-02-04 17:09:47 -0800 | [diff] [blame] | 191 | mBufferItemConsumer->setDefaultBufferFormat(convertBufferFormat(format)); |
chaviw | 565ee54 | 2021-01-14 10:21:23 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Vishnu Nair | f6eddb6 | 2021-01-27 22:02:11 -0800 | [diff] [blame] | 194 | SurfaceComposerClient::Transaction t; |
Vishnu Nair | 5fa91c2 | 2021-06-29 14:30:48 -0700 | [diff] [blame] | 195 | const bool setBackpressureFlag = !SurfaceControl::isSameSurface(mSurfaceControl, surface); |
Vishnu Nair | f6eddb6 | 2021-01-27 22:02:11 -0800 | [diff] [blame] | 196 | bool applyTransaction = false; |
Vishnu Nair | f6eddb6 | 2021-01-27 22:02:11 -0800 | [diff] [blame] | 197 | |
Vishnu Nair | 5fa91c2 | 2021-06-29 14:30:48 -0700 | [diff] [blame] | 198 | // Always update the native object even though they might have the same layer handle, so we can |
| 199 | // get the updated transform hint from WM. |
| 200 | mSurfaceControl = surface; |
Arthur Hung | b6aa9a0 | 2021-06-09 14:23:01 +0800 | [diff] [blame] | 201 | if (mSurfaceControl != nullptr) { |
Vishnu Nair | 5fa91c2 | 2021-06-29 14:30:48 -0700 | [diff] [blame] | 202 | if (setBackpressureFlag) { |
| 203 | t.setFlags(mSurfaceControl, layer_state_t::eEnableBackpressure, |
| 204 | layer_state_t::eEnableBackpressure); |
| 205 | applyTransaction = true; |
| 206 | } |
Arthur Hung | b6aa9a0 | 2021-06-09 14:23:01 +0800 | [diff] [blame] | 207 | mTransformHint = mSurfaceControl->getTransformHint(); |
| 208 | mBufferItemConsumer->setTransformHint(mTransformHint); |
| 209 | } |
Vishnu Nair | 9c16128 | 2021-07-07 16:52:34 -0700 | [diff] [blame] | 210 | BQA_LOGV("update width=%d height=%d format=%d mTransformHint=%d", width, height, format, |
| 211 | mTransformHint); |
Arthur Hung | b6aa9a0 | 2021-06-09 14:23:01 +0800 | [diff] [blame] | 212 | |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 213 | ui::Size newSize(width, height); |
| 214 | if (mRequestedSize != newSize) { |
| 215 | mRequestedSize.set(newSize); |
| 216 | mBufferItemConsumer->setDefaultBufferSize(mRequestedSize.width, mRequestedSize.height); |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 217 | if (mLastBufferInfo.scalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) { |
Vishnu Nair | 53c936c | 2020-12-03 11:46:37 -0800 | [diff] [blame] | 218 | // If the buffer supports scaling, update the frame immediately since the client may |
| 219 | // want to scale the existing buffer to the new size. |
| 220 | mSize = mRequestedSize; |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 221 | // We only need to update the scale if we've received at least one buffer. The reason |
| 222 | // for this is the scale is calculated based on the requested size and buffer size. |
| 223 | // If there's no buffer, the scale will always be 1. |
Vishnu Nair | 084514a | 2021-07-30 16:07:42 -0700 | [diff] [blame] | 224 | SurfaceComposerClient::Transaction* destFrameTransaction = |
| 225 | (outTransaction) ? outTransaction : &t; |
Vishnu Nair | 5fa91c2 | 2021-06-29 14:30:48 -0700 | [diff] [blame] | 226 | if (mSurfaceControl != nullptr && mLastBufferInfo.hasBuffer) { |
Vishnu Nair | 084514a | 2021-07-30 16:07:42 -0700 | [diff] [blame] | 227 | destFrameTransaction->setDestinationFrame(mSurfaceControl, |
| 228 | Rect(0, 0, newSize.getWidth(), |
| 229 | newSize.getHeight())); |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 230 | } |
Vishnu Nair | f6eddb6 | 2021-01-27 22:02:11 -0800 | [diff] [blame] | 231 | applyTransaction = true; |
Vishnu Nair | 53c936c | 2020-12-03 11:46:37 -0800 | [diff] [blame] | 232 | } |
Robert Carr | fc41651 | 2020-04-02 12:32:44 -0700 | [diff] [blame] | 233 | } |
Vishnu Nair | f6eddb6 | 2021-01-27 22:02:11 -0800 | [diff] [blame] | 234 | if (applyTransaction) { |
Vishnu Nair | 084514a | 2021-07-30 16:07:42 -0700 | [diff] [blame] | 235 | t.setApplyToken(mApplyToken).apply(); |
Vishnu Nair | f6eddb6 | 2021-01-27 22:02:11 -0800 | [diff] [blame] | 236 | } |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void transactionCallbackThunk(void* context, nsecs_t latchTime, |
| 240 | const sp<Fence>& presentFence, |
| 241 | const std::vector<SurfaceControlStats>& stats) { |
| 242 | if (context == nullptr) { |
| 243 | return; |
| 244 | } |
Robert Carr | fbcbb4c | 2020-11-02 14:14:34 -0800 | [diff] [blame] | 245 | sp<BLASTBufferQueue> bq = static_cast<BLASTBufferQueue*>(context); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 246 | bq->transactionCallback(latchTime, presentFence, stats); |
| 247 | } |
| 248 | |
| 249 | void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/, |
| 250 | const std::vector<SurfaceControlStats>& stats) { |
chaviw | 71c2cc4 | 2020-10-23 16:42:02 -0700 | [diff] [blame] | 251 | std::function<void(int64_t)> transactionCompleteCallback = nullptr; |
| 252 | uint64_t currFrameNumber = 0; |
Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 253 | |
chaviw | 71c2cc4 | 2020-10-23 16:42:02 -0700 | [diff] [blame] | 254 | { |
| 255 | std::unique_lock _lock{mMutex}; |
| 256 | ATRACE_CALL(); |
| 257 | BQA_LOGV("transactionCallback"); |
chaviw | 71c2cc4 | 2020-10-23 16:42:02 -0700 | [diff] [blame] | 258 | |
chaviw | 4202616 | 2021-04-16 15:46:12 -0500 | [diff] [blame] | 259 | if (!mSurfaceControlsWithPendingCallback.empty()) { |
| 260 | sp<SurfaceControl> pendingSC = mSurfaceControlsWithPendingCallback.front(); |
| 261 | mSurfaceControlsWithPendingCallback.pop(); |
| 262 | bool found = false; |
| 263 | for (auto stat : stats) { |
| 264 | if (!SurfaceControl::isSameSurface(pendingSC, stat.surfaceControl)) { |
| 265 | continue; |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 266 | } |
chaviw | 4202616 | 2021-04-16 15:46:12 -0500 | [diff] [blame] | 267 | |
| 268 | mTransformHint = stat.transformHint; |
| 269 | mBufferItemConsumer->setTransformHint(mTransformHint); |
Vishnu Nair | 9c16128 | 2021-07-07 16:52:34 -0700 | [diff] [blame] | 270 | BQA_LOGV("updated mTransformHint=%d", mTransformHint); |
Vishnu Nair | de66dc7 | 2021-06-17 17:54:41 -0700 | [diff] [blame] | 271 | // Update frametime stamps if the frame was latched and presented, indicated by a |
| 272 | // valid latch time. |
| 273 | if (stat.latchTime > 0) { |
| 274 | mBufferItemConsumer |
| 275 | ->updateFrameTimestamps(stat.frameEventStats.frameNumber, |
| 276 | stat.frameEventStats.refreshStartTime, |
| 277 | stat.frameEventStats.gpuCompositionDoneFence, |
| 278 | stat.presentFence, stat.previousReleaseFence, |
| 279 | stat.frameEventStats.compositorTiming, |
| 280 | stat.latchTime, |
| 281 | stat.frameEventStats.dequeueReadyTime); |
| 282 | } |
chaviw | 4202616 | 2021-04-16 15:46:12 -0500 | [diff] [blame] | 283 | currFrameNumber = stat.frameEventStats.frameNumber; |
| 284 | |
| 285 | if (mTransactionCompleteCallback && |
| 286 | currFrameNumber >= mTransactionCompleteFrameNumber) { |
| 287 | if (currFrameNumber > mTransactionCompleteFrameNumber) { |
| 288 | BQA_LOGE("transactionCallback received for a newer framenumber=%" PRIu64 |
| 289 | " than expected=%" PRIu64, |
| 290 | currFrameNumber, mTransactionCompleteFrameNumber); |
| 291 | } |
| 292 | transactionCompleteCallback = std::move(mTransactionCompleteCallback); |
| 293 | mTransactionCompleteFrameNumber = 0; |
| 294 | } |
| 295 | |
| 296 | found = true; |
| 297 | break; |
chaviw | 71c2cc4 | 2020-10-23 16:42:02 -0700 | [diff] [blame] | 298 | } |
chaviw | 4202616 | 2021-04-16 15:46:12 -0500 | [diff] [blame] | 299 | |
| 300 | if (!found) { |
| 301 | BQA_LOGE("Failed to find matching SurfaceControl in transaction callback"); |
| 302 | } |
| 303 | } else { |
| 304 | BQA_LOGE("No matching SurfaceControls found: mSurfaceControlsWithPendingCallback was " |
| 305 | "empty."); |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 306 | } |
chaviw | 71c2cc4 | 2020-10-23 16:42:02 -0700 | [diff] [blame] | 307 | |
chaviw | 71c2cc4 | 2020-10-23 16:42:02 -0700 | [diff] [blame] | 308 | decStrong((void*)transactionCallbackThunk); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 309 | } |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 310 | |
chaviw | 71c2cc4 | 2020-10-23 16:42:02 -0700 | [diff] [blame] | 311 | if (transactionCompleteCallback) { |
| 312 | transactionCompleteCallback(currFrameNumber); |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 313 | } |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 316 | // Unlike transactionCallbackThunk the release buffer callback does not extend the life of the |
| 317 | // BBQ. This is because if the BBQ is destroyed, then the buffers will be released by the client. |
| 318 | // So we pass in a weak pointer to the BBQ and if it still alive, then we release the buffer. |
| 319 | // Otherwise, this is a no-op. |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 320 | static void releaseBufferCallbackThunk(wp<BLASTBufferQueue> context, const ReleaseCallbackId& id, |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 321 | const sp<Fence>& releaseFence, uint32_t transformHint, |
| 322 | uint32_t currentMaxAcquiredBufferCount) { |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 323 | sp<BLASTBufferQueue> blastBufferQueue = context.promote(); |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 324 | if (blastBufferQueue) { |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 325 | blastBufferQueue->releaseBufferCallback(id, releaseFence, transformHint, |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 326 | currentMaxAcquiredBufferCount); |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 327 | } else { |
| 328 | ALOGV("releaseBufferCallbackThunk %s blastBufferQueue is dead", id.to_string().c_str()); |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 332 | void BLASTBufferQueue::releaseBufferCallback(const ReleaseCallbackId& id, |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 333 | const sp<Fence>& releaseFence, uint32_t transformHint, |
| 334 | uint32_t currentMaxAcquiredBufferCount) { |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 335 | ATRACE_CALL(); |
| 336 | std::unique_lock _lock{mMutex}; |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 337 | BQA_LOGV("releaseBufferCallback %s", id.to_string().c_str()); |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 338 | |
Robert Carr | 82d07c9 | 2021-05-10 11:36:43 -0700 | [diff] [blame] | 339 | if (mSurfaceControl != nullptr) { |
Robert Carr | 97e7cc0 | 2021-06-07 10:45:40 -0700 | [diff] [blame] | 340 | mTransformHint = transformHint; |
| 341 | mSurfaceControl->setTransformHint(transformHint); |
Robert Carr | 82d07c9 | 2021-05-10 11:36:43 -0700 | [diff] [blame] | 342 | mBufferItemConsumer->setTransformHint(mTransformHint); |
Vishnu Nair | 9c16128 | 2021-07-07 16:52:34 -0700 | [diff] [blame] | 343 | BQA_LOGV("updated mTransformHint=%d", mTransformHint); |
Robert Carr | 82d07c9 | 2021-05-10 11:36:43 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 346 | // Calculate how many buffers we need to hold before we release them back |
| 347 | // to the buffer queue. This will prevent higher latency when we are running |
| 348 | // on a lower refresh rate than the max supported. We only do that for EGL |
| 349 | // clients as others don't care about latency |
| 350 | const bool isEGL = [&] { |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 351 | const auto it = mSubmitted.find(id); |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 352 | return it != mSubmitted.end() && it->second.mApi == NATIVE_WINDOW_API_EGL; |
| 353 | }(); |
| 354 | |
| 355 | const auto numPendingBuffersToHold = |
| 356 | isEGL ? std::max(0u, mMaxAcquiredBuffers - currentMaxAcquiredBufferCount) : 0; |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 357 | mPendingRelease.emplace_back(ReleasedBuffer{id, releaseFence}); |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 358 | |
| 359 | // Release all buffers that are beyond the ones that we need to hold |
| 360 | while (mPendingRelease.size() > numPendingBuffersToHold) { |
| 361 | const auto releaseBuffer = mPendingRelease.front(); |
| 362 | mPendingRelease.pop_front(); |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 363 | auto it = mSubmitted.find(releaseBuffer.callbackId); |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 364 | if (it == mSubmitted.end()) { |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 365 | BQA_LOGE("ERROR: releaseBufferCallback without corresponding submitted buffer %s", |
| 366 | releaseBuffer.callbackId.to_string().c_str()); |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 367 | return; |
| 368 | } |
Vishnu Nair | 2a52ca6 | 2021-06-24 13:08:53 -0700 | [diff] [blame] | 369 | mNumAcquired--; |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 370 | BQA_LOGV("released %s", id.to_string().c_str()); |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 371 | mBufferItemConsumer->releaseBuffer(it->second, releaseBuffer.releaseFence); |
| 372 | mSubmitted.erase(it); |
Vishnu Nair | 2a52ca6 | 2021-06-24 13:08:53 -0700 | [diff] [blame] | 373 | processNextBufferLocked(false /* useNextTransaction */); |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 376 | ATRACE_INT("PendingRelease", mPendingRelease.size()); |
Vishnu Nair | 2a52ca6 | 2021-06-24 13:08:53 -0700 | [diff] [blame] | 377 | ATRACE_INT(mQueuedBufferTrace.c_str(), |
| 378 | mNumFrameAvailable + mNumAcquired - mPendingRelease.size()); |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 379 | mCallbackCV.notify_all(); |
| 380 | } |
| 381 | |
Robert Carr | 255acdc | 2020-04-17 14:08:55 -0700 | [diff] [blame] | 382 | void BLASTBufferQueue::processNextBufferLocked(bool useNextTransaction) { |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 383 | ATRACE_CALL(); |
Vishnu Nair | 8b30dd1 | 2021-01-25 14:16:54 -0800 | [diff] [blame] | 384 | // If the next transaction is set, we want to guarantee the our acquire will not fail, so don't |
| 385 | // include the extra buffer when checking if we can acquire the next buffer. |
| 386 | const bool includeExtraAcquire = !useNextTransaction; |
| 387 | if (mNumFrameAvailable == 0 || maxBuffersAcquired(includeExtraAcquire)) { |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 388 | mCallbackCV.notify_all(); |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 389 | return; |
| 390 | } |
| 391 | |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 392 | if (mSurfaceControl == nullptr) { |
Vishnu Nair | 670b3f7 | 2020-09-29 17:52:18 -0700 | [diff] [blame] | 393 | BQA_LOGE("ERROR : surface control is null"); |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 394 | return; |
| 395 | } |
| 396 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 397 | SurfaceComposerClient::Transaction localTransaction; |
| 398 | bool applyTransaction = true; |
| 399 | SurfaceComposerClient::Transaction* t = &localTransaction; |
Robert Carr | 255acdc | 2020-04-17 14:08:55 -0700 | [diff] [blame] | 400 | if (mNextTransaction != nullptr && useNextTransaction) { |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 401 | t = mNextTransaction; |
| 402 | mNextTransaction = nullptr; |
| 403 | applyTransaction = false; |
| 404 | } |
| 405 | |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 406 | BufferItem bufferItem; |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 407 | |
Vishnu Nair | c6f89ee | 2020-12-11 14:27:32 -0800 | [diff] [blame] | 408 | status_t status = |
| 409 | mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false); |
Vishnu Nair | 8b30dd1 | 2021-01-25 14:16:54 -0800 | [diff] [blame] | 410 | if (status == BufferQueue::NO_BUFFER_AVAILABLE) { |
| 411 | BQA_LOGV("Failed to acquire a buffer, err=NO_BUFFER_AVAILABLE"); |
| 412 | return; |
| 413 | } else if (status != OK) { |
Vishnu Nair | bf25577 | 2020-10-16 10:54:41 -0700 | [diff] [blame] | 414 | BQA_LOGE("Failed to acquire a buffer, err=%s", statusToString(status).c_str()); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 415 | return; |
| 416 | } |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 417 | auto buffer = bufferItem.mGraphicBuffer; |
| 418 | mNumFrameAvailable--; |
| 419 | |
| 420 | if (buffer == nullptr) { |
| 421 | mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE); |
Vishnu Nair | bf25577 | 2020-10-16 10:54:41 -0700 | [diff] [blame] | 422 | BQA_LOGE("Buffer was empty"); |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | |
Vishnu Nair | 670b3f7 | 2020-09-29 17:52:18 -0700 | [diff] [blame] | 426 | if (rejectBuffer(bufferItem)) { |
Vishnu Nair | 9c16128 | 2021-07-07 16:52:34 -0700 | [diff] [blame] | 427 | BQA_LOGE("rejecting buffer:active_size=%dx%d, requested_size=%dx%d " |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 428 | "buffer{size=%dx%d transform=%d}", |
| 429 | mSize.width, mSize.height, mRequestedSize.width, mRequestedSize.height, |
| 430 | buffer->getWidth(), buffer->getHeight(), bufferItem.mTransform); |
| 431 | mBufferItemConsumer->releaseBuffer(bufferItem, Fence::NO_FENCE); |
| 432 | processNextBufferLocked(useNextTransaction); |
| 433 | return; |
Vishnu Nair | 670b3f7 | 2020-09-29 17:52:18 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 436 | mNumAcquired++; |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 437 | mLastAcquiredFrameNumber = bufferItem.mFrameNumber; |
| 438 | ReleaseCallbackId releaseCallbackId(buffer->getId(), mLastAcquiredFrameNumber); |
| 439 | mSubmitted[releaseCallbackId] = bufferItem; |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 440 | |
Valerie Hau | 871d635 | 2020-01-29 08:44:02 -0800 | [diff] [blame] | 441 | bool needsDisconnect = false; |
| 442 | mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect); |
| 443 | |
| 444 | // if producer disconnected before, notify SurfaceFlinger |
| 445 | if (needsDisconnect) { |
| 446 | t->notifyProducerDisconnect(mSurfaceControl); |
| 447 | } |
| 448 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 449 | // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback. |
| 450 | incStrong((void*)transactionCallbackThunk); |
| 451 | |
Vishnu Nair | 5cc9ac0 | 2021-04-19 13:23:38 -0700 | [diff] [blame] | 452 | Rect crop = computeCrop(bufferItem); |
Vishnu Nair | 084514a | 2021-07-30 16:07:42 -0700 | [diff] [blame] | 453 | const bool updateDestinationFrame = |
| 454 | bufferItem.mScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE || |
| 455 | !mLastBufferInfo.hasBuffer; |
Chavi Weingarten | a5aedbd | 2021-04-09 13:37:33 +0000 | [diff] [blame] | 456 | mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(), |
| 457 | bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform, |
Vishnu Nair | 5cc9ac0 | 2021-04-19 13:23:38 -0700 | [diff] [blame] | 458 | bufferItem.mScalingMode, crop); |
Vishnu Nair | 53c936c | 2020-12-03 11:46:37 -0800 | [diff] [blame] | 459 | |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 460 | auto releaseBufferCallback = |
| 461 | std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */, |
Ady Abraham | 899dcdb | 2021-06-15 16:56:21 -0700 | [diff] [blame] | 462 | std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, |
| 463 | std::placeholders::_4); |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 464 | t->setBuffer(mSurfaceControl, buffer, releaseCallbackId, releaseBufferCallback); |
John Reck | 137069e | 2020-12-10 22:07:37 -0500 | [diff] [blame] | 465 | t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace)); |
| 466 | t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata); |
| 467 | t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 468 | t->setAcquireFence(mSurfaceControl, |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 469 | bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 470 | t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this)); |
chaviw | 4202616 | 2021-04-16 15:46:12 -0500 | [diff] [blame] | 471 | mSurfaceControlsWithPendingCallback.push(mSurfaceControl); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 472 | |
Vishnu Nair | 084514a | 2021-07-30 16:07:42 -0700 | [diff] [blame] | 473 | if (updateDestinationFrame) { |
| 474 | t->setDestinationFrame(mSurfaceControl, Rect(0, 0, mSize.getWidth(), mSize.getHeight())); |
| 475 | } |
Vishnu Nair | 6bdec7d | 2021-05-10 15:01:13 -0700 | [diff] [blame] | 476 | t->setBufferCrop(mSurfaceControl, crop); |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 477 | t->setTransform(mSurfaceControl, bufferItem.mTransform); |
Valerie Hau | 2882e98 | 2020-01-23 13:33:10 -0800 | [diff] [blame] | 478 | t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse); |
Ady Abraham | f0c5649 | 2020-12-17 18:04:15 -0800 | [diff] [blame] | 479 | if (!bufferItem.mIsAutoTimestamp) { |
| 480 | t->setDesiredPresentTime(bufferItem.mTimestamp); |
| 481 | } |
Vishnu Nair | 6b7c5c9 | 2020-09-29 17:27:05 -0700 | [diff] [blame] | 482 | t->setFrameNumber(mSurfaceControl, bufferItem.mFrameNumber); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 483 | |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 484 | if (!mNextFrameTimelineInfoQueue.empty()) { |
Ady Abraham | 8db1010 | 2021-03-15 17:19:23 -0700 | [diff] [blame] | 485 | t->setFrameTimelineInfo(mNextFrameTimelineInfoQueue.front()); |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 486 | mNextFrameTimelineInfoQueue.pop(); |
Jorim Jaggi | a3fe67b | 2020-12-01 00:24:33 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Vishnu Nair | cf26a0a | 2020-11-13 12:56:20 -0800 | [diff] [blame] | 489 | if (mAutoRefresh != bufferItem.mAutoRefresh) { |
| 490 | t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh); |
| 491 | mAutoRefresh = bufferItem.mAutoRefresh; |
| 492 | } |
Vishnu Nair | adf632b | 2021-01-07 14:05:08 -0800 | [diff] [blame] | 493 | { |
| 494 | std::unique_lock _lock{mTimestampMutex}; |
| 495 | auto dequeueTime = mDequeueTimestamps.find(buffer->getId()); |
| 496 | if (dequeueTime != mDequeueTimestamps.end()) { |
| 497 | Parcel p; |
| 498 | p.writeInt64(dequeueTime->second); |
| 499 | t->setMetadata(mSurfaceControl, METADATA_DEQUEUE_TIME, p); |
| 500 | mDequeueTimestamps.erase(dequeueTime); |
| 501 | } |
| 502 | } |
Vishnu Nair | cf26a0a | 2020-11-13 12:56:20 -0800 | [diff] [blame] | 503 | |
Vishnu Nair | c4a40c1 | 2020-12-23 09:14:32 -0800 | [diff] [blame] | 504 | auto mergeTransaction = |
| 505 | [&t, currentFrameNumber = bufferItem.mFrameNumber]( |
| 506 | std::tuple<uint64_t, SurfaceComposerClient::Transaction> pendingTransaction) { |
| 507 | auto& [targetFrameNumber, transaction] = pendingTransaction; |
| 508 | if (currentFrameNumber < targetFrameNumber) { |
| 509 | return false; |
| 510 | } |
| 511 | t->merge(std::move(transaction)); |
| 512 | return true; |
| 513 | }; |
| 514 | |
| 515 | mPendingTransactions.erase(std::remove_if(mPendingTransactions.begin(), |
| 516 | mPendingTransactions.end(), mergeTransaction), |
| 517 | mPendingTransactions.end()); |
| 518 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 519 | if (applyTransaction) { |
Vishnu Nair | 277142c | 2021-01-05 18:35:29 -0800 | [diff] [blame] | 520 | t->setApplyToken(mApplyToken).apply(); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 521 | } |
Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 522 | |
| 523 | BQA_LOGV("processNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64 |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 524 | " applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d" |
Vishnu Nair | 9c16128 | 2021-07-07 16:52:34 -0700 | [diff] [blame] | 525 | " graphicBufferId=%" PRIu64 "%s transform=%d", |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 526 | mSize.width, mSize.height, bufferItem.mFrameNumber, toString(applyTransaction), |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 527 | bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "", |
Vishnu Nair | 4ba0c2e | 2021-06-24 11:27:17 -0700 | [diff] [blame] | 528 | static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(), |
Vishnu Nair | 9c16128 | 2021-07-07 16:52:34 -0700 | [diff] [blame] | 529 | bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform); |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Valerie Hau | 45e4b3b | 2019-12-03 10:49:17 -0800 | [diff] [blame] | 532 | Rect BLASTBufferQueue::computeCrop(const BufferItem& item) { |
| 533 | if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) { |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 534 | return GLConsumer::scaleDownCrop(item.mCrop, mSize.width, mSize.height); |
Valerie Hau | 45e4b3b | 2019-12-03 10:49:17 -0800 | [diff] [blame] | 535 | } |
| 536 | return item.mCrop; |
| 537 | } |
| 538 | |
Vishnu Nair | aef1de9 | 2020-10-22 12:15:53 -0700 | [diff] [blame] | 539 | void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) { |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 540 | ATRACE_CALL(); |
Valerie Hau | 0188adf | 2020-02-13 08:29:20 -0800 | [diff] [blame] | 541 | std::unique_lock _lock{mMutex}; |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 542 | |
Vishnu Nair | dab9409 | 2020-09-29 16:09:04 -0700 | [diff] [blame] | 543 | const bool nextTransactionSet = mNextTransaction != nullptr; |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 544 | if (nextTransactionSet) { |
Vishnu Nair | 8b30dd1 | 2021-01-25 14:16:54 -0800 | [diff] [blame] | 545 | while (mNumFrameAvailable > 0 || maxBuffersAcquired(false /* includeExtraAcquire */)) { |
Vishnu Nair | 7eb670a | 2020-10-15 12:16:10 -0700 | [diff] [blame] | 546 | BQA_LOGV("waiting in onFrameAvailable..."); |
Valerie Hau | 0188adf | 2020-02-13 08:29:20 -0800 | [diff] [blame] | 547 | mCallbackCV.wait(_lock); |
| 548 | } |
| 549 | } |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 550 | // add to shadow queue |
Valerie Hau | a32c552 | 2019-12-09 10:11:08 -0800 | [diff] [blame] | 551 | mNumFrameAvailable++; |
Vishnu Nair | 2a52ca6 | 2021-06-24 13:08:53 -0700 | [diff] [blame] | 552 | ATRACE_INT(mQueuedBufferTrace.c_str(), |
| 553 | mNumFrameAvailable + mNumAcquired - mPendingRelease.size()); |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 554 | |
| 555 | BQA_LOGV("onFrameAvailable framenumber=%" PRIu64 " nextTransactionSet=%s", item.mFrameNumber, |
| 556 | toString(nextTransactionSet)); |
Vishnu Nair | 8b30dd1 | 2021-01-25 14:16:54 -0800 | [diff] [blame] | 557 | processNextBufferLocked(nextTransactionSet /* useNextTransaction */); |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 558 | } |
| 559 | |
Vishnu Nair | aef1de9 | 2020-10-22 12:15:53 -0700 | [diff] [blame] | 560 | void BLASTBufferQueue::onFrameReplaced(const BufferItem& item) { |
| 561 | BQA_LOGV("onFrameReplaced framenumber=%" PRIu64, item.mFrameNumber); |
| 562 | // Do nothing since we are not storing unacquired buffer items locally. |
| 563 | } |
| 564 | |
Vishnu Nair | adf632b | 2021-01-07 14:05:08 -0800 | [diff] [blame] | 565 | void BLASTBufferQueue::onFrameDequeued(const uint64_t bufferId) { |
| 566 | std::unique_lock _lock{mTimestampMutex}; |
| 567 | mDequeueTimestamps[bufferId] = systemTime(); |
| 568 | }; |
| 569 | |
| 570 | void BLASTBufferQueue::onFrameCancelled(const uint64_t bufferId) { |
| 571 | std::unique_lock _lock{mTimestampMutex}; |
| 572 | mDequeueTimestamps.erase(bufferId); |
| 573 | }; |
| 574 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 575 | void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) { |
Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 576 | std::lock_guard _lock{mMutex}; |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 577 | mNextTransaction = t; |
| 578 | } |
| 579 | |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 580 | bool BLASTBufferQueue::rejectBuffer(const BufferItem& item) { |
Vishnu Nair | 670b3f7 | 2020-09-29 17:52:18 -0700 | [diff] [blame] | 581 | if (item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) { |
Hongguang Chen | 3310028 | 2021-04-15 18:36:05 -0700 | [diff] [blame] | 582 | mSize = mRequestedSize; |
Vishnu Nair | 670b3f7 | 2020-09-29 17:52:18 -0700 | [diff] [blame] | 583 | // Only reject buffers if scaling mode is freeze. |
| 584 | return false; |
| 585 | } |
| 586 | |
Vishnu Nair | e1a4232 | 2020-10-02 17:42:04 -0700 | [diff] [blame] | 587 | uint32_t bufWidth = item.mGraphicBuffer->getWidth(); |
| 588 | uint32_t bufHeight = item.mGraphicBuffer->getHeight(); |
| 589 | |
| 590 | // Take the buffer's orientation into account |
| 591 | if (item.mTransform & ui::Transform::ROT_90) { |
| 592 | std::swap(bufWidth, bufHeight); |
| 593 | } |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 594 | ui::Size bufferSize(bufWidth, bufHeight); |
| 595 | if (mRequestedSize != mSize && mRequestedSize == bufferSize) { |
| 596 | mSize = mRequestedSize; |
| 597 | return false; |
| 598 | } |
Vishnu Nair | e1a4232 | 2020-10-02 17:42:04 -0700 | [diff] [blame] | 599 | |
Vishnu Nair | 670b3f7 | 2020-09-29 17:52:18 -0700 | [diff] [blame] | 600 | // reject buffers if the buffer size doesn't match. |
Vishnu Nair | ea0de00 | 2020-11-17 17:42:37 -0800 | [diff] [blame] | 601 | return mSize != bufferSize; |
Vishnu Nair | 670b3f7 | 2020-09-29 17:52:18 -0700 | [diff] [blame] | 602 | } |
Vishnu Nair | bf25577 | 2020-10-16 10:54:41 -0700 | [diff] [blame] | 603 | |
chaviw | 71c2cc4 | 2020-10-23 16:42:02 -0700 | [diff] [blame] | 604 | void BLASTBufferQueue::setTransactionCompleteCallback( |
| 605 | uint64_t frameNumber, std::function<void(int64_t)>&& transactionCompleteCallback) { |
| 606 | std::lock_guard _lock{mMutex}; |
| 607 | if (transactionCompleteCallback == nullptr) { |
| 608 | mTransactionCompleteCallback = nullptr; |
| 609 | } else { |
| 610 | mTransactionCompleteCallback = std::move(transactionCompleteCallback); |
| 611 | mTransactionCompleteFrameNumber = frameNumber; |
| 612 | } |
| 613 | } |
| 614 | |
Vishnu Nair | bf25577 | 2020-10-16 10:54:41 -0700 | [diff] [blame] | 615 | // Check if we have acquired the maximum number of buffers. |
Vishnu Nair | 8b30dd1 | 2021-01-25 14:16:54 -0800 | [diff] [blame] | 616 | // Consumer can acquire an additional buffer if that buffer is not droppable. Set |
| 617 | // includeExtraAcquire is true to include this buffer to the count. Since this depends on the state |
| 618 | // of the buffer, the next acquire may return with NO_BUFFER_AVAILABLE. |
| 619 | bool BLASTBufferQueue::maxBuffersAcquired(bool includeExtraAcquire) const { |
Ady Abraham | 0bde6b5 | 2021-05-18 13:57:02 -0700 | [diff] [blame] | 620 | int maxAcquiredBuffers = mMaxAcquiredBuffers + (includeExtraAcquire ? 2 : 1); |
Vishnu Nair | 1506b18 | 2021-02-22 14:35:15 -0800 | [diff] [blame] | 621 | return mNumAcquired == maxAcquiredBuffers; |
Vishnu Nair | bf25577 | 2020-10-16 10:54:41 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Robert Carr | 05086b2 | 2020-10-13 18:22:51 -0700 | [diff] [blame] | 624 | class BBQSurface : public Surface { |
Robert Carr | 9c006e0 | 2020-10-14 13:41:57 -0700 | [diff] [blame] | 625 | private: |
Vishnu Nair | c0fa007 | 2021-08-30 15:31:08 -0700 | [diff] [blame] | 626 | std::mutex mMutex; |
Robert Carr | 9c006e0 | 2020-10-14 13:41:57 -0700 | [diff] [blame] | 627 | sp<BLASTBufferQueue> mBbq; |
Vishnu Nair | c0fa007 | 2021-08-30 15:31:08 -0700 | [diff] [blame] | 628 | bool mDestroyed = false; |
| 629 | |
Robert Carr | 05086b2 | 2020-10-13 18:22:51 -0700 | [diff] [blame] | 630 | public: |
Vishnu Nair | 992496b | 2020-10-22 17:27:21 -0700 | [diff] [blame] | 631 | BBQSurface(const sp<IGraphicBufferProducer>& igbp, bool controlledByApp, |
| 632 | const sp<IBinder>& scHandle, const sp<BLASTBufferQueue>& bbq) |
| 633 | : Surface(igbp, controlledByApp, scHandle), mBbq(bbq) {} |
Robert Carr | 9c006e0 | 2020-10-14 13:41:57 -0700 | [diff] [blame] | 634 | |
Robert Carr | 05086b2 | 2020-10-13 18:22:51 -0700 | [diff] [blame] | 635 | void allocateBuffers() override { |
| 636 | uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth; |
| 637 | uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight; |
| 638 | auto gbp = getIGraphicBufferProducer(); |
| 639 | std::thread ([reqWidth, reqHeight, gbp=getIGraphicBufferProducer(), |
| 640 | reqFormat=mReqFormat, reqUsage=mReqUsage] () { |
| 641 | gbp->allocateBuffers(reqWidth, reqHeight, |
| 642 | reqFormat, reqUsage); |
| 643 | |
| 644 | }).detach(); |
| 645 | } |
Robert Carr | 9c006e0 | 2020-10-14 13:41:57 -0700 | [diff] [blame] | 646 | |
Marin Shalamanov | c598677 | 2021-03-16 16:09:49 +0100 | [diff] [blame] | 647 | status_t setFrameRate(float frameRate, int8_t compatibility, |
| 648 | int8_t changeFrameRateStrategy) override { |
Vishnu Nair | c0fa007 | 2021-08-30 15:31:08 -0700 | [diff] [blame] | 649 | std::unique_lock _lock{mMutex}; |
| 650 | if (mDestroyed) { |
| 651 | return DEAD_OBJECT; |
| 652 | } |
Marin Shalamanov | c598677 | 2021-03-16 16:09:49 +0100 | [diff] [blame] | 653 | if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy, |
| 654 | "BBQSurface::setFrameRate")) { |
Robert Carr | 9c006e0 | 2020-10-14 13:41:57 -0700 | [diff] [blame] | 655 | return BAD_VALUE; |
| 656 | } |
Marin Shalamanov | c598677 | 2021-03-16 16:09:49 +0100 | [diff] [blame] | 657 | return mBbq->setFrameRate(frameRate, compatibility, changeFrameRateStrategy); |
Robert Carr | 9c006e0 | 2020-10-14 13:41:57 -0700 | [diff] [blame] | 658 | } |
Robert Carr | 9b611b7 | 2020-10-19 12:00:23 -0700 | [diff] [blame] | 659 | |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 660 | status_t setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) override { |
Vishnu Nair | c0fa007 | 2021-08-30 15:31:08 -0700 | [diff] [blame] | 661 | std::unique_lock _lock{mMutex}; |
| 662 | if (mDestroyed) { |
| 663 | return DEAD_OBJECT; |
| 664 | } |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 665 | return mBbq->setFrameTimelineInfo(frameTimelineInfo); |
Robert Carr | 9b611b7 | 2020-10-19 12:00:23 -0700 | [diff] [blame] | 666 | } |
Vishnu Nair | c0fa007 | 2021-08-30 15:31:08 -0700 | [diff] [blame] | 667 | |
| 668 | void destroy() override { |
| 669 | Surface::destroy(); |
| 670 | |
| 671 | std::unique_lock _lock{mMutex}; |
| 672 | mDestroyed = true; |
| 673 | mBbq = nullptr; |
| 674 | } |
Robert Carr | 05086b2 | 2020-10-13 18:22:51 -0700 | [diff] [blame] | 675 | }; |
| 676 | |
Robert Carr | 9c006e0 | 2020-10-14 13:41:57 -0700 | [diff] [blame] | 677 | // TODO: Can we coalesce this with frame updates? Need to confirm |
| 678 | // no timing issues. |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 679 | status_t BLASTBufferQueue::setFrameRate(float frameRate, int8_t compatibility, |
| 680 | bool shouldBeSeamless) { |
Robert Carr | 9c006e0 | 2020-10-14 13:41:57 -0700 | [diff] [blame] | 681 | std::unique_lock _lock{mMutex}; |
| 682 | SurfaceComposerClient::Transaction t; |
| 683 | |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 684 | return t.setFrameRate(mSurfaceControl, frameRate, compatibility, shouldBeSeamless).apply(); |
Robert Carr | 9c006e0 | 2020-10-14 13:41:57 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 687 | status_t BLASTBufferQueue::setFrameTimelineInfo(const FrameTimelineInfo& frameTimelineInfo) { |
Robert Carr | 9b611b7 | 2020-10-19 12:00:23 -0700 | [diff] [blame] | 688 | std::unique_lock _lock{mMutex}; |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 689 | mNextFrameTimelineInfoQueue.push(frameTimelineInfo); |
Jorim Jaggi | a3fe67b | 2020-12-01 00:24:33 +0100 | [diff] [blame] | 690 | return OK; |
Robert Carr | 9b611b7 | 2020-10-19 12:00:23 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Hongguang Chen | 621ec58 | 2021-02-16 15:42:35 -0800 | [diff] [blame] | 693 | void BLASTBufferQueue::setSidebandStream(const sp<NativeHandle>& stream) { |
| 694 | std::unique_lock _lock{mMutex}; |
| 695 | SurfaceComposerClient::Transaction t; |
| 696 | |
| 697 | t.setSidebandStream(mSurfaceControl, stream).apply(); |
| 698 | } |
| 699 | |
Vishnu Nair | 992496b | 2020-10-22 17:27:21 -0700 | [diff] [blame] | 700 | sp<Surface> BLASTBufferQueue::getSurface(bool includeSurfaceControlHandle) { |
| 701 | std::unique_lock _lock{mMutex}; |
| 702 | sp<IBinder> scHandle = nullptr; |
| 703 | if (includeSurfaceControlHandle && mSurfaceControl) { |
| 704 | scHandle = mSurfaceControl->getHandle(); |
| 705 | } |
| 706 | return new BBQSurface(mProducer, true, scHandle, this); |
Robert Carr | 05086b2 | 2020-10-13 18:22:51 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Vishnu Nair | c4a40c1 | 2020-12-23 09:14:32 -0800 | [diff] [blame] | 709 | void BLASTBufferQueue::mergeWithNextTransaction(SurfaceComposerClient::Transaction* t, |
| 710 | uint64_t frameNumber) { |
| 711 | std::lock_guard _lock{mMutex}; |
| 712 | if (mLastAcquiredFrameNumber >= frameNumber) { |
| 713 | // Apply the transaction since we have already acquired the desired frame. |
| 714 | t->apply(); |
| 715 | } else { |
chaviw | aad6cf5 | 2021-03-23 17:27:20 -0500 | [diff] [blame] | 716 | mPendingTransactions.emplace_back(frameNumber, *t); |
| 717 | // Clear the transaction so it can't be applied elsewhere. |
| 718 | t->clear(); |
Vishnu Nair | c4a40c1 | 2020-12-23 09:14:32 -0800 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
Vishnu Nair | 8949612 | 2020-12-14 17:14:53 -0800 | [diff] [blame] | 722 | // Maintains a single worker thread per process that services a list of runnables. |
| 723 | class AsyncWorker : public Singleton<AsyncWorker> { |
| 724 | private: |
| 725 | std::thread mThread; |
| 726 | bool mDone = false; |
| 727 | std::deque<std::function<void()>> mRunnables; |
| 728 | std::mutex mMutex; |
| 729 | std::condition_variable mCv; |
| 730 | void run() { |
| 731 | std::unique_lock<std::mutex> lock(mMutex); |
| 732 | while (!mDone) { |
Vishnu Nair | 8949612 | 2020-12-14 17:14:53 -0800 | [diff] [blame] | 733 | while (!mRunnables.empty()) { |
| 734 | std::function<void()> runnable = mRunnables.front(); |
| 735 | mRunnables.pop_front(); |
| 736 | runnable(); |
| 737 | } |
Wonsik Kim | 567533e | 2021-05-04 19:31:29 -0700 | [diff] [blame] | 738 | mCv.wait(lock); |
Vishnu Nair | 8949612 | 2020-12-14 17:14:53 -0800 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
| 742 | public: |
| 743 | AsyncWorker() : Singleton<AsyncWorker>() { mThread = std::thread(&AsyncWorker::run, this); } |
| 744 | |
| 745 | ~AsyncWorker() { |
| 746 | mDone = true; |
| 747 | mCv.notify_all(); |
| 748 | if (mThread.joinable()) { |
| 749 | mThread.join(); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | void post(std::function<void()> runnable) { |
| 754 | std::unique_lock<std::mutex> lock(mMutex); |
| 755 | mRunnables.emplace_back(std::move(runnable)); |
| 756 | mCv.notify_one(); |
| 757 | } |
| 758 | }; |
| 759 | ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker); |
| 760 | |
| 761 | // Asynchronously calls ProducerListener functions so we can emulate one way binder calls. |
| 762 | class AsyncProducerListener : public BnProducerListener { |
| 763 | private: |
| 764 | const sp<IProducerListener> mListener; |
| 765 | |
| 766 | public: |
| 767 | AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {} |
| 768 | |
| 769 | void onBufferReleased() override { |
| 770 | AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); }); |
| 771 | } |
| 772 | |
| 773 | void onBuffersDiscarded(const std::vector<int32_t>& slots) override { |
| 774 | AsyncWorker::getInstance().post( |
| 775 | [listener = mListener, slots = slots]() { listener->onBuffersDiscarded(slots); }); |
| 776 | } |
| 777 | }; |
| 778 | |
| 779 | // Extends the BufferQueueProducer to create a wrapper around the listener so the listener calls |
| 780 | // can be non-blocking when the producer is in the client process. |
| 781 | class BBQBufferQueueProducer : public BufferQueueProducer { |
| 782 | public: |
| 783 | BBQBufferQueueProducer(const sp<BufferQueueCore>& core) |
| 784 | : BufferQueueProducer(core, false /* consumerIsSurfaceFlinger*/) {} |
| 785 | |
| 786 | status_t connect(const sp<IProducerListener>& listener, int api, bool producerControlledByApp, |
| 787 | QueueBufferOutput* output) override { |
| 788 | if (!listener) { |
| 789 | return BufferQueueProducer::connect(listener, api, producerControlledByApp, output); |
| 790 | } |
| 791 | |
| 792 | return BufferQueueProducer::connect(new AsyncProducerListener(listener), api, |
| 793 | producerControlledByApp, output); |
| 794 | } |
Vishnu Nair | 17dde61 | 2020-12-28 11:39:59 -0800 | [diff] [blame] | 795 | |
| 796 | int query(int what, int* value) override { |
| 797 | if (what == NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER) { |
| 798 | *value = 1; |
| 799 | return NO_ERROR; |
| 800 | } |
| 801 | return BufferQueueProducer::query(what, value); |
| 802 | } |
Vishnu Nair | 8949612 | 2020-12-14 17:14:53 -0800 | [diff] [blame] | 803 | }; |
| 804 | |
| 805 | // Similar to BufferQueue::createBufferQueue but creates an adapter specific bufferqueue producer. |
| 806 | // This BQP allows invoking client specified ProducerListeners and invoke them asynchronously, |
| 807 | // emulating one way binder call behavior. Without this, if the listener calls back into the queue, |
| 808 | // we can deadlock. |
| 809 | void BLASTBufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer, |
| 810 | sp<IGraphicBufferConsumer>* outConsumer) { |
| 811 | LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BLASTBufferQueue: outProducer must not be NULL"); |
| 812 | LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BLASTBufferQueue: outConsumer must not be NULL"); |
| 813 | |
| 814 | sp<BufferQueueCore> core(new BufferQueueCore()); |
| 815 | LOG_ALWAYS_FATAL_IF(core == nullptr, "BLASTBufferQueue: failed to create BufferQueueCore"); |
| 816 | |
| 817 | sp<IGraphicBufferProducer> producer(new BBQBufferQueueProducer(core)); |
| 818 | LOG_ALWAYS_FATAL_IF(producer == nullptr, |
| 819 | "BLASTBufferQueue: failed to create BBQBufferQueueProducer"); |
| 820 | |
Vishnu Nair | 8b30dd1 | 2021-01-25 14:16:54 -0800 | [diff] [blame] | 821 | sp<BufferQueueConsumer> consumer(new BufferQueueConsumer(core)); |
| 822 | consumer->setAllowExtraAcquire(true); |
Vishnu Nair | 8949612 | 2020-12-14 17:14:53 -0800 | [diff] [blame] | 823 | LOG_ALWAYS_FATAL_IF(consumer == nullptr, |
| 824 | "BLASTBufferQueue: failed to create BufferQueueConsumer"); |
| 825 | |
| 826 | *outProducer = producer; |
| 827 | *outConsumer = consumer; |
| 828 | } |
| 829 | |
chaviw | 497e81c | 2021-02-04 17:09:47 -0800 | [diff] [blame] | 830 | PixelFormat BLASTBufferQueue::convertBufferFormat(PixelFormat& format) { |
| 831 | PixelFormat convertedFormat = format; |
| 832 | switch (format) { |
| 833 | case PIXEL_FORMAT_TRANSPARENT: |
| 834 | case PIXEL_FORMAT_TRANSLUCENT: |
| 835 | convertedFormat = PIXEL_FORMAT_RGBA_8888; |
| 836 | break; |
| 837 | case PIXEL_FORMAT_OPAQUE: |
| 838 | convertedFormat = PIXEL_FORMAT_RGBX_8888; |
| 839 | break; |
| 840 | } |
| 841 | return convertedFormat; |
| 842 | } |
| 843 | |
Robert Carr | 82d07c9 | 2021-05-10 11:36:43 -0700 | [diff] [blame] | 844 | uint32_t BLASTBufferQueue::getLastTransformHint() const { |
| 845 | if (mSurfaceControl != nullptr) { |
| 846 | return mSurfaceControl->getTransformHint(); |
| 847 | } else { |
| 848 | return 0; |
| 849 | } |
| 850 | } |
| 851 | |
Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 852 | } // namespace android |