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> |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 32 | #include <gui/SurfaceTexture.h> |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 34 | |
| 35 | // This compile option causes SurfaceTexture to return the buffer that is currently |
| 36 | // attached to the GL texture from dequeueBuffer when no other buffers are |
| 37 | // available. It requires the drivers (Gralloc, GL, OMX IL, and Camera) to do |
| 38 | // implicit cross-process synchronization to prevent the buffer from being |
| 39 | // written to before the buffer has (a) been detached from the GL texture and |
| 40 | // (b) all GL reads from the buffer have completed. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 41 | |
| 42 | // During refactoring, do not support dequeuing the current buffer |
| 43 | #undef ALLOW_DEQUEUE_CURRENT_BUFFER |
| 44 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 45 | #ifdef ALLOW_DEQUEUE_CURRENT_BUFFER |
| 46 | #define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER true |
| 47 | #warning "ALLOW_DEQUEUE_CURRENT_BUFFER enabled" |
| 48 | #else |
| 49 | #define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER false |
| 50 | #endif |
| 51 | |
| 52 | // Macros for including the BufferQueue name in log messages |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 53 | #define ST_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 54 | #define ST_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 55 | #define ST_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 56 | #define ST_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 57 | #define ST_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 58 | |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 59 | #define ATRACE_BUFFER_INDEX(index) \ |
| 60 | char ___traceBuf[1024]; \ |
| 61 | snprintf(___traceBuf, 1024, "%s: %d", mConsumerName.string(), (index)); \ |
| 62 | android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); |
| 63 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 64 | namespace android { |
| 65 | |
| 66 | // Get an ID that's unique within this process. |
| 67 | static int32_t createProcessUniqueId() { |
| 68 | static volatile int32_t globalCounter = 0; |
| 69 | return android_atomic_inc(&globalCounter); |
| 70 | } |
| 71 | |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame^] | 72 | BufferQueue::BufferQueue( bool allowSynchronousMode, int bufferCount ) : |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 73 | mDefaultWidth(1), |
| 74 | mDefaultHeight(1), |
| 75 | mPixelFormat(PIXEL_FORMAT_RGBA_8888), |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame^] | 76 | mMinUndequeuedBuffers(bufferCount), |
| 77 | mMinAsyncBufferSlots(bufferCount + 1), |
| 78 | mMinSyncBufferSlots(bufferCount), |
| 79 | mBufferCount(mMinAsyncBufferSlots), |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 80 | mClientBufferCount(0), |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame^] | 81 | mServerBufferCount(mMinAsyncBufferSlots), |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 82 | mSynchronousMode(false), |
| 83 | mAllowSynchronousMode(allowSynchronousMode), |
| 84 | mConnectedApi(NO_CONNECTED_API), |
| 85 | mAbandoned(false), |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 86 | mFrameCounter(0), |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 87 | mBufferHasBeenQueued(false), |
| 88 | mDefaultBufferFormat(0), |
| 89 | mConsumerUsageBits(0), |
| 90 | mTransformHint(0) |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 91 | { |
| 92 | // Choose a name using the PID and a process-unique ID. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 93 | mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId()); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 94 | |
| 95 | ST_LOGV("BufferQueue"); |
| 96 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 97 | mGraphicBufferAlloc = composer->createGraphicBufferAlloc(); |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame^] | 98 | if (mGraphicBufferAlloc == 0) { |
| 99 | ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()"); |
| 100 | } |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | BufferQueue::~BufferQueue() { |
| 104 | ST_LOGV("~BufferQueue"); |
| 105 | } |
| 106 | |
| 107 | status_t BufferQueue::setBufferCountServerLocked(int bufferCount) { |
| 108 | if (bufferCount > NUM_BUFFER_SLOTS) |
| 109 | return BAD_VALUE; |
| 110 | |
| 111 | // special-case, nothing to do |
| 112 | if (bufferCount == mBufferCount) |
| 113 | return OK; |
| 114 | |
| 115 | if (!mClientBufferCount && |
| 116 | bufferCount >= mBufferCount) { |
| 117 | // easy, we just have more buffers |
| 118 | mBufferCount = bufferCount; |
| 119 | mServerBufferCount = bufferCount; |
Dave Burke | 74ff8c2 | 2012-03-12 21:49:41 -0700 | [diff] [blame] | 120 | mDequeueCondition.broadcast(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 121 | } else { |
| 122 | // we're here because we're either |
| 123 | // - reducing the number of available buffers |
| 124 | // - or there is a client-buffer-count in effect |
| 125 | |
| 126 | // less than 2 buffers is never allowed |
| 127 | if (bufferCount < 2) |
| 128 | return BAD_VALUE; |
| 129 | |
| 130 | // when there is non client-buffer-count in effect, the client is not |
| 131 | // allowed to dequeue more than one buffer at a time, |
| 132 | // so the next time they dequeue a buffer, we know that they don't |
| 133 | // own one. the actual resizing will happen during the next |
| 134 | // dequeueBuffer. |
| 135 | |
| 136 | mServerBufferCount = bufferCount; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 137 | mDequeueCondition.broadcast(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 138 | } |
| 139 | return OK; |
| 140 | } |
| 141 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 142 | bool BufferQueue::isSynchronousMode() const { |
| 143 | Mutex::Autolock lock(mMutex); |
| 144 | return mSynchronousMode; |
| 145 | } |
| 146 | |
| 147 | void BufferQueue::setConsumerName(const String8& name) { |
| 148 | Mutex::Autolock lock(mMutex); |
| 149 | mConsumerName = name; |
| 150 | } |
| 151 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 152 | status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) { |
| 153 | Mutex::Autolock lock(mMutex); |
| 154 | mDefaultBufferFormat = defaultFormat; |
| 155 | return OK; |
| 156 | } |
| 157 | |
| 158 | status_t BufferQueue::setConsumerUsageBits(uint32_t usage) { |
| 159 | Mutex::Autolock lock(mMutex); |
| 160 | mConsumerUsageBits = usage; |
| 161 | return OK; |
| 162 | } |
| 163 | |
| 164 | status_t BufferQueue::setTransformHint(uint32_t hint) { |
| 165 | Mutex::Autolock lock(mMutex); |
| 166 | mTransformHint = hint; |
| 167 | return OK; |
| 168 | } |
| 169 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 170 | status_t BufferQueue::setBufferCount(int bufferCount) { |
| 171 | ST_LOGV("setBufferCount: count=%d", bufferCount); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 172 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 173 | sp<ConsumerListener> listener; |
| 174 | { |
| 175 | Mutex::Autolock lock(mMutex); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 176 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 177 | if (mAbandoned) { |
| 178 | ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!"); |
| 179 | return NO_INIT; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 180 | } |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 181 | if (bufferCount > NUM_BUFFER_SLOTS) { |
| 182 | ST_LOGE("setBufferCount: bufferCount larger than slots available"); |
| 183 | return BAD_VALUE; |
| 184 | } |
| 185 | |
| 186 | // Error out if the user has dequeued buffers |
| 187 | for (int i=0 ; i<mBufferCount ; i++) { |
| 188 | if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) { |
| 189 | ST_LOGE("setBufferCount: client owns some buffers"); |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | const int minBufferSlots = mSynchronousMode ? |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame^] | 195 | mMinSyncBufferSlots : mMinAsyncBufferSlots; |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 196 | if (bufferCount == 0) { |
| 197 | mClientBufferCount = 0; |
| 198 | bufferCount = (mServerBufferCount >= minBufferSlots) ? |
| 199 | mServerBufferCount : minBufferSlots; |
| 200 | return setBufferCountServerLocked(bufferCount); |
| 201 | } |
| 202 | |
| 203 | if (bufferCount < minBufferSlots) { |
| 204 | ST_LOGE("setBufferCount: requested buffer count (%d) is less than " |
| 205 | "minimum (%d)", bufferCount, minBufferSlots); |
| 206 | return BAD_VALUE; |
| 207 | } |
| 208 | |
| 209 | // here we're guaranteed that the client doesn't have dequeued buffers |
| 210 | // and will release all of its buffer references. |
| 211 | freeAllBuffersLocked(); |
| 212 | mBufferCount = bufferCount; |
| 213 | mClientBufferCount = bufferCount; |
| 214 | mBufferHasBeenQueued = false; |
| 215 | mQueue.clear(); |
| 216 | mDequeueCondition.broadcast(); |
| 217 | listener = mConsumerListener; |
| 218 | } // scope for lock |
| 219 | |
| 220 | if (listener != NULL) { |
| 221 | listener->onBuffersReleased(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 224 | return OK; |
| 225 | } |
| 226 | |
Daniel Lam | b856052 | 2012-01-30 15:51:27 -0800 | [diff] [blame] | 227 | int BufferQueue::query(int what, int* outValue) |
| 228 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 229 | ATRACE_CALL(); |
Daniel Lam | b856052 | 2012-01-30 15:51:27 -0800 | [diff] [blame] | 230 | Mutex::Autolock lock(mMutex); |
| 231 | |
| 232 | if (mAbandoned) { |
| 233 | ST_LOGE("query: SurfaceTexture has been abandoned!"); |
| 234 | return NO_INIT; |
| 235 | } |
| 236 | |
| 237 | int value; |
| 238 | switch (what) { |
| 239 | case NATIVE_WINDOW_WIDTH: |
| 240 | value = mDefaultWidth; |
| 241 | break; |
| 242 | case NATIVE_WINDOW_HEIGHT: |
| 243 | value = mDefaultHeight; |
| 244 | break; |
| 245 | case NATIVE_WINDOW_FORMAT: |
| 246 | value = mPixelFormat; |
| 247 | break; |
| 248 | case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS: |
| 249 | value = mSynchronousMode ? |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame^] | 250 | (mMinUndequeuedBuffers-1) : mMinUndequeuedBuffers; |
Daniel Lam | b856052 | 2012-01-30 15:51:27 -0800 | [diff] [blame] | 251 | break; |
| 252 | default: |
| 253 | return BAD_VALUE; |
| 254 | } |
| 255 | outValue[0] = value; |
| 256 | return NO_ERROR; |
| 257 | } |
| 258 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 259 | status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 260 | ATRACE_CALL(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 261 | ST_LOGV("requestBuffer: slot=%d", slot); |
| 262 | Mutex::Autolock lock(mMutex); |
| 263 | if (mAbandoned) { |
| 264 | ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!"); |
| 265 | return NO_INIT; |
| 266 | } |
| 267 | if (slot < 0 || mBufferCount <= slot) { |
| 268 | ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d", |
| 269 | mBufferCount, slot); |
| 270 | return BAD_VALUE; |
| 271 | } |
| 272 | mSlots[slot].mRequestBufferCalled = true; |
| 273 | *buf = mSlots[slot].mGraphicBuffer; |
| 274 | return NO_ERROR; |
| 275 | } |
| 276 | |
| 277 | status_t BufferQueue::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h, |
| 278 | uint32_t format, uint32_t usage) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 279 | ATRACE_CALL(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 280 | ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage); |
| 281 | |
| 282 | if ((w && !h) || (!w && h)) { |
| 283 | ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h); |
| 284 | return BAD_VALUE; |
| 285 | } |
| 286 | |
| 287 | status_t returnFlags(OK); |
| 288 | EGLDisplay dpy = EGL_NO_DISPLAY; |
| 289 | EGLSyncKHR fence = EGL_NO_SYNC_KHR; |
| 290 | |
| 291 | { // Scope for the lock |
| 292 | Mutex::Autolock lock(mMutex); |
| 293 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 294 | if (format == 0) { |
| 295 | format = mDefaultBufferFormat; |
| 296 | } |
| 297 | // turn on usage bits the consumer requested |
| 298 | usage |= mConsumerUsageBits; |
| 299 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 300 | int found = -1; |
| 301 | int foundSync = -1; |
| 302 | int dequeuedCount = 0; |
| 303 | bool tryAgain = true; |
| 304 | while (tryAgain) { |
| 305 | if (mAbandoned) { |
| 306 | ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!"); |
| 307 | return NO_INIT; |
| 308 | } |
| 309 | |
| 310 | // We need to wait for the FIFO to drain if the number of buffer |
| 311 | // needs to change. |
| 312 | // |
| 313 | // The condition "number of buffers needs to change" is true if |
| 314 | // - the client doesn't care about how many buffers there are |
| 315 | // - AND the actual number of buffer is different from what was |
| 316 | // set in the last setBufferCountServer() |
| 317 | // - OR - |
| 318 | // setBufferCountServer() was set to a value incompatible with |
| 319 | // the synchronization mode (for instance because the sync mode |
| 320 | // changed since) |
| 321 | // |
| 322 | // As long as this condition is true AND the FIFO is not empty, we |
| 323 | // wait on mDequeueCondition. |
| 324 | |
| 325 | const int minBufferCountNeeded = mSynchronousMode ? |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame^] | 326 | mMinSyncBufferSlots : mMinAsyncBufferSlots; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 327 | |
| 328 | const bool numberOfBuffersNeedsToChange = !mClientBufferCount && |
| 329 | ((mServerBufferCount != mBufferCount) || |
| 330 | (mServerBufferCount < minBufferCountNeeded)); |
| 331 | |
| 332 | if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) { |
| 333 | // wait for the FIFO to drain |
| 334 | mDequeueCondition.wait(mMutex); |
| 335 | // NOTE: we continue here because we need to reevaluate our |
| 336 | // whole state (eg: we could be abandoned or disconnected) |
| 337 | continue; |
| 338 | } |
| 339 | |
| 340 | if (numberOfBuffersNeedsToChange) { |
| 341 | // here we're guaranteed that mQueue is empty |
| 342 | freeAllBuffersLocked(); |
| 343 | mBufferCount = mServerBufferCount; |
| 344 | if (mBufferCount < minBufferCountNeeded) |
| 345 | mBufferCount = minBufferCountNeeded; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 346 | mBufferHasBeenQueued = false; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 347 | returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS; |
| 348 | } |
| 349 | |
| 350 | // look for a free buffer to give to the client |
| 351 | found = INVALID_BUFFER_SLOT; |
| 352 | foundSync = INVALID_BUFFER_SLOT; |
| 353 | dequeuedCount = 0; |
| 354 | for (int i = 0; i < mBufferCount; i++) { |
| 355 | const int state = mSlots[i].mBufferState; |
| 356 | if (state == BufferSlot::DEQUEUED) { |
| 357 | dequeuedCount++; |
| 358 | } |
| 359 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 360 | // this logic used to be if (FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER) |
| 361 | // but dequeuing the current buffer is disabled. |
| 362 | if (false) { |
| 363 | // This functionality has been temporarily removed so |
| 364 | // BufferQueue and SurfaceTexture can be refactored into |
| 365 | // separate objects |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 366 | } else { |
| 367 | if (state == BufferSlot::FREE) { |
| 368 | /* We return the oldest of the free buffers to avoid |
| 369 | * stalling the producer if possible. This is because |
| 370 | * the consumer may still have pending reads of the |
| 371 | * buffers in flight. |
| 372 | */ |
| 373 | bool isOlder = mSlots[i].mFrameNumber < |
| 374 | mSlots[found].mFrameNumber; |
| 375 | if (found < 0 || isOlder) { |
| 376 | foundSync = i; |
| 377 | found = i; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // clients are not allowed to dequeue more than one buffer |
| 384 | // if they didn't set a buffer count. |
| 385 | if (!mClientBufferCount && dequeuedCount) { |
| 386 | ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without " |
| 387 | "setting the buffer count"); |
| 388 | return -EINVAL; |
| 389 | } |
| 390 | |
| 391 | // See whether a buffer has been queued since the last |
| 392 | // setBufferCount so we know whether to perform the |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame^] | 393 | // mMinUndequeuedBuffers check below. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 394 | if (mBufferHasBeenQueued) { |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 395 | // make sure the client is not trying to dequeue more buffers |
| 396 | // than allowed. |
| 397 | const int avail = mBufferCount - (dequeuedCount+1); |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame^] | 398 | if (avail < (mMinUndequeuedBuffers-int(mSynchronousMode))) { |
| 399 | ST_LOGE("dequeueBuffer: mMinUndequeuedBuffers=%d exceeded " |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 400 | "(dequeued=%d)", |
Daniel Lam | abe61bf | 2012-03-26 20:37:15 -0700 | [diff] [blame^] | 401 | mMinUndequeuedBuffers-int(mSynchronousMode), |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 402 | dequeuedCount); |
| 403 | return -EBUSY; |
| 404 | } |
| 405 | } |
| 406 | |
Daniel Lam | c2c1f2f | 2012-03-07 14:11:29 -0800 | [diff] [blame] | 407 | // if no buffer is found, wait for a buffer to be released |
| 408 | tryAgain = found == INVALID_BUFFER_SLOT; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 409 | if (tryAgain) { |
| 410 | mDequeueCondition.wait(mMutex); |
| 411 | } |
| 412 | } |
| 413 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 414 | |
| 415 | if (found == INVALID_BUFFER_SLOT) { |
| 416 | // This should not happen. |
| 417 | ST_LOGE("dequeueBuffer: no available buffer slots"); |
| 418 | return -EBUSY; |
| 419 | } |
| 420 | |
| 421 | const int buf = found; |
| 422 | *outBuf = found; |
| 423 | |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 424 | ATRACE_BUFFER_INDEX(buf); |
| 425 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 426 | const bool useDefaultSize = !w && !h; |
| 427 | if (useDefaultSize) { |
| 428 | // use the default size |
| 429 | w = mDefaultWidth; |
| 430 | h = mDefaultHeight; |
| 431 | } |
| 432 | |
| 433 | const bool updateFormat = (format != 0); |
| 434 | if (!updateFormat) { |
| 435 | // keep the current (or default) format |
| 436 | format = mPixelFormat; |
| 437 | } |
| 438 | |
| 439 | // buffer is now in DEQUEUED (but can also be current at the same time, |
| 440 | // if we're in synchronous mode) |
| 441 | mSlots[buf].mBufferState = BufferSlot::DEQUEUED; |
| 442 | |
| 443 | const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer); |
| 444 | if ((buffer == NULL) || |
| 445 | (uint32_t(buffer->width) != w) || |
| 446 | (uint32_t(buffer->height) != h) || |
| 447 | (uint32_t(buffer->format) != format) || |
| 448 | ((uint32_t(buffer->usage) & usage) != usage)) |
| 449 | { |
| 450 | usage |= GraphicBuffer::USAGE_HW_TEXTURE; |
| 451 | status_t error; |
| 452 | sp<GraphicBuffer> graphicBuffer( |
| 453 | mGraphicBufferAlloc->createGraphicBuffer( |
| 454 | w, h, format, usage, &error)); |
| 455 | if (graphicBuffer == 0) { |
| 456 | ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer " |
| 457 | "failed"); |
| 458 | return error; |
| 459 | } |
| 460 | if (updateFormat) { |
| 461 | mPixelFormat = format; |
| 462 | } |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 463 | |
| 464 | mSlots[buf].mAcquireCalled = false; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 465 | mSlots[buf].mGraphicBuffer = graphicBuffer; |
| 466 | mSlots[buf].mRequestBufferCalled = false; |
| 467 | mSlots[buf].mFence = EGL_NO_SYNC_KHR; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 468 | mSlots[buf].mEglDisplay = EGL_NO_DISPLAY; |
| 469 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 470 | returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION; |
| 471 | } |
| 472 | |
| 473 | dpy = mSlots[buf].mEglDisplay; |
| 474 | fence = mSlots[buf].mFence; |
| 475 | mSlots[buf].mFence = EGL_NO_SYNC_KHR; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 476 | } // end lock scope |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 477 | |
| 478 | if (fence != EGL_NO_SYNC_KHR) { |
| 479 | EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000); |
| 480 | // If something goes wrong, log the error, but return the buffer without |
| 481 | // synchronizing access to it. It's too late at this point to abort the |
| 482 | // dequeue operation. |
| 483 | if (result == EGL_FALSE) { |
| 484 | ALOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError()); |
| 485 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 486 | ALOGE("dequeueBuffer: timeout waiting for fence"); |
| 487 | } |
| 488 | eglDestroySyncKHR(dpy, fence); |
| 489 | } |
| 490 | |
| 491 | ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", *outBuf, |
| 492 | mSlots[*outBuf].mGraphicBuffer->handle, returnFlags); |
| 493 | |
| 494 | return returnFlags; |
| 495 | } |
| 496 | |
| 497 | status_t BufferQueue::setSynchronousMode(bool enabled) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 498 | ATRACE_CALL(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 499 | ST_LOGV("setSynchronousMode: enabled=%d", enabled); |
| 500 | Mutex::Autolock lock(mMutex); |
| 501 | |
| 502 | if (mAbandoned) { |
| 503 | ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!"); |
| 504 | return NO_INIT; |
| 505 | } |
| 506 | |
| 507 | status_t err = OK; |
| 508 | if (!mAllowSynchronousMode && enabled) |
| 509 | return err; |
| 510 | |
| 511 | if (!enabled) { |
| 512 | // going to asynchronous mode, drain the queue |
| 513 | err = drainQueueLocked(); |
| 514 | if (err != NO_ERROR) |
| 515 | return err; |
| 516 | } |
| 517 | |
| 518 | if (mSynchronousMode != enabled) { |
| 519 | // - if we're going to asynchronous mode, the queue is guaranteed to be |
| 520 | // empty here |
| 521 | // - if the client set the number of buffers, we're guaranteed that |
| 522 | // we have at least 3 (because we don't allow less) |
| 523 | mSynchronousMode = enabled; |
Dave Burke | 74ff8c2 | 2012-03-12 21:49:41 -0700 | [diff] [blame] | 524 | mDequeueCondition.broadcast(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 525 | } |
| 526 | return err; |
| 527 | } |
| 528 | |
| 529 | status_t BufferQueue::queueBuffer(int buf, int64_t timestamp, |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 530 | const Rect& crop, int scalingMode, uint32_t transform, |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 531 | uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 532 | ATRACE_CALL(); |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 533 | ATRACE_BUFFER_INDEX(buf); |
| 534 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 535 | ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp); |
| 536 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 537 | sp<ConsumerListener> listener; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 538 | |
| 539 | { // scope for the lock |
| 540 | Mutex::Autolock lock(mMutex); |
| 541 | if (mAbandoned) { |
| 542 | ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!"); |
| 543 | return NO_INIT; |
| 544 | } |
| 545 | if (buf < 0 || buf >= mBufferCount) { |
| 546 | ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d", |
| 547 | mBufferCount, buf); |
| 548 | return -EINVAL; |
| 549 | } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) { |
| 550 | ST_LOGE("queueBuffer: slot %d is not owned by the client " |
| 551 | "(state=%d)", buf, mSlots[buf].mBufferState); |
| 552 | return -EINVAL; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 553 | } else if (!mSlots[buf].mRequestBufferCalled) { |
| 554 | ST_LOGE("queueBuffer: slot %d was enqueued without requesting a " |
| 555 | "buffer", buf); |
| 556 | return -EINVAL; |
| 557 | } |
| 558 | |
| 559 | if (mSynchronousMode) { |
| 560 | // In synchronous mode we queue all buffers in a FIFO. |
| 561 | mQueue.push_back(buf); |
| 562 | |
| 563 | // Synchronous mode always signals that an additional frame should |
| 564 | // be consumed. |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 565 | listener = mConsumerListener; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 566 | } else { |
| 567 | // In asynchronous mode we only keep the most recent buffer. |
| 568 | if (mQueue.empty()) { |
| 569 | mQueue.push_back(buf); |
| 570 | |
| 571 | // Asynchronous mode only signals that a frame should be |
| 572 | // consumed if no previous frame was pending. If a frame were |
| 573 | // pending then the consumer would have already been notified. |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 574 | listener = mConsumerListener; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 575 | } else { |
| 576 | Fifo::iterator front(mQueue.begin()); |
| 577 | // buffer currently queued is freed |
| 578 | mSlots[*front].mBufferState = BufferSlot::FREE; |
| 579 | // and we record the new buffer index in the queued list |
| 580 | *front = buf; |
| 581 | } |
| 582 | } |
| 583 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 584 | switch (scalingMode) { |
| 585 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 586 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 587 | break; |
| 588 | default: |
| 589 | ST_LOGE("unknown scaling mode: %d (ignoring)", scalingMode); |
| 590 | scalingMode = mSlots[buf].mScalingMode; |
| 591 | break; |
| 592 | } |
| 593 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 594 | mSlots[buf].mBufferState = BufferSlot::QUEUED; |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 595 | mSlots[buf].mCrop = crop; |
| 596 | mSlots[buf].mTransform = transform; |
| 597 | mSlots[buf].mScalingMode = scalingMode; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 598 | mSlots[buf].mTimestamp = timestamp; |
| 599 | mFrameCounter++; |
| 600 | mSlots[buf].mFrameNumber = mFrameCounter; |
| 601 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 602 | mBufferHasBeenQueued = true; |
Dave Burke | 74ff8c2 | 2012-03-12 21:49:41 -0700 | [diff] [blame] | 603 | mDequeueCondition.broadcast(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 604 | |
| 605 | *outWidth = mDefaultWidth; |
| 606 | *outHeight = mDefaultHeight; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 607 | *outTransform = mTransformHint; |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 608 | |
| 609 | ATRACE_INT(mConsumerName.string(), mQueue.size()); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 610 | } // scope for the lock |
| 611 | |
| 612 | // call back without lock held |
| 613 | if (listener != 0) { |
| 614 | listener->onFrameAvailable(); |
| 615 | } |
| 616 | return OK; |
| 617 | } |
| 618 | |
| 619 | void BufferQueue::cancelBuffer(int buf) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 620 | ATRACE_CALL(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 621 | ST_LOGV("cancelBuffer: slot=%d", buf); |
| 622 | Mutex::Autolock lock(mMutex); |
| 623 | |
| 624 | if (mAbandoned) { |
| 625 | ST_LOGW("cancelBuffer: BufferQueue has been abandoned!"); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | if (buf < 0 || buf >= mBufferCount) { |
| 630 | ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d", |
| 631 | mBufferCount, buf); |
| 632 | return; |
| 633 | } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) { |
| 634 | ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)", |
| 635 | buf, mSlots[buf].mBufferState); |
| 636 | return; |
| 637 | } |
| 638 | mSlots[buf].mBufferState = BufferSlot::FREE; |
| 639 | mSlots[buf].mFrameNumber = 0; |
Dave Burke | 74ff8c2 | 2012-03-12 21:49:41 -0700 | [diff] [blame] | 640 | mDequeueCondition.broadcast(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 643 | status_t BufferQueue::connect(int api, |
| 644 | uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 645 | ATRACE_CALL(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 646 | ST_LOGV("connect: api=%d", api); |
| 647 | Mutex::Autolock lock(mMutex); |
| 648 | |
| 649 | if (mAbandoned) { |
| 650 | ST_LOGE("connect: BufferQueue has been abandoned!"); |
| 651 | return NO_INIT; |
| 652 | } |
| 653 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 654 | if (mConsumerListener == NULL) { |
| 655 | ST_LOGE("connect: BufferQueue has no consumer!"); |
| 656 | return NO_INIT; |
| 657 | } |
| 658 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 659 | int err = NO_ERROR; |
| 660 | switch (api) { |
| 661 | case NATIVE_WINDOW_API_EGL: |
| 662 | case NATIVE_WINDOW_API_CPU: |
| 663 | case NATIVE_WINDOW_API_MEDIA: |
| 664 | case NATIVE_WINDOW_API_CAMERA: |
| 665 | if (mConnectedApi != NO_CONNECTED_API) { |
| 666 | ST_LOGE("connect: already connected (cur=%d, req=%d)", |
| 667 | mConnectedApi, api); |
| 668 | err = -EINVAL; |
| 669 | } else { |
| 670 | mConnectedApi = api; |
| 671 | *outWidth = mDefaultWidth; |
| 672 | *outHeight = mDefaultHeight; |
| 673 | *outTransform = 0; |
| 674 | } |
| 675 | break; |
| 676 | default: |
| 677 | err = -EINVAL; |
| 678 | break; |
| 679 | } |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 680 | |
| 681 | mBufferHasBeenQueued = false; |
| 682 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 683 | return err; |
| 684 | } |
| 685 | |
| 686 | status_t BufferQueue::disconnect(int api) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 687 | ATRACE_CALL(); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 688 | ST_LOGV("disconnect: api=%d", api); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 689 | |
| 690 | int err = NO_ERROR; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 691 | sp<ConsumerListener> listener; |
| 692 | |
| 693 | { // Scope for the lock |
| 694 | Mutex::Autolock lock(mMutex); |
| 695 | |
| 696 | if (mAbandoned) { |
| 697 | // it is not really an error to disconnect after the surface |
| 698 | // has been abandoned, it should just be a no-op. |
| 699 | return NO_ERROR; |
| 700 | } |
| 701 | |
| 702 | switch (api) { |
| 703 | case NATIVE_WINDOW_API_EGL: |
| 704 | case NATIVE_WINDOW_API_CPU: |
| 705 | case NATIVE_WINDOW_API_MEDIA: |
| 706 | case NATIVE_WINDOW_API_CAMERA: |
| 707 | if (mConnectedApi == api) { |
| 708 | drainQueueAndFreeBuffersLocked(); |
| 709 | mConnectedApi = NO_CONNECTED_API; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 710 | mDequeueCondition.broadcast(); |
| 711 | listener = mConsumerListener; |
| 712 | } else { |
| 713 | ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)", |
| 714 | mConnectedApi, api); |
| 715 | err = -EINVAL; |
| 716 | } |
| 717 | break; |
| 718 | default: |
| 719 | ST_LOGE("disconnect: unknown API %d", api); |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 720 | err = -EINVAL; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 721 | break; |
| 722 | } |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 723 | } |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 724 | |
| 725 | if (listener != NULL) { |
| 726 | listener->onBuffersReleased(); |
| 727 | } |
| 728 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 729 | return err; |
| 730 | } |
| 731 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 732 | void BufferQueue::dump(String8& result) const |
| 733 | { |
| 734 | char buffer[1024]; |
| 735 | BufferQueue::dump(result, "", buffer, 1024); |
| 736 | } |
| 737 | |
| 738 | void BufferQueue::dump(String8& result, const char* prefix, |
| 739 | char* buffer, size_t SIZE) const |
| 740 | { |
| 741 | Mutex::Autolock _l(mMutex); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 742 | |
| 743 | String8 fifo; |
| 744 | int fifoSize = 0; |
| 745 | Fifo::const_iterator i(mQueue.begin()); |
| 746 | while (i != mQueue.end()) { |
| 747 | snprintf(buffer, SIZE, "%02d ", *i++); |
| 748 | fifoSize++; |
| 749 | fifo.append(buffer); |
| 750 | } |
| 751 | |
| 752 | snprintf(buffer, SIZE, |
| 753 | "%s-BufferQueue mBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], " |
| 754 | "mPixelFormat=%d, FIFO(%d)={%s}\n", |
| 755 | prefix, mBufferCount, mSynchronousMode, mDefaultWidth, |
| 756 | mDefaultHeight, mPixelFormat, fifoSize, fifo.string()); |
| 757 | result.append(buffer); |
| 758 | |
| 759 | |
| 760 | struct { |
| 761 | const char * operator()(int state) const { |
| 762 | switch (state) { |
| 763 | case BufferSlot::DEQUEUED: return "DEQUEUED"; |
| 764 | case BufferSlot::QUEUED: return "QUEUED"; |
| 765 | case BufferSlot::FREE: return "FREE"; |
| 766 | case BufferSlot::ACQUIRED: return "ACQUIRED"; |
| 767 | default: return "Unknown"; |
| 768 | } |
| 769 | } |
| 770 | } stateName; |
| 771 | |
| 772 | for (int i=0 ; i<mBufferCount ; i++) { |
| 773 | const BufferSlot& slot(mSlots[i]); |
| 774 | snprintf(buffer, SIZE, |
| 775 | "%s%s[%02d] " |
| 776 | "state=%-8s, crop=[%d,%d,%d,%d], " |
| 777 | "transform=0x%02x, timestamp=%lld", |
| 778 | prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i, |
| 779 | stateName(slot.mBufferState), |
| 780 | slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, |
| 781 | slot.mCrop.bottom, slot.mTransform, slot.mTimestamp |
| 782 | ); |
| 783 | result.append(buffer); |
| 784 | |
| 785 | const sp<GraphicBuffer>& buf(slot.mGraphicBuffer); |
| 786 | if (buf != NULL) { |
| 787 | snprintf(buffer, SIZE, |
| 788 | ", %p [%4ux%4u:%4u,%3X]", |
| 789 | buf->handle, buf->width, buf->height, buf->stride, |
| 790 | buf->format); |
| 791 | result.append(buffer); |
| 792 | } |
| 793 | result.append("\n"); |
| 794 | } |
| 795 | } |
| 796 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 797 | void BufferQueue::freeBufferLocked(int i) { |
| 798 | mSlots[i].mGraphicBuffer = 0; |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 799 | if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) { |
| 800 | mSlots[i].mNeedsCleanupOnRelease = true; |
| 801 | } |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 802 | mSlots[i].mBufferState = BufferSlot::FREE; |
| 803 | mSlots[i].mFrameNumber = 0; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 804 | mSlots[i].mAcquireCalled = false; |
| 805 | |
| 806 | // destroy fence as BufferQueue now takes ownership |
| 807 | if (mSlots[i].mFence != EGL_NO_SYNC_KHR) { |
| 808 | eglDestroySyncKHR(mSlots[i].mEglDisplay, mSlots[i].mFence); |
| 809 | mSlots[i].mFence = EGL_NO_SYNC_KHR; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
| 813 | void BufferQueue::freeAllBuffersLocked() { |
| 814 | ALOGW_IF(!mQueue.isEmpty(), |
| 815 | "freeAllBuffersLocked called but mQueue is not empty"); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 816 | mQueue.clear(); |
| 817 | mBufferHasBeenQueued = false; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 818 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 819 | freeBufferLocked(i); |
| 820 | } |
| 821 | } |
| 822 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 823 | status_t BufferQueue::acquireBuffer(BufferItem *buffer) { |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 824 | ATRACE_CALL(); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 825 | Mutex::Autolock _l(mMutex); |
| 826 | // check if queue is empty |
| 827 | // In asynchronous mode the list is guaranteed to be one buffer |
| 828 | // deep, while in synchronous mode we use the oldest buffer. |
| 829 | if (!mQueue.empty()) { |
| 830 | Fifo::iterator front(mQueue.begin()); |
| 831 | int buf = *front; |
| 832 | |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 833 | ATRACE_BUFFER_INDEX(buf); |
| 834 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 835 | if (mSlots[buf].mAcquireCalled) { |
| 836 | buffer->mGraphicBuffer = NULL; |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 837 | } else { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 838 | buffer->mGraphicBuffer = mSlots[buf].mGraphicBuffer; |
| 839 | } |
| 840 | buffer->mCrop = mSlots[buf].mCrop; |
| 841 | buffer->mTransform = mSlots[buf].mTransform; |
| 842 | buffer->mScalingMode = mSlots[buf].mScalingMode; |
| 843 | buffer->mFrameNumber = mSlots[buf].mFrameNumber; |
Daniel Lam | 3fcee50 | 2012-03-02 10:17:34 -0800 | [diff] [blame] | 844 | buffer->mTimestamp = mSlots[buf].mTimestamp; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 845 | buffer->mBuf = buf; |
| 846 | mSlots[buf].mAcquireCalled = true; |
| 847 | |
| 848 | mSlots[buf].mBufferState = BufferSlot::ACQUIRED; |
| 849 | mQueue.erase(front); |
Dave Burke | 74ff8c2 | 2012-03-12 21:49:41 -0700 | [diff] [blame] | 850 | mDequeueCondition.broadcast(); |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 851 | |
| 852 | ATRACE_INT(mConsumerName.string(), mQueue.size()); |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 853 | } else { |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 854 | // should be a better return code? |
| 855 | return -EINVAL; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | return OK; |
| 859 | } |
| 860 | |
| 861 | status_t BufferQueue::releaseBuffer(int buf, EGLDisplay display, |
| 862 | EGLSyncKHR fence) { |
Mathias Agopian | 546ed2d | 2012-03-01 22:11:25 -0800 | [diff] [blame] | 863 | ATRACE_CALL(); |
| 864 | ATRACE_BUFFER_INDEX(buf); |
| 865 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 866 | Mutex::Autolock _l(mMutex); |
| 867 | |
| 868 | if (buf == INVALID_BUFFER_SLOT) { |
| 869 | return -EINVAL; |
| 870 | } |
| 871 | |
| 872 | mSlots[buf].mEglDisplay = display; |
| 873 | mSlots[buf].mFence = fence; |
| 874 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 875 | // The buffer can now only be released if its in the acquired state |
| 876 | if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 877 | mSlots[buf].mBufferState = BufferSlot::FREE; |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 878 | } else if (mSlots[buf].mNeedsCleanupOnRelease) { |
| 879 | ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState); |
| 880 | mSlots[buf].mNeedsCleanupOnRelease = false; |
| 881 | return STALE_BUFFER_SLOT; |
| 882 | } else { |
| 883 | ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState); |
| 884 | return -EINVAL; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 885 | } |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 886 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 887 | mDequeueCondition.broadcast(); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 888 | return OK; |
| 889 | } |
| 890 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 891 | status_t BufferQueue::consumerConnect(const sp<ConsumerListener>& consumerListener) { |
| 892 | ST_LOGV("consumerConnect"); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 893 | Mutex::Autolock lock(mMutex); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 894 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 895 | if (mAbandoned) { |
| 896 | ST_LOGE("consumerConnect: BufferQueue has been abandoned!"); |
| 897 | return NO_INIT; |
| 898 | } |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 899 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 900 | mConsumerListener = consumerListener; |
| 901 | |
| 902 | return OK; |
| 903 | } |
| 904 | |
| 905 | status_t BufferQueue::consumerDisconnect() { |
| 906 | ST_LOGV("consumerDisconnect"); |
| 907 | Mutex::Autolock lock(mMutex); |
| 908 | |
| 909 | if (mConsumerListener == NULL) { |
| 910 | ST_LOGE("consumerDisconnect: No consumer is connected!"); |
| 911 | return -EINVAL; |
| 912 | } |
| 913 | |
| 914 | mAbandoned = true; |
| 915 | mConsumerListener = NULL; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 916 | mQueue.clear(); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 917 | freeAllBuffersLocked(); |
Dave Burke | 74ff8c2 | 2012-03-12 21:49:41 -0700 | [diff] [blame] | 918 | mDequeueCondition.broadcast(); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 919 | return OK; |
| 920 | } |
| 921 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 922 | status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) { |
| 923 | ST_LOGV("getReleasedBuffers"); |
| 924 | Mutex::Autolock lock(mMutex); |
| 925 | |
| 926 | if (mAbandoned) { |
| 927 | ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!"); |
| 928 | return NO_INIT; |
| 929 | } |
| 930 | |
| 931 | uint32_t mask = 0; |
| 932 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 933 | if (!mSlots[i].mAcquireCalled) { |
| 934 | mask |= 1 << i; |
| 935 | } |
| 936 | } |
| 937 | *slotMask = mask; |
| 938 | |
| 939 | ST_LOGV("getReleasedBuffers: returning mask %#x", mask); |
| 940 | return NO_ERROR; |
| 941 | } |
| 942 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 943 | status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) |
| 944 | { |
| 945 | ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h); |
| 946 | if (!w || !h) { |
| 947 | ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)", |
| 948 | w, h); |
| 949 | return BAD_VALUE; |
| 950 | } |
| 951 | |
| 952 | Mutex::Autolock lock(mMutex); |
| 953 | mDefaultWidth = w; |
| 954 | mDefaultHeight = h; |
| 955 | return OK; |
| 956 | } |
| 957 | |
| 958 | status_t BufferQueue::setBufferCountServer(int bufferCount) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 959 | ATRACE_CALL(); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 960 | Mutex::Autolock lock(mMutex); |
| 961 | return setBufferCountServerLocked(bufferCount); |
| 962 | } |
| 963 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 964 | void BufferQueue::freeAllBuffersExceptHeadLocked() { |
| 965 | ALOGW_IF(!mQueue.isEmpty(), |
| 966 | "freeAllBuffersExceptCurrentLocked called but mQueue is not empty"); |
| 967 | int head = -1; |
| 968 | if (!mQueue.empty()) { |
| 969 | Fifo::iterator front(mQueue.begin()); |
| 970 | head = *front; |
| 971 | } |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 972 | mBufferHasBeenQueued = false; |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 973 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 974 | if (i != head) { |
| 975 | freeBufferLocked(i); |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | status_t BufferQueue::drainQueueLocked() { |
| 981 | while (mSynchronousMode && !mQueue.isEmpty()) { |
| 982 | mDequeueCondition.wait(mMutex); |
| 983 | if (mAbandoned) { |
| 984 | ST_LOGE("drainQueueLocked: BufferQueue has been abandoned!"); |
| 985 | return NO_INIT; |
| 986 | } |
| 987 | if (mConnectedApi == NO_CONNECTED_API) { |
| 988 | ST_LOGE("drainQueueLocked: BufferQueue is not connected!"); |
| 989 | return NO_INIT; |
| 990 | } |
| 991 | } |
| 992 | return NO_ERROR; |
| 993 | } |
| 994 | |
| 995 | status_t BufferQueue::drainQueueAndFreeBuffersLocked() { |
| 996 | status_t err = drainQueueLocked(); |
| 997 | if (err == NO_ERROR) { |
| 998 | if (mSynchronousMode) { |
| 999 | freeAllBuffersLocked(); |
| 1000 | } else { |
| 1001 | freeAllBuffersExceptHeadLocked(); |
| 1002 | } |
| 1003 | } |
| 1004 | return err; |
| 1005 | } |
| 1006 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1007 | BufferQueue::ProxyConsumerListener::ProxyConsumerListener( |
| 1008 | const wp<BufferQueue::ConsumerListener>& consumerListener): |
| 1009 | mConsumerListener(consumerListener) {} |
| 1010 | |
| 1011 | BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {} |
| 1012 | |
| 1013 | void BufferQueue::ProxyConsumerListener::onFrameAvailable() { |
| 1014 | sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote()); |
| 1015 | if (listener != NULL) { |
| 1016 | listener->onFrameAvailable(); |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | void BufferQueue::ProxyConsumerListener::onBuffersReleased() { |
| 1021 | sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote()); |
| 1022 | if (listener != NULL) { |
| 1023 | listener->onBuffersReleased(); |
| 1024 | } |
| 1025 | } |
| 1026 | |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1027 | }; // namespace android |