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