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