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 "BufferQueueConsumer" |
| 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 | #include <gui/BufferItem.h> |
| 24 | #include <gui/BufferQueueConsumer.h> |
| 25 | #include <gui/BufferQueueCore.h> |
| 26 | #include <gui/IConsumerListener.h> |
Dan Stoza | d1c1036 | 2014-03-28 15:19:08 -0700 | [diff] [blame] | 27 | #include <gui/IProducerListener.h> |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) : |
| 32 | mCore(core), |
| 33 | mSlots(core->mSlots), |
| 34 | mConsumerName() {} |
| 35 | |
| 36 | BufferQueueConsumer::~BufferQueueConsumer() {} |
| 37 | |
| 38 | status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer, |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 39 | nsecs_t expectedPresent, uint64_t maxFrameNumber) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 40 | ATRACE_CALL(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 41 | |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 42 | int numDroppedBuffers = 0; |
| 43 | sp<IProducerListener> listener; |
| 44 | { |
| 45 | Mutex::Autolock lock(mCore->mMutex); |
| 46 | |
| 47 | // Check that the consumer doesn't currently have the maximum number of |
| 48 | // buffers acquired. We allow the max buffer count to be exceeded by one |
| 49 | // buffer so that the consumer can successfully set up the newly acquired |
| 50 | // buffer before releasing the old one. |
| 51 | int numAcquiredBuffers = 0; |
| 52 | for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) { |
| 53 | if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) { |
| 54 | ++numAcquiredBuffers; |
| 55 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 56 | } |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 57 | if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) { |
| 58 | BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)", |
| 59 | numAcquiredBuffers, mCore->mMaxAcquiredBufferCount); |
| 60 | return INVALID_OPERATION; |
| 61 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 62 | |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 63 | // Check if the queue is empty. |
| 64 | // In asynchronous mode the list is guaranteed to be one buffer deep, |
| 65 | // while in synchronous mode we use the oldest buffer. |
| 66 | if (mCore->mQueue.empty()) { |
| 67 | return NO_BUFFER_AVAILABLE; |
| 68 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 69 | |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 70 | BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin()); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 71 | |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 72 | // If expectedPresent is specified, we may not want to return a buffer yet. |
| 73 | // If it's specified and there's more than one buffer queued, we may want |
| 74 | // to drop a buffer. |
| 75 | if (expectedPresent != 0) { |
| 76 | const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 77 | |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 78 | // The 'expectedPresent' argument indicates when the buffer is expected |
| 79 | // to be presented on-screen. If the buffer's desired present time is |
| 80 | // earlier (less) than expectedPresent -- meaning it will be displayed |
| 81 | // on time or possibly late if we show it as soon as possible -- we |
| 82 | // acquire and return it. If we don't want to display it until after the |
| 83 | // expectedPresent time, we return PRESENT_LATER without acquiring it. |
| 84 | // |
| 85 | // To be safe, we don't defer acquisition if expectedPresent is more |
| 86 | // than one second in the future beyond the desired present time |
| 87 | // (i.e., we'd be holding the buffer for a long time). |
| 88 | // |
| 89 | // NOTE: Code assumes monotonic time values from the system clock |
| 90 | // are positive. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 91 | |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 92 | // Start by checking to see if we can drop frames. We skip this check if |
| 93 | // the timestamps are being auto-generated by Surface. If the app isn't |
| 94 | // generating timestamps explicitly, it probably doesn't want frames to |
| 95 | // be discarded based on them. |
| 96 | while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) { |
| 97 | const BufferItem& bufferItem(mCore->mQueue[1]); |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 98 | |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 99 | // If dropping entry[0] would leave us with a buffer that the |
| 100 | // consumer is not yet ready for, don't drop it. |
| 101 | if (maxFrameNumber && bufferItem.mFrameNumber > maxFrameNumber) { |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | // If entry[1] is timely, drop entry[0] (and repeat). We apply an |
| 106 | // additional criterion here: we only drop the earlier buffer if our |
| 107 | // desiredPresent falls within +/- 1 second of the expected present. |
| 108 | // Otherwise, bogus desiredPresent times (e.g., 0 or a small |
| 109 | // relative timestamp), which normally mean "ignore the timestamp |
| 110 | // and acquire immediately", would cause us to drop frames. |
| 111 | // |
| 112 | // We may want to add an additional criterion: don't drop the |
| 113 | // earlier buffer if entry[1]'s fence hasn't signaled yet. |
| 114 | nsecs_t desiredPresent = bufferItem.mTimestamp; |
| 115 | if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC || |
| 116 | desiredPresent > expectedPresent) { |
| 117 | // This buffer is set to display in the near future, or |
| 118 | // desiredPresent is garbage. Either way we don't want to drop |
| 119 | // the previous buffer just to get this on the screen sooner. |
| 120 | BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%" |
| 121 | PRId64 " (%" PRId64 ") now=%" PRId64, |
| 122 | desiredPresent, expectedPresent, |
| 123 | desiredPresent - expectedPresent, |
| 124 | systemTime(CLOCK_MONOTONIC)); |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64 |
| 129 | " size=%zu", |
| 130 | desiredPresent, expectedPresent, mCore->mQueue.size()); |
| 131 | if (mCore->stillTracking(front)) { |
| 132 | // Front buffer is still in mSlots, so mark the slot as free |
| 133 | mSlots[front->mSlot].mBufferState = BufferSlot::FREE; |
| 134 | mCore->mFreeBuffers.push_back(front->mSlot); |
| 135 | listener = mCore->mConnectedProducerListener; |
| 136 | ++numDroppedBuffers; |
| 137 | } |
| 138 | mCore->mQueue.erase(front); |
| 139 | front = mCore->mQueue.begin(); |
Dan Stoza | ecc5040 | 2015-04-28 14:42:06 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 142 | // See if the front buffer is ready to be acquired |
| 143 | nsecs_t desiredPresent = front->mTimestamp; |
| 144 | bool bufferIsDue = desiredPresent <= expectedPresent || |
| 145 | desiredPresent > expectedPresent + MAX_REASONABLE_NSEC; |
| 146 | bool consumerIsReady = maxFrameNumber > 0 ? |
| 147 | front->mFrameNumber <= maxFrameNumber : true; |
| 148 | if (!bufferIsDue || !consumerIsReady) { |
| 149 | BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64 |
| 150 | " (%" PRId64 ") now=%" PRId64 " frame=%" PRIu64 |
| 151 | " consumer=%" PRIu64, |
Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 152 | desiredPresent, expectedPresent, |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 153 | desiredPresent - expectedPresent, |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 154 | systemTime(CLOCK_MONOTONIC), |
| 155 | front->mFrameNumber, maxFrameNumber); |
| 156 | return PRESENT_LATER; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 159 | BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " " |
| 160 | "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent, |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 161 | desiredPresent - expectedPresent, |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 162 | systemTime(CLOCK_MONOTONIC)); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 165 | int slot = front->mSlot; |
| 166 | *outBuffer = *front; |
| 167 | ATRACE_BUFFER_INDEX(slot); |
| 168 | |
| 169 | BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }", |
| 170 | slot, front->mFrameNumber, front->mGraphicBuffer->handle); |
| 171 | // If the front buffer is still being tracked, update its slot state |
| 172 | if (mCore->stillTracking(front)) { |
| 173 | mSlots[slot].mAcquireCalled = true; |
| 174 | mSlots[slot].mNeedsCleanupOnRelease = false; |
| 175 | mSlots[slot].mBufferState = BufferSlot::ACQUIRED; |
| 176 | mSlots[slot].mFence = Fence::NO_FENCE; |
| 177 | } |
| 178 | |
| 179 | // If the buffer has previously been acquired by the consumer, set |
| 180 | // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer |
| 181 | // on the consumer side |
| 182 | if (outBuffer->mAcquireCalled) { |
| 183 | outBuffer->mGraphicBuffer = NULL; |
| 184 | } |
| 185 | |
| 186 | mCore->mQueue.erase(front); |
| 187 | |
| 188 | // We might have freed a slot while dropping old buffers, or the producer |
| 189 | // may be blocked waiting for the number of buffers in the queue to |
| 190 | // decrease. |
| 191 | mCore->mDequeueCondition.broadcast(); |
| 192 | |
| 193 | ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size()); |
| 194 | |
| 195 | mCore->validateConsistencyLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Lajos Molnar | 5f920c1 | 2015-07-13 16:04:24 -0700 | [diff] [blame] | 198 | if (listener != NULL) { |
| 199 | for (int i = 0; i < numDroppedBuffers; ++i) { |
| 200 | listener->onBufferReleased(); |
| 201 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 204 | return NO_ERROR; |
| 205 | } |
| 206 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 207 | status_t BufferQueueConsumer::detachBuffer(int slot) { |
| 208 | ATRACE_CALL(); |
| 209 | ATRACE_BUFFER_INDEX(slot); |
| 210 | BQ_LOGV("detachBuffer(C): slot %d", slot); |
| 211 | Mutex::Autolock lock(mCore->mMutex); |
| 212 | |
| 213 | if (mCore->mIsAbandoned) { |
| 214 | BQ_LOGE("detachBuffer(C): BufferQueue has been abandoned"); |
| 215 | return NO_INIT; |
| 216 | } |
| 217 | |
| 218 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
| 219 | BQ_LOGE("detachBuffer(C): slot index %d out of range [0, %d)", |
| 220 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); |
| 221 | return BAD_VALUE; |
| 222 | } else if (mSlots[slot].mBufferState != BufferSlot::ACQUIRED) { |
| 223 | BQ_LOGE("detachBuffer(C): slot %d is not owned by the consumer " |
| 224 | "(state = %d)", slot, mSlots[slot].mBufferState); |
| 225 | return BAD_VALUE; |
| 226 | } |
| 227 | |
| 228 | mCore->freeBufferLocked(slot); |
| 229 | mCore->mDequeueCondition.broadcast(); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 230 | mCore->validateConsistencyLocked(); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 231 | |
| 232 | return NO_ERROR; |
| 233 | } |
| 234 | |
| 235 | status_t BufferQueueConsumer::attachBuffer(int* outSlot, |
| 236 | const sp<android::GraphicBuffer>& buffer) { |
| 237 | ATRACE_CALL(); |
| 238 | |
| 239 | if (outSlot == NULL) { |
| 240 | BQ_LOGE("attachBuffer(P): outSlot must not be NULL"); |
| 241 | return BAD_VALUE; |
| 242 | } else if (buffer == NULL) { |
| 243 | BQ_LOGE("attachBuffer(P): cannot attach NULL buffer"); |
| 244 | return BAD_VALUE; |
| 245 | } |
| 246 | |
| 247 | Mutex::Autolock lock(mCore->mMutex); |
| 248 | |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 249 | // Make sure we don't have too many acquired buffers |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 250 | int numAcquiredBuffers = 0; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 251 | for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) { |
| 252 | if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) { |
| 253 | ++numAcquiredBuffers; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) { |
| 258 | BQ_LOGE("attachBuffer(P): max acquired buffer count reached: %d " |
| 259 | "(max %d)", numAcquiredBuffers, |
| 260 | mCore->mMaxAcquiredBufferCount); |
| 261 | return INVALID_OPERATION; |
| 262 | } |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 263 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 264 | if (buffer->getGenerationNumber() != mCore->mGenerationNumber) { |
| 265 | BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] " |
| 266 | "[queue %u]", buffer->getGenerationNumber(), |
| 267 | mCore->mGenerationNumber); |
| 268 | return BAD_VALUE; |
| 269 | } |
| 270 | |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 271 | // Find a free slot to put the buffer into |
| 272 | int found = BufferQueueCore::INVALID_BUFFER_SLOT; |
| 273 | if (!mCore->mFreeSlots.empty()) { |
| 274 | auto slot = mCore->mFreeSlots.begin(); |
| 275 | found = *slot; |
| 276 | mCore->mFreeSlots.erase(slot); |
| 277 | } else if (!mCore->mFreeBuffers.empty()) { |
| 278 | found = mCore->mFreeBuffers.front(); |
| 279 | mCore->mFreeBuffers.remove(found); |
| 280 | } |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 281 | if (found == BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 282 | BQ_LOGE("attachBuffer(P): could not find free buffer slot"); |
| 283 | return NO_MEMORY; |
| 284 | } |
| 285 | |
| 286 | *outSlot = found; |
| 287 | ATRACE_BUFFER_INDEX(*outSlot); |
| 288 | BQ_LOGV("attachBuffer(C): returning slot %d", *outSlot); |
| 289 | |
| 290 | mSlots[*outSlot].mGraphicBuffer = buffer; |
| 291 | mSlots[*outSlot].mBufferState = BufferSlot::ACQUIRED; |
| 292 | mSlots[*outSlot].mAttachedByConsumer = true; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 293 | mSlots[*outSlot].mNeedsCleanupOnRelease = false; |
| 294 | mSlots[*outSlot].mFence = Fence::NO_FENCE; |
| 295 | mSlots[*outSlot].mFrameNumber = 0; |
| 296 | |
Dan Stoza | 99b18b4 | 2014-03-28 15:34:33 -0700 | [diff] [blame] | 297 | // mAcquireCalled tells BufferQueue that it doesn't need to send a valid |
| 298 | // GraphicBuffer pointer on the next acquireBuffer call, which decreases |
| 299 | // Binder traffic by not un/flattening the GraphicBuffer. However, it |
| 300 | // requires that the consumer maintain a cached copy of the slot <--> buffer |
| 301 | // mappings, which is why the consumer doesn't need the valid pointer on |
| 302 | // acquire. |
| 303 | // |
| 304 | // The StreamSplitter is one of the primary users of the attach/detach |
| 305 | // logic, and while it is running, all buffers it acquires are immediately |
| 306 | // detached, and all buffers it eventually releases are ones that were |
| 307 | // attached (as opposed to having been obtained from acquireBuffer), so it |
| 308 | // doesn't make sense to maintain the slot/buffer mappings, which would |
| 309 | // become invalid for every buffer during detach/attach. By setting this to |
| 310 | // false, the valid GraphicBuffer pointer will always be sent with acquire |
| 311 | // for attached buffers. |
| 312 | mSlots[*outSlot].mAcquireCalled = false; |
| 313 | |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 314 | mCore->validateConsistencyLocked(); |
| 315 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 316 | return NO_ERROR; |
| 317 | } |
| 318 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 319 | status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber, |
| 320 | const sp<Fence>& releaseFence, EGLDisplay eglDisplay, |
| 321 | EGLSyncKHR eglFence) { |
| 322 | ATRACE_CALL(); |
| 323 | ATRACE_BUFFER_INDEX(slot); |
| 324 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 325 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS || |
| 326 | releaseFence == NULL) { |
Dan Stoza | 52937cd | 2015-05-01 16:42:55 -0700 | [diff] [blame] | 327 | BQ_LOGE("releaseBuffer: slot %d out of range or fence %p NULL", slot, |
| 328 | releaseFence.get()); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 329 | return BAD_VALUE; |
| 330 | } |
| 331 | |
Dan Stoza | d1c1036 | 2014-03-28 15:19:08 -0700 | [diff] [blame] | 332 | sp<IProducerListener> listener; |
| 333 | { // Autolock scope |
| 334 | Mutex::Autolock lock(mCore->mMutex); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 335 | |
Dan Stoza | d1c1036 | 2014-03-28 15:19:08 -0700 | [diff] [blame] | 336 | // If the frame number has changed because the buffer has been reallocated, |
| 337 | // we can ignore this releaseBuffer for the old buffer |
| 338 | if (frameNumber != mSlots[slot].mFrameNumber) { |
| 339 | return STALE_BUFFER_SLOT; |
| 340 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 341 | |
Dan Stoza | d1c1036 | 2014-03-28 15:19:08 -0700 | [diff] [blame] | 342 | // Make sure this buffer hasn't been queued while acquired by the consumer |
| 343 | BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin()); |
| 344 | while (current != mCore->mQueue.end()) { |
| 345 | if (current->mSlot == slot) { |
| 346 | BQ_LOGE("releaseBuffer: buffer slot %d pending release is " |
| 347 | "currently queued", slot); |
| 348 | return BAD_VALUE; |
| 349 | } |
| 350 | ++current; |
| 351 | } |
| 352 | |
| 353 | if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) { |
| 354 | mSlots[slot].mEglDisplay = eglDisplay; |
| 355 | mSlots[slot].mEglFence = eglFence; |
| 356 | mSlots[slot].mFence = releaseFence; |
| 357 | mSlots[slot].mBufferState = BufferSlot::FREE; |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 358 | mCore->mFreeBuffers.push_back(slot); |
Dan Stoza | d1c1036 | 2014-03-28 15:19:08 -0700 | [diff] [blame] | 359 | listener = mCore->mConnectedProducerListener; |
| 360 | BQ_LOGV("releaseBuffer: releasing slot %d", slot); |
| 361 | } else if (mSlots[slot].mNeedsCleanupOnRelease) { |
| 362 | BQ_LOGV("releaseBuffer: releasing a stale buffer slot %d " |
| 363 | "(state = %d)", slot, mSlots[slot].mBufferState); |
| 364 | mSlots[slot].mNeedsCleanupOnRelease = false; |
| 365 | return STALE_BUFFER_SLOT; |
| 366 | } else { |
Dan Stoza | 52937cd | 2015-05-01 16:42:55 -0700 | [diff] [blame] | 367 | BQ_LOGE("releaseBuffer: attempted to release buffer slot %d " |
Dan Stoza | d1c1036 | 2014-03-28 15:19:08 -0700 | [diff] [blame] | 368 | "but its state was %d", slot, mSlots[slot].mBufferState); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 369 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 370 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 371 | |
Dan Stoza | d1c1036 | 2014-03-28 15:19:08 -0700 | [diff] [blame] | 372 | mCore->mDequeueCondition.broadcast(); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 373 | mCore->validateConsistencyLocked(); |
Dan Stoza | d1c1036 | 2014-03-28 15:19:08 -0700 | [diff] [blame] | 374 | } // Autolock scope |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 375 | |
Dan Stoza | d1c1036 | 2014-03-28 15:19:08 -0700 | [diff] [blame] | 376 | // Call back without lock held |
| 377 | if (listener != NULL) { |
| 378 | listener->onBufferReleased(); |
| 379 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 380 | |
| 381 | return NO_ERROR; |
| 382 | } |
| 383 | |
| 384 | status_t BufferQueueConsumer::connect( |
| 385 | const sp<IConsumerListener>& consumerListener, bool controlledByApp) { |
| 386 | ATRACE_CALL(); |
| 387 | |
| 388 | if (consumerListener == NULL) { |
| 389 | BQ_LOGE("connect(C): consumerListener may not be NULL"); |
| 390 | return BAD_VALUE; |
| 391 | } |
| 392 | |
| 393 | BQ_LOGV("connect(C): controlledByApp=%s", |
| 394 | controlledByApp ? "true" : "false"); |
| 395 | |
| 396 | Mutex::Autolock lock(mCore->mMutex); |
| 397 | |
| 398 | if (mCore->mIsAbandoned) { |
| 399 | BQ_LOGE("connect(C): BufferQueue has been abandoned"); |
| 400 | return NO_INIT; |
| 401 | } |
| 402 | |
| 403 | mCore->mConsumerListener = consumerListener; |
| 404 | mCore->mConsumerControlledByApp = controlledByApp; |
| 405 | |
| 406 | return NO_ERROR; |
| 407 | } |
| 408 | |
| 409 | status_t BufferQueueConsumer::disconnect() { |
| 410 | ATRACE_CALL(); |
| 411 | |
| 412 | BQ_LOGV("disconnect(C)"); |
| 413 | |
| 414 | Mutex::Autolock lock(mCore->mMutex); |
| 415 | |
| 416 | if (mCore->mConsumerListener == NULL) { |
| 417 | BQ_LOGE("disconnect(C): no consumer is connected"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 418 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | mCore->mIsAbandoned = true; |
| 422 | mCore->mConsumerListener = NULL; |
| 423 | mCore->mQueue.clear(); |
| 424 | mCore->freeAllBuffersLocked(); |
| 425 | mCore->mDequeueCondition.broadcast(); |
| 426 | return NO_ERROR; |
| 427 | } |
| 428 | |
Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 429 | status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 430 | ATRACE_CALL(); |
| 431 | |
| 432 | if (outSlotMask == NULL) { |
| 433 | BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL"); |
| 434 | return BAD_VALUE; |
| 435 | } |
| 436 | |
| 437 | Mutex::Autolock lock(mCore->mMutex); |
| 438 | |
| 439 | if (mCore->mIsAbandoned) { |
| 440 | BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned"); |
| 441 | return NO_INIT; |
| 442 | } |
| 443 | |
Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 444 | uint64_t mask = 0; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 445 | for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 446 | if (!mSlots[s].mAcquireCalled) { |
Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 447 | mask |= (1ULL << s); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | // Remove from the mask queued buffers for which acquire has been called, |
| 452 | // since the consumer will not receive their buffer addresses and so must |
| 453 | // retain their cached information |
| 454 | BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin()); |
| 455 | while (current != mCore->mQueue.end()) { |
| 456 | if (current->mAcquireCalled) { |
Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 457 | mask &= ~(1ULL << current->mSlot); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 458 | } |
| 459 | ++current; |
| 460 | } |
| 461 | |
Dan Stoza | febd4f4 | 2014-04-09 16:14:51 -0700 | [diff] [blame] | 462 | BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 463 | *outSlotMask = mask; |
| 464 | return NO_ERROR; |
| 465 | } |
| 466 | |
| 467 | status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width, |
| 468 | uint32_t height) { |
| 469 | ATRACE_CALL(); |
| 470 | |
| 471 | if (width == 0 || height == 0) { |
| 472 | BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u " |
| 473 | "height=%u)", width, height); |
| 474 | return BAD_VALUE; |
| 475 | } |
| 476 | |
| 477 | BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height); |
| 478 | |
| 479 | Mutex::Autolock lock(mCore->mMutex); |
| 480 | mCore->mDefaultWidth = width; |
| 481 | mCore->mDefaultHeight = height; |
| 482 | return NO_ERROR; |
| 483 | } |
| 484 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 485 | status_t BufferQueueConsumer::setMaxBufferCount(int bufferCount) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 486 | ATRACE_CALL(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 487 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 488 | if (bufferCount < 1 || bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) { |
| 489 | BQ_LOGE("setMaxBufferCount: invalid count %d", bufferCount); |
| 490 | return BAD_VALUE; |
| 491 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 492 | |
| 493 | Mutex::Autolock lock(mCore->mMutex); |
| 494 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 495 | if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) { |
| 496 | BQ_LOGE("setMaxBufferCount: producer is already connected"); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 497 | return INVALID_OPERATION; |
| 498 | } |
| 499 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 500 | if (bufferCount < mCore->mMaxAcquiredBufferCount) { |
| 501 | BQ_LOGE("setMaxBufferCount: invalid buffer count (%d) less than" |
| 502 | "mMaxAcquiredBufferCount (%d)", bufferCount, |
| 503 | mCore->mMaxAcquiredBufferCount); |
| 504 | return BAD_VALUE; |
| 505 | } |
| 506 | |
| 507 | mCore->mMaxBufferCount = bufferCount; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 508 | return NO_ERROR; |
| 509 | } |
| 510 | |
| 511 | status_t BufferQueueConsumer::setMaxAcquiredBufferCount( |
| 512 | int maxAcquiredBuffers) { |
| 513 | ATRACE_CALL(); |
| 514 | |
| 515 | if (maxAcquiredBuffers < 1 || |
| 516 | maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) { |
| 517 | BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d", |
| 518 | maxAcquiredBuffers); |
| 519 | return BAD_VALUE; |
| 520 | } |
| 521 | |
| 522 | Mutex::Autolock lock(mCore->mMutex); |
| 523 | |
| 524 | if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) { |
| 525 | BQ_LOGE("setMaxAcquiredBufferCount: producer is already connected"); |
| 526 | return INVALID_OPERATION; |
| 527 | } |
| 528 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 529 | if ((maxAcquiredBuffers + mCore->mMaxDequeuedBufferCount + |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 530 | (mCore->mAsyncMode || mCore->mDequeueBufferCannotBlock ? 1 : 0)) > |
| 531 | mCore->mMaxBufferCount) { |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 532 | BQ_LOGE("setMaxAcquiredBufferCount: %d acquired buffers would exceed " |
| 533 | "the maxBufferCount (%d) (maxDequeued %d async %d)", |
| 534 | maxAcquiredBuffers, mCore->mMaxBufferCount, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 535 | mCore->mMaxDequeuedBufferCount, mCore->mAsyncMode || |
| 536 | mCore->mDequeueBufferCannotBlock); |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 537 | return BAD_VALUE; |
| 538 | } |
| 539 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 540 | BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers); |
| 541 | mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers; |
| 542 | return NO_ERROR; |
| 543 | } |
| 544 | |
| 545 | void BufferQueueConsumer::setConsumerName(const String8& name) { |
| 546 | ATRACE_CALL(); |
| 547 | BQ_LOGV("setConsumerName: '%s'", name.string()); |
| 548 | Mutex::Autolock lock(mCore->mMutex); |
| 549 | mCore->mConsumerName = name; |
| 550 | mConsumerName = name; |
| 551 | } |
| 552 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 553 | status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 554 | ATRACE_CALL(); |
| 555 | BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat); |
| 556 | Mutex::Autolock lock(mCore->mMutex); |
| 557 | mCore->mDefaultBufferFormat = defaultFormat; |
| 558 | return NO_ERROR; |
| 559 | } |
| 560 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 561 | status_t BufferQueueConsumer::setDefaultBufferDataSpace( |
| 562 | android_dataspace defaultDataSpace) { |
| 563 | ATRACE_CALL(); |
| 564 | BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace); |
| 565 | Mutex::Autolock lock(mCore->mMutex); |
| 566 | mCore->mDefaultBufferDataSpace = defaultDataSpace; |
| 567 | return NO_ERROR; |
| 568 | } |
| 569 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 570 | status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) { |
| 571 | ATRACE_CALL(); |
| 572 | BQ_LOGV("setConsumerUsageBits: %#x", usage); |
| 573 | Mutex::Autolock lock(mCore->mMutex); |
| 574 | mCore->mConsumerUsageBits = usage; |
| 575 | return NO_ERROR; |
| 576 | } |
| 577 | |
| 578 | status_t BufferQueueConsumer::setTransformHint(uint32_t hint) { |
| 579 | ATRACE_CALL(); |
| 580 | BQ_LOGV("setTransformHint: %#x", hint); |
| 581 | Mutex::Autolock lock(mCore->mMutex); |
| 582 | mCore->mTransformHint = hint; |
| 583 | return NO_ERROR; |
| 584 | } |
| 585 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 586 | sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const { |
| 587 | return mCore->mSidebandStream; |
| 588 | } |
| 589 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 590 | void BufferQueueConsumer::dump(String8& result, const char* prefix) const { |
| 591 | mCore->dump(result, prefix); |
| 592 | } |
| 593 | |
| 594 | } // namespace android |