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