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