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