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