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