2012-01-22 23:26:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "BufferQueue"
|
2012-02-24 03:27:23 +00:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
|
2012-03-15 21:01:24 +00:00
|
|
|
//#define LOG_NDEBUG 0
|
2012-01-22 23:26:27 +00:00
|
|
|
|
|
|
|
#define GL_GLEXT_PROTOTYPES
|
|
|
|
#define EGL_EGLEXT_PROTOTYPES
|
|
|
|
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <EGL/eglext.h>
|
|
|
|
|
|
|
|
#include <gui/BufferQueue.h>
|
2013-08-01 03:09:53 +00:00
|
|
|
#include <gui/IConsumerListener.h>
|
2012-02-26 02:48:35 +00:00
|
|
|
#include <gui/ISurfaceComposer.h>
|
2012-01-22 23:26:27 +00:00
|
|
|
#include <private/gui/ComposerService.h>
|
|
|
|
|
|
|
|
#include <utils/Log.h>
|
2012-02-24 03:27:23 +00:00
|
|
|
#include <utils/Trace.h>
|
2013-07-19 05:10:56 +00:00
|
|
|
#include <utils/CallStack.h>
|
2012-01-22 23:26:27 +00:00
|
|
|
|
|
|
|
// Macros for including the BufferQueue name in log messages
|
2012-01-22 23:26:27 +00:00
|
|
|
#define ST_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
|
|
|
|
#define ST_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
|
|
|
|
#define ST_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
|
|
|
|
#define ST_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
|
|
|
|
#define ST_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__)
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2012-03-02 06:11:25 +00:00
|
|
|
#define ATRACE_BUFFER_INDEX(index) \
|
2012-04-17 03:34:58 +00:00
|
|
|
if (ATRACE_ENABLED()) { \
|
|
|
|
char ___traceBuf[1024]; \
|
|
|
|
snprintf(___traceBuf, 1024, "%s: %d", mConsumerName.string(), \
|
|
|
|
(index)); \
|
|
|
|
android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \
|
|
|
|
}
|
2012-03-02 06:11:25 +00:00
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
namespace android {
|
|
|
|
|
|
|
|
// Get an ID that's unique within this process.
|
|
|
|
static int32_t createProcessUniqueId() {
|
|
|
|
static volatile int32_t globalCounter = 0;
|
|
|
|
return android_atomic_inc(&globalCounter);
|
|
|
|
}
|
|
|
|
|
2012-05-10 09:22:33 +00:00
|
|
|
static const char* scalingModeName(int scalingMode) {
|
|
|
|
switch (scalingMode) {
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE";
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW";
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:56:09 +00:00
|
|
|
BufferQueue::BufferQueue(const sp<IGraphicBufferAlloc>& allocator) :
|
2012-01-22 23:26:27 +00:00
|
|
|
mDefaultWidth(1),
|
|
|
|
mDefaultHeight(1),
|
2012-08-28 01:48:37 +00:00
|
|
|
mMaxAcquiredBufferCount(1),
|
|
|
|
mDefaultMaxBufferCount(2),
|
2012-08-25 00:25:13 +00:00
|
|
|
mOverrideMaxBufferCount(0),
|
2013-07-17 05:56:09 +00:00
|
|
|
mConsumerControlledByApp(false),
|
|
|
|
mDequeueBufferCannotBlock(false),
|
2013-07-24 00:28:53 +00:00
|
|
|
mUseAsyncBuffer(true),
|
2012-01-22 23:26:27 +00:00
|
|
|
mConnectedApi(NO_CONNECTED_API),
|
|
|
|
mAbandoned(false),
|
2012-01-22 23:26:27 +00:00
|
|
|
mFrameCounter(0),
|
2012-02-23 22:35:13 +00:00
|
|
|
mBufferHasBeenQueued(false),
|
2012-08-03 03:11:05 +00:00
|
|
|
mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888),
|
2012-02-23 22:35:13 +00:00
|
|
|
mConsumerUsageBits(0),
|
|
|
|
mTransformHint(0)
|
2012-01-22 23:26:27 +00:00
|
|
|
{
|
|
|
|
// Choose a name using the PID and a process-unique ID.
|
2012-01-22 23:26:27 +00:00
|
|
|
mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
|
2012-01-22 23:26:27 +00:00
|
|
|
|
|
|
|
ST_LOGV("BufferQueue");
|
2012-06-08 00:52:54 +00:00
|
|
|
if (allocator == NULL) {
|
|
|
|
sp<ISurfaceComposer> composer(ComposerService::getComposerService());
|
|
|
|
mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
|
|
|
|
if (mGraphicBufferAlloc == 0) {
|
|
|
|
ST_LOGE("createGraphicBufferAlloc() failed in BufferQueue()");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mGraphicBufferAlloc = allocator;
|
2012-03-27 03:37:15 +00:00
|
|
|
}
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BufferQueue::~BufferQueue() {
|
|
|
|
ST_LOGV("~BufferQueue");
|
|
|
|
}
|
|
|
|
|
2012-08-25 00:25:13 +00:00
|
|
|
status_t BufferQueue::setDefaultMaxBufferCountLocked(int count) {
|
2013-07-24 00:28:53 +00:00
|
|
|
const int minBufferCount = mUseAsyncBuffer ? 2 : 1;
|
|
|
|
if (count < minBufferCount || count > NUM_BUFFER_SLOTS)
|
2012-01-22 23:26:27 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
|
2012-08-25 00:25:13 +00:00
|
|
|
mDefaultMaxBufferCount = count;
|
2012-08-25 03:26:34 +00:00
|
|
|
mDequeueCondition.broadcast();
|
2012-08-03 03:11:05 +00:00
|
|
|
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
void BufferQueue::setConsumerName(const String8& name) {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mConsumerName = name;
|
|
|
|
}
|
|
|
|
|
2012-02-23 22:35:13 +00:00
|
|
|
status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mDefaultBufferFormat = defaultFormat;
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-02-23 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mConsumerUsageBits = usage;
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-02-23 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t BufferQueue::setTransformHint(uint32_t hint) {
|
2012-09-14 23:10:11 +00:00
|
|
|
ST_LOGV("setTransformHint: %02x", hint);
|
2012-02-23 22:35:13 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mTransformHint = hint;
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-02-23 22:35:13 +00:00
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
status_t BufferQueue::setBufferCount(int bufferCount) {
|
|
|
|
ST_LOGV("setBufferCount: count=%d", bufferCount);
|
|
|
|
|
2013-08-01 03:09:53 +00:00
|
|
|
sp<IConsumerListener> listener;
|
2012-03-27 03:37:15 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2012-03-27 03:37:15 +00:00
|
|
|
if (mAbandoned) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("setBufferCount: BufferQueue has been abandoned!");
|
2012-03-27 03:37:15 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
if (bufferCount > NUM_BUFFER_SLOTS) {
|
2013-04-05 00:09:03 +00:00
|
|
|
ST_LOGE("setBufferCount: bufferCount too large (max %d)",
|
|
|
|
NUM_BUFFER_SLOTS);
|
2012-03-27 03:37:15 +00:00
|
|
|
return BAD_VALUE;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 03:37:15 +00:00
|
|
|
// Error out if the user has dequeued buffers
|
2013-07-19 05:10:56 +00:00
|
|
|
for (int i=0 ; i<NUM_BUFFER_SLOTS; i++) {
|
2012-03-27 03:37:15 +00:00
|
|
|
if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
|
|
|
|
ST_LOGE("setBufferCount: client owns some buffers");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2012-03-27 03:37:15 +00:00
|
|
|
if (bufferCount == 0) {
|
2012-08-25 00:25:13 +00:00
|
|
|
mOverrideMaxBufferCount = 0;
|
2012-08-25 03:26:34 +00:00
|
|
|
mDequeueCondition.broadcast();
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-03-27 03:37:15 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 05:10:56 +00:00
|
|
|
// fine to assume async to false before we're setting the buffer count
|
|
|
|
const int minBufferSlots = getMinMaxBufferCountLocked(false);
|
2012-03-27 03:37:15 +00:00
|
|
|
if (bufferCount < minBufferSlots) {
|
|
|
|
ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
|
|
|
|
"minimum (%d)", bufferCount, minBufferSlots);
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// here we're guaranteed that the client doesn't have dequeued buffers
|
2013-05-06 23:23:07 +00:00
|
|
|
// and will release all of its buffer references. We don't clear the
|
|
|
|
// queue, however, so currently queued buffers still get displayed.
|
2012-03-27 03:37:15 +00:00
|
|
|
freeAllBuffersLocked();
|
2012-08-25 00:25:13 +00:00
|
|
|
mOverrideMaxBufferCount = bufferCount;
|
2012-03-27 03:37:15 +00:00
|
|
|
mDequeueCondition.broadcast();
|
|
|
|
listener = mConsumerListener;
|
|
|
|
} // scope for lock
|
|
|
|
|
|
|
|
if (listener != NULL) {
|
|
|
|
listener->onBuffersReleased();
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2012-01-30 23:51:27 +00:00
|
|
|
int BufferQueue::query(int what, int* outValue)
|
|
|
|
{
|
2012-02-24 03:27:23 +00:00
|
|
|
ATRACE_CALL();
|
2012-01-30 23:51:27 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("query: BufferQueue has been abandoned!");
|
2012-01-30 23:51:27 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
int value;
|
|
|
|
switch (what) {
|
|
|
|
case NATIVE_WINDOW_WIDTH:
|
|
|
|
value = mDefaultWidth;
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_HEIGHT:
|
|
|
|
value = mDefaultHeight;
|
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_FORMAT:
|
2012-08-03 03:11:05 +00:00
|
|
|
value = mDefaultBufferFormat;
|
2012-01-30 23:51:27 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
|
2013-07-19 05:10:56 +00:00
|
|
|
value = getMinUndequeuedBufferCount(false);
|
2012-01-30 23:51:27 +00:00
|
|
|
break;
|
2012-04-21 00:19:28 +00:00
|
|
|
case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND:
|
|
|
|
value = (mQueue.size() >= 2);
|
|
|
|
break;
|
2013-07-30 21:05:02 +00:00
|
|
|
case NATIVE_WINDOW_CONSUMER_USAGE_BITS:
|
|
|
|
value = mConsumerUsageBits;
|
|
|
|
break;
|
2012-01-30 23:51:27 +00:00
|
|
|
default:
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
outValue[0] = value;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
|
2012-02-24 03:27:23 +00:00
|
|
|
ATRACE_CALL();
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGV("requestBuffer: slot=%d", slot);
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
if (mAbandoned) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("requestBuffer: BufferQueue has been abandoned!");
|
2012-01-22 23:26:27 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
2013-07-19 05:10:56 +00:00
|
|
|
if (slot < 0 || slot >= NUM_BUFFER_SLOTS) {
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
|
2013-07-19 05:10:56 +00:00
|
|
|
NUM_BUFFER_SLOTS, slot);
|
2012-08-25 03:26:34 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
} else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
|
|
|
|
ST_LOGE("requestBuffer: slot %d is not owned by the client (state=%d)",
|
|
|
|
slot, mSlots[slot].mBufferState);
|
2012-01-22 23:26:27 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
mSlots[slot].mRequestBufferCalled = true;
|
|
|
|
*buf = mSlots[slot].mGraphicBuffer;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-07-19 05:10:56 +00:00
|
|
|
status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>* outFence, bool async,
|
2012-06-14 22:26:33 +00:00
|
|
|
uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
|
2012-02-24 03:27:23 +00:00
|
|
|
ATRACE_CALL();
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
|
|
|
|
|
|
|
|
if ((w && !h) || (!w && h)) {
|
|
|
|
ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t returnFlags(OK);
|
|
|
|
EGLDisplay dpy = EGL_NO_DISPLAY;
|
2012-06-28 19:52:05 +00:00
|
|
|
EGLSyncKHR eglFence = EGL_NO_SYNC_KHR;
|
2012-01-22 23:26:27 +00:00
|
|
|
|
|
|
|
{ // Scope for the lock
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
2012-02-23 22:35:13 +00:00
|
|
|
if (format == 0) {
|
|
|
|
format = mDefaultBufferFormat;
|
|
|
|
}
|
|
|
|
// turn on usage bits the consumer requested
|
|
|
|
usage |= mConsumerUsageBits;
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
int found = -1;
|
|
|
|
bool tryAgain = true;
|
|
|
|
while (tryAgain) {
|
|
|
|
if (mAbandoned) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
|
2012-01-22 23:26:27 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
2013-07-19 05:10:56 +00:00
|
|
|
const int maxBufferCount = getMaxBufferCountLocked(async);
|
|
|
|
if (async && mOverrideMaxBufferCount) {
|
|
|
|
// FIXME: some drivers are manually setting the buffer-count (which they
|
|
|
|
// shouldn't), so we do this extra test here to handle that case.
|
|
|
|
// This is TEMPORARY, until we get this fixed.
|
|
|
|
if (mOverrideMaxBufferCount < maxBufferCount) {
|
|
|
|
ST_LOGE("dequeueBuffer: async mode is invalid with buffercount override");
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
}
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2012-08-25 03:26:34 +00:00
|
|
|
// Free up any buffers that are in slots beyond the max buffer
|
|
|
|
// count.
|
|
|
|
for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
|
|
|
|
assert(mSlots[i].mBufferState == BufferSlot::FREE);
|
|
|
|
if (mSlots[i].mGraphicBuffer != NULL) {
|
|
|
|
freeBufferLocked(i);
|
2012-12-18 17:49:45 +00:00
|
|
|
returnFlags |= IGraphicBufferProducer::RELEASE_ALL_BUFFERS;
|
2012-08-25 03:26:34 +00:00
|
|
|
}
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// look for a free buffer to give to the client
|
|
|
|
found = INVALID_BUFFER_SLOT;
|
2013-07-24 04:50:20 +00:00
|
|
|
int dequeuedCount = 0;
|
|
|
|
int acquiredCount = 0;
|
2012-08-25 03:26:34 +00:00
|
|
|
for (int i = 0; i < maxBufferCount; i++) {
|
2012-01-22 23:26:27 +00:00
|
|
|
const int state = mSlots[i].mBufferState;
|
2013-07-24 04:50:20 +00:00
|
|
|
switch (state) {
|
|
|
|
case BufferSlot::DEQUEUED:
|
|
|
|
dequeuedCount++;
|
|
|
|
break;
|
|
|
|
case BufferSlot::ACQUIRED:
|
|
|
|
acquiredCount++;
|
|
|
|
break;
|
|
|
|
case BufferSlot::FREE:
|
|
|
|
/* We return the oldest of the free buffers to avoid
|
|
|
|
* stalling the producer if possible. This is because
|
|
|
|
* the consumer may still have pending reads of the
|
|
|
|
* buffers in flight.
|
|
|
|
*/
|
|
|
|
if ((found < 0) ||
|
|
|
|
mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
|
|
|
|
found = i;
|
|
|
|
}
|
|
|
|
break;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clients are not allowed to dequeue more than one buffer
|
|
|
|
// if they didn't set a buffer count.
|
2012-08-25 00:25:13 +00:00
|
|
|
if (!mOverrideMaxBufferCount && dequeuedCount) {
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
|
|
|
|
"setting the buffer count");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See whether a buffer has been queued since the last
|
2012-08-28 01:48:37 +00:00
|
|
|
// setBufferCount so we know whether to perform the min undequeued
|
|
|
|
// buffers check below.
|
2012-01-22 23:26:27 +00:00
|
|
|
if (mBufferHasBeenQueued) {
|
2012-01-22 23:26:27 +00:00
|
|
|
// make sure the client is not trying to dequeue more buffers
|
|
|
|
// than allowed.
|
2012-08-28 01:48:37 +00:00
|
|
|
const int newUndequeuedCount = maxBufferCount - (dequeuedCount+1);
|
2013-07-19 05:10:56 +00:00
|
|
|
const int minUndequeuedCount = getMinUndequeuedBufferCount(async);
|
2012-08-28 01:48:37 +00:00
|
|
|
if (newUndequeuedCount < minUndequeuedCount) {
|
|
|
|
ST_LOGE("dequeueBuffer: min undequeued buffer count (%d) "
|
|
|
|
"exceeded (dequeued=%d undequeudCount=%d)",
|
|
|
|
minUndequeuedCount, dequeuedCount,
|
|
|
|
newUndequeuedCount);
|
2012-01-22 23:26:27 +00:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-25 03:26:34 +00:00
|
|
|
// If no buffer is found, wait for a buffer to be released or for
|
|
|
|
// the max buffer count to change.
|
2012-03-07 22:11:29 +00:00
|
|
|
tryAgain = found == INVALID_BUFFER_SLOT;
|
2012-01-22 23:26:27 +00:00
|
|
|
if (tryAgain) {
|
2013-07-24 04:50:20 +00:00
|
|
|
// return an error if we're in "cannot block" mode (producer and consumer
|
|
|
|
// are controlled by the application) -- however, the consumer is allowed
|
|
|
|
// to acquire briefly an extra buffer (which could cause us to have to wait here)
|
|
|
|
// and that's okay because we know the wait will be brief (it happens
|
|
|
|
// if we dequeue a buffer while the consumer has acquired one but not released
|
|
|
|
// the old one yet -- for e.g.: see GLConsumer::updateTexImage()).
|
|
|
|
if (mDequeueBufferCannotBlock && (acquiredCount <= mMaxAcquiredBufferCount)) {
|
2013-07-17 05:56:09 +00:00
|
|
|
ST_LOGE("dequeueBuffer: would block! returning an error instead.");
|
|
|
|
return WOULD_BLOCK;
|
|
|
|
}
|
2012-01-22 23:26:27 +00:00
|
|
|
mDequeueCondition.wait(mMutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (found == INVALID_BUFFER_SLOT) {
|
|
|
|
// This should not happen.
|
|
|
|
ST_LOGE("dequeueBuffer: no available buffer slots");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int buf = found;
|
|
|
|
*outBuf = found;
|
|
|
|
|
2012-03-02 06:11:25 +00:00
|
|
|
ATRACE_BUFFER_INDEX(buf);
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
const bool useDefaultSize = !w && !h;
|
|
|
|
if (useDefaultSize) {
|
|
|
|
// use the default size
|
|
|
|
w = mDefaultWidth;
|
|
|
|
h = mDefaultHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
|
|
|
|
|
|
|
|
const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
|
|
|
|
if ((buffer == NULL) ||
|
|
|
|
(uint32_t(buffer->width) != w) ||
|
|
|
|
(uint32_t(buffer->height) != h) ||
|
|
|
|
(uint32_t(buffer->format) != format) ||
|
|
|
|
((uint32_t(buffer->usage) & usage) != usage))
|
|
|
|
{
|
2012-01-22 23:26:27 +00:00
|
|
|
mSlots[buf].mAcquireCalled = false;
|
2012-10-05 01:34:01 +00:00
|
|
|
mSlots[buf].mGraphicBuffer = NULL;
|
2012-01-22 23:26:27 +00:00
|
|
|
mSlots[buf].mRequestBufferCalled = false;
|
2012-06-28 19:52:05 +00:00
|
|
|
mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
|
2012-12-20 22:05:45 +00:00
|
|
|
mSlots[buf].mFence = Fence::NO_FENCE;
|
2012-01-22 23:26:27 +00:00
|
|
|
mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
returnFlags |= IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2013-08-01 22:48:40 +00:00
|
|
|
|
|
|
|
if (CC_UNLIKELY(mSlots[buf].mFence == NULL)) {
|
|
|
|
ST_LOGE("dequeueBuffer: about to return a NULL fence from mSlot. "
|
|
|
|
"buf=%d, w=%d, h=%d, format=%d",
|
|
|
|
buf, buffer->width, buffer->height, buffer->format);
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
dpy = mSlots[buf].mEglDisplay;
|
2012-06-28 19:52:05 +00:00
|
|
|
eglFence = mSlots[buf].mEglFence;
|
2013-03-16 04:34:30 +00:00
|
|
|
*outFence = mSlots[buf].mFence;
|
2012-06-28 19:52:05 +00:00
|
|
|
mSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
|
2012-12-20 22:05:45 +00:00
|
|
|
mSlots[buf].mFence = Fence::NO_FENCE;
|
2012-01-22 23:26:27 +00:00
|
|
|
} // end lock scope
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
if (returnFlags & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
|
2012-10-05 01:34:01 +00:00
|
|
|
status_t error;
|
|
|
|
sp<GraphicBuffer> graphicBuffer(
|
2013-08-01 22:48:40 +00:00
|
|
|
mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage, &error));
|
2012-10-05 01:34:01 +00:00
|
|
|
if (graphicBuffer == 0) {
|
2013-08-01 22:48:40 +00:00
|
|
|
ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
|
2012-10-05 01:34:01 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Scope for the lock
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("dequeueBuffer: BufferQueue has been abandoned!");
|
2012-10-05 01:34:01 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
mSlots[*outBuf].mFrameNumber = ~0;
|
2012-10-05 01:34:01 +00:00
|
|
|
mSlots[*outBuf].mGraphicBuffer = graphicBuffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-28 19:52:05 +00:00
|
|
|
if (eglFence != EGL_NO_SYNC_KHR) {
|
|
|
|
EGLint result = eglClientWaitSyncKHR(dpy, eglFence, 0, 1000000000);
|
2012-01-22 23:26:27 +00:00
|
|
|
// If something goes wrong, log the error, but return the buffer without
|
|
|
|
// synchronizing access to it. It's too late at this point to abort the
|
|
|
|
// dequeue operation.
|
|
|
|
if (result == EGL_FALSE) {
|
2012-05-14 02:41:35 +00:00
|
|
|
ST_LOGE("dequeueBuffer: error waiting for fence: %#x", eglGetError());
|
2012-01-22 23:26:27 +00:00
|
|
|
} else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
|
2012-05-14 02:41:35 +00:00
|
|
|
ST_LOGE("dequeueBuffer: timeout waiting for fence");
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
2012-06-28 19:52:05 +00:00
|
|
|
eglDestroySyncKHR(dpy, eglFence);
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
ST_LOGV("dequeueBuffer: returning slot=%d/%llu buf=%p flags=%#x", *outBuf,
|
|
|
|
mSlots[*outBuf].mFrameNumber,
|
2012-01-22 23:26:27 +00:00
|
|
|
mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
|
|
|
|
|
|
|
|
return returnFlags;
|
|
|
|
}
|
|
|
|
|
2012-04-09 23:14:01 +00:00
|
|
|
status_t BufferQueue::queueBuffer(int buf,
|
|
|
|
const QueueBufferInput& input, QueueBufferOutput* output) {
|
2012-02-24 03:27:23 +00:00
|
|
|
ATRACE_CALL();
|
2012-03-02 06:11:25 +00:00
|
|
|
ATRACE_BUFFER_INDEX(buf);
|
|
|
|
|
2012-04-18 02:36:18 +00:00
|
|
|
Rect crop;
|
|
|
|
uint32_t transform;
|
|
|
|
int scalingMode;
|
|
|
|
int64_t timestamp;
|
2013-08-16 21:55:39 +00:00
|
|
|
bool isAutoTimestamp;
|
2013-07-19 05:10:56 +00:00
|
|
|
bool async;
|
2012-06-28 19:52:05 +00:00
|
|
|
sp<Fence> fence;
|
2012-04-18 02:36:18 +00:00
|
|
|
|
2013-08-16 21:55:39 +00:00
|
|
|
input.deflate(×tamp, &isAutoTimestamp, &crop, &scalingMode, &transform,
|
|
|
|
&async, &fence);
|
2012-04-18 02:36:18 +00:00
|
|
|
|
2012-12-20 22:05:45 +00:00
|
|
|
if (fence == NULL) {
|
|
|
|
ST_LOGE("queueBuffer: fence is NULL");
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
switch (scalingMode) {
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_FREEZE:
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ST_LOGE("unknown scaling mode: %d", scalingMode);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2013-08-01 03:09:53 +00:00
|
|
|
sp<IConsumerListener> listener;
|
2012-01-22 23:26:27 +00:00
|
|
|
|
|
|
|
{ // scope for the lock
|
|
|
|
Mutex::Autolock lock(mMutex);
|
2013-05-03 21:50:50 +00:00
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
if (mAbandoned) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("queueBuffer: BufferQueue has been abandoned!");
|
2012-01-22 23:26:27 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
2013-07-19 05:10:56 +00:00
|
|
|
|
|
|
|
const int maxBufferCount = getMaxBufferCountLocked(async);
|
|
|
|
if (async && mOverrideMaxBufferCount) {
|
|
|
|
// FIXME: some drivers are manually setting the buffer-count (which they
|
|
|
|
// shouldn't), so we do this extra test here to handle that case.
|
|
|
|
// This is TEMPORARY, until we get this fixed.
|
|
|
|
if (mOverrideMaxBufferCount < maxBufferCount) {
|
|
|
|
ST_LOGE("queueBuffer: async mode is invalid with buffercount override");
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
}
|
2012-08-25 03:26:34 +00:00
|
|
|
if (buf < 0 || buf >= maxBufferCount) {
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
|
2012-08-25 03:26:34 +00:00
|
|
|
maxBufferCount, buf);
|
2012-01-22 23:26:27 +00:00
|
|
|
return -EINVAL;
|
|
|
|
} else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
|
|
|
|
ST_LOGE("queueBuffer: slot %d is not owned by the client "
|
|
|
|
"(state=%d)", buf, mSlots[buf].mBufferState);
|
|
|
|
return -EINVAL;
|
|
|
|
} else if (!mSlots[buf].mRequestBufferCalled) {
|
|
|
|
ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
|
|
|
|
"buffer", buf);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
ST_LOGV("queueBuffer: slot=%d/%llu time=%#llx crop=[%d,%d,%d,%d] "
|
|
|
|
"tr=%#x scale=%s",
|
|
|
|
buf, mFrameCounter + 1, timestamp,
|
|
|
|
crop.left, crop.top, crop.right, crop.bottom,
|
|
|
|
transform, scalingModeName(scalingMode));
|
|
|
|
|
2012-05-07 20:50:11 +00:00
|
|
|
const sp<GraphicBuffer>& graphicBuffer(mSlots[buf].mGraphicBuffer);
|
|
|
|
Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight());
|
|
|
|
Rect croppedCrop;
|
|
|
|
crop.intersect(bufferRect, &croppedCrop);
|
|
|
|
if (croppedCrop != crop) {
|
|
|
|
ST_LOGE("queueBuffer: crop rect is not contained within the "
|
|
|
|
"buffer in slot %d", buf);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
mSlots[buf].mFence = fence;
|
|
|
|
mSlots[buf].mBufferState = BufferSlot::QUEUED;
|
|
|
|
mFrameCounter++;
|
|
|
|
mSlots[buf].mFrameNumber = mFrameCounter;
|
|
|
|
|
|
|
|
BufferItem item;
|
|
|
|
item.mAcquireCalled = mSlots[buf].mAcquireCalled;
|
|
|
|
item.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
|
|
|
|
item.mCrop = crop;
|
2013-09-18 06:45:22 +00:00
|
|
|
item.mTransform = transform & ~NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
|
|
|
|
item.mTransformToDisplayInverse = bool(transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY);
|
2013-05-03 21:50:50 +00:00
|
|
|
item.mScalingMode = scalingMode;
|
|
|
|
item.mTimestamp = timestamp;
|
2013-08-16 21:55:39 +00:00
|
|
|
item.mIsAutoTimestamp = isAutoTimestamp;
|
2013-05-03 21:50:50 +00:00
|
|
|
item.mFrameNumber = mFrameCounter;
|
|
|
|
item.mBuf = buf;
|
|
|
|
item.mFence = fence;
|
2013-07-19 05:10:56 +00:00
|
|
|
item.mIsDroppable = mDequeueBufferCannotBlock || async;
|
2013-05-03 21:50:50 +00:00
|
|
|
|
2013-07-18 22:55:03 +00:00
|
|
|
if (mQueue.empty()) {
|
|
|
|
// when the queue is empty, we can ignore "mDequeueBufferCannotBlock", and
|
|
|
|
// simply queue this buffer.
|
2013-05-03 21:50:50 +00:00
|
|
|
mQueue.push_back(item);
|
2012-03-15 21:01:24 +00:00
|
|
|
listener = mConsumerListener;
|
2012-01-22 23:26:27 +00:00
|
|
|
} else {
|
2013-07-18 22:55:03 +00:00
|
|
|
// when the queue is not empty, we need to look at the front buffer
|
|
|
|
// state and see if we need to replace it.
|
|
|
|
Fifo::iterator front(mQueue.begin());
|
2013-07-19 05:10:56 +00:00
|
|
|
if (front->mIsDroppable) {
|
2013-05-03 21:50:50 +00:00
|
|
|
// buffer slot currently queued is marked free if still tracked
|
|
|
|
if (stillTracking(front)) {
|
|
|
|
mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
|
2013-07-19 05:25:55 +00:00
|
|
|
// reset the frame number of the freed buffer so that it is the first in
|
|
|
|
// line to be dequeued again.
|
|
|
|
mSlots[front->mBuf].mFrameNumber = 0;
|
2013-05-03 21:50:50 +00:00
|
|
|
}
|
2013-07-18 22:55:03 +00:00
|
|
|
// and we record the new buffer in the queued list
|
2013-05-03 21:50:50 +00:00
|
|
|
*front = item;
|
2013-07-18 22:55:03 +00:00
|
|
|
} else {
|
|
|
|
mQueue.push_back(item);
|
|
|
|
listener = mConsumerListener;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
mBufferHasBeenQueued = true;
|
2012-03-13 04:49:41 +00:00
|
|
|
mDequeueCondition.broadcast();
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2012-04-21 00:19:28 +00:00
|
|
|
output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
|
|
|
|
mQueue.size());
|
2012-02-24 03:27:23 +00:00
|
|
|
|
|
|
|
ATRACE_INT(mConsumerName.string(), mQueue.size());
|
2012-01-22 23:26:27 +00:00
|
|
|
} // scope for the lock
|
|
|
|
|
|
|
|
// call back without lock held
|
|
|
|
if (listener != 0) {
|
|
|
|
listener->onFrameAvailable();
|
|
|
|
}
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2013-03-16 04:34:30 +00:00
|
|
|
void BufferQueue::cancelBuffer(int buf, const sp<Fence>& fence) {
|
2012-02-24 03:27:23 +00:00
|
|
|
ATRACE_CALL();
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGV("cancelBuffer: slot=%d", buf);
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
|
|
|
ST_LOGW("cancelBuffer: BufferQueue has been abandoned!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-19 05:10:56 +00:00
|
|
|
if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
|
2013-07-19 05:10:56 +00:00
|
|
|
NUM_BUFFER_SLOTS, buf);
|
2012-01-22 23:26:27 +00:00
|
|
|
return;
|
|
|
|
} else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
|
|
|
|
ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
|
|
|
|
buf, mSlots[buf].mBufferState);
|
|
|
|
return;
|
2012-12-20 22:05:45 +00:00
|
|
|
} else if (fence == NULL) {
|
|
|
|
ST_LOGE("cancelBuffer: fence is NULL");
|
|
|
|
return;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
mSlots[buf].mBufferState = BufferSlot::FREE;
|
|
|
|
mSlots[buf].mFrameNumber = 0;
|
2012-06-28 19:52:05 +00:00
|
|
|
mSlots[buf].mFence = fence;
|
2012-03-13 04:49:41 +00:00
|
|
|
mDequeueCondition.broadcast();
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 02:35:45 +00:00
|
|
|
|
|
|
|
status_t BufferQueue::connect(const sp<IBinder>& token,
|
|
|
|
int api, bool producerControlledByApp, QueueBufferOutput* output) {
|
2012-02-24 03:27:23 +00:00
|
|
|
ATRACE_CALL();
|
2013-08-29 23:03:50 +00:00
|
|
|
ST_LOGV("connect: api=%d producerControlledByApp=%s", api,
|
|
|
|
producerControlledByApp ? "true" : "false");
|
2012-01-22 23:26:27 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
|
|
|
ST_LOGE("connect: BufferQueue has been abandoned!");
|
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
2012-03-15 21:01:24 +00:00
|
|
|
if (mConsumerListener == NULL) {
|
|
|
|
ST_LOGE("connect: BufferQueue has no consumer!");
|
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
int err = NO_ERROR;
|
|
|
|
switch (api) {
|
|
|
|
case NATIVE_WINDOW_API_EGL:
|
|
|
|
case NATIVE_WINDOW_API_CPU:
|
|
|
|
case NATIVE_WINDOW_API_MEDIA:
|
|
|
|
case NATIVE_WINDOW_API_CAMERA:
|
|
|
|
if (mConnectedApi != NO_CONNECTED_API) {
|
|
|
|
ST_LOGE("connect: already connected (cur=%d, req=%d)",
|
|
|
|
mConnectedApi, api);
|
|
|
|
err = -EINVAL;
|
|
|
|
} else {
|
|
|
|
mConnectedApi = api;
|
2013-09-12 02:35:45 +00:00
|
|
|
output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint, mQueue.size());
|
|
|
|
|
|
|
|
// set-up a death notification so that we can disconnect automatically
|
|
|
|
// when/if the remote producer dies.
|
|
|
|
// This will fail with INVALID_OPERATION if the "token" is local to our process.
|
|
|
|
if (token->linkToDeath(static_cast<IBinder::DeathRecipient*>(this)) == NO_ERROR) {
|
|
|
|
mConnectedProducerToken = token;
|
|
|
|
}
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-22 23:26:27 +00:00
|
|
|
|
|
|
|
mBufferHasBeenQueued = false;
|
2013-07-17 05:56:09 +00:00
|
|
|
mDequeueBufferCannotBlock = mConsumerControlledByApp && producerControlledByApp;
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-09-12 02:35:45 +00:00
|
|
|
void BufferQueue::binderDied(const wp<IBinder>& who) {
|
|
|
|
// If we're here, it means that a producer we were connected to died.
|
|
|
|
// We're GUARANTEED that we still are connected to it because it has no other way
|
|
|
|
// to get disconnected -- or -- we wouldn't be here because we're removing this
|
|
|
|
// callback upon disconnect. Therefore, it's okay to read mConnectedApi without
|
|
|
|
// synchronization here.
|
|
|
|
int api = mConnectedApi;
|
|
|
|
this->disconnect(api);
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
status_t BufferQueue::disconnect(int api) {
|
2012-02-24 03:27:23 +00:00
|
|
|
ATRACE_CALL();
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGV("disconnect: api=%d", api);
|
|
|
|
|
|
|
|
int err = NO_ERROR;
|
2013-08-01 03:09:53 +00:00
|
|
|
sp<IConsumerListener> listener;
|
2012-03-15 21:01:24 +00:00
|
|
|
|
|
|
|
{ // Scope for the lock
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
|
|
|
// it is not really an error to disconnect after the surface
|
|
|
|
// has been abandoned, it should just be a no-op.
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (api) {
|
|
|
|
case NATIVE_WINDOW_API_EGL:
|
|
|
|
case NATIVE_WINDOW_API_CPU:
|
|
|
|
case NATIVE_WINDOW_API_MEDIA:
|
|
|
|
case NATIVE_WINDOW_API_CAMERA:
|
|
|
|
if (mConnectedApi == api) {
|
2013-07-18 22:55:03 +00:00
|
|
|
freeAllBuffersLocked();
|
2013-09-12 02:35:45 +00:00
|
|
|
// remove our death notification callback if we have one
|
|
|
|
sp<IBinder> token = mConnectedProducerToken;
|
|
|
|
if (token != NULL) {
|
|
|
|
// this can fail if we're here because of the death notification
|
|
|
|
// either way, we just ignore.
|
|
|
|
token->unlinkToDeath(static_cast<IBinder::DeathRecipient*>(this));
|
|
|
|
}
|
|
|
|
mConnectedProducerToken = NULL;
|
2012-03-15 21:01:24 +00:00
|
|
|
mConnectedApi = NO_CONNECTED_API;
|
|
|
|
mDequeueCondition.broadcast();
|
|
|
|
listener = mConsumerListener;
|
|
|
|
} else {
|
|
|
|
ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
|
|
|
|
mConnectedApi, api);
|
|
|
|
err = -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ST_LOGE("disconnect: unknown API %d", api);
|
2012-01-22 23:26:27 +00:00
|
|
|
err = -EINVAL;
|
2012-03-15 21:01:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
2012-03-15 21:01:24 +00:00
|
|
|
|
|
|
|
if (listener != NULL) {
|
|
|
|
listener->onBuffersReleased();
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-04-22 14:55:35 +00:00
|
|
|
void BufferQueue::dump(String8& result, const char* prefix) const {
|
2012-01-22 23:26:27 +00:00
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
|
|
|
|
String8 fifo;
|
|
|
|
int fifoSize = 0;
|
|
|
|
Fifo::const_iterator i(mQueue.begin());
|
|
|
|
while (i != mQueue.end()) {
|
2013-05-03 21:50:50 +00:00
|
|
|
fifo.appendFormat("%02d:%p crop=[%d,%d,%d,%d], "
|
|
|
|
"xform=0x%02x, time=%#llx, scale=%s\n",
|
|
|
|
i->mBuf, i->mGraphicBuffer.get(),
|
|
|
|
i->mCrop.left, i->mCrop.top, i->mCrop.right,
|
|
|
|
i->mCrop.bottom, i->mTransform, i->mTimestamp,
|
|
|
|
scalingModeName(i->mScalingMode)
|
|
|
|
);
|
|
|
|
i++;
|
2013-04-22 14:55:35 +00:00
|
|
|
fifoSize++;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2012-08-25 03:26:34 +00:00
|
|
|
|
2013-04-22 14:55:35 +00:00
|
|
|
result.appendFormat(
|
2013-07-19 05:10:56 +00:00
|
|
|
"%s-BufferQueue mMaxAcquiredBufferCount=%d, mDequeueBufferCannotBlock=%d, default-size=[%dx%d], "
|
2012-09-14 23:10:11 +00:00
|
|
|
"default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n",
|
2013-07-19 05:10:56 +00:00
|
|
|
prefix, mMaxAcquiredBufferCount, mDequeueBufferCannotBlock, mDefaultWidth,
|
2012-09-14 23:10:11 +00:00
|
|
|
mDefaultHeight, mDefaultBufferFormat, mTransformHint,
|
|
|
|
fifoSize, fifo.string());
|
2012-01-22 23:26:27 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
const char * operator()(int state) const {
|
|
|
|
switch (state) {
|
|
|
|
case BufferSlot::DEQUEUED: return "DEQUEUED";
|
|
|
|
case BufferSlot::QUEUED: return "QUEUED";
|
|
|
|
case BufferSlot::FREE: return "FREE";
|
|
|
|
case BufferSlot::ACQUIRED: return "ACQUIRED";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} stateName;
|
|
|
|
|
2013-07-19 05:10:56 +00:00
|
|
|
// just trim the free buffers to not spam the dump
|
|
|
|
int maxBufferCount = 0;
|
|
|
|
for (int i=NUM_BUFFER_SLOTS-1 ; i>=0 ; i--) {
|
|
|
|
const BufferSlot& slot(mSlots[i]);
|
|
|
|
if ((slot.mBufferState != BufferSlot::FREE) || (slot.mGraphicBuffer != NULL)) {
|
|
|
|
maxBufferCount = i+1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-25 03:26:34 +00:00
|
|
|
for (int i=0 ; i<maxBufferCount ; i++) {
|
2012-01-22 23:26:27 +00:00
|
|
|
const BufferSlot& slot(mSlots[i]);
|
2013-07-19 05:10:56 +00:00
|
|
|
const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
|
2013-04-22 14:55:35 +00:00
|
|
|
result.appendFormat(
|
2013-05-03 21:50:50 +00:00
|
|
|
"%s%s[%02d:%p] state=%-8s",
|
2013-07-19 05:10:56 +00:00
|
|
|
prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i, buf.get(),
|
2013-05-03 21:50:50 +00:00
|
|
|
stateName(slot.mBufferState)
|
2012-01-22 23:26:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (buf != NULL) {
|
2013-04-22 14:55:35 +00:00
|
|
|
result.appendFormat(
|
2012-01-22 23:26:27 +00:00
|
|
|
", %p [%4ux%4u:%4u,%3X]",
|
|
|
|
buf->handle, buf->width, buf->height, buf->stride,
|
|
|
|
buf->format);
|
|
|
|
}
|
|
|
|
result.append("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-03 03:11:05 +00:00
|
|
|
void BufferQueue::freeBufferLocked(int slot) {
|
|
|
|
ST_LOGV("freeBufferLocked: slot=%d", slot);
|
|
|
|
mSlots[slot].mGraphicBuffer = 0;
|
|
|
|
if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
|
|
|
|
mSlots[slot].mNeedsCleanupOnRelease = true;
|
2012-03-27 03:37:15 +00:00
|
|
|
}
|
2012-08-03 03:11:05 +00:00
|
|
|
mSlots[slot].mBufferState = BufferSlot::FREE;
|
|
|
|
mSlots[slot].mFrameNumber = 0;
|
|
|
|
mSlots[slot].mAcquireCalled = false;
|
2012-01-22 23:26:27 +00:00
|
|
|
|
|
|
|
// destroy fence as BufferQueue now takes ownership
|
2012-08-03 03:11:05 +00:00
|
|
|
if (mSlots[slot].mEglFence != EGL_NO_SYNC_KHR) {
|
|
|
|
eglDestroySyncKHR(mSlots[slot].mEglDisplay, mSlots[slot].mEglFence);
|
|
|
|
mSlots[slot].mEglFence = EGL_NO_SYNC_KHR;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
2012-12-20 22:05:45 +00:00
|
|
|
mSlots[slot].mFence = Fence::NO_FENCE;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BufferQueue::freeAllBuffersLocked() {
|
2012-01-22 23:26:27 +00:00
|
|
|
mBufferHasBeenQueued = false;
|
2012-01-22 23:26:27 +00:00
|
|
|
for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
|
|
|
|
freeBufferLocked(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 20:37:42 +00:00
|
|
|
status_t BufferQueue::acquireBuffer(BufferItem *buffer, nsecs_t expectedPresent) {
|
2012-03-02 06:11:25 +00:00
|
|
|
ATRACE_CALL();
|
2012-01-22 23:26:27 +00:00
|
|
|
Mutex::Autolock _l(mMutex);
|
2012-08-29 00:18:50 +00:00
|
|
|
|
|
|
|
// Check that the consumer doesn't currently have the maximum number of
|
|
|
|
// buffers acquired. We allow the max buffer count to be exceeded by one
|
|
|
|
// buffer, so that the consumer can successfully set up the newly acquired
|
|
|
|
// buffer before releasing the old one.
|
|
|
|
int numAcquiredBuffers = 0;
|
|
|
|
for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
|
|
|
|
if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) {
|
|
|
|
numAcquiredBuffers++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (numAcquiredBuffers >= mMaxAcquiredBufferCount+1) {
|
|
|
|
ST_LOGE("acquireBuffer: max acquired buffer count reached: %d (max=%d)",
|
|
|
|
numAcquiredBuffers, mMaxAcquiredBufferCount);
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
// check if queue is empty
|
|
|
|
// In asynchronous mode the list is guaranteed to be one buffer
|
|
|
|
// deep, while in synchronous mode we use the oldest buffer.
|
2013-06-28 20:52:40 +00:00
|
|
|
if (mQueue.empty()) {
|
|
|
|
return NO_BUFFER_AVAILABLE;
|
|
|
|
}
|
2012-03-02 06:11:25 +00:00
|
|
|
|
2013-06-28 20:52:40 +00:00
|
|
|
Fifo::iterator front(mQueue.begin());
|
|
|
|
|
2013-08-01 20:37:42 +00:00
|
|
|
// If expectedPresent is specified, we may not want to return a buffer yet.
|
|
|
|
// If it's specified and there's more than one buffer queued, we may
|
|
|
|
// want to drop a buffer.
|
|
|
|
if (expectedPresent != 0) {
|
|
|
|
const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
|
|
|
|
|
|
|
|
// The "expectedPresent" argument indicates when the buffer is expected
|
|
|
|
// to be presented on-screen. If the buffer's desired-present time
|
|
|
|
// is earlier (less) than expectedPresent, meaning it'll be displayed
|
|
|
|
// on time or possibly late if we show it ASAP, we acquire and return
|
|
|
|
// it. If we don't want to display it until after the expectedPresent
|
|
|
|
// time, we return PRESENT_LATER without acquiring it.
|
|
|
|
//
|
|
|
|
// To be safe, we don't defer acquisition if expectedPresent is
|
|
|
|
// more than one second in the future beyond the desired present time
|
|
|
|
// (i.e. we'd be holding the buffer for a long time).
|
|
|
|
//
|
|
|
|
// NOTE: code assumes monotonic time values from the system clock are
|
|
|
|
// positive.
|
2013-08-16 21:55:39 +00:00
|
|
|
|
|
|
|
// Start by checking to see if we can drop frames. We skip this check
|
|
|
|
// if the timestamps are being auto-generated by Surface -- if the
|
|
|
|
// app isn't generating timestamps explicitly, they probably don't
|
|
|
|
// want frames to be discarded based on them.
|
|
|
|
while (mQueue.size() > 1 && !mQueue[0].mIsAutoTimestamp) {
|
2013-08-01 20:37:42 +00:00
|
|
|
// If entry[1] is timely, drop entry[0] (and repeat). We apply
|
|
|
|
// an additional criteria here: we only drop the earlier buffer if
|
|
|
|
// our desiredPresent falls within +/- 1 second of the expected
|
|
|
|
// present. Otherwise, bogus desiredPresent times (e.g. 0 or
|
|
|
|
// a small relative timestamp), which normally mean "ignore the
|
|
|
|
// timestamp and acquire immediately", would cause us to drop
|
|
|
|
// frames.
|
|
|
|
//
|
|
|
|
// We may want to add an additional criteria: don't drop the
|
|
|
|
// earlier buffer if entry[1]'s fence hasn't signaled yet.
|
|
|
|
//
|
|
|
|
// (Vector front is [0], back is [size()-1])
|
|
|
|
const BufferItem& bi(mQueue[1]);
|
|
|
|
nsecs_t desiredPresent = bi.mTimestamp;
|
|
|
|
if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
|
|
|
|
desiredPresent > expectedPresent) {
|
|
|
|
// This buffer is set to display in the near future, or
|
|
|
|
// desiredPresent is garbage. Either way we don't want to
|
|
|
|
// drop the previous buffer just to get this on screen sooner.
|
|
|
|
ST_LOGV("pts nodrop: des=%lld expect=%lld (%lld) now=%lld",
|
|
|
|
desiredPresent, expectedPresent, desiredPresent - expectedPresent,
|
|
|
|
systemTime(CLOCK_MONOTONIC));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ST_LOGV("pts drop: queue1des=%lld expect=%lld size=%d",
|
|
|
|
desiredPresent, expectedPresent, mQueue.size());
|
|
|
|
if (stillTracking(front)) {
|
|
|
|
// front buffer is still in mSlots, so mark the slot as free
|
|
|
|
mSlots[front->mBuf].mBufferState = BufferSlot::FREE;
|
|
|
|
}
|
|
|
|
mQueue.erase(front);
|
|
|
|
front = mQueue.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if the front buffer is due.
|
|
|
|
nsecs_t desiredPresent = front->mTimestamp;
|
|
|
|
if (desiredPresent > expectedPresent &&
|
|
|
|
desiredPresent < expectedPresent + MAX_REASONABLE_NSEC) {
|
|
|
|
ST_LOGV("pts defer: des=%lld expect=%lld (%lld) now=%lld",
|
|
|
|
desiredPresent, expectedPresent, desiredPresent - expectedPresent,
|
|
|
|
systemTime(CLOCK_MONOTONIC));
|
|
|
|
return PRESENT_LATER;
|
|
|
|
}
|
|
|
|
|
|
|
|
ST_LOGV("pts accept: des=%lld expect=%lld (%lld) now=%lld",
|
|
|
|
desiredPresent, expectedPresent, desiredPresent - expectedPresent,
|
2013-06-28 20:52:40 +00:00
|
|
|
systemTime(CLOCK_MONOTONIC));
|
|
|
|
}
|
2013-05-03 21:50:50 +00:00
|
|
|
|
2013-08-01 20:37:42 +00:00
|
|
|
int buf = front->mBuf;
|
2013-06-28 20:52:40 +00:00
|
|
|
*buffer = *front;
|
|
|
|
ATRACE_BUFFER_INDEX(buf);
|
2012-08-03 03:11:05 +00:00
|
|
|
|
2013-06-28 20:52:40 +00:00
|
|
|
ST_LOGV("acquireBuffer: acquiring { slot=%d/%llu, buffer=%p }",
|
|
|
|
front->mBuf, front->mFrameNumber,
|
|
|
|
front->mGraphicBuffer->handle);
|
|
|
|
// if front buffer still being tracked update slot state
|
|
|
|
if (stillTracking(front)) {
|
|
|
|
mSlots[buf].mAcquireCalled = true;
|
|
|
|
mSlots[buf].mNeedsCleanupOnRelease = false;
|
|
|
|
mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
|
|
|
|
mSlots[buf].mFence = Fence::NO_FENCE;
|
|
|
|
}
|
2012-02-24 03:27:23 +00:00
|
|
|
|
2013-06-28 20:52:40 +00:00
|
|
|
// If the buffer has previously been acquired by the consumer, set
|
|
|
|
// mGraphicBuffer to NULL to avoid unnecessarily remapping this
|
|
|
|
// buffer on the consumer side.
|
|
|
|
if (buffer->mAcquireCalled) {
|
|
|
|
buffer->mGraphicBuffer = NULL;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2013-06-28 20:52:40 +00:00
|
|
|
mQueue.erase(front);
|
|
|
|
mDequeueCondition.broadcast();
|
|
|
|
|
|
|
|
ATRACE_INT(mConsumerName.string(), mQueue.size());
|
|
|
|
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
status_t BufferQueue::releaseBuffer(
|
|
|
|
int buf, uint64_t frameNumber, EGLDisplay display,
|
2012-06-28 19:52:05 +00:00
|
|
|
EGLSyncKHR eglFence, const sp<Fence>& fence) {
|
2012-03-02 06:11:25 +00:00
|
|
|
ATRACE_CALL();
|
|
|
|
ATRACE_BUFFER_INDEX(buf);
|
|
|
|
|
2012-12-20 22:05:45 +00:00
|
|
|
if (buf == INVALID_BUFFER_SLOT || fence == NULL) {
|
|
|
|
return BAD_VALUE;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 01:00:53 +00:00
|
|
|
Mutex::Autolock _l(mMutex);
|
2013-05-03 21:50:50 +00:00
|
|
|
|
|
|
|
// If the frame number has changed because buffer has been reallocated,
|
|
|
|
// we can ignore this releaseBuffer for the old buffer.
|
|
|
|
if (frameNumber != mSlots[buf].mFrameNumber) {
|
|
|
|
return STALE_BUFFER_SLOT;
|
|
|
|
}
|
2013-07-23 01:00:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Internal state consistency checks:
|
|
|
|
// Make sure this buffers hasn't been queued while we were owning it (acquired)
|
|
|
|
Fifo::iterator front(mQueue.begin());
|
|
|
|
Fifo::const_iterator const end(mQueue.end());
|
|
|
|
while (front != end) {
|
|
|
|
if (front->mBuf == buf) {
|
|
|
|
LOG_ALWAYS_FATAL("[%s] received new buffer(#%lld) on slot #%d that has not yet been "
|
|
|
|
"acquired", mConsumerName.string(), frameNumber, buf);
|
|
|
|
break; // never reached
|
|
|
|
}
|
|
|
|
front++;
|
|
|
|
}
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2012-03-27 03:37:15 +00:00
|
|
|
// The buffer can now only be released if its in the acquired state
|
|
|
|
if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) {
|
2013-05-03 21:50:50 +00:00
|
|
|
mSlots[buf].mEglDisplay = display;
|
|
|
|
mSlots[buf].mEglFence = eglFence;
|
|
|
|
mSlots[buf].mFence = fence;
|
2012-01-22 23:26:27 +00:00
|
|
|
mSlots[buf].mBufferState = BufferSlot::FREE;
|
2012-03-27 03:37:15 +00:00
|
|
|
} else if (mSlots[buf].mNeedsCleanupOnRelease) {
|
|
|
|
ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState);
|
|
|
|
mSlots[buf].mNeedsCleanupOnRelease = false;
|
|
|
|
return STALE_BUFFER_SLOT;
|
|
|
|
} else {
|
|
|
|
ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState);
|
|
|
|
return -EINVAL;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 03:37:15 +00:00
|
|
|
mDequeueCondition.broadcast();
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2013-08-01 03:09:53 +00:00
|
|
|
status_t BufferQueue::consumerConnect(const sp<IConsumerListener>& consumerListener,
|
2013-07-17 05:56:09 +00:00
|
|
|
bool controlledByApp) {
|
2013-08-29 23:03:50 +00:00
|
|
|
ST_LOGV("consumerConnect controlledByApp=%s",
|
|
|
|
controlledByApp ? "true" : "false");
|
2012-03-15 21:01:24 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
|
|
|
ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
|
|
|
|
return NO_INIT;
|
|
|
|
}
|
2013-04-05 00:09:03 +00:00
|
|
|
if (consumerListener == NULL) {
|
|
|
|
ST_LOGE("consumerConnect: consumerListener may not be NULL");
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
2012-03-15 21:01:24 +00:00
|
|
|
|
|
|
|
mConsumerListener = consumerListener;
|
2013-07-17 05:56:09 +00:00
|
|
|
mConsumerControlledByApp = controlledByApp;
|
2012-03-15 21:01:24 +00:00
|
|
|
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-03-15 21:01:24 +00:00
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
status_t BufferQueue::consumerDisconnect() {
|
2012-03-15 21:01:24 +00:00
|
|
|
ST_LOGV("consumerDisconnect");
|
2012-01-22 23:26:27 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-02-23 22:35:13 +00:00
|
|
|
|
2012-03-15 21:01:24 +00:00
|
|
|
if (mConsumerListener == NULL) {
|
|
|
|
ST_LOGE("consumerDisconnect: No consumer is connected!");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2012-02-23 22:35:13 +00:00
|
|
|
|
2012-03-15 21:01:24 +00:00
|
|
|
mAbandoned = true;
|
|
|
|
mConsumerListener = NULL;
|
2012-02-23 22:35:13 +00:00
|
|
|
mQueue.clear();
|
2012-01-22 23:26:27 +00:00
|
|
|
freeAllBuffersLocked();
|
2012-03-13 04:49:41 +00:00
|
|
|
mDequeueCondition.broadcast();
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2012-03-15 21:01:24 +00:00
|
|
|
status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) {
|
|
|
|
ST_LOGV("getReleasedBuffers");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
|
|
|
ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!");
|
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t mask = 0;
|
|
|
|
for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
|
|
|
|
if (!mSlots[i].mAcquireCalled) {
|
|
|
|
mask |= 1 << i;
|
|
|
|
}
|
|
|
|
}
|
2013-05-03 21:50:50 +00:00
|
|
|
|
|
|
|
// Remove buffers in flight (on the queue) from the mask where acquire has
|
|
|
|
// been called, as the consumer will not receive the buffer address, so
|
|
|
|
// it should not free these slots.
|
|
|
|
Fifo::iterator front(mQueue.begin());
|
|
|
|
while (front != mQueue.end()) {
|
|
|
|
if (front->mAcquireCalled)
|
|
|
|
mask &= ~(1 << front->mBuf);
|
|
|
|
front++;
|
|
|
|
}
|
|
|
|
|
2012-03-15 21:01:24 +00:00
|
|
|
*slotMask = mask;
|
|
|
|
|
|
|
|
ST_LOGV("getReleasedBuffers: returning mask %#x", mask);
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-07-23 01:00:53 +00:00
|
|
|
status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) {
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
|
|
|
|
if (!w || !h) {
|
|
|
|
ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
|
|
|
|
w, h);
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mDefaultWidth = w;
|
|
|
|
mDefaultHeight = h;
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2012-08-25 00:25:13 +00:00
|
|
|
status_t BufferQueue::setDefaultMaxBufferCount(int bufferCount) {
|
2012-02-24 03:27:23 +00:00
|
|
|
ATRACE_CALL();
|
2012-01-22 23:26:27 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-08-25 00:25:13 +00:00
|
|
|
return setDefaultMaxBufferCountLocked(bufferCount);
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 00:28:53 +00:00
|
|
|
status_t BufferQueue::disableAsyncBuffer() {
|
|
|
|
ATRACE_CALL();
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
if (mConsumerListener != NULL) {
|
|
|
|
ST_LOGE("disableAsyncBuffer: consumer already connected!");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
mUseAsyncBuffer = false;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-08-28 01:48:37 +00:00
|
|
|
status_t BufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
|
|
|
|
ATRACE_CALL();
|
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-08-31 01:36:22 +00:00
|
|
|
if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > MAX_MAX_ACQUIRED_BUFFERS) {
|
|
|
|
ST_LOGE("setMaxAcquiredBufferCount: invalid count specified: %d",
|
|
|
|
maxAcquiredBuffers);
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
2012-08-28 01:48:37 +00:00
|
|
|
if (mConnectedApi != NO_CONNECTED_API) {
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
mMaxAcquiredBufferCount = maxAcquiredBuffers;
|
2013-04-05 00:09:03 +00:00
|
|
|
return NO_ERROR;
|
2012-08-28 01:48:37 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 05:10:56 +00:00
|
|
|
int BufferQueue::getMinUndequeuedBufferCount(bool async) const {
|
2013-07-24 00:28:53 +00:00
|
|
|
// if dequeueBuffer is allowed to error out, we don't have to
|
|
|
|
// add an extra buffer.
|
|
|
|
if (!mUseAsyncBuffer)
|
|
|
|
return mMaxAcquiredBufferCount;
|
|
|
|
|
|
|
|
// we're in async mode, or we want to prevent the app to
|
|
|
|
// deadlock itself, we throw-in an extra buffer to guarantee it.
|
|
|
|
if (mDequeueBufferCannotBlock || async)
|
|
|
|
return mMaxAcquiredBufferCount+1;
|
|
|
|
|
|
|
|
return mMaxAcquiredBufferCount;
|
2013-07-19 05:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int BufferQueue::getMinMaxBufferCountLocked(bool async) const {
|
|
|
|
return getMinUndequeuedBufferCount(async) + 1;
|
2012-08-25 00:25:13 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 05:10:56 +00:00
|
|
|
int BufferQueue::getMaxBufferCountLocked(bool async) const {
|
|
|
|
int minMaxBufferCount = getMinMaxBufferCountLocked(async);
|
2012-08-25 03:26:34 +00:00
|
|
|
|
|
|
|
int maxBufferCount = mDefaultMaxBufferCount;
|
|
|
|
if (maxBufferCount < minMaxBufferCount) {
|
|
|
|
maxBufferCount = minMaxBufferCount;
|
|
|
|
}
|
|
|
|
if (mOverrideMaxBufferCount != 0) {
|
|
|
|
assert(mOverrideMaxBufferCount >= minMaxBufferCount);
|
|
|
|
maxBufferCount = mOverrideMaxBufferCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any buffers that are dequeued by the producer or sitting in the queue
|
|
|
|
// waiting to be consumed need to have their slots preserved. Such
|
|
|
|
// buffers will temporarily keep the max buffer count up until the slots
|
|
|
|
// no longer need to be preserved.
|
|
|
|
for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) {
|
|
|
|
BufferSlot::BufferState state = mSlots[i].mBufferState;
|
|
|
|
if (state == BufferSlot::QUEUED || state == BufferSlot::DEQUEUED) {
|
|
|
|
maxBufferCount = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxBufferCount;
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
bool BufferQueue::stillTracking(const BufferItem *item) const {
|
|
|
|
const BufferSlot &slot = mSlots[item->mBuf];
|
|
|
|
|
|
|
|
ST_LOGV("stillTracking?: item: { slot=%d/%llu, buffer=%p }, "
|
|
|
|
"slot: { slot=%d/%llu, buffer=%p }",
|
|
|
|
item->mBuf, item->mFrameNumber,
|
|
|
|
(item->mGraphicBuffer.get() ? item->mGraphicBuffer->handle : 0),
|
|
|
|
item->mBuf, slot.mFrameNumber,
|
|
|
|
(slot.mGraphicBuffer.get() ? slot.mGraphicBuffer->handle : 0));
|
|
|
|
|
|
|
|
// Compare item with its original buffer slot. We can check the slot
|
|
|
|
// as the buffer would not be moved to a different slot by the producer.
|
|
|
|
return (slot.mGraphicBuffer != NULL &&
|
|
|
|
item->mGraphicBuffer->handle == slot.mGraphicBuffer->handle);
|
|
|
|
}
|
|
|
|
|
2012-03-15 21:01:24 +00:00
|
|
|
BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
|
2013-08-01 03:09:53 +00:00
|
|
|
const wp<ConsumerListener>& consumerListener):
|
2012-03-15 21:01:24 +00:00
|
|
|
mConsumerListener(consumerListener) {}
|
|
|
|
|
|
|
|
BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
|
|
|
|
|
|
|
|
void BufferQueue::ProxyConsumerListener::onFrameAvailable() {
|
2013-08-01 03:09:53 +00:00
|
|
|
sp<ConsumerListener> listener(mConsumerListener.promote());
|
2012-03-15 21:01:24 +00:00
|
|
|
if (listener != NULL) {
|
|
|
|
listener->onFrameAvailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
|
2013-08-01 03:09:53 +00:00
|
|
|
sp<ConsumerListener> listener(mConsumerListener.promote());
|
2012-03-15 21:01:24 +00:00
|
|
|
if (listener != NULL) {
|
|
|
|
listener->onBuffersReleased();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
}; // namespace android
|