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