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>
|
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>
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 01:48:37 +00:00
|
|
|
BufferQueue::BufferQueue(bool allowSynchronousMode,
|
2012-06-08 00:52:54 +00:00
|
|
|
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),
|
2012-01-22 23:26:27 +00:00
|
|
|
mSynchronousMode(false),
|
|
|
|
mAllowSynchronousMode(allowSynchronousMode),
|
|
|
|
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) {
|
2012-08-25 03:26:34 +00:00
|
|
|
if (count < 2 || 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
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
bool BufferQueue::isSynchronousMode() const {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mSynchronousMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mConsumerUsageBits = usage;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
status_t BufferQueue::setBufferCount(int bufferCount) {
|
|
|
|
ST_LOGV("setBufferCount: count=%d", bufferCount);
|
|
|
|
|
2012-03-27 03:37:15 +00:00
|
|
|
sp<ConsumerListener> listener;
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
ST_LOGE("setBufferCount: bufferCount larger than slots available");
|
|
|
|
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
|
2012-08-25 03:26:34 +00:00
|
|
|
int maxBufferCount = getMaxBufferCountLocked();
|
|
|
|
for (int i=0 ; i<maxBufferCount; 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-08-25 00:25:13 +00:00
|
|
|
const int minBufferSlots = getMinMaxBufferCountLocked();
|
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();
|
|
|
|
return OK;
|
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
|
|
|
|
// and will release all of its buffer references.
|
2012-08-25 03:26:34 +00:00
|
|
|
//
|
|
|
|
// XXX: Should this use drainQueueAndFreeBuffersLocked instead?
|
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
|
|
|
mBufferHasBeenQueued = false;
|
|
|
|
mDequeueCondition.broadcast();
|
|
|
|
listener = mConsumerListener;
|
|
|
|
} // scope for lock
|
|
|
|
|
|
|
|
if (listener != NULL) {
|
|
|
|
listener->onBuffersReleased();
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
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:
|
2012-08-28 01:48:37 +00:00
|
|
|
value = getMinUndequeuedBufferCountLocked();
|
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;
|
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;
|
|
|
|
}
|
2012-08-25 03:26:34 +00:00
|
|
|
int maxBufferCount = getMaxBufferCountLocked();
|
|
|
|
if (slot < 0 || maxBufferCount <= slot) {
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
|
2012-08-25 03:26:34 +00:00
|
|
|
maxBufferCount, slot);
|
|
|
|
return BAD_VALUE;
|
|
|
|
} else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) {
|
|
|
|
// XXX: I vaguely recall there was some reason this can be valid, but
|
|
|
|
// for the life of me I can't recall under what circumstances that's
|
|
|
|
// the case.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-14 22:26:33 +00:00
|
|
|
status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>& outFence,
|
|
|
|
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;
|
|
|
|
int dequeuedCount = 0;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-25 03:26:34 +00:00
|
|
|
const int maxBufferCount = getMaxBufferCountLocked();
|
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;
|
|
|
|
dequeuedCount = 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;
|
|
|
|
if (state == BufferSlot::DEQUEUED) {
|
|
|
|
dequeuedCount++;
|
|
|
|
}
|
|
|
|
|
2012-10-02 23:40:30 +00:00
|
|
|
if (state == 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.
|
|
|
|
*/
|
2012-11-20 00:50:24 +00:00
|
|
|
if ((found < 0) ||
|
|
|
|
mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
|
|
|
|
found = i;
|
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);
|
|
|
|
const int minUndequeuedCount = getMinUndequeuedBufferCountLocked();
|
|
|
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
dpy = mSlots[buf].mEglDisplay;
|
2012-06-28 19:52:05 +00:00
|
|
|
eglFence = mSlots[buf].mEglFence;
|
|
|
|
outFence = mSlots[buf].mFence;
|
|
|
|
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(
|
|
|
|
mGraphicBufferAlloc->createGraphicBuffer(
|
|
|
|
w, h, format, usage, &error));
|
|
|
|
if (graphicBuffer == 0) {
|
|
|
|
ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
|
|
|
|
"failed");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", *outBuf,
|
|
|
|
mSlots[*outBuf].mGraphicBuffer->handle, returnFlags);
|
|
|
|
|
|
|
|
return returnFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t BufferQueue::setSynchronousMode(bool enabled) {
|
2012-02-24 03:27:23 +00:00
|
|
|
ATRACE_CALL();
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGV("setSynchronousMode: enabled=%d", enabled);
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
2012-12-18 17:49:45 +00:00
|
|
|
ST_LOGE("setSynchronousMode: BufferQueue has been abandoned!");
|
2012-01-22 23:26:27 +00:00
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t err = OK;
|
|
|
|
if (!mAllowSynchronousMode && enabled)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (!enabled) {
|
|
|
|
// going to asynchronous mode, drain the queue
|
|
|
|
err = drainQueueLocked();
|
|
|
|
if (err != NO_ERROR)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSynchronousMode != enabled) {
|
|
|
|
// - if we're going to asynchronous mode, the queue is guaranteed to be
|
|
|
|
// empty here
|
|
|
|
// - if the client set the number of buffers, we're guaranteed that
|
|
|
|
// we have at least 3 (because we don't allow less)
|
|
|
|
mSynchronousMode = enabled;
|
2012-03-13 04:49:41 +00:00
|
|
|
mDequeueCondition.broadcast();
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
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;
|
2012-06-28 19:52:05 +00:00
|
|
|
sp<Fence> fence;
|
2012-04-18 02:36:18 +00:00
|
|
|
|
2012-06-28 19:52:05 +00:00
|
|
|
input.deflate(×tamp, &crop, &scalingMode, &transform, &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;
|
|
|
|
}
|
|
|
|
|
2012-05-10 09:22:33 +00:00
|
|
|
ST_LOGV("queueBuffer: slot=%d time=%#llx crop=[%d,%d,%d,%d] tr=%#x "
|
|
|
|
"scale=%s",
|
|
|
|
buf, timestamp, crop.left, crop.top, crop.right, crop.bottom,
|
|
|
|
transform, scalingModeName(scalingMode));
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2012-03-15 21:01:24 +00:00
|
|
|
sp<ConsumerListener> listener;
|
2012-01-22 23:26:27 +00:00
|
|
|
|
|
|
|
{ // scope for the lock
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
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;
|
|
|
|
}
|
2012-08-25 03:26:34 +00:00
|
|
|
int maxBufferCount = getMaxBufferCountLocked();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
if (mSynchronousMode) {
|
|
|
|
// In synchronous mode we queue all buffers in a FIFO.
|
|
|
|
mQueue.push_back(buf);
|
|
|
|
|
|
|
|
// Synchronous mode always signals that an additional frame should
|
|
|
|
// be consumed.
|
2012-03-15 21:01:24 +00:00
|
|
|
listener = mConsumerListener;
|
2012-01-22 23:26:27 +00:00
|
|
|
} else {
|
|
|
|
// In asynchronous mode we only keep the most recent buffer.
|
|
|
|
if (mQueue.empty()) {
|
|
|
|
mQueue.push_back(buf);
|
|
|
|
|
|
|
|
// Asynchronous mode only signals that a frame should be
|
|
|
|
// consumed if no previous frame was pending. If a frame were
|
|
|
|
// pending then the consumer would have already been notified.
|
2012-03-15 21:01:24 +00:00
|
|
|
listener = mConsumerListener;
|
2012-01-22 23:26:27 +00:00
|
|
|
} else {
|
|
|
|
Fifo::iterator front(mQueue.begin());
|
|
|
|
// buffer currently queued is freed
|
|
|
|
mSlots[*front].mBufferState = BufferSlot::FREE;
|
|
|
|
// and we record the new buffer index in the queued list
|
|
|
|
*front = buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-18 02:36:18 +00:00
|
|
|
mSlots[buf].mTimestamp = timestamp;
|
|
|
|
mSlots[buf].mCrop = crop;
|
|
|
|
mSlots[buf].mTransform = transform;
|
2012-06-28 19:52:05 +00:00
|
|
|
mSlots[buf].mFence = fence;
|
2012-04-09 23:14:01 +00:00
|
|
|
|
2012-03-30 00:10:08 +00:00
|
|
|
switch (scalingMode) {
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_FREEZE:
|
|
|
|
case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
|
2012-04-03 22:54:58 +00:00
|
|
|
case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
|
2012-03-30 00:10:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ST_LOGE("unknown scaling mode: %d (ignoring)", scalingMode);
|
|
|
|
scalingMode = mSlots[buf].mScalingMode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
mSlots[buf].mBufferState = BufferSlot::QUEUED;
|
2012-03-30 00:10:08 +00:00
|
|
|
mSlots[buf].mScalingMode = scalingMode;
|
2012-01-22 23:26:27 +00:00
|
|
|
mFrameCounter++;
|
|
|
|
mSlots[buf].mFrameNumber = mFrameCounter;
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-06-28 19:52:05 +00:00
|
|
|
void BufferQueue::cancelBuffer(int buf, 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;
|
|
|
|
}
|
|
|
|
|
2012-08-25 03:26:34 +00:00
|
|
|
int maxBufferCount = getMaxBufferCountLocked();
|
|
|
|
if (buf < 0 || buf >= maxBufferCount) {
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGE("cancelBuffer: 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;
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2012-04-23 21:28:58 +00:00
|
|
|
status_t BufferQueue::connect(int api, QueueBufferOutput* output) {
|
2012-02-24 03:27:23 +00:00
|
|
|
ATRACE_CALL();
|
2012-01-22 23:26:27 +00:00
|
|
|
ST_LOGV("connect: api=%d", api);
|
|
|
|
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;
|
2012-04-21 00:19:28 +00:00
|
|
|
output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint,
|
|
|
|
mQueue.size());
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-22 23:26:27 +00:00
|
|
|
|
|
|
|
mBufferHasBeenQueued = false;
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2012-03-15 21:01:24 +00:00
|
|
|
sp<ConsumerListener> listener;
|
|
|
|
|
|
|
|
{ // 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) {
|
|
|
|
drainQueueAndFreeBuffersLocked();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
void BufferQueue::dump(String8& result) const
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
BufferQueue::dump(result, "", buffer, 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BufferQueue::dump(String8& result, const char* prefix,
|
|
|
|
char* buffer, size_t SIZE) const
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
|
|
|
|
String8 fifo;
|
|
|
|
int fifoSize = 0;
|
|
|
|
Fifo::const_iterator i(mQueue.begin());
|
|
|
|
while (i != mQueue.end()) {
|
|
|
|
snprintf(buffer, SIZE, "%02d ", *i++);
|
|
|
|
fifoSize++;
|
|
|
|
fifo.append(buffer);
|
|
|
|
}
|
|
|
|
|
2012-08-25 03:26:34 +00:00
|
|
|
int maxBufferCount = getMaxBufferCountLocked();
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
snprintf(buffer, SIZE,
|
2012-08-25 03:26:34 +00:00
|
|
|
"%s-BufferQueue maxBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
|
2012-09-14 23:10:11 +00:00
|
|
|
"default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n",
|
2012-08-25 03:26:34 +00:00
|
|
|
prefix, maxBufferCount, mSynchronousMode, mDefaultWidth,
|
2012-09-14 23:10:11 +00:00
|
|
|
mDefaultHeight, mDefaultBufferFormat, mTransformHint,
|
|
|
|
fifoSize, fifo.string());
|
2012-01-22 23:26:27 +00:00
|
|
|
result.append(buffer);
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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]);
|
|
|
|
snprintf(buffer, SIZE,
|
|
|
|
"%s%s[%02d] "
|
|
|
|
"state=%-8s, crop=[%d,%d,%d,%d], "
|
2012-05-10 09:22:33 +00:00
|
|
|
"xform=0x%02x, time=%#llx, scale=%s",
|
2012-01-22 23:26:27 +00:00
|
|
|
prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i,
|
|
|
|
stateName(slot.mBufferState),
|
|
|
|
slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
|
2012-05-10 09:22:33 +00:00
|
|
|
slot.mCrop.bottom, slot.mTransform, slot.mTimestamp,
|
|
|
|
scalingModeName(slot.mScalingMode)
|
2012-01-22 23:26:27 +00:00
|
|
|
);
|
|
|
|
result.append(buffer);
|
|
|
|
|
|
|
|
const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
|
|
|
|
if (buf != NULL) {
|
|
|
|
snprintf(buffer, SIZE,
|
|
|
|
", %p [%4ux%4u:%4u,%3X]",
|
|
|
|
buf->handle, buf->width, buf->height, buf->stride,
|
|
|
|
buf->format);
|
|
|
|
result.append(buffer);
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
ALOGW_IF(!mQueue.isEmpty(),
|
|
|
|
"freeAllBuffersLocked called but mQueue is not empty");
|
2012-01-22 23:26:27 +00:00
|
|
|
mQueue.clear();
|
|
|
|
mBufferHasBeenQueued = false;
|
2012-01-22 23:26:27 +00:00
|
|
|
for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
|
|
|
|
freeBufferLocked(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 21:01:24 +00:00
|
|
|
status_t BufferQueue::acquireBuffer(BufferItem *buffer) {
|
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.
|
|
|
|
if (!mQueue.empty()) {
|
|
|
|
Fifo::iterator front(mQueue.begin());
|
|
|
|
int buf = *front;
|
|
|
|
|
2012-03-02 06:11:25 +00:00
|
|
|
ATRACE_BUFFER_INDEX(buf);
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
if (mSlots[buf].mAcquireCalled) {
|
|
|
|
buffer->mGraphicBuffer = NULL;
|
2012-03-15 21:01:24 +00:00
|
|
|
} else {
|
2012-01-22 23:26:27 +00:00
|
|
|
buffer->mGraphicBuffer = mSlots[buf].mGraphicBuffer;
|
|
|
|
}
|
|
|
|
buffer->mCrop = mSlots[buf].mCrop;
|
|
|
|
buffer->mTransform = mSlots[buf].mTransform;
|
|
|
|
buffer->mScalingMode = mSlots[buf].mScalingMode;
|
|
|
|
buffer->mFrameNumber = mSlots[buf].mFrameNumber;
|
2012-03-02 18:17:34 +00:00
|
|
|
buffer->mTimestamp = mSlots[buf].mTimestamp;
|
2012-01-22 23:26:27 +00:00
|
|
|
buffer->mBuf = buf;
|
2012-06-28 21:27:53 +00:00
|
|
|
buffer->mFence = mSlots[buf].mFence;
|
2012-01-22 23:26:27 +00:00
|
|
|
|
2012-08-03 03:11:05 +00:00
|
|
|
mSlots[buf].mAcquireCalled = true;
|
|
|
|
mSlots[buf].mNeedsCleanupOnRelease = false;
|
2012-01-22 23:26:27 +00:00
|
|
|
mSlots[buf].mBufferState = BufferSlot::ACQUIRED;
|
2012-12-20 22:05:45 +00:00
|
|
|
mSlots[buf].mFence = Fence::NO_FENCE;
|
2012-08-03 03:11:05 +00:00
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
mQueue.erase(front);
|
2012-03-13 04:49:41 +00:00
|
|
|
mDequeueCondition.broadcast();
|
2012-02-24 03:27:23 +00:00
|
|
|
|
|
|
|
ATRACE_INT(mConsumerName.string(), mQueue.size());
|
2012-03-15 21:01:24 +00:00
|
|
|
} else {
|
2012-04-10 05:51:52 +00:00
|
|
|
return NO_BUFFER_AVAILABLE;
|
2012-01-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t BufferQueue::releaseBuffer(int buf, 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-01-22 23:26:27 +00:00
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
mSlots[buf].mEglDisplay = display;
|
2012-06-28 19:52:05 +00:00
|
|
|
mSlots[buf].mEglFence = eglFence;
|
2012-01-22 23:26:27 +00:00
|
|
|
mSlots[buf].mFence = fence;
|
|
|
|
|
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) {
|
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();
|
2012-01-22 23:26:27 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-03-15 21:01:24 +00:00
|
|
|
status_t BufferQueue::consumerConnect(const sp<ConsumerListener>& consumerListener) {
|
|
|
|
ST_LOGV("consumerConnect");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (mAbandoned) {
|
|
|
|
ST_LOGE("consumerConnect: BufferQueue has been abandoned!");
|
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
mConsumerListener = consumerListener;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
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();
|
2012-01-22 23:26:27 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*slotMask = mask;
|
|
|
|
|
|
|
|
ST_LOGV("getReleasedBuffers: returning mask %#x", mask);
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
void BufferQueue::freeAllBuffersExceptHeadLocked() {
|
|
|
|
int head = -1;
|
|
|
|
if (!mQueue.empty()) {
|
|
|
|
Fifo::iterator front(mQueue.begin());
|
|
|
|
head = *front;
|
|
|
|
}
|
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++) {
|
|
|
|
if (i != head) {
|
|
|
|
freeBufferLocked(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t BufferQueue::drainQueueLocked() {
|
2013-03-12 00:59:59 +00:00
|
|
|
while (mSynchronousMode && mQueue.size() > 1) {
|
2012-01-22 23:26:27 +00:00
|
|
|
mDequeueCondition.wait(mMutex);
|
|
|
|
if (mAbandoned) {
|
|
|
|
ST_LOGE("drainQueueLocked: BufferQueue has been abandoned!");
|
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
if (mConnectedApi == NO_CONNECTED_API) {
|
|
|
|
ST_LOGE("drainQueueLocked: BufferQueue is not connected!");
|
|
|
|
return NO_INIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t BufferQueue::drainQueueAndFreeBuffersLocked() {
|
|
|
|
status_t err = drainQueueLocked();
|
|
|
|
if (err == NO_ERROR) {
|
2013-03-12 00:59:59 +00:00
|
|
|
if (mQueue.empty()) {
|
2012-01-22 23:26:27 +00:00
|
|
|
freeAllBuffersLocked();
|
|
|
|
} else {
|
|
|
|
freeAllBuffersExceptHeadLocked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-08-25 00:25:13 +00:00
|
|
|
int BufferQueue::getMinMaxBufferCountLocked() const {
|
2012-08-28 01:48:37 +00:00
|
|
|
return getMinUndequeuedBufferCountLocked() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BufferQueue::getMinUndequeuedBufferCountLocked() const {
|
|
|
|
return mSynchronousMode ? mMaxAcquiredBufferCount :
|
|
|
|
mMaxAcquiredBufferCount + 1;
|
2012-08-25 00:25:13 +00:00
|
|
|
}
|
|
|
|
|
2012-08-25 03:26:34 +00:00
|
|
|
int BufferQueue::getMaxBufferCountLocked() const {
|
|
|
|
int minMaxBufferCount = getMinMaxBufferCountLocked();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-03-15 21:01:24 +00:00
|
|
|
BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
|
|
|
|
const wp<BufferQueue::ConsumerListener>& consumerListener):
|
|
|
|
mConsumerListener(consumerListener) {}
|
|
|
|
|
|
|
|
BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
|
|
|
|
|
|
|
|
void BufferQueue::ProxyConsumerListener::onFrameAvailable() {
|
|
|
|
sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
|
|
|
|
if (listener != NULL) {
|
|
|
|
listener->onFrameAvailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
|
|
|
|
sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote());
|
|
|
|
if (listener != NULL) {
|
|
|
|
listener->onBuffersReleased();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-22 23:26:27 +00:00
|
|
|
}; // namespace android
|