Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 17 | #define LOG_TAG "BufferQueue" |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 20 | |
| 21 | #define GL_GLEXT_PROTOTYPES |
| 22 | #define EGL_EGLEXT_PROTOTYPES |
| 23 | |
| 24 | #include <EGL/egl.h> |
| 25 | #include <EGL/eglext.h> |
| 26 | |
| 27 | #include <gui/BufferQueue.h> |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 28 | #include <gui/IConsumerListener.h> |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 29 | #include <gui/ISurfaceComposer.h> |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 30 | #include <private/gui/ComposerService.h> |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 31 | |
| 32 | #include <utils/Log.h> |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 34 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 35 | // Macros for including the BufferQueue name in log messages |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 36 | #define ST_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 37 | #define ST_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 38 | #define ST_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 39 | #define ST_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 40 | #define ST_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 41 | |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 42 | #define ATRACE_BUFFER_INDEX(index) \ |
Jamie Gennis | 695e331 | 2012-04-16 20:34:58 -0700 | [diff] [blame] | 43 | if (ATRACE_ENABLED()) { \ |
| 44 | char ___traceBuf[1024]; \ |
| 45 | snprintf(___traceBuf, 1024, "%s: %d", mConsumerName.string(), \ |
| 46 | (index)); \ |
| 47 | android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \ |
| 48 | } |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 49 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 50 | namespace android { |
| 51 | |
| 52 | // Get an ID that's unique within this process. |
| 53 | static int32_t createProcessUniqueId() { |
| 54 | static volatile int32_t globalCounter = 0; |
| 55 | return android_atomic_inc(&globalCounter); |
| 56 | } |
| 57 | |
Jamie Gennis | cd1806e | 2012-05-10 02:22:33 -0700 | [diff] [blame] | 58 | static const char* scalingModeName(int scalingMode) { |
| 59 | switch (scalingMode) { |
| 60 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE"; |
| 61 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW"; |
| 62 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP"; |
| 63 | default: return "Unknown"; |
| 64 | } |
| 65 | } |
| 66 | |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 67 | BufferQueue::BufferQueue(const sp<IGraphicBufferAlloc>& allocator) : |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 68 | mDefaultWidth(1), |
| 69 | mDefaultHeight(1), |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 70 | mMaxAcquiredBufferCount(1), |
| 71 | mDefaultMaxBufferCount(2), |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 72 | mOverrideMaxBufferCount(0), |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 73 | mConsumerControlledByApp(false), |
| 74 | mDequeueBufferCannotBlock(false), |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 75 | mUseAsyncBuffer(true), |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 76 | mConnectedApi(NO_CONNECTED_API), |
| 77 | mAbandoned(false), |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 78 | mFrameCounter(0), |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 79 | mBufferHasBeenQueued(false), |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 80 | mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888), |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 81 | mConsumerUsageBits(0), |
| 82 | mTransformHint(0) |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 83 | { |
| 84 | // Choose a name using the PID and a process-unique ID. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 85 | mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId()); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 86 | |
| 87 | ST_LOGV("BufferQueue"); |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 88 | if (allocator == NULL) { |
| 89 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 90 | mGraphicBufferAlloc = composer->createGraphicBufferAlloc(); |
| 91 | if (mGraphicBufferAlloc == 0) { |
| 92 | ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()"); |
| 93 | } |
| 94 | } else { |
| 95 | mGraphicBufferAlloc = allocator; |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 96 | } |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | BufferQueue::~BufferQueue() { |
| 100 | ST_LOGV("~BufferQueue"); |
| 101 | } |
| 102 | |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 103 | status_t BufferQueue::setDefaultMaxBufferCountLocked(int count) { |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 104 | const int minBufferCount = mUseAsyncBuffer ? 2 : 1; |
| 105 | if (count < minBufferCount || count > NUM_BUFFER_SLOTS) |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 106 | return BAD_VALUE; |
| 107 | |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 108 | mDefaultMaxBufferCount = count; |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 109 | mDequeueCondition.broadcast(); |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 110 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 111 | return NO_ERROR; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 114 | void BufferQueue::setConsumerName(const String8& name) { |
| 115 | Mutex::Autolock lock(mMutex); |
| 116 | mConsumerName = name; |
| 117 | } |
| 118 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 119 | status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) { |
| 120 | Mutex::Autolock lock(mMutex); |
| 121 | mDefaultBufferFormat = defaultFormat; |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 122 | return NO_ERROR; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | status_t BufferQueue::setConsumerUsageBits(uint32_t usage) { |
| 126 | Mutex::Autolock lock(mMutex); |
| 127 | mConsumerUsageBits = usage; |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 128 | return NO_ERROR; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | status_t BufferQueue::setTransformHint(uint32_t hint) { |
Andy McFadden | 6905205 | 2012-09-14 16:10:11 -0700 | [diff] [blame] | 132 | ST_LOGV("setTransformHint: %02x", hint); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 133 | Mutex::Autolock lock(mMutex); |
| 134 | mTransformHint = hint; |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 135 | return NO_ERROR; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 138 | status_t BufferQueue::setBufferCount(int bufferCount) { |
| 139 | ST_LOGV("setBufferCount: count=%d", bufferCount); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 140 | |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 141 | sp<IConsumerListener> listener; |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 142 | { |
| 143 | Mutex::Autolock lock(mMutex); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 144 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 145 | if (mAbandoned) { |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 146 | ST_LOGE("setBufferCount: BufferQueue has been abandoned!"); |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 147 | return NO_INIT; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 148 | } |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 149 | if (bufferCount > NUM_BUFFER_SLOTS) { |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 150 | ST_LOGE("setBufferCount: bufferCount too large (max %d)", |
| 151 | NUM_BUFFER_SLOTS); |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 152 | return BAD_VALUE; |
| 153 | } |
| 154 | |
| 155 | // Error out if the user has dequeued buffers |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 156 | for (int i=0 ; i<NUM_BUFFER_SLOTS; i++) { |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 157 | if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) { |
| 158 | ST_LOGE("setBufferCount: client owns some buffers"); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | } |
| 162 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 163 | if (bufferCount == 0) { |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 164 | mOverrideMaxBufferCount = 0; |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 165 | mDequeueCondition.broadcast(); |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 166 | return NO_ERROR; |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 169 | // fine to assume async to false before we're setting the buffer count |
| 170 | const int minBufferSlots = getMinMaxBufferCountLocked(false); |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 171 | if (bufferCount < minBufferSlots) { |
| 172 | ST_LOGE("setBufferCount: requested buffer count (%d) is less than " |
| 173 | "minimum (%d)", bufferCount, minBufferSlots); |
| 174 | return BAD_VALUE; |
| 175 | } |
| 176 | |
| 177 | // here we're guaranteed that the client doesn't have dequeued buffers |
Lajos Molnar | 9e3cb55 | 2013-05-06 16:23:07 -0700 | [diff] [blame] | 178 | // and will release all of its buffer references. We don't clear the |
| 179 | // queue, however, so currently queued buffers still get displayed. |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 180 | freeAllBuffersLocked(); |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 181 | mOverrideMaxBufferCount = bufferCount; |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 182 | mDequeueCondition.broadcast(); |
| 183 | listener = mConsumerListener; |
| 184 | } // scope for lock |
| 185 | |
| 186 | if (listener != NULL) { |
| 187 | listener->onBuffersReleased(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 190 | return NO_ERROR; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Daniel Lam | b856052 | 2012-01-30 15:51:27 -0800 | [diff] [blame] | 193 | int BufferQueue::query(int what, int* outValue) |
| 194 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 195 | ATRACE_CALL(); |
Daniel Lam | b856052 | 2012-01-30 15:51:27 -0800 | [diff] [blame] | 196 | Mutex::Autolock lock(mMutex); |
| 197 | |
| 198 | if (mAbandoned) { |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 199 | ST_LOGE("query: BufferQueue has been abandoned!"); |
Daniel Lam | b856052 | 2012-01-30 15:51:27 -0800 | [diff] [blame] | 200 | return NO_INIT; |
| 201 | } |
| 202 | |
| 203 | int value; |
| 204 | switch (what) { |
| 205 | case NATIVE_WINDOW_WIDTH: |
| 206 | value = mDefaultWidth; |
| 207 | break; |
| 208 | case NATIVE_WINDOW_HEIGHT: |
| 209 | value = mDefaultHeight; |
| 210 | break; |
| 211 | case NATIVE_WINDOW_FORMAT: |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 212 | value = mDefaultBufferFormat; |
Daniel Lam | b856052 | 2012-01-30 15:51:27 -0800 | [diff] [blame] | 213 | break; |
| 214 | case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS: |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 215 | value = getMinUndequeuedBufferCount(false); |
Daniel Lam | b856052 | 2012-01-30 15:51:27 -0800 | [diff] [blame] | 216 | break; |
Mathias Agopian | 2488b20 | 2012-04-20 17:19:28 -0700 | [diff] [blame] | 217 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: |
| 218 | value = (mQueue.size() >= 2); |
| 219 | break; |
Eino-Ville Talvala | f7c6087 | 2013-07-30 14:05:02 -0700 | [diff] [blame] | 220 | case NATIVE_WINDOW_CONSUMER_USAGE_BITS: |
| 221 | value = mConsumerUsageBits; |
| 222 | break; |
Daniel Lam | b856052 | 2012-01-30 15:51:27 -0800 | [diff] [blame] | 223 | default: |
| 224 | return BAD_VALUE; |
| 225 | } |
| 226 | outValue[0] = value; |
| 227 | return NO_ERROR; |
| 228 | } |
| 229 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 230 | status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 231 | ATRACE_CALL(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 232 | ST_LOGV("requestBuffer: slot=%d", slot); |
| 233 | Mutex::Autolock lock(mMutex); |
| 234 | if (mAbandoned) { |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 235 | ST_LOGE("requestBuffer: BufferQueue has been abandoned!"); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 236 | return NO_INIT; |
| 237 | } |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 238 | if (slot < 0 || slot >= NUM_BUFFER_SLOTS) { |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 239 | ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d", |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 240 | NUM_BUFFER_SLOTS, slot); |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 241 | return BAD_VALUE; |
| 242 | } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) { |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 243 | ST_LOGE("requestBuffer: slot %d is not owned by the client (state=%d)", |
| 244 | slot, mSlots[slot].mBufferState); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 245 | return BAD_VALUE; |
| 246 | } |
| 247 | mSlots[slot].mRequestBufferCalled = true; |
| 248 | *buf = mSlots[slot].mGraphicBuffer; |
| 249 | return NO_ERROR; |
| 250 | } |
| 251 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 252 | status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>* outFence, bool async, |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 253 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 254 | ATRACE_CALL(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 255 | ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage); |
| 256 | |
| 257 | if ((w && !h) || (!w && h)) { |
| 258 | ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h); |
| 259 | return BAD_VALUE; |
| 260 | } |
| 261 | |
| 262 | status_t returnFlags(OK); |
| 263 | EGLDisplay dpy = EGL_NO_DISPLAY; |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 264 | EGLSyncKHR eglFence = EGL_NO_SYNC_KHR; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 265 | |
| 266 | { // Scope for the lock |
| 267 | Mutex::Autolock lock(mMutex); |
| 268 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 269 | if (format == 0) { |
| 270 | format = mDefaultBufferFormat; |
| 271 | } |
| 272 | // turn on usage bits the consumer requested |
| 273 | usage |= mConsumerUsageBits; |
| 274 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 275 | int found = -1; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 276 | bool tryAgain = true; |
| 277 | while (tryAgain) { |
| 278 | if (mAbandoned) { |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 279 | ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!"); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 280 | return NO_INIT; |
| 281 | } |
| 282 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 283 | const int maxBufferCount = getMaxBufferCountLocked(async); |
| 284 | if (async && mOverrideMaxBufferCount) { |
| 285 | // FIXME: some drivers are manually setting the buffer-count (which they |
| 286 | // shouldn't), so we do this extra test here to handle that case. |
| 287 | // This is TEMPORARY, until we get this fixed. |
| 288 | if (mOverrideMaxBufferCount < maxBufferCount) { |
| 289 | ST_LOGE("dequeueBuffer: async mode is invalid with buffercount override"); |
| 290 | return BAD_VALUE; |
| 291 | } |
| 292 | } |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 293 | |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 294 | // Free up any buffers that are in slots beyond the max buffer |
| 295 | // count. |
| 296 | for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) { |
| 297 | assert(mSlots[i].mBufferState == BufferSlot::FREE); |
| 298 | if (mSlots[i].mGraphicBuffer != NULL) { |
| 299 | freeBufferLocked(i); |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 300 | returnFlags |= IGraphicBufferProducer::RELEASE_ALL_BUFFERS; |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 301 | } |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // look for a free buffer to give to the client |
| 305 | found = INVALID_BUFFER_SLOT; |
Mathias Agopian | 6bac363 | 2013-07-23 21:50:20 -0700 | [diff] [blame] | 306 | int dequeuedCount = 0; |
| 307 | int acquiredCount = 0; |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 308 | for (int i = 0; i < maxBufferCount; i++) { |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 309 | const int state = mSlots[i].mBufferState; |
Mathias Agopian | 6bac363 | 2013-07-23 21:50:20 -0700 | [diff] [blame] | 310 | switch (state) { |
| 311 | case BufferSlot::DEQUEUED: |
| 312 | dequeuedCount++; |
| 313 | break; |
| 314 | case BufferSlot::ACQUIRED: |
| 315 | acquiredCount++; |
| 316 | break; |
| 317 | case BufferSlot::FREE: |
| 318 | /* We return the oldest of the free buffers to avoid |
| 319 | * stalling the producer if possible. This is because |
| 320 | * the consumer may still have pending reads of the |
| 321 | * buffers in flight. |
| 322 | */ |
| 323 | if ((found < 0) || |
| 324 | mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) { |
| 325 | found = i; |
| 326 | } |
| 327 | break; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | // clients are not allowed to dequeue more than one buffer |
| 332 | // if they didn't set a buffer count. |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 333 | if (!mOverrideMaxBufferCount && dequeuedCount) { |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 334 | ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without " |
| 335 | "setting the buffer count"); |
| 336 | return -EINVAL; |
| 337 | } |
| 338 | |
| 339 | // See whether a buffer has been queued since the last |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 340 | // setBufferCount so we know whether to perform the min undequeued |
| 341 | // buffers check below. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 342 | if (mBufferHasBeenQueued) { |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 343 | // make sure the client is not trying to dequeue more buffers |
| 344 | // than allowed. |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 345 | const int newUndequeuedCount = maxBufferCount - (dequeuedCount+1); |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 346 | const int minUndequeuedCount = getMinUndequeuedBufferCount(async); |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 347 | if (newUndequeuedCount < minUndequeuedCount) { |
| 348 | ST_LOGE("dequeueBuffer: min undequeued buffer count (%d) " |
| 349 | "exceeded (dequeued=%d undequeudCount=%d)", |
| 350 | minUndequeuedCount, dequeuedCount, |
| 351 | newUndequeuedCount); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 352 | return -EBUSY; |
| 353 | } |
| 354 | } |
| 355 | |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 356 | // If no buffer is found, wait for a buffer to be released or for |
| 357 | // the max buffer count to change. |
Daniel Lam | c2c1f2f | 2012-03-07 14:11:29 -0800 | [diff] [blame] | 358 | tryAgain = found == INVALID_BUFFER_SLOT; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 359 | if (tryAgain) { |
Mathias Agopian | 6bac363 | 2013-07-23 21:50:20 -0700 | [diff] [blame] | 360 | // return an error if we're in "cannot block" mode (producer and consumer |
| 361 | // are controlled by the application) -- however, the consumer is allowed |
| 362 | // to acquire briefly an extra buffer (which could cause us to have to wait here) |
| 363 | // and that's okay because we know the wait will be brief (it happens |
| 364 | // if we dequeue a buffer while the consumer has acquired one but not released |
| 365 | // the old one yet -- for e.g.: see GLConsumer::updateTexImage()). |
| 366 | if (mDequeueBufferCannotBlock && (acquiredCount <= mMaxAcquiredBufferCount)) { |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 367 | ST_LOGE("dequeueBuffer: would block! returning an error instead."); |
| 368 | return WOULD_BLOCK; |
| 369 | } |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 370 | mDequeueCondition.wait(mMutex); |
| 371 | } |
| 372 | } |
| 373 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 374 | |
| 375 | if (found == INVALID_BUFFER_SLOT) { |
| 376 | // This should not happen. |
| 377 | ST_LOGE("dequeueBuffer: no available buffer slots"); |
| 378 | return -EBUSY; |
| 379 | } |
| 380 | |
| 381 | const int buf = found; |
| 382 | *outBuf = found; |
| 383 | |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 384 | ATRACE_BUFFER_INDEX(buf); |
| 385 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 386 | const bool useDefaultSize = !w && !h; |
| 387 | if (useDefaultSize) { |
| 388 | // use the default size |
| 389 | w = mDefaultWidth; |
| 390 | h = mDefaultHeight; |
| 391 | } |
| 392 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 393 | mSlots[buf].mBufferState = BufferSlot::DEQUEUED; |
| 394 | |
| 395 | const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer); |
| 396 | if ((buffer == NULL) || |
| 397 | (uint32_t(buffer->width) != w) || |
| 398 | (uint32_t(buffer->height) != h) || |
| 399 | (uint32_t(buffer->format) != format) || |
| 400 | ((uint32_t(buffer->usage) & usage) != usage)) |
| 401 | { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 402 | mSlots[buf].mAcquireCalled = false; |
Jamie Gennis | 1efe099 | 2012-10-04 18:34:01 -0700 | [diff] [blame] | 403 | mSlots[buf].mGraphicBuffer = NULL; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 404 | mSlots[buf].mRequestBufferCalled = false; |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 405 | mSlots[buf].mEglFence = EGL_NO_SYNC_KHR; |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 406 | mSlots[buf].mFence = Fence::NO_FENCE; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 407 | mSlots[buf].mEglDisplay = EGL_NO_DISPLAY; |
| 408 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 409 | returnFlags |= IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 412 | |
| 413 | if (CC_UNLIKELY(mSlots[buf].mFence == NULL)) { |
| 414 | ST_LOGE("dequeueBuffer: about to return a NULL fence from mSlot. " |
| 415 | "buf=%d, w=%d, h=%d, format=%d", |
| 416 | buf, buffer->width, buffer->height, buffer->format); |
| 417 | } |
| 418 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 419 | dpy = mSlots[buf].mEglDisplay; |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 420 | eglFence = mSlots[buf].mEglFence; |
Jesse Hall | 4c00cc1 | 2013-03-15 21:34:30 -0700 | [diff] [blame] | 421 | *outFence = mSlots[buf].mFence; |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 422 | mSlots[buf].mEglFence = EGL_NO_SYNC_KHR; |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 423 | mSlots[buf].mFence = Fence::NO_FENCE; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 424 | } // end lock scope |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 425 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 426 | if (returnFlags & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) { |
Jamie Gennis | 1efe099 | 2012-10-04 18:34:01 -0700 | [diff] [blame] | 427 | status_t error; |
| 428 | sp<GraphicBuffer> graphicBuffer( |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 429 | mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage, &error)); |
Jamie Gennis | 1efe099 | 2012-10-04 18:34:01 -0700 | [diff] [blame] | 430 | if (graphicBuffer == 0) { |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 431 | ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed"); |
Jamie Gennis | 1efe099 | 2012-10-04 18:34:01 -0700 | [diff] [blame] | 432 | return error; |
| 433 | } |
| 434 | |
| 435 | { // Scope for the lock |
| 436 | Mutex::Autolock lock(mMutex); |
| 437 | |
| 438 | if (mAbandoned) { |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 439 | ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!"); |
Jamie Gennis | 1efe099 | 2012-10-04 18:34:01 -0700 | [diff] [blame] | 440 | return NO_INIT; |
| 441 | } |
| 442 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 443 | mSlots[*outBuf].mFrameNumber = ~0; |
Jamie Gennis | 1efe099 | 2012-10-04 18:34:01 -0700 | [diff] [blame] | 444 | mSlots[*outBuf].mGraphicBuffer = graphicBuffer; |
| 445 | } |
| 446 | } |
| 447 | |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 448 | if (eglFence != EGL_NO_SYNC_KHR) { |
| 449 | EGLint result = eglClientWaitSyncKHR(dpy, eglFence, 0, 1000000000); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 450 | // If something goes wrong, log the error, but return the buffer without |
| 451 | // synchronizing access to it. It's too late at this point to abort the |
| 452 | // dequeue operation. |
| 453 | if (result == EGL_FALSE) { |
Jamie Gennis | b7a6b96 | 2012-05-13 19:41:35 -0700 | [diff] [blame] | 454 | ST_LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError()); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 455 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
Jamie Gennis | b7a6b96 | 2012-05-13 19:41:35 -0700 | [diff] [blame] | 456 | ST_LOGE("dequeueBuffer: timeout waiting for fence"); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 457 | } |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 458 | eglDestroySyncKHR(dpy, eglFence); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 461 | ST_LOGV("dequeueBuffer: returning slot=%d/%llu buf=%p flags=%#x", *outBuf, |
| 462 | mSlots[*outBuf].mFrameNumber, |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 463 | mSlots[*outBuf].mGraphicBuffer->handle, returnFlags); |
| 464 | |
| 465 | return returnFlags; |
| 466 | } |
| 467 | |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 468 | status_t BufferQueue::queueBuffer(int buf, |
| 469 | const QueueBufferInput& input, QueueBufferOutput* output) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 470 | ATRACE_CALL(); |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 471 | ATRACE_BUFFER_INDEX(buf); |
| 472 | |
Jamie Gennis | efc7ab6 | 2012-04-17 19:36:18 -0700 | [diff] [blame] | 473 | Rect crop; |
| 474 | uint32_t transform; |
| 475 | int scalingMode; |
| 476 | int64_t timestamp; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 477 | bool isAutoTimestamp; |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 478 | bool async; |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 479 | sp<Fence> fence; |
Jamie Gennis | efc7ab6 | 2012-04-17 19:36:18 -0700 | [diff] [blame] | 480 | |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 481 | input.deflate(×tamp, &isAutoTimestamp, &crop, &scalingMode, &transform, |
| 482 | &async, &fence); |
Jamie Gennis | efc7ab6 | 2012-04-17 19:36:18 -0700 | [diff] [blame] | 483 | |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 484 | if (fence == NULL) { |
| 485 | ST_LOGE("queueBuffer: fence is NULL"); |
| 486 | return BAD_VALUE; |
| 487 | } |
| 488 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 489 | switch (scalingMode) { |
| 490 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 491 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 492 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: |
| 493 | case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: |
| 494 | break; |
| 495 | default: |
| 496 | ST_LOGE("unknown scaling mode: %d", scalingMode); |
| 497 | return -EINVAL; |
| 498 | } |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 499 | |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 500 | sp<IConsumerListener> listener; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 501 | |
| 502 | { // scope for the lock |
| 503 | Mutex::Autolock lock(mMutex); |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 504 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 505 | if (mAbandoned) { |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 506 | ST_LOGE("queueBuffer: BufferQueue has been abandoned!"); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 507 | return NO_INIT; |
| 508 | } |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 509 | |
| 510 | const int maxBufferCount = getMaxBufferCountLocked(async); |
| 511 | if (async && mOverrideMaxBufferCount) { |
| 512 | // FIXME: some drivers are manually setting the buffer-count (which they |
| 513 | // shouldn't), so we do this extra test here to handle that case. |
| 514 | // This is TEMPORARY, until we get this fixed. |
| 515 | if (mOverrideMaxBufferCount < maxBufferCount) { |
| 516 | ST_LOGE("queueBuffer: async mode is invalid with buffercount override"); |
| 517 | return BAD_VALUE; |
| 518 | } |
| 519 | } |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 520 | if (buf < 0 || buf >= maxBufferCount) { |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 521 | ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d", |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 522 | maxBufferCount, buf); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 523 | return -EINVAL; |
| 524 | } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) { |
| 525 | ST_LOGE("queueBuffer: slot %d is not owned by the client " |
| 526 | "(state=%d)", buf, mSlots[buf].mBufferState); |
| 527 | return -EINVAL; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 528 | } else if (!mSlots[buf].mRequestBufferCalled) { |
| 529 | ST_LOGE("queueBuffer: slot %d was enqueued without requesting a " |
| 530 | "buffer", buf); |
| 531 | return -EINVAL; |
| 532 | } |
| 533 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 534 | ST_LOGV("queueBuffer: slot=%d/%llu time=%#llx crop=[%d,%d,%d,%d] " |
| 535 | "tr=%#x scale=%s", |
| 536 | buf, mFrameCounter + 1, timestamp, |
| 537 | crop.left, crop.top, crop.right, crop.bottom, |
| 538 | transform, scalingModeName(scalingMode)); |
| 539 | |
Jamie Gennis | d72f233 | 2012-05-07 13:50:11 -0700 | [diff] [blame] | 540 | const sp<GraphicBuffer>& graphicBuffer(mSlots[buf].mGraphicBuffer); |
| 541 | Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight()); |
| 542 | Rect croppedCrop; |
| 543 | crop.intersect(bufferRect, &croppedCrop); |
| 544 | if (croppedCrop != crop) { |
| 545 | ST_LOGE("queueBuffer: crop rect is not contained within the " |
| 546 | "buffer in slot %d", buf); |
| 547 | return -EINVAL; |
| 548 | } |
| 549 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 550 | mSlots[buf].mFence = fence; |
| 551 | mSlots[buf].mBufferState = BufferSlot::QUEUED; |
| 552 | mFrameCounter++; |
| 553 | mSlots[buf].mFrameNumber = mFrameCounter; |
| 554 | |
| 555 | BufferItem item; |
| 556 | item.mAcquireCalled = mSlots[buf].mAcquireCalled; |
| 557 | item.mGraphicBuffer = mSlots[buf].mGraphicBuffer; |
| 558 | item.mCrop = crop; |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 559 | item.mTransform = transform & ~NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY; |
| 560 | item.mTransformToDisplayInverse = bool(transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY); |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 561 | item.mScalingMode = scalingMode; |
| 562 | item.mTimestamp = timestamp; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 563 | item.mIsAutoTimestamp = isAutoTimestamp; |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 564 | item.mFrameNumber = mFrameCounter; |
| 565 | item.mBuf = buf; |
| 566 | item.mFence = fence; |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 567 | item.mIsDroppable = mDequeueBufferCannotBlock || async; |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 568 | |
Mathias Agopian | a3fbda3 | 2013-07-18 15:55:03 -0700 | [diff] [blame] | 569 | if (mQueue.empty()) { |
| 570 | // when the queue is empty, we can ignore "mDequeueBufferCannotBlock", and |
| 571 | // simply queue this buffer. |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 572 | mQueue.push_back(item); |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 573 | listener = mConsumerListener; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 574 | } else { |
Mathias Agopian | a3fbda3 | 2013-07-18 15:55:03 -0700 | [diff] [blame] | 575 | // when the queue is not empty, we need to look at the front buffer |
| 576 | // state and see if we need to replace it. |
| 577 | Fifo::iterator front(mQueue.begin()); |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 578 | if (front->mIsDroppable) { |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 579 | // buffer slot currently queued is marked free if still tracked |
| 580 | if (stillTracking(front)) { |
| 581 | mSlots[front->mBuf].mBufferState = BufferSlot::FREE; |
Mathias Agopian | 26a6f37 | 2013-07-18 22:25:55 -0700 | [diff] [blame] | 582 | // reset the frame number of the freed buffer so that it is the first in |
| 583 | // line to be dequeued again. |
| 584 | mSlots[front->mBuf].mFrameNumber = 0; |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 585 | } |
Mathias Agopian | a3fbda3 | 2013-07-18 15:55:03 -0700 | [diff] [blame] | 586 | // and we record the new buffer in the queued list |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 587 | *front = item; |
Mathias Agopian | a3fbda3 | 2013-07-18 15:55:03 -0700 | [diff] [blame] | 588 | } else { |
| 589 | mQueue.push_back(item); |
| 590 | listener = mConsumerListener; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 594 | mBufferHasBeenQueued = true; |
Dave Burke | 74ff8c2 | 2012-03-12 21:49:41 -0700 | [diff] [blame] | 595 | mDequeueCondition.broadcast(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 596 | |
Mathias Agopian | 2488b20 | 2012-04-20 17:19:28 -0700 | [diff] [blame] | 597 | output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint, |
| 598 | mQueue.size()); |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 599 | |
| 600 | ATRACE_INT(mConsumerName.string(), mQueue.size()); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 601 | } // scope for the lock |
| 602 | |
| 603 | // call back without lock held |
| 604 | if (listener != 0) { |
| 605 | listener->onFrameAvailable(); |
| 606 | } |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 607 | return NO_ERROR; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Jesse Hall | 4c00cc1 | 2013-03-15 21:34:30 -0700 | [diff] [blame] | 610 | void BufferQueue::cancelBuffer(int buf, const sp<Fence>& fence) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 611 | ATRACE_CALL(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 612 | ST_LOGV("cancelBuffer: slot=%d", buf); |
| 613 | Mutex::Autolock lock(mMutex); |
| 614 | |
| 615 | if (mAbandoned) { |
| 616 | ST_LOGW("cancelBuffer: BufferQueue has been abandoned!"); |
| 617 | return; |
| 618 | } |
| 619 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 620 | if (buf < 0 || buf >= NUM_BUFFER_SLOTS) { |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 621 | ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d", |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 622 | NUM_BUFFER_SLOTS, buf); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 623 | return; |
| 624 | } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) { |
| 625 | ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)", |
| 626 | buf, mSlots[buf].mBufferState); |
| 627 | return; |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 628 | } else if (fence == NULL) { |
| 629 | ST_LOGE("cancelBuffer: fence is NULL"); |
| 630 | return; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 631 | } |
| 632 | mSlots[buf].mBufferState = BufferSlot::FREE; |
| 633 | mSlots[buf].mFrameNumber = 0; |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 634 | mSlots[buf].mFence = fence; |
Dave Burke | 74ff8c2 | 2012-03-12 21:49:41 -0700 | [diff] [blame] | 635 | mDequeueCondition.broadcast(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 638 | |
| 639 | status_t BufferQueue::connect(const sp<IBinder>& token, |
| 640 | int api, bool producerControlledByApp, QueueBufferOutput* output) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 641 | ATRACE_CALL(); |
Jesse Hall | 8db9255 | 2013-08-29 16:03:50 -0700 | [diff] [blame] | 642 | ST_LOGV("connect: api=%d producerControlledByApp=%s", api, |
| 643 | producerControlledByApp ? "true" : "false"); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 644 | Mutex::Autolock lock(mMutex); |
| 645 | |
Andy McFadden | 8507586 | 2013-10-18 15:50:24 -0700 | [diff] [blame] | 646 | retry: |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 647 | if (mAbandoned) { |
| 648 | ST_LOGE("connect: BufferQueue has been abandoned!"); |
| 649 | return NO_INIT; |
| 650 | } |
| 651 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 652 | if (mConsumerListener == NULL) { |
| 653 | ST_LOGE("connect: BufferQueue has no consumer!"); |
| 654 | return NO_INIT; |
| 655 | } |
| 656 | |
Andy McFadden | 8507586 | 2013-10-18 15:50:24 -0700 | [diff] [blame] | 657 | if (mConnectedApi != NO_CONNECTED_API) { |
| 658 | ST_LOGE("connect: already connected (cur=%d, req=%d)", |
| 659 | mConnectedApi, api); |
| 660 | return -EINVAL; |
| 661 | } |
| 662 | |
| 663 | // If we disconnect and reconnect quickly, we can be in a state where our slots are |
| 664 | // empty but we have many buffers in the queue. This can cause us to run out of |
| 665 | // memory if we outrun the consumer. Wait here if it looks like we have too many |
| 666 | // buffers queued up. |
| 667 | int maxBufferCount = getMaxBufferCountLocked(false); // worst-case, i.e. largest value |
| 668 | if (mQueue.size() > (size_t) maxBufferCount) { |
| 669 | // TODO: make this bound tighter? |
| 670 | ST_LOGV("queue size is %d, waiting", mQueue.size()); |
| 671 | mDequeueCondition.wait(mMutex); |
| 672 | goto retry; |
| 673 | } |
| 674 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 675 | int err = NO_ERROR; |
| 676 | switch (api) { |
| 677 | case NATIVE_WINDOW_API_EGL: |
| 678 | case NATIVE_WINDOW_API_CPU: |
| 679 | case NATIVE_WINDOW_API_MEDIA: |
| 680 | case NATIVE_WINDOW_API_CAMERA: |
Andy McFadden | 8507586 | 2013-10-18 15:50:24 -0700 | [diff] [blame] | 681 | mConnectedApi = api; |
| 682 | output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint, mQueue.size()); |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 683 | |
Andy McFadden | 8507586 | 2013-10-18 15:50:24 -0700 | [diff] [blame] | 684 | // set-up a death notification so that we can disconnect |
| 685 | // automatically when/if the remote producer dies. |
| 686 | if (token != NULL && token->remoteBinder() != NULL) { |
| 687 | status_t err = token->linkToDeath(static_cast<IBinder::DeathRecipient*>(this)); |
| 688 | if (err == NO_ERROR) { |
| 689 | mConnectedProducerToken = token; |
| 690 | } else { |
| 691 | ALOGE("linkToDeath failed: %s (%d)", strerror(-err), err); |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 692 | } |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 693 | } |
| 694 | break; |
| 695 | default: |
| 696 | err = -EINVAL; |
| 697 | break; |
| 698 | } |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 699 | |
| 700 | mBufferHasBeenQueued = false; |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 701 | mDequeueBufferCannotBlock = mConsumerControlledByApp && producerControlledByApp; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 702 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 703 | return err; |
| 704 | } |
| 705 | |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 706 | void BufferQueue::binderDied(const wp<IBinder>& who) { |
| 707 | // If we're here, it means that a producer we were connected to died. |
| 708 | // We're GUARANTEED that we still are connected to it because it has no other way |
| 709 | // to get disconnected -- or -- we wouldn't be here because we're removing this |
| 710 | // callback upon disconnect. Therefore, it's okay to read mConnectedApi without |
| 711 | // synchronization here. |
| 712 | int api = mConnectedApi; |
| 713 | this->disconnect(api); |
| 714 | } |
| 715 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 716 | status_t BufferQueue::disconnect(int api) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 717 | ATRACE_CALL(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 718 | ST_LOGV("disconnect: api=%d", api); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 719 | |
| 720 | int err = NO_ERROR; |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 721 | sp<IConsumerListener> listener; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 722 | |
| 723 | { // Scope for the lock |
| 724 | Mutex::Autolock lock(mMutex); |
| 725 | |
| 726 | if (mAbandoned) { |
| 727 | // it is not really an error to disconnect after the surface |
| 728 | // has been abandoned, it should just be a no-op. |
| 729 | return NO_ERROR; |
| 730 | } |
| 731 | |
| 732 | switch (api) { |
| 733 | case NATIVE_WINDOW_API_EGL: |
| 734 | case NATIVE_WINDOW_API_CPU: |
| 735 | case NATIVE_WINDOW_API_MEDIA: |
| 736 | case NATIVE_WINDOW_API_CAMERA: |
| 737 | if (mConnectedApi == api) { |
Mathias Agopian | a3fbda3 | 2013-07-18 15:55:03 -0700 | [diff] [blame] | 738 | freeAllBuffersLocked(); |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 739 | // remove our death notification callback if we have one |
| 740 | sp<IBinder> token = mConnectedProducerToken; |
| 741 | if (token != NULL) { |
| 742 | // this can fail if we're here because of the death notification |
| 743 | // either way, we just ignore. |
| 744 | token->unlinkToDeath(static_cast<IBinder::DeathRecipient*>(this)); |
| 745 | } |
| 746 | mConnectedProducerToken = NULL; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 747 | mConnectedApi = NO_CONNECTED_API; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 748 | mDequeueCondition.broadcast(); |
| 749 | listener = mConsumerListener; |
| 750 | } else { |
| 751 | ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)", |
| 752 | mConnectedApi, api); |
| 753 | err = -EINVAL; |
| 754 | } |
| 755 | break; |
| 756 | default: |
| 757 | ST_LOGE("disconnect: unknown API %d", api); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 758 | err = -EINVAL; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 759 | break; |
| 760 | } |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 761 | } |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 762 | |
| 763 | if (listener != NULL) { |
| 764 | listener->onBuffersReleased(); |
| 765 | } |
| 766 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 767 | return err; |
| 768 | } |
| 769 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 770 | void BufferQueue::dump(String8& result, const char* prefix) const { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 771 | Mutex::Autolock _l(mMutex); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 772 | |
| 773 | String8 fifo; |
| 774 | int fifoSize = 0; |
| 775 | Fifo::const_iterator i(mQueue.begin()); |
| 776 | while (i != mQueue.end()) { |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 777 | fifo.appendFormat("%02d:%p crop=[%d,%d,%d,%d], " |
| 778 | "xform=0x%02x, time=%#llx, scale=%s\n", |
| 779 | i->mBuf, i->mGraphicBuffer.get(), |
| 780 | i->mCrop.left, i->mCrop.top, i->mCrop.right, |
| 781 | i->mCrop.bottom, i->mTransform, i->mTimestamp, |
| 782 | scalingModeName(i->mScalingMode) |
| 783 | ); |
| 784 | i++; |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 785 | fifoSize++; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 786 | } |
| 787 | |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 788 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 789 | result.appendFormat( |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 790 | "%s-BufferQueue mMaxAcquiredBufferCount=%d, mDequeueBufferCannotBlock=%d, default-size=[%dx%d], " |
Andy McFadden | 6905205 | 2012-09-14 16:10:11 -0700 | [diff] [blame] | 791 | "default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n", |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 792 | prefix, mMaxAcquiredBufferCount, mDequeueBufferCannotBlock, mDefaultWidth, |
Andy McFadden | 6905205 | 2012-09-14 16:10:11 -0700 | [diff] [blame] | 793 | mDefaultHeight, mDefaultBufferFormat, mTransformHint, |
| 794 | fifoSize, fifo.string()); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 795 | |
| 796 | struct { |
| 797 | const char * operator()(int state) const { |
| 798 | switch (state) { |
| 799 | case BufferSlot::DEQUEUED: return "DEQUEUED"; |
| 800 | case BufferSlot::QUEUED: return "QUEUED"; |
| 801 | case BufferSlot::FREE: return "FREE"; |
| 802 | case BufferSlot::ACQUIRED: return "ACQUIRED"; |
| 803 | default: return "Unknown"; |
| 804 | } |
| 805 | } |
| 806 | } stateName; |
| 807 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 808 | // just trim the free buffers to not spam the dump |
| 809 | int maxBufferCount = 0; |
| 810 | for (int i=NUM_BUFFER_SLOTS-1 ; i>=0 ; i--) { |
| 811 | const BufferSlot& slot(mSlots[i]); |
| 812 | if ((slot.mBufferState != BufferSlot::FREE) || (slot.mGraphicBuffer != NULL)) { |
| 813 | maxBufferCount = i+1; |
| 814 | break; |
| 815 | } |
| 816 | } |
| 817 | |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 818 | for (int i=0 ; i<maxBufferCount ; i++) { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 819 | const BufferSlot& slot(mSlots[i]); |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 820 | const sp<GraphicBuffer>& buf(slot.mGraphicBuffer); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 821 | result.appendFormat( |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 822 | "%s%s[%02d:%p] state=%-8s", |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 823 | prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i, buf.get(), |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 824 | stateName(slot.mBufferState) |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 825 | ); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 826 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 827 | if (buf != NULL) { |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 828 | result.appendFormat( |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 829 | ", %p [%4ux%4u:%4u,%3X]", |
| 830 | buf->handle, buf->width, buf->height, buf->stride, |
| 831 | buf->format); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 832 | } |
| 833 | result.append("\n"); |
| 834 | } |
| 835 | } |
| 836 | |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 837 | void BufferQueue::freeBufferLocked(int slot) { |
| 838 | ST_LOGV("freeBufferLocked: slot=%d", slot); |
| 839 | mSlots[slot].mGraphicBuffer = 0; |
| 840 | if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) { |
| 841 | mSlots[slot].mNeedsCleanupOnRelease = true; |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 842 | } |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 843 | mSlots[slot].mBufferState = BufferSlot::FREE; |
| 844 | mSlots[slot].mFrameNumber = 0; |
| 845 | mSlots[slot].mAcquireCalled = false; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 846 | |
| 847 | // destroy fence as BufferQueue now takes ownership |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 848 | if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) { |
| 849 | eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence); |
| 850 | mSlots[slot].mEglFence = EGL_NO_SYNC_KHR; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 851 | } |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 852 | mSlots[slot].mFence = Fence::NO_FENCE; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | void BufferQueue::freeAllBuffersLocked() { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 856 | mBufferHasBeenQueued = false; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 857 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 858 | freeBufferLocked(i); |
| 859 | } |
| 860 | } |
| 861 | |
Andy McFadden | 14fab7d | 2013-08-01 13:37:42 -0700 | [diff] [blame] | 862 | status_t BufferQueue::acquireBuffer(BufferItem *buffer, nsecs_t expectedPresent) { |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 863 | ATRACE_CALL(); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 864 | Mutex::Autolock _l(mMutex); |
Jamie Gennis | 5e5efde | 2012-08-28 17:18:50 -0700 | [diff] [blame] | 865 | |
| 866 | // Check that the consumer doesn't currently have the maximum number of |
| 867 | // buffers acquired. We allow the max buffer count to be exceeded by one |
| 868 | // buffer, so that the consumer can successfully set up the newly acquired |
| 869 | // buffer before releasing the old one. |
| 870 | int numAcquiredBuffers = 0; |
| 871 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 872 | if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) { |
| 873 | numAcquiredBuffers++; |
| 874 | } |
| 875 | } |
| 876 | if (numAcquiredBuffers >= mMaxAcquiredBufferCount+1) { |
| 877 | ST_LOGE("acquireBuffer: max acquired buffer count reached: %d (max=%d)", |
| 878 | numAcquiredBuffers, mMaxAcquiredBufferCount); |
| 879 | return INVALID_OPERATION; |
| 880 | } |
| 881 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 882 | // check if queue is empty |
| 883 | // In asynchronous mode the list is guaranteed to be one buffer |
| 884 | // deep, while in synchronous mode we use the oldest buffer. |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 885 | if (mQueue.empty()) { |
Daniel Lam | fbcda93 | 2012-04-09 22:51:52 -0700 | [diff] [blame] | 886 | return NO_BUFFER_AVAILABLE; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 887 | } |
| 888 | |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 889 | Fifo::iterator front(mQueue.begin()); |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 890 | |
Andy McFadden | 14fab7d | 2013-08-01 13:37:42 -0700 | [diff] [blame] | 891 | // If expectedPresent is specified, we may not want to return a buffer yet. |
| 892 | // If it's specified and there's more than one buffer queued, we may |
| 893 | // want to drop a buffer. |
| 894 | if (expectedPresent != 0) { |
| 895 | const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second |
| 896 | |
| 897 | // The "expectedPresent" argument indicates when the buffer is expected |
| 898 | // to be presented on-screen. If the buffer's desired-present time |
| 899 | // is earlier (less) than expectedPresent, meaning it'll be displayed |
| 900 | // on time or possibly late if we show it ASAP, we acquire and return |
| 901 | // it. If we don't want to display it until after the expectedPresent |
| 902 | // time, we return PRESENT_LATER without acquiring it. |
| 903 | // |
| 904 | // To be safe, we don't defer acquisition if expectedPresent is |
| 905 | // more than one second in the future beyond the desired present time |
| 906 | // (i.e. we'd be holding the buffer for a long time). |
| 907 | // |
| 908 | // NOTE: code assumes monotonic time values from the system clock are |
| 909 | // positive. |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 910 | |
| 911 | // Start by checking to see if we can drop frames. We skip this check |
| 912 | // if the timestamps are being auto-generated by Surface -- if the |
| 913 | // app isn't generating timestamps explicitly, they probably don't |
| 914 | // want frames to be discarded based on them. |
| 915 | while (mQueue.size() > 1 && !mQueue[0].mIsAutoTimestamp) { |
Andy McFadden | 14fab7d | 2013-08-01 13:37:42 -0700 | [diff] [blame] | 916 | // If entry[1] is timely, drop entry[0] (and repeat). We apply |
| 917 | // an additional criteria here: we only drop the earlier buffer if |
| 918 | // our desiredPresent falls within +/- 1 second of the expected |
| 919 | // present. Otherwise, bogus desiredPresent times (e.g. 0 or |
| 920 | // a small relative timestamp), which normally mean "ignore the |
| 921 | // timestamp and acquire immediately", would cause us to drop |
| 922 | // frames. |
| 923 | // |
| 924 | // We may want to add an additional criteria: don't drop the |
| 925 | // earlier buffer if entry[1]'s fence hasn't signaled yet. |
| 926 | // |
| 927 | // (Vector front is [0], back is [size()-1]) |
| 928 | const BufferItem& bi(mQueue[1]); |
| 929 | nsecs_t desiredPresent = bi.mTimestamp; |
| 930 | if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC || |
| 931 | desiredPresent > expectedPresent) { |
| 932 | // This buffer is set to display in the near future, or |
| 933 | // desiredPresent is garbage. Either way we don't want to |
| 934 | // drop the previous buffer just to get this on screen sooner. |
| 935 | ST_LOGV("pts nodrop: des=%lld expect=%lld (%lld) now=%lld", |
| 936 | desiredPresent, expectedPresent, desiredPresent - expectedPresent, |
| 937 | systemTime(CLOCK_MONOTONIC)); |
| 938 | break; |
| 939 | } |
| 940 | ST_LOGV("pts drop: queue1des=%lld expect=%lld size=%d", |
| 941 | desiredPresent, expectedPresent, mQueue.size()); |
| 942 | if (stillTracking(front)) { |
| 943 | // front buffer is still in mSlots, so mark the slot as free |
| 944 | mSlots[front->mBuf].mBufferState = BufferSlot::FREE; |
| 945 | } |
| 946 | mQueue.erase(front); |
| 947 | front = mQueue.begin(); |
| 948 | } |
| 949 | |
| 950 | // See if the front buffer is due. |
| 951 | nsecs_t desiredPresent = front->mTimestamp; |
| 952 | if (desiredPresent > expectedPresent && |
| 953 | desiredPresent < expectedPresent + MAX_REASONABLE_NSEC) { |
| 954 | ST_LOGV("pts defer: des=%lld expect=%lld (%lld) now=%lld", |
| 955 | desiredPresent, expectedPresent, desiredPresent - expectedPresent, |
| 956 | systemTime(CLOCK_MONOTONIC)); |
| 957 | return PRESENT_LATER; |
| 958 | } |
| 959 | |
| 960 | ST_LOGV("pts accept: des=%lld expect=%lld (%lld) now=%lld", |
| 961 | desiredPresent, expectedPresent, desiredPresent - expectedPresent, |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 962 | systemTime(CLOCK_MONOTONIC)); |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Andy McFadden | 14fab7d | 2013-08-01 13:37:42 -0700 | [diff] [blame] | 965 | int buf = front->mBuf; |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 966 | *buffer = *front; |
| 967 | ATRACE_BUFFER_INDEX(buf); |
| 968 | |
| 969 | ST_LOGV("acquireBuffer: acquiring { slot=%d/%llu, buffer=%p }", |
| 970 | front->mBuf, front->mFrameNumber, |
| 971 | front->mGraphicBuffer->handle); |
| 972 | // if front buffer still being tracked update slot state |
| 973 | if (stillTracking(front)) { |
| 974 | mSlots[buf].mAcquireCalled = true; |
| 975 | mSlots[buf].mNeedsCleanupOnRelease = false; |
| 976 | mSlots[buf].mBufferState = BufferSlot::ACQUIRED; |
| 977 | mSlots[buf].mFence = Fence::NO_FENCE; |
| 978 | } |
| 979 | |
| 980 | // If the buffer has previously been acquired by the consumer, set |
| 981 | // mGraphicBuffer to NULL to avoid unnecessarily remapping this |
| 982 | // buffer on the consumer side. |
| 983 | if (buffer->mAcquireCalled) { |
| 984 | buffer->mGraphicBuffer = NULL; |
| 985 | } |
| 986 | |
| 987 | mQueue.erase(front); |
| 988 | mDequeueCondition.broadcast(); |
| 989 | |
| 990 | ATRACE_INT(mConsumerName.string(), mQueue.size()); |
| 991 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 992 | return NO_ERROR; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 993 | } |
| 994 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 995 | status_t BufferQueue::releaseBuffer( |
| 996 | int buf, uint64_t frameNumber, EGLDisplay display, |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 997 | EGLSyncKHR eglFence, const sp<Fence>& fence) { |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 998 | ATRACE_CALL(); |
| 999 | ATRACE_BUFFER_INDEX(buf); |
| 1000 | |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 1001 | if (buf == INVALID_BUFFER_SLOT || fence == NULL) { |
| 1002 | return BAD_VALUE; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
Mathias Agopian | 207c1e2 | 2013-07-22 18:00:53 -0700 | [diff] [blame] | 1005 | Mutex::Autolock _l(mMutex); |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 1006 | |
| 1007 | // If the frame number has changed because buffer has been reallocated, |
| 1008 | // we can ignore this releaseBuffer for the old buffer. |
| 1009 | if (frameNumber != mSlots[buf].mFrameNumber) { |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 1010 | return STALE_BUFFER_SLOT; |
| 1011 | } |
Mathias Agopian | 207c1e2 | 2013-07-22 18:00:53 -0700 | [diff] [blame] | 1012 | |
| 1013 | |
| 1014 | // Internal state consistency checks: |
| 1015 | // Make sure this buffers hasn't been queued while we were owning it (acquired) |
| 1016 | Fifo::iterator front(mQueue.begin()); |
| 1017 | Fifo::const_iterator const end(mQueue.end()); |
| 1018 | while (front != end) { |
| 1019 | if (front->mBuf == buf) { |
| 1020 | LOG_ALWAYS_FATAL("[%s] received new buffer(#%lld) on slot #%d that has not yet been " |
| 1021 | "acquired", mConsumerName.string(), frameNumber, buf); |
| 1022 | break; // never reached |
| 1023 | } |
| 1024 | front++; |
| 1025 | } |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1026 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 1027 | // The buffer can now only be released if its in the acquired state |
| 1028 | if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) { |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 1029 | mSlots[buf].mEglDisplay = display; |
| 1030 | mSlots[buf].mEglFence = eglFence; |
| 1031 | mSlots[buf].mFence = fence; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1032 | mSlots[buf].mBufferState = BufferSlot::FREE; |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 1033 | } else if (mSlots[buf].mNeedsCleanupOnRelease) { |
| 1034 | ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState); |
| 1035 | mSlots[buf].mNeedsCleanupOnRelease = false; |
| 1036 | return STALE_BUFFER_SLOT; |
| 1037 | } else { |
| 1038 | ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState); |
| 1039 | return -EINVAL; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1040 | } |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1041 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 1042 | mDequeueCondition.broadcast(); |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 1043 | return NO_ERROR; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 1046 | status_t BufferQueue::consumerConnect(const sp<IConsumerListener>& consumerListener, |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 1047 | bool controlledByApp) { |
Jesse Hall | 8db9255 | 2013-08-29 16:03:50 -0700 | [diff] [blame] | 1048 | ST_LOGV("consumerConnect controlledByApp=%s", |
| 1049 | controlledByApp ? "true" : "false"); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1050 | Mutex::Autolock lock(mMutex); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 1051 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1052 | if (mAbandoned) { |
| 1053 | ST_LOGE("consumerConnect: BufferQueue has been abandoned!"); |
| 1054 | return NO_INIT; |
| 1055 | } |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 1056 | if (consumerListener == NULL) { |
| 1057 | ST_LOGE("consumerConnect: consumerListener may not be NULL"); |
| 1058 | return BAD_VALUE; |
| 1059 | } |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 1060 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1061 | mConsumerListener = consumerListener; |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 1062 | mConsumerControlledByApp = controlledByApp; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1063 | |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 1064 | return NO_ERROR; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | status_t BufferQueue::consumerDisconnect() { |
| 1068 | ST_LOGV("consumerDisconnect"); |
| 1069 | Mutex::Autolock lock(mMutex); |
| 1070 | |
| 1071 | if (mConsumerListener == NULL) { |
| 1072 | ST_LOGE("consumerDisconnect: No consumer is connected!"); |
| 1073 | return -EINVAL; |
| 1074 | } |
| 1075 | |
| 1076 | mAbandoned = true; |
| 1077 | mConsumerListener = NULL; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 1078 | mQueue.clear(); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1079 | freeAllBuffersLocked(); |
Dave Burke | 74ff8c2 | 2012-03-12 21:49:41 -0700 | [diff] [blame] | 1080 | mDequeueCondition.broadcast(); |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 1081 | return NO_ERROR; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1084 | status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) { |
| 1085 | ST_LOGV("getReleasedBuffers"); |
| 1086 | Mutex::Autolock lock(mMutex); |
| 1087 | |
| 1088 | if (mAbandoned) { |
| 1089 | ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!"); |
| 1090 | return NO_INIT; |
| 1091 | } |
| 1092 | |
| 1093 | uint32_t mask = 0; |
| 1094 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 1095 | if (!mSlots[i].mAcquireCalled) { |
| 1096 | mask |= 1 << i; |
| 1097 | } |
| 1098 | } |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 1099 | |
| 1100 | // Remove buffers in flight (on the queue) from the mask where acquire has |
| 1101 | // been called, as the consumer will not receive the buffer address, so |
| 1102 | // it should not free these slots. |
| 1103 | Fifo::iterator front(mQueue.begin()); |
| 1104 | while (front != mQueue.end()) { |
| 1105 | if (front->mAcquireCalled) |
| 1106 | mask &= ~(1 << front->mBuf); |
| 1107 | front++; |
| 1108 | } |
| 1109 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1110 | *slotMask = mask; |
| 1111 | |
| 1112 | ST_LOGV("getReleasedBuffers: returning mask %#x", mask); |
| 1113 | return NO_ERROR; |
| 1114 | } |
| 1115 | |
Mathias Agopian | 207c1e2 | 2013-07-22 18:00:53 -0700 | [diff] [blame] | 1116 | status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1117 | ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h); |
| 1118 | if (!w || !h) { |
| 1119 | ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)", |
| 1120 | w, h); |
| 1121 | return BAD_VALUE; |
| 1122 | } |
| 1123 | |
| 1124 | Mutex::Autolock lock(mMutex); |
| 1125 | mDefaultWidth = w; |
| 1126 | mDefaultHeight = h; |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 1127 | return NO_ERROR; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 1130 | status_t BufferQueue::setDefaultMaxBufferCount(int bufferCount) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 1131 | ATRACE_CALL(); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1132 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 1133 | return setDefaultMaxBufferCountLocked(bufferCount); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 1136 | status_t BufferQueue::disableAsyncBuffer() { |
| 1137 | ATRACE_CALL(); |
| 1138 | Mutex::Autolock lock(mMutex); |
| 1139 | if (mConsumerListener != NULL) { |
| 1140 | ST_LOGE("disableAsyncBuffer: consumer already connected!"); |
| 1141 | return INVALID_OPERATION; |
| 1142 | } |
| 1143 | mUseAsyncBuffer = false; |
| 1144 | return NO_ERROR; |
| 1145 | } |
| 1146 | |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 1147 | status_t BufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) { |
| 1148 | ATRACE_CALL(); |
| 1149 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | c68f2ec | 2012-08-30 18:36:22 -0700 | [diff] [blame] | 1150 | if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > MAX_MAX_ACQUIRED_BUFFERS) { |
| 1151 | ST_LOGE("setMaxAcquiredBufferCount: invalid count specified: %d", |
| 1152 | maxAcquiredBuffers); |
| 1153 | return BAD_VALUE; |
| 1154 | } |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 1155 | if (mConnectedApi != NO_CONNECTED_API) { |
| 1156 | return INVALID_OPERATION; |
| 1157 | } |
| 1158 | mMaxAcquiredBufferCount = maxAcquiredBuffers; |
Andy McFadden | 753e341 | 2013-04-04 17:09:03 -0700 | [diff] [blame] | 1159 | return NO_ERROR; |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 1162 | int BufferQueue::getMinUndequeuedBufferCount(bool async) const { |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 1163 | // if dequeueBuffer is allowed to error out, we don't have to |
| 1164 | // add an extra buffer. |
| 1165 | if (!mUseAsyncBuffer) |
| 1166 | return mMaxAcquiredBufferCount; |
| 1167 | |
| 1168 | // we're in async mode, or we want to prevent the app to |
| 1169 | // deadlock itself, we throw-in an extra buffer to guarantee it. |
| 1170 | if (mDequeueBufferCannotBlock || async) |
| 1171 | return mMaxAcquiredBufferCount+1; |
| 1172 | |
| 1173 | return mMaxAcquiredBufferCount; |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 1176 | int BufferQueue::getMinMaxBufferCountLocked(bool async) const { |
| 1177 | return getMinUndequeuedBufferCount(async) + 1; |
| 1178 | } |
| 1179 | |
| 1180 | int BufferQueue::getMaxBufferCountLocked(bool async) const { |
| 1181 | int minMaxBufferCount = getMinMaxBufferCountLocked(async); |
Jamie Gennis | e191e6c | 2012-08-24 20:26:34 -0700 | [diff] [blame] | 1182 | |
| 1183 | int maxBufferCount = mDefaultMaxBufferCount; |
| 1184 | if (maxBufferCount < minMaxBufferCount) { |
| 1185 | maxBufferCount = minMaxBufferCount; |
| 1186 | } |
| 1187 | if (mOverrideMaxBufferCount != 0) { |
| 1188 | assert(mOverrideMaxBufferCount >= minMaxBufferCount); |
| 1189 | maxBufferCount = mOverrideMaxBufferCount; |
| 1190 | } |
| 1191 | |
| 1192 | // Any buffers that are dequeued by the producer or sitting in the queue |
| 1193 | // waiting to be consumed need to have their slots preserved. Such |
| 1194 | // buffers will temporarily keep the max buffer count up until the slots |
| 1195 | // no longer need to be preserved. |
| 1196 | for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) { |
| 1197 | BufferSlot::BufferState state = mSlots[i].mBufferState; |
| 1198 | if (state == BufferSlot::QUEUED || state == BufferSlot::DEQUEUED) { |
| 1199 | maxBufferCount = i + 1; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | return maxBufferCount; |
| 1204 | } |
| 1205 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 1206 | bool BufferQueue::stillTracking(const BufferItem *item) const { |
| 1207 | const BufferSlot &slot = mSlots[item->mBuf]; |
| 1208 | |
| 1209 | ST_LOGV("stillTracking?: item: { slot=%d/%llu, buffer=%p }, " |
| 1210 | "slot: { slot=%d/%llu, buffer=%p }", |
| 1211 | item->mBuf, item->mFrameNumber, |
| 1212 | (item->mGraphicBuffer.get() ? item->mGraphicBuffer->handle : 0), |
| 1213 | item->mBuf, slot.mFrameNumber, |
| 1214 | (slot.mGraphicBuffer.get() ? slot.mGraphicBuffer->handle : 0)); |
| 1215 | |
| 1216 | // Compare item with its original buffer slot. We can check the slot |
| 1217 | // as the buffer would not be moved to a different slot by the producer. |
| 1218 | return (slot.mGraphicBuffer != NULL && |
| 1219 | item->mGraphicBuffer->handle == slot.mGraphicBuffer->handle); |
| 1220 | } |
| 1221 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1222 | BufferQueue::ProxyConsumerListener::ProxyConsumerListener( |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 1223 | const wp<ConsumerListener>& consumerListener): |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1224 | mConsumerListener(consumerListener) {} |
| 1225 | |
| 1226 | BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {} |
| 1227 | |
| 1228 | void BufferQueue::ProxyConsumerListener::onFrameAvailable() { |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 1229 | sp<ConsumerListener> listener(mConsumerListener.promote()); |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1230 | if (listener != NULL) { |
| 1231 | listener->onFrameAvailable(); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | void BufferQueue::ProxyConsumerListener::onBuffersReleased() { |
Mathias Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 1236 | sp<ConsumerListener> listener(mConsumerListener.promote()); |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1237 | if (listener != NULL) { |
| 1238 | listener->onBuffersReleased(); |
| 1239 | } |
| 1240 | } |
| 1241 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1242 | }; // namespace android |