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