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