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