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