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 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 23 | #define EGL_EGLEXT_PROTOTYPES |
| 24 | |
| 25 | #include <gui/BufferItem.h> |
| 26 | #include <gui/BufferQueueCore.h> |
| 27 | #include <gui/BufferQueueProducer.h> |
| 28 | #include <gui/IConsumerListener.h> |
| 29 | #include <gui/IGraphicBufferAlloc.h> |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 30 | #include <gui/IProducerListener.h> |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 31 | |
| 32 | #include <utils/Log.h> |
| 33 | #include <utils/Trace.h> |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) : |
| 38 | mCore(core), |
| 39 | mSlots(core->mSlots), |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 40 | mConsumerName(), |
Eric Penner | 99a0afb | 2014-09-30 11:28:30 -0700 | [diff] [blame] | 41 | mStickyTransform(0), |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 42 | mLastQueueBufferFence(Fence::NO_FENCE), |
| 43 | mCallbackMutex(), |
| 44 | mNextCallbackTicket(0), |
| 45 | mCurrentCallbackTicket(0), |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 46 | mCallbackCondition(), |
| 47 | mDequeueTimeout(-1) {} |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 48 | |
| 49 | BufferQueueProducer::~BufferQueueProducer() {} |
| 50 | |
| 51 | status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) { |
| 52 | ATRACE_CALL(); |
| 53 | BQ_LOGV("requestBuffer: slot %d", slot); |
| 54 | Mutex::Autolock lock(mCore->mMutex); |
| 55 | |
| 56 | if (mCore->mIsAbandoned) { |
| 57 | BQ_LOGE("requestBuffer: BufferQueue has been abandoned"); |
| 58 | return NO_INIT; |
| 59 | } |
| 60 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 61 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 62 | BQ_LOGE("requestBuffer: BufferQueue has no connected producer"); |
| 63 | return NO_INIT; |
| 64 | } |
| 65 | |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 66 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 67 | BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)", |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 68 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 69 | return BAD_VALUE; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 70 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 71 | BQ_LOGE("requestBuffer: slot %d is not owned by the producer " |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 72 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 73 | return BAD_VALUE; |
| 74 | } |
| 75 | |
| 76 | mSlots[slot].mRequestBufferCalled = true; |
| 77 | *buf = mSlots[slot].mGraphicBuffer; |
| 78 | return NO_ERROR; |
| 79 | } |
| 80 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 81 | status_t BufferQueueProducer::setMaxDequeuedBufferCount( |
| 82 | int maxDequeuedBuffers) { |
| 83 | ATRACE_CALL(); |
| 84 | BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d", |
| 85 | maxDequeuedBuffers); |
| 86 | |
| 87 | sp<IConsumerListener> listener; |
| 88 | { // Autolock scope |
| 89 | Mutex::Autolock lock(mCore->mMutex); |
| 90 | mCore->waitWhileAllocatingLocked(); |
| 91 | |
| 92 | if (mCore->mIsAbandoned) { |
| 93 | BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been " |
| 94 | "abandoned"); |
| 95 | return NO_INIT; |
| 96 | } |
| 97 | |
| 98 | // There must be no dequeued buffers when changing the buffer count. |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 99 | for (int s : mCore->mActiveBuffers) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 100 | if (mSlots[s].mBufferState.isDequeued()) { |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 101 | BQ_LOGE("setMaxDequeuedBufferCount: buffer owned by producer"); |
| 102 | return BAD_VALUE; |
| 103 | } |
| 104 | } |
| 105 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 106 | int bufferCount = mCore->getMinUndequeuedBufferCountLocked(); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 107 | bufferCount += maxDequeuedBuffers; |
| 108 | |
| 109 | if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) { |
| 110 | BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large " |
| 111 | "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS); |
| 112 | return BAD_VALUE; |
| 113 | } |
| 114 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 115 | const int minBufferSlots = mCore->getMinMaxBufferCountLocked(); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 116 | if (bufferCount < minBufferSlots) { |
| 117 | BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is " |
| 118 | "less than minimum %d", bufferCount, minBufferSlots); |
| 119 | return BAD_VALUE; |
| 120 | } |
| 121 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 122 | if (bufferCount > mCore->mMaxBufferCount) { |
| 123 | BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would " |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 124 | "exceed the maxBufferCount (%d) (maxAcquired %d async %d " |
| 125 | "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers, |
| 126 | mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount, |
| 127 | mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock); |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 128 | return BAD_VALUE; |
| 129 | } |
| 130 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 131 | // Here we are guaranteed that the producer doesn't have any dequeued |
| 132 | // buffers and will release all of its buffer references. We don't |
| 133 | // clear the queue, however, so that currently queued buffers still |
| 134 | // get displayed. |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 135 | if (!mCore->adjustAvailableSlotsLocked( |
| 136 | maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount)) { |
| 137 | BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue failed to adjust " |
| 138 | "the number of available slots. Delta = %d", |
| 139 | maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount); |
| 140 | return BAD_VALUE; |
| 141 | } |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 142 | mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 143 | mCore->validateConsistencyLocked(); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 144 | mCore->mDequeueCondition.broadcast(); |
| 145 | listener = mCore->mConsumerListener; |
| 146 | } // Autolock scope |
| 147 | |
| 148 | // Call back without lock held |
| 149 | if (listener != NULL) { |
| 150 | listener->onBuffersReleased(); |
| 151 | } |
| 152 | |
| 153 | return NO_ERROR; |
| 154 | } |
| 155 | |
| 156 | status_t BufferQueueProducer::setAsyncMode(bool async) { |
| 157 | ATRACE_CALL(); |
| 158 | BQ_LOGV("setAsyncMode: async = %d", async); |
| 159 | |
| 160 | sp<IConsumerListener> listener; |
| 161 | { // Autolock scope |
| 162 | Mutex::Autolock lock(mCore->mMutex); |
| 163 | mCore->waitWhileAllocatingLocked(); |
| 164 | |
| 165 | if (mCore->mIsAbandoned) { |
| 166 | BQ_LOGE("setAsyncMode: BufferQueue has been abandoned"); |
| 167 | return NO_INIT; |
| 168 | } |
| 169 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 170 | if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount + |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 171 | (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) > |
| 172 | mCore->mMaxBufferCount) { |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 173 | BQ_LOGE("setAsyncMode(%d): this call would cause the " |
| 174 | "maxBufferCount (%d) to be exceeded (maxAcquired %d " |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 175 | "maxDequeued %d mDequeueBufferCannotBlock %d)", async, |
| 176 | mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount, |
| 177 | mCore->mMaxDequeuedBufferCount, |
| 178 | mCore->mDequeueBufferCannotBlock); |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 179 | return BAD_VALUE; |
| 180 | } |
| 181 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 182 | int delta = mCore->getMaxBufferCountLocked(async, |
| 183 | mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount) |
| 184 | - mCore->getMaxBufferCountLocked(); |
| 185 | |
| 186 | if (!mCore->adjustAvailableSlotsLocked(delta)) { |
| 187 | BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of " |
| 188 | "available slots. Delta = %d", delta); |
| 189 | return BAD_VALUE; |
| 190 | } |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 191 | mCore->mAsyncMode = async; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 192 | mCore->validateConsistencyLocked(); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 193 | mCore->mDequeueCondition.broadcast(); |
| 194 | listener = mCore->mConsumerListener; |
| 195 | } // Autolock scope |
| 196 | |
| 197 | // Call back without lock held |
| 198 | if (listener != NULL) { |
| 199 | listener->onBuffersReleased(); |
| 200 | } |
| 201 | return NO_ERROR; |
| 202 | } |
| 203 | |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 204 | int BufferQueueProducer::getFreeBufferLocked() const { |
| 205 | if (mCore->mFreeBuffers.empty()) { |
| 206 | return BufferQueueCore::INVALID_BUFFER_SLOT; |
| 207 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 208 | int slot = mCore->mFreeBuffers.front(); |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 209 | mCore->mFreeBuffers.pop_front(); |
| 210 | return slot; |
| 211 | } |
| 212 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 213 | int BufferQueueProducer::getFreeSlotLocked() const { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 214 | if (mCore->mFreeSlots.empty()) { |
| 215 | return BufferQueueCore::INVALID_BUFFER_SLOT; |
| 216 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 217 | auto slot = mCore->mFreeSlots.begin(); |
| 218 | mCore->mFreeSlots.erase(slot); |
| 219 | return *slot; |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller, |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 223 | int* found) const { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 224 | auto callerString = (caller == FreeSlotCaller::Dequeue) ? |
| 225 | "dequeueBuffer" : "attachBuffer"; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 226 | bool tryAgain = true; |
| 227 | while (tryAgain) { |
| 228 | if (mCore->mIsAbandoned) { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 229 | BQ_LOGE("%s: BufferQueue has been abandoned", callerString); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 230 | return NO_INIT; |
| 231 | } |
| 232 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 233 | int dequeuedCount = 0; |
| 234 | int acquiredCount = 0; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 235 | for (int s : mCore->mActiveBuffers) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 236 | if (mSlots[s].mBufferState.isDequeued()) { |
| 237 | ++dequeuedCount; |
| 238 | } |
| 239 | if (mSlots[s].mBufferState.isAcquired()) { |
| 240 | ++acquiredCount; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 244 | // Producers are not allowed to dequeue more than |
| 245 | // mMaxDequeuedBufferCount buffers. |
| 246 | // This check is only done if a buffer has already been queued |
| 247 | if (mCore->mBufferHasBeenQueued && |
| 248 | dequeuedCount >= mCore->mMaxDequeuedBufferCount) { |
| 249 | BQ_LOGE("%s: attempting to exceed the max dequeued buffer count " |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 250 | "(%d)", callerString, mCore->mMaxDequeuedBufferCount); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 251 | return INVALID_OPERATION; |
| 252 | } |
| 253 | |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 254 | *found = BufferQueueCore::INVALID_BUFFER_SLOT; |
| 255 | |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 256 | // If we disconnect and reconnect quickly, we can be in a state where |
| 257 | // our slots are empty but we have many buffers in the queue. This can |
| 258 | // cause us to run out of memory if we outrun the consumer. Wait here if |
| 259 | // it looks like we have too many buffers queued up. |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 260 | const int maxBufferCount = mCore->getMaxBufferCountLocked(); |
Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 261 | bool tooManyBuffers = mCore->mQueue.size() |
| 262 | > static_cast<size_t>(maxBufferCount); |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 263 | if (tooManyBuffers) { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 264 | BQ_LOGV("%s: queue size is %zu, waiting", callerString, |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 265 | mCore->mQueue.size()); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 266 | } else { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 267 | // If in single buffer mode and a shared buffer exists, always |
| 268 | // return it. |
| 269 | if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot != |
| 270 | BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 271 | *found = mCore->mSingleBufferSlot; |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 272 | } else { |
| 273 | if (caller == FreeSlotCaller::Dequeue) { |
| 274 | // If we're calling this from dequeue, prefer free buffers |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 275 | int slot = getFreeBufferLocked(); |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 276 | if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 277 | *found = slot; |
| 278 | } else if (mCore->mAllowAllocation) { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 279 | *found = getFreeSlotLocked(); |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 280 | } |
| 281 | } else { |
| 282 | // If we're calling this from attach, prefer free slots |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 283 | int slot = getFreeSlotLocked(); |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 284 | if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 285 | *found = slot; |
| 286 | } else { |
| 287 | *found = getFreeBufferLocked(); |
| 288 | } |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // If no buffer is found, or if the queue has too many buffers |
| 294 | // outstanding, wait for a buffer to be acquired or released, or for the |
| 295 | // max buffer count to change. |
| 296 | tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) || |
| 297 | tooManyBuffers; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 298 | if (tryAgain) { |
| 299 | // Return an error if we're in non-blocking mode (producer and |
| 300 | // consumer are controlled by the application). |
| 301 | // However, the consumer is allowed to briefly acquire an extra |
| 302 | // buffer (which could cause us to have to wait here), which is |
| 303 | // okay, since it is only used to implement an atomic acquire + |
| 304 | // release (e.g., in GLConsumer::updateTexImage()) |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 305 | if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) && |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 306 | (acquiredCount <= mCore->mMaxAcquiredBufferCount)) { |
| 307 | return WOULD_BLOCK; |
| 308 | } |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 309 | if (mDequeueTimeout >= 0) { |
| 310 | status_t result = mCore->mDequeueCondition.waitRelative( |
| 311 | mCore->mMutex, mDequeueTimeout); |
| 312 | if (result == TIMED_OUT) { |
| 313 | return result; |
| 314 | } |
| 315 | } else { |
| 316 | mCore->mDequeueCondition.wait(mCore->mMutex); |
| 317 | } |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 318 | } |
| 319 | } // while (tryAgain) |
| 320 | |
| 321 | return NO_ERROR; |
| 322 | } |
| 323 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 324 | status_t BufferQueueProducer::dequeueBuffer(int *outSlot, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 325 | sp<android::Fence> *outFence, uint32_t width, uint32_t height, |
| 326 | PixelFormat format, uint32_t usage) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 327 | ATRACE_CALL(); |
| 328 | { // Autolock scope |
| 329 | Mutex::Autolock lock(mCore->mMutex); |
| 330 | mConsumerName = mCore->mConsumerName; |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 331 | |
| 332 | if (mCore->mIsAbandoned) { |
| 333 | BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned"); |
| 334 | return NO_INIT; |
| 335 | } |
| 336 | |
| 337 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 338 | BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer"); |
| 339 | return NO_INIT; |
| 340 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 341 | } // Autolock scope |
| 342 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 343 | BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height, |
| 344 | format, usage); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 345 | |
| 346 | if ((width && !height) || (!width && height)) { |
| 347 | BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height); |
| 348 | return BAD_VALUE; |
| 349 | } |
| 350 | |
| 351 | status_t returnFlags = NO_ERROR; |
| 352 | EGLDisplay eglDisplay = EGL_NO_DISPLAY; |
| 353 | EGLSyncKHR eglFence = EGL_NO_SYNC_KHR; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 354 | bool attachedByConsumer = false; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 355 | |
| 356 | { // Autolock scope |
| 357 | Mutex::Autolock lock(mCore->mMutex); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 358 | mCore->waitWhileAllocatingLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 359 | |
| 360 | if (format == 0) { |
| 361 | format = mCore->mDefaultBufferFormat; |
| 362 | } |
| 363 | |
| 364 | // Enable the usage bits the consumer requested |
| 365 | usage |= mCore->mConsumerUsageBits; |
| 366 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 367 | const bool useDefaultSize = !width && !height; |
| 368 | if (useDefaultSize) { |
| 369 | width = mCore->mDefaultWidth; |
| 370 | height = mCore->mDefaultHeight; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 371 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 372 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 373 | int found = BufferItem::INVALID_BUFFER_SLOT; |
| 374 | while (found == BufferItem::INVALID_BUFFER_SLOT) { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 375 | status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 376 | &found); |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 377 | if (status != NO_ERROR) { |
| 378 | return status; |
| 379 | } |
| 380 | |
| 381 | // This should not happen |
| 382 | if (found == BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 383 | BQ_LOGE("dequeueBuffer: no available buffer slots"); |
| 384 | return -EBUSY; |
| 385 | } |
| 386 | |
| 387 | const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer); |
| 388 | |
| 389 | // If we are not allowed to allocate new buffers, |
| 390 | // waitForFreeSlotThenRelock must have returned a slot containing a |
| 391 | // buffer. If this buffer would require reallocation to meet the |
| 392 | // requested attributes, we free it and attempt to get another one. |
| 393 | if (!mCore->mAllowAllocation) { |
| 394 | if (buffer->needsReallocation(width, height, format, usage)) { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 395 | if (mCore->mSingleBufferSlot == found) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 396 | BQ_LOGE("dequeueBuffer: cannot re-allocate a shared" |
| 397 | "buffer"); |
| 398 | return BAD_VALUE; |
| 399 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 400 | mCore->mFreeSlots.insert(found); |
| 401 | mCore->clearBufferSlotLocked(found); |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 402 | found = BufferItem::INVALID_BUFFER_SLOT; |
| 403 | continue; |
| 404 | } |
| 405 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 408 | const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer); |
| 409 | if (mCore->mSingleBufferSlot == found && |
| 410 | buffer->needsReallocation(width, height, format, usage)) { |
| 411 | BQ_LOGE("dequeueBuffer: cannot re-allocate a shared" |
| 412 | "buffer"); |
| 413 | |
| 414 | return BAD_VALUE; |
| 415 | } |
| 416 | |
| 417 | if (mCore->mSingleBufferSlot != found) { |
| 418 | mCore->mActiveBuffers.insert(found); |
| 419 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 420 | *outSlot = found; |
| 421 | ATRACE_BUFFER_INDEX(found); |
| 422 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 423 | attachedByConsumer = mSlots[found].mNeedsReallocation; |
| 424 | mSlots[found].mNeedsReallocation = false; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 425 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 426 | mSlots[found].mBufferState.dequeue(); |
| 427 | |
| 428 | // If single buffer mode has just been enabled, cache the slot of the |
| 429 | // first buffer that is dequeued and mark it as the shared buffer. |
| 430 | if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot == |
| 431 | BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 432 | mCore->mSingleBufferSlot = found; |
| 433 | mSlots[found].mBufferState.mShared = true; |
| 434 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 435 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 436 | if ((buffer == NULL) || |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 437 | buffer->needsReallocation(width, height, format, usage)) |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 438 | { |
| 439 | mSlots[found].mAcquireCalled = false; |
| 440 | mSlots[found].mGraphicBuffer = NULL; |
| 441 | mSlots[found].mRequestBufferCalled = false; |
| 442 | mSlots[found].mEglDisplay = EGL_NO_DISPLAY; |
| 443 | mSlots[found].mEglFence = EGL_NO_SYNC_KHR; |
| 444 | mSlots[found].mFence = Fence::NO_FENCE; |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 445 | mCore->mBufferAge = 0; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 446 | mCore->mIsAllocating = true; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 447 | |
| 448 | returnFlags |= BUFFER_NEEDS_REALLOCATION; |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 449 | } else { |
| 450 | // We add 1 because that will be the frame number when this buffer |
| 451 | // is queued |
| 452 | mCore->mBufferAge = |
| 453 | mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Dan Stoza | 800b41a | 2015-04-28 14:20:04 -0700 | [diff] [blame] | 456 | BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64, |
| 457 | mCore->mBufferAge); |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 458 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 459 | if (CC_UNLIKELY(mSlots[found].mFence == NULL)) { |
| 460 | BQ_LOGE("dequeueBuffer: about to return a NULL fence - " |
| 461 | "slot=%d w=%d h=%d format=%u", |
| 462 | found, buffer->width, buffer->height, buffer->format); |
| 463 | } |
| 464 | |
| 465 | eglDisplay = mSlots[found].mEglDisplay; |
| 466 | eglFence = mSlots[found].mEglFence; |
| 467 | *outFence = mSlots[found].mFence; |
| 468 | mSlots[found].mEglFence = EGL_NO_SYNC_KHR; |
| 469 | mSlots[found].mFence = Fence::NO_FENCE; |
| 470 | } // Autolock scope |
| 471 | |
| 472 | if (returnFlags & BUFFER_NEEDS_REALLOCATION) { |
| 473 | status_t error; |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 474 | BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 475 | sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer( |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 476 | width, height, format, usage, &error)); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 477 | { // Autolock scope |
| 478 | Mutex::Autolock lock(mCore->mMutex); |
| 479 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 480 | if (graphicBuffer != NULL && !mCore->mIsAbandoned) { |
| 481 | graphicBuffer->setGenerationNumber(mCore->mGenerationNumber); |
| 482 | mSlots[*outSlot].mGraphicBuffer = graphicBuffer; |
| 483 | } |
| 484 | |
| 485 | mCore->mIsAllocating = false; |
| 486 | mCore->mIsAllocatingCondition.broadcast(); |
| 487 | |
| 488 | if (graphicBuffer == NULL) { |
| 489 | BQ_LOGE("dequeueBuffer: createGraphicBuffer failed"); |
| 490 | return error; |
| 491 | } |
| 492 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 493 | if (mCore->mIsAbandoned) { |
| 494 | BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned"); |
| 495 | return NO_INIT; |
| 496 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 497 | |
| 498 | mCore->validateConsistencyLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 499 | } // Autolock scope |
| 500 | } |
| 501 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 502 | if (attachedByConsumer) { |
| 503 | returnFlags |= BUFFER_NEEDS_REALLOCATION; |
| 504 | } |
| 505 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 506 | if (eglFence != EGL_NO_SYNC_KHR) { |
| 507 | EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0, |
| 508 | 1000000000); |
| 509 | // If something goes wrong, log the error, but return the buffer without |
| 510 | // synchronizing access to it. It's too late at this point to abort the |
| 511 | // dequeue operation. |
| 512 | if (result == EGL_FALSE) { |
| 513 | BQ_LOGE("dequeueBuffer: error %#x waiting for fence", |
| 514 | eglGetError()); |
| 515 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 516 | BQ_LOGE("dequeueBuffer: timeout waiting for fence"); |
| 517 | } |
| 518 | eglDestroySyncKHR(eglDisplay, eglFence); |
| 519 | } |
| 520 | |
Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 521 | BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x", |
| 522 | *outSlot, |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 523 | mSlots[*outSlot].mFrameNumber, |
| 524 | mSlots[*outSlot].mGraphicBuffer->handle, returnFlags); |
| 525 | |
| 526 | return returnFlags; |
| 527 | } |
| 528 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 529 | status_t BufferQueueProducer::detachBuffer(int slot) { |
| 530 | ATRACE_CALL(); |
| 531 | ATRACE_BUFFER_INDEX(slot); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 532 | BQ_LOGV("detachBuffer: slot %d", slot); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 533 | Mutex::Autolock lock(mCore->mMutex); |
| 534 | |
| 535 | if (mCore->mIsAbandoned) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 536 | BQ_LOGE("detachBuffer: BufferQueue has been abandoned"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 537 | return NO_INIT; |
| 538 | } |
| 539 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 540 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 541 | BQ_LOGE("detachBuffer: BufferQueue has no connected producer"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 542 | return NO_INIT; |
| 543 | } |
| 544 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 545 | if (mCore->mSingleBufferMode || mCore->mSingleBufferSlot == slot) { |
| 546 | BQ_LOGE("detachBuffer: cannot detach a buffer in single buffer mode"); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 547 | return BAD_VALUE; |
| 548 | } |
| 549 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 550 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 551 | BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)", |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 552 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); |
| 553 | return BAD_VALUE; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 554 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
| 555 | BQ_LOGE("detachBuffer: slot %d is not owned by the producer " |
| 556 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 557 | return BAD_VALUE; |
| 558 | } else if (!mSlots[slot].mRequestBufferCalled) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 559 | BQ_LOGE("detachBuffer: buffer in slot %d has not been requested", |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 560 | slot); |
| 561 | return BAD_VALUE; |
| 562 | } |
| 563 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 564 | mSlots[slot].mBufferState.detachProducer(); |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 565 | mCore->mActiveBuffers.erase(slot); |
| 566 | mCore->mFreeSlots.insert(slot); |
| 567 | mCore->clearBufferSlotLocked(slot); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 568 | mCore->mDequeueCondition.broadcast(); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 569 | mCore->validateConsistencyLocked(); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 570 | |
| 571 | return NO_ERROR; |
| 572 | } |
| 573 | |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 574 | status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer, |
| 575 | sp<Fence>* outFence) { |
| 576 | ATRACE_CALL(); |
| 577 | |
| 578 | if (outBuffer == NULL) { |
| 579 | BQ_LOGE("detachNextBuffer: outBuffer must not be NULL"); |
| 580 | return BAD_VALUE; |
| 581 | } else if (outFence == NULL) { |
| 582 | BQ_LOGE("detachNextBuffer: outFence must not be NULL"); |
| 583 | return BAD_VALUE; |
| 584 | } |
| 585 | |
| 586 | Mutex::Autolock lock(mCore->mMutex); |
| 587 | |
| 588 | if (mCore->mIsAbandoned) { |
| 589 | BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned"); |
| 590 | return NO_INIT; |
| 591 | } |
| 592 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 593 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 594 | BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer"); |
| 595 | return NO_INIT; |
| 596 | } |
| 597 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 598 | if (mCore->mSingleBufferMode) { |
| 599 | BQ_LOGE("detachNextBuffer: cannot detach a buffer in single buffer" |
| 600 | "mode"); |
| 601 | return BAD_VALUE; |
| 602 | } |
| 603 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 604 | mCore->waitWhileAllocatingLocked(); |
| 605 | |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 606 | if (mCore->mFreeBuffers.empty()) { |
Dan Stoza | 1fc9cc2 | 2015-04-22 18:57:39 +0000 | [diff] [blame] | 607 | return NO_MEMORY; |
| 608 | } |
Dan Stoza | 8dddc99 | 2015-04-16 15:39:18 -0700 | [diff] [blame] | 609 | |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 610 | int found = mCore->mFreeBuffers.front(); |
| 611 | mCore->mFreeBuffers.remove(found); |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 612 | mCore->mFreeSlots.insert(found); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 613 | |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 614 | BQ_LOGV("detachNextBuffer detached slot %d", found); |
| 615 | |
| 616 | *outBuffer = mSlots[found].mGraphicBuffer; |
| 617 | *outFence = mSlots[found].mFence; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 618 | mCore->clearBufferSlotLocked(found); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 619 | mCore->validateConsistencyLocked(); |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 620 | |
| 621 | return NO_ERROR; |
| 622 | } |
| 623 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 624 | status_t BufferQueueProducer::attachBuffer(int* outSlot, |
| 625 | const sp<android::GraphicBuffer>& buffer) { |
| 626 | ATRACE_CALL(); |
| 627 | |
| 628 | if (outSlot == NULL) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 629 | BQ_LOGE("attachBuffer: outSlot must not be NULL"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 630 | return BAD_VALUE; |
| 631 | } else if (buffer == NULL) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 632 | BQ_LOGE("attachBuffer: cannot attach NULL buffer"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 633 | return BAD_VALUE; |
| 634 | } |
| 635 | |
| 636 | Mutex::Autolock lock(mCore->mMutex); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 637 | |
| 638 | if (mCore->mIsAbandoned) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 639 | BQ_LOGE("attachBuffer: BufferQueue has been abandoned"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 640 | return NO_INIT; |
| 641 | } |
| 642 | |
| 643 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 644 | BQ_LOGE("attachBuffer: BufferQueue has no connected producer"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 645 | return NO_INIT; |
| 646 | } |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 647 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 648 | if (mCore->mSingleBufferMode) { |
Craig Donner | 05249fc | 2016-01-15 19:33:55 -0800 | [diff] [blame] | 649 | BQ_LOGE("attachBuffer: cannot attach a buffer in single buffer mode"); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 650 | return BAD_VALUE; |
| 651 | } |
| 652 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 653 | if (buffer->getGenerationNumber() != mCore->mGenerationNumber) { |
| 654 | BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] " |
| 655 | "[queue %u]", buffer->getGenerationNumber(), |
| 656 | mCore->mGenerationNumber); |
| 657 | return BAD_VALUE; |
| 658 | } |
| 659 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 660 | mCore->waitWhileAllocatingLocked(); |
| 661 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 662 | status_t returnFlags = NO_ERROR; |
| 663 | int found; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 664 | status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 665 | if (status != NO_ERROR) { |
| 666 | return status; |
| 667 | } |
| 668 | |
| 669 | // This should not happen |
| 670 | if (found == BufferQueueCore::INVALID_BUFFER_SLOT) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 671 | BQ_LOGE("attachBuffer: no available buffer slots"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 672 | return -EBUSY; |
| 673 | } |
| 674 | |
| 675 | *outSlot = found; |
| 676 | ATRACE_BUFFER_INDEX(*outSlot); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 677 | BQ_LOGV("attachBuffer: returning slot %d flags=%#x", |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 678 | *outSlot, returnFlags); |
| 679 | |
| 680 | mSlots[*outSlot].mGraphicBuffer = buffer; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 681 | mSlots[*outSlot].mBufferState.attachProducer(); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 682 | mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR; |
| 683 | mSlots[*outSlot].mFence = Fence::NO_FENCE; |
Dan Stoza | 2443c79 | 2014-03-24 15:03:46 -0700 | [diff] [blame] | 684 | mSlots[*outSlot].mRequestBufferCalled = true; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 685 | mSlots[*outSlot].mAcquireCalled = false; |
| 686 | mCore->mActiveBuffers.insert(found); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 687 | mCore->validateConsistencyLocked(); |
| 688 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 689 | return returnFlags; |
| 690 | } |
| 691 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 692 | status_t BufferQueueProducer::queueBuffer(int slot, |
| 693 | const QueueBufferInput &input, QueueBufferOutput *output) { |
| 694 | ATRACE_CALL(); |
| 695 | ATRACE_BUFFER_INDEX(slot); |
| 696 | |
| 697 | int64_t timestamp; |
| 698 | bool isAutoTimestamp; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 699 | android_dataspace dataSpace; |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 700 | Rect crop(Rect::EMPTY_RECT); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 701 | int scalingMode; |
| 702 | uint32_t transform; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 703 | uint32_t stickyTransform; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 704 | sp<Fence> fence; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 705 | input.deflate(×tamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 706 | &transform, &fence, &stickyTransform); |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 707 | Region surfaceDamage = input.getSurfaceDamage(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 708 | |
| 709 | if (fence == NULL) { |
| 710 | BQ_LOGE("queueBuffer: fence is NULL"); |
Jesse Hall | de288fe | 2014-11-04 08:30:48 -0800 | [diff] [blame] | 711 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | switch (scalingMode) { |
| 715 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 716 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 717 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: |
| 718 | case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: |
| 719 | break; |
| 720 | default: |
| 721 | BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 722 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 723 | } |
| 724 | |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 725 | sp<IConsumerListener> frameAvailableListener; |
| 726 | sp<IConsumerListener> frameReplacedListener; |
| 727 | int callbackTicket = 0; |
| 728 | BufferItem item; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 729 | { // Autolock scope |
| 730 | Mutex::Autolock lock(mCore->mMutex); |
| 731 | |
| 732 | if (mCore->mIsAbandoned) { |
| 733 | BQ_LOGE("queueBuffer: BufferQueue has been abandoned"); |
| 734 | return NO_INIT; |
| 735 | } |
| 736 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 737 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 738 | BQ_LOGE("queueBuffer: BufferQueue has no connected producer"); |
| 739 | return NO_INIT; |
| 740 | } |
| 741 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 742 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 743 | BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)", |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 744 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 745 | return BAD_VALUE; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 746 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 747 | BQ_LOGE("queueBuffer: slot %d is not owned by the producer " |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 748 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 749 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 750 | } else if (!mSlots[slot].mRequestBufferCalled) { |
| 751 | BQ_LOGE("queueBuffer: slot %d was queued without requesting " |
| 752 | "a buffer", slot); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 753 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 754 | } |
| 755 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 756 | BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d" |
Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 757 | " crop=[%d,%d,%d,%d] transform=%#x scale=%s", |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 758 | slot, mCore->mFrameCounter + 1, timestamp, dataSpace, |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 759 | crop.left, crop.top, crop.right, crop.bottom, transform, |
| 760 | BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode))); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 761 | |
| 762 | const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer); |
| 763 | Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight()); |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 764 | Rect croppedRect(Rect::EMPTY_RECT); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 765 | crop.intersect(bufferRect, &croppedRect); |
| 766 | if (croppedRect != crop) { |
| 767 | BQ_LOGE("queueBuffer: crop rect is not contained within the " |
| 768 | "buffer in slot %d", slot); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 769 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 770 | } |
| 771 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 772 | // Override UNKNOWN dataspace with consumer default |
| 773 | if (dataSpace == HAL_DATASPACE_UNKNOWN) { |
| 774 | dataSpace = mCore->mDefaultBufferDataSpace; |
| 775 | } |
| 776 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 777 | mSlots[slot].mFence = fence; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 778 | mSlots[slot].mBufferState.queue(); |
| 779 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 780 | ++mCore->mFrameCounter; |
| 781 | mSlots[slot].mFrameNumber = mCore->mFrameCounter; |
| 782 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 783 | item.mAcquireCalled = mSlots[slot].mAcquireCalled; |
| 784 | item.mGraphicBuffer = mSlots[slot].mGraphicBuffer; |
| 785 | item.mCrop = crop; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 786 | item.mTransform = transform & |
| 787 | ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 788 | item.mTransformToDisplayInverse = |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 789 | (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0; |
| 790 | item.mScalingMode = static_cast<uint32_t>(scalingMode); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 791 | item.mTimestamp = timestamp; |
| 792 | item.mIsAutoTimestamp = isAutoTimestamp; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 793 | item.mDataSpace = dataSpace; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 794 | item.mFrameNumber = mCore->mFrameCounter; |
| 795 | item.mSlot = slot; |
| 796 | item.mFence = fence; |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 797 | item.mIsDroppable = mCore->mAsyncMode || |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 798 | mCore->mDequeueBufferCannotBlock || |
| 799 | (mCore->mSingleBufferMode && mCore->mSingleBufferSlot == slot); |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 800 | item.mSurfaceDamage = surfaceDamage; |
Pablo Ceballos | 0631218 | 2015-10-07 16:32:12 -0700 | [diff] [blame] | 801 | item.mSingleBufferMode = mCore->mSingleBufferMode; |
| 802 | item.mQueuedBuffer = true; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 803 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 804 | mStickyTransform = stickyTransform; |
| 805 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 806 | // Cache the shared buffer data so that the BufferItem can be recreated. |
| 807 | if (mCore->mSingleBufferMode) { |
| 808 | mCore->mSingleBufferCache.crop = crop; |
| 809 | mCore->mSingleBufferCache.transform = transform; |
| 810 | mCore->mSingleBufferCache.scalingMode = static_cast<uint32_t>( |
| 811 | scalingMode); |
| 812 | mCore->mSingleBufferCache.dataspace = dataSpace; |
| 813 | } |
| 814 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 815 | if (mCore->mQueue.empty()) { |
| 816 | // When the queue is empty, we can ignore mDequeueBufferCannotBlock |
| 817 | // and simply queue this buffer |
| 818 | mCore->mQueue.push_back(item); |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 819 | frameAvailableListener = mCore->mConsumerListener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 820 | } else { |
| 821 | // When the queue is not empty, we need to look at the front buffer |
| 822 | // state to see if we need to replace it |
| 823 | BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin()); |
| 824 | if (front->mIsDroppable) { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 825 | |
| 826 | if (!front->mIsStale) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 827 | mSlots[front->mSlot].mBufferState.freeQueued(); |
| 828 | |
| 829 | // After leaving single buffer mode, the shared buffer will |
| 830 | // still be around. Mark it as no longer shared if this |
| 831 | // operation causes it to be free. |
| 832 | if (!mCore->mSingleBufferMode && |
| 833 | mSlots[front->mSlot].mBufferState.isFree()) { |
| 834 | mSlots[front->mSlot].mBufferState.mShared = false; |
| 835 | } |
| 836 | // Don't put the shared buffer on the free list. |
| 837 | if (!mSlots[front->mSlot].mBufferState.isShared()) { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 838 | mCore->mActiveBuffers.erase(front->mSlot); |
| 839 | mCore->mFreeBuffers.push_back(front->mSlot); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 840 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 841 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 842 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 843 | // Overwrite the droppable buffer with the incoming one |
| 844 | *front = item; |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 845 | frameReplacedListener = mCore->mConsumerListener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 846 | } else { |
| 847 | mCore->mQueue.push_back(item); |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 848 | frameAvailableListener = mCore->mConsumerListener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
| 852 | mCore->mBufferHasBeenQueued = true; |
| 853 | mCore->mDequeueCondition.broadcast(); |
| 854 | |
| 855 | output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight, |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 856 | mCore->mTransformHint, |
| 857 | static_cast<uint32_t>(mCore->mQueue.size())); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 858 | |
| 859 | ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size()); |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 860 | |
| 861 | // Take a ticket for the callback functions |
| 862 | callbackTicket = mNextCallbackTicket++; |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 863 | |
| 864 | mCore->validateConsistencyLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 865 | } // Autolock scope |
| 866 | |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 867 | // Don't send the GraphicBuffer through the callback, and don't send |
| 868 | // the slot number, since the consumer shouldn't need it |
| 869 | item.mGraphicBuffer.clear(); |
| 870 | item.mSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 871 | |
| 872 | // Call back without the main BufferQueue lock held, but with the callback |
| 873 | // lock held so we can ensure that callbacks occur in order |
| 874 | { |
| 875 | Mutex::Autolock lock(mCallbackMutex); |
| 876 | while (callbackTicket != mCurrentCallbackTicket) { |
| 877 | mCallbackCondition.wait(mCallbackMutex); |
| 878 | } |
| 879 | |
| 880 | if (frameAvailableListener != NULL) { |
| 881 | frameAvailableListener->onFrameAvailable(item); |
| 882 | } else if (frameReplacedListener != NULL) { |
| 883 | frameReplacedListener->onFrameReplaced(item); |
| 884 | } |
| 885 | |
| 886 | ++mCurrentCallbackTicket; |
| 887 | mCallbackCondition.broadcast(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 888 | } |
| 889 | |
Christian Poetzsch | 82fbb12 | 2015-12-07 13:36:22 +0000 | [diff] [blame] | 890 | // Wait without lock held |
| 891 | if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) { |
| 892 | // Waiting here allows for two full buffers to be queued but not a |
| 893 | // third. In the event that frames take varying time, this makes a |
| 894 | // small trade-off in favor of latency rather than throughput. |
| 895 | mLastQueueBufferFence->waitForever("Throttling EGL Production"); |
| 896 | mLastQueueBufferFence = fence; |
| 897 | } |
| 898 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 899 | return NO_ERROR; |
| 900 | } |
| 901 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 902 | status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 903 | ATRACE_CALL(); |
| 904 | BQ_LOGV("cancelBuffer: slot %d", slot); |
| 905 | Mutex::Autolock lock(mCore->mMutex); |
| 906 | |
| 907 | if (mCore->mIsAbandoned) { |
| 908 | BQ_LOGE("cancelBuffer: BufferQueue has been abandoned"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 909 | return NO_INIT; |
| 910 | } |
| 911 | |
| 912 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 913 | BQ_LOGE("cancelBuffer: BufferQueue has no connected producer"); |
| 914 | return NO_INIT; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 915 | } |
| 916 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 917 | if (mCore->mSingleBufferMode) { |
| 918 | BQ_LOGE("cancelBuffer: cannot cancel a buffer in single buffer mode"); |
| 919 | return BAD_VALUE; |
| 920 | } |
| 921 | |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 922 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 923 | BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 924 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 925 | return BAD_VALUE; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 926 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 927 | BQ_LOGE("cancelBuffer: slot %d is not owned by the producer " |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 928 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 929 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 930 | } else if (fence == NULL) { |
| 931 | BQ_LOGE("cancelBuffer: fence is NULL"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 932 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 933 | } |
| 934 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 935 | mSlots[slot].mBufferState.cancel(); |
| 936 | |
| 937 | // After leaving single buffer mode, the shared buffer will still be around. |
| 938 | // Mark it as no longer shared if this operation causes it to be free. |
| 939 | if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) { |
| 940 | mSlots[slot].mBufferState.mShared = false; |
| 941 | } |
| 942 | |
| 943 | // Don't put the shared buffer on the free list. |
| 944 | if (!mSlots[slot].mBufferState.isShared()) { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 945 | mCore->mActiveBuffers.erase(slot); |
| 946 | mCore->mFreeBuffers.push_back(slot); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 947 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 948 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 949 | mSlots[slot].mFence = fence; |
| 950 | mCore->mDequeueCondition.broadcast(); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 951 | mCore->validateConsistencyLocked(); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 952 | |
| 953 | return NO_ERROR; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | int BufferQueueProducer::query(int what, int *outValue) { |
| 957 | ATRACE_CALL(); |
| 958 | Mutex::Autolock lock(mCore->mMutex); |
| 959 | |
| 960 | if (outValue == NULL) { |
| 961 | BQ_LOGE("query: outValue was NULL"); |
| 962 | return BAD_VALUE; |
| 963 | } |
| 964 | |
| 965 | if (mCore->mIsAbandoned) { |
| 966 | BQ_LOGE("query: BufferQueue has been abandoned"); |
| 967 | return NO_INIT; |
| 968 | } |
| 969 | |
| 970 | int value; |
| 971 | switch (what) { |
| 972 | case NATIVE_WINDOW_WIDTH: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 973 | value = static_cast<int32_t>(mCore->mDefaultWidth); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 974 | break; |
| 975 | case NATIVE_WINDOW_HEIGHT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 976 | value = static_cast<int32_t>(mCore->mDefaultHeight); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 977 | break; |
| 978 | case NATIVE_WINDOW_FORMAT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 979 | value = static_cast<int32_t>(mCore->mDefaultBufferFormat); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 980 | break; |
| 981 | case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS: |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 982 | value = mCore->getMinUndequeuedBufferCountLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 983 | break; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 984 | case NATIVE_WINDOW_STICKY_TRANSFORM: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 985 | value = static_cast<int32_t>(mStickyTransform); |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 986 | break; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 987 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: |
| 988 | value = (mCore->mQueue.size() > 1); |
| 989 | break; |
| 990 | case NATIVE_WINDOW_CONSUMER_USAGE_BITS: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 991 | value = static_cast<int32_t>(mCore->mConsumerUsageBits); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 992 | break; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 993 | case NATIVE_WINDOW_DEFAULT_DATASPACE: |
| 994 | value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace); |
| 995 | break; |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 996 | case NATIVE_WINDOW_BUFFER_AGE: |
| 997 | if (mCore->mBufferAge > INT32_MAX) { |
| 998 | value = 0; |
| 999 | } else { |
| 1000 | value = static_cast<int32_t>(mCore->mBufferAge); |
| 1001 | } |
| 1002 | break; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1003 | default: |
| 1004 | return BAD_VALUE; |
| 1005 | } |
| 1006 | |
| 1007 | BQ_LOGV("query: %d? %d", what, value); |
| 1008 | *outValue = value; |
| 1009 | return NO_ERROR; |
| 1010 | } |
| 1011 | |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1012 | status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener, |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1013 | int api, bool producerControlledByApp, QueueBufferOutput *output) { |
| 1014 | ATRACE_CALL(); |
| 1015 | Mutex::Autolock lock(mCore->mMutex); |
| 1016 | mConsumerName = mCore->mConsumerName; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1017 | BQ_LOGV("connect: api=%d producerControlledByApp=%s", api, |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1018 | producerControlledByApp ? "true" : "false"); |
| 1019 | |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1020 | if (mCore->mIsAbandoned) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1021 | BQ_LOGE("connect: BufferQueue has been abandoned"); |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1022 | return NO_INIT; |
| 1023 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1024 | |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1025 | if (mCore->mConsumerListener == NULL) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1026 | BQ_LOGE("connect: BufferQueue has no consumer"); |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1027 | return NO_INIT; |
| 1028 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1029 | |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1030 | if (output == NULL) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1031 | BQ_LOGE("connect: output was NULL"); |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1032 | return BAD_VALUE; |
| 1033 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1034 | |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1035 | if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1036 | BQ_LOGE("connect: already connected (cur=%d req=%d)", |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1037 | mCore->mConnectedApi, api); |
| 1038 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 1041 | int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, |
| 1042 | mDequeueTimeout < 0 ? |
| 1043 | mCore->mConsumerControlledByApp && producerControlledByApp : false, |
| 1044 | mCore->mMaxBufferCount) - |
| 1045 | mCore->getMaxBufferCountLocked(); |
| 1046 | if (!mCore->adjustAvailableSlotsLocked(delta)) { |
| 1047 | BQ_LOGE("connect: BufferQueue failed to adjust the number of available " |
| 1048 | "slots. Delta = %d", delta); |
| 1049 | return BAD_VALUE; |
| 1050 | } |
| 1051 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1052 | int status = NO_ERROR; |
| 1053 | switch (api) { |
| 1054 | case NATIVE_WINDOW_API_EGL: |
| 1055 | case NATIVE_WINDOW_API_CPU: |
| 1056 | case NATIVE_WINDOW_API_MEDIA: |
| 1057 | case NATIVE_WINDOW_API_CAMERA: |
| 1058 | mCore->mConnectedApi = api; |
| 1059 | output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight, |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1060 | mCore->mTransformHint, |
| 1061 | static_cast<uint32_t>(mCore->mQueue.size())); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1062 | |
| 1063 | // Set up a death notification so that we can disconnect |
| 1064 | // automatically if the remote producer dies |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1065 | if (listener != NULL && |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 1066 | IInterface::asBinder(listener)->remoteBinder() != NULL) { |
| 1067 | status = IInterface::asBinder(listener)->linkToDeath( |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1068 | static_cast<IBinder::DeathRecipient*>(this)); |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1069 | if (status != NO_ERROR) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1070 | BQ_LOGE("connect: linkToDeath failed: %s (%d)", |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1071 | strerror(-status), status); |
| 1072 | } |
| 1073 | } |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1074 | mCore->mConnectedProducerListener = listener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1075 | break; |
| 1076 | default: |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1077 | BQ_LOGE("connect: unknown API %d", api); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1078 | status = BAD_VALUE; |
| 1079 | break; |
| 1080 | } |
| 1081 | |
| 1082 | mCore->mBufferHasBeenQueued = false; |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1083 | mCore->mDequeueBufferCannotBlock = false; |
| 1084 | if (mDequeueTimeout < 0) { |
| 1085 | mCore->mDequeueBufferCannotBlock = |
| 1086 | mCore->mConsumerControlledByApp && producerControlledByApp; |
| 1087 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1088 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 1089 | mCore->mAllowAllocation = true; |
| 1090 | mCore->validateConsistencyLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1091 | return status; |
| 1092 | } |
| 1093 | |
| 1094 | status_t BufferQueueProducer::disconnect(int api) { |
| 1095 | ATRACE_CALL(); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1096 | BQ_LOGV("disconnect: api %d", api); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1097 | |
| 1098 | int status = NO_ERROR; |
| 1099 | sp<IConsumerListener> listener; |
| 1100 | { // Autolock scope |
| 1101 | Mutex::Autolock lock(mCore->mMutex); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1102 | mCore->waitWhileAllocatingLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1103 | |
| 1104 | if (mCore->mIsAbandoned) { |
| 1105 | // It's not really an error to disconnect after the surface has |
| 1106 | // been abandoned; it should just be a no-op. |
| 1107 | return NO_ERROR; |
| 1108 | } |
| 1109 | |
| 1110 | switch (api) { |
| 1111 | case NATIVE_WINDOW_API_EGL: |
| 1112 | case NATIVE_WINDOW_API_CPU: |
| 1113 | case NATIVE_WINDOW_API_MEDIA: |
| 1114 | case NATIVE_WINDOW_API_CAMERA: |
| 1115 | if (mCore->mConnectedApi == api) { |
| 1116 | mCore->freeAllBuffersLocked(); |
| 1117 | |
| 1118 | // Remove our death notification callback if we have one |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1119 | if (mCore->mConnectedProducerListener != NULL) { |
| 1120 | sp<IBinder> token = |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 1121 | IInterface::asBinder(mCore->mConnectedProducerListener); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1122 | // This can fail if we're here because of the death |
| 1123 | // notification, but we just ignore it |
| 1124 | token->unlinkToDeath( |
| 1125 | static_cast<IBinder::DeathRecipient*>(this)); |
| 1126 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 1127 | mCore->mSingleBufferSlot = |
| 1128 | BufferQueueCore::INVALID_BUFFER_SLOT; |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1129 | mCore->mConnectedProducerListener = NULL; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1130 | mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API; |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1131 | mCore->mSidebandStream.clear(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1132 | mCore->mDequeueCondition.broadcast(); |
| 1133 | listener = mCore->mConsumerListener; |
Amith Dsouza | 4f21a4c | 2015-06-30 22:54:16 -0700 | [diff] [blame] | 1134 | } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1135 | BQ_LOGE("disconnect: still connected to another API " |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1136 | "(cur=%d req=%d)", mCore->mConnectedApi, api); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 1137 | status = BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1138 | } |
| 1139 | break; |
| 1140 | default: |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1141 | BQ_LOGE("disconnect: unknown API %d", api); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 1142 | status = BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1143 | break; |
| 1144 | } |
| 1145 | } // Autolock scope |
| 1146 | |
| 1147 | // Call back without lock held |
| 1148 | if (listener != NULL) { |
| 1149 | listener->onBuffersReleased(); |
| 1150 | } |
| 1151 | |
| 1152 | return status; |
| 1153 | } |
| 1154 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1155 | status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) { |
Wonsik Kim | afe3081 | 2014-03-31 23:16:08 +0900 | [diff] [blame] | 1156 | sp<IConsumerListener> listener; |
| 1157 | { // Autolock scope |
| 1158 | Mutex::Autolock _l(mCore->mMutex); |
| 1159 | mCore->mSidebandStream = stream; |
| 1160 | listener = mCore->mConsumerListener; |
| 1161 | } // Autolock scope |
| 1162 | |
| 1163 | if (listener != NULL) { |
| 1164 | listener->onSidebandStreamChanged(); |
| 1165 | } |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1166 | return NO_ERROR; |
| 1167 | } |
| 1168 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 1169 | void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height, |
| 1170 | PixelFormat format, uint32_t usage) { |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1171 | ATRACE_CALL(); |
| 1172 | while (true) { |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1173 | size_t newBufferCount = 0; |
| 1174 | uint32_t allocWidth = 0; |
| 1175 | uint32_t allocHeight = 0; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1176 | PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN; |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1177 | uint32_t allocUsage = 0; |
| 1178 | { // Autolock scope |
| 1179 | Mutex::Autolock lock(mCore->mMutex); |
| 1180 | mCore->waitWhileAllocatingLocked(); |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1181 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 1182 | if (!mCore->mAllowAllocation) { |
| 1183 | BQ_LOGE("allocateBuffers: allocation is not allowed for this " |
| 1184 | "BufferQueue"); |
| 1185 | return; |
| 1186 | } |
| 1187 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 1188 | newBufferCount = mCore->mFreeSlots.size(); |
| 1189 | if (newBufferCount == 0) { |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1190 | return; |
| 1191 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 1192 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1193 | allocWidth = width > 0 ? width : mCore->mDefaultWidth; |
| 1194 | allocHeight = height > 0 ? height : mCore->mDefaultHeight; |
| 1195 | allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat; |
| 1196 | allocUsage = usage | mCore->mConsumerUsageBits; |
| 1197 | |
| 1198 | mCore->mIsAllocating = true; |
| 1199 | } // Autolock scope |
| 1200 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1201 | Vector<sp<GraphicBuffer>> buffers; |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1202 | for (size_t i = 0; i < newBufferCount; ++i) { |
| 1203 | status_t result = NO_ERROR; |
| 1204 | sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer( |
| 1205 | allocWidth, allocHeight, allocFormat, allocUsage, &result)); |
| 1206 | if (result != NO_ERROR) { |
| 1207 | BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format" |
| 1208 | " %u, usage %u)", width, height, format, usage); |
| 1209 | Mutex::Autolock lock(mCore->mMutex); |
| 1210 | mCore->mIsAllocating = false; |
| 1211 | mCore->mIsAllocatingCondition.broadcast(); |
| 1212 | return; |
| 1213 | } |
| 1214 | buffers.push_back(graphicBuffer); |
| 1215 | } |
| 1216 | |
| 1217 | { // Autolock scope |
| 1218 | Mutex::Autolock lock(mCore->mMutex); |
| 1219 | uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth; |
| 1220 | uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1221 | PixelFormat checkFormat = format != 0 ? |
| 1222 | format : mCore->mDefaultBufferFormat; |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1223 | uint32_t checkUsage = usage | mCore->mConsumerUsageBits; |
| 1224 | if (checkWidth != allocWidth || checkHeight != allocHeight || |
| 1225 | checkFormat != allocFormat || checkUsage != allocUsage) { |
| 1226 | // Something changed while we released the lock. Retry. |
| 1227 | BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying."); |
| 1228 | mCore->mIsAllocating = false; |
| 1229 | mCore->mIsAllocatingCondition.broadcast(); |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1230 | continue; |
| 1231 | } |
| 1232 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1233 | for (size_t i = 0; i < newBufferCount; ++i) { |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 1234 | if (mCore->mFreeSlots.empty()) { |
| 1235 | BQ_LOGV("allocateBuffers: a slot was occupied while " |
| 1236 | "allocating. Dropping allocated buffer."); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1237 | continue; |
| 1238 | } |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 1239 | auto slot = mCore->mFreeSlots.begin(); |
| 1240 | mCore->clearBufferSlotLocked(*slot); // Clean up the slot first |
| 1241 | mSlots[*slot].mGraphicBuffer = buffers[i]; |
| 1242 | mSlots[*slot].mFence = Fence::NO_FENCE; |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 1243 | |
| 1244 | // freeBufferLocked puts this slot on the free slots list. Since |
| 1245 | // we then attached a buffer, move the slot to free buffer list. |
| 1246 | mCore->mFreeSlots.erase(slot); |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 1247 | mCore->mFreeBuffers.push_front(*slot); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 1248 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 1249 | BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", |
| 1250 | *slot); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1251 | } |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1252 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1253 | mCore->mIsAllocating = false; |
| 1254 | mCore->mIsAllocatingCondition.broadcast(); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 1255 | mCore->validateConsistencyLocked(); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1256 | } // Autolock scope |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 1260 | status_t BufferQueueProducer::allowAllocation(bool allow) { |
| 1261 | ATRACE_CALL(); |
| 1262 | BQ_LOGV("allowAllocation: %s", allow ? "true" : "false"); |
| 1263 | |
| 1264 | Mutex::Autolock lock(mCore->mMutex); |
| 1265 | mCore->mAllowAllocation = allow; |
| 1266 | return NO_ERROR; |
| 1267 | } |
| 1268 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1269 | status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) { |
| 1270 | ATRACE_CALL(); |
| 1271 | BQ_LOGV("setGenerationNumber: %u", generationNumber); |
| 1272 | |
| 1273 | Mutex::Autolock lock(mCore->mMutex); |
| 1274 | mCore->mGenerationNumber = generationNumber; |
| 1275 | return NO_ERROR; |
| 1276 | } |
| 1277 | |
Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 1278 | String8 BufferQueueProducer::getConsumerName() const { |
| 1279 | ATRACE_CALL(); |
| 1280 | BQ_LOGV("getConsumerName: %s", mConsumerName.string()); |
| 1281 | return mConsumerName; |
| 1282 | } |
| 1283 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1284 | uint64_t BufferQueueProducer::getNextFrameNumber() const { |
| 1285 | ATRACE_CALL(); |
| 1286 | |
| 1287 | Mutex::Autolock lock(mCore->mMutex); |
| 1288 | uint64_t nextFrameNumber = mCore->mFrameCounter + 1; |
| 1289 | return nextFrameNumber; |
| 1290 | } |
| 1291 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1292 | status_t BufferQueueProducer::setSingleBufferMode(bool singleBufferMode) { |
| 1293 | ATRACE_CALL(); |
| 1294 | BQ_LOGV("setSingleBufferMode: %d", singleBufferMode); |
| 1295 | |
| 1296 | Mutex::Autolock lock(mCore->mMutex); |
| 1297 | if (!singleBufferMode) { |
| 1298 | mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT; |
| 1299 | } |
| 1300 | mCore->mSingleBufferMode = singleBufferMode; |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1301 | return NO_ERROR; |
| 1302 | } |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1303 | |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1304 | status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) { |
| 1305 | ATRACE_CALL(); |
| 1306 | BQ_LOGV("setDequeueTimeout: %" PRId64, timeout); |
| 1307 | |
| 1308 | Mutex::Autolock lock(mCore->mMutex); |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 1309 | int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, false, |
| 1310 | mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked(); |
| 1311 | if (!mCore->adjustAvailableSlotsLocked(delta)) { |
| 1312 | BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of " |
| 1313 | "available slots. Delta = %d", delta); |
| 1314 | return BAD_VALUE; |
| 1315 | } |
| 1316 | |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1317 | mDequeueTimeout = timeout; |
| 1318 | mCore->mDequeueBufferCannotBlock = false; |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame^] | 1319 | mCore->validateConsistencyLocked(); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1320 | return NO_ERROR; |
| 1321 | } |
| 1322 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1323 | void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) { |
| 1324 | // If we're here, it means that a producer we were connected to died. |
| 1325 | // We're guaranteed that we are still connected to it because we remove |
| 1326 | // this callback upon disconnect. It's therefore safe to read mConnectedApi |
| 1327 | // without synchronization here. |
| 1328 | int api = mCore->mConnectedApi; |
| 1329 | disconnect(api); |
| 1330 | } |
| 1331 | |
| 1332 | } // namespace android |