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