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