520 lines
14 KiB
C++
520 lines
14 KiB
C++
/*
|
|
* Copyright (C) 2007 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.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <math.h>
|
|
#include <sys/types.h>
|
|
|
|
#include <utils/Errors.h>
|
|
#include <utils/Log.h>
|
|
#include <utils/StopWatch.h>
|
|
|
|
#include <ui/PixelFormat.h>
|
|
|
|
#include "LayerBuffer.h"
|
|
#include "SurfaceFlinger.h"
|
|
#include "DisplayHardware/DisplayHardware.h"
|
|
|
|
|
|
namespace android {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const uint32_t LayerBuffer::typeInfo = LayerBaseClient::typeInfo | 0x20;
|
|
const char* const LayerBuffer::typeID = "LayerBuffer";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
LayerBuffer::LayerBuffer(SurfaceFlinger* flinger, DisplayID display,
|
|
const sp<Client>& client, int32_t i)
|
|
: LayerBaseClient(flinger, display, client, i),
|
|
mNeedsBlending(false)
|
|
{
|
|
}
|
|
|
|
LayerBuffer::~LayerBuffer()
|
|
{
|
|
}
|
|
|
|
void LayerBuffer::onFirstRef()
|
|
{
|
|
LayerBaseClient::onFirstRef();
|
|
mSurface = new SurfaceBuffer(mFlinger, clientIndex(),
|
|
const_cast<LayerBuffer *>(this));
|
|
}
|
|
|
|
sp<LayerBaseClient::Surface> LayerBuffer::createSurface() const
|
|
{
|
|
return mSurface;
|
|
}
|
|
|
|
status_t LayerBuffer::ditch()
|
|
{
|
|
mSurface.clear();
|
|
return NO_ERROR;
|
|
}
|
|
|
|
bool LayerBuffer::needsBlending() const {
|
|
return mNeedsBlending;
|
|
}
|
|
|
|
void LayerBuffer::setNeedsBlending(bool blending) {
|
|
mNeedsBlending = blending;
|
|
}
|
|
|
|
void LayerBuffer::postBuffer(ssize_t offset)
|
|
{
|
|
sp<Source> source(getSource());
|
|
if (source != 0)
|
|
source->postBuffer(offset);
|
|
}
|
|
|
|
void LayerBuffer::unregisterBuffers()
|
|
{
|
|
sp<Source> source(clearSource());
|
|
if (source != 0)
|
|
source->unregisterBuffers();
|
|
}
|
|
|
|
uint32_t LayerBuffer::doTransaction(uint32_t flags)
|
|
{
|
|
sp<Source> source(getSource());
|
|
if (source != 0)
|
|
source->onTransaction(flags);
|
|
return LayerBase::doTransaction(flags);
|
|
}
|
|
|
|
void LayerBuffer::unlockPageFlip(const Transform& planeTransform,
|
|
Region& outDirtyRegion)
|
|
{
|
|
// this code-path must be as tight as possible, it's called each time
|
|
// the screen is composited.
|
|
sp<Source> source(getSource());
|
|
if (source != 0)
|
|
source->onVisibilityResolved(planeTransform);
|
|
LayerBase::unlockPageFlip(planeTransform, outDirtyRegion);
|
|
}
|
|
|
|
void LayerBuffer::onDraw(const Region& clip) const
|
|
{
|
|
sp<Source> source(getSource());
|
|
if (LIKELY(source != 0)) {
|
|
source->onDraw(clip);
|
|
} else {
|
|
clearWithOpenGL(clip);
|
|
}
|
|
}
|
|
|
|
bool LayerBuffer::transformed() const
|
|
{
|
|
sp<Source> source(getSource());
|
|
if (LIKELY(source != 0))
|
|
return source->transformed();
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* This creates a "buffer" source for this surface
|
|
*/
|
|
status_t LayerBuffer::registerBuffers(const ISurface::BufferHeap& buffers)
|
|
{
|
|
Mutex::Autolock _l(mLock);
|
|
if (mSource != 0)
|
|
return INVALID_OPERATION;
|
|
|
|
sp<BufferSource> source = new BufferSource(*this, buffers);
|
|
|
|
status_t result = source->getStatus();
|
|
if (result == NO_ERROR) {
|
|
mSource = source;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* This creates an "overlay" source for this surface
|
|
*/
|
|
sp<OverlayRef> LayerBuffer::createOverlay(uint32_t w, uint32_t h, int32_t f)
|
|
{
|
|
sp<OverlayRef> result;
|
|
Mutex::Autolock _l(mLock);
|
|
if (mSource != 0)
|
|
return result;
|
|
|
|
sp<OverlaySource> source = new OverlaySource(*this, &result, w, h, f);
|
|
if (result != 0) {
|
|
mSource = source;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
sp<LayerBuffer::Source> LayerBuffer::getSource() const {
|
|
Mutex::Autolock _l(mLock);
|
|
return mSource;
|
|
}
|
|
|
|
sp<LayerBuffer::Source> LayerBuffer::clearSource() {
|
|
sp<Source> source;
|
|
Mutex::Autolock _l(mLock);
|
|
source = mSource;
|
|
mSource.clear();
|
|
return source;
|
|
}
|
|
|
|
// ============================================================================
|
|
// LayerBuffer::SurfaceBuffer
|
|
// ============================================================================
|
|
|
|
LayerBuffer::SurfaceBuffer::SurfaceBuffer(const sp<SurfaceFlinger>& flinger,
|
|
SurfaceID id, const sp<LayerBuffer>& owner)
|
|
: LayerBaseClient::Surface(flinger, id, owner->getIdentity(), owner)
|
|
{
|
|
}
|
|
|
|
LayerBuffer::SurfaceBuffer::~SurfaceBuffer()
|
|
{
|
|
unregisterBuffers();
|
|
}
|
|
|
|
status_t LayerBuffer::SurfaceBuffer::registerBuffers(
|
|
const ISurface::BufferHeap& buffers)
|
|
{
|
|
sp<LayerBuffer> owner(getOwner());
|
|
if (owner != 0)
|
|
return owner->registerBuffers(buffers);
|
|
return NO_INIT;
|
|
}
|
|
|
|
void LayerBuffer::SurfaceBuffer::postBuffer(ssize_t offset)
|
|
{
|
|
sp<LayerBuffer> owner(getOwner());
|
|
if (owner != 0)
|
|
owner->postBuffer(offset);
|
|
}
|
|
|
|
void LayerBuffer::SurfaceBuffer::unregisterBuffers()
|
|
{
|
|
sp<LayerBuffer> owner(getOwner());
|
|
if (owner != 0)
|
|
owner->unregisterBuffers();
|
|
}
|
|
|
|
sp<OverlayRef> LayerBuffer::SurfaceBuffer::createOverlay(
|
|
uint32_t w, uint32_t h, int32_t format) {
|
|
sp<OverlayRef> result;
|
|
sp<LayerBuffer> owner(getOwner());
|
|
if (owner != 0)
|
|
result = owner->createOverlay(w, h, format);
|
|
return result;
|
|
}
|
|
|
|
// ============================================================================
|
|
// LayerBuffer::Buffer
|
|
// ============================================================================
|
|
|
|
LayerBuffer::Buffer::Buffer(const ISurface::BufferHeap& buffers, ssize_t offset)
|
|
: mBufferHeap(buffers)
|
|
{
|
|
NativeBuffer& src(mNativeBuffer);
|
|
src.crop.l = 0;
|
|
src.crop.t = 0;
|
|
src.crop.r = buffers.w;
|
|
src.crop.b = buffers.h;
|
|
src.img.w = buffers.hor_stride ?: buffers.w;
|
|
src.img.h = buffers.ver_stride ?: buffers.h;
|
|
src.img.format = buffers.format;
|
|
src.img.offset = offset;
|
|
src.img.base = buffers.heap->base();
|
|
src.img.fd = buffers.heap->heapID();
|
|
}
|
|
|
|
LayerBuffer::Buffer::~Buffer()
|
|
{
|
|
}
|
|
|
|
// ============================================================================
|
|
// LayerBuffer::Source
|
|
// LayerBuffer::BufferSource
|
|
// LayerBuffer::OverlaySource
|
|
// ============================================================================
|
|
|
|
LayerBuffer::Source::Source(LayerBuffer& layer)
|
|
: mLayer(layer)
|
|
{
|
|
}
|
|
LayerBuffer::Source::~Source() {
|
|
}
|
|
void LayerBuffer::Source::onDraw(const Region& clip) const {
|
|
}
|
|
void LayerBuffer::Source::onTransaction(uint32_t flags) {
|
|
}
|
|
void LayerBuffer::Source::onVisibilityResolved(
|
|
const Transform& planeTransform) {
|
|
}
|
|
void LayerBuffer::Source::postBuffer(ssize_t offset) {
|
|
}
|
|
void LayerBuffer::Source::unregisterBuffers() {
|
|
}
|
|
bool LayerBuffer::Source::transformed() const {
|
|
return mLayer.mTransformed;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
LayerBuffer::BufferSource::BufferSource(LayerBuffer& layer,
|
|
const ISurface::BufferHeap& buffers)
|
|
: Source(layer), mStatus(NO_ERROR), mBufferSize(0)
|
|
{
|
|
if (buffers.heap == NULL) {
|
|
// this is allowed, but in this case, it is illegal to receive
|
|
// postBuffer(). The surface just erases the framebuffer with
|
|
// fully transparent pixels.
|
|
mBufferHeap = buffers;
|
|
mLayer.setNeedsBlending(false);
|
|
return;
|
|
}
|
|
|
|
status_t err = (buffers.heap->heapID() >= 0) ? NO_ERROR : NO_INIT;
|
|
if (err != NO_ERROR) {
|
|
LOGE("LayerBuffer::BufferSource: invalid heap (%s)", strerror(err));
|
|
mStatus = err;
|
|
return;
|
|
}
|
|
|
|
PixelFormatInfo info;
|
|
err = getPixelFormatInfo(buffers.format, &info);
|
|
if (err != NO_ERROR) {
|
|
LOGE("LayerBuffer::BufferSource: invalid format %d (%s)",
|
|
buffers.format, strerror(err));
|
|
mStatus = err;
|
|
return;
|
|
}
|
|
|
|
if (buffers.hor_stride<0 || buffers.ver_stride<0) {
|
|
LOGE("LayerBuffer::BufferSource: invalid parameters "
|
|
"(w=%d, h=%d, xs=%d, ys=%d)",
|
|
buffers.w, buffers.h, buffers.hor_stride, buffers.ver_stride);
|
|
mStatus = BAD_VALUE;
|
|
return;
|
|
}
|
|
|
|
mBufferHeap = buffers;
|
|
mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0);
|
|
mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride;
|
|
mLayer.forceVisibilityTransaction();
|
|
|
|
}
|
|
|
|
LayerBuffer::BufferSource::~BufferSource()
|
|
{
|
|
if (mTexture.name != -1U) {
|
|
glDeleteTextures(1, &mTexture.name);
|
|
}
|
|
}
|
|
|
|
void LayerBuffer::BufferSource::postBuffer(ssize_t offset)
|
|
{
|
|
ISurface::BufferHeap buffers;
|
|
{ // scope for the lock
|
|
Mutex::Autolock _l(mLock);
|
|
buffers = mBufferHeap;
|
|
if (buffers.heap != 0) {
|
|
const size_t memorySize = buffers.heap->getSize();
|
|
if ((size_t(offset) + mBufferSize) > memorySize) {
|
|
LOGE("LayerBuffer::BufferSource::postBuffer() "
|
|
"invalid buffer (offset=%d, size=%d, heap-size=%d",
|
|
int(offset), int(mBufferSize), int(memorySize));
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
sp<Buffer> buffer;
|
|
if (buffers.heap != 0) {
|
|
buffer = new LayerBuffer::Buffer(buffers, offset);
|
|
if (buffer->getStatus() != NO_ERROR)
|
|
buffer.clear();
|
|
setBuffer(buffer);
|
|
mLayer.invalidate();
|
|
}
|
|
}
|
|
|
|
void LayerBuffer::BufferSource::unregisterBuffers()
|
|
{
|
|
Mutex::Autolock _l(mLock);
|
|
mBufferHeap.heap.clear();
|
|
mBuffer.clear();
|
|
mLayer.invalidate();
|
|
}
|
|
|
|
sp<LayerBuffer::Buffer> LayerBuffer::BufferSource::getBuffer() const
|
|
{
|
|
Mutex::Autolock _l(mLock);
|
|
return mBuffer;
|
|
}
|
|
|
|
void LayerBuffer::BufferSource::setBuffer(const sp<LayerBuffer::Buffer>& buffer)
|
|
{
|
|
Mutex::Autolock _l(mLock);
|
|
mBuffer = buffer;
|
|
}
|
|
|
|
bool LayerBuffer::BufferSource::transformed() const
|
|
{
|
|
return mBufferHeap.transform ? true : Source::transformed();
|
|
}
|
|
|
|
void LayerBuffer::BufferSource::onDraw(const Region& clip) const
|
|
{
|
|
sp<Buffer> ourBuffer(getBuffer());
|
|
if (UNLIKELY(ourBuffer == 0)) {
|
|
// nothing to do, we don't have a buffer
|
|
mLayer.clearWithOpenGL(clip);
|
|
return;
|
|
}
|
|
|
|
const NativeBuffer& src( ourBuffer->getBuffer() );
|
|
|
|
//if (!can_use_copybit || err)
|
|
{
|
|
// OpenGL fall-back
|
|
if (UNLIKELY(mTexture.name == -1LU)) {
|
|
mTexture.name = mLayer.createTexture();
|
|
}
|
|
GLuint w = 0;
|
|
GLuint h = 0;
|
|
GGLSurface t;
|
|
t.version = sizeof(GGLSurface);
|
|
t.width = src.crop.r;
|
|
t.height = src.crop.b;
|
|
t.stride = src.img.w;
|
|
t.vstride= src.img.h;
|
|
t.format = src.img.format;
|
|
t.data = (GGLubyte*)(intptr_t(src.img.base) + src.img.offset);
|
|
const Region dirty(Rect(t.width, t.height));
|
|
mLayer.loadTexture(&mTexture, mTexture.name, dirty, t);
|
|
mTexture.transform = mBufferHeap.transform;
|
|
mLayer.drawWithOpenGL(clip, mTexture);
|
|
}
|
|
}
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer,
|
|
sp<OverlayRef>* overlayRef,
|
|
uint32_t w, uint32_t h, int32_t format)
|
|
: Source(layer), mVisibilityChanged(false),
|
|
mOverlay(0), mOverlayHandle(0), mOverlayDevice(0)
|
|
{
|
|
overlay_control_device_t* overlay_dev = mLayer.mFlinger->getOverlayEngine();
|
|
if (overlay_dev == NULL) {
|
|
// overlays not supported
|
|
return;
|
|
}
|
|
|
|
mOverlayDevice = overlay_dev;
|
|
overlay_t* overlay = overlay_dev->createOverlay(overlay_dev, w, h, format);
|
|
if (overlay == NULL) {
|
|
// couldn't create the overlay (no memory? no more overlays?)
|
|
return;
|
|
}
|
|
|
|
// enable dithering...
|
|
overlay_dev->setParameter(overlay_dev, overlay,
|
|
OVERLAY_DITHER, OVERLAY_ENABLE);
|
|
|
|
mOverlay = overlay;
|
|
mWidth = overlay->w;
|
|
mHeight = overlay->h;
|
|
mFormat = overlay->format;
|
|
mWidthStride = overlay->w_stride;
|
|
mHeightStride = overlay->h_stride;
|
|
|
|
mOverlayHandle = overlay->getHandleRef(overlay);
|
|
|
|
// NOTE: here it's okay to acquire a reference to "this"m as long as
|
|
// the reference is not released before we leave the ctor.
|
|
sp<OverlayChannel> channel = new OverlayChannel(this);
|
|
|
|
*overlayRef = new OverlayRef(mOverlayHandle, channel,
|
|
mWidth, mHeight, mFormat, mWidthStride, mHeightStride);
|
|
}
|
|
|
|
LayerBuffer::OverlaySource::~OverlaySource()
|
|
{
|
|
if (mOverlay && mOverlayDevice) {
|
|
overlay_control_device_t* overlay_dev = mOverlayDevice;
|
|
overlay_dev->destroyOverlay(overlay_dev, mOverlay);
|
|
}
|
|
}
|
|
|
|
void LayerBuffer::OverlaySource::onTransaction(uint32_t flags)
|
|
{
|
|
const Layer::State& front(mLayer.drawingState());
|
|
const Layer::State& temp(mLayer.currentState());
|
|
if (temp.sequence != front.sequence) {
|
|
mVisibilityChanged = true;
|
|
}
|
|
}
|
|
|
|
void LayerBuffer::OverlaySource::onVisibilityResolved(
|
|
const Transform& planeTransform)
|
|
{
|
|
// this code-path must be as tight as possible, it's called each time
|
|
// the screen is composited.
|
|
if (UNLIKELY(mOverlay != 0)) {
|
|
if (mVisibilityChanged) {
|
|
mVisibilityChanged = false;
|
|
const Rect& bounds = mLayer.getTransformedBounds();
|
|
int x = bounds.left;
|
|
int y = bounds.top;
|
|
int w = bounds.width();
|
|
int h = bounds.height();
|
|
|
|
// we need a lock here to protect "destroy"
|
|
Mutex::Autolock _l(mLock);
|
|
if (mOverlay) {
|
|
overlay_control_device_t* overlay_dev = mOverlayDevice;
|
|
overlay_dev->setPosition(overlay_dev, mOverlay, x,y,w,h);
|
|
overlay_dev->setParameter(overlay_dev, mOverlay,
|
|
OVERLAY_TRANSFORM, mLayer.getOrientation());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void LayerBuffer::OverlaySource::serverDestroy()
|
|
{
|
|
mLayer.clearSource();
|
|
destroyOverlay();
|
|
}
|
|
|
|
void LayerBuffer::OverlaySource::destroyOverlay()
|
|
{
|
|
// we need a lock here to protect "onVisibilityResolved"
|
|
Mutex::Autolock _l(mLock);
|
|
if (mOverlay) {
|
|
overlay_control_device_t* overlay_dev = mOverlayDevice;
|
|
overlay_dev->destroyOverlay(overlay_dev, mOverlay);
|
|
mOverlay = 0;
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
}; // namespace android
|