2014-02-20 14:41:45 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
2014-02-20 14:42:17 +04:00
|
|
|
* Copyright (C) 2013 Mozilla Foundation
|
2014-02-20 14:41:45 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
#define LOG_TAG "GonkConsumerBase"
|
2014-02-20 14:41:45 +04:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
|
|
|
|
//#define LOG_NDEBUG 0
|
|
|
|
|
|
|
|
#define EGL_EGLEXT_PROTOTYPES
|
|
|
|
|
|
|
|
#include <hardware/hardware.h>
|
|
|
|
|
|
|
|
#include <gui/IGraphicBufferAlloc.h>
|
|
|
|
#include <utils/Log.h>
|
|
|
|
#include <utils/String8.h>
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
#include "GonkConsumerBaseKK.h"
|
|
|
|
|
2014-02-20 14:41:45 +04: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);
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
GonkConsumerBase::GonkConsumerBase(const sp<GonkBufferQueue>& bufferQueue, bool controlledByApp) :
|
2014-02-20 14:41:45 +04:00
|
|
|
mAbandoned(false),
|
|
|
|
mConsumer(bufferQueue) {
|
|
|
|
// 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.
|
|
|
|
wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
|
2014-02-25 11:51:46 +04:00
|
|
|
sp<IConsumerListener> proxy = new GonkBufferQueue::ProxyConsumerListener(listener);
|
2014-02-20 14:41:45 +04:00
|
|
|
|
|
|
|
status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
|
|
|
|
if (err != NO_ERROR) {
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGE("GonkConsumerBase: error connecting to GonkBufferQueue: %s (%d)",
|
2014-02-20 14:41:45 +04:00
|
|
|
strerror(-err), err);
|
|
|
|
} else {
|
|
|
|
mConsumer->setConsumerName(mName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
GonkConsumerBase::~GonkConsumerBase() {
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGV("~GonkConsumerBase");
|
2014-02-20 14:41:45 +04:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
// Verify that abandon() has been called before we get here. This should
|
2014-02-20 14:42:17 +04:00
|
|
|
// be done by GonkConsumerBase::onLastStrongRef(), but it's possible for a
|
2014-02-20 14:41:45 +04:00
|
|
|
// derived class to override that method and not call
|
2014-02-20 14:42:17 +04:00
|
|
|
// GonkConsumerBase::onLastStrongRef().
|
|
|
|
LOG_ALWAYS_FATAL_IF(!mAbandoned, "[%s] ~GonkConsumerBase was called, but the "
|
2014-02-20 14:41:45 +04:00
|
|
|
"consumer is not abandoned!", mName.string());
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
void GonkConsumerBase::onLastStrongRef(const void* id) {
|
2014-02-20 14:41:45 +04:00
|
|
|
abandon();
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
void GonkConsumerBase::freeBufferLocked(int slotIndex) {
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGV("freeBufferLocked: slotIndex=%d", slotIndex);
|
2014-02-20 14:41:45 +04:00
|
|
|
mSlots[slotIndex].mGraphicBuffer = 0;
|
|
|
|
mSlots[slotIndex].mFence = Fence::NO_FENCE;
|
|
|
|
mSlots[slotIndex].mFrameNumber = 0;
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
// Used for refactoring, should not be in final interface
|
|
|
|
sp<GonkBufferQueue> GonkConsumerBase::getBufferQueue() const {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return mConsumer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GonkConsumerBase::onFrameAvailable() {
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGV("onFrameAvailable");
|
2014-02-20 14:41:45 +04:00
|
|
|
|
|
|
|
sp<FrameAvailableListener> listener;
|
|
|
|
{ // scope for the lock
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
listener = mFrameAvailableListener.promote();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listener != NULL) {
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGV("actually calling onFrameAvailable");
|
2014-02-20 14:41:45 +04:00
|
|
|
listener->onFrameAvailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
void GonkConsumerBase::onBuffersReleased() {
|
2014-02-20 14:41:45 +04:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGV("onBuffersReleased");
|
2014-02-20 14:41:45 +04:00
|
|
|
|
|
|
|
if (mAbandoned) {
|
|
|
|
// Nothing to do if we're already abandoned.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t mask = 0;
|
|
|
|
mConsumer->getReleasedBuffers(&mask);
|
2014-02-20 14:42:17 +04:00
|
|
|
for (int i = 0; i < GonkBufferQueue::NUM_BUFFER_SLOTS; i++) {
|
2014-02-20 14:41:45 +04:00
|
|
|
if (mask & (1 << i)) {
|
|
|
|
freeBufferLocked(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
void GonkConsumerBase::abandon() {
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGV("abandon");
|
2014-02-20 14:41:45 +04:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mAbandoned) {
|
|
|
|
abandonLocked();
|
|
|
|
mAbandoned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
void GonkConsumerBase::abandonLocked() {
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGV("abandonLocked");
|
2014-02-20 14:42:17 +04:00
|
|
|
for (int i =0; i < GonkBufferQueue::NUM_BUFFER_SLOTS; i++) {
|
2014-02-20 14:41:45 +04:00
|
|
|
freeBufferLocked(i);
|
|
|
|
}
|
|
|
|
// disconnect from the BufferQueue
|
|
|
|
mConsumer->consumerDisconnect();
|
|
|
|
mConsumer.clear();
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
void GonkConsumerBase::setFrameAvailableListener(
|
2014-02-20 14:41:45 +04:00
|
|
|
const wp<FrameAvailableListener>& listener) {
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGV("setFrameAvailableListener");
|
2014-02-20 14:41:45 +04:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mFrameAvailableListener = listener;
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
void GonkConsumerBase::dump(String8& result) const {
|
2014-02-20 14:41:45 +04:00
|
|
|
dump(result, "");
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
void GonkConsumerBase::dump(String8& result, const char* prefix) const {
|
2014-02-20 14:41:45 +04:00
|
|
|
Mutex::Autolock _l(mMutex);
|
|
|
|
dumpLocked(result, prefix);
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
void GonkConsumerBase::dumpLocked(String8& result, const char* prefix) const {
|
2014-02-20 14:41:45 +04:00
|
|
|
result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
|
|
|
|
|
|
|
|
if (!mAbandoned) {
|
2014-11-25 04:03:27 +03:00
|
|
|
mConsumer->dumpToString(result, prefix);
|
2014-02-20 14:41:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
status_t GonkConsumerBase::acquireBufferLocked(IGonkGraphicBufferConsumer::BufferItem *item,
|
2014-02-20 14:41:45 +04:00
|
|
|
nsecs_t presentWhen) {
|
|
|
|
status_t err = mConsumer->acquireBuffer(item, presentWhen);
|
|
|
|
if (err != NO_ERROR) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->mGraphicBuffer != NULL) {
|
|
|
|
mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSlots[item->mBuf].mFrameNumber = item->mFrameNumber;
|
|
|
|
mSlots[item->mBuf].mFence = item->mFence;
|
|
|
|
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGV("acquireBufferLocked: -> slot=%d", item->mBuf);
|
2014-02-20 14:41:45 +04:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
status_t GonkConsumerBase::addReleaseFence(int slot,
|
2014-02-20 14:41:45 +04:00
|
|
|
const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
return addReleaseFenceLocked(slot, graphicBuffer, fence);
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
status_t GonkConsumerBase::addReleaseFenceLocked(int slot,
|
2014-02-20 14:41:45 +04:00
|
|
|
const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGV("addReleaseFenceLocked: slot=%d", slot);
|
2014-02-20 14:41:45 +04: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSlots[slot].mFence.get()) {
|
|
|
|
mSlots[slot].mFence = fence;
|
|
|
|
} else {
|
|
|
|
sp<Fence> mergedFence = Fence::merge(
|
|
|
|
String8::format("%.28s:%d", mName.string(), slot),
|
|
|
|
mSlots[slot].mFence, fence);
|
|
|
|
if (!mergedFence.get()) {
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGE("failed to merge release fences");
|
2014-02-20 14:41:45 +04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-02-25 11:51:46 +04:00
|
|
|
status_t GonkConsumerBase::releaseBufferLocked(int slot, const sp<GraphicBuffer> graphicBuffer) {
|
2014-02-20 14:41:45 +04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-07-03 18:24:52 +04:00
|
|
|
ALOGV("releaseBufferLocked: slot=%d/%llu",
|
2014-02-20 14:41:45 +04:00
|
|
|
slot, mSlots[slot].mFrameNumber);
|
2014-02-25 11:51:46 +04:00
|
|
|
status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber, mSlots[slot].mFence);
|
2014-02-20 14:42:17 +04:00
|
|
|
if (err == GonkBufferQueue::STALE_BUFFER_SLOT) {
|
2014-02-20 14:41:45 +04:00
|
|
|
freeBufferLocked(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
mSlots[slot].mFence = Fence::NO_FENCE;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:17 +04:00
|
|
|
bool GonkConsumerBase::stillTracking(int slot,
|
2014-02-20 14:41:45 +04:00
|
|
|
const sp<GraphicBuffer> graphicBuffer) {
|
2014-02-25 11:51:46 +04:00
|
|
|
if (slot < 0 || slot >= GonkBufferQueue::NUM_BUFFER_SLOTS) {
|
2014-02-20 14:41:45 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (mSlots[slot].mGraphicBuffer != NULL &&
|
|
|
|
mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace android
|