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