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