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 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 23 | #define EGL_EGLEXT_PROTOTYPES |
| 24 | |
| 25 | #include <gui/BufferItem.h> |
| 26 | #include <gui/BufferQueueCore.h> |
| 27 | #include <gui/BufferQueueProducer.h> |
| 28 | #include <gui/IConsumerListener.h> |
| 29 | #include <gui/IGraphicBufferAlloc.h> |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 30 | #include <gui/IProducerListener.h> |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 31 | |
| 32 | #include <utils/Log.h> |
| 33 | #include <utils/Trace.h> |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core) : |
| 38 | mCore(core), |
| 39 | mSlots(core->mSlots), |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 40 | mConsumerName(), |
Eric Penner | 99a0afb | 2014-09-30 11:28:30 -0700 | [diff] [blame] | 41 | mStickyTransform(0), |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 42 | mLastQueueBufferFence(Fence::NO_FENCE), |
| 43 | mCallbackMutex(), |
| 44 | mNextCallbackTicket(0), |
| 45 | mCurrentCallbackTicket(0), |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 46 | mCallbackCondition(), |
| 47 | mDequeueTimeout(-1) {} |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 48 | |
| 49 | BufferQueueProducer::~BufferQueueProducer() {} |
| 50 | |
| 51 | status_t BufferQueueProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) { |
| 52 | ATRACE_CALL(); |
| 53 | BQ_LOGV("requestBuffer: slot %d", slot); |
| 54 | Mutex::Autolock lock(mCore->mMutex); |
| 55 | |
| 56 | if (mCore->mIsAbandoned) { |
| 57 | BQ_LOGE("requestBuffer: BufferQueue has been abandoned"); |
| 58 | return NO_INIT; |
| 59 | } |
| 60 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 61 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 62 | BQ_LOGE("requestBuffer: BufferQueue has no connected producer"); |
| 63 | return NO_INIT; |
| 64 | } |
| 65 | |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 66 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 67 | BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)", |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 68 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 69 | return BAD_VALUE; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 70 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 71 | BQ_LOGE("requestBuffer: slot %d is not owned by the producer " |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 72 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 73 | return BAD_VALUE; |
| 74 | } |
| 75 | |
| 76 | mSlots[slot].mRequestBufferCalled = true; |
| 77 | *buf = mSlots[slot].mGraphicBuffer; |
| 78 | return NO_ERROR; |
| 79 | } |
| 80 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 81 | status_t BufferQueueProducer::setMaxDequeuedBufferCount( |
| 82 | int maxDequeuedBuffers) { |
| 83 | ATRACE_CALL(); |
| 84 | BQ_LOGV("setMaxDequeuedBufferCount: maxDequeuedBuffers = %d", |
| 85 | maxDequeuedBuffers); |
| 86 | |
| 87 | sp<IConsumerListener> listener; |
| 88 | { // Autolock scope |
| 89 | Mutex::Autolock lock(mCore->mMutex); |
| 90 | mCore->waitWhileAllocatingLocked(); |
| 91 | |
| 92 | if (mCore->mIsAbandoned) { |
| 93 | BQ_LOGE("setMaxDequeuedBufferCount: BufferQueue has been " |
| 94 | "abandoned"); |
| 95 | return NO_INIT; |
| 96 | } |
| 97 | |
| 98 | // There must be no dequeued buffers when changing the buffer count. |
| 99 | for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 100 | if (mSlots[s].mBufferState.isDequeued()) { |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 101 | BQ_LOGE("setMaxDequeuedBufferCount: buffer owned by producer"); |
| 102 | return BAD_VALUE; |
| 103 | } |
| 104 | } |
| 105 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 106 | int bufferCount = mCore->getMinUndequeuedBufferCountLocked(); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 107 | bufferCount += maxDequeuedBuffers; |
| 108 | |
| 109 | if (bufferCount > BufferQueueDefs::NUM_BUFFER_SLOTS) { |
| 110 | BQ_LOGE("setMaxDequeuedBufferCount: bufferCount %d too large " |
| 111 | "(max %d)", bufferCount, BufferQueueDefs::NUM_BUFFER_SLOTS); |
| 112 | return BAD_VALUE; |
| 113 | } |
| 114 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 115 | const int minBufferSlots = mCore->getMinMaxBufferCountLocked(); |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 116 | if (bufferCount < minBufferSlots) { |
| 117 | BQ_LOGE("setMaxDequeuedBufferCount: requested buffer count %d is " |
| 118 | "less than minimum %d", bufferCount, minBufferSlots); |
| 119 | return BAD_VALUE; |
| 120 | } |
| 121 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 122 | if (bufferCount > mCore->mMaxBufferCount) { |
| 123 | BQ_LOGE("setMaxDequeuedBufferCount: %d dequeued buffers would " |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 124 | "exceed the maxBufferCount (%d) (maxAcquired %d async %d " |
| 125 | "mDequeuedBufferCannotBlock %d)", maxDequeuedBuffers, |
| 126 | mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount, |
| 127 | mCore->mAsyncMode, mCore->mDequeueBufferCannotBlock); |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 128 | return BAD_VALUE; |
| 129 | } |
| 130 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 131 | // Here we are guaranteed that the producer doesn't have any dequeued |
| 132 | // buffers and will release all of its buffer references. We don't |
| 133 | // clear the queue, however, so that currently queued buffers still |
| 134 | // get displayed. |
| 135 | mCore->freeAllBuffersLocked(); |
| 136 | mCore->mMaxDequeuedBufferCount = maxDequeuedBuffers; |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 137 | mCore->mDequeueCondition.broadcast(); |
| 138 | listener = mCore->mConsumerListener; |
| 139 | } // Autolock scope |
| 140 | |
| 141 | // Call back without lock held |
| 142 | if (listener != NULL) { |
| 143 | listener->onBuffersReleased(); |
| 144 | } |
| 145 | |
| 146 | return NO_ERROR; |
| 147 | } |
| 148 | |
| 149 | status_t BufferQueueProducer::setAsyncMode(bool async) { |
| 150 | ATRACE_CALL(); |
| 151 | BQ_LOGV("setAsyncMode: async = %d", async); |
| 152 | |
| 153 | sp<IConsumerListener> listener; |
| 154 | { // Autolock scope |
| 155 | Mutex::Autolock lock(mCore->mMutex); |
| 156 | mCore->waitWhileAllocatingLocked(); |
| 157 | |
| 158 | if (mCore->mIsAbandoned) { |
| 159 | BQ_LOGE("setAsyncMode: BufferQueue has been abandoned"); |
| 160 | return NO_INIT; |
| 161 | } |
| 162 | |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 163 | if ((mCore->mMaxAcquiredBufferCount + mCore->mMaxDequeuedBufferCount + |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 164 | (async || mCore->mDequeueBufferCannotBlock ? 1 : 0)) > |
| 165 | mCore->mMaxBufferCount) { |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 166 | BQ_LOGE("setAsyncMode(%d): this call would cause the " |
| 167 | "maxBufferCount (%d) to be exceeded (maxAcquired %d " |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 168 | "maxDequeued %d mDequeueBufferCannotBlock %d)", async, |
| 169 | mCore->mMaxBufferCount, mCore->mMaxAcquiredBufferCount, |
| 170 | mCore->mMaxDequeuedBufferCount, |
| 171 | mCore->mDequeueBufferCannotBlock); |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 172 | return BAD_VALUE; |
| 173 | } |
| 174 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 175 | mCore->mAsyncMode = async; |
| 176 | mCore->mDequeueCondition.broadcast(); |
| 177 | listener = mCore->mConsumerListener; |
| 178 | } // Autolock scope |
| 179 | |
| 180 | // Call back without lock held |
| 181 | if (listener != NULL) { |
| 182 | listener->onBuffersReleased(); |
| 183 | } |
| 184 | return NO_ERROR; |
| 185 | } |
| 186 | |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 187 | int BufferQueueProducer::getFreeBufferLocked() const { |
| 188 | if (mCore->mFreeBuffers.empty()) { |
| 189 | return BufferQueueCore::INVALID_BUFFER_SLOT; |
| 190 | } |
| 191 | auto slot = mCore->mFreeBuffers.front(); |
| 192 | mCore->mFreeBuffers.pop_front(); |
| 193 | return slot; |
| 194 | } |
| 195 | |
| 196 | int BufferQueueProducer::getFreeSlotLocked(int maxBufferCount) const { |
| 197 | if (mCore->mFreeSlots.empty()) { |
| 198 | return BufferQueueCore::INVALID_BUFFER_SLOT; |
| 199 | } |
| 200 | auto slot = *(mCore->mFreeSlots.begin()); |
| 201 | if (slot < maxBufferCount) { |
| 202 | mCore->mFreeSlots.erase(slot); |
| 203 | return slot; |
| 204 | } |
| 205 | return BufferQueueCore::INVALID_BUFFER_SLOT; |
| 206 | } |
| 207 | |
| 208 | status_t BufferQueueProducer::waitForFreeSlotThenRelock(FreeSlotCaller caller, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 209 | int* found, status_t* returnFlags) const { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 210 | auto callerString = (caller == FreeSlotCaller::Dequeue) ? |
| 211 | "dequeueBuffer" : "attachBuffer"; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 212 | bool tryAgain = true; |
| 213 | while (tryAgain) { |
| 214 | if (mCore->mIsAbandoned) { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 215 | BQ_LOGE("%s: BufferQueue has been abandoned", callerString); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 216 | return NO_INIT; |
| 217 | } |
| 218 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 219 | const int maxBufferCount = mCore->getMaxBufferCountLocked(); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 220 | |
| 221 | // Free up any buffers that are in slots beyond the max buffer count |
| 222 | for (int s = maxBufferCount; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 223 | assert(mSlots[s].mBufferState.isFree()); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 224 | if (mSlots[s].mGraphicBuffer != NULL) { |
| 225 | mCore->freeBufferLocked(s); |
| 226 | *returnFlags |= RELEASE_ALL_BUFFERS; |
| 227 | } |
| 228 | } |
| 229 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 230 | int dequeuedCount = 0; |
| 231 | int acquiredCount = 0; |
| 232 | for (int s = 0; s < maxBufferCount; ++s) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 233 | if (mSlots[s].mBufferState.isDequeued()) { |
| 234 | ++dequeuedCount; |
| 235 | } |
| 236 | if (mSlots[s].mBufferState.isAcquired()) { |
| 237 | ++acquiredCount; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 241 | // Producers are not allowed to dequeue more than |
| 242 | // mMaxDequeuedBufferCount buffers. |
| 243 | // This check is only done if a buffer has already been queued |
| 244 | if (mCore->mBufferHasBeenQueued && |
| 245 | dequeuedCount >= mCore->mMaxDequeuedBufferCount) { |
| 246 | BQ_LOGE("%s: attempting to exceed the max dequeued buffer count " |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 247 | "(%d)", callerString, mCore->mMaxDequeuedBufferCount); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 248 | return INVALID_OPERATION; |
| 249 | } |
| 250 | |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 251 | *found = BufferQueueCore::INVALID_BUFFER_SLOT; |
| 252 | |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 253 | // If we disconnect and reconnect quickly, we can be in a state where |
| 254 | // our slots are empty but we have many buffers in the queue. This can |
| 255 | // cause us to run out of memory if we outrun the consumer. Wait here if |
| 256 | // it looks like we have too many buffers queued up. |
Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 257 | bool tooManyBuffers = mCore->mQueue.size() |
| 258 | > static_cast<size_t>(maxBufferCount); |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 259 | if (tooManyBuffers) { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 260 | BQ_LOGV("%s: queue size is %zu, waiting", callerString, |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 261 | mCore->mQueue.size()); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 262 | } else { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 263 | // If in single buffer mode and a shared buffer exists, always |
| 264 | // return it. |
| 265 | if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot != |
| 266 | BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 267 | *found = mCore->mSingleBufferSlot; |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 268 | } else { |
| 269 | if (caller == FreeSlotCaller::Dequeue) { |
| 270 | // If we're calling this from dequeue, prefer free buffers |
| 271 | auto slot = getFreeBufferLocked(); |
| 272 | if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 273 | *found = slot; |
| 274 | } else if (mCore->mAllowAllocation) { |
| 275 | *found = getFreeSlotLocked(maxBufferCount); |
| 276 | } |
| 277 | } else { |
| 278 | // If we're calling this from attach, prefer free slots |
| 279 | auto slot = getFreeSlotLocked(maxBufferCount); |
| 280 | if (slot != BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 281 | *found = slot; |
| 282 | } else { |
| 283 | *found = getFreeBufferLocked(); |
| 284 | } |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // If no buffer is found, or if the queue has too many buffers |
| 290 | // outstanding, wait for a buffer to be acquired or released, or for the |
| 291 | // max buffer count to change. |
| 292 | tryAgain = (*found == BufferQueueCore::INVALID_BUFFER_SLOT) || |
| 293 | tooManyBuffers; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 294 | if (tryAgain) { |
| 295 | // Return an error if we're in non-blocking mode (producer and |
| 296 | // consumer are controlled by the application). |
| 297 | // However, the consumer is allowed to briefly acquire an extra |
| 298 | // buffer (which could cause us to have to wait here), which is |
| 299 | // okay, since it is only used to implement an atomic acquire + |
| 300 | // release (e.g., in GLConsumer::updateTexImage()) |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 301 | if ((mCore->mDequeueBufferCannotBlock || mCore->mAsyncMode) && |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 302 | (acquiredCount <= mCore->mMaxAcquiredBufferCount)) { |
| 303 | return WOULD_BLOCK; |
| 304 | } |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 305 | if (mDequeueTimeout >= 0) { |
| 306 | status_t result = mCore->mDequeueCondition.waitRelative( |
| 307 | mCore->mMutex, mDequeueTimeout); |
| 308 | if (result == TIMED_OUT) { |
| 309 | return result; |
| 310 | } |
| 311 | } else { |
| 312 | mCore->mDequeueCondition.wait(mCore->mMutex); |
| 313 | } |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 314 | } |
| 315 | } // while (tryAgain) |
| 316 | |
| 317 | return NO_ERROR; |
| 318 | } |
| 319 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 320 | status_t BufferQueueProducer::dequeueBuffer(int *outSlot, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 321 | sp<android::Fence> *outFence, uint32_t width, uint32_t height, |
| 322 | PixelFormat format, uint32_t usage) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 323 | ATRACE_CALL(); |
| 324 | { // Autolock scope |
| 325 | Mutex::Autolock lock(mCore->mMutex); |
| 326 | mConsumerName = mCore->mConsumerName; |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 327 | |
| 328 | if (mCore->mIsAbandoned) { |
| 329 | BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned"); |
| 330 | return NO_INIT; |
| 331 | } |
| 332 | |
| 333 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 334 | BQ_LOGE("dequeueBuffer: BufferQueue has no connected producer"); |
| 335 | return NO_INIT; |
| 336 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 337 | } // Autolock scope |
| 338 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 339 | BQ_LOGV("dequeueBuffer: w=%u h=%u format=%#x, usage=%#x", width, height, |
| 340 | format, usage); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 341 | |
| 342 | if ((width && !height) || (!width && height)) { |
| 343 | BQ_LOGE("dequeueBuffer: invalid size: w=%u h=%u", width, height); |
| 344 | return BAD_VALUE; |
| 345 | } |
| 346 | |
| 347 | status_t returnFlags = NO_ERROR; |
| 348 | EGLDisplay eglDisplay = EGL_NO_DISPLAY; |
| 349 | EGLSyncKHR eglFence = EGL_NO_SYNC_KHR; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 350 | bool attachedByConsumer = false; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 351 | |
| 352 | { // Autolock scope |
| 353 | Mutex::Autolock lock(mCore->mMutex); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 354 | mCore->waitWhileAllocatingLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 355 | |
| 356 | if (format == 0) { |
| 357 | format = mCore->mDefaultBufferFormat; |
| 358 | } |
| 359 | |
| 360 | // Enable the usage bits the consumer requested |
| 361 | usage |= mCore->mConsumerUsageBits; |
| 362 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 363 | const bool useDefaultSize = !width && !height; |
| 364 | if (useDefaultSize) { |
| 365 | width = mCore->mDefaultWidth; |
| 366 | height = mCore->mDefaultHeight; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 367 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 368 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 369 | int found = BufferItem::INVALID_BUFFER_SLOT; |
| 370 | while (found == BufferItem::INVALID_BUFFER_SLOT) { |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 371 | status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Dequeue, |
| 372 | &found, &returnFlags); |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 373 | if (status != NO_ERROR) { |
| 374 | return status; |
| 375 | } |
| 376 | |
| 377 | // This should not happen |
| 378 | if (found == BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 379 | BQ_LOGE("dequeueBuffer: no available buffer slots"); |
| 380 | return -EBUSY; |
| 381 | } |
| 382 | |
| 383 | const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer); |
| 384 | |
| 385 | // If we are not allowed to allocate new buffers, |
| 386 | // waitForFreeSlotThenRelock must have returned a slot containing a |
| 387 | // buffer. If this buffer would require reallocation to meet the |
| 388 | // requested attributes, we free it and attempt to get another one. |
| 389 | if (!mCore->mAllowAllocation) { |
| 390 | if (buffer->needsReallocation(width, height, format, usage)) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 391 | if (mCore->mSingleBufferMode && |
| 392 | mCore->mSingleBufferSlot == found) { |
| 393 | BQ_LOGE("dequeueBuffer: cannot re-allocate a shared" |
| 394 | "buffer"); |
| 395 | return BAD_VALUE; |
| 396 | } |
| 397 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 398 | mCore->freeBufferLocked(found); |
| 399 | found = BufferItem::INVALID_BUFFER_SLOT; |
| 400 | continue; |
| 401 | } |
| 402 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | *outSlot = found; |
| 406 | ATRACE_BUFFER_INDEX(found); |
| 407 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 408 | attachedByConsumer = mSlots[found].mAttachedByConsumer; |
| 409 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 410 | mSlots[found].mBufferState.dequeue(); |
| 411 | |
| 412 | // If single buffer mode has just been enabled, cache the slot of the |
| 413 | // first buffer that is dequeued and mark it as the shared buffer. |
| 414 | if (mCore->mSingleBufferMode && mCore->mSingleBufferSlot == |
| 415 | BufferQueueCore::INVALID_BUFFER_SLOT) { |
| 416 | mCore->mSingleBufferSlot = found; |
| 417 | mSlots[found].mBufferState.mShared = true; |
| 418 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 419 | |
| 420 | const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer); |
| 421 | if ((buffer == NULL) || |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 422 | buffer->needsReallocation(width, height, format, usage)) |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 423 | { |
| 424 | mSlots[found].mAcquireCalled = false; |
| 425 | mSlots[found].mGraphicBuffer = NULL; |
| 426 | mSlots[found].mRequestBufferCalled = false; |
| 427 | mSlots[found].mEglDisplay = EGL_NO_DISPLAY; |
| 428 | mSlots[found].mEglFence = EGL_NO_SYNC_KHR; |
| 429 | mSlots[found].mFence = Fence::NO_FENCE; |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 430 | mCore->mBufferAge = 0; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 431 | mCore->mIsAllocating = true; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 432 | |
| 433 | returnFlags |= BUFFER_NEEDS_REALLOCATION; |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 434 | } else { |
| 435 | // We add 1 because that will be the frame number when this buffer |
| 436 | // is queued |
| 437 | mCore->mBufferAge = |
| 438 | mCore->mFrameCounter + 1 - mSlots[found].mFrameNumber; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Dan Stoza | 800b41a | 2015-04-28 14:20:04 -0700 | [diff] [blame] | 441 | BQ_LOGV("dequeueBuffer: setting buffer age to %" PRIu64, |
| 442 | mCore->mBufferAge); |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 443 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 444 | if (CC_UNLIKELY(mSlots[found].mFence == NULL)) { |
| 445 | BQ_LOGE("dequeueBuffer: about to return a NULL fence - " |
| 446 | "slot=%d w=%d h=%d format=%u", |
| 447 | found, buffer->width, buffer->height, buffer->format); |
| 448 | } |
| 449 | |
| 450 | eglDisplay = mSlots[found].mEglDisplay; |
| 451 | eglFence = mSlots[found].mEglFence; |
| 452 | *outFence = mSlots[found].mFence; |
| 453 | mSlots[found].mEglFence = EGL_NO_SYNC_KHR; |
| 454 | mSlots[found].mFence = Fence::NO_FENCE; |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 455 | |
| 456 | mCore->validateConsistencyLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 457 | } // Autolock scope |
| 458 | |
| 459 | if (returnFlags & BUFFER_NEEDS_REALLOCATION) { |
| 460 | status_t error; |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 461 | BQ_LOGV("dequeueBuffer: allocating a new buffer for slot %d", *outSlot); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 462 | sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer( |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 463 | width, height, format, usage, &error)); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 464 | { // Autolock scope |
| 465 | Mutex::Autolock lock(mCore->mMutex); |
| 466 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 467 | if (graphicBuffer != NULL && !mCore->mIsAbandoned) { |
| 468 | graphicBuffer->setGenerationNumber(mCore->mGenerationNumber); |
| 469 | mSlots[*outSlot].mGraphicBuffer = graphicBuffer; |
| 470 | } |
| 471 | |
| 472 | mCore->mIsAllocating = false; |
| 473 | mCore->mIsAllocatingCondition.broadcast(); |
| 474 | |
| 475 | if (graphicBuffer == NULL) { |
| 476 | BQ_LOGE("dequeueBuffer: createGraphicBuffer failed"); |
| 477 | return error; |
| 478 | } |
| 479 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 480 | if (mCore->mIsAbandoned) { |
| 481 | BQ_LOGE("dequeueBuffer: BufferQueue has been abandoned"); |
| 482 | return NO_INIT; |
| 483 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 484 | } // Autolock scope |
| 485 | } |
| 486 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 487 | if (attachedByConsumer) { |
| 488 | returnFlags |= BUFFER_NEEDS_REALLOCATION; |
| 489 | } |
| 490 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 491 | if (eglFence != EGL_NO_SYNC_KHR) { |
| 492 | EGLint result = eglClientWaitSyncKHR(eglDisplay, eglFence, 0, |
| 493 | 1000000000); |
| 494 | // If something goes wrong, log the error, but return the buffer without |
| 495 | // synchronizing access to it. It's too late at this point to abort the |
| 496 | // dequeue operation. |
| 497 | if (result == EGL_FALSE) { |
| 498 | BQ_LOGE("dequeueBuffer: error %#x waiting for fence", |
| 499 | eglGetError()); |
| 500 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 501 | BQ_LOGE("dequeueBuffer: timeout waiting for fence"); |
| 502 | } |
| 503 | eglDestroySyncKHR(eglDisplay, eglFence); |
| 504 | } |
| 505 | |
Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 506 | BQ_LOGV("dequeueBuffer: returning slot=%d/%" PRIu64 " buf=%p flags=%#x", |
| 507 | *outSlot, |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 508 | mSlots[*outSlot].mFrameNumber, |
| 509 | mSlots[*outSlot].mGraphicBuffer->handle, returnFlags); |
| 510 | |
| 511 | return returnFlags; |
| 512 | } |
| 513 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 514 | status_t BufferQueueProducer::detachBuffer(int slot) { |
| 515 | ATRACE_CALL(); |
| 516 | ATRACE_BUFFER_INDEX(slot); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 517 | BQ_LOGV("detachBuffer: slot %d", slot); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 518 | Mutex::Autolock lock(mCore->mMutex); |
| 519 | |
| 520 | if (mCore->mIsAbandoned) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 521 | BQ_LOGE("detachBuffer: BufferQueue has been abandoned"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 522 | return NO_INIT; |
| 523 | } |
| 524 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 525 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 526 | BQ_LOGE("detachBuffer: BufferQueue has no connected producer"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 527 | return NO_INIT; |
| 528 | } |
| 529 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 530 | if (mCore->mSingleBufferMode) { |
| 531 | BQ_LOGE("detachBuffer: cannot detach a buffer in single buffer" |
| 532 | "mode"); |
| 533 | return BAD_VALUE; |
| 534 | } |
| 535 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 536 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 537 | BQ_LOGE("detachBuffer: slot index %d out of range [0, %d)", |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 538 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); |
| 539 | return BAD_VALUE; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 540 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
| 541 | BQ_LOGE("detachBuffer: slot %d is not owned by the producer " |
| 542 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 543 | return BAD_VALUE; |
| 544 | } else if (!mSlots[slot].mRequestBufferCalled) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 545 | BQ_LOGE("detachBuffer: buffer in slot %d has not been requested", |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 546 | slot); |
| 547 | return BAD_VALUE; |
| 548 | } |
| 549 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 550 | mSlots[slot].mBufferState.detachProducer(); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 551 | mCore->freeBufferLocked(slot); |
| 552 | mCore->mDequeueCondition.broadcast(); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 553 | mCore->validateConsistencyLocked(); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 554 | |
| 555 | return NO_ERROR; |
| 556 | } |
| 557 | |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 558 | status_t BufferQueueProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer, |
| 559 | sp<Fence>* outFence) { |
| 560 | ATRACE_CALL(); |
| 561 | |
| 562 | if (outBuffer == NULL) { |
| 563 | BQ_LOGE("detachNextBuffer: outBuffer must not be NULL"); |
| 564 | return BAD_VALUE; |
| 565 | } else if (outFence == NULL) { |
| 566 | BQ_LOGE("detachNextBuffer: outFence must not be NULL"); |
| 567 | return BAD_VALUE; |
| 568 | } |
| 569 | |
| 570 | Mutex::Autolock lock(mCore->mMutex); |
| 571 | |
| 572 | if (mCore->mIsAbandoned) { |
| 573 | BQ_LOGE("detachNextBuffer: BufferQueue has been abandoned"); |
| 574 | return NO_INIT; |
| 575 | } |
| 576 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 577 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 578 | BQ_LOGE("detachNextBuffer: BufferQueue has no connected producer"); |
| 579 | return NO_INIT; |
| 580 | } |
| 581 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 582 | if (mCore->mSingleBufferMode) { |
| 583 | BQ_LOGE("detachNextBuffer: cannot detach a buffer in single buffer" |
| 584 | "mode"); |
| 585 | return BAD_VALUE; |
| 586 | } |
| 587 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 588 | mCore->waitWhileAllocatingLocked(); |
| 589 | |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 590 | if (mCore->mFreeBuffers.empty()) { |
Dan Stoza | 1fc9cc2 | 2015-04-22 18:57:39 +0000 | [diff] [blame] | 591 | return NO_MEMORY; |
| 592 | } |
Dan Stoza | 8dddc99 | 2015-04-16 15:39:18 -0700 | [diff] [blame] | 593 | |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 594 | int found = mCore->mFreeBuffers.front(); |
| 595 | mCore->mFreeBuffers.remove(found); |
| 596 | |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 597 | BQ_LOGV("detachNextBuffer detached slot %d", found); |
| 598 | |
| 599 | *outBuffer = mSlots[found].mGraphicBuffer; |
| 600 | *outFence = mSlots[found].mFence; |
| 601 | mCore->freeBufferLocked(found); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 602 | mCore->validateConsistencyLocked(); |
Dan Stoza | d9822a3 | 2014-03-28 15:25:31 -0700 | [diff] [blame] | 603 | |
| 604 | return NO_ERROR; |
| 605 | } |
| 606 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 607 | status_t BufferQueueProducer::attachBuffer(int* outSlot, |
| 608 | const sp<android::GraphicBuffer>& buffer) { |
| 609 | ATRACE_CALL(); |
| 610 | |
| 611 | if (outSlot == NULL) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 612 | BQ_LOGE("attachBuffer: outSlot must not be NULL"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 613 | return BAD_VALUE; |
| 614 | } else if (buffer == NULL) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 615 | BQ_LOGE("attachBuffer: cannot attach NULL buffer"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 616 | return BAD_VALUE; |
| 617 | } |
| 618 | |
| 619 | Mutex::Autolock lock(mCore->mMutex); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 620 | |
| 621 | if (mCore->mIsAbandoned) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 622 | BQ_LOGE("attachBuffer: BufferQueue has been abandoned"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 623 | return NO_INIT; |
| 624 | } |
| 625 | |
| 626 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 627 | BQ_LOGE("attachBuffer: BufferQueue has no connected producer"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 628 | return NO_INIT; |
| 629 | } |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 630 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 631 | if (mCore->mSingleBufferMode) { |
Craig Donner | 05249fc | 2016-01-15 19:33:55 -0800 | [diff] [blame] | 632 | BQ_LOGE("attachBuffer: cannot attach a buffer in single buffer mode"); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 633 | return BAD_VALUE; |
| 634 | } |
| 635 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 636 | if (buffer->getGenerationNumber() != mCore->mGenerationNumber) { |
| 637 | BQ_LOGE("attachBuffer: generation number mismatch [buffer %u] " |
| 638 | "[queue %u]", buffer->getGenerationNumber(), |
| 639 | mCore->mGenerationNumber); |
| 640 | return BAD_VALUE; |
| 641 | } |
| 642 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 643 | mCore->waitWhileAllocatingLocked(); |
| 644 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 645 | status_t returnFlags = NO_ERROR; |
| 646 | int found; |
Dan Stoza | 5ecfb68 | 2016-01-04 17:01:02 -0800 | [diff] [blame] | 647 | status_t status = waitForFreeSlotThenRelock(FreeSlotCaller::Attach, &found, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 648 | &returnFlags); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 649 | if (status != NO_ERROR) { |
| 650 | return status; |
| 651 | } |
| 652 | |
| 653 | // This should not happen |
| 654 | if (found == BufferQueueCore::INVALID_BUFFER_SLOT) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 655 | BQ_LOGE("attachBuffer: no available buffer slots"); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 656 | return -EBUSY; |
| 657 | } |
| 658 | |
| 659 | *outSlot = found; |
| 660 | ATRACE_BUFFER_INDEX(*outSlot); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 661 | BQ_LOGV("attachBuffer: returning slot %d flags=%#x", |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 662 | *outSlot, returnFlags); |
| 663 | |
| 664 | mSlots[*outSlot].mGraphicBuffer = buffer; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 665 | mSlots[*outSlot].mBufferState.attachProducer(); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 666 | mSlots[*outSlot].mEglFence = EGL_NO_SYNC_KHR; |
| 667 | mSlots[*outSlot].mFence = Fence::NO_FENCE; |
Dan Stoza | 2443c79 | 2014-03-24 15:03:46 -0700 | [diff] [blame] | 668 | mSlots[*outSlot].mRequestBufferCalled = true; |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 669 | |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 670 | mCore->validateConsistencyLocked(); |
| 671 | |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 672 | return returnFlags; |
| 673 | } |
| 674 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 675 | status_t BufferQueueProducer::queueBuffer(int slot, |
| 676 | const QueueBufferInput &input, QueueBufferOutput *output) { |
| 677 | ATRACE_CALL(); |
| 678 | ATRACE_BUFFER_INDEX(slot); |
| 679 | |
| 680 | int64_t timestamp; |
| 681 | bool isAutoTimestamp; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 682 | android_dataspace dataSpace; |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 683 | Rect crop(Rect::EMPTY_RECT); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 684 | int scalingMode; |
| 685 | uint32_t transform; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 686 | uint32_t stickyTransform; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 687 | sp<Fence> fence; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 688 | input.deflate(×tamp, &isAutoTimestamp, &dataSpace, &crop, &scalingMode, |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 689 | &transform, &fence, &stickyTransform); |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 690 | Region surfaceDamage = input.getSurfaceDamage(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 691 | |
| 692 | if (fence == NULL) { |
| 693 | BQ_LOGE("queueBuffer: fence is NULL"); |
Jesse Hall | de288fe | 2014-11-04 08:30:48 -0800 | [diff] [blame] | 694 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | switch (scalingMode) { |
| 698 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 699 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 700 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: |
| 701 | case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: |
| 702 | break; |
| 703 | default: |
| 704 | BQ_LOGE("queueBuffer: unknown scaling mode %d", scalingMode); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 705 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 706 | } |
| 707 | |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 708 | sp<IConsumerListener> frameAvailableListener; |
| 709 | sp<IConsumerListener> frameReplacedListener; |
| 710 | int callbackTicket = 0; |
| 711 | BufferItem item; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 712 | { // Autolock scope |
| 713 | Mutex::Autolock lock(mCore->mMutex); |
| 714 | |
| 715 | if (mCore->mIsAbandoned) { |
| 716 | BQ_LOGE("queueBuffer: BufferQueue has been abandoned"); |
| 717 | return NO_INIT; |
| 718 | } |
| 719 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 720 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 721 | BQ_LOGE("queueBuffer: BufferQueue has no connected producer"); |
| 722 | return NO_INIT; |
| 723 | } |
| 724 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 725 | const int maxBufferCount = mCore->getMaxBufferCountLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 726 | |
| 727 | if (slot < 0 || slot >= maxBufferCount) { |
| 728 | BQ_LOGE("queueBuffer: slot index %d out of range [0, %d)", |
| 729 | slot, maxBufferCount); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 730 | return BAD_VALUE; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 731 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 732 | BQ_LOGE("queueBuffer: slot %d is not owned by the producer " |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 733 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 734 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 735 | } else if (!mSlots[slot].mRequestBufferCalled) { |
| 736 | BQ_LOGE("queueBuffer: slot %d was queued without requesting " |
| 737 | "a buffer", slot); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 738 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 741 | BQ_LOGV("queueBuffer: slot=%d/%" PRIu64 " time=%" PRIu64 " dataSpace=%d" |
Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 742 | " crop=[%d,%d,%d,%d] transform=%#x scale=%s", |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 743 | slot, mCore->mFrameCounter + 1, timestamp, dataSpace, |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 744 | crop.left, crop.top, crop.right, crop.bottom, transform, |
| 745 | BufferItem::scalingModeName(static_cast<uint32_t>(scalingMode))); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 746 | |
| 747 | const sp<GraphicBuffer>& graphicBuffer(mSlots[slot].mGraphicBuffer); |
| 748 | Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight()); |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 749 | Rect croppedRect(Rect::EMPTY_RECT); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 750 | crop.intersect(bufferRect, &croppedRect); |
| 751 | if (croppedRect != crop) { |
| 752 | BQ_LOGE("queueBuffer: crop rect is not contained within the " |
| 753 | "buffer in slot %d", slot); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 754 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 755 | } |
| 756 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 757 | // Override UNKNOWN dataspace with consumer default |
| 758 | if (dataSpace == HAL_DATASPACE_UNKNOWN) { |
| 759 | dataSpace = mCore->mDefaultBufferDataSpace; |
| 760 | } |
| 761 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 762 | mSlots[slot].mFence = fence; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 763 | mSlots[slot].mBufferState.queue(); |
| 764 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 765 | ++mCore->mFrameCounter; |
| 766 | mSlots[slot].mFrameNumber = mCore->mFrameCounter; |
| 767 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 768 | item.mAcquireCalled = mSlots[slot].mAcquireCalled; |
| 769 | item.mGraphicBuffer = mSlots[slot].mGraphicBuffer; |
| 770 | item.mCrop = crop; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 771 | item.mTransform = transform & |
| 772 | ~static_cast<uint32_t>(NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 773 | item.mTransformToDisplayInverse = |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 774 | (transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) != 0; |
| 775 | item.mScalingMode = static_cast<uint32_t>(scalingMode); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 776 | item.mTimestamp = timestamp; |
| 777 | item.mIsAutoTimestamp = isAutoTimestamp; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 778 | item.mDataSpace = dataSpace; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 779 | item.mFrameNumber = mCore->mFrameCounter; |
| 780 | item.mSlot = slot; |
| 781 | item.mFence = fence; |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 782 | item.mIsDroppable = mCore->mAsyncMode || |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 783 | mCore->mDequeueBufferCannotBlock || |
| 784 | (mCore->mSingleBufferMode && mCore->mSingleBufferSlot == slot); |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 785 | item.mSurfaceDamage = surfaceDamage; |
Pablo Ceballos | 0631218 | 2015-10-07 16:32:12 -0700 | [diff] [blame] | 786 | item.mSingleBufferMode = mCore->mSingleBufferMode; |
| 787 | item.mQueuedBuffer = true; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 788 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 789 | mStickyTransform = stickyTransform; |
| 790 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 791 | // Cache the shared buffer data so that the BufferItem can be recreated. |
| 792 | if (mCore->mSingleBufferMode) { |
| 793 | mCore->mSingleBufferCache.crop = crop; |
| 794 | mCore->mSingleBufferCache.transform = transform; |
| 795 | mCore->mSingleBufferCache.scalingMode = static_cast<uint32_t>( |
| 796 | scalingMode); |
| 797 | mCore->mSingleBufferCache.dataspace = dataSpace; |
| 798 | } |
| 799 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 800 | if (mCore->mQueue.empty()) { |
| 801 | // When the queue is empty, we can ignore mDequeueBufferCannotBlock |
| 802 | // and simply queue this buffer |
| 803 | mCore->mQueue.push_back(item); |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 804 | frameAvailableListener = mCore->mConsumerListener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 805 | } else { |
| 806 | // When the queue is not empty, we need to look at the front buffer |
| 807 | // state to see if we need to replace it |
| 808 | BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin()); |
| 809 | if (front->mIsDroppable) { |
| 810 | // If the front queued buffer is still being tracked, we first |
| 811 | // mark it as freed |
| 812 | if (mCore->stillTracking(front)) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 813 | mSlots[front->mSlot].mBufferState.freeQueued(); |
| 814 | |
| 815 | // After leaving single buffer mode, the shared buffer will |
| 816 | // still be around. Mark it as no longer shared if this |
| 817 | // operation causes it to be free. |
| 818 | if (!mCore->mSingleBufferMode && |
| 819 | mSlots[front->mSlot].mBufferState.isFree()) { |
| 820 | mSlots[front->mSlot].mBufferState.mShared = false; |
| 821 | } |
| 822 | // Don't put the shared buffer on the free list. |
| 823 | if (!mSlots[front->mSlot].mBufferState.isShared()) { |
| 824 | mCore->mFreeBuffers.push_front(front->mSlot); |
| 825 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 826 | } |
| 827 | // Overwrite the droppable buffer with the incoming one |
| 828 | *front = item; |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 829 | frameReplacedListener = mCore->mConsumerListener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 830 | } else { |
| 831 | mCore->mQueue.push_back(item); |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 832 | frameAvailableListener = mCore->mConsumerListener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | |
| 836 | mCore->mBufferHasBeenQueued = true; |
| 837 | mCore->mDequeueCondition.broadcast(); |
| 838 | |
| 839 | output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight, |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 840 | mCore->mTransformHint, |
| 841 | static_cast<uint32_t>(mCore->mQueue.size())); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 842 | |
| 843 | ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size()); |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 844 | |
| 845 | // Take a ticket for the callback functions |
| 846 | callbackTicket = mNextCallbackTicket++; |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 847 | |
| 848 | mCore->validateConsistencyLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 849 | } // Autolock scope |
| 850 | |
Dan Stoza | 8dc5539 | 2014-11-04 11:37:46 -0800 | [diff] [blame] | 851 | // Don't send the GraphicBuffer through the callback, and don't send |
| 852 | // the slot number, since the consumer shouldn't need it |
| 853 | item.mGraphicBuffer.clear(); |
| 854 | item.mSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 855 | |
| 856 | // Call back without the main BufferQueue lock held, but with the callback |
| 857 | // lock held so we can ensure that callbacks occur in order |
| 858 | { |
| 859 | Mutex::Autolock lock(mCallbackMutex); |
| 860 | while (callbackTicket != mCurrentCallbackTicket) { |
| 861 | mCallbackCondition.wait(mCallbackMutex); |
| 862 | } |
| 863 | |
| 864 | if (frameAvailableListener != NULL) { |
| 865 | frameAvailableListener->onFrameAvailable(item); |
| 866 | } else if (frameReplacedListener != NULL) { |
| 867 | frameReplacedListener->onFrameReplaced(item); |
| 868 | } |
| 869 | |
| 870 | ++mCurrentCallbackTicket; |
| 871 | mCallbackCondition.broadcast(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Christian Poetzsch | 82fbb12 | 2015-12-07 13:36:22 +0000 | [diff] [blame] | 874 | // Wait without lock held |
| 875 | if (mCore->mConnectedApi == NATIVE_WINDOW_API_EGL) { |
| 876 | // Waiting here allows for two full buffers to be queued but not a |
| 877 | // third. In the event that frames take varying time, this makes a |
| 878 | // small trade-off in favor of latency rather than throughput. |
| 879 | mLastQueueBufferFence->waitForever("Throttling EGL Production"); |
| 880 | mLastQueueBufferFence = fence; |
| 881 | } |
| 882 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 883 | return NO_ERROR; |
| 884 | } |
| 885 | |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 886 | status_t BufferQueueProducer::cancelBuffer(int slot, const sp<Fence>& fence) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 887 | ATRACE_CALL(); |
| 888 | BQ_LOGV("cancelBuffer: slot %d", slot); |
| 889 | Mutex::Autolock lock(mCore->mMutex); |
| 890 | |
| 891 | if (mCore->mIsAbandoned) { |
| 892 | BQ_LOGE("cancelBuffer: BufferQueue has been abandoned"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 893 | return NO_INIT; |
| 894 | } |
| 895 | |
| 896 | if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) { |
| 897 | BQ_LOGE("cancelBuffer: BufferQueue has no connected producer"); |
| 898 | return NO_INIT; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 901 | if (mCore->mSingleBufferMode) { |
| 902 | BQ_LOGE("cancelBuffer: cannot cancel a buffer in single buffer mode"); |
| 903 | return BAD_VALUE; |
| 904 | } |
| 905 | |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 906 | if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 907 | BQ_LOGE("cancelBuffer: slot index %d out of range [0, %d)", |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 908 | slot, BufferQueueDefs::NUM_BUFFER_SLOTS); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 909 | return BAD_VALUE; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 910 | } else if (!mSlots[slot].mBufferState.isDequeued()) { |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 911 | BQ_LOGE("cancelBuffer: slot %d is not owned by the producer " |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 912 | "(state = %s)", slot, mSlots[slot].mBufferState.string()); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 913 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 914 | } else if (fence == NULL) { |
| 915 | BQ_LOGE("cancelBuffer: fence is NULL"); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 916 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 917 | } |
| 918 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 919 | mSlots[slot].mBufferState.cancel(); |
| 920 | |
| 921 | // After leaving single buffer mode, the shared buffer will still be around. |
| 922 | // Mark it as no longer shared if this operation causes it to be free. |
| 923 | if (!mCore->mSingleBufferMode && mSlots[slot].mBufferState.isFree()) { |
| 924 | mSlots[slot].mBufferState.mShared = false; |
| 925 | } |
| 926 | |
| 927 | // Don't put the shared buffer on the free list. |
| 928 | if (!mSlots[slot].mBufferState.isShared()) { |
| 929 | mCore->mFreeBuffers.push_front(slot); |
| 930 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 931 | mSlots[slot].mFence = fence; |
| 932 | mCore->mDequeueCondition.broadcast(); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 933 | mCore->validateConsistencyLocked(); |
Pablo Ceballos | 583b1b3 | 2015-09-03 18:23:52 -0700 | [diff] [blame] | 934 | |
| 935 | return NO_ERROR; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | int BufferQueueProducer::query(int what, int *outValue) { |
| 939 | ATRACE_CALL(); |
| 940 | Mutex::Autolock lock(mCore->mMutex); |
| 941 | |
| 942 | if (outValue == NULL) { |
| 943 | BQ_LOGE("query: outValue was NULL"); |
| 944 | return BAD_VALUE; |
| 945 | } |
| 946 | |
| 947 | if (mCore->mIsAbandoned) { |
| 948 | BQ_LOGE("query: BufferQueue has been abandoned"); |
| 949 | return NO_INIT; |
| 950 | } |
| 951 | |
| 952 | int value; |
| 953 | switch (what) { |
| 954 | case NATIVE_WINDOW_WIDTH: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 955 | value = static_cast<int32_t>(mCore->mDefaultWidth); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 956 | break; |
| 957 | case NATIVE_WINDOW_HEIGHT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 958 | value = static_cast<int32_t>(mCore->mDefaultHeight); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 959 | break; |
| 960 | case NATIVE_WINDOW_FORMAT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 961 | value = static_cast<int32_t>(mCore->mDefaultBufferFormat); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 962 | break; |
| 963 | case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS: |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 964 | value = mCore->getMinUndequeuedBufferCountLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 965 | break; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 966 | case NATIVE_WINDOW_STICKY_TRANSFORM: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 967 | value = static_cast<int32_t>(mStickyTransform); |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 968 | break; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 969 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: |
| 970 | value = (mCore->mQueue.size() > 1); |
| 971 | break; |
| 972 | case NATIVE_WINDOW_CONSUMER_USAGE_BITS: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 973 | value = static_cast<int32_t>(mCore->mConsumerUsageBits); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 974 | break; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 975 | case NATIVE_WINDOW_DEFAULT_DATASPACE: |
| 976 | value = static_cast<int32_t>(mCore->mDefaultBufferDataSpace); |
| 977 | break; |
Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 978 | case NATIVE_WINDOW_BUFFER_AGE: |
| 979 | if (mCore->mBufferAge > INT32_MAX) { |
| 980 | value = 0; |
| 981 | } else { |
| 982 | value = static_cast<int32_t>(mCore->mBufferAge); |
| 983 | } |
| 984 | break; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 985 | default: |
| 986 | return BAD_VALUE; |
| 987 | } |
| 988 | |
| 989 | BQ_LOGV("query: %d? %d", what, value); |
| 990 | *outValue = value; |
| 991 | return NO_ERROR; |
| 992 | } |
| 993 | |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 994 | status_t BufferQueueProducer::connect(const sp<IProducerListener>& listener, |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 995 | int api, bool producerControlledByApp, QueueBufferOutput *output) { |
| 996 | ATRACE_CALL(); |
| 997 | Mutex::Autolock lock(mCore->mMutex); |
| 998 | mConsumerName = mCore->mConsumerName; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 999 | BQ_LOGV("connect: api=%d producerControlledByApp=%s", api, |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1000 | producerControlledByApp ? "true" : "false"); |
| 1001 | |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1002 | if (mCore->mIsAbandoned) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1003 | BQ_LOGE("connect: BufferQueue has been abandoned"); |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1004 | return NO_INIT; |
| 1005 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1006 | |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1007 | if (mCore->mConsumerListener == NULL) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1008 | BQ_LOGE("connect: BufferQueue has no consumer"); |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1009 | return NO_INIT; |
| 1010 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1011 | |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1012 | if (output == NULL) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1013 | BQ_LOGE("connect: output was NULL"); |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1014 | return BAD_VALUE; |
| 1015 | } |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1016 | |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1017 | if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1018 | BQ_LOGE("connect: already connected (cur=%d req=%d)", |
Dan Stoza | ae3c368 | 2014-04-18 15:43:35 -0700 | [diff] [blame] | 1019 | mCore->mConnectedApi, api); |
| 1020 | return BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | int status = NO_ERROR; |
| 1024 | switch (api) { |
| 1025 | case NATIVE_WINDOW_API_EGL: |
| 1026 | case NATIVE_WINDOW_API_CPU: |
| 1027 | case NATIVE_WINDOW_API_MEDIA: |
| 1028 | case NATIVE_WINDOW_API_CAMERA: |
| 1029 | mCore->mConnectedApi = api; |
| 1030 | output->inflate(mCore->mDefaultWidth, mCore->mDefaultHeight, |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1031 | mCore->mTransformHint, |
| 1032 | static_cast<uint32_t>(mCore->mQueue.size())); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1033 | |
| 1034 | // Set up a death notification so that we can disconnect |
| 1035 | // automatically if the remote producer dies |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1036 | if (listener != NULL && |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 1037 | IInterface::asBinder(listener)->remoteBinder() != NULL) { |
| 1038 | status = IInterface::asBinder(listener)->linkToDeath( |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1039 | static_cast<IBinder::DeathRecipient*>(this)); |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1040 | if (status != NO_ERROR) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1041 | BQ_LOGE("connect: linkToDeath failed: %s (%d)", |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1042 | strerror(-status), status); |
| 1043 | } |
| 1044 | } |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1045 | mCore->mConnectedProducerListener = listener; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1046 | break; |
| 1047 | default: |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1048 | BQ_LOGE("connect: unknown API %d", api); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1049 | status = BAD_VALUE; |
| 1050 | break; |
| 1051 | } |
| 1052 | |
| 1053 | mCore->mBufferHasBeenQueued = false; |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1054 | mCore->mDequeueBufferCannotBlock = false; |
| 1055 | if (mDequeueTimeout < 0) { |
| 1056 | mCore->mDequeueBufferCannotBlock = |
| 1057 | mCore->mConsumerControlledByApp && producerControlledByApp; |
| 1058 | } |
Dan Stoza | 2b83cc9 | 2015-05-12 14:55:15 -0700 | [diff] [blame] | 1059 | mCore->mAllowAllocation = true; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1060 | |
| 1061 | return status; |
| 1062 | } |
| 1063 | |
| 1064 | status_t BufferQueueProducer::disconnect(int api) { |
| 1065 | ATRACE_CALL(); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1066 | BQ_LOGV("disconnect: api %d", api); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1067 | |
| 1068 | int status = NO_ERROR; |
| 1069 | sp<IConsumerListener> listener; |
| 1070 | { // Autolock scope |
| 1071 | Mutex::Autolock lock(mCore->mMutex); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1072 | mCore->waitWhileAllocatingLocked(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1073 | |
| 1074 | if (mCore->mIsAbandoned) { |
| 1075 | // It's not really an error to disconnect after the surface has |
| 1076 | // been abandoned; it should just be a no-op. |
| 1077 | return NO_ERROR; |
| 1078 | } |
| 1079 | |
| 1080 | switch (api) { |
| 1081 | case NATIVE_WINDOW_API_EGL: |
| 1082 | case NATIVE_WINDOW_API_CPU: |
| 1083 | case NATIVE_WINDOW_API_MEDIA: |
| 1084 | case NATIVE_WINDOW_API_CAMERA: |
| 1085 | if (mCore->mConnectedApi == api) { |
| 1086 | mCore->freeAllBuffersLocked(); |
| 1087 | |
| 1088 | // Remove our death notification callback if we have one |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1089 | if (mCore->mConnectedProducerListener != NULL) { |
| 1090 | sp<IBinder> token = |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 1091 | IInterface::asBinder(mCore->mConnectedProducerListener); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1092 | // This can fail if we're here because of the death |
| 1093 | // notification, but we just ignore it |
| 1094 | token->unlinkToDeath( |
| 1095 | static_cast<IBinder::DeathRecipient*>(this)); |
| 1096 | } |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1097 | mCore->mConnectedProducerListener = NULL; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1098 | mCore->mConnectedApi = BufferQueueCore::NO_CONNECTED_API; |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1099 | mCore->mSidebandStream.clear(); |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1100 | mCore->mDequeueCondition.broadcast(); |
| 1101 | listener = mCore->mConsumerListener; |
Amith Dsouza | 4f21a4c | 2015-06-30 22:54:16 -0700 | [diff] [blame] | 1102 | } else if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1103 | BQ_LOGE("disconnect: still connected to another API " |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1104 | "(cur=%d req=%d)", mCore->mConnectedApi, api); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 1105 | status = BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1106 | } |
| 1107 | break; |
| 1108 | default: |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1109 | BQ_LOGE("disconnect: unknown API %d", api); |
Dan Stoza | 9f3053d | 2014-03-06 15:14:33 -0800 | [diff] [blame] | 1110 | status = BAD_VALUE; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1111 | break; |
| 1112 | } |
| 1113 | } // Autolock scope |
| 1114 | |
| 1115 | // Call back without lock held |
| 1116 | if (listener != NULL) { |
| 1117 | listener->onBuffersReleased(); |
| 1118 | } |
| 1119 | |
| 1120 | return status; |
| 1121 | } |
| 1122 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1123 | status_t BufferQueueProducer::setSidebandStream(const sp<NativeHandle>& stream) { |
Wonsik Kim | afe3081 | 2014-03-31 23:16:08 +0900 | [diff] [blame] | 1124 | sp<IConsumerListener> listener; |
| 1125 | { // Autolock scope |
| 1126 | Mutex::Autolock _l(mCore->mMutex); |
| 1127 | mCore->mSidebandStream = stream; |
| 1128 | listener = mCore->mConsumerListener; |
| 1129 | } // Autolock scope |
| 1130 | |
| 1131 | if (listener != NULL) { |
| 1132 | listener->onSidebandStreamChanged(); |
| 1133 | } |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1134 | return NO_ERROR; |
| 1135 | } |
| 1136 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 1137 | void BufferQueueProducer::allocateBuffers(uint32_t width, uint32_t height, |
| 1138 | PixelFormat format, uint32_t usage) { |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1139 | ATRACE_CALL(); |
| 1140 | while (true) { |
| 1141 | Vector<int> freeSlots; |
| 1142 | size_t newBufferCount = 0; |
| 1143 | uint32_t allocWidth = 0; |
| 1144 | uint32_t allocHeight = 0; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1145 | PixelFormat allocFormat = PIXEL_FORMAT_UNKNOWN; |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1146 | uint32_t allocUsage = 0; |
| 1147 | { // Autolock scope |
| 1148 | Mutex::Autolock lock(mCore->mMutex); |
| 1149 | mCore->waitWhileAllocatingLocked(); |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1150 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 1151 | if (!mCore->mAllowAllocation) { |
| 1152 | BQ_LOGE("allocateBuffers: allocation is not allowed for this " |
| 1153 | "BufferQueue"); |
| 1154 | return; |
| 1155 | } |
| 1156 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1157 | int currentBufferCount = 0; |
| 1158 | for (int slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) { |
| 1159 | if (mSlots[slot].mGraphicBuffer != NULL) { |
| 1160 | ++currentBufferCount; |
| 1161 | } else { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1162 | if (!mSlots[slot].mBufferState.isFree()) { |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1163 | BQ_LOGE("allocateBuffers: slot %d without buffer is not FREE", |
| 1164 | slot); |
| 1165 | continue; |
| 1166 | } |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1167 | |
Antoine Labour | 11f1487 | 2014-07-25 18:14:42 -0700 | [diff] [blame] | 1168 | freeSlots.push_back(slot); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1169 | } |
| 1170 | } |
| 1171 | |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 1172 | int maxBufferCount = mCore->getMaxBufferCountLocked(); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1173 | BQ_LOGV("allocateBuffers: allocating from %d buffers up to %d buffers", |
| 1174 | currentBufferCount, maxBufferCount); |
| 1175 | if (maxBufferCount <= currentBufferCount) |
| 1176 | return; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1177 | newBufferCount = |
| 1178 | static_cast<size_t>(maxBufferCount - currentBufferCount); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1179 | if (freeSlots.size() < newBufferCount) { |
| 1180 | BQ_LOGE("allocateBuffers: ran out of free slots"); |
| 1181 | return; |
| 1182 | } |
| 1183 | allocWidth = width > 0 ? width : mCore->mDefaultWidth; |
| 1184 | allocHeight = height > 0 ? height : mCore->mDefaultHeight; |
| 1185 | allocFormat = format != 0 ? format : mCore->mDefaultBufferFormat; |
| 1186 | allocUsage = usage | mCore->mConsumerUsageBits; |
| 1187 | |
| 1188 | mCore->mIsAllocating = true; |
| 1189 | } // Autolock scope |
| 1190 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1191 | Vector<sp<GraphicBuffer>> buffers; |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1192 | for (size_t i = 0; i < newBufferCount; ++i) { |
| 1193 | status_t result = NO_ERROR; |
| 1194 | sp<GraphicBuffer> graphicBuffer(mCore->mAllocator->createGraphicBuffer( |
| 1195 | allocWidth, allocHeight, allocFormat, allocUsage, &result)); |
| 1196 | if (result != NO_ERROR) { |
| 1197 | BQ_LOGE("allocateBuffers: failed to allocate buffer (%u x %u, format" |
| 1198 | " %u, usage %u)", width, height, format, usage); |
| 1199 | Mutex::Autolock lock(mCore->mMutex); |
| 1200 | mCore->mIsAllocating = false; |
| 1201 | mCore->mIsAllocatingCondition.broadcast(); |
| 1202 | return; |
| 1203 | } |
| 1204 | buffers.push_back(graphicBuffer); |
| 1205 | } |
| 1206 | |
| 1207 | { // Autolock scope |
| 1208 | Mutex::Autolock lock(mCore->mMutex); |
| 1209 | uint32_t checkWidth = width > 0 ? width : mCore->mDefaultWidth; |
| 1210 | uint32_t checkHeight = height > 0 ? height : mCore->mDefaultHeight; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1211 | PixelFormat checkFormat = format != 0 ? |
| 1212 | format : mCore->mDefaultBufferFormat; |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1213 | uint32_t checkUsage = usage | mCore->mConsumerUsageBits; |
| 1214 | if (checkWidth != allocWidth || checkHeight != allocHeight || |
| 1215 | checkFormat != allocFormat || checkUsage != allocUsage) { |
| 1216 | // Something changed while we released the lock. Retry. |
| 1217 | BQ_LOGV("allocateBuffers: size/format/usage changed while allocating. Retrying."); |
| 1218 | mCore->mIsAllocating = false; |
| 1219 | mCore->mIsAllocatingCondition.broadcast(); |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1220 | continue; |
| 1221 | } |
| 1222 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1223 | for (size_t i = 0; i < newBufferCount; ++i) { |
| 1224 | int slot = freeSlots[i]; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1225 | if (!mSlots[slot].mBufferState.isFree()) { |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1226 | // A consumer allocated the FREE slot with attachBuffer. Discard the buffer we |
| 1227 | // allocated. |
| 1228 | BQ_LOGV("allocateBuffers: slot %d was acquired while allocating. " |
| 1229 | "Dropping allocated buffer.", slot); |
| 1230 | continue; |
| 1231 | } |
| 1232 | mCore->freeBufferLocked(slot); // Clean up the slot first |
| 1233 | mSlots[slot].mGraphicBuffer = buffers[i]; |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1234 | mSlots[slot].mFence = Fence::NO_FENCE; |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 1235 | |
| 1236 | // freeBufferLocked puts this slot on the free slots list. Since |
| 1237 | // we then attached a buffer, move the slot to free buffer list. |
| 1238 | mCore->mFreeSlots.erase(slot); |
| 1239 | mCore->mFreeBuffers.push_front(slot); |
| 1240 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1241 | BQ_LOGV("allocateBuffers: allocated a new buffer in slot %d", slot); |
| 1242 | } |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1243 | |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1244 | mCore->mIsAllocating = false; |
| 1245 | mCore->mIsAllocatingCondition.broadcast(); |
Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 1246 | mCore->validateConsistencyLocked(); |
Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 1247 | } // Autolock scope |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 1251 | status_t BufferQueueProducer::allowAllocation(bool allow) { |
| 1252 | ATRACE_CALL(); |
| 1253 | BQ_LOGV("allowAllocation: %s", allow ? "true" : "false"); |
| 1254 | |
| 1255 | Mutex::Autolock lock(mCore->mMutex); |
| 1256 | mCore->mAllowAllocation = allow; |
| 1257 | return NO_ERROR; |
| 1258 | } |
| 1259 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1260 | status_t BufferQueueProducer::setGenerationNumber(uint32_t generationNumber) { |
| 1261 | ATRACE_CALL(); |
| 1262 | BQ_LOGV("setGenerationNumber: %u", generationNumber); |
| 1263 | |
| 1264 | Mutex::Autolock lock(mCore->mMutex); |
| 1265 | mCore->mGenerationNumber = generationNumber; |
| 1266 | return NO_ERROR; |
| 1267 | } |
| 1268 | |
Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 1269 | String8 BufferQueueProducer::getConsumerName() const { |
| 1270 | ATRACE_CALL(); |
| 1271 | BQ_LOGV("getConsumerName: %s", mConsumerName.string()); |
| 1272 | return mConsumerName; |
| 1273 | } |
| 1274 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1275 | uint64_t BufferQueueProducer::getNextFrameNumber() const { |
| 1276 | ATRACE_CALL(); |
| 1277 | |
| 1278 | Mutex::Autolock lock(mCore->mMutex); |
| 1279 | uint64_t nextFrameNumber = mCore->mFrameCounter + 1; |
| 1280 | return nextFrameNumber; |
| 1281 | } |
| 1282 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1283 | status_t BufferQueueProducer::setSingleBufferMode(bool singleBufferMode) { |
| 1284 | ATRACE_CALL(); |
| 1285 | BQ_LOGV("setSingleBufferMode: %d", singleBufferMode); |
| 1286 | |
| 1287 | Mutex::Autolock lock(mCore->mMutex); |
| 1288 | if (!singleBufferMode) { |
| 1289 | mCore->mSingleBufferSlot = BufferQueueCore::INVALID_BUFFER_SLOT; |
| 1290 | } |
| 1291 | mCore->mSingleBufferMode = singleBufferMode; |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1292 | return NO_ERROR; |
| 1293 | } |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1294 | |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 1295 | status_t BufferQueueProducer::setDequeueTimeout(nsecs_t timeout) { |
| 1296 | ATRACE_CALL(); |
| 1297 | BQ_LOGV("setDequeueTimeout: %" PRId64, timeout); |
| 1298 | |
| 1299 | Mutex::Autolock lock(mCore->mMutex); |
| 1300 | mDequeueTimeout = timeout; |
| 1301 | mCore->mDequeueBufferCannotBlock = false; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1302 | return NO_ERROR; |
| 1303 | } |
| 1304 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1305 | void BufferQueueProducer::binderDied(const wp<android::IBinder>& /* who */) { |
| 1306 | // If we're here, it means that a producer we were connected to died. |
| 1307 | // We're guaranteed that we are still connected to it because we remove |
| 1308 | // this callback upon disconnect. It's therefore safe to read mConnectedApi |
| 1309 | // without synchronization here. |
| 1310 | int api = mCore->mConnectedApi; |
| 1311 | disconnect(api); |
| 1312 | } |
| 1313 | |
| 1314 | } // namespace android |