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