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