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