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