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