| 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> | 
|  | 18 |  | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 19 | #define LOG_TAG "BufferQueueProducer" | 
|  | 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 21 | //#define LOG_NDEBUG 0 | 
|  | 22 |  | 
| Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 23 | #if DEBUG_ONLY_CODE | 
|  | 24 | #define VALIDATE_CONSISTENCY() do { mCore->validateConsistencyLocked(); } while (0) | 
|  | 25 | #else | 
|  | 26 | #define VALIDATE_CONSISTENCY() | 
|  | 27 | #endif | 
|  | 28 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 29 | #define EGL_EGLEXT_PROTOTYPES | 
|  | 30 |  | 
| Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 31 | #include <binder/IPCThreadState.h> | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 32 | #include <gui/BufferItem.h> | 
|  | 33 | #include <gui/BufferQueueCore.h> | 
|  | 34 | #include <gui/BufferQueueProducer.h> | 
| John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 35 | #include <gui/GLConsumer.h> | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 36 | #include <gui/IConsumerListener.h> | 
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 37 | #include <gui/IProducerListener.h> | 
| Jayant Chowdhary | ad9fe27 | 2019-03-07 22:36:06 -0800 | [diff] [blame] | 38 | #include <private/gui/BufferQueueThreadState.h> | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 39 |  | 
|  | 40 | #include <utils/Log.h> | 
|  | 41 | #include <utils/Trace.h> | 
|  | 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 |  | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 69 | static constexpr uint32_t BQ_LAYER_COUNT = 1; | 
|  | 70 |  | 
| Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 71 | BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core, | 
|  | 72 | bool consumerIsSurfaceFlinger) : | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 73 | mCore(core), | 
|  | 74 | mSlots(core->mSlots), | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 75 | mConsumerName(), | 
| Eric Penner | 99a0afb | 2014-09-30 11:28:30 -0700 | [diff] [blame] | 76 | mStickyTransform(0), | 
| Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 77 | mConsumerIsSurfaceFlinger(consumerIsSurfaceFlinger), | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 78 | mLastQueueBufferFence(Fence::NO_FENCE), | 
| Pablo Ceballos | bd3577e | 2016-06-20 17:40:34 -0700 | [diff] [blame] | 79 | mLastQueuedTransform(0), | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 80 | mCallbackMutex(), | 
|  | 81 | mNextCallbackTicket(0), | 
|  | 82 | mCurrentCallbackTicket(0), | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 83 | mCallbackCondition(), | 
| Jorim Jaggi | 35b4e38 | 2019-03-28 00:44:03 +0100 | [diff] [blame] | 84 | mDequeueTimeout(-1), | 
|  | 85 | mDequeueWaitingForAllocation(false) {} | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 86 |  | 
|  | 87 | BufferQueueProducer::~BufferQueueProducer() {} | 
|  | 88 |  | 
|  | 89 | status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) { | 
|  | 90 | ATRACE_CALL(); | 
|  | 91 | BQ_LOGV("requestBuffer: slot %d", slot); | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 92 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 93 |  | 
|  | 94 | if (mCore->mIsAbandoned) { | 
|  | 95 | BQ_LOGE("requestBuffer: BufferQueue has been abandoned"); | 
|  | 96 | return NO_INIT; | 
|  | 97 | } | 
|  | 98 |  | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 99 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { | 
|  | 100 | BQ_LOGE("requestBuffer: BufferQueue has no connected producer"); | 
|  | 101 | return NO_INIT; | 
|  | 102 | } | 
|  | 103 |  | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 104 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 105 | BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)", | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 106 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 107 | return BAD_VALUE; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 108 | } else if (!mSlots[slot].mBufferState.isDequeued()) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 109 | BQ_LOGE("requestBuffer: slot %d is not owned by the producer " | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 110 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 111 | return BAD_VALUE; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | mSlots[slot].mRequestBufferCalled = true; | 
|  | 115 | *buf = mSlots[slot].mGraphicBuffer; | 
|  | 116 | return NO_ERROR; | 
|  | 117 | } | 
|  | 118 |  | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 119 | status_t BufferQueueProducer::setMaxDequeuedBufferCount( | 
|  | 120 | int maxDequeuedBuffers) { | 
|  | 121 | ATRACE_CALL(); | 
|  | 122 | BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d", | 
|  | 123 | maxDequeuedBuffers); | 
|  | 124 |  | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 125 | sp<IConsumerListener> listener; | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 126 | { // Autolock scope | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 127 | std::unique_lock<std::mutex> lock(mCore->mMutex); | 
|  | 128 | mCore->waitWhileAllocatingLocked(lock); | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 129 |  | 
|  | 130 | if (mCore->mIsAbandoned) { | 
|  | 131 | BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been " | 
|  | 132 | "abandoned"); | 
|  | 133 | return NO_INIT; | 
|  | 134 | } | 
|  | 135 |  | 
| Pablo Ceballos | 245cc5b | 2016-04-19 11:33:00 -0700 | [diff] [blame] | 136 | if (maxDequeuedBuffers == mCore->mMaxDequeuedBufferCount) { | 
|  | 137 | return NO_ERROR; | 
|  | 138 | } | 
|  | 139 |  | 
| Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 140 | // The new maxDequeuedBuffer count should not be violated by the number | 
|  | 141 | // of currently dequeued buffers | 
|  | 142 | int dequeuedCount = 0; | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 143 | for (int s : mCore->mActiveBuffers) { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 144 | if (mSlots[s].mBufferState.isDequeued()) { | 
| Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 145 | dequeuedCount++; | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 146 | } | 
|  | 147 | } | 
| Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 148 | if (dequeuedCount > maxDequeuedBuffers) { | 
|  | 149 | BQ_LOGE("setMaxDequeuedBufferCount: the requested maxDequeuedBuffer" | 
|  | 150 | "count (%d) exceeds the current dequeued buffer count (%d)", | 
|  | 151 | maxDequeuedBuffers, dequeuedCount); | 
|  | 152 | return BAD_VALUE; | 
|  | 153 | } | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 154 |  | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 155 | int bufferCount = mCore->getMinUndequeuedBufferCountLocked(); | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 156 | bufferCount += maxDequeuedBuffers; | 
|  | 157 |  | 
|  | 158 | if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) { | 
|  | 159 | BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large " | 
|  | 160 | "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS); | 
|  | 161 | return BAD_VALUE; | 
|  | 162 | } | 
|  | 163 |  | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 164 | const int minBufferSlots = mCore->getMinMaxBufferCountLocked(); | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 165 | if (bufferCount < minBufferSlots) { | 
|  | 166 | BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is " | 
|  | 167 | "less than minimum %d", bufferCount, minBufferSlots); | 
|  | 168 | return BAD_VALUE; | 
|  | 169 | } | 
|  | 170 |  | 
| Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 171 | if (bufferCount > mCore->mMaxBufferCount) { | 
|  | 172 | BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would " | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 173 | "exceed the maxBufferCount (%d) (maxAcquired %d async %d " | 
|  | 174 | "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers, | 
|  | 175 | mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount, | 
|  | 176 | mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock); | 
| Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 177 | return BAD_VALUE; | 
|  | 178 | } | 
|  | 179 |  | 
| Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 180 | int delta = maxDequeuedBuffers - mCore->mMaxDequeuedBufferCount; | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 181 | if (!mCore->adjustAvailableSlotsLocked(delta)) { | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 182 | return BAD_VALUE; | 
|  | 183 | } | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 184 | mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers; | 
| Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 185 | VALIDATE_CONSISTENCY(); | 
| Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 186 | if (delta < 0) { | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 187 | listener = mCore->mConsumerListener; | 
| Pablo Ceballos | 72daab6 | 2015-12-07 16:38:43 -0800 | [diff] [blame] | 188 | } | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 189 | mCore->mDequeueCondition.notify_all(); | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 190 | } // Autolock scope | 
|  | 191 |  | 
|  | 192 | // Call back without lock held | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 193 | if (listener != nullptr) { | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 194 | listener->onBuffersReleased(); | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
|  | 197 | return NO_ERROR; | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | status_t BufferQueueProducer::setAsyncMode(bool async) { | 
|  | 201 | ATRACE_CALL(); | 
|  | 202 | BQ_LOGV("setAsyncMode: async = %d", async); | 
|  | 203 |  | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 204 | sp<IConsumerListener> listener; | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 205 | { // Autolock scope | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 206 | std::unique_lock<std::mutex> lock(mCore->mMutex); | 
|  | 207 | mCore->waitWhileAllocatingLocked(lock); | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 208 |  | 
|  | 209 | if (mCore->mIsAbandoned) { | 
|  | 210 | BQ_LOGE("setAsyncMode: BufferQueue has been abandoned"); | 
|  | 211 | return NO_INIT; | 
|  | 212 | } | 
|  | 213 |  | 
| Pablo Ceballos | 245cc5b | 2016-04-19 11:33:00 -0700 | [diff] [blame] | 214 | if (async == mCore->mAsyncMode) { | 
|  | 215 | return NO_ERROR; | 
|  | 216 | } | 
|  | 217 |  | 
| Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 218 | if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount + | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 219 | (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) > | 
|  | 220 | mCore->mMaxBufferCount) { | 
| Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 221 | BQ_LOGE("setAsyncMode(%d): this call would cause the " | 
|  | 222 | "maxBufferCount (%d) to be exceeded (maxAcquired %d " | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 223 | "maxDequeued %d mDequeueBufferCannotBlock %d)", async, | 
|  | 224 | mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount, | 
|  | 225 | mCore->mMaxDequeuedBufferCount, | 
|  | 226 | mCore->mDequeueBufferCannotBlock); | 
| Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 227 | return BAD_VALUE; | 
|  | 228 | } | 
|  | 229 |  | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 230 | int delta = mCore->getMaxBufferCountLocked(async, | 
|  | 231 | mCore->mDequeueBufferCannotBlock, mCore->mMaxBufferCount) | 
|  | 232 | - mCore->getMaxBufferCountLocked(); | 
|  | 233 |  | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 234 | if (!mCore->adjustAvailableSlotsLocked(delta)) { | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 235 | BQ_LOGE("setAsyncMode: BufferQueue failed to adjust the number of " | 
|  | 236 | "available slots. Delta = %d", delta); | 
|  | 237 | return BAD_VALUE; | 
|  | 238 | } | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 239 | mCore->mAsyncMode = async; | 
| Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 240 | VALIDATE_CONSISTENCY(); | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 241 | mCore->mDequeueCondition.notify_all(); | 
| Pablo Ceballos | 245cc5b | 2016-04-19 11:33:00 -0700 | [diff] [blame] | 242 | if (delta < 0) { | 
|  | 243 | listener = mCore->mConsumerListener; | 
|  | 244 | } | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 245 | } // Autolock scope | 
|  | 246 |  | 
|  | 247 | // Call back without lock held | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 248 | if (listener != nullptr) { | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 249 | listener->onBuffersReleased(); | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 250 | } | 
|  | 251 | return NO_ERROR; | 
|  | 252 | } | 
|  | 253 |  | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 254 | int BufferQueueProducer::getFreeBufferLocked() const { | 
|  | 255 | if (mCore->mFreeBuffers.empty()) { | 
|  | 256 | return BufferQueueCore::INVALID_BUFFER_SLOT; | 
|  | 257 | } | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 258 | int slot = mCore->mFreeBuffers.front(); | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 259 | mCore->mFreeBuffers.pop_front(); | 
|  | 260 | return slot; | 
|  | 261 | } | 
|  | 262 |  | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 263 | int BufferQueueProducer::getFreeSlotLocked() const { | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 264 | if (mCore->mFreeSlots.empty()) { | 
|  | 265 | return BufferQueueCore::INVALID_BUFFER_SLOT; | 
|  | 266 | } | 
| Pablo Ceballos | dce5c55 | 2016-02-10 15:43:22 -0800 | [diff] [blame] | 267 | int slot = *(mCore->mFreeSlots.begin()); | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 268 | mCore->mFreeSlots.erase(slot); | 
| Pablo Ceballos | dce5c55 | 2016-02-10 15:43:22 -0800 | [diff] [blame] | 269 | return slot; | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 270 | } | 
|  | 271 |  | 
|  | 272 | status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller, | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 273 | std::unique_lock<std::mutex>& lock, int* found) const { | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 274 | auto callerString = (caller == FreeSlotCaller::Dequeue) ? | 
|  | 275 | "dequeueBuffer" : "attachBuffer"; | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 276 | bool tryAgain = true; | 
|  | 277 | while (tryAgain) { | 
|  | 278 | if (mCore->mIsAbandoned) { | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 279 | BQ_LOGE("%s: BufferQueue has been abandoned", callerString); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 280 | return NO_INIT; | 
|  | 281 | } | 
|  | 282 |  | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 283 | int dequeuedCount = 0; | 
|  | 284 | int acquiredCount = 0; | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 285 | for (int s : mCore->mActiveBuffers) { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 286 | if (mSlots[s].mBufferState.isDequeued()) { | 
|  | 287 | ++dequeuedCount; | 
|  | 288 | } | 
|  | 289 | if (mSlots[s].mBufferState.isAcquired()) { | 
|  | 290 | ++acquiredCount; | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 291 | } | 
|  | 292 | } | 
|  | 293 |  | 
| Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 294 | // Producers are not allowed to dequeue more than | 
|  | 295 | // mMaxDequeuedBufferCount buffers. | 
|  | 296 | // This check is only done if a buffer has already been queued | 
|  | 297 | if (mCore->mBufferHasBeenQueued && | 
|  | 298 | dequeuedCount >= mCore->mMaxDequeuedBufferCount) { | 
| Sungtak Lee | af14124 | 2019-04-24 16:36:44 -0700 | [diff] [blame] | 299 | // Supress error logs when timeout is non-negative. | 
|  | 300 | if (mDequeueTimeout < 0) { | 
|  | 301 | BQ_LOGE("%s: attempting to exceed the max dequeued buffer " | 
|  | 302 | "count (%d)", callerString, | 
|  | 303 | mCore->mMaxDequeuedBufferCount); | 
|  | 304 | } | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 305 | return INVALID_OPERATION; | 
|  | 306 | } | 
|  | 307 |  | 
| Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 308 | *found = BufferQueueCore::INVALID_BUFFER_SLOT; | 
|  | 309 |  | 
| Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 310 | // If we disconnect and reconnect quickly, we can be in a state where | 
|  | 311 | // our slots are empty but we have many buffers in the queue. This can | 
|  | 312 | // cause us to run out of memory if we outrun the consumer. Wait here if | 
|  | 313 | // it looks like we have too many buffers queued up. | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 314 | const int maxBufferCount = mCore->getMaxBufferCountLocked(); | 
| Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 315 | bool tooManyBuffers = mCore->mQueue.size() | 
|  | 316 | > static_cast<size_t>(maxBufferCount); | 
| Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 317 | if (tooManyBuffers) { | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 318 | BQ_LOGV("%s: queue size is %zu, waiting", callerString, | 
| Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 319 | mCore->mQueue.size()); | 
| Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 320 | } else { | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 321 | // If in shared buffer mode and a shared buffer exists, always | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 322 | // return it. | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 323 | if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot != | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 324 | BufferQueueCore::INVALID_BUFFER_SLOT) { | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 325 | *found = mCore->mSharedBufferSlot; | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 326 | } else { | 
|  | 327 | if (caller == FreeSlotCaller::Dequeue) { | 
|  | 328 | // If we're calling this from dequeue, prefer free buffers | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 329 | int slot = getFreeBufferLocked(); | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 330 | if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) { | 
|  | 331 | *found = slot; | 
|  | 332 | } else if (mCore->mAllowAllocation) { | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 333 | *found = getFreeSlotLocked(); | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 334 | } | 
|  | 335 | } else { | 
|  | 336 | // If we're calling this from attach, prefer free slots | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 337 | int slot = getFreeSlotLocked(); | 
| Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 338 | if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) { | 
|  | 339 | *found = slot; | 
|  | 340 | } else { | 
|  | 341 | *found = getFreeBufferLocked(); | 
|  | 342 | } | 
| Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 343 | } | 
|  | 344 | } | 
| Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 345 | } | 
|  | 346 |  | 
|  | 347 | // If no buffer is found, or if the queue has too many buffers | 
|  | 348 | // outstanding, wait for a buffer to be acquired or released, or for the | 
|  | 349 | // max buffer count to change. | 
|  | 350 | tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) || | 
|  | 351 | tooManyBuffers; | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 352 | if (tryAgain) { | 
|  | 353 | // Return an error if we're in non-blocking mode (producer and | 
|  | 354 | // consumer are controlled by the application). | 
|  | 355 | // However, the consumer is allowed to briefly acquire an extra | 
|  | 356 | // buffer (which could cause us to have to wait here), which is | 
|  | 357 | // okay, since it is only used to implement an atomic acquire + | 
|  | 358 | // release (e.g., in GLConsumer::updateTexImage()) | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 359 | if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) && | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 360 | (acquiredCount <= mCore->mMaxAcquiredBufferCount)) { | 
|  | 361 | return WOULD_BLOCK; | 
|  | 362 | } | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 363 | if (mDequeueTimeout >= 0) { | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 364 | std::cv_status result = mCore->mDequeueCondition.wait_for(lock, | 
|  | 365 | std::chrono::nanoseconds(mDequeueTimeout)); | 
|  | 366 | if (result == std::cv_status::timeout) { | 
|  | 367 | return TIMED_OUT; | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 368 | } | 
|  | 369 | } else { | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 370 | mCore->mDequeueCondition.wait(lock); | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 371 | } | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 372 | } | 
|  | 373 | } // while (tryAgain) | 
|  | 374 |  | 
|  | 375 | return NO_ERROR; | 
|  | 376 | } | 
|  | 377 |  | 
| Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 378 | status_t BufferQueueProducer::dequeueBuffer(int* outSlot, sp<android::Fence>* outFence, | 
|  | 379 | uint32_t width, uint32_t height, PixelFormat format, | 
|  | 380 | uint64_t usage, uint64_t* outBufferAge, | 
|  | 381 | FrameEventHistoryDelta* outTimestamps) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 382 | ATRACE_CALL(); | 
|  | 383 | { // Autolock scope | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 384 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 385 | mConsumerName = mCore->mConsumerName; | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 386 |  | 
|  | 387 | if (mCore->mIsAbandoned) { | 
|  | 388 | BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned"); | 
|  | 389 | return NO_INIT; | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { | 
|  | 393 | BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer"); | 
|  | 394 | return NO_INIT; | 
|  | 395 | } | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 396 | } // Autolock scope | 
|  | 397 |  | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 398 | BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#" PRIx64, width, height, format, usage); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 399 |  | 
|  | 400 | if ((width && !height) || (!width && height)) { | 
|  | 401 | BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height); | 
|  | 402 | return BAD_VALUE; | 
|  | 403 | } | 
|  | 404 |  | 
|  | 405 | status_t returnFlags = NO_ERROR; | 
|  | 406 | EGLDisplay eglDisplay = EGL_NO_DISPLAY; | 
|  | 407 | EGLSyncKHR eglFence = EGL_NO_SYNC_KHR; | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 408 | bool attachedByConsumer = false; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 409 |  | 
|  | 410 | { // Autolock scope | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 411 | std::unique_lock<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 412 |  | 
| Jorim Jaggi | 35b4e38 | 2019-03-28 00:44:03 +0100 | [diff] [blame] | 413 | // If we don't have a free buffer, but we are currently allocating, we wait until allocation | 
|  | 414 | // is finished such that we don't allocate in parallel. | 
|  | 415 | if (mCore->mFreeBuffers.empty() && mCore->mIsAllocating) { | 
|  | 416 | mDequeueWaitingForAllocation = true; | 
|  | 417 | mCore->waitWhileAllocatingLocked(lock); | 
|  | 418 | mDequeueWaitingForAllocation = false; | 
|  | 419 | mDequeueWaitingForAllocationCondition.notify_all(); | 
|  | 420 | } | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 421 |  | 
|  | 422 | if (format == 0) { | 
|  | 423 | format = mCore->mDefaultBufferFormat; | 
|  | 424 | } | 
|  | 425 |  | 
|  | 426 | // Enable the usage bits the consumer requested | 
|  | 427 | usage |= mCore->mConsumerUsageBits; | 
|  | 428 |  | 
| Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 429 | const bool useDefaultSize = !width && !height; | 
|  | 430 | if (useDefaultSize) { | 
|  | 431 | width = mCore->mDefaultWidth; | 
|  | 432 | height = mCore->mDefaultHeight; | 
| Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 433 | if (mCore->mAutoPrerotation && | 
|  | 434 | (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) { | 
|  | 435 | std::swap(width, height); | 
|  | 436 | } | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 437 | } | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 438 |  | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 439 | int found = BufferItem::INVALID_BUFFER_SLOT; | 
| Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 440 | while (found == BufferItem::INVALID_BUFFER_SLOT) { | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 441 | status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, lock, &found); | 
| Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 442 | if (status != NO_ERROR) { | 
|  | 443 | return status; | 
|  | 444 | } | 
|  | 445 |  | 
|  | 446 | // This should not happen | 
|  | 447 | if (found == BufferQueueCore::INVALID_BUFFER_SLOT) { | 
|  | 448 | BQ_LOGE("dequeueBuffer: no available buffer slots"); | 
|  | 449 | return -EBUSY; | 
|  | 450 | } | 
|  | 451 |  | 
|  | 452 | const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer); | 
|  | 453 |  | 
|  | 454 | // If we are not allowed to allocate new buffers, | 
|  | 455 | // waitForFreeSlotThenRelock must have returned a slot containing a | 
|  | 456 | // buffer. If this buffer would require reallocation to meet the | 
|  | 457 | // requested attributes, we free it and attempt to get another one. | 
|  | 458 | if (!mCore->mAllowAllocation) { | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 459 | if (buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) { | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 460 | if (mCore->mSharedBufferSlot == found) { | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 461 | BQ_LOGE("dequeueBuffer: cannot re-allocate a sharedbuffer"); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 462 | return BAD_VALUE; | 
|  | 463 | } | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 464 | mCore->mFreeSlots.insert(found); | 
|  | 465 | mCore->clearBufferSlotLocked(found); | 
| Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 466 | found = BufferItem::INVALID_BUFFER_SLOT; | 
|  | 467 | continue; | 
|  | 468 | } | 
|  | 469 | } | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 470 | } | 
|  | 471 |  | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 472 | const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer); | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 473 | if (mCore->mSharedBufferSlot == found && | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 474 | buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) { | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 475 | BQ_LOGE("dequeueBuffer: cannot re-allocate a shared" | 
|  | 476 | "buffer"); | 
|  | 477 |  | 
|  | 478 | return BAD_VALUE; | 
|  | 479 | } | 
|  | 480 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 481 | if (mCore->mSharedBufferSlot != found) { | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 482 | mCore->mActiveBuffers.insert(found); | 
|  | 483 | } | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 484 | *outSlot = found; | 
|  | 485 | ATRACE_BUFFER_INDEX(found); | 
|  | 486 |  | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 487 | attachedByConsumer = mSlots[found].mNeedsReallocation; | 
|  | 488 | mSlots[found].mNeedsReallocation = false; | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 489 |  | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 490 | mSlots[found].mBufferState.dequeue(); | 
|  | 491 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 492 | if ((buffer == nullptr) || | 
| Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 493 | buffer->needsReallocation(width, height, format, BQ_LAYER_COUNT, usage)) | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 494 | { | 
|  | 495 | mSlots[found].mAcquireCalled = false; | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 496 | mSlots[found].mGraphicBuffer = nullptr; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 497 | mSlots[found].mRequestBufferCalled = false; | 
|  | 498 | mSlots[found].mEglDisplay = EGL_NO_DISPLAY; | 
|  | 499 | mSlots[found].mEglFence = EGL_NO_SYNC_KHR; | 
|  | 500 | mSlots[found].mFence = Fence::NO_FENCE; | 
| Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 501 | mCore->mBufferAge = 0; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 502 | mCore->mIsAllocating = true; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 503 |  | 
|  | 504 | returnFlags |= BUFFER_NEEDS_REALLOCATION; | 
| Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 505 | } else { | 
|  | 506 | // We add 1 because that will be the frame number when this buffer | 
|  | 507 | // is queued | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 508 | mCore->mBufferAge = mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 509 | } | 
|  | 510 |  | 
| Dan Stoza | 800b41a | 2015-04-28 14:20:04 -0700 | [diff] [blame] | 511 | BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64, | 
|  | 512 | mCore->mBufferAge); | 
| Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 513 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 514 | if (CC_UNLIKELY(mSlots[found].mFence == nullptr)) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 515 | BQ_LOGE("dequeueBuffer: about to return a NULL fence - " | 
|  | 516 | "slot=%d w=%d h=%d format=%u", | 
|  | 517 | found, buffer->width, buffer->height, buffer->format); | 
|  | 518 | } | 
|  | 519 |  | 
|  | 520 | eglDisplay = mSlots[found].mEglDisplay; | 
|  | 521 | eglFence = mSlots[found].mEglFence; | 
| Pablo Ceballos | 28c65ad | 2016-06-01 15:03:21 -0700 | [diff] [blame] | 522 | // Don't return a fence in shared buffer mode, except for the first | 
|  | 523 | // frame. | 
|  | 524 | *outFence = (mCore->mSharedBufferMode && | 
|  | 525 | mCore->mSharedBufferSlot == found) ? | 
|  | 526 | Fence::NO_FENCE : mSlots[found].mFence; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 527 | mSlots[found].mEglFence = EGL_NO_SYNC_KHR; | 
|  | 528 | mSlots[found].mFence = Fence::NO_FENCE; | 
| Pablo Ceballos | 28c65ad | 2016-06-01 15:03:21 -0700 | [diff] [blame] | 529 |  | 
|  | 530 | // If shared buffer mode has just been enabled, cache the slot of the | 
|  | 531 | // first buffer that is dequeued and mark it as the shared buffer. | 
|  | 532 | if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == | 
|  | 533 | BufferQueueCore::INVALID_BUFFER_SLOT) { | 
|  | 534 | mCore->mSharedBufferSlot = found; | 
|  | 535 | mSlots[found].mBufferState.mShared = true; | 
|  | 536 | } | 
| Adithya Srinivasan | a820af9 | 2019-11-01 13:55:17 -0700 | [diff] [blame] | 537 |  | 
|  | 538 | if (!(returnFlags & BUFFER_NEEDS_REALLOCATION)) { | 
|  | 539 | if (mCore->mConsumerListener != nullptr) { | 
|  | 540 | mCore->mConsumerListener->onFrameDequeued(mSlots[*outSlot].mGraphicBuffer->getId()); | 
|  | 541 | } | 
|  | 542 | } | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 543 | } // Autolock scope | 
|  | 544 |  | 
|  | 545 | if (returnFlags & BUFFER_NEEDS_REALLOCATION) { | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 546 | BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot); | 
| Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 547 | sp<GraphicBuffer> graphicBuffer = new GraphicBuffer( | 
| Chris Forbes | f3ef3ea | 2017-04-20 12:43:28 -0700 | [diff] [blame] | 548 | width, height, format, BQ_LAYER_COUNT, usage, | 
| Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 549 | {mConsumerName.string(), mConsumerName.size()}); | 
|  | 550 |  | 
|  | 551 | status_t error = graphicBuffer->initCheck(); | 
|  | 552 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 553 | { // Autolock scope | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 554 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 555 |  | 
| Chia-I Wu | feec3b1 | 2017-05-25 09:34:56 -0700 | [diff] [blame] | 556 | if (error == NO_ERROR && !mCore->mIsAbandoned) { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 557 | graphicBuffer->setGenerationNumber(mCore->mGenerationNumber); | 
|  | 558 | mSlots[*outSlot].mGraphicBuffer = graphicBuffer; | 
| Adithya Srinivasan | a820af9 | 2019-11-01 13:55:17 -0700 | [diff] [blame] | 559 | if (mCore->mConsumerListener != nullptr) { | 
|  | 560 | mCore->mConsumerListener->onFrameDequeued( | 
|  | 561 | mSlots[*outSlot].mGraphicBuffer->getId()); | 
|  | 562 | } | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 563 | } | 
|  | 564 |  | 
|  | 565 | mCore->mIsAllocating = false; | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 566 | mCore->mIsAllocatingCondition.notify_all(); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 567 |  | 
| Chia-I Wu | feec3b1 | 2017-05-25 09:34:56 -0700 | [diff] [blame] | 568 | if (error != NO_ERROR) { | 
| Pablo Ceballos | 0a06809 | 2016-06-29 15:08:33 -0700 | [diff] [blame] | 569 | mCore->mFreeSlots.insert(*outSlot); | 
|  | 570 | mCore->clearBufferSlotLocked(*outSlot); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 571 | BQ_LOGE("dequeueBuffer: createGraphicBuffer failed"); | 
|  | 572 | return error; | 
|  | 573 | } | 
|  | 574 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 575 | if (mCore->mIsAbandoned) { | 
| Pablo Ceballos | 0a06809 | 2016-06-29 15:08:33 -0700 | [diff] [blame] | 576 | mCore->mFreeSlots.insert(*outSlot); | 
|  | 577 | mCore->clearBufferSlotLocked(*outSlot); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 578 | BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned"); | 
|  | 579 | return NO_INIT; | 
|  | 580 | } | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 581 |  | 
| Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 582 | VALIDATE_CONSISTENCY(); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 583 | } // Autolock scope | 
|  | 584 | } | 
|  | 585 |  | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 586 | if (attachedByConsumer) { | 
|  | 587 | returnFlags |= BUFFER_NEEDS_REALLOCATION; | 
|  | 588 | } | 
|  | 589 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 590 | if (eglFence != EGL_NO_SYNC_KHR) { | 
|  | 591 | EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0, | 
|  | 592 | 1000000000); | 
|  | 593 | // If something goes wrong, log the error, but return the buffer without | 
|  | 594 | // synchronizing access to it. It's too late at this point to abort the | 
|  | 595 | // dequeue operation. | 
|  | 596 | if (result == EGL_FALSE) { | 
|  | 597 | BQ_LOGE("dequeueBuffer: error %#x waiting for fence", | 
|  | 598 | eglGetError()); | 
|  | 599 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { | 
|  | 600 | BQ_LOGE("dequeueBuffer: timeout waiting for fence"); | 
|  | 601 | } | 
|  | 602 | eglDestroySyncKHR(eglDisplay, eglFence); | 
|  | 603 | } | 
|  | 604 |  | 
| Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 605 | BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x", | 
|  | 606 | *outSlot, | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 607 | mSlots[*outSlot].mFrameNumber, | 
|  | 608 | mSlots[*outSlot].mGraphicBuffer->handle, returnFlags); | 
|  | 609 |  | 
| Ian Elliott | d11b044 | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 610 | if (outBufferAge) { | 
|  | 611 | *outBufferAge = mCore->mBufferAge; | 
|  | 612 | } | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 613 | addAndGetFrameTimestamps(nullptr, outTimestamps); | 
|  | 614 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 615 | return returnFlags; | 
|  | 616 | } | 
|  | 617 |  | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 618 | status_t BufferQueueProducer::detachBuffer(int slot) { | 
|  | 619 | ATRACE_CALL(); | 
|  | 620 | ATRACE_BUFFER_INDEX(slot); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 621 | BQ_LOGV("detachBuffer: slot %d", slot); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 622 |  | 
| Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 623 | sp<IConsumerListener> listener; | 
|  | 624 | { | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 625 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 626 |  | 
|  | 627 | if (mCore->mIsAbandoned) { | 
|  | 628 | BQ_LOGE("detachBuffer: BufferQueue has been abandoned"); | 
|  | 629 | return NO_INIT; | 
|  | 630 | } | 
|  | 631 |  | 
|  | 632 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { | 
|  | 633 | BQ_LOGE("detachBuffer: BufferQueue has no connected producer"); | 
|  | 634 | return NO_INIT; | 
|  | 635 | } | 
|  | 636 |  | 
|  | 637 | if (mCore->mSharedBufferMode || mCore->mSharedBufferSlot == slot) { | 
|  | 638 | BQ_LOGE("detachBuffer: cannot detach a buffer in shared buffer mode"); | 
|  | 639 | return BAD_VALUE; | 
|  | 640 | } | 
|  | 641 |  | 
|  | 642 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { | 
|  | 643 | BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)", | 
|  | 644 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); | 
|  | 645 | return BAD_VALUE; | 
|  | 646 | } else if (!mSlots[slot].mBufferState.isDequeued()) { | 
|  | 647 | BQ_LOGE("detachBuffer: slot %d is not owned by the producer " | 
|  | 648 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); | 
|  | 649 | return BAD_VALUE; | 
|  | 650 | } else if (!mSlots[slot].mRequestBufferCalled) { | 
|  | 651 | BQ_LOGE("detachBuffer: buffer in slot %d has not been requested", | 
|  | 652 | slot); | 
|  | 653 | return BAD_VALUE; | 
|  | 654 | } | 
|  | 655 |  | 
| Adithya Srinivasan | 2e43438 | 2019-10-09 11:43:01 -0700 | [diff] [blame] | 656 | listener = mCore->mConsumerListener; | 
|  | 657 | if (listener != nullptr) { | 
|  | 658 | listener->onFrameDetached(mSlots[slot].mGraphicBuffer->getId()); | 
|  | 659 | } | 
| Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 660 | mSlots[slot].mBufferState.detachProducer(); | 
|  | 661 | mCore->mActiveBuffers.erase(slot); | 
|  | 662 | mCore->mFreeSlots.insert(slot); | 
|  | 663 | mCore->clearBufferSlotLocked(slot); | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 664 | mCore->mDequeueCondition.notify_all(); | 
| Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 665 | VALIDATE_CONSISTENCY(); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 666 | } | 
|  | 667 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 668 | if (listener != nullptr) { | 
| Eino-Ville Talvala | 93dd051 | 2016-06-10 14:21:02 -0700 | [diff] [blame] | 669 | listener->onBuffersReleased(); | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 670 | } | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 671 |  | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 672 | return NO_ERROR; | 
|  | 673 | } | 
|  | 674 |  | 
| Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 675 | status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer, | 
|  | 676 | sp<Fence>* outFence) { | 
|  | 677 | ATRACE_CALL(); | 
|  | 678 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 679 | if (outBuffer == nullptr) { | 
| Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 680 | BQ_LOGE("detachNextBuffer: outBuffer must not be NULL"); | 
|  | 681 | return BAD_VALUE; | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 682 | } else if (outFence == nullptr) { | 
| Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 683 | BQ_LOGE("detachNextBuffer: outFence must not be NULL"); | 
|  | 684 | return BAD_VALUE; | 
|  | 685 | } | 
|  | 686 |  | 
| Eino-Ville Talvala | 2672dec | 2017-06-13 16:39:11 -0700 | [diff] [blame] | 687 | sp<IConsumerListener> listener; | 
|  | 688 | { | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 689 | std::unique_lock<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 690 |  | 
| Eino-Ville Talvala | 2672dec | 2017-06-13 16:39:11 -0700 | [diff] [blame] | 691 | if (mCore->mIsAbandoned) { | 
|  | 692 | BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned"); | 
|  | 693 | return NO_INIT; | 
|  | 694 | } | 
|  | 695 |  | 
|  | 696 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { | 
|  | 697 | BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer"); | 
|  | 698 | return NO_INIT; | 
|  | 699 | } | 
|  | 700 |  | 
|  | 701 | if (mCore->mSharedBufferMode) { | 
|  | 702 | BQ_LOGE("detachNextBuffer: cannot detach a buffer in shared buffer " | 
|  | 703 | "mode"); | 
|  | 704 | return BAD_VALUE; | 
|  | 705 | } | 
|  | 706 |  | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 707 | mCore->waitWhileAllocatingLocked(lock); | 
| Eino-Ville Talvala | 2672dec | 2017-06-13 16:39:11 -0700 | [diff] [blame] | 708 |  | 
|  | 709 | if (mCore->mFreeBuffers.empty()) { | 
|  | 710 | return NO_MEMORY; | 
|  | 711 | } | 
|  | 712 |  | 
|  | 713 | int found = mCore->mFreeBuffers.front(); | 
|  | 714 | mCore->mFreeBuffers.remove(found); | 
|  | 715 | mCore->mFreeSlots.insert(found); | 
|  | 716 |  | 
|  | 717 | BQ_LOGV("detachNextBuffer detached slot %d", found); | 
|  | 718 |  | 
|  | 719 | *outBuffer = mSlots[found].mGraphicBuffer; | 
|  | 720 | *outFence = mSlots[found].mFence; | 
|  | 721 | mCore->clearBufferSlotLocked(found); | 
|  | 722 | VALIDATE_CONSISTENCY(); | 
|  | 723 | listener = mCore->mConsumerListener; | 
| Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 724 | } | 
|  | 725 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 726 | if (listener != nullptr) { | 
| Eino-Ville Talvala | 2672dec | 2017-06-13 16:39:11 -0700 | [diff] [blame] | 727 | listener->onBuffersReleased(); | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 728 | } | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 729 |  | 
| Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 730 | return NO_ERROR; | 
|  | 731 | } | 
|  | 732 |  | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 733 | status_t BufferQueueProducer::attachBuffer(int* outSlot, | 
|  | 734 | const sp<android::GraphicBuffer>& buffer) { | 
|  | 735 | ATRACE_CALL(); | 
|  | 736 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 737 | if (outSlot == nullptr) { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 738 | BQ_LOGE("attachBuffer: outSlot must not be NULL"); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 739 | return BAD_VALUE; | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 740 | } else if (buffer == nullptr) { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 741 | BQ_LOGE("attachBuffer: cannot attach NULL buffer"); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 742 | return BAD_VALUE; | 
|  | 743 | } | 
|  | 744 |  | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 745 | std::unique_lock<std::mutex> lock(mCore->mMutex); | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 746 |  | 
|  | 747 | if (mCore->mIsAbandoned) { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 748 | BQ_LOGE("attachBuffer: BufferQueue has been abandoned"); | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 749 | return NO_INIT; | 
|  | 750 | } | 
|  | 751 |  | 
|  | 752 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 753 | BQ_LOGE("attachBuffer: BufferQueue has no connected producer"); | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 754 | return NO_INIT; | 
|  | 755 | } | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 756 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 757 | if (mCore->mSharedBufferMode) { | 
|  | 758 | BQ_LOGE("attachBuffer: cannot attach a buffer in shared buffer mode"); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 759 | return BAD_VALUE; | 
|  | 760 | } | 
|  | 761 |  | 
| Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 762 | if (buffer->getGenerationNumber() != mCore->mGenerationNumber) { | 
|  | 763 | BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] " | 
|  | 764 | "[queue %u]", buffer->getGenerationNumber(), | 
|  | 765 | mCore->mGenerationNumber); | 
|  | 766 | return BAD_VALUE; | 
|  | 767 | } | 
|  | 768 |  | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 769 | mCore->waitWhileAllocatingLocked(lock); | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 770 |  | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 771 | status_t returnFlags = NO_ERROR; | 
|  | 772 | int found; | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 773 | status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, lock, &found); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 774 | if (status != NO_ERROR) { | 
|  | 775 | return status; | 
|  | 776 | } | 
|  | 777 |  | 
|  | 778 | // This should not happen | 
|  | 779 | if (found == BufferQueueCore::INVALID_BUFFER_SLOT) { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 780 | BQ_LOGE("attachBuffer: no available buffer slots"); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 781 | return -EBUSY; | 
|  | 782 | } | 
|  | 783 |  | 
|  | 784 | *outSlot = found; | 
|  | 785 | ATRACE_BUFFER_INDEX(*outSlot); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 786 | BQ_LOGV("attachBuffer: returning slot %d flags=%#x", | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 787 | *outSlot, returnFlags); | 
|  | 788 |  | 
|  | 789 | mSlots[*outSlot].mGraphicBuffer = buffer; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 790 | mSlots[*outSlot].mBufferState.attachProducer(); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 791 | mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR; | 
|  | 792 | mSlots[*outSlot].mFence = Fence::NO_FENCE; | 
| Dan Stoza | 2443c79 | 2014-03-24 15:03:46 -0700 | [diff] [blame] | 793 | mSlots[*outSlot].mRequestBufferCalled = true; | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 794 | mSlots[*outSlot].mAcquireCalled = false; | 
| Jammy Yu | 69958b8 | 2017-02-22 16:41:38 -0800 | [diff] [blame] | 795 | mSlots[*outSlot].mNeedsReallocation = false; | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 796 | mCore->mActiveBuffers.insert(found); | 
| Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 797 | VALIDATE_CONSISTENCY(); | 
| Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 798 |  | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 799 | return returnFlags; | 
|  | 800 | } | 
|  | 801 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 802 | status_t BufferQueueProducer::queueBuffer(int slot, | 
|  | 803 | const QueueBufferInput &input, QueueBufferOutput *output) { | 
|  | 804 | ATRACE_CALL(); | 
|  | 805 | ATRACE_BUFFER_INDEX(slot); | 
|  | 806 |  | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 807 | int64_t requestedPresentTimestamp; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 808 | bool isAutoTimestamp; | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 809 | android_dataspace dataSpace; | 
| Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 810 | Rect crop(Rect::EMPTY_RECT); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 811 | int scalingMode; | 
|  | 812 | uint32_t transform; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 813 | uint32_t stickyTransform; | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 814 | sp<Fence> acquireFence; | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 815 | bool getFrameTimestamps = false; | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 816 | input.deflate(&requestedPresentTimestamp, &isAutoTimestamp, &dataSpace, | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 817 | &crop, &scalingMode, &transform, &acquireFence, &stickyTransform, | 
|  | 818 | &getFrameTimestamps); | 
| Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 819 | const Region& surfaceDamage = input.getSurfaceDamage(); | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 820 | const HdrMetadata& hdrMetadata = input.getHdrMetadata(); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 821 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 822 | if (acquireFence == nullptr) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 823 | BQ_LOGE("queueBuffer: fence is NULL"); | 
| Jesse Hall | de288fe | 2014-11-04 08:30:48 -0800 | [diff] [blame] | 824 | return BAD_VALUE; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 825 | } | 
|  | 826 |  | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 827 | auto acquireFenceTime = std::make_shared<FenceTime>(acquireFence); | 
|  | 828 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 829 | switch (scalingMode) { | 
|  | 830 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: | 
|  | 831 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: | 
|  | 832 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: | 
|  | 833 | case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: | 
|  | 834 | break; | 
|  | 835 | default: | 
|  | 836 | BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 837 | return BAD_VALUE; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 838 | } | 
|  | 839 |  | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 840 | sp<IConsumerListener> frameAvailableListener; | 
|  | 841 | sp<IConsumerListener> frameReplacedListener; | 
|  | 842 | int callbackTicket = 0; | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 843 | uint64_t currentFrameNumber = 0; | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 844 | BufferItem item; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 845 | { // Autolock scope | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 846 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 847 |  | 
|  | 848 | if (mCore->mIsAbandoned) { | 
|  | 849 | BQ_LOGE("queueBuffer: BufferQueue has been abandoned"); | 
|  | 850 | return NO_INIT; | 
|  | 851 | } | 
|  | 852 |  | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 853 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { | 
|  | 854 | BQ_LOGE("queueBuffer: BufferQueue has no connected producer"); | 
|  | 855 | return NO_INIT; | 
|  | 856 | } | 
|  | 857 |  | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 858 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 859 | BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)", | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 860 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 861 | return BAD_VALUE; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 862 | } else if (!mSlots[slot].mBufferState.isDequeued()) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 863 | BQ_LOGE("queueBuffer: slot %d is not owned by the producer " | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 864 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 865 | return BAD_VALUE; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 866 | } else if (!mSlots[slot].mRequestBufferCalled) { | 
|  | 867 | BQ_LOGE("queueBuffer: slot %d was queued without requesting " | 
|  | 868 | "a buffer", slot); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 869 | return BAD_VALUE; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 870 | } | 
|  | 871 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 872 | // If shared buffer mode has just been enabled, cache the slot of the | 
| Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 873 | // first buffer that is queued and mark it as the shared buffer. | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 874 | if (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == | 
| Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 875 | BufferQueueCore::INVALID_BUFFER_SLOT) { | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 876 | mCore->mSharedBufferSlot = slot; | 
| Pablo Ceballos | 295a9fc | 2016-03-14 16:02:19 -0700 | [diff] [blame] | 877 | mSlots[slot].mBufferState.mShared = true; | 
|  | 878 | } | 
|  | 879 |  | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 880 | BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d" | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 881 | " validHdrMetadataTypes=0x%x crop=[%d,%d,%d,%d] transform=%#x scale=%s", | 
|  | 882 | slot, mCore->mFrameCounter + 1, requestedPresentTimestamp, dataSpace, | 
|  | 883 | hdrMetadata.validTypes, crop.left, crop.top, crop.right, crop.bottom, | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 884 | transform, | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 885 | BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode))); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 886 |  | 
|  | 887 | const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer); | 
|  | 888 | Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight()); | 
| Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 889 | Rect croppedRect(Rect::EMPTY_RECT); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 890 | crop.intersect(bufferRect, &croppedRect); | 
|  | 891 | if (croppedRect != crop) { | 
|  | 892 | BQ_LOGE("queueBuffer: crop rect is not contained within the " | 
|  | 893 | "buffer in slot %d", slot); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 894 | return BAD_VALUE; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 895 | } | 
|  | 896 |  | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 897 | // Override UNKNOWN dataspace with consumer default | 
|  | 898 | if (dataSpace == HAL_DATASPACE_UNKNOWN) { | 
|  | 899 | dataSpace = mCore->mDefaultBufferDataSpace; | 
|  | 900 | } | 
|  | 901 |  | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 902 | mSlots[slot].mFence = acquireFence; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 903 | mSlots[slot].mBufferState.queue(); | 
|  | 904 |  | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 905 | // Increment the frame counter and store a local version of it | 
|  | 906 | // for use outside the lock on mCore->mMutex. | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 907 | ++mCore->mFrameCounter; | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 908 | currentFrameNumber = mCore->mFrameCounter; | 
|  | 909 | mSlots[slot].mFrameNumber = currentFrameNumber; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 910 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 911 | item.mAcquireCalled = mSlots[slot].mAcquireCalled; | 
|  | 912 | item.mGraphicBuffer = mSlots[slot].mGraphicBuffer; | 
|  | 913 | item.mCrop = crop; | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 914 | item.mTransform = transform & | 
|  | 915 | ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 916 | item.mTransformToDisplayInverse = | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 917 | (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0; | 
|  | 918 | item.mScalingMode = static_cast<uint32_t>(scalingMode); | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 919 | item.mTimestamp = requestedPresentTimestamp; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 920 | item.mIsAutoTimestamp = isAutoTimestamp; | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 921 | item.mDataSpace = dataSpace; | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 922 | item.mHdrMetadata = hdrMetadata; | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 923 | item.mFrameNumber = currentFrameNumber; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 924 | item.mSlot = slot; | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 925 | item.mFence = acquireFence; | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 926 | item.mFenceTime = acquireFenceTime; | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 927 | item.mIsDroppable = mCore->mAsyncMode || | 
| Sungtak Lee | 45e9e0b | 2019-05-28 13:38:23 -0700 | [diff] [blame] | 928 | (mConsumerIsSurfaceFlinger && mCore->mQueueBufferCanDrop) || | 
| Sungtak Lee | 3249fb6 | 2019-03-02 16:40:47 -0800 | [diff] [blame] | 929 | (mCore->mLegacyBufferDrop && mCore->mQueueBufferCanDrop) || | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 930 | (mCore->mSharedBufferMode && mCore->mSharedBufferSlot == slot); | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 931 | item.mSurfaceDamage = surfaceDamage; | 
| Pablo Ceballos | 0631218 | 2015-10-07 16:32:12 -0700 | [diff] [blame] | 932 | item.mQueuedBuffer = true; | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 933 | item.mAutoRefresh = mCore->mSharedBufferMode && mCore->mAutoRefresh; | 
| Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 934 | item.mApi = mCore->mConnectedApi; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 935 |  | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 936 | mStickyTransform = stickyTransform; | 
|  | 937 |  | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 938 | // Cache the shared buffer data so that the BufferItem can be recreated. | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 939 | if (mCore->mSharedBufferMode) { | 
|  | 940 | mCore->mSharedBufferCache.crop = crop; | 
|  | 941 | mCore->mSharedBufferCache.transform = transform; | 
|  | 942 | mCore->mSharedBufferCache.scalingMode = static_cast<uint32_t>( | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 943 | scalingMode); | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 944 | mCore->mSharedBufferCache.dataspace = dataSpace; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 945 | } | 
|  | 946 |  | 
| Shuzhen Wang | 22f842b | 2017-01-18 23:02:36 -0800 | [diff] [blame] | 947 | output->bufferReplaced = false; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 948 | if (mCore->mQueue.empty()) { | 
|  | 949 | // When the queue is empty, we can ignore mDequeueBufferCannotBlock | 
|  | 950 | // and simply queue this buffer | 
|  | 951 | mCore->mQueue.push_back(item); | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 952 | frameAvailableListener = mCore->mConsumerListener; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 953 | } else { | 
| Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 954 | // When the queue is not empty, we need to look at the last buffer | 
|  | 955 | // in the queue to see if we need to replace it | 
|  | 956 | const BufferItem& last = mCore->mQueue.itemAt( | 
|  | 957 | mCore->mQueue.size() - 1); | 
|  | 958 | if (last.mIsDroppable) { | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 959 |  | 
| Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 960 | if (!last.mIsStale) { | 
|  | 961 | mSlots[last.mSlot].mBufferState.freeQueued(); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 962 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 963 | // After leaving shared buffer mode, the shared buffer will | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 964 | // still be around. Mark it as no longer shared if this | 
|  | 965 | // operation causes it to be free. | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 966 | if (!mCore->mSharedBufferMode && | 
| Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 967 | mSlots[last.mSlot].mBufferState.isFree()) { | 
|  | 968 | mSlots[last.mSlot].mBufferState.mShared = false; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 969 | } | 
|  | 970 | // Don't put the shared buffer on the free list. | 
| Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 971 | if (!mSlots[last.mSlot].mBufferState.isShared()) { | 
|  | 972 | mCore->mActiveBuffers.erase(last.mSlot); | 
|  | 973 | mCore->mFreeBuffers.push_back(last.mSlot); | 
| Shuzhen Wang | 22f842b | 2017-01-18 23:02:36 -0800 | [diff] [blame] | 974 | output->bufferReplaced = true; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 975 | } | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 976 | } | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 977 |  | 
| Steven Thomas | 862e753 | 2019-07-10 19:21:03 -0700 | [diff] [blame] | 978 | // Make sure to merge the damage rect from the frame we're about | 
|  | 979 | // to drop into the new frame's damage rect. | 
|  | 980 | if (last.mSurfaceDamage.bounds() == Rect::INVALID_RECT || | 
|  | 981 | item.mSurfaceDamage.bounds() == Rect::INVALID_RECT) { | 
|  | 982 | item.mSurfaceDamage = Region::INVALID_REGION; | 
|  | 983 | } else { | 
|  | 984 | item.mSurfaceDamage |= last.mSurfaceDamage; | 
|  | 985 | } | 
|  | 986 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 987 | // Overwrite the droppable buffer with the incoming one | 
| Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 988 | mCore->mQueue.editItemAt(mCore->mQueue.size() - 1) = item; | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 989 | frameReplacedListener = mCore->mConsumerListener; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 990 | } else { | 
|  | 991 | mCore->mQueue.push_back(item); | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 992 | frameAvailableListener = mCore->mConsumerListener; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 993 | } | 
|  | 994 | } | 
|  | 995 |  | 
|  | 996 | mCore->mBufferHasBeenQueued = true; | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 997 | mCore->mDequeueCondition.notify_all(); | 
| Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 998 | mCore->mLastQueuedSlot = slot; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 999 |  | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1000 | output->width = mCore->mDefaultWidth; | 
|  | 1001 | output->height = mCore->mDefaultHeight; | 
| Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1002 | output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint; | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1003 | output->numPendingBuffers = static_cast<uint32_t>(mCore->mQueue.size()); | 
|  | 1004 | output->nextFrameNumber = mCore->mFrameCounter + 1; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1005 |  | 
| Colin Cross | 152c3b7 | 2016-09-27 14:08:19 -0700 | [diff] [blame] | 1006 | ATRACE_INT(mCore->mConsumerName.string(), | 
|  | 1007 | static_cast<int32_t>(mCore->mQueue.size())); | 
| Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 1008 | mCore->mOccupancyTracker.registerOccupancyChange(mCore->mQueue.size()); | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1009 |  | 
|  | 1010 | // Take a ticket for the callback functions | 
|  | 1011 | callbackTicket = mNextCallbackTicket++; | 
| Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 1012 |  | 
| Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 1013 | VALIDATE_CONSISTENCY(); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1014 | } // Autolock scope | 
|  | 1015 |  | 
| Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 1016 | // It is okay not to clear the GraphicBuffer when the consumer is SurfaceFlinger because | 
|  | 1017 | // it is guaranteed that the BufferQueue is inside SurfaceFlinger's process and | 
|  | 1018 | // there will be no Binder call | 
|  | 1019 | if (!mConsumerIsSurfaceFlinger) { | 
|  | 1020 | item.mGraphicBuffer.clear(); | 
|  | 1021 | } | 
|  | 1022 |  | 
| JihCheng Chiu | 544c522 | 2019-04-02 20:50:58 +0800 | [diff] [blame] | 1023 | // Update and get FrameEventHistory. | 
|  | 1024 | nsecs_t postedTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1025 | NewFrameEventsEntry newFrameEventsEntry = { | 
|  | 1026 | currentFrameNumber, | 
|  | 1027 | postedTime, | 
|  | 1028 | requestedPresentTimestamp, | 
|  | 1029 | std::move(acquireFenceTime) | 
|  | 1030 | }; | 
|  | 1031 | addAndGetFrameTimestamps(&newFrameEventsEntry, | 
|  | 1032 | getFrameTimestamps ? &output->frameTimestamps : nullptr); | 
|  | 1033 |  | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1034 | // Call back without the main BufferQueue lock held, but with the callback | 
|  | 1035 | // lock held so we can ensure that callbacks occur in order | 
| Mathias Agopian | 4f6ce7c | 2017-04-03 17:14:31 -0700 | [diff] [blame] | 1036 |  | 
|  | 1037 | int connectedApi; | 
|  | 1038 | sp<Fence> lastQueuedFence; | 
|  | 1039 |  | 
|  | 1040 | { // scope for the lock | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1041 | std::unique_lock<std::mutex> lock(mCallbackMutex); | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1042 | while (callbackTicket != mCurrentCallbackTicket) { | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1043 | mCallbackCondition.wait(lock); | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1044 | } | 
|  | 1045 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1046 | if (frameAvailableListener != nullptr) { | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1047 | frameAvailableListener->onFrameAvailable(item); | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1048 | } else if (frameReplacedListener != nullptr) { | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1049 | frameReplacedListener->onFrameReplaced(item); | 
|  | 1050 | } | 
|  | 1051 |  | 
| Mathias Agopian | 4f6ce7c | 2017-04-03 17:14:31 -0700 | [diff] [blame] | 1052 | connectedApi = mCore->mConnectedApi; | 
|  | 1053 | lastQueuedFence = std::move(mLastQueueBufferFence); | 
|  | 1054 |  | 
|  | 1055 | mLastQueueBufferFence = std::move(acquireFence); | 
|  | 1056 | mLastQueuedCrop = item.mCrop; | 
|  | 1057 | mLastQueuedTransform = item.mTransform; | 
|  | 1058 |  | 
| Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 1059 | ++mCurrentCallbackTicket; | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1060 | mCallbackCondition.notify_all(); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1061 | } | 
|  | 1062 |  | 
| Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 1063 | // Wait without lock held | 
|  | 1064 | if (connectedApi == NATIVE_WINDOW_API_EGL) { | 
|  | 1065 | // Waiting here allows for two full buffers to be queued but not a | 
|  | 1066 | // third. In the event that frames take varying time, this makes a | 
|  | 1067 | // small trade-off in favor of latency rather than throughput. | 
|  | 1068 | lastQueuedFence->waitForever("Throttling EGL Production"); | 
|  | 1069 | } | 
|  | 1070 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1071 | return NO_ERROR; | 
|  | 1072 | } | 
|  | 1073 |  | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 1074 | status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1075 | ATRACE_CALL(); | 
|  | 1076 | BQ_LOGV("cancelBuffer: slot %d", slot); | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1077 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1078 |  | 
|  | 1079 | if (mCore->mIsAbandoned) { | 
|  | 1080 | BQ_LOGE("cancelBuffer: BufferQueue has been abandoned"); | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 1081 | return NO_INIT; | 
|  | 1082 | } | 
|  | 1083 |  | 
|  | 1084 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { | 
|  | 1085 | BQ_LOGE("cancelBuffer: BufferQueue has no connected producer"); | 
|  | 1086 | return NO_INIT; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1087 | } | 
|  | 1088 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1089 | if (mCore->mSharedBufferMode) { | 
|  | 1090 | BQ_LOGE("cancelBuffer: cannot cancel a buffer in shared buffer mode"); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1091 | return BAD_VALUE; | 
|  | 1092 | } | 
|  | 1093 |  | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 1094 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1095 | BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", | 
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 1096 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 1097 | return BAD_VALUE; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1098 | } else if (!mSlots[slot].mBufferState.isDequeued()) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1099 | BQ_LOGE("cancelBuffer: slot %d is not owned by the producer " | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1100 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 1101 | return BAD_VALUE; | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1102 | } else if (fence == nullptr) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1103 | BQ_LOGE("cancelBuffer: fence is NULL"); | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 1104 | return BAD_VALUE; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1105 | } | 
|  | 1106 |  | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1107 | mSlots[slot].mBufferState.cancel(); | 
|  | 1108 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1109 | // After leaving shared buffer mode, the shared buffer will still be around. | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1110 | // Mark it as no longer shared if this operation causes it to be free. | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1111 | if (!mCore->mSharedBufferMode && mSlots[slot].mBufferState.isFree()) { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1112 | mSlots[slot].mBufferState.mShared = false; | 
|  | 1113 | } | 
|  | 1114 |  | 
|  | 1115 | // Don't put the shared buffer on the free list. | 
|  | 1116 | if (!mSlots[slot].mBufferState.isShared()) { | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1117 | mCore->mActiveBuffers.erase(slot); | 
|  | 1118 | mCore->mFreeBuffers.push_back(slot); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1119 | } | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1120 |  | 
| Adithya Srinivasan | 2e43438 | 2019-10-09 11:43:01 -0700 | [diff] [blame] | 1121 | if (mCore->mConsumerListener != nullptr) { | 
|  | 1122 | mCore->mConsumerListener->onFrameCancelled(mSlots[slot].mGraphicBuffer->getId()); | 
|  | 1123 | } | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1124 | mSlots[slot].mFence = fence; | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1125 | mCore->mDequeueCondition.notify_all(); | 
| Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 1126 | VALIDATE_CONSISTENCY(); | 
| Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 1127 |  | 
|  | 1128 | return NO_ERROR; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1129 | } | 
|  | 1130 |  | 
|  | 1131 | int BufferQueueProducer::query(int what, int *outValue) { | 
|  | 1132 | ATRACE_CALL(); | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1133 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1134 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1135 | if (outValue == nullptr) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1136 | BQ_LOGE("query: outValue was NULL"); | 
|  | 1137 | return BAD_VALUE; | 
|  | 1138 | } | 
|  | 1139 |  | 
|  | 1140 | if (mCore->mIsAbandoned) { | 
|  | 1141 | BQ_LOGE("query: BufferQueue has been abandoned"); | 
|  | 1142 | return NO_INIT; | 
|  | 1143 | } | 
|  | 1144 |  | 
|  | 1145 | int value; | 
|  | 1146 | switch (what) { | 
|  | 1147 | case NATIVE_WINDOW_WIDTH: | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1148 | value = static_cast<int32_t>(mCore->mDefaultWidth); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1149 | break; | 
|  | 1150 | case NATIVE_WINDOW_HEIGHT: | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1151 | value = static_cast<int32_t>(mCore->mDefaultHeight); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1152 | break; | 
|  | 1153 | case NATIVE_WINDOW_FORMAT: | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1154 | value = static_cast<int32_t>(mCore->mDefaultBufferFormat); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1155 | break; | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 1156 | case NATIVE_WINDOW_LAYER_COUNT: | 
|  | 1157 | // All BufferQueue buffers have a single layer. | 
|  | 1158 | value = BQ_LAYER_COUNT; | 
|  | 1159 | break; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1160 | case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS: | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 1161 | value = mCore->getMinUndequeuedBufferCountLocked(); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1162 | break; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1163 | case NATIVE_WINDOW_STICKY_TRANSFORM: | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1164 | value = static_cast<int32_t>(mStickyTransform); | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1165 | break; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1166 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: | 
|  | 1167 | value = (mCore->mQueue.size() > 1); | 
|  | 1168 | break; | 
|  | 1169 | case NATIVE_WINDOW_CONSUMER_USAGE_BITS: | 
| Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1170 | // deprecated; higher 32 bits are truncated | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1171 | value = static_cast<int32_t>(mCore->mConsumerUsageBits); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1172 | break; | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1173 | case NATIVE_WINDOW_DEFAULT_DATASPACE: | 
|  | 1174 | value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace); | 
|  | 1175 | break; | 
| Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 1176 | case NATIVE_WINDOW_BUFFER_AGE: | 
|  | 1177 | if (mCore->mBufferAge > INT32_MAX) { | 
|  | 1178 | value = 0; | 
|  | 1179 | } else { | 
|  | 1180 | value = static_cast<int32_t>(mCore->mBufferAge); | 
|  | 1181 | } | 
|  | 1182 | break; | 
| Jiwen 'Steve' Cai | 2041913 | 2017-04-21 18:49:53 -0700 | [diff] [blame] | 1183 | case NATIVE_WINDOW_CONSUMER_IS_PROTECTED: | 
|  | 1184 | value = static_cast<int32_t>(mCore->mConsumerIsProtected); | 
|  | 1185 | break; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1186 | default: | 
|  | 1187 | return BAD_VALUE; | 
|  | 1188 | } | 
|  | 1189 |  | 
|  | 1190 | BQ_LOGV("query: %d? %d", what, value); | 
|  | 1191 | *outValue = value; | 
|  | 1192 | return NO_ERROR; | 
|  | 1193 | } | 
|  | 1194 |  | 
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1195 | status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener, | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1196 | int api, bool producerControlledByApp, QueueBufferOutput *output) { | 
|  | 1197 | ATRACE_CALL(); | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1198 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1199 | mConsumerName = mCore->mConsumerName; | 
|  | 1200 | BQ_LOGV("connect: api=%d producerControlledByApp=%s", api, | 
|  | 1201 | producerControlledByApp ? "true" : "false"); | 
|  | 1202 |  | 
|  | 1203 | if (mCore->mIsAbandoned) { | 
|  | 1204 | BQ_LOGE("connect: BufferQueue has been abandoned"); | 
|  | 1205 | return NO_INIT; | 
|  | 1206 | } | 
|  | 1207 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1208 | if (mCore->mConsumerListener == nullptr) { | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1209 | BQ_LOGE("connect: BufferQueue has no consumer"); | 
|  | 1210 | return NO_INIT; | 
|  | 1211 | } | 
|  | 1212 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1213 | if (output == nullptr) { | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1214 | BQ_LOGE("connect: output was NULL"); | 
|  | 1215 | return BAD_VALUE; | 
|  | 1216 | } | 
|  | 1217 |  | 
|  | 1218 | if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) { | 
|  | 1219 | BQ_LOGE("connect: already connected (cur=%d req=%d)", | 
|  | 1220 | mCore->mConnectedApi, api); | 
|  | 1221 | return BAD_VALUE; | 
|  | 1222 | } | 
|  | 1223 |  | 
|  | 1224 | int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, | 
|  | 1225 | mDequeueTimeout < 0 ? | 
|  | 1226 | mCore->mConsumerControlledByApp && producerControlledByApp : false, | 
|  | 1227 | mCore->mMaxBufferCount) - | 
|  | 1228 | mCore->getMaxBufferCountLocked(); | 
|  | 1229 | if (!mCore->adjustAvailableSlotsLocked(delta)) { | 
|  | 1230 | BQ_LOGE("connect: BufferQueue failed to adjust the number of available " | 
|  | 1231 | "slots. Delta = %d", delta); | 
|  | 1232 | return BAD_VALUE; | 
|  | 1233 | } | 
|  | 1234 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1235 | int status = NO_ERROR; | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1236 | switch (api) { | 
|  | 1237 | case NATIVE_WINDOW_API_EGL: | 
|  | 1238 | case NATIVE_WINDOW_API_CPU: | 
|  | 1239 | case NATIVE_WINDOW_API_MEDIA: | 
|  | 1240 | case NATIVE_WINDOW_API_CAMERA: | 
|  | 1241 | mCore->mConnectedApi = api; | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1242 |  | 
|  | 1243 | output->width = mCore->mDefaultWidth; | 
|  | 1244 | output->height = mCore->mDefaultHeight; | 
| Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1245 | output->transformHint = mCore->mTransformHintInUse = mCore->mTransformHint; | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1246 | output->numPendingBuffers = | 
|  | 1247 | static_cast<uint32_t>(mCore->mQueue.size()); | 
|  | 1248 | output->nextFrameNumber = mCore->mFrameCounter + 1; | 
| Shuzhen Wang | 22f842b | 2017-01-18 23:02:36 -0800 | [diff] [blame] | 1249 | output->bufferReplaced = false; | 
| silence_dogood | e9d092a | 2019-06-19 16:14:53 -0700 | [diff] [blame] | 1250 | output->maxBufferCount = mCore->mMaxBufferCount; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1251 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1252 | if (listener != nullptr) { | 
| Matthew Bouyack | 3b8e6b2 | 2016-10-03 16:24:26 -0700 | [diff] [blame] | 1253 | // Set up a death notification so that we can disconnect | 
|  | 1254 | // automatically if the remote producer dies | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1255 | if (IInterface::asBinder(listener)->remoteBinder() != nullptr) { | 
| Matthew Bouyack | 3b8e6b2 | 2016-10-03 16:24:26 -0700 | [diff] [blame] | 1256 | status = IInterface::asBinder(listener)->linkToDeath( | 
|  | 1257 | static_cast<IBinder::DeathRecipient*>(this)); | 
|  | 1258 | if (status != NO_ERROR) { | 
|  | 1259 | BQ_LOGE("connect: linkToDeath failed: %s (%d)", | 
|  | 1260 | strerror(-status), status); | 
|  | 1261 | } | 
|  | 1262 | mCore->mLinkedToDeath = listener; | 
|  | 1263 | } | 
| Shuzhen Wang | 067fcd3 | 2019-08-14 10:41:12 -0700 | [diff] [blame] | 1264 | mCore->mConnectedProducerListener = listener; | 
|  | 1265 | mCore->mBufferReleasedCbEnabled = listener->needsReleaseNotify(); | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1266 | } | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1267 | break; | 
|  | 1268 | default: | 
|  | 1269 | BQ_LOGE("connect: unknown API %d", api); | 
|  | 1270 | status = BAD_VALUE; | 
|  | 1271 | break; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1272 | } | 
| Jayant Chowdhary | ad9fe27 | 2019-03-07 22:36:06 -0800 | [diff] [blame] | 1273 | mCore->mConnectedPid = BufferQueueThreadState::getCallingPid(); | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1274 | mCore->mBufferHasBeenQueued = false; | 
|  | 1275 | mCore->mDequeueBufferCannotBlock = false; | 
| Sungtak Lee | 3249fb6 | 2019-03-02 16:40:47 -0800 | [diff] [blame] | 1276 | mCore->mQueueBufferCanDrop = false; | 
|  | 1277 | mCore->mLegacyBufferDrop = true; | 
|  | 1278 | if (mCore->mConsumerControlledByApp && producerControlledByApp) { | 
|  | 1279 | mCore->mDequeueBufferCannotBlock = mDequeueTimeout < 0; | 
|  | 1280 | mCore->mQueueBufferCanDrop = mDequeueTimeout <= 0; | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1281 | } | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1282 |  | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1283 | mCore->mAllowAllocation = true; | 
|  | 1284 | VALIDATE_CONSISTENCY(); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1285 | return status; | 
|  | 1286 | } | 
|  | 1287 |  | 
| Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1288 | status_t BufferQueueProducer::disconnect(int api, DisconnectMode mode) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1289 | ATRACE_CALL(); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1290 | BQ_LOGV("disconnect: api %d", api); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1291 |  | 
|  | 1292 | int status = NO_ERROR; | 
|  | 1293 | sp<IConsumerListener> listener; | 
|  | 1294 | { // Autolock scope | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1295 | std::unique_lock<std::mutex> lock(mCore->mMutex); | 
| Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1296 |  | 
|  | 1297 | if (mode == DisconnectMode::AllLocal) { | 
| Jayant Chowdhary | ad9fe27 | 2019-03-07 22:36:06 -0800 | [diff] [blame] | 1298 | if (BufferQueueThreadState::getCallingPid() != mCore->mConnectedPid) { | 
| Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1299 | return NO_ERROR; | 
|  | 1300 | } | 
|  | 1301 | api = BufferQueueCore::CURRENTLY_CONNECTED_API; | 
|  | 1302 | } | 
|  | 1303 |  | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1304 | mCore->waitWhileAllocatingLocked(lock); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1305 |  | 
|  | 1306 | if (mCore->mIsAbandoned) { | 
|  | 1307 | // It's not really an error to disconnect after the surface has | 
|  | 1308 | // been abandoned; it should just be a no-op. | 
|  | 1309 | return NO_ERROR; | 
|  | 1310 | } | 
|  | 1311 |  | 
| Chong Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 1312 | if (api == BufferQueueCore::CURRENTLY_CONNECTED_API) { | 
| Chong Zhang | 5ac5148 | 2017-02-16 15:52:33 -0800 | [diff] [blame] | 1313 | if (mCore->mConnectedApi == NATIVE_WINDOW_API_MEDIA) { | 
|  | 1314 | ALOGD("About to force-disconnect API_MEDIA, mode=%d", mode); | 
|  | 1315 | } | 
| Chong Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 1316 | api = mCore->mConnectedApi; | 
| Chong Zhang | 26bb2b1 | 2016-03-08 12:08:33 -0800 | [diff] [blame] | 1317 | // If we're asked to disconnect the currently connected api but | 
|  | 1318 | // nobody is connected, it's not really an error. | 
|  | 1319 | if (api == BufferQueueCore::NO_CONNECTED_API) { | 
|  | 1320 | return NO_ERROR; | 
|  | 1321 | } | 
| Chong Zhang | 1b3a9ac | 2016-02-29 16:47:47 -0800 | [diff] [blame] | 1322 | } | 
|  | 1323 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1324 | switch (api) { | 
|  | 1325 | case NATIVE_WINDOW_API_EGL: | 
|  | 1326 | case NATIVE_WINDOW_API_CPU: | 
|  | 1327 | case NATIVE_WINDOW_API_MEDIA: | 
|  | 1328 | case NATIVE_WINDOW_API_CAMERA: | 
|  | 1329 | if (mCore->mConnectedApi == api) { | 
|  | 1330 | mCore->freeAllBuffersLocked(); | 
|  | 1331 |  | 
|  | 1332 | // Remove our death notification callback if we have one | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1333 | if (mCore->mLinkedToDeath != nullptr) { | 
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1334 | sp<IBinder> token = | 
| Matthew Bouyack | 3b8e6b2 | 2016-10-03 16:24:26 -0700 | [diff] [blame] | 1335 | IInterface::asBinder(mCore->mLinkedToDeath); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1336 | // This can fail if we're here because of the death | 
|  | 1337 | // notification, but we just ignore it | 
|  | 1338 | token->unlinkToDeath( | 
|  | 1339 | static_cast<IBinder::DeathRecipient*>(this)); | 
|  | 1340 | } | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1341 | mCore->mSharedBufferSlot = | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1342 | BufferQueueCore::INVALID_BUFFER_SLOT; | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1343 | mCore->mLinkedToDeath = nullptr; | 
|  | 1344 | mCore->mConnectedProducerListener = nullptr; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1345 | mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API; | 
| Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1346 | mCore->mConnectedPid = -1; | 
| Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1347 | mCore->mSidebandStream.clear(); | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1348 | mCore->mDequeueCondition.notify_all(); | 
| Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1349 | mCore->mAutoPrerotation = false; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1350 | listener = mCore->mConsumerListener; | 
| Wonsik Kim | 3e198b2 | 2017-04-07 15:43:16 -0700 | [diff] [blame] | 1351 | } else if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { | 
|  | 1352 | BQ_LOGE("disconnect: not connected (req=%d)", api); | 
|  | 1353 | status = NO_INIT; | 
|  | 1354 | } else { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1355 | BQ_LOGE("disconnect: still connected to another API " | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1356 | "(cur=%d req=%d)", mCore->mConnectedApi, api); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 1357 | status = BAD_VALUE; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1358 | } | 
|  | 1359 | break; | 
|  | 1360 | default: | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1361 | BQ_LOGE("disconnect: unknown API %d", api); | 
| Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 1362 | status = BAD_VALUE; | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1363 | break; | 
|  | 1364 | } | 
|  | 1365 | } // Autolock scope | 
|  | 1366 |  | 
|  | 1367 | // Call back without lock held | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1368 | if (listener != nullptr) { | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1369 | listener->onBuffersReleased(); | 
| Brian Anderson | 5ea5e59 | 2016-12-01 16:54:33 -0800 | [diff] [blame] | 1370 | listener->onDisconnect(); | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1371 | } | 
|  | 1372 |  | 
|  | 1373 | return status; | 
|  | 1374 | } | 
|  | 1375 |  | 
| Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1376 | status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) { | 
| Wonsik Kim | afe3081 | 2014-03-31 23:16:08 +0900 | [diff] [blame] | 1377 | sp<IConsumerListener> listener; | 
|  | 1378 | { // Autolock scope | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1379 | std::lock_guard<std::mutex> _l(mCore->mMutex); | 
| Wonsik Kim | afe3081 | 2014-03-31 23:16:08 +0900 | [diff] [blame] | 1380 | mCore->mSidebandStream = stream; | 
|  | 1381 | listener = mCore->mConsumerListener; | 
|  | 1382 | } // Autolock scope | 
|  | 1383 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1384 | if (listener != nullptr) { | 
| Wonsik Kim | afe3081 | 2014-03-31 23:16:08 +0900 | [diff] [blame] | 1385 | listener->onSidebandStreamChanged(); | 
|  | 1386 | } | 
| Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1387 | return NO_ERROR; | 
|  | 1388 | } | 
|  | 1389 |  | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 1390 | void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height, | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1391 | PixelFormat format, uint64_t usage) { | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1392 | ATRACE_CALL(); | 
| Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1393 |  | 
|  | 1394 | const bool useDefaultSize = !width && !height; | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1395 | while (true) { | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1396 | size_t newBufferCount = 0; | 
|  | 1397 | uint32_t allocWidth = 0; | 
|  | 1398 | uint32_t allocHeight = 0; | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1399 | PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN; | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1400 | uint64_t allocUsage = 0; | 
| Chia-I Wu | 1dbf0e3 | 2018-02-07 14:40:08 -0800 | [diff] [blame] | 1401 | std::string allocName; | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1402 | { // Autolock scope | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1403 | std::unique_lock<std::mutex> lock(mCore->mMutex); | 
|  | 1404 | mCore->waitWhileAllocatingLocked(lock); | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1405 |  | 
| Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 1406 | if (!mCore->mAllowAllocation) { | 
|  | 1407 | BQ_LOGE("allocateBuffers: allocation is not allowed for this " | 
|  | 1408 | "BufferQueue"); | 
|  | 1409 | return; | 
|  | 1410 | } | 
|  | 1411 |  | 
| Jorim Jaggi | 0a3e784 | 2018-07-17 13:48:33 +0200 | [diff] [blame] | 1412 | // Only allocate one buffer at a time to reduce risks of overlapping an allocation from | 
|  | 1413 | // both allocateBuffers and dequeueBuffer. | 
|  | 1414 | newBufferCount = mCore->mFreeSlots.empty() ? 0 : 1; | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1415 | if (newBufferCount == 0) { | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1416 | return; | 
|  | 1417 | } | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1418 |  | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1419 | allocWidth = width > 0 ? width : mCore->mDefaultWidth; | 
|  | 1420 | allocHeight = height > 0 ? height : mCore->mDefaultHeight; | 
| Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1421 | if (useDefaultSize && mCore->mAutoPrerotation && | 
|  | 1422 | (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) { | 
|  | 1423 | std::swap(allocWidth, allocHeight); | 
|  | 1424 | } | 
|  | 1425 |  | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1426 | allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat; | 
|  | 1427 | allocUsage = usage | mCore->mConsumerUsageBits; | 
| Chia-I Wu | 1dbf0e3 | 2018-02-07 14:40:08 -0800 | [diff] [blame] | 1428 | allocName.assign(mCore->mConsumerName.string(), mCore->mConsumerName.size()); | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1429 |  | 
|  | 1430 | mCore->mIsAllocating = true; | 
|  | 1431 | } // Autolock scope | 
|  | 1432 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1433 | Vector<sp<GraphicBuffer>> buffers; | 
| Jorim Jaggi | 0a3e784 | 2018-07-17 13:48:33 +0200 | [diff] [blame] | 1434 | for (size_t i = 0; i < newBufferCount; ++i) { | 
| Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 1435 | sp<GraphicBuffer> graphicBuffer = new GraphicBuffer( | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 1436 | allocWidth, allocHeight, allocFormat, BQ_LAYER_COUNT, | 
| Chia-I Wu | 1dbf0e3 | 2018-02-07 14:40:08 -0800 | [diff] [blame] | 1437 | allocUsage, allocName); | 
| Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 1438 |  | 
|  | 1439 | status_t result = graphicBuffer->initCheck(); | 
|  | 1440 |  | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1441 | if (result != NO_ERROR) { | 
|  | 1442 | BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format" | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1443 | " %u, usage %#" PRIx64 ")", width, height, format, usage); | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1444 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1445 | mCore->mIsAllocating = false; | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1446 | mCore->mIsAllocatingCondition.notify_all(); | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1447 | return; | 
|  | 1448 | } | 
|  | 1449 | buffers.push_back(graphicBuffer); | 
|  | 1450 | } | 
|  | 1451 |  | 
|  | 1452 | { // Autolock scope | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1453 | std::unique_lock<std::mutex> lock(mCore->mMutex); | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1454 | uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth; | 
|  | 1455 | uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight; | 
| Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1456 | if (useDefaultSize && mCore->mAutoPrerotation && | 
|  | 1457 | (mCore->mTransformHintInUse & NATIVE_WINDOW_TRANSFORM_ROT_90)) { | 
|  | 1458 | std::swap(checkWidth, checkHeight); | 
|  | 1459 | } | 
|  | 1460 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1461 | PixelFormat checkFormat = format != 0 ? | 
|  | 1462 | format : mCore->mDefaultBufferFormat; | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1463 | uint64_t checkUsage = usage | mCore->mConsumerUsageBits; | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1464 | if (checkWidth != allocWidth || checkHeight != allocHeight || | 
|  | 1465 | checkFormat != allocFormat || checkUsage != allocUsage) { | 
|  | 1466 | // Something changed while we released the lock. Retry. | 
|  | 1467 | BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying."); | 
|  | 1468 | mCore->mIsAllocating = false; | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1469 | mCore->mIsAllocatingCondition.notify_all(); | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1470 | continue; | 
|  | 1471 | } | 
|  | 1472 |  | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1473 | for (size_t i = 0; i < newBufferCount; ++i) { | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1474 | if (mCore->mFreeSlots.empty()) { | 
|  | 1475 | BQ_LOGV("allocateBuffers: a slot was occupied while " | 
|  | 1476 | "allocating. Dropping allocated buffer."); | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1477 | continue; | 
|  | 1478 | } | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1479 | auto slot = mCore->mFreeSlots.begin(); | 
|  | 1480 | mCore->clearBufferSlotLocked(*slot); // Clean up the slot first | 
|  | 1481 | mSlots[*slot].mGraphicBuffer = buffers[i]; | 
|  | 1482 | mSlots[*slot].mFence = Fence::NO_FENCE; | 
| Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 1483 |  | 
|  | 1484 | // freeBufferLocked puts this slot on the free slots list. Since | 
|  | 1485 | // we then attached a buffer, move the slot to free buffer list. | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1486 | mCore->mFreeBuffers.push_front(*slot); | 
| Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 1487 |  | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1488 | BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", | 
|  | 1489 | *slot); | 
| Christopher Ferris | 87e94cd | 2016-04-26 11:29:08 -0700 | [diff] [blame] | 1490 |  | 
|  | 1491 | // Make sure the erase is done after all uses of the slot | 
|  | 1492 | // iterator since it will be invalid after this point. | 
|  | 1493 | mCore->mFreeSlots.erase(slot); | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1494 | } | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1495 |  | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1496 | mCore->mIsAllocating = false; | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1497 | mCore->mIsAllocatingCondition.notify_all(); | 
| Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 1498 | VALIDATE_CONSISTENCY(); | 
| Jorim Jaggi | 35b4e38 | 2019-03-28 00:44:03 +0100 | [diff] [blame] | 1499 |  | 
|  | 1500 | // If dequeue is waiting for to allocate a buffer, release the lock until it's not | 
|  | 1501 | // waiting anymore so it can use the buffer we just allocated. | 
|  | 1502 | while (mDequeueWaitingForAllocation) { | 
|  | 1503 | mDequeueWaitingForAllocationCondition.wait(lock); | 
|  | 1504 | } | 
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1505 | } // Autolock scope | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1506 | } | 
|  | 1507 | } | 
|  | 1508 |  | 
| Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 1509 | status_t BufferQueueProducer::allowAllocation(bool allow) { | 
|  | 1510 | ATRACE_CALL(); | 
|  | 1511 | BQ_LOGV("allowAllocation: %s", allow ? "true" : "false"); | 
|  | 1512 |  | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1513 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 1514 | mCore->mAllowAllocation = allow; | 
|  | 1515 | return NO_ERROR; | 
|  | 1516 | } | 
|  | 1517 |  | 
| Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1518 | status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) { | 
|  | 1519 | ATRACE_CALL(); | 
|  | 1520 | BQ_LOGV("setGenerationNumber: %u", generationNumber); | 
|  | 1521 |  | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1522 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1523 | mCore->mGenerationNumber = generationNumber; | 
|  | 1524 | return NO_ERROR; | 
|  | 1525 | } | 
|  | 1526 |  | 
| Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 1527 | String8 BufferQueueProducer::getConsumerName() const { | 
|  | 1528 | ATRACE_CALL(); | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1529 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 1530 | BQ_LOGV("getConsumerName: %s", mConsumerName.string()); | 
|  | 1531 | return mConsumerName; | 
|  | 1532 | } | 
|  | 1533 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1534 | status_t BufferQueueProducer::setSharedBufferMode(bool sharedBufferMode) { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1535 | ATRACE_CALL(); | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1536 | BQ_LOGV("setSharedBufferMode: %d", sharedBufferMode); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1537 |  | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1538 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1539 | if (!sharedBufferMode) { | 
|  | 1540 | mCore->mSharedBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT; | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1541 | } | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1542 | mCore->mSharedBufferMode = sharedBufferMode; | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1543 | return NO_ERROR; | 
|  | 1544 | } | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1545 |  | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1546 | status_t BufferQueueProducer::setAutoRefresh(bool autoRefresh) { | 
|  | 1547 | ATRACE_CALL(); | 
|  | 1548 | BQ_LOGV("setAutoRefresh: %d", autoRefresh); | 
|  | 1549 |  | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1550 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1551 |  | 
|  | 1552 | mCore->mAutoRefresh = autoRefresh; | 
|  | 1553 | return NO_ERROR; | 
|  | 1554 | } | 
|  | 1555 |  | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1556 | status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) { | 
|  | 1557 | ATRACE_CALL(); | 
|  | 1558 | BQ_LOGV("setDequeueTimeout: %" PRId64, timeout); | 
|  | 1559 |  | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1560 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Sungtak Lee | 42a94c6 | 2019-05-24 14:27:14 -0700 | [diff] [blame] | 1561 | bool dequeueBufferCannotBlock = | 
|  | 1562 | timeout >= 0 ? false : mCore->mDequeueBufferCannotBlock; | 
|  | 1563 | int delta = mCore->getMaxBufferCountLocked(mCore->mAsyncMode, dequeueBufferCannotBlock, | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1564 | mCore->mMaxBufferCount) - mCore->getMaxBufferCountLocked(); | 
|  | 1565 | if (!mCore->adjustAvailableSlotsLocked(delta)) { | 
|  | 1566 | BQ_LOGE("setDequeueTimeout: BufferQueue failed to adjust the number of " | 
|  | 1567 | "available slots. Delta = %d", delta); | 
|  | 1568 | return BAD_VALUE; | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1569 | } | 
|  | 1570 |  | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1571 | mDequeueTimeout = timeout; | 
| Sungtak Lee | 42a94c6 | 2019-05-24 14:27:14 -0700 | [diff] [blame] | 1572 | mCore->mDequeueBufferCannotBlock = dequeueBufferCannotBlock; | 
|  | 1573 | if (timeout > 0) { | 
|  | 1574 | mCore->mQueueBufferCanDrop = false; | 
| Sungtak Lee | 3249fb6 | 2019-03-02 16:40:47 -0800 | [diff] [blame] | 1575 | } | 
| Pablo Ceballos | 9e31433 | 2016-01-12 13:49:19 -0800 | [diff] [blame] | 1576 |  | 
| Pablo Ceballos | 981066c | 2016-02-18 12:54:37 -0800 | [diff] [blame] | 1577 | VALIDATE_CONSISTENCY(); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1578 | return NO_ERROR; | 
|  | 1579 | } | 
|  | 1580 |  | 
| Sungtak Lee | 3249fb6 | 2019-03-02 16:40:47 -0800 | [diff] [blame] | 1581 | status_t BufferQueueProducer::setLegacyBufferDrop(bool drop) { | 
|  | 1582 | ATRACE_CALL(); | 
|  | 1583 | BQ_LOGV("setLegacyBufferDrop: drop = %d", drop); | 
|  | 1584 |  | 
|  | 1585 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
|  | 1586 | mCore->mLegacyBufferDrop = drop; | 
|  | 1587 | return NO_ERROR; | 
|  | 1588 | } | 
|  | 1589 |  | 
| Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 1590 | status_t BufferQueueProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, | 
| John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 1591 | sp<Fence>* outFence, float outTransformMatrix[16]) { | 
| Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 1592 | ATRACE_CALL(); | 
|  | 1593 | BQ_LOGV("getLastQueuedBuffer"); | 
|  | 1594 |  | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1595 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 1596 | if (mCore->mLastQueuedSlot == BufferItem::INVALID_BUFFER_SLOT) { | 
|  | 1597 | *outBuffer = nullptr; | 
|  | 1598 | *outFence = Fence::NO_FENCE; | 
|  | 1599 | return NO_ERROR; | 
|  | 1600 | } | 
|  | 1601 |  | 
|  | 1602 | *outBuffer = mSlots[mCore->mLastQueuedSlot].mGraphicBuffer; | 
|  | 1603 | *outFence = mLastQueueBufferFence; | 
|  | 1604 |  | 
| John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 1605 | // Currently only SurfaceFlinger internally ever changes | 
|  | 1606 | // GLConsumer's filtering mode, so we just use 'true' here as | 
|  | 1607 | // this is slightly specialized for the current client of this API, | 
|  | 1608 | // which does want filtering. | 
|  | 1609 | GLConsumer::computeTransformMatrix(outTransformMatrix, | 
|  | 1610 | mSlots[mCore->mLastQueuedSlot].mGraphicBuffer, mLastQueuedCrop, | 
|  | 1611 | mLastQueuedTransform, true /* filter */); | 
|  | 1612 |  | 
| Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 1613 | return NO_ERROR; | 
|  | 1614 | } | 
|  | 1615 |  | 
| Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1616 | void BufferQueueProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) { | 
|  | 1617 | addAndGetFrameTimestamps(nullptr, outDelta); | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1618 | } | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1619 |  | 
| Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1620 | void BufferQueueProducer::addAndGetFrameTimestamps( | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1621 | const NewFrameEventsEntry* newTimestamps, | 
| Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1622 | FrameEventHistoryDelta* outDelta) { | 
|  | 1623 | if (newTimestamps == nullptr && outDelta == nullptr) { | 
|  | 1624 | return; | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1625 | } | 
|  | 1626 |  | 
|  | 1627 | ATRACE_CALL(); | 
|  | 1628 | BQ_LOGV("addAndGetFrameTimestamps"); | 
|  | 1629 | sp<IConsumerListener> listener; | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1630 | { | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1631 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1632 | listener = mCore->mConsumerListener; | 
|  | 1633 | } | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1634 | if (listener != nullptr) { | 
| Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1635 | listener->addAndGetFrameTimestamps(newTimestamps, outDelta); | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1636 | } | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1637 | } | 
|  | 1638 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1639 | void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) { | 
|  | 1640 | // If we're here, it means that a producer we were connected to died. | 
|  | 1641 | // We're guaranteed that we are still connected to it because we remove | 
|  | 1642 | // this callback upon disconnect. It's therefore safe to read mConnectedApi | 
|  | 1643 | // without synchronization here. | 
|  | 1644 | int api = mCore->mConnectedApi; | 
|  | 1645 | disconnect(api); | 
|  | 1646 | } | 
|  | 1647 |  | 
| Pablo Ceballos | 8e3e92b | 2016-06-27 17:56:53 -0700 | [diff] [blame] | 1648 | status_t BufferQueueProducer::getUniqueId(uint64_t* outId) const { | 
|  | 1649 | BQ_LOGV("getUniqueId"); | 
|  | 1650 |  | 
|  | 1651 | *outId = mCore->mUniqueId; | 
|  | 1652 | return NO_ERROR; | 
|  | 1653 | } | 
|  | 1654 |  | 
| Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1655 | status_t BufferQueueProducer::getConsumerUsage(uint64_t* outUsage) const { | 
|  | 1656 | BQ_LOGV("getConsumerUsage"); | 
|  | 1657 |  | 
| Jorim Jaggi | 6ae5503 | 2019-04-02 02:27:44 +0200 | [diff] [blame] | 1658 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
| Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1659 | *outUsage = mCore->mConsumerUsageBits; | 
|  | 1660 | return NO_ERROR; | 
|  | 1661 | } | 
|  | 1662 |  | 
| Yiwei Zhang | 538cedc | 2019-06-24 19:35:03 -0700 | [diff] [blame] | 1663 | status_t BufferQueueProducer::setAutoPrerotation(bool autoPrerotation) { | 
|  | 1664 | ATRACE_CALL(); | 
|  | 1665 | BQ_LOGV("setAutoPrerotation: %d", autoPrerotation); | 
|  | 1666 |  | 
|  | 1667 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
|  | 1668 |  | 
|  | 1669 | mCore->mAutoPrerotation = autoPrerotation; | 
|  | 1670 | return NO_ERROR; | 
|  | 1671 | } | 
|  | 1672 |  | 
| Steven Thomas | 3172e20 | 2020-01-06 19:25:30 -0800 | [diff] [blame] | 1673 | status_t BufferQueueProducer::setFrameRate(float frameRate) { | 
|  | 1674 | ATRACE_CALL(); | 
|  | 1675 | BQ_LOGV("setFrameRate: %.0f", frameRate); | 
|  | 1676 |  | 
|  | 1677 | std::lock_guard<std::mutex> lock(mCore->mMutex); | 
|  | 1678 |  | 
|  | 1679 | mCore->mFrameRate = frameRate; | 
|  | 1680 | return NO_ERROR; | 
|  | 1681 | } | 
|  | 1682 |  | 
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1683 | } // namespace android |