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