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