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