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