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