Layer.cpp revision b79f61d41ef053bee1087ec612896c59f95f9686
165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich/*
265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich * Copyright (C) 2007 The Android Open Source Project
365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich *
465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich * Licensed under the Apache License, Version 2.0 (the "License");
565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich * you may not use this file except in compliance with the License.
665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich * You may obtain a copy of the License at
765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich *
865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich *      http://www.apache.org/licenses/LICENSE-2.0
965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich *
1065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich * Unless required by applicable law or agreed to in writing, software
1165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich * distributed under the License is distributed on an "AS IS" BASIS,
1265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich * See the License for the specific language governing permissions and
1465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich * limitations under the License.
1565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich */
1665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
1765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#define ATRACE_TAG ATRACE_TAG_GRAPHICS
1865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
1965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <stdlib.h>
2065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <stdint.h>
2165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <sys/types.h>
2265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
2365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <cutils/compiler.h>
2465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <cutils/native_handle.h>
2565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <cutils/properties.h>
2665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
2765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <utils/Errors.h>
2865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <utils/Log.h>
2965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <utils/StopWatch.h>
3065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <utils/Trace.h>
3165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
3265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <ui/GraphicBuffer.h>
3365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <ui/PixelFormat.h>
3465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
3565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include <gui/Surface.h>
3665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
3765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include "clz.h"
3865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include "DisplayDevice.h"
3965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include "GLExtensions.h"
4065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include "Layer.h"
4165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include "SurfaceFlinger.h"
4265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include "SurfaceTextureLayer.h"
4365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
4465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#include "DisplayHardware/HWComposer.h"
4565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
4665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#define DEBUG_RESIZE    0
4765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
4865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichnamespace android {
4965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
5065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich// ---------------------------------------------------------------------------
5165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
5265de34233da93a3d65c00b8aad3ff9aad44c57deNick KralevichLayer::Layer(SurfaceFlinger* flinger, const sp<Client>& client)
5365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    :   LayerBase(flinger, client),
5465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mTextureName(-1U),
5565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mQueuedFrames(0),
5665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mCurrentTransform(0),
5765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
5865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mCurrentOpacity(true),
5965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mRefreshPending(false),
6065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mFrameLatencyNeeded(false),
6165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mFormat(PIXEL_FORMAT_NONE),
6265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mGLExtensions(GLExtensions::getInstance()),
6365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mOpaqueLayer(true),
6465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mSecure(false),
6565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mProtectedByApp(false)
6665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich{
6765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mCurrentCrop.makeInvalid();
6865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    glGenTextures(1, &mTextureName);
6965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
7065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
7165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichvoid Layer::onLayerDisplayed(const sp<const DisplayDevice>& hw,
7265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        HWComposer::HWCLayerInterface* layer) {
7365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    LayerBase::onLayerDisplayed(hw, layer);
7465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    if (layer) {
7565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFenceFd());
7665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    }
7765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
7865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
7965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichvoid Layer::onFirstRef()
8065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich{
8165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    LayerBase::onFirstRef();
8265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
8365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
8465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    sp<BufferQueue> bq = new SurfaceTextureLayer();
8565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(mTextureName, true,
8665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            GL_TEXTURE_EXTERNAL_OES, false, bq);
8765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
8865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
8965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSurfaceFlingerConsumer->setFrameAvailableListener(this);
9065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSurfaceFlingerConsumer->setSynchronousMode(true);
9165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
9265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
9365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#warning "disabling triple buffering"
9465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSurfaceFlingerConsumer->setDefaultMaxBufferCount(2);
9565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#else
9665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSurfaceFlingerConsumer->setDefaultMaxBufferCount(3);
9765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich#endif
9865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
9965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
10065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    updateTransformHint(hw);
10165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
10265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
10365de34233da93a3d65c00b8aad3ff9aad44c57deNick KralevichLayer::~Layer()
10465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich{
10565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mFlinger->deleteTextureAsync(mTextureName);
10665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
10765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
10865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichvoid Layer::onFrameAvailable() {
10965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    android_atomic_inc(&mQueuedFrames);
11065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mFlinger->signalLayerUpdate();
11165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
11265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
11365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich// called with SurfaceFlinger::mStateLock as soon as the layer is entered
11465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich// in the purgatory list
11565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichvoid Layer::onRemoved()
11665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich{
11765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSurfaceFlingerConsumer->abandon();
11865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
11965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
12065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichvoid Layer::setName(const String8& name) {
12165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    LayerBase::setName(name);
12265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSurfaceFlingerConsumer->setName(name);
12365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
12465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
12565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichsp<ISurface> Layer::createSurface()
12665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich{
12765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    /*
12865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * This class provides an implementation of BnSurface (the "native" or
12965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * "remote" side of the Binder IPC interface ISurface), and mixes in
13065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * LayerCleaner to ensure that mFlinger->onLayerDestroyed() is called for
13165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * this layer when the BSurface is destroyed.
13265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     *
13365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * The idea is to provide a handle to the Layer through ISurface that
13465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * is cleaned up automatically when the last reference to the ISurface
13565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * goes away.  (The references will be held on the "proxy" side, while
13665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * the Layer exists on the "native" side.)
13765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     *
13865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * The Layer has a reference to an instance of SurfaceFlinger's variant
13965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * of GLConsumer, which holds a reference to the BufferQueue.  The
14065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * getSurfaceTexture() call returns a Binder interface reference for
14165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * the producer interface of the buffer queue associated with the Layer.
14265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     */
14365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    class BSurface : public BnSurface, public LayerCleaner {
14465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        wp<const Layer> mOwner;
14565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        virtual sp<IGraphicBufferProducer> getSurfaceTexture() const {
14665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            sp<IGraphicBufferProducer> res;
14765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            sp<const Layer> that( mOwner.promote() );
14865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            if (that != NULL) {
14965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich                res = that->mSurfaceFlingerConsumer->getBufferQueue();
15065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            }
15165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            return res;
15265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        }
15365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    public:
15465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        BSurface(const sp<SurfaceFlinger>& flinger,
15565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich                const sp<Layer>& layer)
15665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            : LayerCleaner(flinger, layer), mOwner(layer) { }
15765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    };
15865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    sp<ISurface> sur(new BSurface(mFlinger, this));
15965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    return sur;
16065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
16165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
16265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichwp<IBinder> Layer::getSurfaceTextureBinder() const
16365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich{
16465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    return mSurfaceFlingerConsumer->getBufferQueue()->asBinder();
16565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
16665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
16765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichstatus_t Layer::setBuffers( uint32_t w, uint32_t h,
16865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich                            PixelFormat format, uint32_t flags)
16965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich{
17065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // this surfaces pixel format
17165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    PixelFormatInfo info;
17265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    status_t err = getPixelFormatInfo(format, &info);
17365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    if (err) {
17465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        ALOGE("unsupported pixelformat %d", format);
17565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        return err;
17665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    }
17765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
17865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    uint32_t const maxSurfaceDims = min(
17965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
18065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
18165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // never allow a surface larger than what our underlying GL implementation
18265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // can handle.
18365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) {
18465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
18565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        return BAD_VALUE;
18665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    }
18765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
18865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mFormat = format;
18965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
19065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSecure = (flags & ISurfaceComposerClient::eSecure) ? true : false;
19165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
19265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mOpaqueLayer = (flags & ISurfaceComposerClient::eOpaque);
19365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mCurrentOpacity = getOpacityForFormat(format);
19465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
19565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
19665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
19765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
19865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
19965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    return NO_ERROR;
20065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
20165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
20265de34233da93a3d65c00b8aad3ff9aad44c57deNick KralevichRect Layer::getContentCrop() const {
20365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // this is the crop rectangle that applies to the buffer
20465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // itself (as opposed to the window)
20565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    Rect crop;
20665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    if (!mCurrentCrop.isEmpty()) {
20765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // if the buffer crop is defined, we use that
20865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        crop = mCurrentCrop;
20965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    } else if (mActiveBuffer != NULL) {
21065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // otherwise we use the whole buffer
21165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        crop = mActiveBuffer->getBounds();
21265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    } else {
21365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // if we don't have a buffer yet, we use an empty/invalid crop
21465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        crop.makeInvalid();
21565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    }
21665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    return crop;
21765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
21865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
21965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichuint32_t Layer::getContentTransform() const {
22065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    return mCurrentTransform;
22165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
22265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
22365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichvoid Layer::setGeometry(
22465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    const sp<const DisplayDevice>& hw,
22565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        HWComposer::HWCLayerInterface& layer)
22665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich{
22765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    LayerBase::setGeometry(hw, layer);
22865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
22965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // enable this layer
23065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    layer.setSkip(false);
23165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
23265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    if (isSecure() && !hw->isSecure()) {
23365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        layer.setSkip(true);
23465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    }
23565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
23665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    const State& s(drawingState());
23765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    layer.setPlaneAlpha(s.alpha);
23865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
23965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    /*
24065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * Transformations are applied in this order:
24165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * 1) buffer orientation/flip/mirror
24265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * 2) state transformation (window manager)
24365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * 3) layer orientation (screen orientation)
24465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     * (NOTE: the matrices are multiplied in reverse order)
24565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich     */
24665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
24765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    const Transform bufferOrientation(mCurrentTransform);
24865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    const Transform tr(hw->getTransform() * s.transform * bufferOrientation);
24965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
25065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // this gives us only the "orientation" component of the transform
25165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    const uint32_t finalTransform = tr.getOrientation();
25265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
25365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // we can only handle simple transformation
25465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    if (finalTransform & Transform::ROT_INVALID) {
25565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        layer.setSkip(true);
25665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    } else {
25765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        layer.setTransform(finalTransform);
25865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    }
25965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
26065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
26165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichvoid Layer::setPerFrameData(const sp<const DisplayDevice>& hw,
26265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        HWComposer::HWCLayerInterface& layer) {
26365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    LayerBase::setPerFrameData(hw, layer);
26465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // NOTE: buffer can be NULL if the client never drew into this
26565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // layer yet, or if we ran out of memory
26665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    layer.setBuffer(mActiveBuffer);
26765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
26865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
26965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichvoid Layer::setAcquireFence(const sp<const DisplayDevice>& hw,
27065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        HWComposer::HWCLayerInterface& layer) {
27165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    int fenceFd = -1;
27265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
27365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // TODO: there is a possible optimization here: we only need to set the
27465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // acquire fence the first time a new buffer is acquired on EACH display.
27565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
27665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    if (layer.getCompositionType() == HWC_OVERLAY) {
27765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence();
27865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        if (fence->isValid()) {
27965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            fenceFd = fence->dup();
28065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            if (fenceFd == -1) {
28165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich                ALOGW("failed to dup layer fence, skipping sync: %d", errno);
28265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            }
28365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        }
28465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    }
28565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    layer.setAcquireFenceFd(fenceFd);
28665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich}
28765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
28865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevichvoid Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
28965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich{
29065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    ATRACE_CALL();
29165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
29265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    if (CC_UNLIKELY(mActiveBuffer == 0)) {
29365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // the texture has not been created yet, this Layer has
29465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // in fact never been drawn into. This happens frequently with
29565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // SurfaceView because the WindowManager can't know when the client
29665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // has drawn the first time.
29765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
29865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // If there is nothing under us, we paint the screen in black, otherwise
29965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // we just skip this update.
30065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
30165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // figure out if there is something below us
30265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        Region under;
30365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        const SurfaceFlinger::LayerVector& drawingLayers(
30465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich                mFlinger->mDrawingState.layersSortedByZ);
30565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        const size_t count = drawingLayers.size();
30665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        for (size_t i=0 ; i<count ; ++i) {
30765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            const sp<LayerBase>& layer(drawingLayers[i]);
30865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            if (layer.get() == static_cast<LayerBase const*>(this))
30965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich                break;
31065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            under.orSelf( hw->getTransform().transform(layer->visibleRegion) );
31165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        }
31265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // if not everything below us is covered, we plug the holes!
31365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        Region holes(clip.subtract(under));
31465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        if (!holes.isEmpty()) {
31565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            clearWithOpenGL(hw, holes, 0, 0, 0, 1);
31665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        }
31765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        return;
31865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    }
31965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
32065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // Bind the current buffer to the GL texture, and wait for it to be
32165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    // ready for us to draw into.
32265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    status_t err = mSurfaceFlingerConsumer->bindTextureImage();
32365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    if (err != NO_ERROR) {
32465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
32565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // Go ahead and draw the buffer anyway; no matter what we do the screen
32665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // is probably going to have something visibly wrong.
32765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    }
32865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
32965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure());
33065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
33165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich    if (!blackOutLayer) {
33265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // TODO: we could be more subtle with isFixedSize()
33365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize();
33465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
33565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // Query the texture matrix given our current filtering mode.
33665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        float textureMatrix[16];
33765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
33865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
33965de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich
34065de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        // Set things up for texturing.
34165de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureName);
34265de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        GLenum filter = GL_NEAREST;
34365de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        if (useFiltering) {
34465de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich            filter = GL_LINEAR;
34565de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        }
34665de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, filter);
34765de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, filter);
34865de34233da93a3d65c00b8aad3ff9aad44c57deNick Kralevich        glMatrixMode(GL_TEXTURE);
349        glLoadMatrixf(textureMatrix);
350        glMatrixMode(GL_MODELVIEW);
351        glDisable(GL_TEXTURE_2D);
352        glEnable(GL_TEXTURE_EXTERNAL_OES);
353    } else {
354        glBindTexture(GL_TEXTURE_2D, mFlinger->getProtectedTexName());
355        glMatrixMode(GL_TEXTURE);
356        glLoadIdentity();
357        glMatrixMode(GL_MODELVIEW);
358        glDisable(GL_TEXTURE_EXTERNAL_OES);
359        glEnable(GL_TEXTURE_2D);
360    }
361
362    drawWithOpenGL(hw, clip);
363
364    glDisable(GL_TEXTURE_EXTERNAL_OES);
365    glDisable(GL_TEXTURE_2D);
366}
367
368// As documented in libhardware header, formats in the range
369// 0x100 - 0x1FF are specific to the HAL implementation, and
370// are known to have no alpha channel
371// TODO: move definition for device-specific range into
372// hardware.h, instead of using hard-coded values here.
373#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
374
375bool Layer::getOpacityForFormat(uint32_t format)
376{
377    if (HARDWARE_IS_DEVICE_FORMAT(format)) {
378        return true;
379    }
380    PixelFormatInfo info;
381    status_t err = getPixelFormatInfo(PixelFormat(format), &info);
382    // in case of error (unknown format), we assume no blending
383    return (err || info.h_alpha <= info.l_alpha);
384}
385
386
387bool Layer::isOpaque() const
388{
389    // if we don't have a buffer yet, we're translucent regardless of the
390    // layer's opaque flag.
391    if (mActiveBuffer == 0) {
392        return false;
393    }
394
395    // if the layer has the opaque flag, then we're always opaque,
396    // otherwise we use the current buffer's format.
397    return mOpaqueLayer || mCurrentOpacity;
398}
399
400bool Layer::isProtected() const
401{
402    const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
403    return (activeBuffer != 0) &&
404            (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
405}
406
407uint32_t Layer::doTransaction(uint32_t flags)
408{
409    ATRACE_CALL();
410
411    const Layer::State& front(drawingState());
412    const Layer::State& temp(currentState());
413
414    const bool sizeChanged = (temp.requested.w != front.requested.w) ||
415                             (temp.requested.h != front.requested.h);
416
417    if (sizeChanged) {
418        // the size changed, we need to ask our client to request a new buffer
419        ALOGD_IF(DEBUG_RESIZE,
420                "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
421                "  current={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
422                "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n"
423                "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
424                "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
425                this, (const char*) getName(), mCurrentTransform, mCurrentScalingMode,
426                temp.active.w, temp.active.h,
427                temp.active.crop.left,
428                temp.active.crop.top,
429                temp.active.crop.right,
430                temp.active.crop.bottom,
431                temp.active.crop.getWidth(),
432                temp.active.crop.getHeight(),
433                temp.requested.w, temp.requested.h,
434                temp.requested.crop.left,
435                temp.requested.crop.top,
436                temp.requested.crop.right,
437                temp.requested.crop.bottom,
438                temp.requested.crop.getWidth(),
439                temp.requested.crop.getHeight(),
440                front.active.w, front.active.h,
441                front.active.crop.left,
442                front.active.crop.top,
443                front.active.crop.right,
444                front.active.crop.bottom,
445                front.active.crop.getWidth(),
446                front.active.crop.getHeight(),
447                front.requested.w, front.requested.h,
448                front.requested.crop.left,
449                front.requested.crop.top,
450                front.requested.crop.right,
451                front.requested.crop.bottom,
452                front.requested.crop.getWidth(),
453                front.requested.crop.getHeight());
454
455        // record the new size, form this point on, when the client request
456        // a buffer, it'll get the new size.
457        mSurfaceFlingerConsumer->setDefaultBufferSize(
458                temp.requested.w, temp.requested.h);
459    }
460
461    if (!isFixedSize()) {
462
463        const bool resizePending = (temp.requested.w != temp.active.w) ||
464                                   (temp.requested.h != temp.active.h);
465
466        if (resizePending) {
467            // don't let LayerBase::doTransaction update the drawing state
468            // if we have a pending resize, unless we are in fixed-size mode.
469            // the drawing state will be updated only once we receive a buffer
470            // with the correct size.
471            //
472            // in particular, we want to make sure the clip (which is part
473            // of the geometry state) is latched together with the size but is
474            // latched immediately when no resizing is involved.
475
476            flags |= eDontUpdateGeometryState;
477        }
478    }
479
480    return LayerBase::doTransaction(flags);
481}
482
483bool Layer::isFixedSize() const {
484    return mCurrentScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
485}
486
487bool Layer::isCropped() const {
488    return !mCurrentCrop.isEmpty();
489}
490
491// ----------------------------------------------------------------------------
492// pageflip handling...
493// ----------------------------------------------------------------------------
494
495bool Layer::onPreComposition() {
496    mRefreshPending = false;
497    return mQueuedFrames > 0;
498}
499
500void Layer::onPostComposition() {
501    if (mFrameLatencyNeeded) {
502        nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
503        mFrameTracker.setDesiredPresentTime(desiredPresentTime);
504
505        sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence();
506        if (frameReadyFence->isValid()) {
507            mFrameTracker.setFrameReadyFence(frameReadyFence);
508        } else {
509            // There was no fence for this frame, so assume that it was ready
510            // to be presented at the desired present time.
511            mFrameTracker.setFrameReadyTime(desiredPresentTime);
512        }
513
514        const HWComposer& hwc = mFlinger->getHwComposer();
515        sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
516        if (presentFence->isValid()) {
517            mFrameTracker.setActualPresentFence(presentFence);
518        } else {
519            // The HWC doesn't support present fences, so use the refresh
520            // timestamp instead.
521            nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
522            mFrameTracker.setActualPresentTime(presentTime);
523        }
524
525        mFrameTracker.advanceFrame();
526        mFrameLatencyNeeded = false;
527    }
528}
529
530bool Layer::isVisible() const {
531    return LayerBase::isVisible() && (mActiveBuffer != NULL);
532}
533
534Region Layer::latchBuffer(bool& recomputeVisibleRegions)
535{
536    ATRACE_CALL();
537
538    Region outDirtyRegion;
539    if (mQueuedFrames > 0) {
540
541        // if we've already called updateTexImage() without going through
542        // a composition step, we have to skip this layer at this point
543        // because we cannot call updateTeximage() without a corresponding
544        // compositionComplete() call.
545        // we'll trigger an update in onPreComposition().
546        if (mRefreshPending) {
547            return outDirtyRegion;
548        }
549
550        // Capture the old state of the layer for comparisons later
551        const bool oldOpacity = isOpaque();
552        sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
553
554        // signal another event if we have more frames pending
555        if (android_atomic_dec(&mQueuedFrames) > 1) {
556            mFlinger->signalLayerUpdate();
557        }
558
559        struct Reject : public SurfaceFlingerConsumer::BufferRejecter {
560            Layer::State& front;
561            Layer::State& current;
562            bool& recomputeVisibleRegions;
563            Reject(Layer::State& front, Layer::State& current,
564                    bool& recomputeVisibleRegions)
565                : front(front), current(current),
566                  recomputeVisibleRegions(recomputeVisibleRegions) {
567            }
568
569            virtual bool reject(const sp<GraphicBuffer>& buf,
570                    const BufferQueue::BufferItem& item) {
571                if (buf == NULL) {
572                    return false;
573                }
574
575                uint32_t bufWidth  = buf->getWidth();
576                uint32_t bufHeight = buf->getHeight();
577
578                // check that we received a buffer of the right size
579                // (Take the buffer's orientation into account)
580                if (item.mTransform & Transform::ROT_90) {
581                    swap(bufWidth, bufHeight);
582                }
583
584
585                bool isFixedSize = item.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE;
586                if (front.active != front.requested) {
587
588                    if (isFixedSize ||
589                            (bufWidth == front.requested.w &&
590                             bufHeight == front.requested.h))
591                    {
592                        // Here we pretend the transaction happened by updating the
593                        // current and drawing states. Drawing state is only accessed
594                        // in this thread, no need to have it locked
595                        front.active = front.requested;
596
597                        // We also need to update the current state so that
598                        // we don't end-up overwriting the drawing state with
599                        // this stale current state during the next transaction
600                        //
601                        // NOTE: We don't need to hold the transaction lock here
602                        // because State::active is only accessed from this thread.
603                        current.active = front.active;
604
605                        // recompute visible region
606                        recomputeVisibleRegions = true;
607                    }
608
609                    ALOGD_IF(DEBUG_RESIZE,
610                            "latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n"
611                            "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
612                            "            requested={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }}\n",
613                            bufWidth, bufHeight, item.mTransform, item.mScalingMode,
614                            front.active.w, front.active.h,
615                            front.active.crop.left,
616                            front.active.crop.top,
617                            front.active.crop.right,
618                            front.active.crop.bottom,
619                            front.active.crop.getWidth(),
620                            front.active.crop.getHeight(),
621                            front.requested.w, front.requested.h,
622                            front.requested.crop.left,
623                            front.requested.crop.top,
624                            front.requested.crop.right,
625                            front.requested.crop.bottom,
626                            front.requested.crop.getWidth(),
627                            front.requested.crop.getHeight());
628                }
629
630                if (!isFixedSize) {
631                    if (front.active.w != bufWidth ||
632                        front.active.h != bufHeight) {
633                        // reject this buffer
634                        return true;
635                    }
636                }
637                return false;
638            }
639        };
640
641
642        Reject r(mDrawingState, currentState(), recomputeVisibleRegions);
643
644        if (mSurfaceFlingerConsumer->updateTexImage(&r) != NO_ERROR) {
645            // something happened!
646            recomputeVisibleRegions = true;
647            return outDirtyRegion;
648        }
649
650        // update the active buffer
651        mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer();
652        if (mActiveBuffer == NULL) {
653            // this can only happen if the very first buffer was rejected.
654            return outDirtyRegion;
655        }
656
657        mRefreshPending = true;
658        mFrameLatencyNeeded = true;
659        if (oldActiveBuffer == NULL) {
660             // the first time we receive a buffer, we need to trigger a
661             // geometry invalidation.
662            recomputeVisibleRegions = true;
663         }
664
665        Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
666        const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
667        const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
668        if ((crop != mCurrentCrop) ||
669            (transform != mCurrentTransform) ||
670            (scalingMode != mCurrentScalingMode))
671        {
672            mCurrentCrop = crop;
673            mCurrentTransform = transform;
674            mCurrentScalingMode = scalingMode;
675            recomputeVisibleRegions = true;
676        }
677
678        if (oldActiveBuffer != NULL) {
679            uint32_t bufWidth  = mActiveBuffer->getWidth();
680            uint32_t bufHeight = mActiveBuffer->getHeight();
681            if (bufWidth != uint32_t(oldActiveBuffer->width) ||
682                bufHeight != uint32_t(oldActiveBuffer->height)) {
683                recomputeVisibleRegions = true;
684            }
685        }
686
687        mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
688        if (oldOpacity != isOpaque()) {
689            recomputeVisibleRegions = true;
690        }
691
692        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
693        glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
694
695        // FIXME: postedRegion should be dirty & bounds
696        const Layer::State& front(drawingState());
697        Region dirtyRegion(Rect(front.active.w, front.active.h));
698
699        // transform the dirty region to window-manager space
700        outDirtyRegion = (front.transform.transform(dirtyRegion));
701    }
702    return outDirtyRegion;
703}
704
705void Layer::dump(String8& result, char* buffer, size_t SIZE) const
706{
707    LayerBase::dump(result, buffer, SIZE);
708
709    sp<const GraphicBuffer> buf0(mActiveBuffer);
710    uint32_t w0=0, h0=0, s0=0, f0=0;
711    if (buf0 != 0) {
712        w0 = buf0->getWidth();
713        h0 = buf0->getHeight();
714        s0 = buf0->getStride();
715        f0 = buf0->format;
716    }
717    snprintf(buffer, SIZE,
718            "      "
719            "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X],"
720            " queued-frames=%d, mRefreshPending=%d\n",
721            mFormat, w0, h0, s0,f0,
722            mQueuedFrames, mRefreshPending);
723
724    result.append(buffer);
725
726    if (mSurfaceFlingerConsumer != 0) {
727        mSurfaceFlingerConsumer->dump(result, "            ", buffer, SIZE);
728    }
729}
730
731void Layer::dumpStats(String8& result, char* buffer, size_t SIZE) const
732{
733    LayerBase::dumpStats(result, buffer, SIZE);
734    mFrameTracker.dump(result);
735}
736
737void Layer::clearStats()
738{
739    LayerBase::clearStats();
740    mFrameTracker.clear();
741}
742
743uint32_t Layer::getEffectiveUsage(uint32_t usage) const
744{
745    // TODO: should we do something special if mSecure is set?
746    if (mProtectedByApp) {
747        // need a hardware-protected path to external video sink
748        usage |= GraphicBuffer::USAGE_PROTECTED;
749    }
750    usage |= GraphicBuffer::USAGE_HW_COMPOSER;
751    return usage;
752}
753
754void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
755    uint32_t orientation = 0;
756    if (!mFlinger->mDebugDisableTransformHint) {
757        // The transform hint is used to improve performance, but we can
758        // only have a single transform hint, it cannot
759        // apply to all displays.
760        const Transform& planeTransform(hw->getTransform());
761        orientation = planeTransform.getOrientation();
762        if (orientation & Transform::ROT_INVALID) {
763            orientation = 0;
764        }
765    }
766    mSurfaceFlingerConsumer->setTransformHint(orientation);
767}
768
769// ---------------------------------------------------------------------------
770
771
772}; // namespace android
773