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