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