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