2012-08-03 03:11:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 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 "ConsumerBase"
|
|
|
|
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
|
|
|
|
//#define LOG_NDEBUG 0
|
|
|
|
|
|
|
|
#define EGL_EGLEXT_PROTOTYPES
|
|
|
|
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <EGL/eglext.h>
|
|
|
|
|
|
|
|
#include <hardware/hardware.h>
|
|
|
|
|
|
|
|
#include <gui/IGraphicBufferAlloc.h>
|
|
|
|
#include <gui/ISurfaceComposer.h>
|
|
|
|
#include <gui/SurfaceComposerClient.h>
|
|
|
|
#include <gui/ConsumerBase.h>
|
|
|
|
|
|
|
|
#include <private/gui/ComposerService.h>
|
|
|
|
|
|
|
|
#include <utils/Log.h>
|
|
|
|
#include <utils/String8.h>
|
|
|
|
#include <utils/Trace.h>
|
|
|
|
|
|
|
|
// Macros for including the ConsumerBase name in log messages
|
|
|
|
#define CB_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
|
|
|
|
#define CB_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
|
|
|
|
#define CB_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
|
|
|
|
#define CB_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
|
|
|
|
#define CB_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-08-02 08:40:18 +00:00
|
|
|
ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) :
|
2012-08-08 01:03:04 +00:00
|
|
|
mAbandoned(false),
|
2013-08-02 08:40:18 +00:00
|
|
|
mConsumer(bufferQueue) {
|
2012-08-03 03:11:05 +00:00
|
|
|
// Choose a name using the PID and a process-unique ID.
|
|
|
|
mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
|
|
|
|
|
|
|
|
// Note that we can't create an sp<...>(this) in a ctor that will not keep a
|
|
|
|
// reference once the ctor ends, as that would cause the refcount of 'this'
|
|
|
|
// dropping to 0 at the end of the ctor. Since all we need is a wp<...>
|
|
|
|
// that's what we create.
|
2013-08-01 03:09:53 +00:00
|
|
|
wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
|
|
|
|
sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);
|
2012-08-03 03:11:05 +00:00
|
|
|
|
2013-08-02 08:40:18 +00:00
|
|
|
status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
|
2012-08-03 03:11:05 +00:00
|
|
|
if (err != NO_ERROR) {
|
2012-12-18 17:49:45 +00:00
|
|
|
CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)",
|
2012-08-03 03:11:05 +00:00
|
|
|
strerror(-err), err);
|
|
|
|
} else {
|
2013-08-02 08:40:18 +00:00
|
|
|
mConsumer->setConsumerName(mName);
|
2012-08-03 03:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsumerBase::~ConsumerBase() {
|
2013-04-05 23:41:27 +00:00
|
|
|
CB_LOGV("~ConsumerBase");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
// Verify that abandon() has been called before we get here. This should
|
|
|
|
// be done by ConsumerBase::onLastStrongRef(), but it's possible for a
|
|
|
|
// derived class to override that method and not call
|
|
|
|
// ConsumerBase::onLastStrongRef().
|
|
|
|
LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~ConsumerBase was called, but the "
|
|
|
|
"consumer is not abandoned!", mName.string());
|
|
|
|
}
|
|
|
|
|
2013-11-13 02:02:20 +00:00
|
|
|
void ConsumerBase::onLastStrongRef(const void* id __attribute__((unused))) {
|
2012-08-03 03:11:05 +00:00
|
|
|
abandon();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumerBase::freeBufferLocked(int slotIndex) {
|
|
|
|
CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
|
|
|
|
mSlots[slotIndex].mGraphicBuffer = 0;
|
2012-12-20 22:05:45 +00:00
|
|
|
mSlots[slotIndex].mFence = Fence::NO_FENCE;
|
2013-05-03 21:50:50 +00:00
|
|
|
mSlots[slotIndex].mFrameNumber = 0;
|
2012-08-03 03:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumerBase::onFrameAvailable() {
|
|
|
|
CB_LOGV("onFrameAvailable");
|
|
|
|
|
|
|
|
sp<FrameAvailableListener> listener;
|
|
|
|
{ // scope for the lock
|
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-10-29 20:36:11 +00:00
|
|
|
listener = mFrameAvailableListener.promote();
|
2012-08-03 03:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (listener != NULL) {
|
|
|
|
CB_LOGV("actually calling onFrameAvailable");
|
|
|
|
listener->onFrameAvailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumerBase::onBuffersReleased() {
|
2012-12-07 08:41:56 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-12-07 01:51:53 +00:00
|
|
|
|
2012-12-07 08:41:56 +00:00
|
|
|
CB_LOGV("onBuffersReleased");
|
2012-08-03 03:11:05 +00:00
|
|
|
|
2012-12-07 08:41:56 +00:00
|
|
|
if (mAbandoned) {
|
|
|
|
// Nothing to do if we're already abandoned.
|
|
|
|
return;
|
2012-08-03 03:11:05 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 08:41:56 +00:00
|
|
|
uint32_t mask = 0;
|
2013-08-02 08:40:18 +00:00
|
|
|
mConsumer->getReleasedBuffers(&mask);
|
2012-08-03 03:11:05 +00:00
|
|
|
for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
|
2012-12-07 08:41:56 +00:00
|
|
|
if (mask & (1 << i)) {
|
|
|
|
freeBufferLocked(i);
|
|
|
|
}
|
2012-08-03 03:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 23:42:54 +00:00
|
|
|
void ConsumerBase::onSidebandStreamChanged() {
|
|
|
|
}
|
|
|
|
|
2012-08-03 03:11:05 +00:00
|
|
|
void ConsumerBase::abandon() {
|
|
|
|
CB_LOGV("abandon");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mAbandoned) {
|
|
|
|
abandonLocked();
|
|
|
|
mAbandoned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumerBase::abandonLocked() {
|
|
|
|
CB_LOGV("abandonLocked");
|
|
|
|
for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
|
|
|
|
freeBufferLocked(i);
|
|
|
|
}
|
|
|
|
// disconnect from the BufferQueue
|
2013-08-02 08:40:18 +00:00
|
|
|
mConsumer->consumerDisconnect();
|
|
|
|
mConsumer.clear();
|
2012-08-03 03:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumerBase::setFrameAvailableListener(
|
2012-10-29 20:36:11 +00:00
|
|
|
const wp<FrameAvailableListener>& listener) {
|
2012-08-03 03:11:05 +00:00
|
|
|
CB_LOGV("setFrameAvailableListener");
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mFrameAvailableListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsumerBase::dump(String8& result) const {
|
2013-04-22 14:55:35 +00:00
|
|
|
dump(result, "");
|
2012-08-03 03:11:05 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 14:55:35 +00:00
|
|
|
void ConsumerBase::dump(String8& result, const char* prefix) const {
|
2012-08-03 03:11:05 +00:00
|
|
|
Mutex::Autolock _l(mMutex);
|
2013-04-22 14:55:35 +00:00
|
|
|
dumpLocked(result, prefix);
|
2012-08-03 03:11:05 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 14:55:35 +00:00
|
|
|
void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
|
|
|
|
result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
|
2012-08-03 03:11:05 +00:00
|
|
|
|
|
|
|
if (!mAbandoned) {
|
2013-08-02 08:40:18 +00:00
|
|
|
mConsumer->dump(result, prefix);
|
2012-08-03 03:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-28 20:52:40 +00:00
|
|
|
status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item,
|
|
|
|
nsecs_t presentWhen) {
|
2013-08-02 08:40:18 +00:00
|
|
|
status_t err = mConsumer->acquireBuffer(item, presentWhen);
|
2012-08-03 03:11:05 +00:00
|
|
|
if (err != NO_ERROR) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->mGraphicBuffer != NULL) {
|
|
|
|
mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
mSlots[item->mBuf].mFrameNumber = item->mFrameNumber;
|
2012-09-06 03:09:05 +00:00
|
|
|
mSlots[item->mBuf].mFence = item->mFence;
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
CB_LOGV("acquireBufferLocked: -> slot=%d/%llu",
|
|
|
|
item->mBuf, item->mFrameNumber);
|
2012-08-03 03:11:05 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
status_t ConsumerBase::addReleaseFence(int slot,
|
|
|
|
const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
|
2012-10-05 21:34:21 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2013-05-03 21:50:50 +00:00
|
|
|
return addReleaseFenceLocked(slot, graphicBuffer, fence);
|
2012-10-05 21:34:21 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
status_t ConsumerBase::addReleaseFenceLocked(int slot,
|
|
|
|
const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
|
2012-10-05 21:34:21 +00:00
|
|
|
CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
|
2012-09-06 03:09:05 +00:00
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
// If consumer no longer tracks this graphicBuffer, we can safely
|
|
|
|
// drop this fence, as it will never be received by the producer.
|
|
|
|
if (!stillTracking(slot, graphicBuffer)) {
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-09-06 03:09:05 +00:00
|
|
|
if (!mSlots[slot].mFence.get()) {
|
|
|
|
mSlots[slot].mFence = fence;
|
|
|
|
} else {
|
|
|
|
sp<Fence> mergedFence = Fence::merge(
|
2012-09-24 19:25:15 +00:00
|
|
|
String8::format("%.28s:%d", mName.string(), slot),
|
2012-09-06 03:09:05 +00:00
|
|
|
mSlots[slot].mFence, fence);
|
|
|
|
if (!mergedFence.get()) {
|
|
|
|
CB_LOGE("failed to merge release fences");
|
|
|
|
// synchronization is broken, the best we can do is hope fences
|
|
|
|
// signal in order so the new fence will act like a union
|
|
|
|
mSlots[slot].mFence = fence;
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
mSlots[slot].mFence = mergedFence;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
status_t ConsumerBase::releaseBufferLocked(
|
|
|
|
int slot, const sp<GraphicBuffer> graphicBuffer,
|
|
|
|
EGLDisplay display, EGLSyncKHR eglFence) {
|
|
|
|
// If consumer no longer tracks this graphicBuffer (we received a new
|
|
|
|
// buffer on the same slot), the buffer producer is definitely no longer
|
|
|
|
// tracking it.
|
|
|
|
if (!stillTracking(slot, graphicBuffer)) {
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CB_LOGV("releaseBufferLocked: slot=%d/%llu",
|
|
|
|
slot, mSlots[slot].mFrameNumber);
|
2013-08-02 08:40:18 +00:00
|
|
|
status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
|
2013-05-03 21:50:50 +00:00
|
|
|
display, eglFence, mSlots[slot].mFence);
|
2013-11-13 02:02:20 +00:00
|
|
|
if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
|
2012-08-03 03:11:05 +00:00
|
|
|
freeBufferLocked(slot);
|
|
|
|
}
|
|
|
|
|
2012-12-20 22:05:45 +00:00
|
|
|
mSlots[slot].mFence = Fence::NO_FENCE;
|
2012-08-03 03:11:05 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-05-03 21:50:50 +00:00
|
|
|
bool ConsumerBase::stillTracking(int slot,
|
|
|
|
const sp<GraphicBuffer> graphicBuffer) {
|
|
|
|
if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (mSlots[slot].mGraphicBuffer != NULL &&
|
|
|
|
mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
|
|
|
|
}
|
|
|
|
|
2012-12-18 17:49:45 +00:00
|
|
|
} // namespace android
|