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