2012-06-08 00:52:54 +00:00
|
|
|
/*
|
|
|
|
**
|
2012-08-03 03:11:05 +00:00
|
|
|
** Copyright 2012 The Android Open Source Project
|
2012-06-08 00:52:54 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <cutils/log.h>
|
|
|
|
|
|
|
|
#include <utils/String8.h>
|
|
|
|
|
|
|
|
#include <ui/Rect.h>
|
|
|
|
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
|
|
|
|
#include <hardware/hardware.h>
|
2013-02-15 01:11:02 +00:00
|
|
|
#include <gui/Surface.h>
|
2012-11-30 07:26:29 +00:00
|
|
|
#include <gui/GraphicBufferAlloc.h>
|
2012-06-08 00:52:54 +00:00
|
|
|
#include <ui/GraphicBuffer.h>
|
|
|
|
|
2013-04-01 23:54:58 +00:00
|
|
|
#include "FramebufferSurface.h"
|
|
|
|
#include "HWComposer.h"
|
2012-06-08 00:52:54 +00:00
|
|
|
|
2012-10-12 21:18:10 +00:00
|
|
|
#ifndef NUM_FRAMEBUFFER_SURFACE_BUFFERS
|
|
|
|
#define NUM_FRAMEBUFFER_SURFACE_BUFFERS (2)
|
|
|
|
#endif
|
|
|
|
|
2012-06-08 00:52:54 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This implements the (main) framebuffer management. This class is used
|
|
|
|
* mostly by SurfaceFlinger, but also by command line GL application.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-09-20 01:16:22 +00:00
|
|
|
FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp) :
|
2013-07-17 05:56:09 +00:00
|
|
|
ConsumerBase(new BufferQueue(new GraphicBufferAlloc())),
|
2012-09-20 01:16:22 +00:00
|
|
|
mDisplayType(disp),
|
2012-08-03 03:11:05 +00:00
|
|
|
mCurrentBufferSlot(-1),
|
2012-09-10 21:08:09 +00:00
|
|
|
mCurrentBuffer(0),
|
|
|
|
mHwc(hwc)
|
2012-06-08 00:52:54 +00:00
|
|
|
{
|
2012-09-10 21:08:09 +00:00
|
|
|
mName = "FramebufferSurface";
|
|
|
|
mBufferQueue->setConsumerName(mName);
|
|
|
|
mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
|
2012-09-20 01:16:22 +00:00
|
|
|
GRALLOC_USAGE_HW_RENDER |
|
|
|
|
GRALLOC_USAGE_HW_COMPOSER);
|
|
|
|
mBufferQueue->setDefaultBufferFormat(mHwc.getFormat(disp));
|
|
|
|
mBufferQueue->setDefaultBufferSize(mHwc.getWidth(disp), mHwc.getHeight(disp));
|
2012-10-12 21:18:10 +00:00
|
|
|
mBufferQueue->setDefaultMaxBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS);
|
2012-08-03 03:11:05 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 21:29:29 +00:00
|
|
|
sp<IGraphicBufferProducer> FramebufferSurface::getIGraphicBufferProducer() const {
|
|
|
|
return getBufferQueue();
|
|
|
|
}
|
|
|
|
|
2013-04-07 06:12:29 +00:00
|
|
|
status_t FramebufferSurface::prepareFrame(CompositionType compositionType) {
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-03-14 21:29:29 +00:00
|
|
|
status_t FramebufferSurface::advanceFrame() {
|
|
|
|
// Once we remove FB HAL support, we can call nextBuffer() from here
|
|
|
|
// instead of using onFrameAvailable(). No real benefit, except it'll be
|
|
|
|
// more like VirtualDisplaySurface.
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-09-14 01:17:13 +00:00
|
|
|
status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
|
2012-08-03 03:11:05 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-06-08 00:52:54 +00:00
|
|
|
|
2012-08-03 03:11:05 +00:00
|
|
|
BufferQueue::BufferItem item;
|
2013-06-28 20:52:40 +00:00
|
|
|
status_t err = acquireBufferLocked(&item, 0);
|
2012-08-03 03:11:05 +00:00
|
|
|
if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
|
2012-09-14 01:17:13 +00:00
|
|
|
outBuffer = mCurrentBuffer;
|
2012-08-03 03:11:05 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
} else if (err != NO_ERROR) {
|
|
|
|
ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
|
|
|
|
return err;
|
|
|
|
}
|
2012-06-08 00:52:54 +00:00
|
|
|
|
2012-08-03 03:11:05 +00:00
|
|
|
// If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
|
|
|
|
// then we may have acquired the slot we already own. If we had released
|
|
|
|
// our current buffer before we call acquireBuffer then that release call
|
|
|
|
// would have returned STALE_BUFFER_SLOT, and we would have called
|
|
|
|
// freeBufferLocked on that slot. Because the buffer slot has already
|
|
|
|
// been overwritten with the new buffer all we have to do is skip the
|
|
|
|
// releaseBuffer call and we should be in the same state we'd be in if we
|
|
|
|
// had released the old buffer first.
|
|
|
|
if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
|
|
|
|
item.mBuf != mCurrentBufferSlot) {
|
|
|
|
// Release the previous buffer.
|
2013-05-03 21:50:50 +00:00
|
|
|
err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer,
|
|
|
|
EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
|
2013-07-24 00:28:53 +00:00
|
|
|
if (err < NO_ERROR) {
|
2012-08-03 03:11:05 +00:00
|
|
|
ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
|
|
|
|
return err;
|
2012-06-08 00:52:54 +00:00
|
|
|
}
|
2012-08-03 03:11:05 +00:00
|
|
|
}
|
|
|
|
mCurrentBufferSlot = item.mBuf;
|
|
|
|
mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
|
2012-09-14 01:17:13 +00:00
|
|
|
outFence = item.mFence;
|
|
|
|
outBuffer = mCurrentBuffer;
|
2012-08-03 03:11:05 +00:00
|
|
|
return NO_ERROR;
|
2012-06-08 00:52:54 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 21:08:09 +00:00
|
|
|
// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
|
2012-08-03 03:11:05 +00:00
|
|
|
void FramebufferSurface::onFrameAvailable() {
|
|
|
|
sp<GraphicBuffer> buf;
|
2012-09-14 01:17:13 +00:00
|
|
|
sp<Fence> acquireFence;
|
|
|
|
status_t err = nextBuffer(buf, acquireFence);
|
2012-08-03 03:11:05 +00:00
|
|
|
if (err != NO_ERROR) {
|
2012-09-14 01:17:13 +00:00
|
|
|
ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
|
2012-08-03 03:11:05 +00:00
|
|
|
strerror(-err), err);
|
|
|
|
return;
|
|
|
|
}
|
2012-09-20 01:16:22 +00:00
|
|
|
err = mHwc.fbPost(mDisplayType, acquireFence, buf);
|
2012-08-03 03:11:05 +00:00
|
|
|
if (err != NO_ERROR) {
|
|
|
|
ALOGE("error posting framebuffer: %d", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FramebufferSurface::freeBufferLocked(int slotIndex) {
|
|
|
|
ConsumerBase::freeBufferLocked(slotIndex);
|
|
|
|
if (slotIndex == mCurrentBufferSlot) {
|
|
|
|
mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-20 20:44:00 +00:00
|
|
|
void FramebufferSurface::onFrameCommitted() {
|
|
|
|
sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType);
|
2013-03-20 18:37:21 +00:00
|
|
|
if (fence->isValid() &&
|
|
|
|
mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
|
2013-05-03 21:50:50 +00:00
|
|
|
status_t err = addReleaseFence(mCurrentBufferSlot,
|
|
|
|
mCurrentBuffer, fence);
|
2013-03-20 18:37:21 +00:00
|
|
|
ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
|
|
|
|
strerror(-err), err);
|
2012-09-14 01:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 00:52:54 +00:00
|
|
|
status_t FramebufferSurface::compositionComplete()
|
|
|
|
{
|
2012-09-10 21:08:09 +00:00
|
|
|
return mHwc.fbCompositionComplete();
|
2012-06-08 00:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 21:29:29 +00:00
|
|
|
// Since DisplaySurface and ConsumerBase both have a method with this
|
|
|
|
// signature, results will vary based on the static pointer type the caller is
|
|
|
|
// using:
|
|
|
|
// void dump(FrameBufferSurface* fbs, String8& s) {
|
|
|
|
// // calls FramebufferSurface::dump()
|
|
|
|
// fbs->dump(s);
|
|
|
|
//
|
|
|
|
// // calls ConsumerBase::dump() since it is non-virtual
|
|
|
|
// static_cast<ConsumerBase*>(fbs)->dump(s);
|
|
|
|
//
|
|
|
|
// // calls FramebufferSurface::dump() since it is virtual
|
|
|
|
// static_cast<DisplaySurface*>(fbs)->dump(s);
|
|
|
|
// }
|
|
|
|
// To make sure that all of these end up doing the same thing, we just redirect
|
|
|
|
// to ConsumerBase::dump() here. It will take the internal lock, and then call
|
|
|
|
// virtual dumpLocked(), which is where the real work happens.
|
|
|
|
void FramebufferSurface::dump(String8& result) const {
|
2012-08-03 03:11:05 +00:00
|
|
|
ConsumerBase::dump(result);
|
2012-06-08 00:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 14:55:35 +00:00
|
|
|
void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
|
2013-03-07 00:13:49 +00:00
|
|
|
{
|
|
|
|
mHwc.fbDump(result);
|
2013-04-22 14:55:35 +00:00
|
|
|
ConsumerBase::dumpLocked(result, prefix);
|
2013-03-07 00:13:49 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 00:52:54 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|
|
|
|
// ----------------------------------------------------------------------------
|