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