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