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