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