blob: 9b6bc38ecd51477ec9487d688fa6699427837d7a [file] [log] [blame]
Daniel Lam6b091c52012-01-22 15:26:27 -08001/*
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 Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070019//#define LOG_NDEBUG 0
Daniel Lam6b091c52012-01-22 15:26:27 -080020
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 Agopian90ac7992012-02-25 18:48:35 -080028#include <gui/ISurfaceComposer.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080029#include <private/gui/ComposerService.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080030
31#include <utils/Log.h>
Daniel Lameae59d22012-01-22 15:26:27 -080032#include <gui/SurfaceTexture.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080033#include <utils/Trace.h>
Daniel Lam6b091c52012-01-22 15:26:27 -080034
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 Lameae59d22012-01-22 15:26:27 -080041
42// During refactoring, do not support dequeuing the current buffer
43#undef ALLOW_DEQUEUE_CURRENT_BUFFER
44
Daniel Lam6b091c52012-01-22 15:26:27 -080045#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 Lameae59d22012-01-22 15:26:27 -080053#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 Lam6b091c52012-01-22 15:26:27 -080058
Mathias Agopian546ed2d2012-03-01 22:11:25 -080059#define ATRACE_BUFFER_INDEX(index) \
Jamie Gennis695e3312012-04-16 20:34:58 -070060 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 Agopian546ed2d2012-03-01 22:11:25 -080066
Daniel Lam6b091c52012-01-22 15:26:27 -080067namespace android {
68
69// Get an ID that's unique within this process.
70static int32_t createProcessUniqueId() {
71 static volatile int32_t globalCounter = 0;
72 return android_atomic_inc(&globalCounter);
73}
74
Jamie Genniscd1806e2012-05-10 02:22:33 -070075static 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 Lamabe61bf2012-03-26 20:37:15 -070084BufferQueue::BufferQueue( bool allowSynchronousMode, int bufferCount ) :
Daniel Lam6b091c52012-01-22 15:26:27 -080085 mDefaultWidth(1),
86 mDefaultHeight(1),
87 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Daniel Lamabe61bf2012-03-26 20:37:15 -070088 mMinUndequeuedBuffers(bufferCount),
89 mMinAsyncBufferSlots(bufferCount + 1),
90 mMinSyncBufferSlots(bufferCount),
91 mBufferCount(mMinAsyncBufferSlots),
Daniel Lam6b091c52012-01-22 15:26:27 -080092 mClientBufferCount(0),
Daniel Lamabe61bf2012-03-26 20:37:15 -070093 mServerBufferCount(mMinAsyncBufferSlots),
Daniel Lam6b091c52012-01-22 15:26:27 -080094 mSynchronousMode(false),
95 mAllowSynchronousMode(allowSynchronousMode),
96 mConnectedApi(NO_CONNECTED_API),
97 mAbandoned(false),
Daniel Lameae59d22012-01-22 15:26:27 -080098 mFrameCounter(0),
Daniel Lamb2675792012-02-23 14:35:13 -080099 mBufferHasBeenQueued(false),
100 mDefaultBufferFormat(0),
101 mConsumerUsageBits(0),
102 mTransformHint(0)
Daniel Lam6b091c52012-01-22 15:26:27 -0800103{
104 // Choose a name using the PID and a process-unique ID.
Daniel Lameae59d22012-01-22 15:26:27 -0800105 mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
Daniel Lam6b091c52012-01-22 15:26:27 -0800106
107 ST_LOGV("BufferQueue");
108 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
109 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Daniel Lamabe61bf2012-03-26 20:37:15 -0700110 if (mGraphicBufferAlloc == 0) {
111 ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()");
112 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800113}
114
115BufferQueue::~BufferQueue() {
116 ST_LOGV("~BufferQueue");
117}
118
119status_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 Burke74ff8c22012-03-12 21:49:41 -0700132 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800133 } 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 Lamb2675792012-02-23 14:35:13 -0800149 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800150 }
151 return OK;
152}
153
Daniel Lameae59d22012-01-22 15:26:27 -0800154bool BufferQueue::isSynchronousMode() const {
155 Mutex::Autolock lock(mMutex);
156 return mSynchronousMode;
157}
158
159void BufferQueue::setConsumerName(const String8& name) {
160 Mutex::Autolock lock(mMutex);
161 mConsumerName = name;
162}
163
Daniel Lamb2675792012-02-23 14:35:13 -0800164status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) {
165 Mutex::Autolock lock(mMutex);
166 mDefaultBufferFormat = defaultFormat;
167 return OK;
168}
169
170status_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
171 Mutex::Autolock lock(mMutex);
172 mConsumerUsageBits = usage;
173 return OK;
174}
175
176status_t BufferQueue::setTransformHint(uint32_t hint) {
177 Mutex::Autolock lock(mMutex);
178 mTransformHint = hint;
179 return OK;
180}
181
Daniel Lam6b091c52012-01-22 15:26:27 -0800182status_t BufferQueue::setBufferCount(int bufferCount) {
183 ST_LOGV("setBufferCount: count=%d", bufferCount);
Daniel Lam6b091c52012-01-22 15:26:27 -0800184
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700185 sp<ConsumerListener> listener;
186 {
187 Mutex::Autolock lock(mMutex);
Daniel Lam6b091c52012-01-22 15:26:27 -0800188
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700189 if (mAbandoned) {
190 ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
191 return NO_INIT;
Daniel Lam6b091c52012-01-22 15:26:27 -0800192 }
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700193 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 Lamabe61bf2012-03-26 20:37:15 -0700207 mMinSyncBufferSlots : mMinAsyncBufferSlots;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700208 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 Lam6b091c52012-01-22 15:26:27 -0800234 }
235
Daniel Lam6b091c52012-01-22 15:26:27 -0800236 return OK;
237}
238
Daniel Lamb8560522012-01-30 15:51:27 -0800239int BufferQueue::query(int what, int* outValue)
240{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800241 ATRACE_CALL();
Daniel Lamb8560522012-01-30 15:51:27 -0800242 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 Lamabe61bf2012-03-26 20:37:15 -0700262 (mMinUndequeuedBuffers-1) : mMinUndequeuedBuffers;
Daniel Lamb8560522012-01-30 15:51:27 -0800263 break;
Mathias Agopian2488b202012-04-20 17:19:28 -0700264 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
265 value = (mQueue.size() >= 2);
266 break;
Daniel Lamb8560522012-01-30 15:51:27 -0800267 default:
268 return BAD_VALUE;
269 }
270 outValue[0] = value;
271 return NO_ERROR;
272}
273
Daniel Lam6b091c52012-01-22 15:26:27 -0800274status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800275 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800276 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
292status_t BufferQueue::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
293 uint32_t format, uint32_t usage) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800294 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800295 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 Lamb2675792012-02-23 14:35:13 -0800309 if (format == 0) {
310 format = mDefaultBufferFormat;
311 }
312 // turn on usage bits the consumer requested
313 usage |= mConsumerUsageBits;
314
Daniel Lam6b091c52012-01-22 15:26:27 -0800315 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 Lamabe61bf2012-03-26 20:37:15 -0700341 mMinSyncBufferSlots : mMinAsyncBufferSlots;
Daniel Lam6b091c52012-01-22 15:26:27 -0800342
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 Lameae59d22012-01-22 15:26:27 -0800361 mBufferHasBeenQueued = false;
Daniel Lam6b091c52012-01-22 15:26:27 -0800362 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 Lameae59d22012-01-22 15:26:27 -0800375 // 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 Lam6b091c52012-01-22 15:26:27 -0800381 } 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 Lamabe61bf2012-03-26 20:37:15 -0700408 // mMinUndequeuedBuffers check below.
Daniel Lameae59d22012-01-22 15:26:27 -0800409 if (mBufferHasBeenQueued) {
Daniel Lam6b091c52012-01-22 15:26:27 -0800410 // make sure the client is not trying to dequeue more buffers
411 // than allowed.
412 const int avail = mBufferCount - (dequeuedCount+1);
Daniel Lamabe61bf2012-03-26 20:37:15 -0700413 if (avail < (mMinUndequeuedBuffers-int(mSynchronousMode))) {
414 ST_LOGE("dequeueBuffer: mMinUndequeuedBuffers=%d exceeded "
Daniel Lam6b091c52012-01-22 15:26:27 -0800415 "(dequeued=%d)",
Daniel Lamabe61bf2012-03-26 20:37:15 -0700416 mMinUndequeuedBuffers-int(mSynchronousMode),
Daniel Lam6b091c52012-01-22 15:26:27 -0800417 dequeuedCount);
418 return -EBUSY;
419 }
420 }
421
Daniel Lamc2c1f2f2012-03-07 14:11:29 -0800422 // if no buffer is found, wait for a buffer to be released
423 tryAgain = found == INVALID_BUFFER_SLOT;
Daniel Lam6b091c52012-01-22 15:26:27 -0800424 if (tryAgain) {
425 mDequeueCondition.wait(mMutex);
426 }
427 }
428
Daniel Lam6b091c52012-01-22 15:26:27 -0800429
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 Agopian546ed2d2012-03-01 22:11:25 -0800439 ATRACE_BUFFER_INDEX(buf);
440
Daniel Lam6b091c52012-01-22 15:26:27 -0800441 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 Lam6b091c52012-01-22 15:26:27 -0800465 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 Lameae59d22012-01-22 15:26:27 -0800477
478 mSlots[buf].mAcquireCalled = false;
Daniel Lam6b091c52012-01-22 15:26:27 -0800479 mSlots[buf].mGraphicBuffer = graphicBuffer;
480 mSlots[buf].mRequestBufferCalled = false;
481 mSlots[buf].mFence = EGL_NO_SYNC_KHR;
Daniel Lameae59d22012-01-22 15:26:27 -0800482 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
483
Daniel Lam6b091c52012-01-22 15:26:27 -0800484 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 Lameae59d22012-01-22 15:26:27 -0800490 } // end lock scope
Daniel Lam6b091c52012-01-22 15:26:27 -0800491
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 Gennisb7a6b962012-05-13 19:41:35 -0700498 ST_LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
Daniel Lam6b091c52012-01-22 15:26:27 -0800499 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Jamie Gennisb7a6b962012-05-13 19:41:35 -0700500 ST_LOGE("dequeueBuffer: timeout waiting for fence");
Daniel Lam6b091c52012-01-22 15:26:27 -0800501 }
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
511status_t BufferQueue::setSynchronousMode(bool enabled) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800512 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800513 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 Burke74ff8c22012-03-12 21:49:41 -0700538 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800539 }
540 return err;
541}
542
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700543status_t BufferQueue::queueBuffer(int buf,
544 const QueueBufferInput& input, QueueBufferOutput* output) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800545 ATRACE_CALL();
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800546 ATRACE_BUFFER_INDEX(buf);
547
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700548 Rect crop;
549 uint32_t transform;
550 int scalingMode;
551 int64_t timestamp;
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700552
Jamie Gennisd72f2332012-05-07 13:50:11 -0700553 input.deflate(&timestamp, &crop, &scalingMode, &transform);
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700554
Jamie Genniscd1806e2012-05-10 02:22:33 -0700555 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 Lam6b091c52012-01-22 15:26:27 -0800559
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700560 sp<ConsumerListener> listener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800561
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 Lam6b091c52012-01-22 15:26:27 -0800576 } 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 Gennisd72f2332012-05-07 13:50:11 -0700582 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 Lam6b091c52012-01-22 15:26:27 -0800592 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 Gennisfa5b40e2012-03-15 14:01:24 -0700598 listener = mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800599 } 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 Gennisfa5b40e2012-03-15 14:01:24 -0700607 listener = mConsumerListener;
Daniel Lam6b091c52012-01-22 15:26:27 -0800608 } 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 Gennisefc7ab62012-04-17 19:36:18 -0700617 mSlots[buf].mTimestamp = timestamp;
618 mSlots[buf].mCrop = crop;
619 mSlots[buf].mTransform = transform;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700620
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700621 switch (scalingMode) {
622 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
623 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
Daniel Lam016c8cb2012-04-03 15:54:58 -0700624 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700625 break;
626 default:
627 ST_LOGE("unknown scaling mode: %d (ignoring)", scalingMode);
628 scalingMode = mSlots[buf].mScalingMode;
629 break;
630 }
631
Daniel Lam6b091c52012-01-22 15:26:27 -0800632 mSlots[buf].mBufferState = BufferSlot::QUEUED;
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700633 mSlots[buf].mScalingMode = scalingMode;
Daniel Lam6b091c52012-01-22 15:26:27 -0800634 mFrameCounter++;
635 mSlots[buf].mFrameNumber = mFrameCounter;
636
Daniel Lameae59d22012-01-22 15:26:27 -0800637 mBufferHasBeenQueued = true;
Dave Burke74ff8c22012-03-12 21:49:41 -0700638 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800639
Mathias Agopian2488b202012-04-20 17:19:28 -0700640 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
641 mQueue.size());
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800642
643 ATRACE_INT(mConsumerName.string(), mQueue.size());
Daniel Lam6b091c52012-01-22 15:26:27 -0800644 } // scope for the lock
645
646 // call back without lock held
647 if (listener != 0) {
648 listener->onFrameAvailable();
649 }
650 return OK;
651}
652
653void BufferQueue::cancelBuffer(int buf) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800654 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800655 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 Burke74ff8c22012-03-12 21:49:41 -0700674 mDequeueCondition.broadcast();
Daniel Lam6b091c52012-01-22 15:26:27 -0800675}
676
Mathias Agopian24202f52012-04-23 14:28:58 -0700677status_t BufferQueue::connect(int api, QueueBufferOutput* output) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800678 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800679 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 Gennisfa5b40e2012-03-15 14:01:24 -0700687 if (mConsumerListener == NULL) {
688 ST_LOGE("connect: BufferQueue has no consumer!");
689 return NO_INIT;
690 }
691
Daniel Lam6b091c52012-01-22 15:26:27 -0800692 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 Agopian2488b202012-04-20 17:19:28 -0700704 output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
705 mQueue.size());
Daniel Lam6b091c52012-01-22 15:26:27 -0800706 }
707 break;
708 default:
709 err = -EINVAL;
710 break;
711 }
Daniel Lameae59d22012-01-22 15:26:27 -0800712
713 mBufferHasBeenQueued = false;
714
Daniel Lam6b091c52012-01-22 15:26:27 -0800715 return err;
716}
717
718status_t BufferQueue::disconnect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800719 ATRACE_CALL();
Daniel Lam6b091c52012-01-22 15:26:27 -0800720 ST_LOGV("disconnect: api=%d", api);
Daniel Lam6b091c52012-01-22 15:26:27 -0800721
722 int err = NO_ERROR;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700723 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 Gennisfa5b40e2012-03-15 14:01:24 -0700742 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 Lam6b091c52012-01-22 15:26:27 -0800752 err = -EINVAL;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700753 break;
754 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800755 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700756
757 if (listener != NULL) {
758 listener->onBuffersReleased();
759 }
760
Daniel Lam6b091c52012-01-22 15:26:27 -0800761 return err;
762}
763
Daniel Lameae59d22012-01-22 15:26:27 -0800764void BufferQueue::dump(String8& result) const
765{
766 char buffer[1024];
767 BufferQueue::dump(result, "", buffer, 1024);
768}
769
770void BufferQueue::dump(String8& result, const char* prefix,
771 char* buffer, size_t SIZE) const
772{
773 Mutex::Autolock _l(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800774
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 Genniscd1806e2012-05-10 02:22:33 -0700809 "xform=0x%02x, time=%#llx, scale=%s",
Daniel Lameae59d22012-01-22 15:26:27 -0800810 prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i,
811 stateName(slot.mBufferState),
812 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
Jamie Genniscd1806e2012-05-10 02:22:33 -0700813 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp,
814 scalingModeName(slot.mScalingMode)
Daniel Lameae59d22012-01-22 15:26:27 -0800815 );
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 Lam6b091c52012-01-22 15:26:27 -0800830void BufferQueue::freeBufferLocked(int i) {
831 mSlots[i].mGraphicBuffer = 0;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700832 if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) {
833 mSlots[i].mNeedsCleanupOnRelease = true;
834 }
Daniel Lam6b091c52012-01-22 15:26:27 -0800835 mSlots[i].mBufferState = BufferSlot::FREE;
836 mSlots[i].mFrameNumber = 0;
Daniel Lameae59d22012-01-22 15:26:27 -0800837 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 Lam6b091c52012-01-22 15:26:27 -0800843 }
844}
845
846void BufferQueue::freeAllBuffersLocked() {
847 ALOGW_IF(!mQueue.isEmpty(),
848 "freeAllBuffersLocked called but mQueue is not empty");
Daniel Lameae59d22012-01-22 15:26:27 -0800849 mQueue.clear();
850 mBufferHasBeenQueued = false;
Daniel Lam6b091c52012-01-22 15:26:27 -0800851 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
852 freeBufferLocked(i);
853 }
854}
855
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700856status_t BufferQueue::acquireBuffer(BufferItem *buffer) {
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800857 ATRACE_CALL();
Daniel Lameae59d22012-01-22 15:26:27 -0800858 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 Agopian546ed2d2012-03-01 22:11:25 -0800866 ATRACE_BUFFER_INDEX(buf);
867
Daniel Lameae59d22012-01-22 15:26:27 -0800868 if (mSlots[buf].mAcquireCalled) {
869 buffer->mGraphicBuffer = NULL;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700870 } else {
Daniel Lameae59d22012-01-22 15:26:27 -0800871 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 Lam3fcee502012-03-02 10:17:34 -0800877 buffer->mTimestamp = mSlots[buf].mTimestamp;
Daniel Lameae59d22012-01-22 15:26:27 -0800878 buffer->mBuf = buf;
879 mSlots[buf].mAcquireCalled = true;
880
881 mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
882 mQueue.erase(front);
Dave Burke74ff8c22012-03-12 21:49:41 -0700883 mDequeueCondition.broadcast();
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800884
885 ATRACE_INT(mConsumerName.string(), mQueue.size());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700886 } else {
Daniel Lamfbcda932012-04-09 22:51:52 -0700887 return NO_BUFFER_AVAILABLE;
Daniel Lameae59d22012-01-22 15:26:27 -0800888 }
889
890 return OK;
891}
892
893status_t BufferQueue::releaseBuffer(int buf, EGLDisplay display,
894 EGLSyncKHR fence) {
Mathias Agopian546ed2d2012-03-01 22:11:25 -0800895 ATRACE_CALL();
896 ATRACE_BUFFER_INDEX(buf);
897
Daniel Lameae59d22012-01-22 15:26:27 -0800898 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 Lam9abe1eb2012-03-26 20:37:15 -0700907 // The buffer can now only be released if its in the acquired state
908 if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
Daniel Lameae59d22012-01-22 15:26:27 -0800909 mSlots[buf].mBufferState = BufferSlot::FREE;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700910 } 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 Lameae59d22012-01-22 15:26:27 -0800917 }
Daniel Lameae59d22012-01-22 15:26:27 -0800918
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700919 mDequeueCondition.broadcast();
Daniel Lameae59d22012-01-22 15:26:27 -0800920 return OK;
921}
922
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700923status_t BufferQueue::consumerConnect(const sp<ConsumerListener>& consumerListener) {
924 ST_LOGV("consumerConnect");
Daniel Lameae59d22012-01-22 15:26:27 -0800925 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800926
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700927 if (mAbandoned) {
928 ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
929 return NO_INIT;
930 }
Daniel Lamb2675792012-02-23 14:35:13 -0800931
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700932 mConsumerListener = consumerListener;
933
934 return OK;
935}
936
937status_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 Lamb2675792012-02-23 14:35:13 -0800948 mQueue.clear();
Daniel Lameae59d22012-01-22 15:26:27 -0800949 freeAllBuffersLocked();
Dave Burke74ff8c22012-03-12 21:49:41 -0700950 mDequeueCondition.broadcast();
Daniel Lameae59d22012-01-22 15:26:27 -0800951 return OK;
952}
953
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700954status_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 Lameae59d22012-01-22 15:26:27 -0800975status_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
990status_t BufferQueue::setBufferCountServer(int bufferCount) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800991 ATRACE_CALL();
Daniel Lameae59d22012-01-22 15:26:27 -0800992 Mutex::Autolock lock(mMutex);
993 return setBufferCountServerLocked(bufferCount);
994}
995
Daniel Lam6b091c52012-01-22 15:26:27 -0800996void 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 Lameae59d22012-01-22 15:26:27 -08001004 mBufferHasBeenQueued = false;
Daniel Lam6b091c52012-01-22 15:26:27 -08001005 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1006 if (i != head) {
1007 freeBufferLocked(i);
1008 }
1009 }
1010}
1011
1012status_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
1027status_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 Gennisfa5b40e2012-03-15 14:01:24 -07001039BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
1040 const wp<BufferQueue::ConsumerListener>& consumerListener):
1041 mConsumerListener(consumerListener) {}
1042
1043BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
1044
1045void BufferQueue::ProxyConsumerListener::onFrameAvailable() {
1046 sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1047 if (listener != NULL) {
1048 listener->onFrameAvailable();
1049 }
1050}
1051
1052void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
1053 sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
1054 if (listener != NULL) {
1055 listener->onBuffersReleased();
1056 }
1057}
1058
Daniel Lam6b091c52012-01-22 15:26:27 -08001059}; // namespace android