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