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