2012-04-17 00:54:33 +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_NDEBUG 0
|
|
|
|
#define LOG_TAG "CpuConsumer"
|
2014-11-18 18:24:03 +00:00
|
|
|
//#define ATRACE_TAG ATRACE_TAG_GRAPHICS
|
2012-04-17 00:54:33 +00:00
|
|
|
|
2013-03-01 02:23:24 +00:00
|
|
|
#include <cutils/compiler.h>
|
|
|
|
#include <utils/Log.h>
|
2015-03-12 20:58:47 +00:00
|
|
|
#include <gui/BufferItem.h>
|
2012-04-17 00:54:33 +00:00
|
|
|
#include <gui/CpuConsumer.h>
|
|
|
|
|
2015-01-29 18:55:21 +00:00
|
|
|
#define CC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
|
2014-11-18 18:24:03 +00:00
|
|
|
//#define CC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
|
|
|
|
//#define CC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
|
2014-09-09 01:53:39 +00:00
|
|
|
#define CC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
|
|
|
|
#define CC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
|
2012-04-17 00:54:33 +00:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
2013-08-02 08:40:18 +00:00
|
|
|
CpuConsumer::CpuConsumer(const sp<IGraphicBufferConsumer>& bq,
|
2014-11-18 18:24:03 +00:00
|
|
|
size_t maxLockedBuffers, bool controlledByApp) :
|
2013-07-17 05:56:09 +00:00
|
|
|
ConsumerBase(bq, controlledByApp),
|
2012-04-17 00:54:33 +00:00
|
|
|
mMaxLockedBuffers(maxLockedBuffers),
|
|
|
|
mCurrentLockedBuffers(0)
|
|
|
|
{
|
2013-03-01 02:23:24 +00:00
|
|
|
// Create tracking entries for locked buffers
|
|
|
|
mAcquiredBuffers.insertAt(0, maxLockedBuffers);
|
2012-04-17 00:54:33 +00:00
|
|
|
|
2013-08-02 08:40:18 +00:00
|
|
|
mConsumer->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN);
|
2014-11-18 18:24:03 +00:00
|
|
|
mConsumer->setMaxAcquiredBufferCount(static_cast<int32_t>(maxLockedBuffers));
|
2012-04-17 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2012-08-21 20:37:35 +00:00
|
|
|
CpuConsumer::~CpuConsumer() {
|
2013-03-01 02:23:24 +00:00
|
|
|
// ConsumerBase destructor does all the work.
|
2012-08-21 20:37:35 +00:00
|
|
|
}
|
|
|
|
|
2013-03-01 02:23:24 +00:00
|
|
|
|
|
|
|
|
2012-04-17 00:54:33 +00:00
|
|
|
void CpuConsumer::setName(const String8& name) {
|
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
mName = name;
|
2013-08-02 08:40:18 +00:00
|
|
|
mConsumer->setConsumerName(name);
|
2012-04-17 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 18:59:21 +00:00
|
|
|
status_t CpuConsumer::setDefaultBufferSize(uint32_t width, uint32_t height)
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mMutex);
|
2013-08-02 08:40:18 +00:00
|
|
|
return mConsumer->setDefaultBufferSize(width, height);
|
2013-06-06 18:59:21 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
status_t CpuConsumer::setDefaultBufferFormat(PixelFormat defaultFormat)
|
2013-06-06 18:59:21 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mMutex);
|
2013-08-02 08:40:18 +00:00
|
|
|
return mConsumer->setDefaultBufferFormat(defaultFormat);
|
2013-06-06 18:59:21 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 00:10:43 +00:00
|
|
|
status_t CpuConsumer::setDefaultBufferDataSpace(
|
|
|
|
android_dataspace defaultDataSpace)
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
|
|
|
|
}
|
|
|
|
|
2015-01-23 00:37:37 +00:00
|
|
|
static bool isPossiblyYUV(PixelFormat format) {
|
2015-01-29 18:55:21 +00:00
|
|
|
switch (static_cast<int>(format)) {
|
2015-01-23 00:37:37 +00:00
|
|
|
case HAL_PIXEL_FORMAT_RGBA_8888:
|
|
|
|
case HAL_PIXEL_FORMAT_RGBX_8888:
|
|
|
|
case HAL_PIXEL_FORMAT_RGB_888:
|
|
|
|
case HAL_PIXEL_FORMAT_RGB_565:
|
|
|
|
case HAL_PIXEL_FORMAT_BGRA_8888:
|
|
|
|
case HAL_PIXEL_FORMAT_Y8:
|
|
|
|
case HAL_PIXEL_FORMAT_Y16:
|
2015-02-20 00:10:43 +00:00
|
|
|
case HAL_PIXEL_FORMAT_RAW16:
|
2015-01-23 00:37:37 +00:00
|
|
|
case HAL_PIXEL_FORMAT_RAW10:
|
|
|
|
case HAL_PIXEL_FORMAT_RAW_OPAQUE:
|
|
|
|
case HAL_PIXEL_FORMAT_BLOB:
|
|
|
|
case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case HAL_PIXEL_FORMAT_YV12:
|
|
|
|
case HAL_PIXEL_FORMAT_YCbCr_420_888:
|
|
|
|
case HAL_PIXEL_FORMAT_YCbCr_422_SP:
|
|
|
|
case HAL_PIXEL_FORMAT_YCrCb_420_SP:
|
|
|
|
case HAL_PIXEL_FORMAT_YCbCr_422_I:
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-17 00:54:33 +00:00
|
|
|
status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) {
|
|
|
|
status_t err;
|
|
|
|
|
|
|
|
if (!nativeBuffer) return BAD_VALUE;
|
|
|
|
if (mCurrentLockedBuffers == mMaxLockedBuffers) {
|
2014-11-18 18:24:03 +00:00
|
|
|
CC_LOGW("Max buffers have been locked (%zd), cannot lock anymore.",
|
2013-08-15 01:49:12 +00:00
|
|
|
mMaxLockedBuffers);
|
|
|
|
return NOT_ENOUGH_DATA;
|
2012-04-17 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 20:58:47 +00:00
|
|
|
BufferItem b;
|
2012-04-17 00:54:33 +00:00
|
|
|
|
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
|
2013-06-28 20:52:40 +00:00
|
|
|
err = acquireBufferLocked(&b, 0);
|
2012-04-17 00:54:33 +00:00
|
|
|
if (err != OK) {
|
|
|
|
if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
|
|
|
|
return BAD_VALUE;
|
|
|
|
} else {
|
|
|
|
CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int buf = b.mBuf;
|
|
|
|
|
2013-02-28 22:08:34 +00:00
|
|
|
void *bufferPointer = NULL;
|
2013-05-05 01:07:43 +00:00
|
|
|
android_ycbcr ycbcr = android_ycbcr();
|
|
|
|
|
2015-01-23 00:37:37 +00:00
|
|
|
PixelFormat format = mSlots[buf].mGraphicBuffer->getPixelFormat();
|
|
|
|
PixelFormat flexFormat = format;
|
|
|
|
if (isPossiblyYUV(format)) {
|
|
|
|
if (b.mFence.get()) {
|
2014-08-18 23:57:11 +00:00
|
|
|
err = mSlots[buf].mGraphicBuffer->lockAsyncYCbCr(
|
|
|
|
GraphicBuffer::USAGE_SW_READ_OFTEN,
|
|
|
|
b.mCrop,
|
|
|
|
&ycbcr,
|
|
|
|
b.mFence->dup());
|
|
|
|
} else {
|
2015-01-23 00:37:37 +00:00
|
|
|
err = mSlots[buf].mGraphicBuffer->lockYCbCr(
|
2014-08-18 23:57:11 +00:00
|
|
|
GraphicBuffer::USAGE_SW_READ_OFTEN,
|
|
|
|
b.mCrop,
|
2015-01-23 00:37:37 +00:00
|
|
|
&ycbcr);
|
|
|
|
}
|
|
|
|
if (err == OK) {
|
|
|
|
bufferPointer = ycbcr.y;
|
|
|
|
flexFormat = HAL_PIXEL_FORMAT_YCbCr_420_888;
|
|
|
|
if (format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
|
|
|
|
CC_LOGV("locking buffer of format %#x as flex YUV", format);
|
2014-08-18 23:57:11 +00:00
|
|
|
}
|
2015-01-23 00:37:37 +00:00
|
|
|
} else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
|
|
|
|
CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)",
|
|
|
|
strerror(-err), err);
|
|
|
|
return err;
|
2013-05-05 01:07:43 +00:00
|
|
|
}
|
2015-01-23 00:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bufferPointer == NULL) { // not flexible YUV
|
|
|
|
if (b.mFence.get()) {
|
|
|
|
err = mSlots[buf].mGraphicBuffer->lockAsync(
|
2014-08-18 23:57:11 +00:00
|
|
|
GraphicBuffer::USAGE_SW_READ_OFTEN,
|
|
|
|
b.mCrop,
|
2015-01-23 00:37:37 +00:00
|
|
|
&bufferPointer,
|
|
|
|
b.mFence->dup());
|
2014-08-18 23:57:11 +00:00
|
|
|
} else {
|
|
|
|
err = mSlots[buf].mGraphicBuffer->lock(
|
|
|
|
GraphicBuffer::USAGE_SW_READ_OFTEN,
|
|
|
|
b.mCrop,
|
|
|
|
&bufferPointer);
|
2015-01-23 00:37:37 +00:00
|
|
|
}
|
|
|
|
if (err != OK) {
|
|
|
|
CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)",
|
|
|
|
strerror(-err), err);
|
|
|
|
return err;
|
2013-05-05 01:07:43 +00:00
|
|
|
}
|
2012-04-17 00:54:33 +00:00
|
|
|
}
|
2013-05-05 01:07:43 +00:00
|
|
|
|
2013-03-01 02:23:24 +00:00
|
|
|
size_t lockedIdx = 0;
|
2014-11-18 18:24:03 +00:00
|
|
|
for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
|
2013-03-01 02:23:24 +00:00
|
|
|
if (mAcquiredBuffers[lockedIdx].mSlot ==
|
|
|
|
BufferQueue::INVALID_BUFFER_SLOT) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(lockedIdx < mMaxLockedBuffers);
|
|
|
|
|
|
|
|
AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
|
|
|
|
ab.mSlot = buf;
|
|
|
|
ab.mBufferPointer = bufferPointer;
|
|
|
|
ab.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
|
2012-04-17 00:54:33 +00:00
|
|
|
|
2013-02-28 22:08:34 +00:00
|
|
|
nativeBuffer->data =
|
|
|
|
reinterpret_cast<uint8_t*>(bufferPointer);
|
2012-08-20 22:44:40 +00:00
|
|
|
nativeBuffer->width = mSlots[buf].mGraphicBuffer->getWidth();
|
|
|
|
nativeBuffer->height = mSlots[buf].mGraphicBuffer->getHeight();
|
2015-01-23 00:37:37 +00:00
|
|
|
nativeBuffer->format = format;
|
|
|
|
nativeBuffer->flexFormat = flexFormat;
|
2013-05-05 01:07:43 +00:00
|
|
|
nativeBuffer->stride = (ycbcr.y != NULL) ?
|
2014-11-18 18:24:03 +00:00
|
|
|
static_cast<uint32_t>(ycbcr.ystride) :
|
2013-05-05 01:07:43 +00:00
|
|
|
mSlots[buf].mGraphicBuffer->getStride();
|
2012-04-17 00:54:33 +00:00
|
|
|
|
|
|
|
nativeBuffer->crop = b.mCrop;
|
|
|
|
nativeBuffer->transform = b.mTransform;
|
|
|
|
nativeBuffer->scalingMode = b.mScalingMode;
|
|
|
|
nativeBuffer->timestamp = b.mTimestamp;
|
2015-02-20 00:10:43 +00:00
|
|
|
nativeBuffer->dataSpace = b.mDataSpace;
|
2012-04-17 00:54:33 +00:00
|
|
|
nativeBuffer->frameNumber = b.mFrameNumber;
|
|
|
|
|
2013-05-05 01:07:43 +00:00
|
|
|
nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
|
|
|
|
nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
|
2014-11-18 18:24:03 +00:00
|
|
|
nativeBuffer->chromaStride = static_cast<uint32_t>(ycbcr.cstride);
|
|
|
|
nativeBuffer->chromaStep = static_cast<uint32_t>(ycbcr.chroma_step);
|
2013-05-05 01:07:43 +00:00
|
|
|
|
2012-04-17 00:54:33 +00:00
|
|
|
mCurrentLockedBuffers++;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) {
|
|
|
|
Mutex::Autolock _l(mMutex);
|
2013-03-01 02:23:24 +00:00
|
|
|
size_t lockedIdx = 0;
|
2012-04-17 00:54:33 +00:00
|
|
|
|
|
|
|
void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data);
|
2014-11-18 18:24:03 +00:00
|
|
|
for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
|
2013-03-01 02:23:24 +00:00
|
|
|
if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break;
|
2012-04-17 00:54:33 +00:00
|
|
|
}
|
2013-03-01 02:23:24 +00:00
|
|
|
if (lockedIdx == mMaxLockedBuffers) {
|
2012-04-17 00:54:33 +00:00
|
|
|
CC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2013-03-01 02:23:24 +00:00
|
|
|
return releaseAcquiredBufferLocked(lockedIdx);
|
|
|
|
}
|
|
|
|
|
2014-11-18 18:24:03 +00:00
|
|
|
status_t CpuConsumer::releaseAcquiredBufferLocked(size_t lockedIdx) {
|
2013-03-01 02:23:24 +00:00
|
|
|
status_t err;
|
2014-08-18 23:57:11 +00:00
|
|
|
int fd = -1;
|
2013-03-01 02:23:24 +00:00
|
|
|
|
2014-08-18 23:57:11 +00:00
|
|
|
err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlockAsync(&fd);
|
2012-04-17 00:54:33 +00:00
|
|
|
if (err != OK) {
|
2014-11-18 18:24:03 +00:00
|
|
|
CC_LOGE("%s: Unable to unlock graphic buffer %zd", __FUNCTION__,
|
2013-03-01 02:23:24 +00:00
|
|
|
lockedIdx);
|
2012-04-17 00:54:33 +00:00
|
|
|
return err;
|
|
|
|
}
|
2013-03-01 02:23:24 +00:00
|
|
|
int buf = mAcquiredBuffers[lockedIdx].mSlot;
|
2014-08-18 23:57:11 +00:00
|
|
|
if (CC_LIKELY(fd != -1)) {
|
|
|
|
sp<Fence> fence(new Fence(fd));
|
|
|
|
addReleaseFenceLocked(
|
|
|
|
mAcquiredBuffers[lockedIdx].mSlot,
|
|
|
|
mSlots[buf].mGraphicBuffer,
|
|
|
|
fence);
|
|
|
|
}
|
2013-03-01 02:23:24 +00:00
|
|
|
|
|
|
|
// release the buffer if it hasn't already been freed by the BufferQueue.
|
|
|
|
// This can happen, for example, when the producer of this buffer
|
|
|
|
// disconnected after this buffer was acquired.
|
|
|
|
if (CC_LIKELY(mAcquiredBuffers[lockedIdx].mGraphicBuffer ==
|
|
|
|
mSlots[buf].mGraphicBuffer)) {
|
2013-05-03 21:50:50 +00:00
|
|
|
releaseBufferLocked(
|
|
|
|
buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer,
|
|
|
|
EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
|
2013-03-01 02:23:24 +00:00
|
|
|
}
|
2012-04-17 00:54:33 +00:00
|
|
|
|
2013-03-01 02:23:24 +00:00
|
|
|
AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
|
|
|
|
ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT;
|
|
|
|
ab.mBufferPointer = NULL;
|
|
|
|
ab.mGraphicBuffer.clear();
|
2012-04-17 00:54:33 +00:00
|
|
|
|
2013-03-01 02:23:24 +00:00
|
|
|
mCurrentLockedBuffers--;
|
2012-04-17 00:54:33 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-08-20 22:44:40 +00:00
|
|
|
void CpuConsumer::freeBufferLocked(int slotIndex) {
|
|
|
|
ConsumerBase::freeBufferLocked(slotIndex);
|
2012-04-17 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace android
|