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