Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
| 18 | |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 19 | #define LOG_TAG "BufferQueueProducer" |
| 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | //#define LOG_NDEBUG 0 |
| 22 | |
Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 23 | #if DEBUG_ONLY_CODE |
| 24 | #define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0) |
| 25 | #else |
| 26 | #define VALIDATE_CONSISTENCY() |
| 27 | #endif |
| 28 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 29 | #define EGL_EGLEXT_PROTOTYPES |
| 30 | |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 31 | #include <binder/IPCThreadState.h> |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 32 | #include <gui/BufferItem.h> |
| 33 | #include <gui/BufferQueueCore.h> |
| 34 | #include <gui/BufferQueueProducer.h> |
Ady Abraham | 107788e | 2023-10-17 12:31:08 -0700 | [diff] [blame] | 35 | |
Ady Abraham | 6cdd3fd | 2023-09-07 18:45:58 -0700 | [diff] [blame] | 36 | #include <gui/FrameRateUtils.h> |
John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 37 | #include <gui/GLConsumer.h> |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 38 | #include <gui/IConsumerListener.h> |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 39 | #include <gui/IProducerListener.h> |
Vishnu Nair | 14a3c11 | 2023-04-21 14:49:47 -0700 | [diff] [blame] | 40 | #include <gui/TraceUtils.h> |
Jayant Chowdhary | ad9fe27 | 2019-03-07 22:36:06 -0800 | [diff] [blame] | 41 | #include <private/gui/BufferQueueThreadState.h> |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 42 | |
| 43 | #include <utils/Log.h> |
| 44 | #include <utils/Trace.h> |
| 45 | |
Mathias Agopian | 6a3c05b | 2017-04-27 20:06:55 -0700 | [diff] [blame] | 46 | #include <system/window.h> |
| 47 | |
Mathias Agopian | b96661f | 2024-09-17 11:45:38 -0700 | [diff] [blame] | 48 | #include <com_android_graphics_libgui_flags.h> |
| 49 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 50 | namespace android { |
Mathias Agopian | b96661f | 2024-09-17 11:45:38 -0700 | [diff] [blame] | 51 | using namespace com::android::graphics::libgui; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 52 | |
Iris Chang | 430193f | 2019-12-04 16:25:46 +0800 | [diff] [blame] | 53 | // Macros for include BufferQueueCore information in log messages |
| 54 | #define BQ_LOGV(x, ...) \ |
Tomasz Wasilczyk | f2add40 | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 55 | ALOGV("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \ |
Iris Chang | 430193f | 2019-12-04 16:25:46 +0800 | [diff] [blame] | 56 | mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \ |
| 57 | ##__VA_ARGS__) |
| 58 | #define BQ_LOGD(x, ...) \ |
Tomasz Wasilczyk | f2add40 | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 59 | ALOGD("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \ |
Iris Chang | 430193f | 2019-12-04 16:25:46 +0800 | [diff] [blame] | 60 | mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \ |
| 61 | ##__VA_ARGS__) |
| 62 | #define BQ_LOGI(x, ...) \ |
Tomasz Wasilczyk | f2add40 | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 63 | ALOGI("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \ |
Iris Chang | 430193f | 2019-12-04 16:25:46 +0800 | [diff] [blame] | 64 | mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \ |
| 65 | ##__VA_ARGS__) |
| 66 | #define BQ_LOGW(x, ...) \ |
Tomasz Wasilczyk | f2add40 | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 67 | ALOGW("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \ |
Iris Chang | 430193f | 2019-12-04 16:25:46 +0800 | [diff] [blame] | 68 | mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \ |
| 69 | ##__VA_ARGS__) |
| 70 | #define BQ_LOGE(x, ...) \ |
Tomasz Wasilczyk | f2add40 | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 71 | ALOGE("[%s](id:%" PRIx64 ",api:%d,p:%d,c:%" PRIu64 ") " x, mConsumerName.c_str(), \ |
Iris Chang | 430193f | 2019-12-04 16:25:46 +0800 | [diff] [blame] | 72 | mCore->mUniqueId, mCore->mConnectedApi, mCore->mConnectedPid, (mCore->mUniqueId) >> 32, \ |
| 73 | ##__VA_ARGS__) |
| 74 | |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 75 | static constexpr uint32_t BQ_LAYER_COUNT = 1; |
Chong Zhang | 6249309 | 2020-01-15 16:04:47 -0800 | [diff] [blame] | 76 | ProducerListener::~ProducerListener() = default; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 77 | |
Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 78 | BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core, |
| 79 | bool consumerIsSurfaceFlinger) : |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 80 | mCore(core), |
| 81 | mSlots(core->mSlots), |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 82 | mConsumerName(), |
Eric Penner | 99a0afb | 2014-09-30 11:28:30 -0700 | [diff] [blame] | 83 | mStickyTransform(0), |
Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 84 | mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger), |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 85 | mLastQueueBufferFence(Fence::NO_FENCE), |
Pablo Ceballos | bd3577e | 2016-06-20 17:40:34 -0700 | [diff] [blame] | 86 | mLastQueuedTransform(0), |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 87 | mCallbackMutex(), |
| 88 | mNextCallbackTicket(0), |
| 89 | mCurrentCallbackTicket(0), |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 90 | mCallbackCondition(), |
Jorim Jaggi | 35b4e38 | 2019-03-28 00:44:03 +0100 | [diff] [blame] | 91 | mDequeueTimeout(-1), |
| 92 | mDequeueWaitingForAllocation(false) {} |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 93 | |
| 94 | BufferQueueProducer::~BufferQueueProducer() {} |
| 95 | |
| 96 | status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) { |
| 97 | ATRACE_CALL(); |
| 98 | BQ_LOGV("requestBuffer: slot %d", slot); |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 99 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 100 | |
| 101 | if (mCore->mIsAbandoned) { |
| 102 | BQ_LOGE("requestBuffer: BufferQueue has been abandoned"); |
| 103 | return NO_INIT; |
| 104 | } |
| 105 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 106 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 107 | BQ_LOGE("requestBuffer: BufferQueue has no connected producer"); |
| 108 | return NO_INIT; |
| 109 | } |
| 110 | |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 111 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 112 | BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)", |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 113 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 114 | return BAD_VALUE; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 115 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 116 | BQ_LOGE("requestBuffer: slot %d is not owned by the producer " |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 117 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 118 | return BAD_VALUE; |
| 119 | } |
| 120 | |
| 121 | mSlots[slot].mRequestBufferCalled = true; |
| 122 | *buf = mSlots[slot].mGraphicBuffer; |
| 123 | return NO_ERROR; |
| 124 | } |
| 125 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 126 | status_t BufferQueueProducer::setMaxDequeuedBufferCount( |
| 127 | int maxDequeuedBuffers) { |
Brian Lindahl | c794b69 | 2023-01-31 15:42:47 -0700 | [diff] [blame] | 128 | int maxBufferCount; |
| 129 | return setMaxDequeuedBufferCount(maxDequeuedBuffers, &maxBufferCount); |
| 130 | } |
| 131 | |
| 132 | status_t BufferQueueProducer::setMaxDequeuedBufferCount(int maxDequeuedBuffers, |
| 133 | int* maxBufferCount) { |
Vishnu Nair | 14a3c11 | 2023-04-21 14:49:47 -0700 | [diff] [blame] | 134 | ATRACE_FORMAT("%s(%d)", __func__, maxDequeuedBuffers); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 135 | BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d", |
| 136 | maxDequeuedBuffers); |
| 137 | |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 138 | sp<IConsumerListener> listener; |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 139 | { // Autolock scope |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 140 | std::unique_lock<std::mutex> lock(mCore->mMutex); |
| 141 | mCore->waitWhileAllocatingLocked(lock); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 142 | |
| 143 | if (mCore->mIsAbandoned) { |
| 144 | BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been " |
| 145 | "abandoned"); |
| 146 | return NO_INIT; |
| 147 | } |
| 148 | |
Brian Lindahl | c794b69 | 2023-01-31 15:42:47 -0700 | [diff] [blame] | 149 | *maxBufferCount = mCore->getMaxBufferCountLocked(); |
| 150 | |
Pablo Ceballos | 245cc5b | 2016-04-19 11:33:00 -0700 | [diff] [blame] | 151 | if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) { |
| 152 | return NO_ERROR; |
| 153 | } |
| 154 | |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 155 | // The new maxDequeuedBuffer count should not be violated by the number |
| 156 | // of currently dequeued buffers |
| 157 | int dequeuedCount = 0; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 158 | for (int s : mCore->mActiveBuffers) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 159 | if (mSlots[s].mBufferState.isDequeued()) { |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 160 | dequeuedCount++; |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 163 | if (dequeuedCount > maxDequeuedBuffers) { |
| 164 | BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer" |
| 165 | "count (%d) exceeds the current dequeued buffer count (%d)", |
| 166 | maxDequeuedBuffers, dequeuedCount); |
| 167 | return BAD_VALUE; |
| 168 | } |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 169 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 170 | int bufferCount = mCore->getMinUndequeuedBufferCountLocked(); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 171 | bufferCount += maxDequeuedBuffers; |
| 172 | |
| 173 | if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) { |
| 174 | BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large " |
| 175 | "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS); |
| 176 | return BAD_VALUE; |
| 177 | } |
| 178 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 179 | const int minBufferSlots = mCore->getMinMaxBufferCountLocked(); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 180 | if (bufferCount < minBufferSlots) { |
| 181 | BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is " |
| 182 | "less than minimum %d", bufferCount, minBufferSlots); |
| 183 | return BAD_VALUE; |
| 184 | } |
| 185 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 186 | if (bufferCount > mCore->mMaxBufferCount) { |
| 187 | BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would " |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 188 | "exceed the maxBufferCount (%d) (maxAcquired %d async %d " |
| 189 | "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers, |
| 190 | mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount, |
| 191 | mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock); |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 192 | return BAD_VALUE; |
| 193 | } |
| 194 | |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 195 | int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount; |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 196 | if (!mCore->adjustAvailableSlotsLocked(delta)) { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 197 | return BAD_VALUE; |
| 198 | } |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 199 | mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers; |
Brian Lindahl | c794b69 | 2023-01-31 15:42:47 -0700 | [diff] [blame] | 200 | *maxBufferCount = mCore->getMaxBufferCountLocked(); |
Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 201 | VALIDATE_CONSISTENCY(); |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 202 | if (delta < 0) { |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 203 | listener = mCore->mConsumerListener; |
Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 204 | } |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 205 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL) |
| 206 | mCore->notifyBufferReleased(); |
| 207 | #else |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 208 | mCore->mDequeueCondition.notify_all(); |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 209 | #endif |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 210 | } // Autolock scope |
| 211 | |
| 212 | // Call back without lock held |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 213 | if (listener != nullptr) { |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 214 | listener->onBuffersReleased(); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return NO_ERROR; |
| 218 | } |
| 219 | |
| 220 | status_t BufferQueueProducer::setAsyncMode(bool async) { |
| 221 | ATRACE_CALL(); |
| 222 | BQ_LOGV("setAsyncMode: async = %d", async); |
| 223 | |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 224 | sp<IConsumerListener> listener; |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 225 | { // Autolock scope |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 226 | std::unique_lock<std::mutex> lock(mCore->mMutex); |
| 227 | mCore->waitWhileAllocatingLocked(lock); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 228 | |
| 229 | if (mCore->mIsAbandoned) { |
| 230 | BQ_LOGE("setAsyncMode: BufferQueue has been abandoned"); |
| 231 | return NO_INIT; |
| 232 | } |
| 233 | |
Pablo Ceballos | 245cc5b | 2016-04-19 11:33:00 -0700 | [diff] [blame] | 234 | if (async == mCore->mAsyncMode) { |
| 235 | return NO_ERROR; |
| 236 | } |
| 237 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 238 | if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount + |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 239 | (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) > |
| 240 | mCore->mMaxBufferCount) { |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 241 | BQ_LOGE("setAsyncMode(%d): this call would cause the " |
| 242 | "maxBufferCount (%d) to be exceeded (maxAcquired %d " |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 243 | "maxDequeued %d mDequeueBufferCannotBlock %d)", async, |
| 244 | mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount, |
| 245 | mCore->mMaxDequeuedBufferCount, |
| 246 | mCore->mDequeueBufferCannotBlock); |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 247 | return BAD_VALUE; |
| 248 | } |
| 249 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 250 | int delta = mCore->getMaxBufferCountLocked(async, |
| 251 | mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount) |
| 252 | - mCore->getMaxBufferCountLocked(); |
| 253 | |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 254 | if (!mCore->adjustAvailableSlotsLocked(delta)) { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 255 | BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of " |
| 256 | "available slots. Delta = %d", delta); |
| 257 | return BAD_VALUE; |
| 258 | } |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 259 | mCore->mAsyncMode = async; |
Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 260 | VALIDATE_CONSISTENCY(); |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 261 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL) |
| 262 | mCore->notifyBufferReleased(); |
| 263 | #else |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 264 | mCore->mDequeueCondition.notify_all(); |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 265 | #endif |
| 266 | |
Pablo Ceballos | 245cc5b | 2016-04-19 11:33:00 -0700 | [diff] [blame] | 267 | if (delta < 0) { |
| 268 | listener = mCore->mConsumerListener; |
| 269 | } |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 270 | } // Autolock scope |
| 271 | |
| 272 | // Call back without lock held |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 273 | if (listener != nullptr) { |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 274 | listener->onBuffersReleased(); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 275 | } |
| 276 | return NO_ERROR; |
| 277 | } |
| 278 | |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 279 | int BufferQueueProducer::getFreeBufferLocked() const { |
| 280 | if (mCore->mFreeBuffers.empty()) { |
| 281 | return BufferQueueCore::INVALID_BUFFER_SLOT; |
| 282 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 283 | int slot = mCore->mFreeBuffers.front(); |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 284 | mCore->mFreeBuffers.pop_front(); |
| 285 | return slot; |
| 286 | } |
| 287 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 288 | int BufferQueueProducer::getFreeSlotLocked() const { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 289 | if (mCore->mFreeSlots.empty()) { |
| 290 | return BufferQueueCore::INVALID_BUFFER_SLOT; |
| 291 | } |
Pablo Ceballos | dce5c55 | 2016-02-10 15:43:22 -0800 | [diff] [blame] | 292 | int slot = *(mCore->mFreeSlots.begin()); |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 293 | mCore->mFreeSlots.erase(slot); |
Pablo Ceballos | dce5c55 | 2016-02-10 15:43:22 -0800 | [diff] [blame] | 294 | return slot; |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller, |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 298 | std::unique_lock<std::mutex>& lock, int* found) const { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 299 | auto callerString = (caller == FreeSlotCaller::Dequeue) ? |
| 300 | "dequeueBuffer" : "attachBuffer"; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 301 | bool tryAgain = true; |
| 302 | while (tryAgain) { |
| 303 | if (mCore->mIsAbandoned) { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 304 | BQ_LOGE("%s: BufferQueue has been abandoned", callerString); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 305 | return NO_INIT; |
| 306 | } |
| 307 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 308 | int dequeuedCount = 0; |
| 309 | int acquiredCount = 0; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 310 | for (int s : mCore->mActiveBuffers) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 311 | if (mSlots[s].mBufferState.isDequeued()) { |
| 312 | ++dequeuedCount; |
| 313 | } |
| 314 | if (mSlots[s].mBufferState.isAcquired()) { |
| 315 | ++acquiredCount; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 319 | // Producers are not allowed to dequeue more than |
| 320 | // mMaxDequeuedBufferCount buffers. |
| 321 | // This check is only done if a buffer has already been queued |
| 322 | if (mCore->mBufferHasBeenQueued && |
| 323 | dequeuedCount >= mCore->mMaxDequeuedBufferCount) { |
Sungtak Lee | af14124 | 2019-04-24 16:36:44 -0700 | [diff] [blame] | 324 | // Supress error logs when timeout is non-negative. |
| 325 | if (mDequeueTimeout < 0) { |
| 326 | BQ_LOGE("%s: attempting to exceed the max dequeued buffer " |
| 327 | "count (%d)", callerString, |
| 328 | mCore->mMaxDequeuedBufferCount); |
| 329 | } |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 330 | return INVALID_OPERATION; |
| 331 | } |
| 332 | |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 333 | *found = BufferQueueCore::INVALID_BUFFER_SLOT; |
| 334 | |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 335 | // If we disconnect and reconnect quickly, we can be in a state where |
| 336 | // our slots are empty but we have many buffers in the queue. This can |
| 337 | // cause us to run out of memory if we outrun the consumer. Wait here if |
| 338 | // it looks like we have too many buffers queued up. |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 339 | const int maxBufferCount = mCore->getMaxBufferCountLocked(); |
Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 340 | bool tooManyBuffers = mCore->mQueue.size() |
| 341 | > static_cast<size_t>(maxBufferCount); |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 342 | if (tooManyBuffers) { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 343 | BQ_LOGV("%s: queue size is %zu, waiting", callerString, |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 344 | mCore->mQueue.size()); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 345 | } else { |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 346 | // If in shared buffer mode and a shared buffer exists, always |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 347 | // return it. |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 348 | if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot != |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 349 | BufferQueueCore::INVALID_BUFFER_SLOT) { |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 350 | *found = mCore->mSharedBufferSlot; |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 351 | } else { |
| 352 | if (caller == FreeSlotCaller::Dequeue) { |
| 353 | // If we're calling this from dequeue, prefer free buffers |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 354 | int slot = getFreeBufferLocked(); |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 355 | if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 356 | *found = slot; |
| 357 | } else if (mCore->mAllowAllocation) { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 358 | *found = getFreeSlotLocked(); |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 359 | } |
| 360 | } else { |
| 361 | // If we're calling this from attach, prefer free slots |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 362 | int slot = getFreeSlotLocked(); |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 363 | if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 364 | *found = slot; |
| 365 | } else { |
| 366 | *found = getFreeBufferLocked(); |
| 367 | } |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // If no buffer is found, or if the queue has too many buffers |
| 373 | // outstanding, wait for a buffer to be acquired or released, or for the |
| 374 | // max buffer count to change. |
| 375 | tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) || |
| 376 | tooManyBuffers; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 377 | if (tryAgain) { |
| 378 | // Return an error if we're in non-blocking mode (producer and |
| 379 | // consumer are controlled by the application). |
| 380 | // However, the consumer is allowed to briefly acquire an extra |
| 381 | // buffer (which could cause us to have to wait here), which is |
| 382 | // okay, since it is only used to implement an atomic acquire + |
| 383 | // release (e.g., in GLConsumer::updateTexImage()) |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 384 | if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) && |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 385 | (acquiredCount <= mCore->mMaxAcquiredBufferCount)) { |
| 386 | return WOULD_BLOCK; |
| 387 | } |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 388 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL) |
| 389 | if (status_t status = waitForBufferRelease(lock, mDequeueTimeout); |
| 390 | status == TIMED_OUT) { |
| 391 | return TIMED_OUT; |
| 392 | } |
| 393 | #else |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 394 | if (mDequeueTimeout >= 0) { |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 395 | std::cv_status result = mCore->mDequeueCondition.wait_for(lock, |
| 396 | std::chrono::nanoseconds(mDequeueTimeout)); |
| 397 | if (result == std::cv_status::timeout) { |
| 398 | return TIMED_OUT; |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 399 | } |
| 400 | } else { |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 401 | mCore->mDequeueCondition.wait(lock); |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 402 | } |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 403 | #endif |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 404 | } |
| 405 | } // while (tryAgain) |
| 406 | |
| 407 | return NO_ERROR; |
| 408 | } |
| 409 | |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 410 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL) |
| 411 | status_t BufferQueueProducer::waitForBufferRelease(std::unique_lock<std::mutex>& lock, |
| 412 | nsecs_t timeout) const { |
| 413 | if (mDequeueTimeout >= 0) { |
| 414 | std::cv_status result = |
| 415 | mCore->mDequeueCondition.wait_for(lock, std::chrono::nanoseconds(timeout)); |
| 416 | if (result == std::cv_status::timeout) { |
| 417 | return TIMED_OUT; |
| 418 | } |
| 419 | } else { |
| 420 | mCore->mDequeueCondition.wait(lock); |
| 421 | } |
| 422 | return OK; |
| 423 | } |
| 424 | #endif |
| 425 | |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 426 | status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence, |
| 427 | uint32_t width, uint32_t height, PixelFormat format, |
| 428 | uint64_t usage, uint64_t* outBufferAge, |
| 429 | FrameEventHistoryDelta* outTimestamps) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 430 | ATRACE_CALL(); |
| 431 | { // Autolock scope |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 432 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 433 | mConsumerName = mCore->mConsumerName; |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 434 | |
| 435 | if (mCore->mIsAbandoned) { |
| 436 | BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned"); |
| 437 | return NO_INIT; |
| 438 | } |
| 439 | |
| 440 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 441 | BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer"); |
| 442 | return NO_INIT; |
| 443 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 444 | } // Autolock scope |
| 445 | |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 446 | BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 447 | |
| 448 | if ((width && !height) || (!width && height)) { |
| 449 | BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height); |
| 450 | return BAD_VALUE; |
| 451 | } |
| 452 | |
| 453 | status_t returnFlags = NO_ERROR; |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 454 | #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP) |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 455 | EGLDisplay eglDisplay = EGL_NO_DISPLAY; |
| 456 | EGLSyncKHR eglFence = EGL_NO_SYNC_KHR; |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 457 | #endif |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 458 | bool attachedByConsumer = false; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 459 | |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 460 | sp<IConsumerListener> listener; |
| 461 | bool callOnFrameDequeued = false; |
| 462 | uint64_t bufferId = 0; // Only used if callOnFrameDequeued == true |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 463 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 464 | std::vector<gui::AdditionalOptions> allocOptions; |
| 465 | uint32_t allocOptionsGenId = 0; |
| 466 | #endif |
| 467 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 468 | { // Autolock scope |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 469 | std::unique_lock<std::mutex> lock(mCore->mMutex); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 470 | |
Jorim Jaggi | 35b4e38 | 2019-03-28 00:44:03 +0100 | [diff] [blame] | 471 | // If we don't have a free buffer, but we are currently allocating, we wait until allocation |
| 472 | // is finished such that we don't allocate in parallel. |
| 473 | if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) { |
| 474 | mDequeueWaitingForAllocation = true; |
| 475 | mCore->waitWhileAllocatingLocked(lock); |
| 476 | mDequeueWaitingForAllocation = false; |
| 477 | mDequeueWaitingForAllocationCondition.notify_all(); |
| 478 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 479 | |
| 480 | if (format == 0) { |
| 481 | format = mCore->mDefaultBufferFormat; |
| 482 | } |
| 483 | |
| 484 | // Enable the usage bits the consumer requested |
| 485 | usage |= mCore->mConsumerUsageBits; |
| 486 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 487 | const bool useDefaultSize = !width && !height; |
| 488 | if (useDefaultSize) { |
| 489 | width = mCore->mDefaultWidth; |
| 490 | height = mCore->mDefaultHeight; |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 491 | if (mCore->mAutoPrerotation && |
| 492 | (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) { |
| 493 | std::swap(width, height); |
| 494 | } |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 495 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 496 | |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 497 | int found = BufferItem::INVALID_BUFFER_SLOT; |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 498 | while (found == BufferItem::INVALID_BUFFER_SLOT) { |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 499 | status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found); |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 500 | if (status != NO_ERROR) { |
| 501 | return status; |
| 502 | } |
| 503 | |
| 504 | // This should not happen |
| 505 | if (found == BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 506 | BQ_LOGE("dequeueBuffer: no available buffer slots"); |
| 507 | return -EBUSY; |
| 508 | } |
| 509 | |
| 510 | const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer); |
| 511 | |
| 512 | // If we are not allowed to allocate new buffers, |
| 513 | // waitForFreeSlotThenRelock must have returned a slot containing a |
| 514 | // buffer. If this buffer would require reallocation to meet the |
| 515 | // requested attributes, we free it and attempt to get another one. |
| 516 | if (!mCore->mAllowAllocation) { |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 517 | if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) { |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 518 | if (mCore->mSharedBufferSlot == found) { |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 519 | BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer"); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 520 | return BAD_VALUE; |
| 521 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 522 | mCore->mFreeSlots.insert(found); |
| 523 | mCore->clearBufferSlotLocked(found); |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 524 | found = BufferItem::INVALID_BUFFER_SLOT; |
| 525 | continue; |
| 526 | } |
| 527 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 530 | const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer); |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 531 | |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 532 | bool needsReallocation = buffer == nullptr || |
| 533 | buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage); |
| 534 | |
| 535 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 536 | needsReallocation |= mSlots[found].mAdditionalOptionsGenerationId != |
| 537 | mCore->mAdditionalOptionsGenerationId; |
| 538 | #endif |
| 539 | |
| 540 | if (mCore->mSharedBufferSlot == found && needsReallocation) { |
| 541 | BQ_LOGE("dequeueBuffer: cannot re-allocate a shared buffer"); |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 542 | return BAD_VALUE; |
| 543 | } |
| 544 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 545 | if (mCore->mSharedBufferSlot != found) { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 546 | mCore->mActiveBuffers.insert(found); |
| 547 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 548 | *outSlot = found; |
| 549 | ATRACE_BUFFER_INDEX(found); |
| 550 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 551 | attachedByConsumer = mSlots[found].mNeedsReallocation; |
| 552 | mSlots[found].mNeedsReallocation = false; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 553 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 554 | mSlots[found].mBufferState.dequeue(); |
| 555 | |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 556 | if (needsReallocation) { |
Vishnu Nair | 14a3c11 | 2023-04-21 14:49:47 -0700 | [diff] [blame] | 557 | if (CC_UNLIKELY(ATRACE_ENABLED())) { |
| 558 | if (buffer == nullptr) { |
Tomasz Wasilczyk | 02fd95c | 2023-08-30 17:51:31 +0000 | [diff] [blame] | 559 | ATRACE_FORMAT_INSTANT("%s buffer reallocation: null", mConsumerName.c_str()); |
Vishnu Nair | 14a3c11 | 2023-04-21 14:49:47 -0700 | [diff] [blame] | 560 | } else { |
| 561 | ATRACE_FORMAT_INSTANT("%s buffer reallocation actual %dx%d format:%d " |
| 562 | "layerCount:%d " |
| 563 | "usage:%d requested: %dx%d format:%d layerCount:%d " |
| 564 | "usage:%d ", |
Tomasz Wasilczyk | 02fd95c | 2023-08-30 17:51:31 +0000 | [diff] [blame] | 565 | mConsumerName.c_str(), width, height, format, |
Vishnu Nair | 14a3c11 | 2023-04-21 14:49:47 -0700 | [diff] [blame] | 566 | BQ_LAYER_COUNT, usage, buffer->getWidth(), |
| 567 | buffer->getHeight(), buffer->getPixelFormat(), |
| 568 | buffer->getLayerCount(), buffer->getUsage()); |
| 569 | } |
| 570 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 571 | mSlots[found].mAcquireCalled = false; |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 572 | mSlots[found].mGraphicBuffer = nullptr; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 573 | mSlots[found].mRequestBufferCalled = false; |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 574 | #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP) |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 575 | mSlots[found].mEglDisplay = EGL_NO_DISPLAY; |
| 576 | mSlots[found].mEglFence = EGL_NO_SYNC_KHR; |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 577 | #endif |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 578 | mSlots[found].mFence = Fence::NO_FENCE; |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 579 | mCore->mBufferAge = 0; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 580 | mCore->mIsAllocating = true; |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 581 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 582 | allocOptions = mCore->mAdditionalOptions; |
| 583 | allocOptionsGenId = mCore->mAdditionalOptionsGenerationId; |
| 584 | #endif |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 585 | |
| 586 | returnFlags |= BUFFER_NEEDS_REALLOCATION; |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 587 | } else { |
| 588 | // We add 1 because that will be the frame number when this buffer |
| 589 | // is queued |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 590 | mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Dan Stoza | 800b41a | 2015-04-28 14:20:04 -0700 | [diff] [blame] | 593 | BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64, |
| 594 | mCore->mBufferAge); |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 595 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 596 | if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 597 | BQ_LOGE("dequeueBuffer: about to return a NULL fence - " |
| 598 | "slot=%d w=%d h=%d format=%u", |
| 599 | found, buffer->width, buffer->height, buffer->format); |
| 600 | } |
| 601 | |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 602 | #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP) |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 603 | eglDisplay = mSlots[found].mEglDisplay; |
| 604 | eglFence = mSlots[found].mEglFence; |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 605 | #endif |
Pablo Ceballos | 28c65ad | 2016-06-01 15:03:21 -0700 | [diff] [blame] | 606 | // Don't return a fence in shared buffer mode, except for the first |
| 607 | // frame. |
| 608 | *outFence = (mCore->mSharedBufferMode && |
| 609 | mCore->mSharedBufferSlot == found) ? |
| 610 | Fence::NO_FENCE : mSlots[found].mFence; |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 611 | #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP) |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 612 | mSlots[found].mEglFence = EGL_NO_SYNC_KHR; |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 613 | #endif |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 614 | mSlots[found].mFence = Fence::NO_FENCE; |
Pablo Ceballos | 28c65ad | 2016-06-01 15:03:21 -0700 | [diff] [blame] | 615 | |
| 616 | // If shared buffer mode has just been enabled, cache the slot of the |
| 617 | // first buffer that is dequeued and mark it as the shared buffer. |
| 618 | if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == |
| 619 | BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 620 | mCore->mSharedBufferSlot = found; |
| 621 | mSlots[found].mBufferState.mShared = true; |
| 622 | } |
Adithya Srinivasan | a820af9 | 2019-11-01 13:55:17 -0700 | [diff] [blame] | 623 | |
| 624 | if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) { |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 625 | callOnFrameDequeued = true; |
| 626 | bufferId = mSlots[*outSlot].mGraphicBuffer->getId(); |
Adithya Srinivasan | a820af9 | 2019-11-01 13:55:17 -0700 | [diff] [blame] | 627 | } |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 628 | |
| 629 | listener = mCore->mConsumerListener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 630 | } // Autolock scope |
| 631 | |
| 632 | if (returnFlags & BUFFER_NEEDS_REALLOCATION) { |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 633 | BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot); |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 634 | |
| 635 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 636 | std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions; |
| 637 | tempOptions.reserve(allocOptions.size()); |
| 638 | for (const auto& it : allocOptions) { |
| 639 | tempOptions.emplace_back(it.name.c_str(), it.value); |
| 640 | } |
| 641 | const GraphicBufferAllocator::AllocationRequest allocRequest = { |
| 642 | .importBuffer = true, |
| 643 | .width = width, |
| 644 | .height = height, |
| 645 | .format = format, |
| 646 | .layerCount = BQ_LAYER_COUNT, |
| 647 | .usage = usage, |
| 648 | .requestorName = {mConsumerName.c_str(), mConsumerName.size()}, |
| 649 | .extras = std::move(tempOptions), |
| 650 | }; |
| 651 | sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest); |
| 652 | #else |
Tomasz Wasilczyk | f2add40 | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 653 | sp<GraphicBuffer> graphicBuffer = |
| 654 | new GraphicBuffer(width, height, format, BQ_LAYER_COUNT, usage, |
| 655 | {mConsumerName.c_str(), mConsumerName.size()}); |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 656 | #endif |
Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 657 | |
| 658 | status_t error = graphicBuffer->initCheck(); |
| 659 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 660 | { // Autolock scope |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 661 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 662 | |
Chia-I Wu | feec3b1 | 2017-05-25 09:34:56 -0700 | [diff] [blame] | 663 | if (error == NO_ERROR && !mCore->mIsAbandoned) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 664 | graphicBuffer->setGenerationNumber(mCore->mGenerationNumber); |
| 665 | mSlots[*outSlot].mGraphicBuffer = graphicBuffer; |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 666 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 667 | mSlots[*outSlot].mAdditionalOptionsGenerationId = allocOptionsGenId; |
| 668 | #endif |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 669 | callOnFrameDequeued = true; |
| 670 | bufferId = mSlots[*outSlot].mGraphicBuffer->getId(); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | mCore->mIsAllocating = false; |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 674 | mCore->mIsAllocatingCondition.notify_all(); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 675 | |
Chia-I Wu | feec3b1 | 2017-05-25 09:34:56 -0700 | [diff] [blame] | 676 | if (error != NO_ERROR) { |
Pablo Ceballos | 0a06809 | 2016-06-29 15:08:33 -0700 | [diff] [blame] | 677 | mCore->mFreeSlots.insert(*outSlot); |
| 678 | mCore->clearBufferSlotLocked(*outSlot); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 679 | BQ_LOGE("dequeueBuffer: createGraphicBuffer failed"); |
| 680 | return error; |
| 681 | } |
| 682 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 683 | if (mCore->mIsAbandoned) { |
Pablo Ceballos | 0a06809 | 2016-06-29 15:08:33 -0700 | [diff] [blame] | 684 | mCore->mFreeSlots.insert(*outSlot); |
| 685 | mCore->clearBufferSlotLocked(*outSlot); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 686 | BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned"); |
| 687 | return NO_INIT; |
| 688 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 689 | |
Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 690 | VALIDATE_CONSISTENCY(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 691 | } // Autolock scope |
| 692 | } |
| 693 | |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 694 | if (listener != nullptr && callOnFrameDequeued) { |
| 695 | listener->onFrameDequeued(bufferId); |
| 696 | } |
| 697 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 698 | if (attachedByConsumer) { |
| 699 | returnFlags |= BUFFER_NEEDS_REALLOCATION; |
| 700 | } |
| 701 | |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 702 | #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP) |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 703 | if (eglFence != EGL_NO_SYNC_KHR) { |
| 704 | EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0, |
| 705 | 1000000000); |
| 706 | // If something goes wrong, log the error, but return the buffer without |
| 707 | // synchronizing access to it. It's too late at this point to abort the |
| 708 | // dequeue operation. |
| 709 | if (result == EGL_FALSE) { |
| 710 | BQ_LOGE("dequeueBuffer: error %#x waiting for fence", |
| 711 | eglGetError()); |
| 712 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 713 | BQ_LOGE("dequeueBuffer: timeout waiting for fence"); |
| 714 | } |
| 715 | eglDestroySyncKHR(eglDisplay, eglFence); |
| 716 | } |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 717 | #endif |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 718 | |
Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 719 | BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x", |
| 720 | *outSlot, |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 721 | mSlots[*outSlot].mFrameNumber, |
tangcheng | 5614ca0 | 2022-04-07 14:26:05 +0800 | [diff] [blame] | 722 | mSlots[*outSlot].mGraphicBuffer != nullptr ? |
| 723 | mSlots[*outSlot].mGraphicBuffer->handle : nullptr, returnFlags); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 724 | |
Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 725 | if (outBufferAge) { |
| 726 | *outBufferAge = mCore->mBufferAge; |
| 727 | } |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 728 | addAndGetFrameTimestamps(nullptr, outTimestamps); |
| 729 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 730 | return returnFlags; |
| 731 | } |
| 732 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 733 | status_t BufferQueueProducer::detachBuffer(int slot) { |
| 734 | ATRACE_CALL(); |
| 735 | ATRACE_BUFFER_INDEX(slot); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 736 | BQ_LOGV("detachBuffer: slot %d", slot); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 737 | |
Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 738 | sp<IConsumerListener> listener; |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 739 | bool callOnFrameDetached = false; |
| 740 | uint64_t bufferId = 0; // Only used if callOnFrameDetached is true |
Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 741 | { |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 742 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 743 | |
| 744 | if (mCore->mIsAbandoned) { |
| 745 | BQ_LOGE("detachBuffer: BufferQueue has been abandoned"); |
| 746 | return NO_INIT; |
| 747 | } |
| 748 | |
| 749 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 750 | BQ_LOGE("detachBuffer: BufferQueue has no connected producer"); |
| 751 | return NO_INIT; |
| 752 | } |
| 753 | |
| 754 | if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) { |
| 755 | BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode"); |
| 756 | return BAD_VALUE; |
| 757 | } |
| 758 | |
| 759 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
| 760 | BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)", |
| 761 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); |
| 762 | return BAD_VALUE; |
| 763 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
Darwin Huang | 29936fa | 2021-09-08 18:29:18 +0000 | [diff] [blame] | 764 | // TODO(http://b/140581935): This message is BQ_LOGW because it |
| 765 | // often logs when no actionable errors are present. Return to |
| 766 | // using BQ_LOGE after ensuring this only logs during errors. |
| 767 | BQ_LOGW("detachBuffer: slot %d is not owned by the producer " |
Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 768 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); |
| 769 | return BAD_VALUE; |
| 770 | } else if (!mSlots[slot].mRequestBufferCalled) { |
| 771 | BQ_LOGE("detachBuffer: buffer in slot %d has not been requested", |
| 772 | slot); |
| 773 | return BAD_VALUE; |
| 774 | } |
| 775 | |
Adithya Srinivasan | 2e43438 | 2019-10-09 11:43:01 -0700 | [diff] [blame] | 776 | listener = mCore->mConsumerListener; |
Robert Carr | fc069ba | 2020-04-20 13:26:31 -0700 | [diff] [blame] | 777 | auto gb = mSlots[slot].mGraphicBuffer; |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 778 | if (gb != nullptr) { |
| 779 | callOnFrameDetached = true; |
| 780 | bufferId = gb->getId(); |
Adithya Srinivasan | 2e43438 | 2019-10-09 11:43:01 -0700 | [diff] [blame] | 781 | } |
Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 782 | mSlots[slot].mBufferState.detachProducer(); |
| 783 | mCore->mActiveBuffers.erase(slot); |
| 784 | mCore->mFreeSlots.insert(slot); |
| 785 | mCore->clearBufferSlotLocked(slot); |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 786 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL) |
| 787 | mCore->notifyBufferReleased(); |
| 788 | #else |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 789 | mCore->mDequeueCondition.notify_all(); |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 790 | #endif |
Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 791 | VALIDATE_CONSISTENCY(); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 792 | } |
| 793 | |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 794 | if (listener != nullptr && callOnFrameDetached) { |
| 795 | listener->onFrameDetached(bufferId); |
| 796 | } |
| 797 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 798 | if (listener != nullptr) { |
Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 799 | listener->onBuffersReleased(); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 800 | } |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 801 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 802 | return NO_ERROR; |
| 803 | } |
| 804 | |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 805 | status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer, |
| 806 | sp<Fence>* outFence) { |
| 807 | ATRACE_CALL(); |
| 808 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 809 | if (outBuffer == nullptr) { |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 810 | BQ_LOGE("detachNextBuffer: outBuffer must not be NULL"); |
| 811 | return BAD_VALUE; |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 812 | } else if (outFence == nullptr) { |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 813 | BQ_LOGE("detachNextBuffer: outFence must not be NULL"); |
| 814 | return BAD_VALUE; |
| 815 | } |
| 816 | |
Eino-Ville Talvala | 2672dec | 2017-06-13 16:39:11 -0700 | [diff] [blame] | 817 | sp<IConsumerListener> listener; |
| 818 | { |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 819 | std::unique_lock<std::mutex> lock(mCore->mMutex); |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 820 | |
Eino-Ville Talvala | 2672dec | 2017-06-13 16:39:11 -0700 | [diff] [blame] | 821 | if (mCore->mIsAbandoned) { |
| 822 | BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned"); |
| 823 | return NO_INIT; |
| 824 | } |
| 825 | |
| 826 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 827 | BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer"); |
| 828 | return NO_INIT; |
| 829 | } |
| 830 | |
| 831 | if (mCore->mSharedBufferMode) { |
| 832 | BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer " |
| 833 | "mode"); |
| 834 | return BAD_VALUE; |
| 835 | } |
| 836 | |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 837 | mCore->waitWhileAllocatingLocked(lock); |
Eino-Ville Talvala | 2672dec | 2017-06-13 16:39:11 -0700 | [diff] [blame] | 838 | |
| 839 | if (mCore->mFreeBuffers.empty()) { |
| 840 | return NO_MEMORY; |
| 841 | } |
| 842 | |
| 843 | int found = mCore->mFreeBuffers.front(); |
| 844 | mCore->mFreeBuffers.remove(found); |
| 845 | mCore->mFreeSlots.insert(found); |
| 846 | |
| 847 | BQ_LOGV("detachNextBuffer detached slot %d", found); |
| 848 | |
| 849 | *outBuffer = mSlots[found].mGraphicBuffer; |
| 850 | *outFence = mSlots[found].mFence; |
| 851 | mCore->clearBufferSlotLocked(found); |
| 852 | VALIDATE_CONSISTENCY(); |
| 853 | listener = mCore->mConsumerListener; |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 856 | if (listener != nullptr) { |
Eino-Ville Talvala | 2672dec | 2017-06-13 16:39:11 -0700 | [diff] [blame] | 857 | listener->onBuffersReleased(); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 858 | } |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 859 | |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 860 | return NO_ERROR; |
| 861 | } |
| 862 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 863 | status_t BufferQueueProducer::attachBuffer(int* outSlot, |
| 864 | const sp<android::GraphicBuffer>& buffer) { |
| 865 | ATRACE_CALL(); |
| 866 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 867 | if (outSlot == nullptr) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 868 | BQ_LOGE("attachBuffer: outSlot must not be NULL"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 869 | return BAD_VALUE; |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 870 | } else if (buffer == nullptr) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 871 | BQ_LOGE("attachBuffer: cannot attach NULL buffer"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 872 | return BAD_VALUE; |
| 873 | } |
| 874 | |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 875 | std::unique_lock<std::mutex> lock(mCore->mMutex); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 876 | |
| 877 | if (mCore->mIsAbandoned) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 878 | BQ_LOGE("attachBuffer: BufferQueue has been abandoned"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 879 | return NO_INIT; |
| 880 | } |
| 881 | |
| 882 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 883 | BQ_LOGE("attachBuffer: BufferQueue has no connected producer"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 884 | return NO_INIT; |
| 885 | } |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 886 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 887 | if (mCore->mSharedBufferMode) { |
| 888 | BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode"); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 889 | return BAD_VALUE; |
| 890 | } |
| 891 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 892 | if (buffer->getGenerationNumber() != mCore->mGenerationNumber) { |
| 893 | BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] " |
| 894 | "[queue %u]", buffer->getGenerationNumber(), |
| 895 | mCore->mGenerationNumber); |
| 896 | return BAD_VALUE; |
| 897 | } |
| 898 | |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 899 | mCore->waitWhileAllocatingLocked(lock); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 900 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 901 | status_t returnFlags = NO_ERROR; |
| 902 | int found; |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 903 | status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 904 | if (status != NO_ERROR) { |
| 905 | return status; |
| 906 | } |
| 907 | |
| 908 | // This should not happen |
| 909 | if (found == BufferQueueCore::INVALID_BUFFER_SLOT) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 910 | BQ_LOGE("attachBuffer: no available buffer slots"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 911 | return -EBUSY; |
| 912 | } |
| 913 | |
| 914 | *outSlot = found; |
| 915 | ATRACE_BUFFER_INDEX(*outSlot); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 916 | BQ_LOGV("attachBuffer: returning slot %d flags=%#x", |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 917 | *outSlot, returnFlags); |
| 918 | |
| 919 | mSlots[*outSlot].mGraphicBuffer = buffer; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 920 | mSlots[*outSlot].mBufferState.attachProducer(); |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 921 | #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP) |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 922 | mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR; |
Jim Shargo | 9084218 | 2024-11-14 00:49:27 +0000 | [diff] [blame^] | 923 | #endif |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 924 | mSlots[*outSlot].mFence = Fence::NO_FENCE; |
Dan Stoza | 2443c79 | 2014-03-24 15:03:46 -0700 | [diff] [blame] | 925 | mSlots[*outSlot].mRequestBufferCalled = true; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 926 | mSlots[*outSlot].mAcquireCalled = false; |
Jammy Yu | 69958b8 | 2017-02-22 16:41:38 -0800 | [diff] [blame] | 927 | mSlots[*outSlot].mNeedsReallocation = false; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 928 | mCore->mActiveBuffers.insert(found); |
Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 929 | VALIDATE_CONSISTENCY(); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 930 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 931 | return returnFlags; |
| 932 | } |
| 933 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 934 | status_t BufferQueueProducer::queueBuffer(int slot, |
| 935 | const QueueBufferInput &input, QueueBufferOutput *output) { |
| 936 | ATRACE_CALL(); |
| 937 | ATRACE_BUFFER_INDEX(slot); |
| 938 | |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 939 | int64_t requestedPresentTimestamp; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 940 | bool isAutoTimestamp; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 941 | android_dataspace dataSpace; |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 942 | Rect crop(Rect::EMPTY_RECT); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 943 | int scalingMode; |
| 944 | uint32_t transform; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 945 | uint32_t stickyTransform; |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 946 | sp<Fence> acquireFence; |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 947 | bool getFrameTimestamps = false; |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 948 | input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace, |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 949 | &crop, &scalingMode, &transform, &acquireFence, &stickyTransform, |
| 950 | &getFrameTimestamps); |
Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 951 | const Region& surfaceDamage = input.getSurfaceDamage(); |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 952 | const HdrMetadata& hdrMetadata = input.getHdrMetadata(); |
Brian Lindahl | 628cff4 | 2024-10-30 11:50:28 -0600 | [diff] [blame] | 953 | const std::optional<PictureProfileHandle>& pictureProfileHandle = |
| 954 | input.getPictureProfileHandle(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 955 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 956 | if (acquireFence == nullptr) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 957 | BQ_LOGE("queueBuffer: fence is NULL"); |
Jesse Hall | de288fe | 2014-11-04 08:30:48 -0800 | [diff] [blame] | 958 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 961 | auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence); |
| 962 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 963 | switch (scalingMode) { |
| 964 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 965 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 966 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: |
| 967 | case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: |
| 968 | break; |
| 969 | default: |
| 970 | BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 971 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 972 | } |
| 973 | |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 974 | sp<IConsumerListener> frameAvailableListener; |
| 975 | sp<IConsumerListener> frameReplacedListener; |
| 976 | int callbackTicket = 0; |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 977 | uint64_t currentFrameNumber = 0; |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 978 | BufferItem item; |
John Reck | d2c4a4a | 2024-02-07 10:31:09 -0500 | [diff] [blame] | 979 | int connectedApi; |
Mathias Agopian | b96661f | 2024-09-17 11:45:38 -0700 | [diff] [blame] | 980 | bool enableEglCpuThrottling = true; |
John Reck | d2c4a4a | 2024-02-07 10:31:09 -0500 | [diff] [blame] | 981 | sp<Fence> lastQueuedFence; |
| 982 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 983 | { // Autolock scope |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 984 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 985 | |
| 986 | if (mCore->mIsAbandoned) { |
| 987 | BQ_LOGE("queueBuffer: BufferQueue has been abandoned"); |
| 988 | return NO_INIT; |
| 989 | } |
| 990 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 991 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 992 | BQ_LOGE("queueBuffer: BufferQueue has no connected producer"); |
| 993 | return NO_INIT; |
| 994 | } |
| 995 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 996 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 997 | BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)", |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 998 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 999 | return BAD_VALUE; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1000 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1001 | BQ_LOGE("queueBuffer: slot %d is not owned by the producer " |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1002 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 1003 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1004 | } else if (!mSlots[slot].mRequestBufferCalled) { |
| 1005 | BQ_LOGE("queueBuffer: slot %d was queued without requesting " |
| 1006 | "a buffer", slot); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 1007 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1010 | // If shared buffer mode has just been enabled, cache the slot of the |
Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 1011 | // first buffer that is queued and mark it as the shared buffer. |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1012 | if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == |
Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 1013 | BufferQueueCore::INVALID_BUFFER_SLOT) { |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1014 | mCore->mSharedBufferSlot = slot; |
Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 1015 | mSlots[slot].mBufferState.mShared = true; |
| 1016 | } |
| 1017 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1018 | BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d" |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 1019 | " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s", |
| 1020 | slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace, |
| 1021 | hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom, |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1022 | transform, |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1023 | BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode))); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1024 | |
| 1025 | const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer); |
| 1026 | Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight()); |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 1027 | Rect croppedRect(Rect::EMPTY_RECT); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1028 | crop.intersect(bufferRect, &croppedRect); |
| 1029 | if (croppedRect != crop) { |
| 1030 | BQ_LOGE("queueBuffer: crop rect is not contained within the " |
| 1031 | "buffer in slot %d", slot); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 1032 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1035 | // Override UNKNOWN dataspace with consumer default |
| 1036 | if (dataSpace == HAL_DATASPACE_UNKNOWN) { |
| 1037 | dataSpace = mCore->mDefaultBufferDataSpace; |
| 1038 | } |
| 1039 | |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1040 | mSlots[slot].mFence = acquireFence; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1041 | mSlots[slot].mBufferState.queue(); |
| 1042 | |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1043 | // Increment the frame counter and store a local version of it |
| 1044 | // for use outside the lock on mCore->mMutex. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1045 | ++mCore->mFrameCounter; |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1046 | currentFrameNumber = mCore->mFrameCounter; |
| 1047 | mSlots[slot].mFrameNumber = currentFrameNumber; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1048 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1049 | item.mAcquireCalled = mSlots[slot].mAcquireCalled; |
| 1050 | item.mGraphicBuffer = mSlots[slot].mGraphicBuffer; |
| 1051 | item.mCrop = crop; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1052 | item.mTransform = transform & |
| 1053 | ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1054 | item.mTransformToDisplayInverse = |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1055 | (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0; |
| 1056 | item.mScalingMode = static_cast<uint32_t>(scalingMode); |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1057 | item.mTimestamp = requestedPresentTimestamp; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1058 | item.mIsAutoTimestamp = isAutoTimestamp; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1059 | item.mDataSpace = dataSpace; |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 1060 | item.mHdrMetadata = hdrMetadata; |
Brian Lindahl | 628cff4 | 2024-10-30 11:50:28 -0600 | [diff] [blame] | 1061 | item.mPictureProfileHandle = pictureProfileHandle; |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1062 | item.mFrameNumber = currentFrameNumber; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1063 | item.mSlot = slot; |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1064 | item.mFence = acquireFence; |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1065 | item.mFenceTime = acquireFenceTime; |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 1066 | item.mIsDroppable = mCore->mAsyncMode || |
Sungtak Lee | 45e9e0b | 2019-05-28 13:38:23 -0700 | [diff] [blame] | 1067 | (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) || |
Sungtak Lee | 3249fb6 | 2019-03-02 16:40:47 -0800 | [diff] [blame] | 1068 | (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) || |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1069 | (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot); |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1070 | item.mSurfaceDamage = surfaceDamage; |
Pablo Ceballos | 0631218 | 2015-10-07 16:32:12 -0700 | [diff] [blame] | 1071 | item.mQueuedBuffer = true; |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1072 | item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh; |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 1073 | item.mApi = mCore->mConnectedApi; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1074 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1075 | mStickyTransform = stickyTransform; |
| 1076 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1077 | // Cache the shared buffer data so that the BufferItem can be recreated. |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1078 | if (mCore->mSharedBufferMode) { |
| 1079 | mCore->mSharedBufferCache.crop = crop; |
| 1080 | mCore->mSharedBufferCache.transform = transform; |
| 1081 | mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>( |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1082 | scalingMode); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1083 | mCore->mSharedBufferCache.dataspace = dataSpace; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Shuzhen Wang | 22f842b | 2017-01-18 23:02:36 -0800 | [diff] [blame] | 1086 | output->bufferReplaced = false; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1087 | if (mCore->mQueue.empty()) { |
| 1088 | // When the queue is empty, we can ignore mDequeueBufferCannotBlock |
| 1089 | // and simply queue this buffer |
| 1090 | mCore->mQueue.push_back(item); |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1091 | frameAvailableListener = mCore->mConsumerListener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1092 | } else { |
Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 1093 | // When the queue is not empty, we need to look at the last buffer |
| 1094 | // in the queue to see if we need to replace it |
| 1095 | const BufferItem& last = mCore->mQueue.itemAt( |
| 1096 | mCore->mQueue.size() - 1); |
| 1097 | if (last.mIsDroppable) { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1098 | |
Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 1099 | if (!last.mIsStale) { |
| 1100 | mSlots[last.mSlot].mBufferState.freeQueued(); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1101 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1102 | // After leaving shared buffer mode, the shared buffer will |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1103 | // still be around. Mark it as no longer shared if this |
| 1104 | // operation causes it to be free. |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1105 | if (!mCore->mSharedBufferMode && |
Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 1106 | mSlots[last.mSlot].mBufferState.isFree()) { |
| 1107 | mSlots[last.mSlot].mBufferState.mShared = false; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1108 | } |
| 1109 | // Don't put the shared buffer on the free list. |
Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 1110 | if (!mSlots[last.mSlot].mBufferState.isShared()) { |
| 1111 | mCore->mActiveBuffers.erase(last.mSlot); |
| 1112 | mCore->mFreeBuffers.push_back(last.mSlot); |
Shuzhen Wang | 22f842b | 2017-01-18 23:02:36 -0800 | [diff] [blame] | 1113 | output->bufferReplaced = true; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1114 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1115 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1116 | |
Steven Thomas | 862e753 | 2019-07-10 19:21:03 -0700 | [diff] [blame] | 1117 | // Make sure to merge the damage rect from the frame we're about |
| 1118 | // to drop into the new frame's damage rect. |
| 1119 | if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT || |
| 1120 | item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) { |
| 1121 | item.mSurfaceDamage = Region::INVALID_REGION; |
| 1122 | } else { |
| 1123 | item.mSurfaceDamage |= last.mSurfaceDamage; |
| 1124 | } |
| 1125 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1126 | // Overwrite the droppable buffer with the incoming one |
Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 1127 | mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item; |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1128 | frameReplacedListener = mCore->mConsumerListener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1129 | } else { |
| 1130 | mCore->mQueue.push_back(item); |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1131 | frameAvailableListener = mCore->mConsumerListener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | mCore->mBufferHasBeenQueued = true; |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 1136 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL) |
| 1137 | mCore->notifyBufferReleased(); |
| 1138 | #else |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1139 | mCore->mDequeueCondition.notify_all(); |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 1140 | #endif |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 1141 | mCore->mLastQueuedSlot = slot; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1142 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1143 | output->width = mCore->mDefaultWidth; |
| 1144 | output->height = mCore->mDefaultHeight; |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1145 | output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint; |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1146 | output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size()); |
| 1147 | output->nextFrameNumber = mCore->mFrameCounter + 1; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1148 | |
Tomasz Wasilczyk | f2add40 | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 1149 | ATRACE_INT(mCore->mConsumerName.c_str(), static_cast<int32_t>(mCore->mQueue.size())); |
Chong Zhang | 6249309 | 2020-01-15 16:04:47 -0800 | [diff] [blame] | 1150 | #ifndef NO_BINDER |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 1151 | mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size()); |
Chong Zhang | 6249309 | 2020-01-15 16:04:47 -0800 | [diff] [blame] | 1152 | #endif |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1153 | // Take a ticket for the callback functions |
| 1154 | callbackTicket = mNextCallbackTicket++; |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 1155 | |
Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 1156 | VALIDATE_CONSISTENCY(); |
John Reck | d2c4a4a | 2024-02-07 10:31:09 -0500 | [diff] [blame] | 1157 | |
| 1158 | connectedApi = mCore->mConnectedApi; |
Mathias Agopian | b96661f | 2024-09-17 11:45:38 -0700 | [diff] [blame] | 1159 | if (flags::bq_producer_throttles_only_async_mode()) { |
| 1160 | enableEglCpuThrottling = mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock; |
| 1161 | } |
John Reck | d2c4a4a | 2024-02-07 10:31:09 -0500 | [diff] [blame] | 1162 | lastQueuedFence = std::move(mLastQueueBufferFence); |
| 1163 | |
| 1164 | mLastQueueBufferFence = std::move(acquireFence); |
| 1165 | mLastQueuedCrop = item.mCrop; |
| 1166 | mLastQueuedTransform = item.mTransform; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1167 | } // Autolock scope |
| 1168 | |
Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 1169 | // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because |
| 1170 | // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and |
| 1171 | // there will be no Binder call |
| 1172 | if (!mConsumerIsSurfaceFlinger) { |
| 1173 | item.mGraphicBuffer.clear(); |
| 1174 | } |
| 1175 | |
JihCheng Chiu | 544c522 | 2019-04-02 20:50:58 +0800 | [diff] [blame] | 1176 | // Update and get FrameEventHistory. |
| 1177 | nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1178 | NewFrameEventsEntry newFrameEventsEntry = { |
| 1179 | currentFrameNumber, |
| 1180 | postedTime, |
| 1181 | requestedPresentTimestamp, |
| 1182 | std::move(acquireFenceTime) |
| 1183 | }; |
| 1184 | addAndGetFrameTimestamps(&newFrameEventsEntry, |
| 1185 | getFrameTimestamps ? &output->frameTimestamps : nullptr); |
| 1186 | |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1187 | // Call back without the main BufferQueue lock held, but with the callback |
| 1188 | // lock held so we can ensure that callbacks occur in order |
Mathias Agopian | 4f6ce7c | 2017-04-03 17:14:31 -0700 | [diff] [blame] | 1189 | |
Mathias Agopian | 4f6ce7c | 2017-04-03 17:14:31 -0700 | [diff] [blame] | 1190 | { // scope for the lock |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1191 | std::unique_lock<std::mutex> lock(mCallbackMutex); |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1192 | while (callbackTicket != mCurrentCallbackTicket) { |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1193 | mCallbackCondition.wait(lock); |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1194 | } |
| 1195 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1196 | if (frameAvailableListener != nullptr) { |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1197 | frameAvailableListener->onFrameAvailable(item); |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1198 | } else if (frameReplacedListener != nullptr) { |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1199 | frameReplacedListener->onFrameReplaced(item); |
| 1200 | } |
| 1201 | |
| 1202 | ++mCurrentCallbackTicket; |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1203 | mCallbackCondition.notify_all(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 1206 | // Wait without lock held |
Mathias Agopian | b96661f | 2024-09-17 11:45:38 -0700 | [diff] [blame] | 1207 | if (connectedApi == NATIVE_WINDOW_API_EGL && enableEglCpuThrottling) { |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 1208 | // Waiting here allows for two full buffers to be queued but not a |
| 1209 | // third. In the event that frames take varying time, this makes a |
| 1210 | // small trade-off in favor of latency rather than throughput. |
| 1211 | lastQueuedFence->waitForever("Throttling EGL Production"); |
| 1212 | } |
| 1213 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1214 | return NO_ERROR; |
| 1215 | } |
| 1216 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 1217 | status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1218 | ATRACE_CALL(); |
| 1219 | BQ_LOGV("cancelBuffer: slot %d", slot); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1220 | |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 1221 | sp<IConsumerListener> listener; |
| 1222 | bool callOnFrameCancelled = false; |
| 1223 | uint64_t bufferId = 0; // Only used if callOnFrameCancelled == true |
| 1224 | { |
| 1225 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
| 1226 | |
| 1227 | if (mCore->mIsAbandoned) { |
| 1228 | BQ_LOGE("cancelBuffer: BufferQueue has been abandoned"); |
| 1229 | return NO_INIT; |
| 1230 | } |
| 1231 | |
| 1232 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 1233 | BQ_LOGE("cancelBuffer: BufferQueue has no connected producer"); |
| 1234 | return NO_INIT; |
| 1235 | } |
| 1236 | |
| 1237 | if (mCore->mSharedBufferMode) { |
| 1238 | BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode"); |
| 1239 | return BAD_VALUE; |
| 1240 | } |
| 1241 | |
| 1242 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
| 1243 | BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", slot, |
| 1244 | BufferQueueDefs::NUM_BUFFER_SLOTS); |
| 1245 | return BAD_VALUE; |
| 1246 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
| 1247 | BQ_LOGE("cancelBuffer: slot %d is not owned by the producer " |
| 1248 | "(state = %s)", |
| 1249 | slot, mSlots[slot].mBufferState.string()); |
| 1250 | return BAD_VALUE; |
| 1251 | } else if (fence == nullptr) { |
| 1252 | BQ_LOGE("cancelBuffer: fence is NULL"); |
| 1253 | return BAD_VALUE; |
| 1254 | } |
| 1255 | |
| 1256 | mSlots[slot].mBufferState.cancel(); |
| 1257 | |
| 1258 | // After leaving shared buffer mode, the shared buffer will still be around. |
| 1259 | // Mark it as no longer shared if this operation causes it to be free. |
| 1260 | if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) { |
| 1261 | mSlots[slot].mBufferState.mShared = false; |
| 1262 | } |
| 1263 | |
| 1264 | // Don't put the shared buffer on the free list. |
| 1265 | if (!mSlots[slot].mBufferState.isShared()) { |
| 1266 | mCore->mActiveBuffers.erase(slot); |
| 1267 | mCore->mFreeBuffers.push_back(slot); |
| 1268 | } |
| 1269 | |
| 1270 | auto gb = mSlots[slot].mGraphicBuffer; |
| 1271 | if (gb != nullptr) { |
| 1272 | callOnFrameCancelled = true; |
| 1273 | bufferId = gb->getId(); |
| 1274 | } |
| 1275 | mSlots[slot].mFence = fence; |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 1276 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL) |
| 1277 | mCore->notifyBufferReleased(); |
| 1278 | #else |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 1279 | mCore->mDequeueCondition.notify_all(); |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 1280 | #endif |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 1281 | listener = mCore->mConsumerListener; |
| 1282 | VALIDATE_CONSISTENCY(); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Shuzhen Wang | 266f31a | 2023-07-24 22:45:44 +0000 | [diff] [blame] | 1285 | if (listener != nullptr && callOnFrameCancelled) { |
| 1286 | listener->onFrameCancelled(bufferId); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1287 | } |
| 1288 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 1289 | return NO_ERROR; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | int BufferQueueProducer::query(int what, int *outValue) { |
| 1293 | ATRACE_CALL(); |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1294 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1295 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1296 | if (outValue == nullptr) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1297 | BQ_LOGE("query: outValue was NULL"); |
| 1298 | return BAD_VALUE; |
| 1299 | } |
| 1300 | |
| 1301 | if (mCore->mIsAbandoned) { |
| 1302 | BQ_LOGE("query: BufferQueue has been abandoned"); |
| 1303 | return NO_INIT; |
| 1304 | } |
| 1305 | |
| 1306 | int value; |
| 1307 | switch (what) { |
| 1308 | case NATIVE_WINDOW_WIDTH: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1309 | value = static_cast<int32_t>(mCore->mDefaultWidth); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1310 | break; |
| 1311 | case NATIVE_WINDOW_HEIGHT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1312 | value = static_cast<int32_t>(mCore->mDefaultHeight); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1313 | break; |
| 1314 | case NATIVE_WINDOW_FORMAT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1315 | value = static_cast<int32_t>(mCore->mDefaultBufferFormat); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1316 | break; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 1317 | case NATIVE_WINDOW_LAYER_COUNT: |
| 1318 | // All BufferQueue buffers have a single layer. |
| 1319 | value = BQ_LAYER_COUNT; |
| 1320 | break; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1321 | case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS: |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 1322 | value = mCore->getMinUndequeuedBufferCountLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1323 | break; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1324 | case NATIVE_WINDOW_STICKY_TRANSFORM: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1325 | value = static_cast<int32_t>(mStickyTransform); |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1326 | break; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1327 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: |
| 1328 | value = (mCore->mQueue.size() > 1); |
| 1329 | break; |
| 1330 | case NATIVE_WINDOW_CONSUMER_USAGE_BITS: |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1331 | // deprecated; higher 32 bits are truncated |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1332 | value = static_cast<int32_t>(mCore->mConsumerUsageBits); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1333 | break; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1334 | case NATIVE_WINDOW_DEFAULT_DATASPACE: |
| 1335 | value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace); |
| 1336 | break; |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 1337 | case NATIVE_WINDOW_BUFFER_AGE: |
| 1338 | if (mCore->mBufferAge > INT32_MAX) { |
| 1339 | value = 0; |
| 1340 | } else { |
| 1341 | value = static_cast<int32_t>(mCore->mBufferAge); |
| 1342 | } |
| 1343 | break; |
Jiwen 'Steve' Cai | 2041913 | 2017-04-21 18:49:53 -0700 | [diff] [blame] | 1344 | case NATIVE_WINDOW_CONSUMER_IS_PROTECTED: |
| 1345 | value = static_cast<int32_t>(mCore->mConsumerIsProtected); |
| 1346 | break; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1347 | default: |
| 1348 | return BAD_VALUE; |
| 1349 | } |
| 1350 | |
| 1351 | BQ_LOGV("query: %d? %d", what, value); |
| 1352 | *outValue = value; |
| 1353 | return NO_ERROR; |
| 1354 | } |
| 1355 | |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1356 | status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener, |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1357 | int api, bool producerControlledByApp, QueueBufferOutput *output) { |
| 1358 | ATRACE_CALL(); |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1359 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1360 | mConsumerName = mCore->mConsumerName; |
| 1361 | BQ_LOGV("connect: api=%d producerControlledByApp=%s", api, |
| 1362 | producerControlledByApp ? "true" : "false"); |
| 1363 | |
| 1364 | if (mCore->mIsAbandoned) { |
| 1365 | BQ_LOGE("connect: BufferQueue has been abandoned"); |
| 1366 | return NO_INIT; |
| 1367 | } |
| 1368 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1369 | if (mCore->mConsumerListener == nullptr) { |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1370 | BQ_LOGE("connect: BufferQueue has no consumer"); |
| 1371 | return NO_INIT; |
| 1372 | } |
| 1373 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1374 | if (output == nullptr) { |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1375 | BQ_LOGE("connect: output was NULL"); |
| 1376 | return BAD_VALUE; |
| 1377 | } |
| 1378 | |
| 1379 | if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) { |
| 1380 | BQ_LOGE("connect: already connected (cur=%d req=%d)", |
| 1381 | mCore->mConnectedApi, api); |
| 1382 | return BAD_VALUE; |
| 1383 | } |
| 1384 | |
| 1385 | int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, |
| 1386 | mDequeueTimeout < 0 ? |
| 1387 | mCore->mConsumerControlledByApp && producerControlledByApp : false, |
| 1388 | mCore->mMaxBufferCount) - |
| 1389 | mCore->getMaxBufferCountLocked(); |
| 1390 | if (!mCore->adjustAvailableSlotsLocked(delta)) { |
| 1391 | BQ_LOGE("connect: BufferQueue failed to adjust the number of available " |
| 1392 | "slots. Delta = %d", delta); |
| 1393 | return BAD_VALUE; |
| 1394 | } |
| 1395 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1396 | int status = NO_ERROR; |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1397 | switch (api) { |
| 1398 | case NATIVE_WINDOW_API_EGL: |
| 1399 | case NATIVE_WINDOW_API_CPU: |
| 1400 | case NATIVE_WINDOW_API_MEDIA: |
| 1401 | case NATIVE_WINDOW_API_CAMERA: |
| 1402 | mCore->mConnectedApi = api; |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1403 | |
| 1404 | output->width = mCore->mDefaultWidth; |
| 1405 | output->height = mCore->mDefaultHeight; |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1406 | output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint; |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1407 | output->numPendingBuffers = |
| 1408 | static_cast<uint32_t>(mCore->mQueue.size()); |
| 1409 | output->nextFrameNumber = mCore->mFrameCounter + 1; |
Shuzhen Wang | 22f842b | 2017-01-18 23:02:36 -0800 | [diff] [blame] | 1410 | output->bufferReplaced = false; |
silence_dogood | e9d092a | 2019-06-19 16:14:53 -0700 | [diff] [blame] | 1411 | output->maxBufferCount = mCore->mMaxBufferCount; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1412 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1413 | if (listener != nullptr) { |
Matthew Bouyack | 3b8e6b2 | 2016-10-03 16:24:26 -0700 | [diff] [blame] | 1414 | // Set up a death notification so that we can disconnect |
| 1415 | // automatically if the remote producer dies |
Chong Zhang | 6249309 | 2020-01-15 16:04:47 -0800 | [diff] [blame] | 1416 | #ifndef NO_BINDER |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1417 | if (IInterface::asBinder(listener)->remoteBinder() != nullptr) { |
Matthew Bouyack | 3b8e6b2 | 2016-10-03 16:24:26 -0700 | [diff] [blame] | 1418 | status = IInterface::asBinder(listener)->linkToDeath( |
| 1419 | static_cast<IBinder::DeathRecipient*>(this)); |
| 1420 | if (status != NO_ERROR) { |
| 1421 | BQ_LOGE("connect: linkToDeath failed: %s (%d)", |
| 1422 | strerror(-status), status); |
| 1423 | } |
| 1424 | mCore->mLinkedToDeath = listener; |
| 1425 | } |
Chong Zhang | 6249309 | 2020-01-15 16:04:47 -0800 | [diff] [blame] | 1426 | #endif |
Shuzhen Wang | 067fcd3 | 2019-08-14 10:41:12 -0700 | [diff] [blame] | 1427 | mCore->mConnectedProducerListener = listener; |
| 1428 | mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify(); |
Sungtak Lee | cd21747 | 2024-07-19 17:17:40 +0000 | [diff] [blame] | 1429 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_CONSUMER_ATTACH_CALLBACK) |
| 1430 | mCore->mBufferAttachedCbEnabled = listener->needsAttachNotify(); |
| 1431 | #endif |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1432 | } |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1433 | break; |
| 1434 | default: |
| 1435 | BQ_LOGE("connect: unknown API %d", api); |
| 1436 | status = BAD_VALUE; |
| 1437 | break; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1438 | } |
Jayant Chowdhary | ad9fe27 | 2019-03-07 22:36:06 -0800 | [diff] [blame] | 1439 | mCore->mConnectedPid = BufferQueueThreadState::getCallingPid(); |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1440 | mCore->mBufferHasBeenQueued = false; |
| 1441 | mCore->mDequeueBufferCannotBlock = false; |
Sungtak Lee | 3249fb6 | 2019-03-02 16:40:47 -0800 | [diff] [blame] | 1442 | mCore->mQueueBufferCanDrop = false; |
| 1443 | mCore->mLegacyBufferDrop = true; |
| 1444 | if (mCore->mConsumerControlledByApp && producerControlledByApp) { |
| 1445 | mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0; |
| 1446 | mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0; |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1447 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1448 | |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1449 | mCore->mAllowAllocation = true; |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1450 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 1451 | mCore->mAdditionalOptions.clear(); |
| 1452 | #endif |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1453 | VALIDATE_CONSISTENCY(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1454 | return status; |
| 1455 | } |
| 1456 | |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1457 | status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1458 | ATRACE_CALL(); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1459 | BQ_LOGV("disconnect: api %d", api); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1460 | |
| 1461 | int status = NO_ERROR; |
| 1462 | sp<IConsumerListener> listener; |
| 1463 | { // Autolock scope |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1464 | std::unique_lock<std::mutex> lock(mCore->mMutex); |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1465 | |
| 1466 | if (mode == DisconnectMode::AllLocal) { |
Jayant Chowdhary | ad9fe27 | 2019-03-07 22:36:06 -0800 | [diff] [blame] | 1467 | if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) { |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1468 | return NO_ERROR; |
| 1469 | } |
| 1470 | api = BufferQueueCore::CURRENTLY_CONNECTED_API; |
| 1471 | } |
| 1472 | |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1473 | mCore->waitWhileAllocatingLocked(lock); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1474 | |
| 1475 | if (mCore->mIsAbandoned) { |
| 1476 | // It's not really an error to disconnect after the surface has |
| 1477 | // been abandoned; it should just be a no-op. |
| 1478 | return NO_ERROR; |
| 1479 | } |
| 1480 | |
Chong Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 1481 | if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) { |
Chong Zhang | 5ac5148 | 2017-02-16 15:52:33 -0800 | [diff] [blame] | 1482 | if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) { |
| 1483 | ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode); |
| 1484 | } |
Chong Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 1485 | api = mCore->mConnectedApi; |
Chong Zhang | 26bb2b1 | 2016-03-08 12:08:33 -0800 | [diff] [blame] | 1486 | // If we're asked to disconnect the currently connected api but |
| 1487 | // nobody is connected, it's not really an error. |
| 1488 | if (api == BufferQueueCore::NO_CONNECTED_API) { |
| 1489 | return NO_ERROR; |
| 1490 | } |
Chong Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 1491 | } |
| 1492 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1493 | switch (api) { |
| 1494 | case NATIVE_WINDOW_API_EGL: |
| 1495 | case NATIVE_WINDOW_API_CPU: |
| 1496 | case NATIVE_WINDOW_API_MEDIA: |
| 1497 | case NATIVE_WINDOW_API_CAMERA: |
| 1498 | if (mCore->mConnectedApi == api) { |
| 1499 | mCore->freeAllBuffersLocked(); |
| 1500 | |
Chong Zhang | 6249309 | 2020-01-15 16:04:47 -0800 | [diff] [blame] | 1501 | #ifndef NO_BINDER |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1502 | // Remove our death notification callback if we have one |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1503 | if (mCore->mLinkedToDeath != nullptr) { |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1504 | sp<IBinder> token = |
Matthew Bouyack | 3b8e6b2 | 2016-10-03 16:24:26 -0700 | [diff] [blame] | 1505 | IInterface::asBinder(mCore->mLinkedToDeath); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1506 | // This can fail if we're here because of the death |
| 1507 | // notification, but we just ignore it |
| 1508 | token->unlinkToDeath( |
| 1509 | static_cast<IBinder::DeathRecipient*>(this)); |
| 1510 | } |
Chong Zhang | 6249309 | 2020-01-15 16:04:47 -0800 | [diff] [blame] | 1511 | #endif |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1512 | mCore->mSharedBufferSlot = |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1513 | BufferQueueCore::INVALID_BUFFER_SLOT; |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1514 | mCore->mLinkedToDeath = nullptr; |
| 1515 | mCore->mConnectedProducerListener = nullptr; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1516 | mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API; |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1517 | mCore->mConnectedPid = -1; |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1518 | mCore->mSidebandStream.clear(); |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 1519 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL) |
| 1520 | mCore->notifyBufferReleased(); |
| 1521 | #else |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1522 | mCore->mDequeueCondition.notify_all(); |
Patrick Williams | 078d736 | 2024-08-27 10:20:39 -0500 | [diff] [blame] | 1523 | #endif |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1524 | mCore->mAutoPrerotation = false; |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1525 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 1526 | mCore->mAdditionalOptions.clear(); |
| 1527 | #endif |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1528 | listener = mCore->mConsumerListener; |
Wonsik Kim | 3e198b2 | 2017-04-07 15:43:16 -0700 | [diff] [blame] | 1529 | } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 1530 | BQ_LOGE("disconnect: not connected (req=%d)", api); |
| 1531 | status = NO_INIT; |
| 1532 | } else { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1533 | BQ_LOGE("disconnect: still connected to another API " |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1534 | "(cur=%d req=%d)", mCore->mConnectedApi, api); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 1535 | status = BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1536 | } |
| 1537 | break; |
| 1538 | default: |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1539 | BQ_LOGE("disconnect: unknown API %d", api); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 1540 | status = BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1541 | break; |
| 1542 | } |
| 1543 | } // Autolock scope |
| 1544 | |
| 1545 | // Call back without lock held |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1546 | if (listener != nullptr) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1547 | listener->onBuffersReleased(); |
Brian Anderson | 5ea5e59 | 2016-12-01 16:54:33 -0800 | [diff] [blame] | 1548 | listener->onDisconnect(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | return status; |
| 1552 | } |
| 1553 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1554 | status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) { |
Wonsik Kim | afe3081 | 2014-03-31 23:16:08 +0900 | [diff] [blame] | 1555 | sp<IConsumerListener> listener; |
| 1556 | { // Autolock scope |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1557 | std::lock_guard<std::mutex> _l(mCore->mMutex); |
Wonsik Kim | afe3081 | 2014-03-31 23:16:08 +0900 | [diff] [blame] | 1558 | mCore->mSidebandStream = stream; |
| 1559 | listener = mCore->mConsumerListener; |
| 1560 | } // Autolock scope |
| 1561 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1562 | if (listener != nullptr) { |
Wonsik Kim | afe3081 | 2014-03-31 23:16:08 +0900 | [diff] [blame] | 1563 | listener->onSidebandStreamChanged(); |
| 1564 | } |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1565 | return NO_ERROR; |
| 1566 | } |
| 1567 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 1568 | void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height, |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1569 | PixelFormat format, uint64_t usage) { |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1570 | ATRACE_CALL(); |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1571 | |
| 1572 | const bool useDefaultSize = !width && !height; |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1573 | while (true) { |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1574 | uint32_t allocWidth = 0; |
| 1575 | uint32_t allocHeight = 0; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1576 | PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1577 | uint64_t allocUsage = 0; |
Chia-I Wu | 1dbf0e3 | 2018-02-07 14:40:08 -0800 | [diff] [blame] | 1578 | std::string allocName; |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1579 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 1580 | std::vector<gui::AdditionalOptions> allocOptions; |
| 1581 | uint32_t allocOptionsGenId = 0; |
| 1582 | #endif |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1583 | { // Autolock scope |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1584 | std::unique_lock<std::mutex> lock(mCore->mMutex); |
| 1585 | mCore->waitWhileAllocatingLocked(lock); |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1586 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 1587 | if (!mCore->mAllowAllocation) { |
| 1588 | BQ_LOGE("allocateBuffers: allocation is not allowed for this " |
| 1589 | "BufferQueue"); |
| 1590 | return; |
| 1591 | } |
| 1592 | |
Jorim Jaggi | 0a3e784 | 2018-07-17 13:48:33 +0200 | [diff] [blame] | 1593 | // Only allocate one buffer at a time to reduce risks of overlapping an allocation from |
| 1594 | // both allocateBuffers and dequeueBuffer. |
Shuangxi Xiang | 045a30e | 2024-10-14 09:38:32 +0000 | [diff] [blame] | 1595 | if (mCore->mFreeSlots.empty()) { |
| 1596 | BQ_LOGV("allocateBuffers: a slot was occupied while " |
| 1597 | "allocating. Dropping allocated buffer."); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1598 | return; |
| 1599 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1600 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1601 | allocWidth = width > 0 ? width : mCore->mDefaultWidth; |
| 1602 | allocHeight = height > 0 ? height : mCore->mDefaultHeight; |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1603 | if (useDefaultSize && mCore->mAutoPrerotation && |
| 1604 | (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) { |
| 1605 | std::swap(allocWidth, allocHeight); |
| 1606 | } |
| 1607 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1608 | allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat; |
| 1609 | allocUsage = usage | mCore->mConsumerUsageBits; |
Tomasz Wasilczyk | f2add40 | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 1610 | allocName.assign(mCore->mConsumerName.c_str(), mCore->mConsumerName.size()); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1611 | |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1612 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 1613 | allocOptions = mCore->mAdditionalOptions; |
| 1614 | allocOptionsGenId = mCore->mAdditionalOptionsGenerationId; |
| 1615 | #endif |
| 1616 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1617 | mCore->mIsAllocating = true; |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1618 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1619 | } // Autolock scope |
| 1620 | |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1621 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 1622 | std::vector<GraphicBufferAllocator::AdditionalOptions> tempOptions; |
| 1623 | tempOptions.reserve(allocOptions.size()); |
| 1624 | for (const auto& it : allocOptions) { |
| 1625 | tempOptions.emplace_back(it.name.c_str(), it.value); |
| 1626 | } |
| 1627 | const GraphicBufferAllocator::AllocationRequest allocRequest = { |
| 1628 | .importBuffer = true, |
| 1629 | .width = allocWidth, |
| 1630 | .height = allocHeight, |
| 1631 | .format = allocFormat, |
| 1632 | .layerCount = BQ_LAYER_COUNT, |
| 1633 | .usage = allocUsage, |
| 1634 | .requestorName = allocName, |
| 1635 | .extras = std::move(tempOptions), |
| 1636 | }; |
| 1637 | #endif |
| 1638 | |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1639 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
Shuangxi Xiang | 045a30e | 2024-10-14 09:38:32 +0000 | [diff] [blame] | 1640 | sp<GraphicBuffer> graphicBuffer = new GraphicBuffer(allocRequest); |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1641 | #else |
Shuangxi Xiang | 045a30e | 2024-10-14 09:38:32 +0000 | [diff] [blame] | 1642 | sp<GraphicBuffer> graphicBuffer = new GraphicBuffer( |
| 1643 | allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT, |
| 1644 | allocUsage, allocName); |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1645 | #endif |
Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 1646 | |
Shuangxi Xiang | 045a30e | 2024-10-14 09:38:32 +0000 | [diff] [blame] | 1647 | status_t result = graphicBuffer->initCheck(); |
Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 1648 | |
Shuangxi Xiang | 045a30e | 2024-10-14 09:38:32 +0000 | [diff] [blame] | 1649 | if (result != NO_ERROR) { |
| 1650 | BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format" |
| 1651 | " %u, usage %#" PRIx64 ")", width, height, format, usage); |
| 1652 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
| 1653 | mCore->mIsAllocating = false; |
| 1654 | mCore->mIsAllocatingCondition.notify_all(); |
| 1655 | return; |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | { // Autolock scope |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1659 | std::unique_lock<std::mutex> lock(mCore->mMutex); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1660 | uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth; |
| 1661 | uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight; |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1662 | if (useDefaultSize && mCore->mAutoPrerotation && |
| 1663 | (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) { |
| 1664 | std::swap(checkWidth, checkHeight); |
| 1665 | } |
| 1666 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1667 | PixelFormat checkFormat = format != 0 ? |
| 1668 | format : mCore->mDefaultBufferFormat; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1669 | uint64_t checkUsage = usage | mCore->mConsumerUsageBits; |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1670 | bool allocOptionsChanged = false; |
| 1671 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 1672 | allocOptionsChanged = allocOptionsGenId != mCore->mAdditionalOptionsGenerationId; |
| 1673 | #endif |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1674 | if (checkWidth != allocWidth || checkHeight != allocHeight || |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1675 | checkFormat != allocFormat || checkUsage != allocUsage || allocOptionsChanged) { |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1676 | // Something changed while we released the lock. Retry. |
| 1677 | BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying."); |
| 1678 | mCore->mIsAllocating = false; |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1679 | mCore->mIsAllocatingCondition.notify_all(); |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1680 | continue; |
| 1681 | } |
| 1682 | |
Shuangxi Xiang | 045a30e | 2024-10-14 09:38:32 +0000 | [diff] [blame] | 1683 | if (mCore->mFreeSlots.empty()) { |
| 1684 | BQ_LOGV("allocateBuffers: a slot was occupied while " |
| 1685 | "allocating. Dropping allocated buffer."); |
| 1686 | } else { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1687 | auto slot = mCore->mFreeSlots.begin(); |
| 1688 | mCore->clearBufferSlotLocked(*slot); // Clean up the slot first |
Shuangxi Xiang | 045a30e | 2024-10-14 09:38:32 +0000 | [diff] [blame] | 1689 | mSlots[*slot].mGraphicBuffer = graphicBuffer; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1690 | mSlots[*slot].mFence = Fence::NO_FENCE; |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1691 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 1692 | mSlots[*slot].mAdditionalOptionsGenerationId = allocOptionsGenId; |
| 1693 | #endif |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 1694 | |
| 1695 | // freeBufferLocked puts this slot on the free slots list. Since |
| 1696 | // we then attached a buffer, move the slot to free buffer list. |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1697 | mCore->mFreeBuffers.push_front(*slot); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 1698 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1699 | BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", |
| 1700 | *slot); |
Christopher Ferris | 87e94cd | 2016-04-26 11:29:08 -0700 | [diff] [blame] | 1701 | |
| 1702 | // Make sure the erase is done after all uses of the slot |
| 1703 | // iterator since it will be invalid after this point. |
| 1704 | mCore->mFreeSlots.erase(slot); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1705 | } |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1706 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1707 | mCore->mIsAllocating = false; |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1708 | mCore->mIsAllocatingCondition.notify_all(); |
Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 1709 | VALIDATE_CONSISTENCY(); |
Jorim Jaggi | 35b4e38 | 2019-03-28 00:44:03 +0100 | [diff] [blame] | 1710 | |
| 1711 | // If dequeue is waiting for to allocate a buffer, release the lock until it's not |
| 1712 | // waiting anymore so it can use the buffer we just allocated. |
| 1713 | while (mDequeueWaitingForAllocation) { |
| 1714 | mDequeueWaitingForAllocationCondition.wait(lock); |
| 1715 | } |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1716 | } // Autolock scope |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 1720 | status_t BufferQueueProducer::allowAllocation(bool allow) { |
| 1721 | ATRACE_CALL(); |
| 1722 | BQ_LOGV("allowAllocation: %s", allow ? "true" : "false"); |
| 1723 | |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1724 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 1725 | mCore->mAllowAllocation = allow; |
| 1726 | return NO_ERROR; |
| 1727 | } |
| 1728 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1729 | status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) { |
| 1730 | ATRACE_CALL(); |
| 1731 | BQ_LOGV("setGenerationNumber: %u", generationNumber); |
| 1732 | |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1733 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1734 | mCore->mGenerationNumber = generationNumber; |
| 1735 | return NO_ERROR; |
| 1736 | } |
| 1737 | |
Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 1738 | String8 BufferQueueProducer::getConsumerName() const { |
| 1739 | ATRACE_CALL(); |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1740 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Tomasz Wasilczyk | f2add40 | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 1741 | BQ_LOGV("getConsumerName: %s", mConsumerName.c_str()); |
Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 1742 | return mConsumerName; |
| 1743 | } |
| 1744 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1745 | status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1746 | ATRACE_CALL(); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1747 | BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1748 | |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1749 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1750 | if (!sharedBufferMode) { |
| 1751 | mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1752 | } |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1753 | mCore->mSharedBufferMode = sharedBufferMode; |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1754 | return NO_ERROR; |
| 1755 | } |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1756 | |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1757 | status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) { |
| 1758 | ATRACE_CALL(); |
| 1759 | BQ_LOGV("setAutoRefresh: %d", autoRefresh); |
| 1760 | |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1761 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1762 | |
| 1763 | mCore->mAutoRefresh = autoRefresh; |
| 1764 | return NO_ERROR; |
| 1765 | } |
| 1766 | |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1767 | status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) { |
| 1768 | ATRACE_CALL(); |
| 1769 | BQ_LOGV("setDequeueTimeout: %" PRId64, timeout); |
| 1770 | |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1771 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Sungtak Lee | 42a94c6 | 2019-05-24 14:27:14 -0700 | [diff] [blame] | 1772 | bool dequeueBufferCannotBlock = |
| 1773 | timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock; |
| 1774 | int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock, |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1775 | mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked(); |
| 1776 | if (!mCore->adjustAvailableSlotsLocked(delta)) { |
| 1777 | BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of " |
| 1778 | "available slots. Delta = %d", delta); |
| 1779 | return BAD_VALUE; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1782 | mDequeueTimeout = timeout; |
Sungtak Lee | 42a94c6 | 2019-05-24 14:27:14 -0700 | [diff] [blame] | 1783 | mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock; |
| 1784 | if (timeout > 0) { |
| 1785 | mCore->mQueueBufferCanDrop = false; |
Sungtak Lee | 3249fb6 | 2019-03-02 16:40:47 -0800 | [diff] [blame] | 1786 | } |
Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 1787 | |
Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1788 | VALIDATE_CONSISTENCY(); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1789 | return NO_ERROR; |
| 1790 | } |
| 1791 | |
Sungtak Lee | 3249fb6 | 2019-03-02 16:40:47 -0800 | [diff] [blame] | 1792 | status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) { |
| 1793 | ATRACE_CALL(); |
| 1794 | BQ_LOGV("setLegacyBufferDrop: drop = %d", drop); |
| 1795 | |
| 1796 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
| 1797 | mCore->mLegacyBufferDrop = drop; |
| 1798 | return NO_ERROR; |
| 1799 | } |
| 1800 | |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 1801 | status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, |
John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 1802 | sp<Fence>* outFence, float outTransformMatrix[16]) { |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 1803 | ATRACE_CALL(); |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 1804 | |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1805 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
John Reck | d2c4a4a | 2024-02-07 10:31:09 -0500 | [diff] [blame] | 1806 | BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot); |
| 1807 | |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 1808 | if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) { |
| 1809 | *outBuffer = nullptr; |
| 1810 | *outFence = Fence::NO_FENCE; |
| 1811 | return NO_ERROR; |
| 1812 | } |
| 1813 | |
| 1814 | *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer; |
| 1815 | *outFence = mLastQueueBufferFence; |
| 1816 | |
John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 1817 | // Currently only SurfaceFlinger internally ever changes |
| 1818 | // GLConsumer's filtering mode, so we just use 'true' here as |
| 1819 | // this is slightly specialized for the current client of this API, |
| 1820 | // which does want filtering. |
| 1821 | GLConsumer::computeTransformMatrix(outTransformMatrix, |
| 1822 | mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop, |
| 1823 | mLastQueuedTransform, true /* filter */); |
| 1824 | |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 1825 | return NO_ERROR; |
| 1826 | } |
| 1827 | |
John Reck | f0f13e8 | 2021-05-18 00:42:56 -0400 | [diff] [blame] | 1828 | status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence, |
| 1829 | Rect* outRect, uint32_t* outTransform) { |
| 1830 | ATRACE_CALL(); |
John Reck | f0f13e8 | 2021-05-18 00:42:56 -0400 | [diff] [blame] | 1831 | |
| 1832 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
John Reck | d2c4a4a | 2024-02-07 10:31:09 -0500 | [diff] [blame] | 1833 | BQ_LOGV("getLastQueuedBuffer, slot=%d", mCore->mLastQueuedSlot); |
| 1834 | if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT || |
| 1835 | mSlots[mCore->mLastQueuedSlot].mBufferState.isDequeued()) { |
John Reck | f0f13e8 | 2021-05-18 00:42:56 -0400 | [diff] [blame] | 1836 | *outBuffer = nullptr; |
| 1837 | *outFence = Fence::NO_FENCE; |
| 1838 | return NO_ERROR; |
| 1839 | } |
| 1840 | |
| 1841 | *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer; |
| 1842 | *outFence = mLastQueueBufferFence; |
| 1843 | *outRect = mLastQueuedCrop; |
| 1844 | *outTransform = mLastQueuedTransform; |
| 1845 | |
| 1846 | return NO_ERROR; |
| 1847 | } |
| 1848 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1849 | void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) { |
| 1850 | addAndGetFrameTimestamps(nullptr, outDelta); |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1851 | } |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1852 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1853 | void BufferQueueProducer::addAndGetFrameTimestamps( |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1854 | const NewFrameEventsEntry* newTimestamps, |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1855 | FrameEventHistoryDelta* outDelta) { |
| 1856 | if (newTimestamps == nullptr && outDelta == nullptr) { |
| 1857 | return; |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | ATRACE_CALL(); |
| 1861 | BQ_LOGV("addAndGetFrameTimestamps"); |
| 1862 | sp<IConsumerListener> listener; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1863 | { |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1864 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1865 | listener = mCore->mConsumerListener; |
| 1866 | } |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1867 | if (listener != nullptr) { |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1868 | listener->addAndGetFrameTimestamps(newTimestamps, outDelta); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1869 | } |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1870 | } |
| 1871 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1872 | void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) { |
| 1873 | // If we're here, it means that a producer we were connected to died. |
| 1874 | // We're guaranteed that we are still connected to it because we remove |
| 1875 | // this callback upon disconnect. It's therefore safe to read mConnectedApi |
| 1876 | // without synchronization here. |
| 1877 | int api = mCore->mConnectedApi; |
| 1878 | disconnect(api); |
| 1879 | } |
| 1880 | |
Pablo Ceballos | 8e3e92b | 2016-06-27 17:56:53 -0700 | [diff] [blame] | 1881 | status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const { |
| 1882 | BQ_LOGV("getUniqueId"); |
| 1883 | |
| 1884 | *outId = mCore->mUniqueId; |
| 1885 | return NO_ERROR; |
| 1886 | } |
| 1887 | |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1888 | status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const { |
| 1889 | BQ_LOGV("getConsumerUsage"); |
| 1890 | |
Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1891 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1892 | *outUsage = mCore->mConsumerUsageBits; |
| 1893 | return NO_ERROR; |
| 1894 | } |
| 1895 | |
Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1896 | status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) { |
| 1897 | ATRACE_CALL(); |
| 1898 | BQ_LOGV("setAutoPrerotation: %d", autoPrerotation); |
| 1899 | |
| 1900 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
| 1901 | |
| 1902 | mCore->mAutoPrerotation = autoPrerotation; |
| 1903 | return NO_ERROR; |
| 1904 | } |
| 1905 | |
Ady Abraham | 107788e | 2023-10-17 12:31:08 -0700 | [diff] [blame] | 1906 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE) |
Ady Abraham | 6cdd3fd | 2023-09-07 18:45:58 -0700 | [diff] [blame] | 1907 | status_t BufferQueueProducer::setFrameRate(float frameRate, int8_t compatibility, |
| 1908 | int8_t changeFrameRateStrategy) { |
| 1909 | ATRACE_CALL(); |
| 1910 | BQ_LOGV("setFrameRate: %.2f", frameRate); |
| 1911 | |
| 1912 | if (!ValidateFrameRate(frameRate, compatibility, changeFrameRateStrategy, |
| 1913 | "BufferQueueProducer::setFrameRate")) { |
| 1914 | return BAD_VALUE; |
| 1915 | } |
| 1916 | |
| 1917 | sp<IConsumerListener> listener; |
| 1918 | { |
| 1919 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
| 1920 | listener = mCore->mConsumerListener; |
| 1921 | } |
| 1922 | if (listener != nullptr) { |
| 1923 | listener->onSetFrameRate(frameRate, compatibility, changeFrameRateStrategy); |
| 1924 | } |
| 1925 | return NO_ERROR; |
| 1926 | } |
| 1927 | #endif |
| 1928 | |
John Reck | db164ff | 2024-04-03 16:59:28 -0400 | [diff] [blame] | 1929 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_EXTENDEDALLOCATE) |
| 1930 | status_t BufferQueueProducer::setAdditionalOptions( |
| 1931 | const std::vector<gui::AdditionalOptions>& options) { |
| 1932 | ATRACE_CALL(); |
| 1933 | BQ_LOGV("setAdditionalOptions, size = %zu", options.size()); |
| 1934 | |
| 1935 | if (!GraphicBufferAllocator::get().supportsAdditionalOptions()) { |
| 1936 | return INVALID_OPERATION; |
| 1937 | } |
| 1938 | |
| 1939 | std::lock_guard<std::mutex> lock(mCore->mMutex); |
| 1940 | |
| 1941 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 1942 | BQ_LOGE("setAdditionalOptions: BufferQueue not connected, cannot set additional options"); |
| 1943 | return NO_INIT; |
| 1944 | } |
| 1945 | |
| 1946 | if (mCore->mAdditionalOptions != options) { |
| 1947 | mCore->mAdditionalOptions = options; |
| 1948 | mCore->mAdditionalOptionsGenerationId++; |
| 1949 | } |
| 1950 | return NO_ERROR; |
| 1951 | } |
| 1952 | #endif |
| 1953 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1954 | } // namespace android |